blob: 2cd71a5bec0cba2e734a0eabf63d7fed8b2c6a81 [file] [log] [blame]
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05301import frappe, re
2from frappe import _
3from erpnext.regional.india import states, state_numbers
Nabin Haitb962fc12017-07-17 18:02:31 +05304from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05305
6def validate_gstin_for_india(doc, method):
7 if not hasattr(doc, 'gstin'):
8 return
9
10 if doc.gstin:
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053011 doc.gstin = doc.gstin.upper()
Aditya Duggalf1bd39c2017-06-29 14:25:19 +053012 if doc.gstin != "NA":
13 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}")
14 if not p.match(doc.gstin):
15 frappe.throw(_("Invalid GSTIN or Enter NA for Unregistered"))
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053016
Nabin Hait35cd1d32017-11-28 13:01:01 +053017 if not doc.gst_state:
18 if doc.state in states:
19 doc.gst_state = doc.state
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053020
Nabin Hait35cd1d32017-11-28 13:01:01 +053021 if doc.gst_state:
22 doc.gst_state_number = state_numbers[doc.gst_state]
23 if doc.gstin and doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]:
24 frappe.throw(_("First 2 digits of GSTIN should match with State number {0}")
25 .format(doc.gst_state_number))
Rushabh Mehta7231f292017-07-13 15:00:56 +053026
Nabin Haitb962fc12017-07-17 18:02:31 +053027def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
28 if frappe.get_meta(item_doctype).has_field('gst_hsn_code'):
29 return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts
30 else:
31 return [_("Item"), _("Taxable Amount")] + tax_accounts
32
33def get_itemised_tax_breakup_data(doc):
34 itemised_tax = get_itemised_tax(doc.taxes)
35
36 itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
37
38 if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'):
39 return itemised_tax, itemised_taxable_amount
40
41 item_hsn_map = frappe._dict()
42 for d in doc.items:
43 item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code"))
44
45 hsn_tax = {}
46 for item, taxes in itemised_tax.items():
47 hsn_code = item_hsn_map.get(item)
48 hsn_tax.setdefault(hsn_code, frappe._dict())
49 for tax_account, tax_detail in taxes.items():
50 hsn_tax[hsn_code].setdefault(tax_account, {"tax_rate": 0, "tax_amount": 0})
51 hsn_tax[hsn_code][tax_account]["tax_rate"] = tax_detail.get("tax_rate")
52 hsn_tax[hsn_code][tax_account]["tax_amount"] += tax_detail.get("tax_amount")
53
54 # set taxable amount
55 hsn_taxable_amount = frappe._dict()
56 for item, taxable_amount in itemised_taxable_amount.items():
57 hsn_code = item_hsn_map.get(item)
58 hsn_taxable_amount.setdefault(hsn_code, 0)
59 hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item)
60
61 return hsn_tax, hsn_taxable_amount
62
Rushabh Mehta7231f292017-07-13 15:00:56 +053063# don't remove this function it is used in tests
64def test_method():
65 '''test function'''
66 return 'overridden'