diff --git a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json index 16aedf9eb..04b6cc72b 100644 --- a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json +++ b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.json @@ -29,6 +29,11 @@ "sales_order_series", "sales_invoice_series", "order_status_days", + "sync_old_orders_section", + "sync_old_orders", + "column_break_xcby", + "from_date", + "to_date", "delivery_note_settings_section", "delivery_note", "inventory_sync_settings_section", @@ -262,12 +267,41 @@ "fieldname": "delivery_note", "fieldtype": "Check", "label": "Import Delivery Notes from Unicommerce on Shipment" + }, + { + "fieldname": "sync_old_orders_section", + "fieldtype": "Section Break", + "label": "Sync Old Orders" + }, + { + "default": "0", + "fieldname": "sync_old_orders", + "fieldtype": "Check", + "label": "Sync Old Orders" + }, + { + "fieldname": "column_break_xcby", + "fieldtype": "Column Break" + }, + { + "depends_on": "eval: doc.sync_old_orders", + "fieldname": "from_date", + "fieldtype": "Datetime", + "label": "From", + "mandatory_depends_on": "eval: doc.sync_old_orders" + }, + { + "depends_on": "eval: doc.sync_old_orders", + "fieldname": "to_date", + "fieldtype": "Datetime", + "label": "To", + "mandatory_depends_on": "eval: doc.sync_old_orders" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2023-05-02 14:04:26.684256", + "modified": "2025-05-21 13:11:26.045783", "modified_by": "Administrator", "module": "unicommerce", "name": "Unicommerce Settings", @@ -284,8 +318,9 @@ "write": 1 } ], + "row_format": "Dynamic", "sort_field": "modified", "sort_order": "DESC", "states": [], "track_changes": 1 -} \ No newline at end of file +} diff --git a/ecommerce_integrations/unicommerce/order.py b/ecommerce_integrations/unicommerce/order.py index 7c78608b1..2b3fbf1df 100644 --- a/ecommerce_integrations/unicommerce/order.py +++ b/ecommerce_integrations/unicommerce/order.py @@ -51,8 +51,8 @@ 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) - - if new_orders is None: + + if not new_orders: return for order in new_orders: @@ -61,19 +61,45 @@ def sync_new_orders(client: UnicommerceAPIClient = None, force=False): if settings.only_sync_completed_orders: _create_sales_invoices(order, sales_order, client) - def _get_new_orders(client: UnicommerceAPIClient, status: str | None) -> Iterator[UnicommerceOrder] | None: + """This is called from a scheduled job and syncs all orders within the specified date range.""" + settings = frappe.get_doc(SETTINGS_DOCTYPE) + """Search new sales order from unicommerce.""" + from_date = None #datetime + to_date = None #datetime + updated_since = None #minutes + + if settings.sync_old_orders: + from_date = settings.from_date + to_date = settings.to_date + else: + updated_since = 24 * 60 + + uni_orders = client.search_sales_order( + updated_since=updated_since, + status=status, + from_date=from_date, + to_date=to_date + ) + - updated_since = 24 * 60 # minutes - uni_orders = client.search_sales_order(updated_since=updated_since, status=status) configured_channels = { c.channel_id for c in frappe.get_all("Unicommerce Channel", filters={"enabled": 1}, fields="channel_id") } - if uni_orders is None: + + if not uni_orders: return + if settings.sync_old_orders: + settings.update({ + "sync_old_orders": 0, + "from_date": "", + "to_date": "" + }) + settings.save() + for order in uni_orders: if order["channel"] not in configured_channels: continue