blob: f12f54b1da235a667bc02aa01b1203708452fe35 [file] [log] [blame]
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05301import frappe, re
2from frappe import _
Nabin Haitb95ecd72018-02-16 13:19:04 +05303from frappe.utils import cstr
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05304from erpnext.regional.india import states, state_numbers
Nabin Haitb962fc12017-07-17 18:02:31 +05305from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
Shreya Shah4fa600a2018-06-05 11:27:53 +05306from erpnext.controllers.accounts_controller import get_taxes_and_charges
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05307
8def validate_gstin_for_india(doc, method):
9 if not hasattr(doc, 'gstin'):
10 return
11
12 if doc.gstin:
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053013 doc.gstin = doc.gstin.upper()
rohitwaghchaure2c111b72018-04-11 15:50:06 +053014 if doc.gstin not in ["NA", "na"]:
Aditya Duggalf1bd39c2017-06-29 14:25:19 +053015 p = re.compile("[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9A-Za-z]{1}[Z]{1}[0-9a-zA-Z]{1}")
16 if not p.match(doc.gstin):
17 frappe.throw(_("Invalid GSTIN or Enter NA for Unregistered"))
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053018
Nabin Hait35cd1d32017-11-28 13:01:01 +053019 if not doc.gst_state:
20 if doc.state in states:
21 doc.gst_state = doc.state
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053022
Nabin Hait35cd1d32017-11-28 13:01:01 +053023 if doc.gst_state:
24 doc.gst_state_number = state_numbers[doc.gst_state]
25 if doc.gstin and doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]:
26 frappe.throw(_("First 2 digits of GSTIN should match with State number {0}")
27 .format(doc.gst_state_number))
Rushabh Mehta7231f292017-07-13 15:00:56 +053028
Nabin Haitb962fc12017-07-17 18:02:31 +053029def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
30 if frappe.get_meta(item_doctype).has_field('gst_hsn_code'):
31 return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts
32 else:
33 return [_("Item"), _("Taxable Amount")] + tax_accounts
Nabin Haitb95ecd72018-02-16 13:19:04 +053034
Nabin Haitb962fc12017-07-17 18:02:31 +053035def get_itemised_tax_breakup_data(doc):
36 itemised_tax = get_itemised_tax(doc.taxes)
37
38 itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
Nabin Haitb95ecd72018-02-16 13:19:04 +053039
Nabin Haitb962fc12017-07-17 18:02:31 +053040 if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'):
41 return itemised_tax, itemised_taxable_amount
42
43 item_hsn_map = frappe._dict()
44 for d in doc.items:
45 item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code"))
46
47 hsn_tax = {}
48 for item, taxes in itemised_tax.items():
49 hsn_code = item_hsn_map.get(item)
50 hsn_tax.setdefault(hsn_code, frappe._dict())
51 for tax_account, tax_detail in taxes.items():
52 hsn_tax[hsn_code].setdefault(tax_account, {"tax_rate": 0, "tax_amount": 0})
53 hsn_tax[hsn_code][tax_account]["tax_rate"] = tax_detail.get("tax_rate")
54 hsn_tax[hsn_code][tax_account]["tax_amount"] += tax_detail.get("tax_amount")
55
56 # set taxable amount
57 hsn_taxable_amount = frappe._dict()
58 for item, taxable_amount in itemised_taxable_amount.items():
59 hsn_code = item_hsn_map.get(item)
60 hsn_taxable_amount.setdefault(hsn_code, 0)
61 hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item)
62
63 return hsn_tax, hsn_taxable_amount
64
Shreya Shah4fa600a2018-06-05 11:27:53 +053065def set_place_of_supply(doc, method=None):
66 doc.place_of_supply = get_place_of_supply(doc, doc.doctype)
Nabin Haitb95ecd72018-02-16 13:19:04 +053067
Rushabh Mehta7231f292017-07-13 15:00:56 +053068# don't remove this function it is used in tests
69def test_method():
70 '''test function'''
Nabin Haitb95ecd72018-02-16 13:19:04 +053071 return 'overridden'
Shreya Shah4fa600a2018-06-05 11:27:53 +053072
73def get_place_of_supply(out, doctype):
74 if not frappe.get_meta('Address').has_field('gst_state'): return
75
76 if doctype in ("Sales Invoice", "Delivery Note"):
77 address_name = out.shipping_address_name or out.customer_address
78 elif doctype == "Purchase Invoice":
79 address_name = out.shipping_address or out.supplier_address
80
81 if address_name:
82 address = frappe.db.get_value("Address", address_name, ["gst_state", "gst_state_number"], as_dict=1)
83 return cstr(address.gst_state_number) + "-" + cstr(address.gst_state)
84
85def get_regional_address_details(out, doctype, company):
Shreya Shah4fa600a2018-06-05 11:27:53 +053086 out.place_of_supply = get_place_of_supply(out, doctype)
87
88 if not out.place_of_supply: return
89
90 if doctype in ("Sales Invoice", "Delivery Note"):
91 master_doctype = "Sales Taxes and Charges Template"
92 if not (out.company_gstin or out.place_of_supply):
93 return
94 else:
95 master_doctype = "Purchase Taxes and Charges Template"
96 if not (out.supplier_gstin or out.place_of_supply):
97 return
98
Rohit Waghchaurede82ad12018-06-07 13:20:57 +053099 if ((doctype in ("Sales Invoice", "Delivery Note") and out.company_gstin
100 and out.company_gstin[:2] != out.place_of_supply[:2]) or (doctype == "Purchase Invoice"
101 and out.supplier_gstin and out.supplier_gstin[:2] != out.place_of_supply[:2])):
Shreya Shah4fa600a2018-06-05 11:27:53 +0530102 default_tax = frappe.db.get_value(master_doctype, {"company": company, "is_inter_state":1, "disabled":0})
103 else:
104 default_tax = frappe.db.get_value(master_doctype, {"company": company, "disabled":0, "is_default": 1})
105
106 if not default_tax:
107 return
108 out["taxes_and_charges"] = default_tax
109 out.taxes = get_taxes_and_charges(master_doctype, default_tax)