|
|
|
|
@ -88,7 +88,10 @@ def get_sps_item_code_if_exists(order_uom: str, description: str, sku: str, sett
|
|
|
|
|
item_doc.save()
|
|
|
|
|
frappe.db.commit()
|
|
|
|
|
except:
|
|
|
|
|
print(frappe.get_traceback())
|
|
|
|
|
frappe.log_error(
|
|
|
|
|
title="Item creation",
|
|
|
|
|
message=f"{frappe.get_traceback()}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return item_doc.name
|
|
|
|
|
|
|
|
|
|
@ -113,20 +116,6 @@ def create_sps_sales_order_item_row(sku: str, qty: int, amount: float, uom: str,
|
|
|
|
|
"custom_sku": sku,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# def get_sps_market_place_order_ID(market_place_order_id: str, setting_doc: str) -> str:
|
|
|
|
|
# """Get or create Marketplace Order ID."""
|
|
|
|
|
# if frappe.db.exists("Marketplace Order ID", market_place_order_id):
|
|
|
|
|
# return market_place_order_id
|
|
|
|
|
|
|
|
|
|
# doc = frappe.new_doc("Marketplace Order ID")
|
|
|
|
|
# doc.update({
|
|
|
|
|
# "marketplace_order_id": market_place_order_id,
|
|
|
|
|
# "marketplace": frappe.get_value("SPS Integration Settings", setting_doc, "marketplace"),
|
|
|
|
|
# })
|
|
|
|
|
# doc.save()
|
|
|
|
|
# frappe.db.commit()
|
|
|
|
|
# return doc.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_sps_market_place_order_ID(market_place_order_id: str, setting_doc: str) -> str:
|
|
|
|
|
"""Get or create Marketplace Order ID."""
|
|
|
|
|
@ -170,7 +159,6 @@ def get_link_row(docname: str, link_name: str) -> Dict:
|
|
|
|
|
|
|
|
|
|
def check_and_create_contact(data, setting_doc):
|
|
|
|
|
"""Check if contacts exist, else create new contacts"""
|
|
|
|
|
print("Checking for contacts in data...")
|
|
|
|
|
|
|
|
|
|
addresses = data.get('Header', {}).get('Address', [])
|
|
|
|
|
contacts = None
|
|
|
|
|
@ -179,47 +167,36 @@ def check_and_create_contact(data, setting_doc):
|
|
|
|
|
for address in addresses:
|
|
|
|
|
if address.get("AddressTypeCode") == "BT" and address.get("Contacts"):
|
|
|
|
|
contacts = address["Contacts"]
|
|
|
|
|
print(f"Billing contact found: {contacts}")
|
|
|
|
|
break # Stop once billing contacts are found
|
|
|
|
|
|
|
|
|
|
if not contacts:
|
|
|
|
|
for address in addresses:
|
|
|
|
|
if address.get("AddressTypeCode") == "ST" and address.get("Contacts"):
|
|
|
|
|
contacts = address["Contacts"]
|
|
|
|
|
print(f"Shipping contact found: {contacts}")
|
|
|
|
|
break # Stop once shipping contacts are found
|
|
|
|
|
|
|
|
|
|
if not contacts:
|
|
|
|
|
print("No contacts available in the provided data.")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# Check if customer exists
|
|
|
|
|
print("Checking if customer exists...")
|
|
|
|
|
customer_exists, customer_name = check_customer_if_exists(data)
|
|
|
|
|
print(f"Customer Exists: {customer_exists}, Customer Name: {customer_name}")
|
|
|
|
|
|
|
|
|
|
if customer_exists:
|
|
|
|
|
# Check if the contact email already exists
|
|
|
|
|
email_id = contacts[0].get("PrimaryEmail", "")
|
|
|
|
|
print(f"Checking if contact email {email_id} exists in the system...")
|
|
|
|
|
if frappe.db.exists("Contact Email", {"email_id": email_id}):
|
|
|
|
|
contact_name = frappe.get_value(
|
|
|
|
|
"Contact Email", {"email_id": email_id}, "parent"
|
|
|
|
|
)
|
|
|
|
|
print(f"Existing contact found: {contact_name}")
|
|
|
|
|
else:
|
|
|
|
|
print("No existing contact found. Creating a new contact...")
|
|
|
|
|
|
|
|
|
|
doc = frappe.new_doc("Contact")
|
|
|
|
|
doc.first_name = customer_name
|
|
|
|
|
print(f"Setting contact first name: {customer_name}")
|
|
|
|
|
if email_id:
|
|
|
|
|
email_row = {
|
|
|
|
|
"email_id": email_id,
|
|
|
|
|
"is_primary": 1
|
|
|
|
|
}
|
|
|
|
|
doc.append("email_ids", email_row)
|
|
|
|
|
print(f"Added email: {email_id}")
|
|
|
|
|
|
|
|
|
|
phone_number = contacts[0].get("PrimaryPhone", "")
|
|
|
|
|
if phone_number:
|
|
|
|
|
@ -228,11 +205,9 @@ def check_and_create_contact(data, setting_doc):
|
|
|
|
|
"is_primary_phone": 1
|
|
|
|
|
}
|
|
|
|
|
doc.append("phone_nos", num_row)
|
|
|
|
|
print(f"Added phone number: {phone_number}")
|
|
|
|
|
|
|
|
|
|
link_row = get_link_row("Customer", customer_name)
|
|
|
|
|
doc.append("links", link_row)
|
|
|
|
|
print(f"Linked contact to customer: {customer_name}")
|
|
|
|
|
|
|
|
|
|
doc.save()
|
|
|
|
|
frappe.db.commit()
|
|
|
|
|
@ -253,52 +228,38 @@ def create_customer_if_not_exists(data: dict, setting_doc: str) -> str:
|
|
|
|
|
Returns:
|
|
|
|
|
customer_name (str): Return customer name after creation of customer.
|
|
|
|
|
"""
|
|
|
|
|
print("Checking Trading Partner ID in the payload...")
|
|
|
|
|
trading_id = data.get("Header", {}).get("OrderHeader", {}).get("TradingPartnerId")
|
|
|
|
|
print(f"Trading Partner ID: {trading_id}")
|
|
|
|
|
|
|
|
|
|
print("Fetching addresses from payload...")
|
|
|
|
|
addresses = data.get("Header", {}).get("Address", [])
|
|
|
|
|
address_names = [address.get("AddressName") for address in addresses]
|
|
|
|
|
print(f"Extracted Address Names: {address_names}")
|
|
|
|
|
|
|
|
|
|
print("Checking if customer already exists...")
|
|
|
|
|
if frappe.db.exists("Customer", {"custom_trading_partner_id": trading_id}):
|
|
|
|
|
customer_name = frappe.get_value(
|
|
|
|
|
"Customer",
|
|
|
|
|
{"custom_trading_partner_id": trading_id},
|
|
|
|
|
"name"
|
|
|
|
|
)
|
|
|
|
|
print(f"Customer already exists: {customer_name}")
|
|
|
|
|
else:
|
|
|
|
|
print("Customer does not exist. Creating a new customer...")
|
|
|
|
|
|
|
|
|
|
doc = frappe.new_doc("Customer")
|
|
|
|
|
doc.custom_trading_partner_id = trading_id
|
|
|
|
|
doc.customer_name = address_names[0].capitalize()
|
|
|
|
|
print(f"Assigned customer name: {doc.customer_name}")
|
|
|
|
|
|
|
|
|
|
doc.customer_type = frappe.get_value(
|
|
|
|
|
"SPS Integration Settings", setting_doc, "customer_type"
|
|
|
|
|
)
|
|
|
|
|
print(f"Fetched customer type from settings: {doc.customer_type}")
|
|
|
|
|
|
|
|
|
|
doc.save()
|
|
|
|
|
frappe.db.commit()
|
|
|
|
|
print(f"New customer created: {doc.name}")
|
|
|
|
|
|
|
|
|
|
for address in addresses:
|
|
|
|
|
address_type = address.get("AddressTypeCode")
|
|
|
|
|
print(f"Processing address type: {address_type}")
|
|
|
|
|
|
|
|
|
|
if address_type == "BT":
|
|
|
|
|
print("Billing address found, creating or updating address...")
|
|
|
|
|
doc.customer_primary_contact = create_or_update_address(address, "Billing", doc.name)
|
|
|
|
|
print(f"Primary contact assigned: {doc.customer_primary_contact}")
|
|
|
|
|
|
|
|
|
|
doc.save()
|
|
|
|
|
frappe.db.commit()
|
|
|
|
|
print(f"Customer document saved: {doc.name}")
|
|
|
|
|
|
|
|
|
|
return doc.name
|
|
|
|
|
|
|
|
|
|
|