From a80582df0a6736d0d101a55f6e9a5f25d57ced3b Mon Sep 17 00:00:00 2001 From: Vikas8600 Date: Thu, 11 May 2023 23:24:48 +0530 Subject: [PATCH] fix: test cases added --- .../unicommerce/delivery_note.py | 24 ++++--- .../unicommerce/tests/test_delivery_note.py | 66 +++++++++++++++++++ 2 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 ecommerce_integrations/unicommerce/tests/test_delivery_note.py diff --git a/ecommerce_integrations/unicommerce/delivery_note.py b/ecommerce_integrations/unicommerce/delivery_note.py index 1a089830..e149b83c 100644 --- a/ecommerce_integrations/unicommerce/delivery_note.py +++ b/ecommerce_integrations/unicommerce/delivery_note.py @@ -29,21 +29,18 @@ def prepare_delivery_note(): continue shipped_packages = [p for p in valid_packages if p["status"] in ["DISPATCHED"]] for order in shipped_packages: - if not frappe.db.exists("Delivery Note", {"unicommerce_shipment_id": order["code"]}, "name"): - if frappe.db.exists("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]}): - sales_order = frappe.get_doc("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]}) - create_delivery_note(order, settings, sales_order) + if not frappe.db.exists("Delivery Note", {"unicommerce_shipment_id": order["code"]}, "name") and frappe.db.exists("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]}) and frappe.db.exists("Sales Invoice", {"unicommerce_order_code": sales_order.unicommerce_order_code}): + sales_order = frappe.get_doc("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]}) + sales_invoice = frappe.get_doc( + "Sales Invoice", {"unicommerce_order_code": sales_order.unicommerce_order_code} + ) + create_delivery_note(sales_order,sales_invoice) except Exception as e: create_unicommerce_log(status="Error", exception=e, rollback=True) -def create_delivery_note(order, settings, so): +def create_delivery_note(so,sales_invoice): try: - # Get the sales invoice - sales_invoice = frappe.get_doc( - "Sales Invoice", {"unicommerce_order_code": so.unicommerce_order_code} - ) - # Create the delivery note from frappe.model.mapper import make_mapped_doc @@ -80,8 +77,8 @@ def create_delivery_note(order, settings, so): "dont_recompute_tax": item.dont_recompute_tax, }, ) - res.unicommerce_order_code = order["saleOrderCode"] - res.unicommerce_shipment_id = order["code"] + res.unicommerce_order_code = sales_invoice.unicommerce_order_code + res.unicommerce_shipment_id = sales_invoice.unicommerce_shipping_package_code res.save() res.submit() log = create_unicommerce_log(method="create_delevery_note", make_new=True) @@ -91,4 +88,5 @@ def create_delivery_note(order, settings, so): else: create_unicommerce_log(status="Success") frappe.flags.request_id = None - + return res + diff --git a/ecommerce_integrations/unicommerce/tests/test_delivery_note.py b/ecommerce_integrations/unicommerce/tests/test_delivery_note.py new file mode 100644 index 00000000..80e2d1ad --- /dev/null +++ b/ecommerce_integrations/unicommerce/tests/test_delivery_note.py @@ -0,0 +1,66 @@ +import unittest +import base64 +import responses +import frappe + +from ecommerce_integrations.unicommerce.constants import ( + FACILITY_CODE_FIELD, + INVOICE_CODE_FIELD, + ORDER_CODE_FIELD, + SHIPPING_PACKAGE_CODE_FIELD, +) +from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry +from ecommerce_integrations.unicommerce.invoice import create_sales_invoice,bulk_generate_invoices +from ecommerce_integrations.unicommerce.order import create_order +from ecommerce_integrations.unicommerce.tests.test_client import TestCaseApiClient +from ecommerce_integrations.unicommerce.delivery_note import create_delivery_note + +class TestDeliveryNote(TestCaseApiClient): + @classmethod + def setUpClass(cls): + super().setUpClass() + + + def test_create_invoice_and_delivery_note(self): + """Use mocked invoice json to create and assert synced fields""" + from ecommerce_integrations.unicommerce import invoice + + si_data = self.load_fixture("invoice-SDU0026")["invoice"] + + # HACK to allow invoicing test + invoice.INVOICED_STATE.append("CREATED") + self.responses.add( + responses.POST, + "https://demostaging.unicommerce.com/services/rest/v1/oms/shippingPackage/createInvoiceAndAllocateShippingProvider", + status=200, + json=self.load_fixture("create_invoice_and_assign_shipper"), + match=[responses.json_params_matcher({"shippingPackageCode": "TEST00949"})], + ) + self.responses.add( + responses.POST, + "https://demostaging.unicommerce.com/services/rest/v1/invoice/details/get", + status=200, + json=self.load_fixture("invoice-SDU0026"), + match=[responses.json_params_matcher({"shippingPackageCode": "TEST00949", "return": False})], + ) + self.responses.add( + responses.GET, + "https://example.com", + status=200, + body=base64.b64decode(self.load_fixture("invoice_label_response")["label"]), + ) + + order = self.load_fixture("order-SO5906")["saleOrderDTO"] + so = create_order(order, client=self.client) + make_stock_entry(item_code="MC-100", qty=15, to_warehouse="Stores - WP", rate=42) + + bulk_generate_invoices(sales_orders=[so.name], client=self.client) + + sales_invoice_code = frappe.db.get_value("Sales Invoice", {INVOICE_CODE_FIELD: "SDU0026"}) + + if not sales_invoice_code: + self.fail("Sales invoice not generated") + + si = frappe.get_doc("Sales Invoice", sales_invoice_code) + dn = create_delivery_note(so,si) + self.assertEqual(dn.unicommerce_order_code, so.unicommerce_order_code)