Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 1 | import frappe, re |
| 2 | from frappe import _ |
| 3 | from erpnext.regional.india import states, state_numbers |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 4 | from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 5 | |
| 6 | def validate_gstin_for_india(doc, method): |
| 7 | if not hasattr(doc, 'gstin'): |
| 8 | return |
| 9 | |
| 10 | if doc.gstin: |
Nabin Hait | f3f0dfe | 2017-07-06 14:49:34 +0530 | [diff] [blame] | 11 | doc.gstin = doc.gstin.upper() |
Aditya Duggal | f1bd39c | 2017-06-29 14:25:19 +0530 | [diff] [blame] | 12 | 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 Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 16 | |
| 17 | if not doc.gst_state: |
| 18 | if doc.state in states: |
| 19 | doc.gst_state = doc.state |
| 20 | |
Nabin Hait | cd61a20 | 2017-07-08 13:52:13 +0530 | [diff] [blame] | 21 | if doc.gst_state: |
Nabin Hait | f3f0dfe | 2017-07-06 14:49:34 +0530 | [diff] [blame] | 22 | doc.gst_state_number = state_numbers[doc.gst_state] |
Nabin Hait | cd61a20 | 2017-07-08 13:52:13 +0530 | [diff] [blame] | 23 | if doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]: |
Nabin Hait | f3f0dfe | 2017-07-06 14:49:34 +0530 | [diff] [blame] | 24 | frappe.throw(_("First 2 digits of GSTIN should match with State number {0}") |
Nabin Hait | cd61a20 | 2017-07-08 13:52:13 +0530 | [diff] [blame] | 25 | .format(doc.gst_state_number)) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 26 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 27 | def 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 | |
| 33 | def 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 Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 63 | # don't remove this function it is used in tests |
| 64 | def test_method(): |
| 65 | '''test function''' |
| 66 | return 'overridden' |