Merge pull request #100 from i-vyshnevska/12.0-mig_account_invoice_update_wizard
[12.0] [MIG] account invoice update wizard
This commit is contained in:
39
account_invoice_update_wizard/README.rst
Normal file
39
account_invoice_update_wizard/README.rst
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
|
||||||
|
:target: https://www.gnu.org/licenses/agpl-3.0-standalone.html
|
||||||
|
:alt: License: AGPL-3
|
||||||
|
|
||||||
|
Account Invoice Update Wizard
|
||||||
|
=============================
|
||||||
|
|
||||||
|
This module adds a button *Update Invoice* on Customer and Supplier invoices in
|
||||||
|
Open or Paid state. This button starts a wizard which allows the user to update
|
||||||
|
non-legal fields of the invoice:
|
||||||
|
|
||||||
|
* Source Document
|
||||||
|
* Reference/Description
|
||||||
|
* Payment terms (update allowed only to a payment term with same number of terms
|
||||||
|
of the same amount and on invoices without any payment)
|
||||||
|
* Bank Account
|
||||||
|
* Salesman
|
||||||
|
* Notes
|
||||||
|
* Description of invoice lines
|
||||||
|
* Analytic account
|
||||||
|
* Analytic tags
|
||||||
|
|
||||||
|
Bug Tracker
|
||||||
|
===========
|
||||||
|
|
||||||
|
Bugs are tracked on `GitHub Issues
|
||||||
|
<https://github.com/akretion/odoo-usability/issues>`_. In case of trouble, please
|
||||||
|
check there if your issue has already been reported. If you spotted it first,
|
||||||
|
help us smash it by providing detailed and welcomed feedback.
|
||||||
|
|
||||||
|
Contributors
|
||||||
|
------------
|
||||||
|
|
||||||
|
* Alexis de Lattre <alexis.delattre@akretion.com>
|
||||||
|
* Florian da Costa <florian.dacosta@akretion.com>
|
||||||
|
* Matthieu Dietrich <matthieu.dietrich@camptocamp.com>
|
||||||
|
* Yannick Vaucher <yannick.vaucher@camptocamp.com>
|
||||||
|
* Mykhailo Panarin <m.panarin@mobilunity.com>
|
||||||
|
* Artem Kostyuk <a.kostyuk@mobilunity.com>
|
||||||
2
account_invoice_update_wizard/__init__.py
Normal file
2
account_invoice_update_wizard/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from . import models
|
||||||
|
from . import wizard
|
||||||
21
account_invoice_update_wizard/__manifest__.py
Normal file
21
account_invoice_update_wizard/__manifest__.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Copyright 2017 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||||
|
# Copyright 2018-2019 Camptocamp
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
{
|
||||||
|
'name': 'Account Invoice Update Wizard',
|
||||||
|
'version': '12.0.1.0.0',
|
||||||
|
'category': 'Accounting & Finance',
|
||||||
|
'license': 'AGPL-3',
|
||||||
|
'summary': 'Wizard to update non-legal fields of an open/paid invoice',
|
||||||
|
'author': 'Akretion',
|
||||||
|
'website': 'https://github.com/akretion/odoo-usability',
|
||||||
|
'depends': [
|
||||||
|
'account',
|
||||||
|
],
|
||||||
|
'data': [
|
||||||
|
'wizard/account_invoice_update_view.xml',
|
||||||
|
'views/account_invoice.xml',
|
||||||
|
],
|
||||||
|
'installable': True,
|
||||||
|
}
|
||||||
1
account_invoice_update_wizard/models/__init__.py
Normal file
1
account_invoice_update_wizard/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import account_invoice
|
||||||
22
account_invoice_update_wizard/models/account_invoice.py
Normal file
22
account_invoice_update_wizard/models/account_invoice.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Copyright 2019 Camptocamp
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo import models, fields, api, _
|
||||||
|
from odoo.exceptions import UserError
|
||||||
|
import odoo.addons.decimal_precision as dp
|
||||||
|
|
||||||
|
|
||||||
|
class AccountInvoice(models.Model):
|
||||||
|
_inherit = 'account.invoice'
|
||||||
|
|
||||||
|
def prepare_update_wizard(self):
|
||||||
|
self.ensure_one()
|
||||||
|
wizard = self.env['account.invoice.update']
|
||||||
|
res = wizard._prepare_default_get(self)
|
||||||
|
action = self.env.ref(
|
||||||
|
'account_invoice_update_wizard.account_invoice_update_action'
|
||||||
|
).read()[0]
|
||||||
|
action['name'] = "Update Wizard"
|
||||||
|
action['res_id'] = wizard.create(res).id
|
||||||
|
return action
|
||||||
|
|
||||||
1
account_invoice_update_wizard/tests/__init__.py
Normal file
1
account_invoice_update_wizard/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import test_account_invoice_update_wizard
|
||||||
@@ -0,0 +1,196 @@
|
|||||||
|
# Copyright 2018-2019 Camptocamp
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo.tests.common import SavepointCase
|
||||||
|
from odoo.exceptions import UserError
|
||||||
|
|
||||||
|
|
||||||
|
class TestAccountInvoiceUpdateWizard(SavepointCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.customer12 = cls.env.ref('base.res_partner_12')
|
||||||
|
cls.product16 = cls.env.ref('product.product_product_16')
|
||||||
|
cls.product24 = cls.env.ref('product.product_product_24')
|
||||||
|
uom_unit = cls.env.ref('uom.product_uom_categ_unit')
|
||||||
|
|
||||||
|
cls.invoice1 = cls.env['account.invoice'].create({
|
||||||
|
'name': 'Test invoice',
|
||||||
|
'partner_id': cls.customer12.id,
|
||||||
|
})
|
||||||
|
cls.inv_line1 = cls.env['account.invoice.line'].create({
|
||||||
|
'invoice_id': cls.invoice1.id,
|
||||||
|
'name': "Line1",
|
||||||
|
'product_id': cls.product16.id,
|
||||||
|
'product_uom_id': uom_unit.id,
|
||||||
|
'account_id': cls.invoice1.account_id.id,
|
||||||
|
'price_unit': 42.0,
|
||||||
|
})
|
||||||
|
cls.inv_line2 = cls.env['account.invoice.line'].create({
|
||||||
|
'invoice_id': cls.invoice1.id,
|
||||||
|
'name': "Line2",
|
||||||
|
'product_id': cls.product24.id,
|
||||||
|
'product_uom_id': uom_unit.id,
|
||||||
|
'account_id': cls.invoice1.account_id.id,
|
||||||
|
'price_unit': 1111.1,
|
||||||
|
})
|
||||||
|
|
||||||
|
cls.aa1 = cls.env.ref('analytic.analytic_partners_camp_to_camp')
|
||||||
|
cls.aa2 = cls.env.ref('analytic.analytic_nebula')
|
||||||
|
cls.atag1 = cls.env.ref('analytic.tag_contract')
|
||||||
|
cls.atag2 = cls.env['account.analytic.tag'].create({
|
||||||
|
'name': 'の',
|
||||||
|
})
|
||||||
|
|
||||||
|
def create_wizard(self, invoice):
|
||||||
|
res = self.invoice1.prepare_update_wizard()
|
||||||
|
self.wiz = self.env['account.invoice.update'].browse(res['res_id'])
|
||||||
|
|
||||||
|
def test_add_analytic_account_line1(self):
|
||||||
|
""" Add analytic account on an invoice line
|
||||||
|
after the invoice has been approved.
|
||||||
|
|
||||||
|
This will:
|
||||||
|
- update the move line
|
||||||
|
- create a new analytic line.
|
||||||
|
"""
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
wiz_line = self.wiz.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == self.inv_line1)
|
||||||
|
wiz_line.account_analytic_id = self.aa1
|
||||||
|
self.wiz.run()
|
||||||
|
|
||||||
|
related_ml = self.invoice1.move_id.line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == self.product16)
|
||||||
|
self.assertEqual(related_ml.analytic_account_id, self.aa1)
|
||||||
|
self.assertEqual(related_ml.analytic_line_ids.account_id, self.aa1)
|
||||||
|
|
||||||
|
def test_change_analytic_account_line1(self):
|
||||||
|
""" Change analytic account on an invoice line
|
||||||
|
after the invoice has been approved.
|
||||||
|
|
||||||
|
This will:
|
||||||
|
- update the move line
|
||||||
|
- update the existing analytic line."""
|
||||||
|
self.inv_line1.account_analytic_id = self.aa2
|
||||||
|
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
wiz_line = self.wiz.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == self.inv_line1)
|
||||||
|
wiz_line.account_analytic_id = self.aa1
|
||||||
|
self.wiz.run()
|
||||||
|
|
||||||
|
related_ml = self.invoice1.move_id.line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == self.product16)
|
||||||
|
self.assertEqual(related_ml.analytic_account_id, self.aa1)
|
||||||
|
self.assertEqual(related_ml.analytic_line_ids.account_id, self.aa1)
|
||||||
|
|
||||||
|
def test_error_grouped_move_lines(self):
|
||||||
|
""" Change analytic account on an invoice line
|
||||||
|
after the invoice has been approved where both
|
||||||
|
lines were grouped in the same move line.
|
||||||
|
|
||||||
|
This will raise an error.
|
||||||
|
"""
|
||||||
|
self.invoice1.journal_id.group_invoice_lines = True
|
||||||
|
|
||||||
|
self.inv_line2.product_id = self.product16
|
||||||
|
self.inv_line2.unit_price = 42.0
|
||||||
|
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
line1 = self.wiz.line_ids[0]
|
||||||
|
line1.account_analytic_id = self.aa1
|
||||||
|
with self.assertRaises(UserError):
|
||||||
|
self.wiz.run()
|
||||||
|
|
||||||
|
def test_add_analytic_tags_line1(self):
|
||||||
|
""" Add analytic tags on an invoice line
|
||||||
|
after the invoice has been approved.
|
||||||
|
|
||||||
|
This will update move line.
|
||||||
|
"""
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
wiz_line = self.wiz.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == self.inv_line1)
|
||||||
|
wiz_line.analytic_tag_ids = self.atag2
|
||||||
|
self.wiz.run()
|
||||||
|
|
||||||
|
related_ml = self.invoice1.move_id.line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == self.product16)
|
||||||
|
self.assertEqual(related_ml.analytic_tag_ids, self.atag2)
|
||||||
|
self.assertFalse(related_ml.analytic_line_ids)
|
||||||
|
|
||||||
|
def test_change_analytic_tags_line1(self):
|
||||||
|
""" Change analytic tags on an invoice line
|
||||||
|
after the invoice has been approved.
|
||||||
|
|
||||||
|
It will update move line and analytic line
|
||||||
|
"""
|
||||||
|
self.inv_line1.account_analytic_id = self.aa2
|
||||||
|
self.inv_line1.analytic_tag_ids = self.atag1
|
||||||
|
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
wiz_line = self.wiz.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == self.inv_line1)
|
||||||
|
wiz_line.analytic_tag_ids = self.atag2
|
||||||
|
self.wiz.run()
|
||||||
|
|
||||||
|
related_ml = self.invoice1.move_id.line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == self.product16)
|
||||||
|
self.assertEqual(related_ml.analytic_tag_ids, self.atag2)
|
||||||
|
self.assertEqual(related_ml.analytic_line_ids.tag_ids, self.atag2)
|
||||||
|
|
||||||
|
def test_add_analytic_info_line1(self):
|
||||||
|
""" Add analytic account and tags on an invoice line
|
||||||
|
after the invoice has been approved.
|
||||||
|
|
||||||
|
This will:
|
||||||
|
- update move line
|
||||||
|
- create an analytic line
|
||||||
|
"""
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
wiz_line = self.wiz.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == self.inv_line1)
|
||||||
|
wiz_line.account_analytic_id = self.aa1
|
||||||
|
wiz_line.analytic_tag_ids = self.atag2
|
||||||
|
self.wiz.run()
|
||||||
|
|
||||||
|
related_ml = self.invoice1.move_id.line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == self.product16)
|
||||||
|
self.assertEqual(related_ml.analytic_account_id, self.aa1)
|
||||||
|
self.assertEqual(related_ml.analytic_tag_ids, self.atag2)
|
||||||
|
self.assertEqual(related_ml.analytic_line_ids.account_id, self.aa1)
|
||||||
|
self.assertEqual(related_ml.analytic_line_ids.tag_ids, self.atag2)
|
||||||
|
|
||||||
|
def test_empty_analytic_account_line1(self):
|
||||||
|
""" Remove analytic account
|
||||||
|
after the invoice has been approved.
|
||||||
|
|
||||||
|
This will raise an error as it is not implemented.
|
||||||
|
"""
|
||||||
|
self.inv_line1.account_analytic_id = self.aa2
|
||||||
|
|
||||||
|
self.invoice1.action_invoice_open()
|
||||||
|
self.create_wizard(self.invoice1)
|
||||||
|
|
||||||
|
wiz_line = self.wiz.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == self.inv_line1)
|
||||||
|
wiz_line.account_analytic_id = False
|
||||||
|
self.wiz.run()
|
||||||
|
related_ml = self.invoice1.move_id.line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == self.product16)
|
||||||
|
self.assertFalse(related_ml.analytic_account_id)
|
||||||
|
self.assertFalse(related_ml.analytic_line_ids)
|
||||||
29
account_invoice_update_wizard/views/account_invoice.xml
Normal file
29
account_invoice_update_wizard/views/account_invoice.xml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright 2017 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||||
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
-->
|
||||||
|
|
||||||
|
<odoo>
|
||||||
|
|
||||||
|
<record id="invoice_supplier_form" model="ir.ui.view">
|
||||||
|
<field name="model">account.invoice</field>
|
||||||
|
<field name="inherit_id" ref="account.invoice_supplier_form"/>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<button name="action_invoice_draft" position="before">
|
||||||
|
<button name="prepare_update_wizard" type="object" string="Update Invoice" states="open,paid" groups="account.group_account_invoice"/>
|
||||||
|
</button>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="invoice_form" model="ir.ui.view">
|
||||||
|
<field name="model">account.invoice</field>
|
||||||
|
<field name="inherit_id" ref="account.invoice_form"/>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<button name="action_invoice_draft" position="before">
|
||||||
|
<button name="prepare_update_wizard" type="object" string="Update Invoice" states="open,paid" groups="account.group_account_invoice"/>
|
||||||
|
</button>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
</odoo>
|
||||||
1
account_invoice_update_wizard/wizard/__init__.py
Normal file
1
account_invoice_update_wizard/wizard/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import account_invoice_update
|
||||||
302
account_invoice_update_wizard/wizard/account_invoice_update.py
Normal file
302
account_invoice_update_wizard/wizard/account_invoice_update.py
Normal file
@@ -0,0 +1,302 @@
|
|||||||
|
# Copyright 2017 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||||
|
# Copyright 2018-2019 Camptocamp
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo import models, fields, api, _
|
||||||
|
from odoo.exceptions import UserError
|
||||||
|
import odoo.addons.decimal_precision as dp
|
||||||
|
|
||||||
|
|
||||||
|
class AccountInvoiceUpdate(models.TransientModel):
|
||||||
|
_name = 'account.invoice.update'
|
||||||
|
_description = 'Wizard to update non-legal fields of invoice'
|
||||||
|
|
||||||
|
invoice_id = fields.Many2one(
|
||||||
|
'account.invoice', string='Invoice', required=True,
|
||||||
|
readonly=True)
|
||||||
|
type = fields.Selection(related='invoice_id.type', readonly=True)
|
||||||
|
company_id = fields.Many2one(
|
||||||
|
related='invoice_id.company_id', readonly=True)
|
||||||
|
partner_id = fields.Many2one(
|
||||||
|
related='invoice_id.partner_id', readonly=True)
|
||||||
|
user_id = fields.Many2one('res.users', string='Salesperson')
|
||||||
|
payment_term_id = fields.Many2one(
|
||||||
|
'account.payment.term', string='Payment Term')
|
||||||
|
reference = fields.Char(string='Invoice Reference')
|
||||||
|
name = fields.Char(string='Reference/Description')
|
||||||
|
origin = fields.Char(string='Source Document')
|
||||||
|
comment = fields.Text('Additional Information')
|
||||||
|
partner_bank_id = fields.Many2one(
|
||||||
|
'res.partner.bank', string='Bank Account')
|
||||||
|
line_ids = fields.One2many(
|
||||||
|
'account.invoice.line.update', 'parent_id', string='Invoice Lines')
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _simple_fields2update(self):
|
||||||
|
'''List boolean, date, datetime, char, text fields'''
|
||||||
|
return ['reference', 'name', 'origin', 'comment']
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _m2o_fields2update(self):
|
||||||
|
return ['payment_term_id', 'user_id', 'partner_bank_id']
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _prepare_default_get(self, invoice):
|
||||||
|
res = {'invoice_id': invoice.id, 'line_ids': []}
|
||||||
|
for sfield in self._simple_fields2update():
|
||||||
|
res[sfield] = invoice[sfield]
|
||||||
|
for m2ofield in self._m2o_fields2update():
|
||||||
|
res[m2ofield] = invoice[m2ofield].id or False
|
||||||
|
for line in invoice.invoice_line_ids:
|
||||||
|
aa_tags = line.analytic_tag_ids
|
||||||
|
aa_tags = [(6, 0, aa_tags.ids)] if aa_tags else False
|
||||||
|
res['line_ids'].append([0, 0, {
|
||||||
|
'invoice_line_id': line.id,
|
||||||
|
'name': line.name,
|
||||||
|
'quantity': line.quantity,
|
||||||
|
'price_subtotal': line.price_subtotal,
|
||||||
|
'account_analytic_id': line.account_analytic_id.id,
|
||||||
|
'analytic_tag_ids': aa_tags,
|
||||||
|
'display_type': line.display_type,
|
||||||
|
}])
|
||||||
|
return res
|
||||||
|
|
||||||
|
@api.onchange('type')
|
||||||
|
def type_on_change(self):
|
||||||
|
res = {'domain': {}}
|
||||||
|
if self.type in ('out_invoice', 'out_refund'):
|
||||||
|
res['domain']['partner_bank_id'] =\
|
||||||
|
"[('partner_id.ref_company_ids', 'in', [company_id])]"
|
||||||
|
else:
|
||||||
|
res['domain']['partner_bank_id'] =\
|
||||||
|
"[('partner_id', '=', partner_id)]"
|
||||||
|
return res
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _prepare_invoice(self):
|
||||||
|
vals = {}
|
||||||
|
inv = self.invoice_id
|
||||||
|
for sfield in self._simple_fields2update():
|
||||||
|
if self[sfield] != inv[sfield]:
|
||||||
|
vals[sfield] = self[sfield]
|
||||||
|
for m2ofield in self._m2o_fields2update():
|
||||||
|
if self[m2ofield] != inv[m2ofield]:
|
||||||
|
vals[m2ofield] = self[m2ofield].id or False
|
||||||
|
if 'payment_term_id' in vals:
|
||||||
|
pterm_list = self.payment_term_id.compute(
|
||||||
|
value=1, date_ref=inv.date_invoice)[0]
|
||||||
|
if pterm_list:
|
||||||
|
vals['date_due'] = max(line[0] for line in pterm_list)
|
||||||
|
return vals
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _line_simple_fields2update(self):
|
||||||
|
return ["name",]
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _line_m2o_fields2update(self):
|
||||||
|
return ["account_analytic_id",]
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _line_m2m_fields2update(self):
|
||||||
|
return ["analytic_tag_ids",]
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _prepare_invoice_line(self, line):
|
||||||
|
vals = {}
|
||||||
|
for field in self._line_simple_fields2update():
|
||||||
|
if line[field] != line.invoice_line_id[field]:
|
||||||
|
vals[field] = line[field]
|
||||||
|
for field in self._line_m2o_fields2update():
|
||||||
|
if line[field] != line.invoice_line_id[field]:
|
||||||
|
vals[field] = line[field].id
|
||||||
|
for field in self._line_m2m_fields2update():
|
||||||
|
if line[field] != line.invoice_line_id[field]:
|
||||||
|
vals[field] = [(6, 0, line[field].ids)]
|
||||||
|
return vals
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _prepare_move(self):
|
||||||
|
mvals = {}
|
||||||
|
inv = self.invoice_id
|
||||||
|
ini_ref = inv.move_id.ref
|
||||||
|
ref = inv.reference or inv.name
|
||||||
|
if ini_ref != ref:
|
||||||
|
mvals['ref'] = ref
|
||||||
|
return mvals
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _get_matching_inv_line(self, move_line):
|
||||||
|
""" Find matching invoice line by product """
|
||||||
|
# TODO make it accept more case as lines won't
|
||||||
|
# be grouped unless journal.group_invoice_line is True
|
||||||
|
inv_line = self.invoice_id.invoice_line_ids.filtered(
|
||||||
|
lambda rec: rec.product_id == move_line.product_id)
|
||||||
|
if len(inv_line) != 1:
|
||||||
|
raise UserError(
|
||||||
|
"Cannot match a single invoice line to move line %s" %
|
||||||
|
move_line.name)
|
||||||
|
return inv_line
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _prepare_move_line(self, inv_line):
|
||||||
|
mlvals = {}
|
||||||
|
inv_line_upd = self.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == inv_line)
|
||||||
|
|
||||||
|
ini_aa = inv_line.account_analytic_id
|
||||||
|
new_aa = inv_line_upd.account_analytic_id
|
||||||
|
|
||||||
|
if ini_aa != new_aa:
|
||||||
|
mlvals['analytic_account_id'] = new_aa.id
|
||||||
|
|
||||||
|
ini_aa_tags = inv_line.analytic_tag_ids
|
||||||
|
new_aa_tags = inv_line_upd.analytic_tag_ids
|
||||||
|
|
||||||
|
if ini_aa_tags != new_aa_tags:
|
||||||
|
mlvals['analytic_tag_ids'] = [(6, None, new_aa_tags.ids)]
|
||||||
|
return mlvals
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _prepare_analytic_line(self, inv_line):
|
||||||
|
alvals = {}
|
||||||
|
inv_line_upd = self.line_ids.filtered(
|
||||||
|
lambda rec: rec.invoice_line_id == inv_line)
|
||||||
|
|
||||||
|
ini_aa = inv_line.account_analytic_id
|
||||||
|
new_aa = inv_line_upd.account_analytic_id
|
||||||
|
|
||||||
|
if ini_aa != new_aa:
|
||||||
|
alvals['account_id'] = new_aa.id
|
||||||
|
|
||||||
|
ini_aa_tags = inv_line.analytic_tag_ids
|
||||||
|
new_aa_tags = inv_line_upd.analytic_tag_ids
|
||||||
|
|
||||||
|
if ini_aa_tags != new_aa_tags:
|
||||||
|
alvals['tag_ids'] = [(6, None, new_aa_tags.ids)]
|
||||||
|
return alvals
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _update_payment_term_move(self):
|
||||||
|
self.ensure_one()
|
||||||
|
inv = self.invoice_id
|
||||||
|
if (
|
||||||
|
self.payment_term_id and
|
||||||
|
self.payment_term_id != inv.payment_term_id and
|
||||||
|
inv.move_id):
|
||||||
|
# I don't update pay term when the invoice is partially (or fully)
|
||||||
|
# paid because if you have a payment term with several lines
|
||||||
|
# of the same amount, you would also have to take into account
|
||||||
|
# the reconcile marks to put the new maturity date on the right
|
||||||
|
# lines
|
||||||
|
if inv.payment_ids:
|
||||||
|
raise UserError(_(
|
||||||
|
"This wizard doesn't support the update of payment "
|
||||||
|
"terms on an invoice which is partially or fully "
|
||||||
|
"paid."))
|
||||||
|
prec = self.env['decimal.precision'].precision_get('Account')
|
||||||
|
term_res = self.payment_term_id.compute(
|
||||||
|
inv.amount_total, inv.date_invoice)[0]
|
||||||
|
new_pterm = {} # key = int(amount * 100), value = [date1, date2]
|
||||||
|
for entry in term_res:
|
||||||
|
amount = int(entry[1] * 10 * prec)
|
||||||
|
if amount in new_pterm:
|
||||||
|
new_pterm[amount].append(entry[0])
|
||||||
|
else:
|
||||||
|
new_pterm[amount] = [entry[0]]
|
||||||
|
mlines = {} # key = int(amount * 100), value : [line1, line2]
|
||||||
|
for line in inv.move_id.line_ids:
|
||||||
|
if line.account_id == inv.account_id:
|
||||||
|
amount = int(abs(line.credit - line.debit) * 10 * prec)
|
||||||
|
if amount in mlines:
|
||||||
|
mlines[amount].append(line)
|
||||||
|
else:
|
||||||
|
mlines[amount] = [line]
|
||||||
|
for iamount, lines in mlines.items():
|
||||||
|
if len(lines) != len(new_pterm.get(iamount, [])):
|
||||||
|
raise UserError(_(
|
||||||
|
"The original payment term '%s' doesn't have the "
|
||||||
|
"same terms (number of terms and/or amount) as the "
|
||||||
|
"new payment term '%s'. You can only switch to a "
|
||||||
|
"payment term that has the same number of terms "
|
||||||
|
"with the same amount.") % (
|
||||||
|
inv.payment_term_id.name, self.payment_term_id.name))
|
||||||
|
for line in lines:
|
||||||
|
line.date_maturity = new_pterm[iamount].pop()
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def run(self):
|
||||||
|
self.ensure_one()
|
||||||
|
inv = self.invoice_id
|
||||||
|
updated = False
|
||||||
|
# re-write date_maturity on move line
|
||||||
|
self._update_payment_term_move()
|
||||||
|
ivals = self._prepare_invoice()
|
||||||
|
if ivals:
|
||||||
|
updated = True
|
||||||
|
inv.write(ivals)
|
||||||
|
if inv.move_id:
|
||||||
|
mvals = self._prepare_move()
|
||||||
|
if mvals:
|
||||||
|
inv.move_id.write(mvals)
|
||||||
|
for ml in inv.move_id.line_ids.filtered(
|
||||||
|
# we are only interested in invoice lines, not tax lines
|
||||||
|
lambda rec: bool(rec.product_id)
|
||||||
|
):
|
||||||
|
if ml.credit == 0.0:
|
||||||
|
continue
|
||||||
|
inv_line = self._get_matching_inv_line(ml)
|
||||||
|
mlvals = self._prepare_move_line(inv_line)
|
||||||
|
if mlvals:
|
||||||
|
updated = True
|
||||||
|
ml.write(mlvals)
|
||||||
|
aalines = ml.analytic_line_ids
|
||||||
|
alvals = self._prepare_analytic_line(inv_line)
|
||||||
|
if aalines and alvals:
|
||||||
|
updated = True
|
||||||
|
if ('account_id' in alvals and
|
||||||
|
alvals['account_id'] is False):
|
||||||
|
former_aa = inv_line.account_analytic_id
|
||||||
|
to_remove_aalines = aalines.filtered(
|
||||||
|
lambda rec: rec.account_id == former_aa)
|
||||||
|
# remove existing analytic line
|
||||||
|
to_remove_aalines.unlink()
|
||||||
|
else:
|
||||||
|
aalines.write(alvals)
|
||||||
|
elif 'account_id' in alvals:
|
||||||
|
# Create analytic lines if analytic account
|
||||||
|
# is added later
|
||||||
|
ml.create_analytic_lines()
|
||||||
|
for line in self.line_ids:
|
||||||
|
ilvals = self._prepare_invoice_line(line)
|
||||||
|
if ilvals:
|
||||||
|
updated = True
|
||||||
|
line.invoice_line_id.write(ilvals)
|
||||||
|
if updated:
|
||||||
|
inv.message_post(body=_(
|
||||||
|
'Non-legal fields of invoice updated via the Invoice Update '
|
||||||
|
'wizard.'))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class AccountInvoiceLineUpdate(models.TransientModel):
|
||||||
|
_name = 'account.invoice.line.update'
|
||||||
|
_description = 'Update non-legal fields of invoice lines'
|
||||||
|
|
||||||
|
parent_id = fields.Many2one(
|
||||||
|
'account.invoice.update', string='Wizard', ondelete='cascade')
|
||||||
|
invoice_line_id = fields.Many2one(
|
||||||
|
'account.invoice.line', string='Invoice Line', readonly=True)
|
||||||
|
name = fields.Text(string='Description', required=True)
|
||||||
|
display_type = fields.Selection([
|
||||||
|
('line_section', "Section"),
|
||||||
|
('line_note', "Note")], default=False, help="Technical field for UX purpose.")
|
||||||
|
quantity = fields.Float(
|
||||||
|
string='Quantity', digits=dp.get_precision('Product Unit of Measure'),
|
||||||
|
readonly=True)
|
||||||
|
price_subtotal = fields.Float(
|
||||||
|
string='Amount', readonly=True, digits=dp.get_precision('Account'))
|
||||||
|
account_analytic_id = fields.Many2one(
|
||||||
|
'account.analytic.account', string='Analytic Account')
|
||||||
|
analytic_tag_ids = fields.Many2many(
|
||||||
|
'account.analytic.tag', string='Analytic Tags')
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
© 2017 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||||
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
-->
|
||||||
|
|
||||||
|
<odoo>
|
||||||
|
|
||||||
|
<record id="account_invoice_update_form" model="ir.ui.view">
|
||||||
|
<field name="model">account.invoice.update</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<form string="Update Invoice Wizard">
|
||||||
|
<group name="main">
|
||||||
|
<field name="invoice_id" invisible="1"/>
|
||||||
|
<field name="type" invisible="1"/>
|
||||||
|
<field name="company_id" invisible="1"/>
|
||||||
|
<field name="partner_id" invisible="1"/>
|
||||||
|
<field name="reference" attrs="{'invisible': [('type', 'not in', ('in_invoice', 'in_refund'))]}"/>
|
||||||
|
<field name="origin"/>
|
||||||
|
<field name="name"/>
|
||||||
|
<field name="payment_term_id" widget="selection"/>
|
||||||
|
<field name="partner_bank_id"/>
|
||||||
|
<field name="user_id"/>
|
||||||
|
<field name="comment"/>
|
||||||
|
</group>
|
||||||
|
<group name="lines">
|
||||||
|
<field name="line_ids" nolabel="1">
|
||||||
|
<tree editable="bottom" create="false" delete="false" edit="true">
|
||||||
|
<field name="invoice_line_id" invisible="1"/>
|
||||||
|
<field name="display_type" invisible="1"/>
|
||||||
|
<field name="name"/>
|
||||||
|
<field name="quantity" attrs="{'invisible': [('display_type', '!=', False)]}"/>
|
||||||
|
<field name="price_subtotal" attrs="{'invisible': [('display_type', '!=', False)]}"/>
|
||||||
|
<field name="account_analytic_id" attrs="{'invisible': [('display_type', '!=', False)]}" groups="analytic.group_analytic_accounting"/>
|
||||||
|
<field name="analytic_tag_ids" attrs="{'invisible': [('display_type', '!=', False)]}" groups="analytic.group_analytic_accounting" widget="many2many_tags"/>
|
||||||
|
</tree>
|
||||||
|
</field>
|
||||||
|
</group>
|
||||||
|
<footer>
|
||||||
|
<button name="run" type="object" class="oe_highlight" string="Update"/>
|
||||||
|
<button special="cancel" string="Cancel" class="oe_link"/>
|
||||||
|
</footer>
|
||||||
|
</form>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="account_invoice_update_action" model="ir.actions.act_window">
|
||||||
|
<field name="name">Invoice Update Wizard</field>
|
||||||
|
<field name="res_model">account.invoice.update</field>
|
||||||
|
<field name="view_mode">form</field>
|
||||||
|
<field name="target">new</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
</odoo>
|
||||||
Reference in New Issue
Block a user