diff --git a/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_repository.py b/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_repository.py index 1672801d4..f07e2ef7c 100644 --- a/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_repository.py +++ b/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_repository.py @@ -96,58 +96,58 @@ def get_charges_and_fees(self, order_id) -> dict: ) charges_and_fees = {"charges": [], "fees": []} - - while True: - shipment_event_list = financial_events_payload.get("FinancialEvents", {}).get( - "ShipmentEventList", [] - ) - next_token = financial_events_payload.get("NextToken") - - for shipment_event in shipment_event_list: - if shipment_event: - for shipment_item in shipment_event.get("ShipmentItemList", []): - charges = shipment_item.get("ItemChargeList", []) - fees = shipment_item.get("ItemFeeList", []) - seller_sku = shipment_item.get("SellerSKU") - - for charge in charges: - charge_type = charge.get("ChargeType") - amount = charge.get("ChargeAmount", {}).get("CurrencyAmount", 0) - - if charge_type != "Principal" and float(amount) != 0: - charge_account = self.get_account(charge_type) - charges_and_fees.get("charges").append( - { - "charge_type": "Actual", - "account_head": charge_account, - "tax_amount": amount, - "description": charge_type + " for " + seller_sku, - } - ) - - for fee in fees: - fee_type = fee.get("FeeType") - amount = fee.get("FeeAmount", {}).get("CurrencyAmount", 0) - - if float(amount) != 0: - fee_account = self.get_account(fee_type) - charges_and_fees.get("fees").append( - { - "charge_type": "Actual", - "account_head": fee_account, - "tax_amount": amount, - "description": fee_type + " for " + seller_sku, - } - ) - - if not next_token: - break - - financial_events_payload = self.call_sp_api_method( - sp_api_method=finances.list_financial_events_by_order_id, - order_id=order_id, - next_token=next_token, - ) + if financial_events_payload: + while True: + shipment_event_list = financial_events_payload.get("FinancialEvents", {}).get( + "ShipmentEventList", [] + ) + next_token = financial_events_payload.get("NextToken") + + for shipment_event in shipment_event_list: + if shipment_event: + for shipment_item in shipment_event.get("ShipmentItemList", []): + charges = shipment_item.get("ItemChargeList", []) + fees = shipment_item.get("ItemFeeList", []) + seller_sku = shipment_item.get("SellerSKU") + + for charge in charges: + charge_type = charge.get("ChargeType") + amount = charge.get("ChargeAmount", {}).get("CurrencyAmount", 0) + + if charge_type != "Principal" and float(amount) != 0: + charge_account = self.get_account(charge_type) + charges_and_fees.get("charges").append( + { + "charge_type": "Actual", + "account_head": charge_account, + "tax_amount": amount, + "description": charge_type + " for " + seller_sku, + } + ) + + for fee in fees: + fee_type = fee.get("FeeType") + amount = fee.get("FeeAmount", {}).get("CurrencyAmount", 0) + + if float(amount) != 0: + fee_account = self.get_account(fee_type) + charges_and_fees.get("fees").append( + { + "charge_type": "Actual", + "account_head": fee_account, + "tax_amount": amount, + "description": fee_type + " for " + seller_sku, + } + ) + + if not next_token: + break + + financial_events_payload = self.call_sp_api_method( + sp_api_method=finances.list_financial_events_by_order_id, + order_id=order_id, + next_token=next_token, + ) return charges_and_fees @@ -389,6 +389,7 @@ def create_address(order, customer_name) -> str | None: order_id = order.get("AmazonOrderId") so = frappe.db.get_value("Sales Order", filters={"amazon_order_id": order_id}, fieldname="name") + si = frappe.db.get_value("Sales Invoice", filters={"amazon_order_id": order_id}, fieldname="name") if so: return so @@ -404,32 +405,64 @@ def create_address(order, customer_name) -> str | None: delivery_date = dateutil.parser.parse(order.get("LatestShipDate")).strftime("%Y-%m-%d") transaction_date = dateutil.parser.parse(order.get("PurchaseDate")).strftime("%Y-%m-%d") - so = frappe.new_doc("Sales Order") - so.amazon_order_id = order_id - so.marketplace_id = order.get("MarketplaceId") - so.customer = customer_name - so.delivery_date = delivery_date - so.transaction_date = transaction_date - so.company = self.amz_setting.company + if order.get("IsReplacementOrder") == "true": + if si: + return + si = frappe.new_doc("Sales Invoice") + si.amazon_order_id = order_id + si.customer = customer_name + si.posting_date = transaction_date + si.company = self.amz_setting.company + si.is_return = 1 + + for item in items: + item["qty"] = -abs(item["qty"]) + si.append("items", item) + taxes_and_charges = self.amz_setting.taxes_charges + + if taxes_and_charges: + charges_and_fees = self.get_charges_and_fees(order_id) + if charges_and_fees: + for charge in charges_and_fees.get("charges"): + si.append("taxes", charge) + + for fee in charges_and_fees.get("fees"): + si.append("taxes", fee) + + si.insert(ignore_permissions=True) + if self.amz_setting.submit_credit_note: + si.submit() + else: + si.save() + return - for item in items: - so.append("items", item) + else: + so = frappe.new_doc("Sales Order") + so.amazon_order_id = order_id + so.marketplace_id = order.get("MarketplaceId") + so.customer = customer_name + so.delivery_date = delivery_date + so.transaction_date = transaction_date + so.company = self.amz_setting.company - taxes_and_charges = self.amz_setting.taxes_charges + for item in items: + so.append("items", item) - if taxes_and_charges: - charges_and_fees = self.get_charges_and_fees(order_id) + taxes_and_charges = self.amz_setting.taxes_charges - for charge in charges_and_fees.get("charges"): - so.append("taxes", charge) + if taxes_and_charges: + charges_and_fees = self.get_charges_and_fees(order_id) + if charges_and_fees: + for charge in charges_and_fees.get("charges"): + so.append("taxes", charge) - for fee in charges_and_fees.get("fees"): - so.append("taxes", fee) + for fee in charges_and_fees.get("fees"): + so.append("taxes", fee) - so.insert(ignore_permissions=True) - so.submit() + so.insert(ignore_permissions=True) + so.submit() - return so.name + return so.name def get_orders(self, created_after) -> list: orders = self.get_orders_instance() diff --git a/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.json b/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.json index 91dd8a142..cb6e5b5ae 100644 --- a/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.json +++ b/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.json @@ -33,6 +33,7 @@ "section_break_3", "after_date", "taxes_charges", + "submit_credit_note", "sync_orders", "column_break_4", "enable_sync", @@ -254,10 +255,17 @@ "fieldname": "create_item_if_not_exists", "fieldtype": "Check", "label": "Create Item If Not Exists" + }, + { + "default": "0", + "description": "Check this to submit the Credit Note for the Return Order", + "fieldname": "submit_credit_note", + "fieldtype": "Check", + "label": "Submit Credit Note" } ], "links": [], - "modified": "2023-07-31 17:20:21.492153", + "modified": "2025-02-14 17:27:24.072514", "modified_by": "Administrator", "module": "Amazon", "name": "Amazon SP API Settings", diff --git a/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.py b/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.py index bcafd5ac7..e4e474345 100644 --- a/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.py +++ b/ecommerce_integrations/amazon/doctype/amazon_sp_api_settings/amazon_sp_api_settings.py @@ -158,7 +158,17 @@ def setup_custom_fields(): fieldname="amazon_order_id", label="Amazon Order ID", fieldtype="Data", - insert_after="title", + insert_after="order_type", + read_only=1, + print_hide=1, + ) + ], + "Sales Invoice": [ + dict( + fieldname="amazon_order_id", + label="Amazon Order ID", + fieldtype="Data", + insert_after="remarks", read_only=1, print_hide=1, )