Use _prepare methods that are easy to inherit
Add support for multi-term invoices
This commit is contained in:
@@ -30,6 +30,34 @@ logger = logging.getLogger(__name__)
|
|||||||
class AccountInvoice(models.Model):
|
class AccountInvoice(models.Model):
|
||||||
_inherit = 'account.invoice'
|
_inherit = 'account.invoice'
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _prepare_payment_order(self, invoice):
|
||||||
|
vals = {
|
||||||
|
'mode': invoice.payment_mode_id.id,
|
||||||
|
'payment_order_type': 'debit',
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _prepare_payment_line(self, move_line, payment_order):
|
||||||
|
assert move_line.invoice, 'The move line must be linked to an invoice'
|
||||||
|
if not move_line.invoice.mandate_id:
|
||||||
|
raise Warning(
|
||||||
|
_('Missing Mandate on Invoice %s') % move_line.invoice.number)
|
||||||
|
vals = {
|
||||||
|
'order_id': payment_order.id,
|
||||||
|
'move_line_id': move_line.id,
|
||||||
|
'partner_id': move_line.partner_id.id,
|
||||||
|
'amount_currency': move_line.amount_to_receive,
|
||||||
|
'communication': move_line.invoice.number.replace('/', ''),
|
||||||
|
'state': 'structured',
|
||||||
|
'date': move_line.date_maturity,
|
||||||
|
'currency': move_line.invoice.currency_id.id,
|
||||||
|
'mandate_id': move_line.invoice.mandate_id.id,
|
||||||
|
'bank_id': move_line.invoice.mandate_id.partner_bank_id.id,
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def invoice_validate(self):
|
def invoice_validate(self):
|
||||||
'''Create Direct debit payment order on invoice validation or update
|
'''Create Direct debit payment order on invoice validation or update
|
||||||
@@ -55,10 +83,8 @@ class AccountInvoice(models.Model):
|
|||||||
payorder = payorders[0]
|
payorder = payorders[0]
|
||||||
payorder_type = _('existing')
|
payorder_type = _('existing')
|
||||||
else:
|
else:
|
||||||
payorder = poo.create({
|
payorder_vals = self._prepare_payment_order(invoice)
|
||||||
'mode': invoice.payment_mode_id.id,
|
payorder = poo.create(payorder_vals)
|
||||||
'payment_order_type': 'debit',
|
|
||||||
})
|
|
||||||
payorder_type = _('new')
|
payorder_type = _('new')
|
||||||
logger.info(
|
logger.info(
|
||||||
'New Direct Debit Order created %s'
|
'New Direct Debit Order created %s'
|
||||||
@@ -66,31 +92,16 @@ class AccountInvoice(models.Model):
|
|||||||
move_lines = [
|
move_lines = [
|
||||||
line for line in invoice.move_id.line_id
|
line for line in invoice.move_id.line_id
|
||||||
if line.account_id == invoice.account_id]
|
if line.account_id == invoice.account_id]
|
||||||
if len(move_lines) != 1:
|
for move_line in move_lines:
|
||||||
raise Warning(
|
|
||||||
_("We do not support multi-term invoices via "
|
|
||||||
"Direct Debit for the moment. We can't "
|
|
||||||
"automatically create the Direct Debit Order "
|
|
||||||
"for the invoice %s") % invoice.number)
|
|
||||||
move_line = move_lines[0]
|
|
||||||
if not invoice.mandate_id:
|
if not invoice.mandate_id:
|
||||||
raise Warning(
|
raise Warning(
|
||||||
_("Missing Mandate on invoice %s" % invoice.number))
|
_("Missing Mandate on invoice %s")
|
||||||
|
% invoice.number)
|
||||||
# add payment line
|
# add payment line
|
||||||
plo.create({
|
pl_vals = self._prepare_payment_line(move_line, payorder)
|
||||||
'order_id': payorder.id,
|
pl = plo.create(pl_vals)
|
||||||
'move_line_id': move_line.id,
|
|
||||||
'partner_id': move_line.partner_id.id,
|
|
||||||
'amount_currency': move_line.amount_to_receive,
|
|
||||||
'communication': invoice.number.replace('/', ''),
|
|
||||||
'state': 'structured',
|
|
||||||
'date': move_line.date_maturity,
|
|
||||||
'currency': invoice.currency_id.id,
|
|
||||||
'mandate_id': invoice.mandate_id.id,
|
|
||||||
'bank_id': invoice.mandate_id.partner_bank_id.id,
|
|
||||||
})
|
|
||||||
invoice.message_post(
|
invoice.message_post(
|
||||||
_("A new payment line has been automatically created "
|
_("A new payment line %s has been automatically "
|
||||||
"on the %s direct debit order %s")
|
"created on the %s direct debit order %s")
|
||||||
% (payorder_type, payorder.reference))
|
% (pl.name, payorder_type, payorder.reference))
|
||||||
return res
|
return res
|
||||||
|
|||||||
Reference in New Issue
Block a user