Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How can extend account_analytic_default module with product categories??

By
Bonnet Denis
on 7/18/13, 2:18 PM 1,406 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])]

0

Bonnet Denis

--Bonnet Denis--
925
| 5 6 8
Vannes, France
--Bonnet Denis--

Engeneer at MGS Consulting Ouest at Vannes, FRANCE

Bonnet Denis
On 7/19/13, 11:14 AM

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>

Thanks for this!

Tekse Arpad
on 1/23/14, 4:26 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 7/18/13, 2:18 PM
Seen: 1406 times
Last updated: 3/16/15, 8:10 AM