|
|
|
@ -8,7 +8,7 @@ from .orders_utills import (check_customer_if_exists,
|
|
|
|
create_customer_if_not_exists)
|
|
|
|
create_customer_if_not_exists)
|
|
|
|
# from .order_acknowledgement import get_sales_order_pyload
|
|
|
|
# from .order_acknowledgement import get_sales_order_pyload
|
|
|
|
from .validate_creds import validate_api_user
|
|
|
|
from .validate_creds import validate_api_user
|
|
|
|
from .constants import SPS_SO_URL
|
|
|
|
# from .constants import SPS_SO_URL
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from frappe.utils import get_datetime, now_datetime
|
|
|
|
from frappe.utils import get_datetime, now_datetime
|
|
|
|
from .oauth import refresh_access_token
|
|
|
|
from .oauth import refresh_access_token
|
|
|
|
@ -115,12 +115,13 @@ def get_sps_sales_order(doc: str):
|
|
|
|
response = requests.get(url=so_url, headers=headers)
|
|
|
|
response = requests.get(url=so_url, headers=headers)
|
|
|
|
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
if response.status_code == 200:
|
|
|
|
create_sps_sales_order(
|
|
|
|
sales_order = create_sps_sales_order(
|
|
|
|
data=response.json(),
|
|
|
|
data=response.json(),
|
|
|
|
setting_doc=doc,
|
|
|
|
setting_doc=doc,
|
|
|
|
path=path
|
|
|
|
path=path
|
|
|
|
)
|
|
|
|
)
|
|
|
|
sales_order_acknowledgement(data=response.json())
|
|
|
|
if frappe.db.exists("Sales Order", {"name": sales_order, "docstatus":1}):
|
|
|
|
|
|
|
|
sales_order_acknowledgement(data=response.json())
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
failed_requests.append(so_url)
|
|
|
|
failed_requests.append(so_url)
|
|
|
|
except requests.RequestException as e:
|
|
|
|
except requests.RequestException as e:
|
|
|
|
@ -153,7 +154,7 @@ def retrieve_sps_order_urls(setting_doc_name: str) -> None:
|
|
|
|
"Authorization": f"Bearer {access_token}",
|
|
|
|
"Authorization": f"Bearer {access_token}",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = requests.get(url=SPS_SO_URL,
|
|
|
|
response = requests.get(url=setting_doc.custom_get_sales_order_url,
|
|
|
|
headers=headers
|
|
|
|
headers=headers
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if response.status_code == 200:
|
|
|
|
if response.status_code == 200:
|
|
|
|
@ -182,7 +183,7 @@ def create_sps_sales_order(data: dict, setting_doc: str, path: str) -> None:
|
|
|
|
data (dict): Sales order payload.
|
|
|
|
data (dict): Sales order payload.
|
|
|
|
setting_doc (str): SPS Integration Settings document.
|
|
|
|
setting_doc (str): SPS Integration Settings document.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
print("Initializing Sales Order creation...")
|
|
|
|
# print("Initializing Sales Order creation...")
|
|
|
|
|
|
|
|
|
|
|
|
# Extract order details
|
|
|
|
# Extract order details
|
|
|
|
order_header = data.get("Header", {}).get("OrderHeader", {})
|
|
|
|
order_header = data.get("Header", {}).get("OrderHeader", {})
|
|
|
|
@ -209,9 +210,9 @@ def create_sps_sales_order(data: dict, setting_doc: str, path: str) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
# Check and create contact if not exists
|
|
|
|
# Check and create contact if not exists
|
|
|
|
contact_name = check_and_create_contact(data, setting_doc)
|
|
|
|
contact_name = check_and_create_contact(data, setting_doc)
|
|
|
|
print("")
|
|
|
|
# print("")
|
|
|
|
if contact_name:
|
|
|
|
# if contact_name:
|
|
|
|
print(f"Contact verified/created: {contact_name}")
|
|
|
|
# print(f"Contact verified/created: {contact_name}")
|
|
|
|
|
|
|
|
|
|
|
|
# Create addresses if not exists
|
|
|
|
# Create addresses if not exists
|
|
|
|
billing_address, shipping_address = create_address_contact(data, customer_name)
|
|
|
|
billing_address, shipping_address = create_address_contact(data, customer_name)
|
|
|
|
@ -227,11 +228,11 @@ def create_sps_sales_order(data: dict, setting_doc: str, path: str) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
if not marketplace_data:
|
|
|
|
if not marketplace_data:
|
|
|
|
frappe.log_error("SPS Order Error", "No wholesale marketplace found.")
|
|
|
|
frappe.log_error("SPS Order Error", "No wholesale marketplace found.")
|
|
|
|
print("No wholesale marketplace found. Aborting order creation.")
|
|
|
|
# print("No wholesale marketplace found. Aborting order creation.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
marketplace_name, series_name = marketplace_data[0]
|
|
|
|
marketplace_name, series_name = marketplace_data[0]
|
|
|
|
print(f"Marketplace found: {marketplace_name}, Series: {series_name}")
|
|
|
|
# print(f"Marketplace found: {marketplace_name}, Series: {series_name}")
|
|
|
|
|
|
|
|
|
|
|
|
# Create Sales Order
|
|
|
|
# Create Sales Order
|
|
|
|
sales_order = frappe.new_doc("Sales Order")
|
|
|
|
sales_order = frappe.new_doc("Sales Order")
|
|
|
|
@ -249,7 +250,7 @@ def create_sps_sales_order(data: dict, setting_doc: str, path: str) -> None:
|
|
|
|
if contact_name:
|
|
|
|
if contact_name:
|
|
|
|
sales_order.contact_person = contact_name
|
|
|
|
sales_order.contact_person = contact_name
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Created Sales Order Document: {sales_order.customer}")
|
|
|
|
# print(f"Created Sales Order Document: {sales_order.customer}")
|
|
|
|
|
|
|
|
|
|
|
|
# Adding items to the sales order
|
|
|
|
# Adding items to the sales order
|
|
|
|
for item in data.get("LineItem", []):
|
|
|
|
for item in data.get("LineItem", []):
|
|
|
|
@ -265,17 +266,17 @@ def create_sps_sales_order(data: dict, setting_doc: str, path: str) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
item_row = create_sps_sales_order_item_row(sku, quantity, amount, uom, description, setting_doc)
|
|
|
|
item_row = create_sps_sales_order_item_row(sku, quantity, amount, uom, description, setting_doc)
|
|
|
|
sales_order.append("items", item_row)
|
|
|
|
sales_order.append("items", item_row)
|
|
|
|
print(f"Added item: {sku}, Quantity: {quantity}, Amount: {amount}")
|
|
|
|
# print(f"Added item: {sku}, Quantity: {quantity}, Amount: {amount}")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
print("Saving and submitting Sales Order...")
|
|
|
|
# print("Saving and submitting Sales Order...")
|
|
|
|
print(sales_order.__dict__) # Debugging
|
|
|
|
# print(sales_order.__dict__) # Debugging
|
|
|
|
|
|
|
|
|
|
|
|
sales_order.save()
|
|
|
|
sales_order.save()
|
|
|
|
sales_order.submit()
|
|
|
|
sales_order.submit()
|
|
|
|
frappe.db.commit()
|
|
|
|
frappe.db.commit()
|
|
|
|
|
|
|
|
return sales_order.name
|
|
|
|
print(f"Sales order successfully created and submitted: {sales_order.name}")
|
|
|
|
# print(f"Sales order successfully created and submitted: {sales_order.name}")
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
error_msg = frappe.get_traceback()
|
|
|
|
error_msg = frappe.get_traceback()
|
|
|
|
@ -283,81 +284,8 @@ def create_sps_sales_order(data: dict, setting_doc: str, path: str) -> None:
|
|
|
|
title="Sales Order Creation Error",
|
|
|
|
title="Sales Order Creation Error",
|
|
|
|
message=f"Marketplace: SPS\n\nTraceback:\n{error_msg}",
|
|
|
|
message=f"Marketplace: SPS\n\nTraceback:\n{error_msg}",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print(f"Error while creating sales order: {e}")
|
|
|
|
# print(f"Error while creating sales order: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
|
|
"Meta" : {
|
|
|
|
|
|
|
|
"OrderManagement" : "SA"
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"Header" : {
|
|
|
|
|
|
|
|
"OrderHeader" : {
|
|
|
|
|
|
|
|
"TradingPartnerId" : "0RXALLALPHARDGO",
|
|
|
|
|
|
|
|
"PurchaseOrderNumber" : "WGE686124-176",
|
|
|
|
|
|
|
|
"TsetPurposeCode" : "00",
|
|
|
|
|
|
|
|
"PrimaryPOTypeCode" : "SA",
|
|
|
|
|
|
|
|
"PurchaseOrderDate" : "2025-02-27",
|
|
|
|
|
|
|
|
"Vendor" : "ALPHAR"
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"Dates" : [ {
|
|
|
|
|
|
|
|
"DateTimeQualifier" : "010",
|
|
|
|
|
|
|
|
"Date" : "2025-02-27"
|
|
|
|
|
|
|
|
}, {
|
|
|
|
|
|
|
|
"DateTimeQualifier" : "001",
|
|
|
|
|
|
|
|
"Date" : "2025-03-27"
|
|
|
|
|
|
|
|
} ],
|
|
|
|
|
|
|
|
"Contacts" : [ {
|
|
|
|
|
|
|
|
"ContactTypeCode" : "BD",
|
|
|
|
|
|
|
|
"ContactName" : "Roland Anderson"
|
|
|
|
|
|
|
|
} ],
|
|
|
|
|
|
|
|
"Address" : [ {
|
|
|
|
|
|
|
|
"AddressTypeCode" : "BT",
|
|
|
|
|
|
|
|
"LocationCodeQualifier" : "92",
|
|
|
|
|
|
|
|
"AddressLocationNumber" : "00",
|
|
|
|
|
|
|
|
"AddressName" : "Worldwide Golf Enterprises",
|
|
|
|
|
|
|
|
"Address1" : "1430 Village Way",
|
|
|
|
|
|
|
|
"Address2" : "Suite J",
|
|
|
|
|
|
|
|
"City" : "Santa Ana",
|
|
|
|
|
|
|
|
"State" : "CA",
|
|
|
|
|
|
|
|
"PostalCode" : "92705"
|
|
|
|
|
|
|
|
}, {
|
|
|
|
|
|
|
|
"AddressTypeCode" : "ST",
|
|
|
|
|
|
|
|
"LocationCodeQualifier" : "92",
|
|
|
|
|
|
|
|
"AddressLocationNumber" : "176",
|
|
|
|
|
|
|
|
"AddressName" : "GOLF & SKI WAREHOUSE",
|
|
|
|
|
|
|
|
"Address1" : "290 PLAINFIELD ROAD",
|
|
|
|
|
|
|
|
"City" : "WEST LEBANON",
|
|
|
|
|
|
|
|
"State" : "NH",
|
|
|
|
|
|
|
|
"PostalCode" : "03784",
|
|
|
|
|
|
|
|
"Country" : "US"
|
|
|
|
|
|
|
|
} ],
|
|
|
|
|
|
|
|
"Notes" : [ {
|
|
|
|
|
|
|
|
"Note" : "https://commerce.spscommerce.com/fulfillment/redirect/?searchType=exact&documentType=Order&keyword=WGE686124-176"
|
|
|
|
|
|
|
|
}, {
|
|
|
|
|
|
|
|
"Note" : "https: //commerce.spscommerce.com/fulfillment-monitor"
|
|
|
|
|
|
|
|
} ]
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"LineItem" : [ {
|
|
|
|
|
|
|
|
"OrderLine" : {
|
|
|
|
|
|
|
|
"LineSequenceNumber" : "1",
|
|
|
|
|
|
|
|
"VendorPartNumber" : "CYBERE-BL",
|
|
|
|
|
|
|
|
"ConsumerPackageCode" : "849650001612",
|
|
|
|
|
|
|
|
"OrderQty" : 6.0,
|
|
|
|
|
|
|
|
"OrderQtyUOM" : "EA",
|
|
|
|
|
|
|
|
"PurchasePrice" : 949.0
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"ProductOrItemDescription" : [ {
|
|
|
|
|
|
|
|
"ProductCharacteristicCode" : "08",
|
|
|
|
|
|
|
|
"ProductDescription" : "CYBERCART"
|
|
|
|
|
|
|
|
} ]
|
|
|
|
|
|
|
|
} ],
|
|
|
|
|
|
|
|
"Summary" : {
|
|
|
|
|
|
|
|
"TotalAmount" : 5694.0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Trigger the order processing
|
|
|
|
|
|
|
|
create_sps_sales_order(data, setting_doc="Develop", path="")
|
|
|
|
|
|
|
|
# from sps_integration.sps_integration import order_sync
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|