[12.0][IMP] account_spread_cost_revenue, auto spread on validate invoice

This commit is contained in:
Kitti U
2020-05-17 21:37:40 +07:00
parent 3e7eb50781
commit 40f0c605b6
11 changed files with 338 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo.exceptions import UserError
class AccountInvoiceLine(models.Model):
@@ -65,3 +66,43 @@ class AccountInvoiceLine(models.Model):
'target': 'new',
'context': ctx,
}
def create_auto_spread(self):
""" Create auto spread table for each invoice line, when needed """
def _filter_line(aline, iline):
""" Find matching template auto line with invoice line """
if aline.product_id and iline.product_id != aline.product_id:
return False
if aline.account_id and iline.account_id != aline.account_id:
return False
if aline.analytic_account_id and \
iline.account_analytic_id != aline.analytic_account_id:
return False
return True
for line in self:
if line.spread_check == 'linked':
continue
spread_type = (
'sale' if line.invoice_type in ['out_invoice', 'out_refund']
else 'purchase')
spread_auto = self.env['account.spread.template.auto'].search(
[('template_id.auto_spread', '=', True),
('template_id.spread_type', '=', spread_type)])
matched = spread_auto.filtered(lambda a, i=line: _filter_line(a, i))
template = matched.mapped('template_id')
if not template:
continue
elif len(template) > 1:
raise UserError(
_('Too many auto spread templates (%s) matched with the '
'invoice line, %s') % (len(template), line.display_name))
# Found auto spread template for this invoice line, create it
wizard = self.env['account.spread.invoice.line.link.wizard'].new({
'invoice_line_id': line.id,
'company_id': line.company_id.id,
'spread_action_type': 'template',
'template_id': template.id,
})
wizard.confirm()