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

fix: error when there is no billing address in shopify order #284

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion ecommerce_integrations/shopify/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
create_shopify_log(status="Invalid", message="Sales order already exists, not synced")
return
try:
shopify_customer = order.get("customer") if order.get("customer") is not None else {}
shopify_customer["billing_address"] = order.get("billing_address", "")
shopify_customer["shipping_address"] = order.get("shipping_address", "")

Check warning on line 43 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L41-L43

Added lines #L41 - L43 were not covered by tests
customer_id = shopify_customer.get("id")
if customer_id:
customer = ShopifyCustomer(customer_id=customer_id)
Expand Down Expand Up @@ -74,9 +74,9 @@


def create_sales_order(shopify_order, setting, company=None):
customer = setting.default_customer
if customer_id := shopify_order.get("customer", {}).get("id"):
if customer_id := shopify_order.get("customer").get("id") if shopify_order.get("customer") is not None else "":
customer = frappe.db.get_value("Customer", {CUSTOMER_ID_FIELD: customer_id}, "name")

Check warning on line 79 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L77-L79

Added lines #L77 - L79 were not covered by tests

so = frappe.db.get_value("Sales Order", {ORDER_ID_FIELD: shopify_order.get("id")}, "name")

Expand All @@ -100,7 +100,7 @@

return ""

taxes = get_order_taxes(shopify_order, setting, items)

Check warning on line 103 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L103

Added line #L103 was not covered by tests
so = frappe.get_doc(
{
"doctype": "Sales Order",
Expand Down Expand Up @@ -215,7 +215,7 @@
}
)

update_taxes_with_shipping_lines(

Check warning on line 218 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L218

Added line #L218 was not covered by tests
taxes,
shopify_order.get("shipping_lines"),
setting,
Expand All @@ -223,22 +223,22 @@
taxes_inclusive=shopify_order.get("taxes_included"),
)

if cint(setting.consolidate_taxes):
taxes = consolidate_order_taxes(taxes)

Check warning on line 227 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L226-L227

Added lines #L226 - L227 were not covered by tests

for row in taxes:
tax_detail = row.get("item_wise_tax_detail")
if isinstance(tax_detail, dict):
row["item_wise_tax_detail"] = json.dumps(tax_detail)

Check warning on line 232 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L229-L232

Added lines #L229 - L232 were not covered by tests

return taxes


def consolidate_order_taxes(taxes):
tax_account_wise_data = {}
for tax in taxes:
account_head = tax["account_head"]
tax_account_wise_data.setdefault(

Check warning on line 241 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L238-L241

Added lines #L238 - L241 were not covered by tests
account_head,
{
"charge_type": "Actual",
Expand All @@ -251,11 +251,11 @@
"item_wise_tax_detail": {},
},
)
tax_account_wise_data[account_head]["tax_amount"] += flt(tax.get("tax_amount"))
if tax.get("item_wise_tax_detail"):
tax_account_wise_data[account_head]["item_wise_tax_detail"].update(tax["item_wise_tax_detail"])

Check warning on line 256 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L254-L256

Added lines #L254 - L256 were not covered by tests

return tax_account_wise_data.values()

Check warning on line 258 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L258

Added line #L258 was not covered by tests


def get_tax_account_head(tax, charge_type: Optional[Literal["shipping", "sales_tax"]] = None):
Expand All @@ -265,8 +265,8 @@
"Shopify Tax Account", {"parent": SETTING_DOCTYPE, "shopify_tax": tax_title}, "tax_account",
)

if not tax_account and charge_type:
tax_account = frappe.db.get_single_value(SETTING_DOCTYPE, DEFAULT_TAX_FIELDS[charge_type])

Check warning on line 269 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L268-L269

Added lines #L268 - L269 were not covered by tests

if not tax_account:
frappe.throw(_("Tax Account not specified for Shopify Tax {0}").format(tax.get("title")))
Expand All @@ -287,7 +287,7 @@
def update_taxes_with_shipping_lines(taxes, shipping_lines, setting, items, taxes_inclusive=False):
"""Shipping lines represents the shipping details,
each such shipping detail consists of a list of tax_lines"""
shipping_as_item = cint(setting.add_shipping_as_item) and setting.shipping_item

Check warning on line 290 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L290

Added line #L290 was not covered by tests
for shipping_charge in shipping_lines:
if shipping_charge.get("price"):
shipping_discounts = shipping_charge.get("discount_allocations") or []
Expand All @@ -300,8 +300,8 @@
if bool(taxes_inclusive):
shipping_charge_amount -= total_tax

if shipping_as_item:
items.append(

Check warning on line 304 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L303-L304

Added lines #L303 - L304 were not covered by tests
{
"item_code": setting.shipping_item,
"rate": shipping_charge_amount,
Expand All @@ -312,7 +312,7 @@
}
)
else:
taxes.append(

Check warning on line 315 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L315

Added line #L315 was not covered by tests
{
"charge_type": "Actual",
"account_head": get_tax_account_head(shipping_charge, charge_type="shipping"),
Expand Down Expand Up @@ -395,21 +395,21 @@

@temp_shopify_session
def sync_old_orders():
shopify_setting = frappe.get_cached_doc(SETTING_DOCTYPE)

Check warning on line 398 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L398

Added line #L398 was not covered by tests
if not cint(shopify_setting.sync_old_orders):
return

orders = _fetch_old_orders(shopify_setting.old_orders_from, shopify_setting.old_orders_to)

Check warning on line 402 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L402

Added line #L402 was not covered by tests

for order in orders:
log = create_shopify_log(

Check warning on line 405 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L404-L405

Added lines #L404 - L405 were not covered by tests
method=EVENT_MAPPER["orders/create"], request_data=json.dumps(order), make_new=True
)
sync_sales_order(order, request_id=log.name)

Check warning on line 408 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L408

Added line #L408 was not covered by tests

shopify_setting = frappe.get_doc(SETTING_DOCTYPE)
shopify_setting.sync_old_orders = 0
shopify_setting.save()

Check warning on line 412 in ecommerce_integrations/shopify/order.py

View check run for this annotation

Codecov / codecov/patch

ecommerce_integrations/shopify/order.py#L410-L412

Added lines #L410 - L412 were not covered by tests


def _fetch_old_orders(from_time, to_time):
Expand Down
Loading