Skip to content

Commit

Permalink
[IMP] connector_magento: The creation of a transaction for the amount…
Browse files Browse the repository at this point in the history
… paid is added (only if the import of paid orders is used in the payment method and a payment acquirer is defined). If the amount paid is equal to the order total, the transaction will be marked as completed and the order will be auto-confirmed.
  • Loading branch information
victoralmau committed Sep 3, 2021
1 parent a9d45d9 commit 76ae249
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
27 changes: 26 additions & 1 deletion connector_magento/models/sale_order/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _rule_authorized(self, record, method):
def _rule_paid(self, record, method):
""" Import the order only if it has received a payment, or if there
is nothing to pay in the first place. """
amount_paid = record.get("payment", {}).get("amount_paid") or 0
amount_paid = float(record.get("payment", {}).get("amount_paid") or 0.0)
if record["grand_total"] and amount_paid <= 0:
raise OrderImportRuleRetry(
"The order has not been paid.\n" "The import will be retried later."
Expand Down Expand Up @@ -504,6 +504,30 @@ def _link_parent_orders(self, binding):
parent_binding.write({"canceled_in_backend": True})
current_binding = parent_binding

def _create_payment_transaction(self, binding):
order = binding.odoo_id
if (
order.payment_mode_id.import_rule == "paid"
and order.payment_mode_id.rule_paid_acquirer_id
):
amount_paid = float(
self.magento_record.get("payment", {}).get("amount_paid") or 0.0
)
transaction = self.env["payment.transaction"].create(
{
"acquirer_id": order.payment_mode_id.rule_paid_acquirer_id.id,
"amount": amount_paid,
"currency_id": order.currency_id.id,
"partner_id": order.partner_id.id,
"reference": order.name,
"sale_order_ids": [(4, order.id)],
"state": "pending",
}
)
if amount_paid == order.amount_total:
transaction.write({"state": "done", "date": order.date_order})
transaction._post_process_after_done()

def _create(self, data):
binding = super()._create(data)
if binding.fiscal_position_id:
Expand All @@ -512,6 +536,7 @@ def _create(self, data):

def _after_import(self, binding):
self._link_parent_orders(binding)
self._create_payment_transaction(binding)

def _get_storeview(self, record):
""" Return the tax inclusion setting for the appropriate storeview """
Expand Down
11 changes: 11 additions & 0 deletions connector_magento/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ def setUp(self):
}
)

journal_bank = self.env["account.journal"].search(
[("type", "=", "bank")], limit=1
)
self.magento_acquirer = self.env["payment.acquirer"].create(
{
"name": "Magento acquirer",
"provider": "transfer",
"journal_id": journal_bank.id,
}
)

