Skip to content

Commit

Permalink
feat(shopify): consolidate tax accounts in order
Browse files Browse the repository at this point in the history
  • Loading branch information
rtdany10 committed Feb 9, 2023
1 parent 1cbdf6b commit 4d60fb0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
"column_break_22",
"section_break_25",
"sales_order_series",
"delivery_note_series",
"sales_invoice_series",
"shipping_item",
"column_break_27",
"sync_delivery_note",
"delivery_note_series",
"sync_sales_invoice",
"sales_invoice_series",
"add_shipping_as_item",
"consolidate_taxes",
"section_break_22",
"html_16",
"taxes",
Expand Down Expand Up @@ -178,7 +181,7 @@
{
"fieldname": "section_break_25",
"fieldtype": "Section Break",
"label": "Order sync Settings"
"label": "Order Sync Settings"
},
{
"fieldname": "sales_order_series",
Expand Down Expand Up @@ -347,12 +350,32 @@
"fieldname": "sync_new_item_as_active",
"fieldtype": "Check",
"label": "Sync New Items as Active"
},
{
"default": "0",
"fieldname": "add_shipping_as_item",
"fieldtype": "Check",
"label": "Add Shipping Charge as an Item in Order"
},
{
"depends_on": "add_shipping_as_item",
"fieldname": "shipping_item",
"fieldtype": "Link",
"label": "Shipping Item",
"mandatory_depends_on": "add_shipping_as_item",
"options": "Item"
},
{
"default": "0",
"fieldname": "consolidate_taxes",
"fieldtype": "Check",
"label": "Consolidate Taxes"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2022-11-01 16:09:42.685577",
"modified": "2023-02-09 17:46:33.636068",
"modified_by": "Administrator",
"module": "shopify",
"name": "Shopify Setting",
Expand Down
81 changes: 62 additions & 19 deletions ecommerce_integrations/shopify/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def create_sales_order(shopify_order, setting, company=None):

return ""

taxes = get_order_taxes(shopify_order, setting, items)
so = frappe.get_doc(
{
"doctype": "Sales Order",
Expand All @@ -106,7 +107,7 @@ def create_sales_order(shopify_order, setting, company=None):
"selling_price_list": get_dummy_price_list(),
"ignore_pricing_rule": 1,
"items": items,
"taxes": get_order_taxes(shopify_order, setting),
"taxes": taxes,
"tax_category": get_dummy_tax_category(),
}
)
Expand Down Expand Up @@ -185,7 +186,7 @@ def _get_total_discount(line_item) -> float:
return sum(flt(discount.get("amount")) for discount in discount_allocations)


def get_order_taxes(shopify_order, setting):
def get_order_taxes(shopify_order, setting, items):
taxes = []
line_items = shopify_order.get("line_items")

Expand All @@ -202,23 +203,51 @@ def get_order_taxes(shopify_order, setting):
"tax_amount": tax.get("price"),
"included_in_print_rate": 0,
"cost_center": setting.cost_center,
"item_wise_tax_detail": json.dumps(
{item_code: [flt(tax.get("rate")) * 100, flt(tax.get("price"))]}
),
"item_wise_tax_detail": {
item_code: [flt(tax.get("rate")) * 100, flt(tax.get("price"))]
},
"dont_recompute_tax": 1,
}
)

taxes = update_taxes_with_shipping_lines(
update_taxes_with_shipping_lines(
taxes,
shopify_order.get("shipping_lines"),
setting,
items,
taxes_inclusive=shopify_order.get("taxes_included"),
)

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

for row in taxes:
row["item_wise_tax_detail"] = json.dumps(row["item_wise_tax_detail"])

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(account_head, {
"charge_type": "Actual",
"account_head": account_head,
"description": tax.get("description"),
"cost_center": tax.get("cost_center"),
"included_in_print_rate": 0,
"dont_recompute_tax": 1,
"tax_amount": 0,
"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"])

return tax_account_wise_data.values()


def get_tax_account_head(tax):
tax_title = str(tax.get("title"))

Expand All @@ -242,12 +271,12 @@ def get_tax_account_description(tax):
return tax_description


def update_taxes_with_shipping_lines(taxes, shipping_lines, setting, taxes_inclusive=False):
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)
for shipping_charge in shipping_lines:
if shipping_charge.get("price"):

shipping_discounts = shipping_charge.get("discount_allocations") or []
total_discount = sum(flt(discount.get("amount")) for discount in shipping_discounts)

Expand All @@ -258,15 +287,27 @@ def update_taxes_with_shipping_lines(taxes, shipping_lines, setting, taxes_inclu
if bool(taxes_inclusive):
shipping_charge_amount -= total_tax

taxes.append(
{
"charge_type": "Actual",
"account_head": get_tax_account_head(shipping_charge),
"description": get_tax_account_description(shipping_charge) or shipping_charge["title"],
"tax_amount": shipping_charge_amount,
"cost_center": setting.cost_center,
}
)
if shipping_as_item:
items.append(
{
"item_code": setting.shipping_item,
"rate": shipping_charge_amount,
"delivery_date": items[-1]["delivery_date"] if items else nowdate(),
"qty": 1,
"stock_uom": "Nos",
"warehouse": setting.warehouse,
}
)
else:
taxes.append(
{
"charge_type": "Actual",
"account_head": get_tax_account_head(shipping_charge),
"description": get_tax_account_description(shipping_charge) or shipping_charge["title"],
"tax_amount": shipping_charge_amount,
"cost_center": setting.cost_center,
}
)

for tax in shipping_charge.get("tax_lines"):
taxes.append(
Expand All @@ -278,11 +319,13 @@ def update_taxes_with_shipping_lines(taxes, shipping_lines, setting, taxes_inclu
),
"tax_amount": tax["price"],
"cost_center": setting.cost_center,
"item_wise_tax_detail": {
setting.shipping_item: [flt(tax.get("rate")) * 100, flt(tax.get("price"))]
} if shipping_as_item else {},
"dont_recompute_tax": 1,
}
)

return taxes


def get_sales_order(order_id):
"""Get ERPNext sales order using shopify order id."""
Expand Down

0 comments on commit 4d60fb0

Please sign in to comment.