Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions ecommerce_integrations/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,31 @@
# ---------------
# Hook on document methods and events

doc_events = {
"Item": {
"after_insert": "ecommerce_integrations.shopify.product.upload_erpnext_item",
"on_update": "ecommerce_integrations.shopify.product.upload_erpnext_item",
"validate": [
"ecommerce_integrations.utils.taxation.validate_tax_template",
"ecommerce_integrations.unicommerce.product.validate_item",
],
},
"Sales Order": {
"on_update_after_submit": "ecommerce_integrations.unicommerce.order.update_shipping_info",
"on_cancel": "ecommerce_integrations.unicommerce.status_updater.ignore_pick_list_on_sales_order_cancel",
},
"Stock Entry": {
"validate": "ecommerce_integrations.unicommerce.grn.validate_stock_entry_for_grn",
"on_submit": "ecommerce_integrations.unicommerce.grn.upload_grn",
"on_cancel": "ecommerce_integrations.unicommerce.grn.prevent_grn_cancel",
},
"Item Price": {"on_change": "ecommerce_integrations.utils.price_list.discard_item_prices"},
"Pick List": {"validate": "ecommerce_integrations.unicommerce.pick_list.validate"},
"Sales Invoice": {
"on_submit": "ecommerce_integrations.unicommerce.invoice.on_submit",
"on_cancel": "ecommerce_integrations.unicommerce.invoice.on_cancel",
},
}
# doc_events = {
# "Item": {
# "after_insert": "ecommerce_integrations.shopify.product.upload_erpnext_item",
# "on_update": "ecommerce_integrations.shopify.product.upload_erpnext_item",
# "validate": [
# "ecommerce_integrations.utils.taxation.validate_tax_template",
# "ecommerce_integrations.unicommerce.product.validate_item",
# ],
# },
# "Sales Order": {
# "on_update_after_submit": "ecommerce_integrations.unicommerce.order.update_shipping_info",
# "on_cancel": "ecommerce_integrations.unicommerce.status_updater.ignore_pick_list_on_sales_order_cancel",
# },
# "Stock Entry": {
# "validate": "ecommerce_integrations.unicommerce.grn.validate_stock_entry_for_grn",
# "on_submit": "ecommerce_integrations.unicommerce.grn.upload_grn",
# "on_cancel": "ecommerce_integrations.unicommerce.grn.prevent_grn_cancel",
# },
# "Item Price": {"on_change": "ecommerce_integrations.utils.price_list.discard_item_prices"},
# "Pick List": {"validate": "ecommerce_integrations.unicommerce.pick_list.validate"},
# "Sales Invoice": {
# "on_submit": "ecommerce_integrations.unicommerce.invoice.on_submit",
# "on_cancel": "ecommerce_integrations.unicommerce.invoice.on_cancel",
# },
# }

# Scheduled Tasks
# ---------------
Expand Down
11 changes: 6 additions & 5 deletions ecommerce_integrations/unicommerce/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@

# Tax -> Unicommerce tax amount field mapping
TAX_FIELDS_MAPPING = {
"igst": "integratedGst",
"cgst": "centralGst",
"sgst": "stateGst",
"ugst": "unionTerritoryGst",
"igst": "totalIntegratedGst",
"cgst": "totalCentralGst",
"sgst": "totalStateGst",
"ugst": "totalUnionTerritoryGst",
"tcs": "tcsAmount",
"cash_on_delivery_charges": "cashOnDeliveryCharges",
"gift_wrap_charges": "giftWrapCharges",
"shipping_charges": "shippingCharges",
"shipping_method_charges": "shippingMethodCharges",
}


