From 23f492b2aada5bea3722ff1703e338a73f509f99 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 18 Oct 2016 23:02:55 +0200 Subject: [PATCH] 8.0 Add support for partner bank matching on invoice update (#6) Add support for partner bank matching on invoice update (before, it was only supported on invoice creation) --- account_invoice_import/README.rst | 4 +- .../wizard/account_invoice_import.py | 38 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/account_invoice_import/README.rst b/account_invoice_import/README.rst index 1cfbea356c..5f7f8d6c13 100644 --- a/account_invoice_import/README.rst +++ b/account_invoice_import/README.rst @@ -45,7 +45,7 @@ To use this module, go to the menu *Accounting > Suppliers > Import Invoices* an .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/95/8.0 + :target: https://runbot.odoo-community.org/runbot/226/8.0 Known issues / Roadmap ====================== @@ -60,7 +60,7 @@ Bug Tracker =========== Bugs are tracked on `GitHub Issues -`_. In case of trouble, please +`_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback. diff --git a/account_invoice_import/wizard/account_invoice_import.py b/account_invoice_import/wizard/account_invoice_import.py index 1c85584456..2e38af48a1 100644 --- a/account_invoice_import/wizard/account_invoice_import.py +++ b/account_invoice_import/wizard/account_invoice_import.py @@ -119,6 +119,7 @@ def _prepare_create_invoice_vals(self, parsed_inv): aio = self.env['account.invoice'] ailo = self.env['account.invoice.line'] bdio = self.env['business.document.import'] + rpo = self.env['res.partner'] company = self.env.user.company_id assert parsed_inv.get('amount_total'), 'Missing amount_total' partner = bdio._match_partner( @@ -146,28 +147,12 @@ def _prepare_create_invoice_vals(self, parsed_inv): vals['date_due'] = parsed_inv.get('date_due') # Bank info if parsed_inv.get('iban'): - iban = parsed_inv.get('iban').replace(' ', '') - self._cr.execute( - """SELECT id FROM res_partner_bank - WHERE replace(acc_number, ' ', '')=%s - AND state='iban' - AND partner_id=%s - """, (iban, vals['partner_id'])) - rpb_res = self._cr.fetchall() - if rpb_res: - vals['partner_bank_id'] = rpb_res[0][0] - else: - partner_bank = self.env['res.partner.bank'].create({ - 'partner_id': vals['partner_id'], - 'state': 'iban', - 'acc_number': parsed_inv['iban'], - 'bank_bic': parsed_inv.get('bic'), - }) + partner = rpo.browse(vals['partner_id']) + partner_bank = bdio._match_partner_bank( + partner, parsed_inv['iban'], parsed_inv.get('bic'), + parsed_inv['chatter_msg'], create_if_not_found=True) + if partner_bank: vals['partner_bank_id'] = partner_bank.id - parsed_inv['chatter_msg'].append(_( - "The bank account IBAN %s has been automatically " - "added on the supplier %s") % ( - parsed_inv['iban'], partner.name)) config = partner.invoice_import_id if config.invoice_line_method.startswith('1line'): if config.invoice_line_method == '1line_no_product': @@ -550,7 +535,8 @@ def _prepare_create_invoice_line(self, product, uom, import_line, invoice): return vals @api.model - def _prepare_update_invoice_vals(self, parsed_inv): + def _prepare_update_invoice_vals(self, parsed_inv, partner): + bdio = self.env['business.document.import'] vals = { 'supplier_invoice_number': parsed_inv.get('invoice_number'), @@ -559,6 +545,12 @@ def _prepare_update_invoice_vals(self, parsed_inv): } if parsed_inv.get('date_due'): vals['date_due'] = parsed_inv['date_due'] + if parsed_inv.get('iban'): + partner_bank = bdio._match_partner_bank( + partner, parsed_inv['iban'], parsed_inv.get('bic'), + parsed_inv['chatter_msg'], create_if_not_found=True) + if partner_bank: + vals['partner_bank_id'] = partner_bank.id return vals @api.multi @@ -597,7 +589,7 @@ def update_invoice(self): currency.name, invoice.currency_id.name)) # When invoice with embedded XML files will be more widely used, # we should also update invoice lines - vals = self._prepare_update_invoice_vals(parsed_inv) + vals = self._prepare_update_invoice_vals(parsed_inv, partner) logger.debug('Updating supplier invoice with vals=%s', vals) self.invoice_id.write(vals) if (