From 1b04b1d71a78633a43155d01a37c79b9e483bb2b Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Thu, 28 Jan 2016 15:21:53 +0100 Subject: [PATCH] [IMP] purchase: default account journal When the user clicks on the stat button "Invoices", then creates a new invoice, the default currency of the new invoice will be based on the default account journal. If the default account journal is not in the currency of the PO, this will lead to an invoice which is not in the same currency than the PO. This is not a bug in itself, but most of the time, the user will want to create an invoice in the same currency than the PO. If no invoice is linked to the PO, the fix sets a default account journal in the currency of the PO, as long as one can be found. If an invoice is already linked to the PO, it uses the same account journal. opw-667743 --- addons/purchase/purchase.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index d344019137e..6592202ac38 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -398,15 +398,28 @@ class PurchaseOrder(models.Model): #override the context to get rid of the default filtering result['context'] = {'type': 'in_invoice', 'default_purchase_id': self.id} - result['domain'] = "[('purchase_id', '=', %s)]" % self.id - invoice_ids = sum([order.invoice_ids.ids for order in self], []) + + if not self.invoice_ids: + # Choose a default account journal in the same currency in case a new invoice is created + journal_domain = [ + ('type', '=', 'purchase'), + ('company_id', '=', self.company_id.id), + ('currency_id', '=', self.currency_id.id), + ] + default_journal_id = self.env['account.journal'].search(journal_domain, limit=1) + if default_journal_id: + result['context']['default_journal_id'] = default_journal_id.id + else: + # Use the same account journal than a previous invoice + result['context']['default_journal_id'] = self.invoice_ids[0].journal_id.id + #choose the view_mode accordingly - if len(invoice_ids) > 1: - result['domain'] = "[('id','in',[" + ','.join(map(str, invoice_ids)) + "])]" - elif len(invoice_ids) == 1: + if len(self.invoice_ids) != 1: + result['domain'] = "[('id', 'in', " + str(self.invoice_ids.ids) + ")]" + elif len(self.invoice_ids) == 1: res = self.env.ref('account.invoice_supplier_form', False) result['views'] = [(res and res.id or False, 'form')] - result['res_id'] = invoice_ids and invoice_ids[0] or False + result['res_id'] = self.invoice_ids.id return result