Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add phone no in contact for customer #91

Closed
wants to merge 2 commits into from
Closed
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
269 changes: 155 additions & 114 deletions shipstation_integration/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,96 @@
ShipstationStore,
)


def update_customer_details(
existing_so: str, order: "ShipStationOrder", store: "ShipstationStore"
def update_amazon_order(
existing_so: str, order: "ShipStationOrder", store: "ShipstationStore"
):
existing_so_doc: "SalesOrder" = frappe.get_doc("Sales Order", existing_so)

email_id, _ = parse_addr(existing_so_doc.amazon_customer)
if email_id:
contact = create_contact(order, email_id)
existing_so_doc.contact_person = contact.name

existing_so_doc.update(
{
"shipstation_order_id": order.order_id,
"shipstation_store_name": store.store_name,
"shipstation_customer_notes": getattr(order, "customer_notes", None),
"shipstation_internal_notes": getattr(order, "internal_notes", None),
"marketplace_order_id": order.order_number,
"delivery_date": getdate(order.ship_date),
"has_pii": True,
"integration_doctype": "Shipstation Settings",
"integration_doc": store.parent,
}
)

if order.bill_to and order.bill_to.street1:
if existing_so_doc.customer_address:
bill_address = update_address(
order.bill_to,
existing_so_doc.customer_address,
order.customer_email,
"Billing",
)
else:
bill_address = create_address(
order.bill_to,
existing_so_doc.amazon_customer,
order.customer_email,
"Billing",
)
existing_so_doc.customer_address = bill_address.name
if order.ship_to and order.ship_to.street1:
if existing_so_doc.shipping_address_name:
ship_address = update_address(
order.ship_to,
existing_so_doc.shipping_address_name,
order.customer_email,
"Shipping",
)
else:
ship_address = create_address(
order.ship_to,
existing_so_doc.amazon_customer,
order.customer_email,
"Shipping",
)
existing_so_doc.shipping_address_name = ship_address.name

existing_so_doc.flags.ignore_validate_update_after_submit = True
existing_so_doc.run_method("set_customer_address")
existing_so_doc.save()
return existing_so_doc
existing_so_doc: "SalesOrder" = frappe.get_doc("Sales Order", existing_so)

email_id, user_name = parse_addr(existing_so_doc.amazon_customer)
phone_no = order.ship_to.phone if order.ship_to and order.ship_to.phone else None
if email_id or phone_no:
contact = create_contact(order, email_id, phone_no)
existing_so_doc.contact_person = contact.name

existing_so_doc.update(
{
"shipstation_order_id": order.order_id,
"shipstation_store_name": store.store_name,
"shipstation_customer_notes": getattr(order, "customer_notes", None),
"shipstation_internal_notes": getattr(order, "internal_notes", None),
"marketplace_order_id": order.order_number,
"delivery_date": getdate(order.ship_date),
"has_pii": True,
}
)

if order.bill_to and order.bill_to.street1:
if existing_so_doc.customer_address:
bill_address = update_address(
order.bill_to,
existing_so_doc.customer_address,
order.customer_email,
"Billing",
)
else:
bill_address = create_address(
order.bill_to,
existing_so_doc.amazon_customer,
order.customer_email,
"Billing",
)
existing_so_doc.customer_address = bill_address.name
if order.ship_to and order.ship_to.street1:
if existing_so_doc.shipping_address_name:
ship_address = update_address(
order.ship_to,
existing_so_doc.shipping_address_name,
order.customer_email,
"Shipping",
)
else:
ship_address = create_address(
order.ship_to,
existing_so_doc.amazon_customer,
order.customer_email,
"Shipping",
)
existing_so_doc.shipping_address_name = ship_address.name

existing_so_doc.flags.ignore_validate_update_after_submit = True
existing_so_doc.run_method("set_customer_address")
existing_so_doc.save()
return existing_so_doc


def update_shopify_order(
existing_so: str, order: "ShipStationOrder", store: "ShipstationStore"
):
existing_so_doc: "SalesOrder" = frappe.get_doc("Sales Order", existing_so)
existing_so_doc.update(
{
"shipstation_order_id": order.order_id,
"shipstation_store_name": store.store_name,
"shipstation_customer_notes": getattr(order, "customer_notes", None),
"shipstation_internal_notes": getattr(order, "internal_notes", None),
"marketplace_order_id": order.order_number,
"delivery_date": getdate(order.ship_date),
"has_pii": True,
}
)

existing_so_doc.flags.ignore_validate_update_after_submit = True
existing_so_doc.save()
return existing_so_doc


def create_address(
address: "ShipStationAddress", customer: str, email: str, address_type: str
):
addr: "Address" = frappe.new_doc("Address")
addr.append("links", {"link_doctype": "Customer", "link_name": customer})
_update_address(address, addr, email, address_type)
return addr


def create_address(address: "ShipStationAddress", customer: str, email: str, address_type: str):
Expand Down Expand Up @@ -111,59 +139,72 @@ def _update_address(address: "ShipStationAddress", addr: "Address", email: str,


def create_customer(order: "ShipStationOrder"):
customer_id = (
order.customer_id or order.customer_email or order.ship_to.name or frappe.generate_hash("", 10)
)

customer_name = order.customer_email or order.customer_id or order.ship_to.name or customer_id

if frappe.db.exists("Customer", customer_name):
return frappe.get_doc("Customer", customer_name)

cust = frappe.new_doc("Customer")
cust.shipstation_customer_id = customer_id
cust.customer_name = customer_name
cust.customer_type = "Individual"
cust.customer_group = "ShipStation"
cust.territory = "United States"
cust.save()
frappe.db.commit()

email_id, _ = parse_addr(customer_name)
if email_id:
customer_primary_contact = create_contact(order, email_id)
if customer_primary_contact:
cust.customer_primary_contact = customer_primary_contact.name

if order.ship_to.street1:
create_address(order.ship_to, customer_name, order.customer_email, "Shipping")
if order.bill_to.street1:
create_address(order.bill_to, order.customer_username, order.customer_email, "Billing")

try:
cust.save()
return cust
except Exception as e:
frappe.log_error(title="Error saving Shipstation Customer", message=e)


def create_contact(order: "ShipStationOrder", customer_name: str):
contact = frappe.get_value("Contact Email", {"email_id": customer_name}, "parent")
if contact:
return frappe._dict({"name": contact})
cont: "Contact" = frappe.new_doc("Contact")
cont.first_name = order.bill_to.name or "Not Provided"
for char in "<>":
cont.first_name = cont.first_name.replace(char, "")
if customer_name:
cont.append("email_ids", {"email_id": customer_name})
cont.append("links", {"link_doctype": "Customer", "link_name": customer_name})
try:
cont.save()
frappe.db.commit()
return cont
except Exception as e:
frappe.log_error(title="Error saving Shipstation Contact", message=e)
customer_id = (
order.customer_id
or order.customer_email
or order.ship_to.name
or frappe.generate_hash("", 10)
)

customer_name = (
order.customer_email or order.customer_id or order.ship_to.name or customer_id
).strip()

if frappe.db.exists("Customer", customer_name):
return frappe.get_doc("Customer", customer_name)

cust = frappe.new_doc("Customer")
cust.customer_name = customer_name
cust.customer_type = "Individual"
cust.customer_group = "ShipStation"
cust.territory = "United States"
cust.save()
frappe.db.commit()

email_id, user_name = parse_addr(customer_name)
phone_no = order.ship_to.phone if order.ship_to and order.ship_to.phone else None
if email_id or phone_no:
customer_primary_contact = create_contact(order, email_id, phone_no)
if customer_primary_contact:
cust.customer_primary_contact = customer_primary_contact.name

if order.ship_to.street1:
create_address(
order.ship_to, customer_name, order.customer_email, "Shipping"
).name
if order.bill_to.street1:
create_address(
order.bill_to, order.customer_username, order.customer_email, "Billing"
).name

try:
cust.save()
return cust
except Exception as e:
frappe.log_error(title="Error saving Shipstation Customer", message=e)


def create_contact(order: "ShipStationOrder", email_id: str=None, phone_no: str=None):
contact = frappe.get_value("Contact Email", {"email_id": email_id}, "parent")
if contact:
return frappe._dict({"name": contact})
cont: "Contact" = frappe.new_doc("Contact")
cont.first_name = order.bill_to.name or "Not Provided"
for char in "<>":
cont.first_name = cont.first_name.replace(char, "")
if email_id:
cont.append("email_ids", {"email_id": email_id})
cont.append("links", {"link_doctype": "Customer", "link_name": email_id})
if phone_no:
cont.append("phone_nos", {"phone": phone_no})
# as the data for links is same as above for email it will saved only once
cont.append("links", {"link_doctype": "Customer", "link_name": email_id})
try:
cont.save()
frappe.db.commit()
return cont
except Exception as e:
frappe.log_error(title="Error saving Shipstation Contact", message=e)


def get_billing_address(customer_name: str):
Expand Down
Loading