2017-06-21 11:52:38 +00:00
|
|
|
import frappe, re
|
|
|
|
from frappe import _
|
2018-02-16 07:49:04 +00:00
|
|
|
from frappe.utils import cstr
|
2017-06-21 11:52:38 +00:00
|
|
|
from erpnext.regional.india import states, state_numbers
|
2017-07-17 12:32:31 +00:00
|
|
|
from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
|
2017-06-21 11:52:38 +00:00
|
|
|
|
|
|
|
def validate_gstin_for_india(doc, method):
|
|
|
|
if not hasattr(doc, 'gstin'):
|
|
|
|
return
|
|
|
|
|
2019-01-10 05:37:51 +00:00
|
|
|
doc.gstin = doc.gstin.upper().strip()
|
|
|
|
if not doc.gstin or doc.gstin == 'NA':
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(doc.gstin) != 15:
|
|
|
|
frappe.throw(_("Invalid GSTIN! A GSTIN must have 15 characters."))
|
|
|
|
|
|
|
|
p = re.compile("^[0-9]{2}[A-Z]{4}[0-9A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}[1-9A-Z]{1}[0-9A-Z]{1}$")
|
|
|
|
if not p.match(doc.gstin):
|
|
|
|
frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the format of GSTIN."))
|
2017-06-21 11:52:38 +00:00
|
|
|
|
2019-01-10 05:37:51 +00:00
|
|
|
validate_gstin_check_digit(doc.gstin)
|
2017-06-21 11:52:38 +00:00
|
|
|
|
2019-01-10 05:37:51 +00:00
|
|
|
if not doc.gst_state and doc.state:
|
|
|
|
state = doc.state.lower()
|
|
|
|
states_lowercase = {s.lower():s for s in states}
|
|
|
|
if state in states_lowercase:
|
|
|
|
doc.gst_state = states_lowercase[state]
|
|
|
|
else:
|
|
|
|
return
|
2017-07-13 09:30:56 +00:00
|
|
|
|
2019-01-10 05:37:51 +00:00
|
|
|
doc.gst_state_number = state_numbers[doc.gst_state]
|
|
|
|
if doc.gst_state_number != doc.gstin[:2]:
|
|
|
|
frappe.throw(_("Invalid GSTIN! First 2 digits of GSTIN should match with State number {0}.")
|
|
|
|
.format(doc.gst_state_number))
|
2019-01-09 13:45:10 +00:00
|
|
|
|
2019-01-10 05:37:51 +00:00
|
|
|
def validate_gstin_check_digit(gstin):
|
|
|
|
''' Function to validate the check digit of the GSTIN.'''
|
2019-01-09 13:45:10 +00:00
|
|
|
factor = 1
|
|
|
|
total = 0
|
|
|
|
code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
mod = len(code_point_chars)
|
2019-01-10 05:37:51 +00:00
|
|
|
input_chars = gstin[:-1]
|
2019-01-09 13:45:10 +00:00
|
|
|
for char in input_chars:
|
|
|
|
digit = factor * code_point_chars.find(char)
|
2019-01-10 05:37:51 +00:00
|
|
|
digit = (digit // mod) + (digit % mod)
|
2019-01-09 13:45:10 +00:00
|
|
|
total += digit
|
|
|
|
factor = 2 if factor == 1 else 1
|
2019-01-10 05:37:51 +00:00
|
|
|
if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]:
|
|
|
|
frappe.throw(_("Invalid GSTIN! The check digit validation has failed. " +
|
|
|
|
"Please ensure you've typed the GSTIN correctly."))
|
2019-01-09 13:45:10 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
|
|
|
|
if frappe.get_meta(item_doctype).has_field('gst_hsn_code'):
|
|
|
|
return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts
|
|
|
|
else:
|
|
|
|
return [_("Item"), _("Taxable Amount")] + tax_accounts
|
2018-02-16 07:49:04 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
def get_itemised_tax_breakup_data(doc):
|
|
|
|
itemised_tax = get_itemised_tax(doc.taxes)
|
|
|
|
|
|
|
|
itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
|
2018-02-16 07:49:04 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'):
|
|
|
|
return itemised_tax, itemised_taxable_amount
|
|
|
|
|
|
|
|
item_hsn_map = frappe._dict()
|
|
|
|
for d in doc.items:
|
|
|
|
item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code"))
|
|
|
|
|
|
|
|
hsn_tax = {}
|
|
|
|
for item, taxes in itemised_tax.items():
|
|
|
|
hsn_code = item_hsn_map.get(item)
|
|
|
|
hsn_tax.setdefault(hsn_code, frappe._dict())
|
|
|
|
for tax_account, tax_detail in taxes.items():
|
|
|
|
hsn_tax[hsn_code].setdefault(tax_account, {"tax_rate": 0, "tax_amount": 0})
|
|
|
|
hsn_tax[hsn_code][tax_account]["tax_rate"] = tax_detail.get("tax_rate")
|
|
|
|
hsn_tax[hsn_code][tax_account]["tax_amount"] += tax_detail.get("tax_amount")
|
|
|
|
|
|
|
|
# set taxable amount
|
|
|
|
hsn_taxable_amount = frappe._dict()
|
|
|
|
for item, taxable_amount in itemised_taxable_amount.items():
|
|
|
|
hsn_code = item_hsn_map.get(item)
|
|
|
|
hsn_taxable_amount.setdefault(hsn_code, 0)
|
|
|
|
hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item)
|
|
|
|
|
|
|
|
return hsn_tax, hsn_taxable_amount
|
|
|
|
|
2018-02-16 07:49:04 +00:00
|
|
|
def set_place_of_supply(doc, method):
|
|
|
|
if not frappe.get_meta('Address').has_field('gst_state'): return
|
|
|
|
|
2018-04-04 05:35:21 +00:00
|
|
|
if doc.doctype in ("Sales Invoice", "Delivery Note"):
|
2018-02-16 07:49:04 +00:00
|
|
|
address_name = doc.shipping_address_name or doc.customer_address
|
|
|
|
elif doc.doctype == "Purchase Invoice":
|
|
|
|
address_name = doc.shipping_address or doc.supplier_address
|
|
|
|
|
|
|
|
if address_name:
|
|
|
|
address = frappe.db.get_value("Address", address_name, ["gst_state", "gst_state_number"], as_dict=1)
|
|
|
|
doc.place_of_supply = cstr(address.gst_state_number) + "-" + cstr(address.gst_state)
|
|
|
|
|
2017-07-13 09:30:56 +00:00
|
|
|
# don't remove this function it is used in tests
|
|
|
|
def test_method():
|
|
|
|
'''test function'''
|
2018-02-16 07:49:04 +00:00
|
|
|
return 'overridden'
|