# Tax -> Unicommerce tax "rate" field mapping
TAX_RATE_FIELDS_MAPPING = {
"igst": "integratedGstPercentage",
Expand Down Expand Up @@ -346,4 +347,4 @@
"UL": "Uttarakhand",
"JH": "Jharkhand",
"JR": "Jharkhand",
}
}
8 changes: 6 additions & 2 deletions ecommerce_integrations/unicommerce/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def _create_customer_address(uni_address, address_type, customer, also_shipping=
state = uni_address.get("state")
if country_code == "IN" and state in UNICOMMERCE_INDIAN_STATES_MAPPING:
state = UNICOMMERCE_INDIAN_STATES_MAPPING.get(state)

email_id=uni_address.get("email")
if not frappe.utils.validate_email_address(email_id, throw=False):
email_id = ""

frappe.get_doc(
{
Expand All @@ -109,12 +113,12 @@ def _create_customer_address(uni_address, address_type, customer, also_shipping=
"country": country,
"county": uni_address.get("district"),
"doctype": "Address",
"email_id": uni_address.get("email"),
"email_id": email_id,
"phone": uni_address.get("phone"),
"pincode": uni_address.get("pincode"),
"state": state,
"links": [{"link_doctype": "Customer", "link_name": customer.name}],
"is_primary_address": int(address_type == "Billing"),
"is_shipping_address": int(also_shipping or address_type == "Shipping"),
}
).insert(ignore_mandatory=True)
).insert(ignore_mandatory=True)
114 changes: 67 additions & 47 deletions ecommerce_integrations/unicommerce/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,75 @@

@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(

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}
):
sales_invoice = frappe.get_doc(
"Sales Invoice", {"unicommerce_order_code": sales_order.unicommerce_order_code}
)
)
try:
create_delivery_note(sales_order, sales_invoice)
except Exception as e:
create_unicommerce_log(status="Error", exception=e, rollback=True)
except Exception as e:
create_unicommerce_log(status="Error", exception=e, rollback=True)



def create_delivery_note(so, sales_invoice):
# Create the delivery note
from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note

res = make_delivery_note(source_name=so.name)
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
create_unicommerce_log(status="Success")
frappe.flags.request_id = None
return res
# Create the delivery note
from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note
from collections import defaultdict

res = make_delivery_note(source_name=so.name)
res.unicommerce_order_code = sales_invoice.unicommerce_order_code
res.unicommerce_shipment_id = sales_invoice.unicommerce_shipping_package_code

si_item_map = defaultdict(list)
for si_item in sales_invoice.items:
si_item_map[si_item.item_code].append(si_item)

used_si_items = set()

for item in res.items:
item.against_sales_invoice = sales_invoice.name
for si_item in si_item_map.get(item.item_code, []):
if si_item.name not in used_si_items:
item.si_detail = si_item.name
used_si_items.add(si_item.name)
break

res.save()
res.submit()

log = create_unicommerce_log(method="create_delivery_note", make_new=True)
frappe.flags.request_id = log.name
create_unicommerce_log(status="Success")
frappe.flags.request_id = None

return res
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ frappe.ui.form.on("Unicommerce Settings", {
__("Sync Now"),
);
});

frm.add_custom_button(__("Test sync_new_orders"), () => {
frappe.call({
method: "ecommerce_integrations.unicommerce.order.sync_new_orders",
args: { force: true },
freeze: true,
freeze_message: "Running sync_new_orders...",
callback: (r) => {
frappe.msgprint("Done! Check VS Code debugger.");
console.log(r);
},
});
}, __("Debug"));

frm.add_custom_button(__("Test prepare_delivery_note"), () => {
frappe.call({
method: "ecommerce_integrations.unicommerce.delivery_note.prepare_delivery_note",
args: { force: 1 },
freeze: true,
freeze_message: "Running prepare_delivery_note...",
callback: (r) => {
frappe.msgprint("Done! Check logs.");
console.log(r);
},
});
}, __("Debug"));
},

onload: function (frm) {
Expand All @@ -57,4 +83,4 @@ frappe.ui.form.on("Unicommerce Settings", {
};
};
},
});
});
Loading