Merge branch '8.0' of github.com:akretion/odoo-usability into 8.0

This commit is contained in:
Alexis de Lattre
2015-04-28 16:14:16 +02:00

View File

@@ -30,6 +30,34 @@ logger = logging.getLogger(__name__)
class AccountInvoice(models.Model):
_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
def invoice_validate(self):
'''Create Direct debit payment order on invoice validation or update
@@ -55,10 +83,8 @@ class AccountInvoice(models.Model):
payorder = payorders[0]
payorder_type = _('existing')
else:
payorder = poo.create({
'mode': invoice.payment_mode_id.id,
'payment_order_type': 'debit',
})
payorder_vals = self._prepare_payment_order(invoice)
payorder = poo.create(payorder_vals)
payorder_type = _('new')
logger.info(
'New Direct Debit Order created %s'
@@ -66,31 +92,16 @@ class AccountInvoice(models.Model):
move_lines = [
line for line in invoice.move_id.line_id
if line.account_id == invoice.account_id]
if len(move_lines) != 1:
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]
for move_line in move_lines:
if not invoice.mandate_id:
raise Warning(
_("Missing Mandate on invoice %s" % invoice.number))
_("Missing Mandate on invoice %s")
% invoice.number)
# add payment line
plo.create({
'order_id': payorder.id,
'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,
})
pl_vals = self._prepare_payment_line(move_line, payorder)
pl = plo.create(pl_vals)
invoice.message_post(
_("A new payment line has been automatically created "
"on the %s direct debit order %s")
% (payorder_type, payorder.reference))
_("A new payment line %s has been automatically "
"created on the %s direct debit order %s")
% (pl.name, payorder_type, payorder.reference))
return res