This question has been flagged
1 Reply
5047 Views

I work on account_analytic_default module on OEv7.0. And I try to add a new field that valid the rule if the product category match or if this is a child of the rule category.

I have a form with a product_category field. It's a many2one field to product.category objects. this is the function that get the better rule for this invoice line.

domain = []
    if product_id:
        domain += ['|', ('product_id', '=', product_id)]
    domain += [('product_id','=', False)]
    if partner_id:
        domain += ['|', ('partner_id', '=', partner_id)]
    domain += [('partner_id', '=', False)]
    if user_id:
        domain += ['|',('user_id', '=', user_id)]
    domain += [('user_id','=', False)]
    if date:
        domain += ['|', ('date_start', '<=', date), ('date_start', '=', False)]
        domain += ['|', ('date_stop', '>=', date), ('date_stop', '=', False)]
    best_index = -1
    res = False
    for rec in self.browse(cr, uid, self.search(cr, uid, domain, context=context), context=context):
        index = 0
        if rec.product_id: index += 1
        if rec.partner_id: index += 1
        if rec.user_id: index += 1
        if rec.date_start: index += 1
        if rec.date_stop: index += 1
        if index > best_index:
                res = rec
                best_index = index
    return res

I think about a piece of code like that:

if product_categ_id:
     domain += ['|', ('product_categ_id', 'parent_of', product_categ_id)]
domain += [('product_categ_id','=', False)]

Or course I'll created a new field product_categ_id and the value of domain will be the product category.

But 'parent_of' doesn't exist and I don't know how to do something like this.

edit: I've seen something like domain=[('product.categ_id','in',[list of child of something])]

Avatar
Discard
Author Best Answer

I've made an extension of the module :

import time
from openerp.osv import fields, osv
from openerp.tools.translate import _

class account_analytic_default_juliana(osv.osv):
    _name = "account.analytic.default"
    _inherit = "account.analytic.default"
    _columns = {
        'product_categ_id': fields.many2one('product.category', 'Catégorie d\'article', ondelete='cascade', help="Select a category product which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select a product from this category, it will automatically take this as an analytic account)")
    }

    def account_get(self, cr, uid, product_id=None, partner_id=None, user_id=None, date=None, context=None):
        domain = []
        if product_id:
            product_model = self.pool.get('product.product')
            product_object = product_model.browse(cr, uid, product_id, context=context)
            categ=product_object.categ_id
            domain += ['|','&', ('product_categ_id.parent_left', '<=', categ.parent_left),('product_categ_id.parent_right', '>=', categ.parent_right)]
        domain += [('product_categ_id','=', False)]
        if product_id:
            domain += ['|', ('product_id', '=', product_id)]
        domain += [('product_id','=', False)]
        if partner_id:
            domain += ['|', ('partner_id', '=', partner_id)]
        domain += [('partner_id', '=', False)]
        if user_id:
            domain += ['|',('user_id', '=', user_id)]
        domain += [('user_id','=', False)]
        if date:
            domain += ['|', ('date_start', '<=', date), ('date_start', '=', False)]
            domain += ['|', ('date_stop', '>=', date), ('date_stop', '=', False)]
        best_index = -1
        res = False
        for rec in self.browse(cr, uid, self.search(cr, uid, domain, context=context), context=context):
            index = 0
            if rec.product_id: index += 1
            if rec.partner_id: index += 1
            if rec.user_id: index += 1
            if rec.date_start: index += 1
            if rec.date_stop: index += 1
            if index > best_index:
                res = rec
                best_index = index
        return res

account_analytic_default_juliana()

and the view:

 <openerp>
    <data>
        <record id="view_account_analytic_default_form_Juliana" model="ir.ui.view">
            <field name="name">account.analytic.default.form.juliana</field>
            <field name="model">account.analytic.default</field>
            <field name="inherit_id" ref="account_analytic_default.view_account_analytic_default_form"/>
            <field name="arch" type="xml">
                <field name="product_id" position="after">
                    <field name="product_categ_id"/>
                </field>
            </field>
        </record>
        <record id="view_account_analytic_default_tree_Juliana" model="ir.ui.view">
            <field name="name">account.analytic.default.tree.juliana</field>
            <field name="model">account.analytic.default</field>
            <field name="inherit_id" ref="account_analytic_default.view_account_analytic_default_tree"/>
            <field name="arch" type="xml">
                <field name="product_id" position="after">
                    <field name="product_categ_id"/>
                </field>
            </field>
        </record>
    </data>
</openerp>
Avatar
Discard

Thanks for this!