From f47430133d1341d810f66c8e50c9d27e5f9c5ec5 Mon Sep 17 00:00:00 2001 From: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Mon, 5 Jun 2023 13:47:46 +0530 Subject: [PATCH] feat: (Uni-commerce) generate Delivery Note and sync item fields (#239) Squashed commit of the following: commit b3bf98d9a75d3d012b54eb4bc248a0dd31d9ccbb Merge: 13928e9 fa461c8 Author: Ankush Menat Date: Mon Jun 5 13:09:31 2023 +0530 Merge branch 'develop' into dev-fix commit 13928e9a39754c361faee32444d26c5fd1fd1f81 Author: Vikas8600 Date: Sat May 13 06:15:59 2023 +0530 fix: pre-commit commit 1eba131e3714a26977603ebbbc6749244eda8aa3 Author: Ankush Menat Date: Fri May 12 14:28:49 2023 +0530 chore: format and trigger tests Commiting this myself to allow workflows to run (first contribution restriction) commit c284ac71a7ea809928766012a20df44cf07fe24f Author: Vikas8600 Date: Fri May 12 14:05:48 2023 +0530 fix: reformatted commit 5fd6611264c4b95e6411b246385d7e038b3d27f4 Author: Vikas8600 Date: Fri May 12 14:04:24 2023 +0530 fix: pre-commit commit 708bbf26faf8152e1c72893a501b176511d0be6e Author: Vikas8600 Date: Fri May 12 14:03:40 2023 +0530 fix: pre-commit commit 511fdc677931190b14b9052c80b6bec45d9163e5 Author: Vikas8600 Date: Thu May 11 23:31:18 2023 +0530 fixed commit a80582df0a6736d0d101a55f6e9a5f25d57ced3b Author: Vikas8600 Date: Thu May 11 23:24:48 2023 +0530 fix: test cases added commit 31cfa6023d9e28453d31d922918b0421be687988 Author: Vikas8600 Date: Thu May 4 21:30:08 2023 +0530 fix: log statement commit 28568c5be98547aaf1892042140dbcb0882f606a Author: Vikas8600 Date: Tue May 2 17:38:23 2023 +0530 fix: item changes commit 770e3a49c97adb5f8487a90f608a82f0a0a2cc4b Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 17:28:02 2023 +0530 Delete ci2.yml commit 70dcc4a5bc5ff7c6df5b3c7e00ba9b077fb13297 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 17:23:29 2023 +0530 Update and rename dev_fix.yml to ci2.yml commit 9ae78a6ded879c69f9cf1f7390561c6c043af5ec Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 17:21:17 2023 +0530 Update dev_fix.yml commit 3dafd106bb96aa3e414446c66f4b633703fb072c Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 17:19:54 2023 +0530 Create dev_fix.yml commit bae33f3ff415474d10b6b9c1dfd71dfc5b76e0c3 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 17:00:40 2023 +0530 Update ci.yml commit be20d74d978b83e4409eaca05ab5d42473001348 Author: Vikas8600 Date: Tue May 2 16:04:47 2023 +0530 fix: code changes commit 4b24fa638675ea1dc5e1b93d3f1e49cb4c3a2062 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 15:32:30 2023 +0530 Update ci.yml commit a277f33670b7ecd66094c7e619163f5cb75a1fb2 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Tue May 2 15:17:20 2023 +0530 Update ci.yml commit 82e688cb0dcbb2c5ed8a7e9d078516aeb6fb5c61 Author: Vikas8600 Date: Tue May 2 13:14:35 2023 +0530 fix: code changes commit e9a46d46a3c815f057e070d95f03d7edad2a5996 Author: Vikas8600 Date: Tue May 2 10:49:58 2023 +0530 feat: delivery note feature commit 0b4d68cc064b8f9972f7f7356da77be0d378ef80 Author: Vikas8600 Date: Tue May 2 10:32:14 2023 +0530 fix: code changes commit a2be0f88c78d6628f88c8f444fe71a9bafe1d995 Author: Vikas8600 Date: Mon May 1 22:38:16 2023 +0530 fix: test product commit db7982d7d53a1e7d5266e49d7b3a530fd6885c35 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Mon May 1 20:29:42 2023 +0530 Update product-MC-100.json commit eb5c82ee16331c56d4ca2e8a6104baac480f5a87 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Mon May 1 20:27:54 2023 +0530 Update product-MC-100.json commit 1e2071ce04ea234c64be778b4f3161080c61f613 Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Mon May 1 19:39:59 2023 +0530 Update delivery_note.py commit 087f02a89d376b7a011583a5ece40d2c30568cbd Author: Vikas8600 <49873594+Vikas8600@users.noreply.github.com> Date: Mon May 1 19:38:45 2023 +0530 Update ci.yml commit 40adea6ee75dfab47719ea1064b9ac5154b87ab9 Author: Vikas8600 Date: Mon May 1 15:10:30 2023 +0530 fix: sales invoice getting skip commit d56d55e0719cabbd9c90171049e066b52195c995 Author: Vikas8600 Date: Fri Apr 28 15:39:31 2023 +0530 fix: code related changes commit ec99301036ee81d74bf5e7dfda4ce8fbc046844c Author: Vikas8600 Date: Fri Apr 28 15:37:11 2023 +0530 fix: code related changes commit f7ecd889cd6c50ec3195bda37f62a5d6e2232d94 Author: Ankush Menat Date: Wed Apr 26 11:34:30 2023 +0530 chore: format commit ea407d9d2118c9340c8a265e8d5aab30f849254d Author: Vikas8600 Date: Fri Apr 21 17:53:38 2023 +0530 fix: debit to account commit 901fe1458afa61986881838c32c34f183797a767 Author: Vikas8600 Date: Fri Apr 21 14:17:56 2023 +0530 fix: remove html tag from Description commit d3155d3463799f0e51cb2be3c1db0248e2c84582 Author: Vikas8600 Date: Thu Apr 20 19:32:01 2023 +0530 fix: address gst category commit 79be0016ca5d2ecb4c913c7620665f40a6a6e74f Author: Vikas8600 Date: Wed Apr 19 15:36:36 2023 +0530 fix: remove unuse code commit ef111206462fc3834b0eeb5bf9cedf1ec6d8b4ac Author: Vikas8600 Date: Wed Apr 19 15:05:44 2023 +0530 feat: Delivery Note button commit 22b7b6e61ad7992fb9f2464b1f81a078dfad183b Author: Vikas8600 Date: Wed Apr 19 13:03:15 2023 +0530 fix: logger commit 67ce869d0562d73f62a19d323fe0a46169040d6d Author: Vikas8600 Date: Wed Apr 19 12:55:05 2023 +0530 logger added commit 533928eda660231abd380f9d90f1558b5a74dd0e Author: Vikas8600 Date: Tue Apr 18 15:35:58 2023 +0530 feat: item resync commit deb9fdbe9a30b7c7232dcc0fce0ce51bbcd494a6 Author: Vikas8600 Date: Mon Apr 17 11:45:06 2023 +0530 feat: added valuation rate commit 7f3fe72e430b7583fb893f55c45d0a6bbb890644 Author: Vikas8600 Date: Sun Apr 16 10:38:31 2023 +0530 fix: item stock commit 7b5e73341234f9087ae058270ec56d55d637b8f8 Author: Vikas8600 Date: Fri Apr 14 22:12:09 2023 +0530 fixed commit c71c88651ac1f88af446617d8d0f75c1c8b54bef Author: Vikas8600 Date: Fri Apr 14 21:33:14 2023 +0530 fixed commit 5813d32f5c4f2b4bb722c91d5e9cf026a18d2960 Author: Vikas8600 Date: Fri Apr 14 19:17:05 2023 +0530 fixed commit 8c397270e248e67af6f44d72929ff8bf62813fc9 Author: Vikas8600 Date: Fri Apr 14 16:50:18 2023 +0530 fix: hcn_code commit 5a1d22d55bdbb25320b2d19d76a892ead7ab6c3a Author: Vikas8600 Date: Fri Apr 14 16:33:01 2023 +0530 feat: taxTempType commit e7d42f582fbe8e2239521696e88f6594a4d694ef Author: Vikas8600 Date: Fri Apr 14 14:23:44 2023 +0530 fix: item warehouse commit 07b8c193c715fdb664afaec954302d1cc3bdec70 Author: Vikas8600 Date: Wed Apr 12 14:00:14 2023 +0530 fix: delivery note commit a9a73f98f4aab1f1c7bf619186ab761309196ea2 Author: Vikas8600 Date: Wed Apr 12 12:48:37 2023 +0530 fix: param commit 0d288e7b470ff3aa60de2a299de5787f4f172bab Author: Vikas8600 Date: Wed Apr 12 12:42:45 2023 +0530 fix: param added commit 0e18d3c3e37e63fa35d7606e9a7bb19b10b92ac1 Author: Vikas8600 Date: Wed Apr 12 12:01:20 2023 +0530 feat: custome fields commit bd24b34fcada08f4fbd5e5bbc96977c884333b7f Author: Vikas8600 Date: Wed Apr 12 11:21:30 2023 +0530 feat: delivery note commit 8f9e1403f093de9f0a403919dcabfd1f9c9cd581 Author: Vikas8600 Date: Thu Apr 6 22:21:19 2023 +0530 fix: sales invoice commit f70516498db2dafb36181a3f5c79665f9b619a85 Author: Vikas8600 Date: Thu Apr 6 17:41:42 2023 +0530 fix: sales invoice commit a5c06936b1f0210f47455e72d485a2f9805033d8 Author: Vikas860086 Date: Thu Apr 6 14:38:14 2023 +0530 fix: sales invoice and item fields Co-authored-by: Ankush Menat --- ecommerce_integrations/hooks.py | 1 + .../unicommerce/constants.py | 1 + .../unicommerce/delivery_note.py | 99 +++++++++++++++++++ .../unicommerce_settings.json | 15 ++- .../unicommerce_settings.py | 26 +++++ ecommerce_integrations/unicommerce/invoice.py | 2 +- ecommerce_integrations/unicommerce/order.py | 29 +++--- ecommerce_integrations/unicommerce/product.py | 5 + .../unicommerce/tests/test_delivery_note.py | 65 ++++++++++++ 9 files changed, 230 insertions(+), 13 deletions(-) create mode 100644 ecommerce_integrations/unicommerce/delivery_note.py create mode 100644 ecommerce_integrations/unicommerce/tests/test_delivery_note.py diff --git a/ecommerce_integrations/hooks.py b/ecommerce_integrations/hooks.py index 5d2d97d1..f28de191 100644 --- a/ecommerce_integrations/hooks.py +++ b/ecommerce_integrations/hooks.py @@ -148,6 +148,7 @@ "*/5 * * * *": [ "ecommerce_integrations.unicommerce.order.sync_new_orders", "ecommerce_integrations.unicommerce.inventory.update_inventory_on_unicommerce", + "ecommerce_integrations.unicommerce.delivery_note.prepare_delivery_note", ], }, } diff --git a/ecommerce_integrations/unicommerce/constants.py b/ecommerce_integrations/unicommerce/constants.py index 9bc2ba8c..6e57bcee 100644 --- a/ecommerce_integrations/unicommerce/constants.py +++ b/ecommerce_integrations/unicommerce/constants.py @@ -33,6 +33,7 @@ SHIPPING_PACKAGE_STATUS_FIELD = "unicommerce_shipping_package_status" IS_COD_CHECKBOX = "unicommerce_is_cod" SHIPPING_METHOD_FIELD = "unicommerce_shipping_method" +UNICOMMERCE_SHIPPING_ID = "unicommerce_shipment_id" PICKLIST_ORDER_DETAILS_FIELD = "order_details" GRN_STOCK_ENTRY_TYPE = "GRN on Unicommerce" diff --git a/ecommerce_integrations/unicommerce/delivery_note.py b/ecommerce_integrations/unicommerce/delivery_note.py new file mode 100644 index 00000000..c578f6ab --- /dev/null +++ b/ecommerce_integrations/unicommerce/delivery_note.py @@ -0,0 +1,99 @@ +import frappe + +from ecommerce_integrations.unicommerce.api_client import UnicommerceAPIClient +from ecommerce_integrations.unicommerce.constants import ORDER_CODE_FIELD, SETTINGS_DOCTYPE +from ecommerce_integrations.unicommerce.utils import create_unicommerce_log + + +@frappe.whitelist() +def prepare_delivery_note(): + try: + settings = frappe.get_cached_doc(SETTINGS_DOCTYPE) + if not settings.delivery_note: + return + + client = UnicommerceAPIClient() + + days_to_sync = min(settings.get("order_status_days") or 2, 14) + minutes = days_to_sync * 24 * 60 + + # find all Facilities + enabled_facilities = list(settings.get_integration_to_erpnext_wh_mapping().keys()) + enabled_channels = frappe.db.get_list( + "Unicommerce Channel", filters={"enabled": 1}, pluck="channel_id" + ) + + for facility in enabled_facilities: + updated_packages = client.search_shipping_packages( + updated_since=minutes, facility_code=facility + ) + valid_packages = [p for p in updated_packages if p.get("channel") in enabled_channels] + if not valid_packages: + 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" + ) and frappe.db.exists("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]}): + sales_order = frappe.get_doc("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]}) + if frappe.db.exists( + "Sales Invoice", {"unicommerce_order_code": sales_order.unicommerce_order_code} + ): + 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(so, sales_invoice): + try: + # Create the delivery note + from frappe.model.mapper import make_mapped_doc + + res = make_mapped_doc( + method="erpnext.selling.doctype.sales_order.sales_order.make_delivery_note", source_name=so.name + ) + res.update({"items": []}) + for item in sales_invoice.items: + res.append( + "items", + { + "item_code": item.item_code, + "item_name": item.item_name, + "description": item.description, + "qty": item.qty, + "uom": item.uom, + "rate": item.rate, + "amount": item.amount, + "warehouse": item.warehouse, + "against_sales_order": item.sales_order, + "batch_no": item.batch_no, + "so_detail": item.so_detail, + }, + ) + for item in sales_invoice.taxes: + res.append( + "taxes", + { + "charge_type": item.charge_type, + "account_head": item.account_head, + "tax_amount": item.tax_amount, + "description": item.description, + "item_wise_tax_detail": item.item_wise_tax_detail, + "dont_recompute_tax": item.dont_recompute_tax, + }, + ) + 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) + frappe.flags.request_id = log.name + except Exception as e: + create_unicommerce_log(status="Error", exception=e, rollback=True) + else: + create_unicommerce_log(status="Success") + frappe.flags.request_id = None + return res diff --git a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json index 65aac5d9..16aedf9e 100644 --- a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json +++ b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json @@ -29,6 +29,8 @@ "sales_order_series", "sales_invoice_series", "order_status_days", + "delivery_note_settings_section", + "delivery_note", "inventory_sync_settings_section", "enable_inventory_sync", "inventory_sync_frequency", @@ -249,12 +251,23 @@ "fieldname": "vendor_code", "fieldtype": "Data", "label": "Vendor Code" + }, + { + "fieldname": "delivery_note_settings_section", + "fieldtype": "Section Break", + "label": "Delivery Note Settings" + }, + { + "default": "0", + "fieldname": "delivery_note", + "fieldtype": "Check", + "label": "Import Delivery Notes from Unicommerce on Shipment" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2022-02-07 14:11:34.599710", + "modified": "2023-05-02 14:04:26.684256", "modified_by": "Administrator", "module": "unicommerce", "name": "Unicommerce Settings", diff --git a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.py b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.py index dc60d8af..cd5a46da 100644 --- a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.py +++ b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.py @@ -42,6 +42,7 @@ SHIPPING_PACKAGE_STATUS_FIELD, SHIPPING_PROVIDER_CODE, TRACKING_CODE_FIELD, + UNICOMMERCE_SHIPPING_ID, ) from ecommerce_integrations.unicommerce.utils import create_unicommerce_log @@ -202,6 +203,15 @@ def setup_custom_fields(update=True): collapsible=1, ), ], + "Delivery Note": [ + dict( + fieldname="unicommerce_section", + label="Unicommerce Details", + fieldtype="Section Break", + insert_after="instructions", + collapsible=1, + ), + ], } custom_fields = { @@ -429,6 +439,22 @@ def setup_custom_fields(update=True): read_only=1, ), ], + "Delivery Note": [ + dict( + fieldname=ORDER_CODE_FIELD, + label="Unicommerce Order No", + fieldtype="Data", + insert_after="unicommerce_section", + read_only=1, + ), + dict( + fieldname=UNICOMMERCE_SHIPPING_ID, + label="Unicommerce Shipment Id", + fieldtype="Data", + insert_after=ORDER_CODE_FIELD, + read_only=1, + ), + ], "Pick List": [ dict( fieldname=PICKLIST_ORDER_DETAILS_FIELD, diff --git a/ecommerce_integrations/unicommerce/invoice.py b/ecommerce_integrations/unicommerce/invoice.py index d9d3bbed..4e5cffda 100644 --- a/ecommerce_integrations/unicommerce/invoice.py +++ b/ecommerce_integrations/unicommerce/invoice.py @@ -381,7 +381,7 @@ def create_sales_invoice( si.naming_series = channel_config.sales_invoice_series or settings.sales_invoice_series si.delivery_date = so.delivery_date si.ignore_pricing_rule = 1 - si.update_stock = update_stock + si.update_stock = False if settings.delivery_note else update_stock si.flags.raw_data = si_data si.insert() diff --git a/ecommerce_integrations/unicommerce/order.py b/ecommerce_integrations/unicommerce/order.py index 1c7e4824..6b1ea741 100644 --- a/ecommerce_integrations/unicommerce/order.py +++ b/ecommerce_integrations/unicommerce/order.py @@ -12,6 +12,7 @@ CHANNEL_ID_FIELD, CHANNEL_TAX_ACCOUNT_FIELD_MAP, FACILITY_CODE_FIELD, + INVOICE_CODE_FIELD, IS_COD_CHECKBOX, MODULE_NAME, ORDER_CODE_FIELD, @@ -78,9 +79,8 @@ def _get_new_orders( for order in uni_orders: if order["channel"] not in configured_channels: continue - if frappe.db.exists("Sales Order", {ORDER_CODE_FIELD: order["code"]}): - continue + # In case a sales invoice is not generated for some reason and is skipped, we need to create it manually. Therefore, I have commented out this line of code. order = client.get_sales_order(order_code=order["code"]) if order: yield order @@ -95,12 +95,19 @@ def _create_sales_invoices(unicommerce_order, sales_order, client: UnicommerceAP shipping_packages = unicommerce_order["shippingPackages"] for package in shipping_packages: try: - log = create_unicommerce_log(method="create_sales_invoice", make_new=True) - frappe.flags.request_id = log.name - + # This code was added because the log statement below was being executed every time. invoice_data = client.get_sales_invoice( shipping_package_code=package["code"], facility_code=facility_code ) + existing_si = frappe.db.get_value( + "Sales Invoice", {INVOICE_CODE_FIELD: invoice_data["invoice"]["code"]} + ) + if existing_si: + continue + + log = create_unicommerce_log(method="create_sales_invoice", make_new=True) + frappe.flags.request_id = log.name + warehouse_allocations = _get_warehouse_allocations(sales_order) create_sales_invoice( invoice_data["invoice"], @@ -121,18 +128,18 @@ def create_order(payload: UnicommerceOrder, request_id: Optional[str] = None, cl order = payload + existing_so = frappe.db.get_value("Sales Order", {ORDER_CODE_FIELD: order["code"]}) + if existing_so: + so = frappe.get_doc("Sales Order", existing_so) + return so + + # If a sales order already exists, then every time it's executed if request_id is None: log = create_unicommerce_log( method="ecommerce_integrations.unicommerce.order.create_order", request_data=payload ) request_id = log.name - existing_so = frappe.db.get_value("Sales Order", {ORDER_CODE_FIELD: order["code"]}) - if existing_so: - so = frappe.get_doc("Sales Order", existing_so) - create_unicommerce_log(status="Invalid", message="Sales Order already exists, skipped") - return so - if client is None: client = UnicommerceAPIClient() diff --git a/ecommerce_integrations/unicommerce/product.py b/ecommerce_integrations/unicommerce/product.py index f98a9310..553bc9e3 100644 --- a/ecommerce_integrations/unicommerce/product.py +++ b/ecommerce_integrations/unicommerce/product.py @@ -39,6 +39,8 @@ "width": ITEM_WIDTH_FIELD, "height": ITEM_HEIGHT_FIELD, "batchGroupCode": ITEM_BATCH_GROUP_FIELD, + "maxRetailPrice": "standard_rate", + "costPrice": "valuation_rate", } ERPNEXT_TO_UNI_ITEM_MAPPING = {v: k for k, v in UNI_TO_ERPNEXT_ITEM_MAPPING.items()} @@ -287,6 +289,9 @@ def _build_unicommerce_item(item_code: ItemCode) -> JsonDict: ) # append site prefix to image url item_json["imageUrl"] = get_url(item.image) + item_json["maxRetailPrice"] = item.standard_rate + item_json["description"] = frappe.utils.strip_html_tags(item.description) + item_json["costPrice"] = item.valuation_rate return item_json 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..205ad8e1 --- /dev/null +++ b/ecommerce_integrations/unicommerce/tests/test_delivery_note.py @@ -0,0 +1,65 @@ +import base64 +import unittest + +import frappe +import responses +from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry + +from ecommerce_integrations.unicommerce.constants import ( + FACILITY_CODE_FIELD, + INVOICE_CODE_FIELD, + ORDER_CODE_FIELD, + SHIPPING_PACKAGE_CODE_FIELD, +) +from ecommerce_integrations.unicommerce.delivery_note import create_delivery_note +from ecommerce_integrations.unicommerce.invoice import bulk_generate_invoices, create_sales_invoice +from ecommerce_integrations.unicommerce.order import create_order +from ecommerce_integrations.unicommerce.tests.test_client import TestCaseApiClient + + +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 + + # 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)