mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[FIX] contract_payment_auto: transaction create must always get a token (#167)
When a contrat had no payment token but the corresponding partner had one, the transaction was created without an acquirer, leading to an integrity error in postgres. This change makes sure the token used to test the ability to pay an invoice is passed along to the transaction creation call. Tests were also added to check the ability to use the contract token if present, but the partner's in the opposite case. This change fixes #165.
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
{
|
{
|
||||||
"name": "Contract - Auto Payment",
|
"name": "Contract - Auto Payment",
|
||||||
"summary": "Adds automatic payments to contracts.",
|
"summary": "Adds automatic payments to contracts.",
|
||||||
"version": "10.0.1.0.0",
|
"version": "10.0.1.0.1",
|
||||||
"category": "Contract Management",
|
"category": "Contract Management",
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"author": "LasLabs, "
|
"author": "LasLabs, "
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ class AccountAnalyticAccount(models.Model):
|
|||||||
return
|
return
|
||||||
|
|
||||||
transaction = self.env['payment.transaction'].create(
|
transaction = self.env['payment.transaction'].create(
|
||||||
self._get_tx_vals(invoice),
|
self._get_tx_vals(invoice, token),
|
||||||
)
|
)
|
||||||
valid_states = ['authorized', 'done']
|
valid_states = ['authorized', 'done']
|
||||||
|
|
||||||
@@ -136,10 +136,9 @@ class AccountAnalyticAccount(models.Model):
|
|||||||
return
|
return
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def _get_tx_vals(self, invoice):
|
def _get_tx_vals(self, invoice, token):
|
||||||
""" Return values for create of payment.transaction for invoice."""
|
""" Return values for create of payment.transaction for invoice."""
|
||||||
amount_due = invoice.residual
|
amount_due = invoice.residual
|
||||||
token = self.payment_token_id
|
|
||||||
partner = token.partner_id
|
partner = token.partner_id
|
||||||
reference = self.env['payment.transaction'].get_next_reference(
|
reference = self.env['payment.transaction'].get_next_reference(
|
||||||
invoice.number,
|
invoice.number,
|
||||||
|
|||||||
@@ -45,6 +45,13 @@ class TestAccountAnalyticAccount(common.HttpCase):
|
|||||||
'acquirer_id': self.acquirer.id,
|
'acquirer_id': self.acquirer.id,
|
||||||
'acquirer_ref': 'Test',
|
'acquirer_ref': 'Test',
|
||||||
})
|
})
|
||||||
|
self.other_payment_token = self.env['payment.token'].create({
|
||||||
|
'name': 'Test Other Token',
|
||||||
|
'partner_id': self.partner.id,
|
||||||
|
'active': True,
|
||||||
|
'acquirer_id': self.acquirer.id,
|
||||||
|
'acquirer_ref': 'OtherTest',
|
||||||
|
})
|
||||||
self.contract = self.Model.create({
|
self.contract = self.Model.create({
|
||||||
'name': 'Test Contract',
|
'name': 'Test Contract',
|
||||||
'partner_id': self.partner.id,
|
'partner_id': self.partner.id,
|
||||||
@@ -182,12 +189,33 @@ class TestAccountAnalyticAccount(common.HttpCase):
|
|||||||
res = self.contract._pay_invoice(invoice)
|
res = self.contract._pay_invoice(invoice)
|
||||||
self.assertIs(res, None)
|
self.assertIs(res, None)
|
||||||
|
|
||||||
def test_pay_invoice_success(self):
|
def assert_successful_pay_invoice(self, expected_token=None):
|
||||||
""" It should return True on success. """
|
|
||||||
with self._mock_transaction(s2s_side_effect=True):
|
with self._mock_transaction(s2s_side_effect=True):
|
||||||
invoice = self._create_invoice(True)
|
invoice = self._create_invoice(True)
|
||||||
res = self.contract._pay_invoice(invoice)
|
res = self.contract._pay_invoice(invoice)
|
||||||
self.assertTrue(res)
|
self.assertTrue(res)
|
||||||
|
if expected_token is not None:
|
||||||
|
Transactions = self.contract.env['payment.transaction']
|
||||||
|
tx_vals = Transactions.create.call_args[0][0]
|
||||||
|
self.assertEqual(tx_vals.get('payment_token_id'),
|
||||||
|
expected_token.id)
|
||||||
|
|
||||||
|
def test_pay_invoice_success(self):
|
||||||
|
""" It should return True on success. """
|
||||||
|
self.assert_successful_pay_invoice()
|
||||||
|
|
||||||
|
def test_pay_invoice_with_contract_token(self):
|
||||||
|
""" When contract and partner have a token, contract's is used. """
|
||||||
|
self.partner.payment_token_id = self.other_payment_token
|
||||||
|
self.contract.payment_token_id = self.payment_token
|
||||||
|
self.assert_successful_pay_invoice(expected_token=self.payment_token)
|
||||||
|
|
||||||
|
def test_pay_invoice_with_partner_token_success(self):
|
||||||
|
""" When contract has no related token, it should use partner's. """
|
||||||
|
self.contract.payment_token_id = False
|
||||||
|
self.partner.payment_token_id = self.other_payment_token
|
||||||
|
self.assert_successful_pay_invoice(
|
||||||
|
expected_token=self.other_payment_token)
|
||||||
|
|
||||||
@mute_logger(account_analytic_account.__name__)
|
@mute_logger(account_analytic_account.__name__)
|
||||||
def test_pay_invoice_exception(self):
|
def test_pay_invoice_exception(self):
|
||||||
@@ -243,7 +271,8 @@ class TestAccountAnalyticAccount(common.HttpCase):
|
|||||||
def test_get_tx_vals(self):
|
def test_get_tx_vals(self):
|
||||||
""" It should return a dict. """
|
""" It should return a dict. """
|
||||||
self.assertIsInstance(
|
self.assertIsInstance(
|
||||||
self.contract._get_tx_vals(self._create_invoice()),
|
self.contract._get_tx_vals(self._create_invoice(),
|
||||||
|
self.contract.payment_token_id),
|
||||||
dict,
|
dict,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user