def get_magento_helper(self, model_name):
return MagentoHelper(self.cr, self.registry, model_name)

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interactions:
Honey Bluff Parkway"],"telephone":"(555)
229-3326"},"payment":{"account_status":null,"additional_information":["Check
\/ Money
order"],"amount_ordered":396.87,"base_amount_ordered":396.87,"base_shipping_amount":5,"cc_exp_year":"0","cc_last4":null,"cc_ss_start_month":"0","cc_ss_start_year":"0","entity_id":9,"method":"checkmo","parent_id":9,"shipping_amount":5},"status_histories":[],"extension_attributes":{"shipping_assignments":[{"shipping":{"address":{"address_type":"shipping","city":"Calder","country_id":"US","customer_address_id":1,"email":"[email protected]","entity_id":17,"firstname":"Veronica","lastname":"Costello","parent_id":9,"postcode":"49628-7978","region":"Michigan","region_code":"MI","region_id":33,"street":["6146
order"],"amount_ordered":396.87,"base_amount_ordered":396.87,"amount_paid":421.3,"base_shipping_amount":5,"cc_exp_year":"0","cc_last4":null,"cc_ss_start_month":"0","cc_ss_start_year":"0","entity_id":9,"method":"checkmo","parent_id":9,"shipping_amount":5},"status_histories":[],"extension_attributes":{"shipping_assignments":[{"shipping":{"address":{"address_type":"shipping","city":"Calder","country_id":"US","customer_address_id":1,"email":"[email protected]","entity_id":17,"firstname":"Veronica","lastname":"Costello","parent_id":9,"postcode":"49628-7978","region":"Michigan","region_code":"MI","region_id":33,"street":["6146
Honey Bluff Parkway"],"telephone":"(555)
229-3326"},"method":"tablerate_bestway","total":{"base_shipping_amount":5,"base_shipping_discount_amount":0,"base_shipping_discount_tax_compensation_amnt":0,"base_shipping_incl_tax":5,"base_shipping_tax_amount":0,"shipping_amount":5,"shipping_discount_amount":0,"shipping_discount_tax_compensation_amount":0,"shipping_incl_tax":5,"shipping_tax_amount":0}},"items":[{"amount_refunded":0,"applied_rule_ids":"2,3","base_amount_refunded":0,"base_discount_amount":0,"base_discount_invoiced":0,"base_discount_tax_compensation_amount":0,"base_original_price":69,"base_price":69,"base_price_incl_tax":74.7,"base_row_invoiced":0,"base_row_total":138,"base_row_total_incl_tax":149.39,"base_tax_amount":11.39,"base_tax_invoiced":0,"created_at":"2020-04-24
15:50:17","discount_amount":0,"discount_invoiced":0,"discount_percent":0,"free_shipping":0,"discount_tax_compensation_amount":0,"is_qty_decimal":0,"is_virtual":0,"item_id":16,"name":"Abominable
Expand Down
17 changes: 17 additions & 0 deletions connector_magento/tests/magento2/test_magento2_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ def test_import_sale_order_eur_usd(self):
self.assertEqual(binding_13.currency_id, eur)
self.assertEqual(binding_13.amount_total, 269.56)

def test_import_sale_order_paid(self):
payment_checkmo = self.env["account.payment.mode"].search(
[("name", "=", "checkmo")]
)
payment_checkmo.write(
{"import_rule": "paid", "rule_paid_acquirer_id": self.magento_acquirer.id}
)
# We need to disable exceptions to prevent differents according to taxed
rule_a = self.env.ref("connector_magento.excep_wrong_total_amount")
rule_a.active = False
rule_b = self.env.ref("connector_magento.excep_wrong_total_amount_tax")
rule_b.active = False
binding = self._import_sale_order(9)
self.assertEqual(binding.state, "sale")
self.assertEqual(len(binding.transaction_ids), 1)
self.assertEqual(len(binding.picking_ids), 1)

def test_import_sale_order_with_prefix(self):
""" Import sale order with prefix """
self.backend.write({"sale_prefix": "EC"})
Expand Down
17 changes: 17 additions & 0 deletions connector_magento/tests/test_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def test_import_sale_order(self):
"onchanges have not been applied.",
)

def test_import_sale_order_paid(self):
payment_checkmo = self.env["account.payment.mode"].search(
[("name", "=", "checkmo")]
)
payment_checkmo.write(
{"import_rule": "paid", "rule_paid_acquirer_id": self.magento_acquirer.id}
)
# We need to disable exceptions to prevent differents according to taxed
rule_a = self.env.ref("connector_magento.excep_wrong_total_amount")
rule_a.active = False
rule_b = self.env.ref("connector_magento.excep_wrong_total_amount_tax")
rule_b.active = False
binding = self._import_sale_order(100000201)
self.assertEqual(binding.state, "sale")
self.assertEqual(len(binding.transaction_ids), 1)
self.assertEqual(len(binding.picking_ids), 1)

def test_import_sale_order_with_prefix(self):
""" Import sale order with prefix """
self.backend.write({"sale_prefix": "EC"})
Expand Down

0 comments on commit 76ae249

Please sign in to comment.