Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 1 | import frappe, re |
| 2 | from frappe import _ |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 3 | from frappe.utils import cstr |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 4 | from erpnext.regional.india import states, state_numbers |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 5 | from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 6 | from erpnext.controllers.accounts_controller import get_taxes_and_charges |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 7 | |
| 8 | def validate_gstin_for_india(doc, method): |
| 9 | if not hasattr(doc, 'gstin'): |
| 10 | return |
| 11 | |
| 12 | if doc.gstin: |
Nabin Hait | f3f0dfe | 2017-07-06 14:49:34 +0530 | [diff] [blame] | 13 | doc.gstin = doc.gstin.upper() |
rohitwaghchaure | 2c111b7 | 2018-04-11 15:50:06 +0530 | [diff] [blame] | 14 | if doc.gstin not in ["NA", "na"]: |
Aditya Duggal | f1bd39c | 2017-06-29 14:25:19 +0530 | [diff] [blame] | 15 | 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 Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 18 | |
Nabin Hait | 35cd1d3 | 2017-11-28 13:01:01 +0530 | [diff] [blame] | 19 | if not doc.gst_state: |
| 20 | if doc.state in states: |
| 21 | doc.gst_state = doc.state |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 22 | |
Nabin Hait | 35cd1d3 | 2017-11-28 13:01:01 +0530 | [diff] [blame] | 23 | 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 Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 28 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 29 | def 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 Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 34 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 35 | def 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 Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 39 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 40 | 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 Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 65 | def set_place_of_supply(doc, method=None): |
| 66 | doc.place_of_supply = get_place_of_supply(doc, doc.doctype) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 67 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 68 | # don't remove this function it is used in tests |
| 69 | def test_method(): |
| 70 | '''test function''' |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 71 | return 'overridden' |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 72 | |
| 73 | def 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 | |
| 85 | def get_regional_address_details(out, doctype, company): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 86 | 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" |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 92 | if not out.company_gstin: |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 93 | return |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 94 | elif doctype == "Purchase Invoice": |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 95 | master_doctype = "Purchase Taxes and Charges Template" |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 96 | if not out.supplier_gstin: |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 97 | return |
| 98 | |
Rohit Waghchaure | de82ad1 | 2018-06-07 13:20:57 +0530 | [diff] [blame] | 99 | 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 Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 102 | 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) |