[FIX] sale: allow inheriting and modifying function values

closes odoo/odoo#44069

X-original-commit: 14a12ced1d5cb79b9ec8b9df926a12330aa34cf1
Signed-off-by: Damien Bouvy (dbo) <dbo@odoo.com>
This commit is contained in:
Yenthe666
2020-01-28 09:56:51 +00:00
committed by Damien Bouvy
parent b32449d14f
commit 65fd8e4602
2 changed files with 48 additions and 31 deletions
@@ -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
+44 -27
View File
@@ -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,
}