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 |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 6 | |
| 7 | def validate_gstin_for_india(doc, method): |
| 8 | if not hasattr(doc, 'gstin'): |
| 9 | return |
| 10 | |
| 11 | if doc.gstin: |
Nabin Hait | f3f0dfe | 2017-07-06 14:49:34 +0530 | [diff] [blame] | 12 | doc.gstin = doc.gstin.upper() |
Aditya Duggal | f1bd39c | 2017-06-29 14:25:19 +0530 | [diff] [blame] | 13 | if doc.gstin != "NA": |
| 14 | 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}") |
| 15 | if not p.match(doc.gstin): |
| 16 | frappe.throw(_("Invalid GSTIN or Enter NA for Unregistered")) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 17 | |
Nabin Hait | 35cd1d3 | 2017-11-28 13:01:01 +0530 | [diff] [blame] | 18 | if not doc.gst_state: |
| 19 | if doc.state in states: |
| 20 | doc.gst_state = doc.state |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 21 | |
Nabin Hait | 35cd1d3 | 2017-11-28 13:01:01 +0530 | [diff] [blame] | 22 | if doc.gst_state: |
| 23 | doc.gst_state_number = state_numbers[doc.gst_state] |
| 24 | if doc.gstin and doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]: |
| 25 | frappe.throw(_("First 2 digits of GSTIN should match with State number {0}") |
| 26 | .format(doc.gst_state_number)) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 27 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 28 | def get_itemised_tax_breakup_header(item_doctype, tax_accounts): |
| 29 | if frappe.get_meta(item_doctype).has_field('gst_hsn_code'): |
| 30 | return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts |
| 31 | else: |
| 32 | return [_("Item"), _("Taxable Amount")] + tax_accounts |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 33 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 34 | def get_itemised_tax_breakup_data(doc): |
| 35 | itemised_tax = get_itemised_tax(doc.taxes) |
| 36 | |
| 37 | itemised_taxable_amount = get_itemised_taxable_amount(doc.items) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 38 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 39 | if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'): |
| 40 | return itemised_tax, itemised_taxable_amount |
| 41 | |
| 42 | item_hsn_map = frappe._dict() |
| 43 | for d in doc.items: |
| 44 | item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code")) |
| 45 | |
| 46 | hsn_tax = {} |
| 47 | for item, taxes in itemised_tax.items(): |
| 48 | hsn_code = item_hsn_map.get(item) |
| 49 | hsn_tax.setdefault(hsn_code, frappe._dict()) |
| 50 | for tax_account, tax_detail in taxes.items(): |
| 51 | hsn_tax[hsn_code].setdefault(tax_account, {"tax_rate": 0, "tax_amount": 0}) |
| 52 | hsn_tax[hsn_code][tax_account]["tax_rate"] = tax_detail.get("tax_rate") |
| 53 | hsn_tax[hsn_code][tax_account]["tax_amount"] += tax_detail.get("tax_amount") |
| 54 | |
| 55 | # set taxable amount |
| 56 | hsn_taxable_amount = frappe._dict() |
| 57 | for item, taxable_amount in itemised_taxable_amount.items(): |
| 58 | hsn_code = item_hsn_map.get(item) |
| 59 | hsn_taxable_amount.setdefault(hsn_code, 0) |
| 60 | hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item) |
| 61 | |
| 62 | return hsn_tax, hsn_taxable_amount |
| 63 | |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 64 | def set_place_of_supply(doc, method): |
| 65 | if not frappe.get_meta('Address').has_field('gst_state'): return |
| 66 | |
Vishal Dhayagude | 2505c74 | 2018-04-04 11:05:21 +0530 | [diff] [blame] | 67 | if doc.doctype in ("Sales Invoice", "Delivery Note"): |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 68 | address_name = doc.shipping_address_name or doc.customer_address |
| 69 | elif doc.doctype == "Purchase Invoice": |
| 70 | address_name = doc.shipping_address or doc.supplier_address |
| 71 | |
| 72 | if address_name: |
| 73 | address = frappe.db.get_value("Address", address_name, ["gst_state", "gst_state_number"], as_dict=1) |
| 74 | doc.place_of_supply = cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
| 75 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 76 | # don't remove this function it is used in tests |
| 77 | def test_method(): |
| 78 | '''test function''' |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 79 | return 'overridden' |