[MERGE] Migration of account_analytic_default to new API
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import account_analytic_default
|
||||
import models
|
||||
|
||||
@@ -20,11 +20,10 @@ Allows to automatically select analytic accounts based on criterions:
|
||||
'website': 'https://www.odoo.com/page/accounting',
|
||||
'depends': ['sale_stock'],
|
||||
'data': [
|
||||
'security/ir.model.access.csv',
|
||||
'security/account_analytic_default_security.xml',
|
||||
'account_analytic_default_view.xml'
|
||||
'security/ir.model.access.csv',
|
||||
'security/account_analytic_default_security.xml',
|
||||
'views/account_analytic_default_view.xml',
|
||||
'views/product_views.xml'
|
||||
],
|
||||
'demo': [],
|
||||
'installable': True,
|
||||
'auto_install': False,
|
||||
}
|
||||
|
||||
@@ -1,146 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import time
|
||||
|
||||
from openerp.osv import fields, osv
|
||||
from openerp import api
|
||||
|
||||
class account_analytic_default(osv.osv):
|
||||
_name = "account.analytic.default"
|
||||
_description = "Analytic Distribution"
|
||||
_rec_name = "analytic_id"
|
||||
_order = "sequence"
|
||||
_columns = {
|
||||
'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of analytic distribution"),
|
||||
'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account', domain=[('account_type', '=', 'normal')]),
|
||||
'product_id': fields.many2one('product.product', 'Product', ondelete='cascade', help="Select a product which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this product, it will automatically take this as an analytic account)"),
|
||||
'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade', help="Select a partner which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this partner, it will automatically take this as an analytic account)"),
|
||||
'user_id': fields.many2one('res.users', 'User', ondelete='cascade', help="Select a user which will use analytic account specified in analytic default."),
|
||||
'company_id': fields.many2one('res.company', 'Company', ondelete='cascade', help="Select a company which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this company, it will automatically take this as an analytic account)"),
|
||||
'date_start': fields.date('Start Date', help="Default start date for this Analytic Account."),
|
||||
'date_stop': fields.date('End Date', help="Default end date for this Analytic Account."),
|
||||
}
|
||||
|
||||
def account_get(self, cr, uid, product_id=None, partner_id=None, user_id=None, date=None, company_id=None, context=None):
|
||||
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 company_id:
|
||||
domain += ['|', ('company_id', '=', company_id)]
|
||||
domain += [('company_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.company_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
|
||||
|
||||
|
||||
class account_invoice_line(osv.osv):
|
||||
_inherit = "account.invoice.line"
|
||||
_description = "Invoice Line"
|
||||
|
||||
@api.onchange('product_id')
|
||||
def _onchange_product_id(self):
|
||||
res = super(account_invoice_line, self)._onchange_product_id()
|
||||
rec = self.env['account.analytic.default'].account_get(self.product_id.id, self.invoice_id.partner_id.id, self._uid,
|
||||
time.strftime('%Y-%m-%d'), company_id=self.company_id.id, context=self._context)
|
||||
if rec:
|
||||
self.account_analytic_id = rec.analytic_id.id
|
||||
else:
|
||||
self.account_analytic_id = False
|
||||
return res
|
||||
|
||||
|
||||
class stock_picking(osv.osv):
|
||||
_inherit = "stock.picking"
|
||||
|
||||
def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
|
||||
partner_id = picking.partner_id and picking.partner_id.id or False
|
||||
rec = self.pool.get('account.analytic.default').account_get(cursor, user, move_line.product_id.id, partner_id, user, time.strftime('%Y-%m-%d'))
|
||||
|
||||
if rec:
|
||||
return rec.analytic_id.id
|
||||
|
||||
return super(stock_picking, self)._get_account_analytic_invoice(cursor, user, picking, move_line)
|
||||
|
||||
|
||||
class sale_order_line(osv.osv):
|
||||
_inherit = "sale.order.line"
|
||||
|
||||
@api.multi
|
||||
def _prepare_invoice_line(self, qty):
|
||||
res = super(sale_order_line, self)._prepare_invoice_line(qty)
|
||||
default_analytic_account = self.env['account.analytic.default'].account_get(self.product_id.id, self.order_id.partner_id.id, self.order_id.user_id.id, time.strftime('%Y-%m-%d'))
|
||||
if default_analytic_account:
|
||||
res.update({'account_analytic_id': default_analytic_account.analytic_id.id})
|
||||
return res
|
||||
|
||||
|
||||
class product_product(osv.Model):
|
||||
_inherit = 'product.product'
|
||||
def _rules_count(self, cr, uid, ids, field_name, arg, context=None):
|
||||
Analytic = self.pool['account.analytic.default']
|
||||
return {
|
||||
product_id: Analytic.search_count(cr, uid, [('product_id', '=', product_id)], context=context)
|
||||
for product_id in ids
|
||||
}
|
||||
_columns = {
|
||||
'rules_count': fields.function(_rules_count, string='# Analytic Rules', type='integer'),
|
||||
}
|
||||
|
||||
|
||||
class product_template(osv.Model):
|
||||
_inherit = 'product.template'
|
||||
|
||||
def _rules_count(self, cr, uid, ids, field_name, arg, context=None):
|
||||
res = {}
|
||||
for product_tmpl_id in self.browse(cr, uid, ids, context=context):
|
||||
res[product_tmpl_id.id] = sum([p.rules_count for p in product_tmpl_id.product_variant_ids])
|
||||
return res
|
||||
|
||||
_columns = {
|
||||
'rules_count': fields.function(_rules_count, string='# Analytic Rules', type='integer'),
|
||||
}
|
||||
|
||||
|
||||
def action_view_rules(self, cr, uid, ids, context=None):
|
||||
products = self._get_products(cr, uid, ids, context=context)
|
||||
result = self._get_act_window_dict(cr, uid, 'account_analytic_default.action_product_default_list', context=context)
|
||||
result['domain'] = "[('product_id','in',[" + ','.join(map(str, products)) + "])]"
|
||||
# Remove context so it is not going to filter on product_id with active_id of template
|
||||
result['context'] = "{}"
|
||||
return result
|
||||
|
||||
|
||||
class stock_move(osv.Model):
|
||||
_inherit = 'stock.move'
|
||||
|
||||
def _create_invoice_line_from_vals(self, cr, uid, move, invoice_line_vals, context=None):
|
||||
# It will set the default analtyic account on the invoice line
|
||||
partner_id = self.pool['account.invoice'].browse(cr, uid, invoice_line_vals.get('invoice_id'), context=context).partner_id.id
|
||||
if 'account_analytic_id' not in invoice_line_vals or not invoice_line_vals.get('account_analytic_id'):
|
||||
rec = self.pool['account.analytic.default'].account_get(cr, uid, move.product_id.id, partner_id, uid, time.strftime('%Y-%m-%d'), company_id=move.company_id.id, context=context)
|
||||
if rec:
|
||||
invoice_line_vals.update({'account_analytic_id': rec.analytic_id.id})
|
||||
res = super(stock_move, self)._create_invoice_line_from_vals(cr, uid, move, invoice_line_vals, context=context)
|
||||
return res
|
||||
@@ -0,0 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import account_analytic_default
|
||||
import sale_order_line
|
||||
import product
|
||||
@@ -0,0 +1,65 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class AccountAnalyticDefault(models.Model):
|
||||
_name = "account.analytic.default"
|
||||
_description = "Analytic Distribution"
|
||||
_rec_name = "analytic_id"
|
||||
_order = "sequence"
|
||||
|
||||
sequence = fields.Integer(string='Sequence', help="Gives the sequence order when displaying a list of analytic distribution")
|
||||
analytic_id = fields.Many2one('account.analytic.account', string='Analytic Account', domain=[('account_type', '=', 'normal')])
|
||||
product_id = fields.Many2one('product.product', string='Product', ondelete='cascade', help="Select a product which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this product, it will automatically take this as an analytic account)")
|
||||
partner_id = fields.Many2one('res.partner', string='Partner', ondelete='cascade', help="Select a partner which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this partner, it will automatically take this as an analytic account)")
|
||||
user_id = fields.Many2one('res.users', string='User', ondelete='cascade', help="Select a user which will use analytic account specified in analytic default.")
|
||||
company_id = fields.Many2one('res.company', string='Company', ondelete='cascade', help="Select a company which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this company, it will automatically take this as an analytic account)")
|
||||
date_start = fields.Date(string='Start Date', help="Default start date for this Analytic Account.")
|
||||
date_stop = fields.Date(string='End Date', help="Default end date for this Analytic Account.")
|
||||
|
||||
@api.model
|
||||
def account_get(self, product_id=None, partner_id=None, user_id=None, date=None, company_id=None):
|
||||
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 company_id:
|
||||
domain += ['|', ('company_id', '=', company_id)]
|
||||
domain += [('company_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 = self.env['account.analytic.default']
|
||||
for rec in self.search(domain):
|
||||
index = 0
|
||||
if rec.product_id: index += 1
|
||||
if rec.partner_id: index += 1
|
||||
if rec.company_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
|
||||
|
||||
|
||||
class AccountInvoiceLine(models.Model):
|
||||
_inherit = "account.invoice.line"
|
||||
|
||||
@api.onchange('product_id')
|
||||
def _onchange_product_id(self):
|
||||
res = super(AccountInvoiceLine, self)._onchange_product_id()
|
||||
rec = self.env['account.analytic.default'].account_get(self.product_id.id, self.invoice_id.partner_id.id, self.env.uid,
|
||||
fields.Date.today(), company_id=self.company_id.id)
|
||||
self.account_analytic_id = rec.analytic_id.id
|
||||
return res
|
||||
@@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class ProductProduct(models.Model):
|
||||
_inherit = 'product.product'
|
||||
|
||||
rules_count = fields.Integer(compute='_compute_rules_count', string='# Analytic Rules')
|
||||
|
||||
def _compute_rules_count(self):
|
||||
Analytic = self.env['account.analytic.default']
|
||||
for product in self:
|
||||
product.rules_count = Analytic.search_count([('product_id', '=', product.id)])
|
||||
|
||||
|
||||
class ProductTemplate(models.Model):
|
||||
_inherit = 'product.template'
|
||||
|
||||
rules_count = fields.Integer(compute='_compute_rules_count', string='# Analytic Rules')
|
||||
|
||||
def _compute_rules_count(self):
|
||||
for template in self:
|
||||
self.rules_count = sum([p.rules_count for p in template.product_variant_ids])
|
||||
|
||||
@api.multi
|
||||
def action_view_rules(self):
|
||||
result = self._get_act_window_dict('account_analytic_default.action_product_default_list')
|
||||
result['domain'] = "[('product_id','in',[" + ','.join(map(str, self._get_products())) + "])]"
|
||||
# Remove context so it is not going to filter on product_id with active_id of template
|
||||
result['context'] = "{}"
|
||||
return result
|
||||
@@ -0,0 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class SaleOrderLine(models.Model):
|
||||
_inherit = "sale.order.line"
|
||||
|
||||
@api.multi
|
||||
def _prepare_invoice_line(self, qty):
|
||||
res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
|
||||
default_analytic_account = self.env['account.analytic.default'].account_get(self.product_id.id, self.order_id.partner_id.id, self.order_id.user_id.id, fields.Date.today())
|
||||
if default_analytic_account:
|
||||
res.update({'account_analytic_id': default_analytic_account.analytic_id.id})
|
||||
return res
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<odoo>
|
||||
<data noupdate="1">
|
||||
|
||||
<record id="analytic_default_comp_rule" model="ir.rule">
|
||||
@@ -10,4 +10,4 @@
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
+3
-34
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<odoo>
|
||||
<record id="view_account_analytic_default_tree" model="ir.ui.view">
|
||||
<field name="name">account.analytic.default.tree</field>
|
||||
<field name="model">account.analytic.default</field>
|
||||
@@ -74,33 +73,6 @@
|
||||
<field name="res_model">account.analytic.default</field>
|
||||
<field name="context">{'search_default_product_id': [active_id], 'default_product_id': active_id}</field>
|
||||
</record>
|
||||
<record model="ir.ui.view" id="product_form_view_default_analytic_button">
|
||||
<field name="name">product.product.stock.move</field>
|
||||
<field name="model">product.product</field>
|
||||
<field name="inherit_id" ref="product.product_normal_form_view"/>
|
||||
<field name="arch" type="xml">
|
||||
<div name="button_box" position="inside">
|
||||
<button class="oe_stat_button" name= "%(action_product_default_list)d" type="action"
|
||||
groups="analytic.group_analytic_accounting" icon="fa-bookmark-o">
|
||||
<field string="Analytic Rules" name="rules_count" widget="statinfo" />
|
||||
</button>
|
||||
</div>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="product_template_view_default_analytic_button">
|
||||
<field name="name">product.template.stock.move</field>
|
||||
<field name="model">product.template</field>
|
||||
<field name="inherit_id" ref="product.product_template_only_form_view"/>
|
||||
<field name="arch" type="xml">
|
||||
<div name="button_box" position="inside">
|
||||
<button class="oe_stat_button" name= "action_view_rules" type="object"
|
||||
groups="analytic.group_analytic_accounting" icon="fa-bookmark-o">
|
||||
<field string="Analytic Rules" name="rules_count" widget="statinfo" />
|
||||
</button>
|
||||
</div>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<act_window
|
||||
name="Entries"
|
||||
@@ -110,9 +82,8 @@
|
||||
context="{'search_default_account_id': [active_id], 'default_account_id': active_id}"
|
||||
/>
|
||||
|
||||
<menuitem
|
||||
<menuitem id="menu_analytic_default_list"
|
||||
action="action_analytic_default_list"
|
||||
id="menu_analytic_default_list"
|
||||
parent="account.menu_analytic_accounting"/>
|
||||
|
||||
|
||||
@@ -131,6 +102,4 @@
|
||||
src_model="res.users"
|
||||
context="{'search_default_user_id': [active_id], 'default_user_id': active_id}"
|
||||
groups="analytic.group_analytic_accounting"/>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<record id="product_form_view_default_analytic_button" model="ir.ui.view">
|
||||
<field name="name">product.product.inherit.rules_count</field>
|
||||
<field name="model">product.product</field>
|
||||
<field name="inherit_id" ref="product.product_normal_form_view"/>
|
||||
<field name="arch" type="xml">
|
||||
<div name="button_box" position="inside">
|
||||
<button name= "%(action_product_default_list)d" class="oe_stat_button" type="action"
|
||||
groups="analytic.group_analytic_accounting" icon="fa-bookmark-o">
|
||||
<field name="rules_count" string="Analytic Rules" widget="statinfo" />
|
||||
</button>
|
||||
</div>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="product_template_view_default_analytic_button" model="ir.ui.view">
|
||||
<field name="name">product.template.inherit.rules_count</field>
|
||||
<field name="model">product.template</field>
|
||||
<field name="inherit_id" ref="product.product_template_only_form_view"/>
|
||||
<field name="arch" type="xml">
|
||||
<div name="button_box" position="inside">
|
||||
<button name= "action_view_rules" class="oe_stat_button" type="object"
|
||||
groups="analytic.group_analytic_accounting" icon="fa-bookmark-o">
|
||||
<field name="rules_count" string="Analytic Rules" widget="statinfo" />
|
||||
</button>
|
||||
</div>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user