Trunk Multi Company Improvement

Bug #485058 reported by Fabien (Open ERP)
10
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Odoo Addons (MOVED TO GITHUB)
Fix Released
Undecided
Unassigned

Bug Description

Specifications to Improve Multi-Company in Trunk
=======================================

These specs details security issues for the multi-company only. Others issues (sales -> purchase, outgoing -> incoming) are detailed in others specifications. The security for the multi-company is based on the following concept:
  - multi-company objects have a field called 'company_id': fields.many2one('res.company', 'Company')
    -> sometimes required, sometimes not. Sometimes with a default value.
  - records are based on record rules that filter objects based on this field.

Modifications in the client and the server so that a user belongs to several companies have already been applied.

Merge Modules
-------------------

We do not need multi-company modules anymore. Every module most be multi-company by default.
In trunk, we must merge multi_company_XXX modules in their respective modules, put directly the
company_id field on the object, no inheritancy of object/view anymore.

Each time you put a company_id field on an object, do:

* A default value (see bellow for more info):
   'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, _name, c)
I don't know if _name will work. Otherwise put the name of the object 'account.invoice'

* Some multi_company fields must be related fields. For example, in sale.order.line:
  'company_id': fields.related('order_id','company_id',type='many2one',object='res.company',string='Company')
Do the same for:
  - purchase.order.line
  - account.invoice.line
  - task.work.line

* A record rule like this one for this object:
        <record id="journal_comp_rule_group" model="ir.rule.group">
             <field name="name">Journal multi-company (Children+Parents)</field>
             <field model="ir.model" ref="model_account_journal"/>
             <field eval="True" name="global"/>
         </record>
        <record id="journal_comp_rule" model="ir.rule">
             <field model="ir.model.fields" ref="field_account_journal_company_id"/>
             <field name="domain_force">['|','|',('company_id','=',False),('company_id.child_ids','child_of',[user.company_id.id]),('company_id','child_of',[user.company_id.id])]</field>
             <field name="rule_group" ref="journal_comp_rule_group"/>
         </record>

This rule must be put on all generic objects (partners, products, ...)
For more sensible objects (like all accounting and sales objects), use a rule like this one:
        <record id="journal_comp_rule_group" model="ir.rule.group">
             <field name="name">Journal multi-company (Children)</field>
             <field model="ir.model" ref="model_account_journal"/>
             <field eval="True" name="global"/>
         </record>
         <record id="journal_comp_rule" model="ir.rule">
             <field model="ir.model.fields" ref="field_account_journal_company_id"/>
             <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
             <field name="rule_group" ref="journal_comp_rule_group"/>
         </record>

  -> The way record rules were defined in old multi-company modules was wrong, change all rules to be like the above one.

On the view, add a group on all company_id field, so that users belonging to a single company have a way to hide
all multi-company fields:

   <field name="company_id" groups="base.multi_company"/>

The multi_company module
-----------------------------------

Develop a multi_company module (or improve the existing one). It has no code nor security rules at all.
It just define demonstration data with a several companies and assign some companies to some products, partners, ...

The default value system
--------------------------------

Put this in the base module, on res.company object:
+ def _company_default_get(self, cr, uid, object=False, context={}):
+ return self.pool.get('res.users').browse(cr, uid, uid).company_id.id

Put this in multi_company module:
+class multi_company_default(osv.osv):
+ _name = 'multi_company.default'
+ _order = 'sequence,id'
+ _columns = {
+ 'sequence': fields.integer('Sequence'),
+ 'name': fields.char('Name', size=32, required=True),
+ 'company_id': fields.many2one('res.company', 'Main Company', required=True),
+ 'company_dest_id': fields.many2one('res.company', 'Default Company', required=True),
+ 'object_id': fields.many2one('ir.model', 'Object', required=True),
+ 'expression': fields.char('Expression', required=True),
+ }
+ _defaults = {
+ 'expression': lambda *a: 'True',
+ 'sequence': lambda *a: 1
+ }
+multi_company_default()
+
+class res_company(osv.osv):
+ _inherit = 'res.company'
+ def _company_default_get(self, cr, uid, object=False, context={}):
+ proxy = self.pool.get('multi_company.default')
+ ids = proxy.search(cr, uid, [('object_id.name', '=', object)]
+ for rule in proxy.browse(cr, uid, ids, context):
+ user = self.pool.get('res.user').browse(cr, uid, uid)
+ if eval(rule.expression, {'context': context, 'user': user}):
+ return rule.company_dest_id.id
+ return super(res_company, self)._company_default_get(cr, uid, object, context)
+res_company()

Define a menu and a view to configure the multi_company.default object.

Put a security rule a multi_company security rule on this object too.

Revision history for this message
Fabien (Open ERP) (fp-tinyerp) wrote :

vra, can you let me know when this task is merged in to the trunk branch.
Thanks,

Changed in openobject-addons:
milestone: none → 2009-11
assignee: nobody → vra (openerp) (vra-openerp)
status: New → Confirmed
Revision history for this message
Joël Grand-Guillaume @ camptocamp (jgrandguillaume-c2c) wrote :

Hi !

This is good news :) ! I think it's really a good idea to do that this way... no more multi-company module needed and a better rules definition.

I'm asking myself about this blueprint : https://blueprints.launchpad.net/openobject-addons/+spec/support-multi-currency-cost

Shouldn't we include this into this "bug report" ? I summarize the problematics here :

Today, we're not able to make stock move with accounting part in multi-currency. This is because all product are recorded in only one currency. Also, in a multi-company context, all product are define in one currency when sharing them.. For example into account analytic lines, the cost price is always in company currency which is wrong (2 compagnies : EUR & USD => Not the same cost price...)

And my suggested solution:

To handle this feature, we should (just a start, not exhaustive):

- Add a currency field on product
- Add a new price list type : "company cost"
- Add a price iist on the company to handle cost computation

- Change the price_get function into pricelist.py to take care of the product currency
- Change the computation of the cost stored into account analytic line, especially when recording costs from timesheet lines. This should take care of product cost price with his currency and the new "company cost" price list rules/computation.
- Change the way accounting move related to stock move are done in both "PMP" and "Standard price" valuation types.

What do you think about Fabien ? I remember we already discuss that with qdp...

Regards,

Changed in openobject-addons:
status: Confirmed → In Progress
Changed in openobject-addons:
status: In Progress → Fix Committed
Revision history for this message
Joël Grand-Guillaume @ camptocamp (jgrandguillaume-c2c) wrote :

@ vra

Hi !

I saw you committed something about his bug report, is this on the official trunk branch right now ?

Thanks for your advise.

Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) wrote :

Hello Joël Grand-Guillaume @ CampToCamp,

Please follow the following link where the multi company related changes have been committed and few development are still in progress :

https://code.launchpad.net/~openerp/openobject-addons/trunk

Thanks.

Revision history for this message
Martin Collins (mkc-steadfast) wrote :

[6.0rc1] Shouldn't addons/base/res/res_company_view.xml be referenced in addons/base/__openerp__.py?
The multi-company menu is not visible until I add it and reload.

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Multi-company facility has been handled well on trunk.
If you find a bug, please post a new thread.
Thanks.

Changed in openobject-addons:
milestone: 2009-11 → none
assignee: vra (openerp) (vra-openerp) → nobody
status: Fix Committed → Fix Released
milestone: none → 6.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.