Skip to content

Commit

Permalink
feat: delivery note
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikas8600 committed Apr 12, 2023
1 parent 8f9e140 commit bd24b34
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 7 deletions.
1 change: 1 addition & 0 deletions ecommerce_integrations/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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"
],
},
}
Expand Down
106 changes: 106 additions & 0 deletions ecommerce_integrations/unicommerce/delivery_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import frappe
from ecommerce_integrations.unicommerce.constants import (
CHANNEL_ID_FIELD,
CHANNEL_TAX_ACCOUNT_FIELD_MAP,
FACILITY_CODE_FIELD,
IS_COD_CHECKBOX,
MODULE_NAME,
ORDER_CODE_FIELD,
ORDER_ITEM_BATCH_NO,
ORDER_ITEM_CODE_FIELD,
ORDER_STATUS_FIELD,
PACKAGE_TYPE_FIELD,
SETTINGS_DOCTYPE,
TAX_FIELDS_MAPPING,
TAX_RATE_FIELDS_MAPPING,
)
from ecommerce_integrations.unicommerce.api_client import UnicommerceAPIClient
from ecommerce_integrations.unicommerce.order import _get_new_orders
from ecommerce_integrations.unicommerce.utils import create_unicommerce_log, get_unicommerce_date
SHIPMENT_STATES = [
"CREATED",
"LOCATION_NOT_SERVICEABLE",
"PICKING",
"PICKED",
"PACKED",
"READY_TO_SHIP",
"CANCELLED",
"MANIFESTED",
"DISPATCHED",
"SHIPPED",
"DELIVERED",
"PENDING_CUSTOMIZATION",
"CUSTOMIZATION_COMPLETE",
"RETURN_EXPECTED",
"RETURNED",
"SPLITTED",
"RETURN_ACKNOWLEDGED",
"MERGED",
]

def prepare_delivery_note():
try:
settings = frappe.get_cached_doc(SETTINGS_DOCTYPE)
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:
sales_order = frappe.get_doc("Sales Order", {ORDER_CODE_FIELD: order["saleOrderCode"]})
if sales_order:
create_delivery_note(order, settings, sales_order)
create_unicommerce_log(status="Success")
except Exception as e:
create_unicommerce_log(status="Error", exception=e, rollback=True)


def create_delivery_note(order, settings, so):
try:
if (
not frappe.db.get_value("Delivery Note", {"shipment_id":order["code"]}, "name")
):
# create a new delivery note
delivery_note = frappe.get_doc({
"doctype": "Delivery Note",
"customer": so.customer,
"unicommerce_order_no":order["saleOrderCode"],
"shipment_id":order["code"],
"status": "Completed"
})

# add items to the delivery note
for item in so.items:
delivery_note.append("items", {
"item_code": item.item_code,
"qty": item.qty,
"rate": item.rate
})
# add texes to the delivery note
for item in so.taxes:
delivery_note.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,
})

# save the delivery note
delivery_note.insert()
delivery_note.submit()

except Exception as e:
create_unicommerce_log(status="Error", exception=e, rollback=True)
15 changes: 8 additions & 7 deletions ecommerce_integrations/unicommerce/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, Iterator, List, NewType, Optional, Set, Tuple

import frappe
from frappe.utils import add_to_date, flt
from frappe.utils import add_to_date, flt,cint,getdate

from ecommerce_integrations.controllers.scheduling import need_to_run
from ecommerce_integrations.ecommerce_integrations.doctype.ecommerce_item import ecommerce_item
Expand All @@ -27,7 +27,6 @@
from ecommerce_integrations.unicommerce.product import import_product_from_unicommerce
from ecommerce_integrations.unicommerce.utils import create_unicommerce_log, get_unicommerce_date
from ecommerce_integrations.utils.taxation import get_dummy_tax_category

UnicommerceOrder = NewType("UnicommerceOrder", Dict[str, Any])


Expand All @@ -49,31 +48,32 @@ def sync_new_orders(client: UnicommerceAPIClient = None, force=False):
status = "COMPLETE" if settings.only_sync_completed_orders else None

new_orders = _get_new_orders(client, status=status,check=True)

if new_orders is None:
return
for order in new_orders:
sales_order = create_order(order, client=client)
# if settings.only_sync_completed_orders:
# _create_sales_invoices(order, sales_order, client)

#for sales order
#for sales Invoice
new_sales_orders = _get_new_orders(client, status="COMPLETE")

if new_sales_orders is None:
return
for order in new_sales_orders:
if frappe.db.exists("Sales Order", {ORDER_CODE_FIELD: order["code"]}):
sales_order = frappe.get_doc("Sales Order", {ORDER_CODE_FIELD: order["code"]})
_create_sales_invoices(order, sales_order, client)
if not frappe.db.exists("Sales Invoice", {ORDER_CODE_FIELD: order["code"]}):
_create_sales_invoices(order, sales_order, client)

def _get_new_orders(
client: UnicommerceAPIClient, status: Optional[str],check=None
) -> Optional[Iterator[UnicommerceOrder]]:

"""Search new sales order from unicommerce."""

updated_since = 24 * 60 # minutes
# updated_since = 24 * 60 # minutes
updated_since = None
uni_orders = client.search_sales_order(updated_since=updated_since, status=status)
configured_channels = {
c.channel_id
Expand Down Expand Up @@ -393,3 +393,4 @@ def _get_warehouse_allocations(sales_order):
}
)
return item_details

2 changes: 2 additions & 0 deletions ecommerce_integrations/unicommerce/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ def _build_unicommerce_item(item_code: ItemCode) -> JsonDict:
item_json["imageUrl"] = get_url(item.image)
item_json["maxRetailPrice"] = frappe.get_value("Item Price",{"item_code":item_code},"price_list_rate")
item_json["hsnCode"] = frappe.get_value("Item",item.name,"gst_hsn_code")
item_json["description"] = frappe.get_value("Item",item.name,"description")
# item_json["gstTaxTypeCode"] =
return item_json


Expand Down

0 comments on commit bd24b34

Please sign in to comment.