Skip to content

Commit

Permalink
fix: test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikas8600 committed May 11, 2023
1 parent 31cfa60 commit a80582d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
24 changes: 11 additions & 13 deletions ecommerce_integrations/unicommerce/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -91,4 +88,5 @@ def create_delivery_note(order, settings, so):
else:
create_unicommerce_log(status="Success")
frappe.flags.request_id = None

return res

66 changes: 66 additions & 0 deletions ecommerce_integrations/unicommerce/tests/test_delivery_note.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a80582d

Please sign in to comment.