Comment 172 for bug 1160365

Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote : Re: [Bug 1160365] Re: [7.0] incorrect handling of contact/companies for invoicing and related purposes

On 04/15/2013 04:09 PM, Mario Arias wrote:
> Again, could you please explain how are you handling encapsulation of
> your "denormalized" contacts proposal ??

The bulk of the idea is in the description of this bug ("Proposed solution")
and the actual implementation can be reviewed on the framework merge proposal:
https://code.launchpad.net/~openerp-dev/openobject-server/7.0-fix-contact-company-handling/+merge/157577

> * How are you copying data/attributes from companies to contacts?

This is part B of the described solution.

Since business contact and companies live in the same database table, they both
have all accounting attributes at SQL level. In the UI we forbid editing the
accounting attributes on business contacts and replace the fields with a link
to edit these attributes on the parent company.

On the database side, in order to avoid keeping empty (or worse: incorrect)
values in the accounting fields of business contacts, we implement automated
inheritance of the values (similarly to what fields.related would do). Whenever
these accounting attributes are modified on the company, the values are
automatically synchronized to the children. Same thing if the "contact-company"
link changes.

> * How is this being encapsulated/hidden from addons?

This logic is completely implemented by the res.partner model in the framework.
Addons can extend the list of fields that are considered "accounting
atttributes" and therefore automatically synchronized by overriding the
res.partner._commercial_fields() method.

The merge proposal for addons does this for the accounting attributes of all
official addons, if you want to get an idea:
https://code.launchpad.net/~openerp-dev/openobject-addons/7.0-fix-contact-company-handling/+merge/157576

> * How is this better than a relation from contact to company?

It's not better, it comes *in addition* to the contact-company relationship as
an extra security. Addons that do care about the contact vs company distinction
can directly access the correct value on the company.
But if some fail to do it properly they will still get the right value, be it
payment term, payable account, etc.

> * How should we proceed if we need to add any aditional field from company to contact?

If your module adds an accounting-related field 'special_invoice_data_id' to
res.partner, just override the method as follows and synchronization will be
done automatically:

def _commercial_fields(self, cr, uid, context=None):
   return super(res_partner, self)._commercial_fields(cr, uid, context=context)\
       + ['special_invoice_data_id']

If your field is normally editable in the "Accounting" tab of the Partner form
you have nothing else to do: it will be replaced by a link to the company form
when viewing a contact of a company. If not, have a look at the examples in the
addons merge proposal, a simple `attrs` attribute will do the job, and you can
easily add a link to open the company form if you want.