From 65fd8e460261cbd191b7b579970df7b6a1735c98 Mon Sep 17 00:00:00 2001 From: Yenthe666 Date: Wed, 11 Dec 2019 07:38:52 +0000 Subject: [PATCH] [FIX] sale: allow inheriting and modifying function values closes odoo/odoo#44069 X-original-commit: 14a12ced1d5cb79b9ec8b9df926a12330aa34cf1 Signed-off-by: Damien Bouvy (dbo) --- .../wizard/sale_make_invoice_advance.py | 8 +-- .../sale/wizard/sale_make_invoice_advance.py | 71 ++++++++++++------- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/addons/l10n_in_sale/wizard/sale_make_invoice_advance.py b/addons/l10n_in_sale/wizard/sale_make_invoice_advance.py index 880f07d2537..f067495d77f 100644 --- a/addons/l10n_in_sale/wizard/sale_make_invoice_advance.py +++ b/addons/l10n_in_sale/wizard/sale_make_invoice_advance.py @@ -7,8 +7,8 @@ from odoo import models class SaleAdvancePaymentInv(models.TransientModel): _inherit = "sale.advance.payment.inv" - def _create_invoice(self, order, so_line, amount): - invoice = super()._create_invoice(order, so_line, amount) + def _prepare_invoice_values(self, order, name, amount, so_line): + res = super()._prepare_invoice_values(order, name, amount, so_line) if order.l10n_in_journal_id: - invoice.journal_id = order.l10n_in_journal_id - return invoice + res['journal_id'] = order.l10n_in_journal_id.id + return res diff --git a/addons/sale/wizard/sale_make_invoice_advance.py b/addons/sale/wizard/sale_make_invoice_advance.py index ba2937c884b..eb29a4c4ccf 100644 --- a/addons/sale/wizard/sale_make_invoice_advance.py +++ b/addons/sale/wizard/sale_make_invoice_advance.py @@ -69,16 +69,7 @@ class SaleAdvancePaymentInv(models.TransientModel): return {'value': {'amount': 0}} return {} - def _create_invoice(self, order, so_line, amount): - if (self.advance_payment_method == 'percentage' and self.amount <= 0.00) or (self.advance_payment_method == 'fixed' and self.fixed_amount <= 0.00): - raise UserError(_('The value of the down payment amount must be positive.')) - if self.advance_payment_method == 'percentage': - amount = order.amount_untaxed * self.amount / 100 - name = _("Down payment of %s%%") % (self.amount,) - else: - amount = self.fixed_amount - name = _('Down Payment') - + def _prepare_invoice_values(self, order, name, amount, so_line): invoice_vals = { 'type': 'out_invoice', 'invoice_origin': order.name, @@ -107,6 +98,27 @@ class SaleAdvancePaymentInv(models.TransientModel): 'analytic_account_id': order.analytic_account_id.id or False, })], } + + return invoice_vals + + def _get_advance_details(self, order): + if self.advance_payment_method == 'percentage': + amount = order.amount_untaxed * self.amount / 100 + name = _("Down payment of %s%%") % (self.amount) + else: + amount = self.fixed_amount + name = _('Down Payment') + + return amount, name + + def _create_invoice(self, order, so_line, amount): + if (self.advance_payment_method == 'percentage' and self.amount <= 0.00) or (self.advance_payment_method == 'fixed' and self.fixed_amount <= 0.00): + raise UserError(_('The value of the down payment amount must be positive.')) + + amount, name = self._get_advance_details(order) + + invoice_vals = self._prepare_invoice_values(order, name, amount, so_line) + if order.fiscal_position_id: invoice_vals['fiscal_position_id'] = order.fiscal_position_id.id invoice = self.env['account.move'].sudo().create(invoice_vals).with_user(self.env.uid) @@ -115,6 +127,22 @@ class SaleAdvancePaymentInv(models.TransientModel): subtype_id=self.env.ref('mail.mt_note').id) return invoice + def _prepare_so_line(self, order, analytic_tag_ids, tax_ids, amount): + so_values = { + 'name': _('Down Payment: %s') % (time.strftime('%m %Y'),), + 'price_unit': amount, + 'product_uom_qty': 0.0, + 'order_id': order.id, + 'discount': 0.0, + 'product_uom': self.product_id.uom_id.id, + 'product_id': self.product_id.id, + 'analytic_tag_ids': analytic_tag_ids, + 'tax_id': [(6, 0, tax_ids)], + 'is_downpayment': True, + 'sequence': order.order_line[-1].sequence + 1 + } + return so_values + def create_invoices(self): sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', [])) @@ -129,10 +157,8 @@ class SaleAdvancePaymentInv(models.TransientModel): sale_line_obj = self.env['sale.order.line'] for order in sale_orders: - if self.advance_payment_method == 'percentage': - amount = order.amount_untaxed * self.amount / 100 - else: - amount = self.fixed_amount + amount, name = self._get_advance_details(order) + if self.product_id.invoice_policy != 'order': raise UserError(_('The product used to invoice a down payment should have an invoice policy set to "Ordered quantities". Please update your deposit product to be able to create a deposit invoice.')) if self.product_id.type != 'service': @@ -143,19 +169,9 @@ class SaleAdvancePaymentInv(models.TransientModel): analytic_tag_ids = [] for line in order.order_line: analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in line.analytic_tag_ids] - so_line = sale_line_obj.create({ - 'name': _('Down Payment: %s') % (time.strftime('%m %Y'),), - 'price_unit': amount, - 'product_uom_qty': 0.0, - 'order_id': order.id, - 'discount': 0.0, - 'product_uom': self.product_id.uom_id.id, - 'product_id': self.product_id.id, - 'analytic_tag_ids': analytic_tag_ids, - 'tax_id': [(6, 0, tax_ids)], - 'is_downpayment': True, - 'sequence': order.order_line[-1].sequence + 1 - }) + + so_line_values = self._prepare_so_line(order, analytic_tag_ids, tax_ids, amount) + so_line = sale_line_obj.create(so_line_values) del context self._create_invoice(order, so_line, amount) if self._context.get('open_invoices', False): @@ -171,3 +187,4 @@ class SaleAdvancePaymentInv(models.TransientModel): 'taxes_id': [(6, 0, self.deposit_taxes_id.ids)], 'company_id': False, } +