[FIX] purchase: partner_bank_id is wrongly set up

How to reproduce the bug ?

- install the accounting, contacts and purchase apps
- create several companies and enable the checkboxes of all
  these companies
- create a new contact and add a bank account for each company
- switch to a company different than the first one
- create a request for quotations in the purchase apps and
  confirm it
- in the accounting app, create a new vendor bill and use the
  auto-complete field to select the RFQ created earlier.

The bug:

When you try to create a vendor bill from a request for quotations,
the recipient bank is wrongly chosen. Because of that, there will be
2 issues. The first one is when you want to create the bill from the
RFQ: you will get an error and the invoice won't be created. The second
one is when you create an invoice then use the auto-complete field. In
this case, there won't be any error but the recipient bank will
be wrong.

opw-2731264

closes odoo/odoo#86078

X-original-commit: 074fee2e19547f4145ae0aa93d68741ab4aa9bae
Signed-off-by: Adrien Minet <admi@odoo.com>
Signed-off-by: Minet Adrien (admi) <admi@odoo.com>
Signed-off-by: Steve Van Essche <svs@odoo.com>
This commit is contained in:
Adrien Minet
2022-03-10 12:58:42 +00:00
parent b379c0765a
commit 34fea66e41
3 changed files with 70 additions and 2 deletions
@@ -73,7 +73,6 @@ class AccountMove(models.Model):
self.purchase_id = False
self._onchange_currency()
self.partner_bank_id = self.bank_partner_id.bank_ids and self.bank_partner_id.bank_ids[0]
@api.onchange('partner_id', 'company_id')
def _onchange_partner_id(self):
+2 -1
View File
@@ -626,6 +626,7 @@ class PurchaseOrder(models.Model):
raise UserError(_('Please define an accounting purchase journal for the company %s (%s).') % (self.company_id.name, self.company_id.id))
partner_invoice = self.env['res.partner'].browse(self.partner_id.address_get(['invoice'])['invoice'])
partner_bank_id = self.partner_id.bank_ids.filtered_domain(['|', ('company_id', '=', False), ('company_id', '=', self.company_id.id)])[:1]
invoice_vals = {
'ref': self.partner_ref or '',
'move_type': move_type,
@@ -635,7 +636,7 @@ class PurchaseOrder(models.Model):
'partner_id': partner_invoice.id,
'fiscal_position_id': (self.fiscal_position_id or self.fiscal_position_id._get_fiscal_position(partner_invoice)).id,
'payment_reference': self.partner_ref or '',
'partner_bank_id': self.partner_id.bank_ids[:1].id,
'partner_bank_id': partner_bank_id.id,
'invoice_origin': self.name,
'invoice_payment_term_id': self.payment_term_id.id,
'invoice_line_ids': [],
@@ -327,3 +327,71 @@ class TestPurchaseToInvoice(AccountTestInvoicingCommon):
aml = self.env['account.move.line'].search([('purchase_line_id', '=', purchase_order.order_line.id)])
self.assertRecordValues(aml, [{'analytic_account_id': analytic_account_manual.id}])
def test_multicompany_partner_bank(self):
""" Test that in a multiple company environment, the bank account of the invoice
is the one corresponding to the active company. """
company_a = self.env.user.company_id
company_b = self.env['res.company'].create({
'name': 'Company B',
})
self.env.ref('l10n_generic_coa.configurable_chart_template')._load(15.0, 15.0, company_b)
partner = self.env['res.partner'].create({
'name': 'AAAAA',
})
partner_bank_a = self.env['res.partner.bank'].create({
'acc_number': "BE01 23456789 10",
'partner_id': partner.id,
'acc_type': 'bank'
})
partner_bank_b = self.env['res.partner.bank'].create({
'acc_number': "BE10 98765432 10",
'partner_id': partner.id,
'acc_type': 'bank'
})
partner.bank_ids = [partner_bank_a.id, partner_bank_b.id]
partner_bank_a.company_id = company_a
partner_bank_b.company_id = company_b
PurchaseOrder = self.env['purchase.order'].with_context(tracking_disable=True)
po_a = PurchaseOrder.with_company(company_a).create({
'partner_id': partner.id,
'company_id': company_a.id,
'currency_id': company_a.currency_id.id,
'date_order': '2019-01-01',
})
po_b = PurchaseOrder.with_company(company_b).create({
'partner_id': partner.id,
'company_id': company_b.id,
'currency_id': company_b.currency_id.id,
'date_order': '2019-01-01',
})
vals_pol = {
'name': self.product_deliver.name,
'product_id': self.product_deliver.id,
'product_qty': 10.0,
'product_uom': self.product_deliver.uom_id.id,
'price_unit': self.product_deliver.list_price,
'order_id': po_a.id,
'taxes_id': False,
}
PurchaseOrderLine = self.env['purchase.order.line'].with_context(tracking_disable=True)
PurchaseOrderLine.create(vals_pol)
vals_pol['order_id'] = po_b.id
PurchaseOrderLine.create(vals_pol)
po_a.button_confirm()
po_b.button_confirm()
po_a.order_line.qty_received = po_b.order_line.qty_received = 10
po_a.with_company(company_a).action_create_invoice()
po_b.with_company(company_b).action_create_invoice()
self.assertEqual(po_a.invoice_ids.company_id, company_a)
self.assertEqual(po_a.invoice_ids.partner_bank_id, partner_bank_a)
self.assertEqual(po_b.invoice_ids.company_id, company_b)
self.assertEqual(po_b.invoice_ids.partner_bank_id, partner_bank_b)