From 051dfea35e21ad2e249acc08c91a553a7ef6a4aa Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Wed, 25 Apr 2018 17:00:43 +0200 Subject: [PATCH] [IMP] account: new way of auto-completion of vendor bills/credit notes This adds the possibilty to select another account.invoice object in a vendor bill or credit note, to automatically copy its lines. When the purchase app is installed, the same field can be used for auto-completion and users can choose between vendor bills or purchase orders (preferably PO) Co-authored-by: dat@openerp.com --- addons/account/models/account_invoice.py | 33 +++++++++ addons/account/views/account_invoice_view.xml | 6 +- addons/purchase/models/__init__.py | 1 + addons/purchase/models/account_invoice.py | 12 ++- addons/purchase/models/purchase_bill.py | 73 +++++++++++++++++++ addons/purchase/security/ir.model.access.csv | 3 +- .../purchase/security/purchase_security.xml | 6 ++ .../purchase/views/account_invoice_views.xml | 47 +++++++++++- 8 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 addons/purchase/models/purchase_bill.py diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index 2c0da345e3f..cd8a907c796 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -286,6 +286,8 @@ class AccountInvoice(models.Model): partner_id = fields.Many2one('res.partner', string='Partner', change_default=True, required=True, readonly=True, states={'draft': [('readonly', False)]}, track_visibility='always') + vendor_bill_id = fields.Many2one('account.invoice', string='Vendor Bill', + help="Auto-complete from a past bill.") payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', oldname='payment_term', readonly=True, states={'draft': [('readonly', False)]}, help="If you use payment terms, the due date will be computed automatically at the generation " @@ -372,6 +374,20 @@ class AccountInvoice(models.Model): ('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number must be unique per Company!'), ] + # Load all Vendor Bill lines + @api.onchange('vendor_bill_id') + def _onchange_vendor_bill(self): + if not self.vendor_bill_id: + return {} + self.currency_id = self.vendor_bill_id.currency_id + new_lines = self.env['account.invoice.line'] + for line in self.vendor_bill_id.invoice_line_ids: + new_lines += new_lines.new(line._prepare_invoice_line()) + self.invoice_line_ids += new_lines + self.payment_term_id = self.vendor_bill_id.payment_term_id + self.vendor_bill_id = False + return {} + def _get_seq_number_next_stuff(self): self.ensure_one() journal_sequence = self.journal_id.sequence_id @@ -1655,6 +1671,23 @@ class AccountInvoiceLine(models.Model): raise UserError(_('You can only delete an invoice line if the invoice is in draft state.')) return super(AccountInvoiceLine, self).unlink() + def _prepare_invoice_line(self): + data = { + 'name': self.name, + 'origin': self.origin, + 'uom_id': self.uom_id.id, + 'product_id': self.product_id.id, + 'account_id': self.account_id.id, + 'price_unit': self.price_unit, + 'quantity': self.quantity, + 'discount': self.discount, + 'account_analytic_id': self.account_analytic_id.id, + 'analytic_tag_ids': self.analytic_tag_ids.ids, + 'invoice_line_tax_ids': self.invoice_line_tax_ids.ids + } + return data + + class AccountInvoiceTax(models.Model): _name = "account.invoice.tax" _description = "Invoice Tax" diff --git a/addons/account/views/account_invoice_view.xml b/addons/account/views/account_invoice_view.xml index 1eaf678e93e..33a5c8579a7 100644 --- a/addons/account/views/account_invoice_view.xml +++ b/addons/account/views/account_invoice_view.xml @@ -238,7 +238,7 @@