Aditya Hase | f3c22f3 | 2019-01-22 18:22:20 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 2 | import frappe, re, json |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 3 | from frappe import _ |
Deepesh Garg | 1c14606 | 2020-08-18 19:32:52 +0530 | [diff] [blame] | 4 | import erpnext |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 5 | from frappe.utils import cstr, flt, date_diff, nowdate, round_based_on_smallest_currency_fraction, money_in_words, getdate |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 6 | from erpnext.regional.india import states, state_numbers |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 7 | 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] | 8 | from erpnext.controllers.accounts_controller import get_taxes_and_charges |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 9 | from erpnext.hr.utils import get_salary_assignment |
Anurag Mishra | 289c822 | 2020-06-19 19:17:57 +0530 | [diff] [blame] | 10 | from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 11 | from erpnext.regional.india import number_state_mapping |
| 12 | from six import string_types |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 13 | from erpnext.accounts.general_ledger import make_gl_entries |
| 14 | from erpnext.accounts.utils import get_account_currency |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 15 | from frappe.model.utils import get_fetch_values |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 16 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 17 | |
| 18 | GST_INVOICE_NUMBER_FORMAT = re.compile(r"^[a-zA-Z0-9\-/]+$") #alphanumeric and - / |
| 19 | GSTIN_FORMAT = 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}$") |
| 20 | GSTIN_UIN_FORMAT = re.compile("^[0-9]{4}[A-Z]{3}[0-9]{5}[0-9A-Z]{3}") |
| 21 | PAN_NUMBER_FORMAT = re.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}") |
| 22 | |
| 23 | |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 24 | def validate_gstin_for_india(doc, method): |
rushin29 | 08a209b | 2019-03-15 15:28:50 +0530 | [diff] [blame] | 25 | if hasattr(doc, 'gst_state') and doc.gst_state: |
| 26 | doc.gst_state_number = state_numbers[doc.gst_state] |
FinByz Tech Pvt. Ltd | 237a871 | 2019-01-22 20:49:06 +0530 | [diff] [blame] | 27 | if not hasattr(doc, 'gstin') or not doc.gstin: |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 28 | return |
| 29 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 30 | gst_category = [] |
| 31 | |
| 32 | if len(doc.links): |
| 33 | link_doctype = doc.links[0].get("link_doctype") |
| 34 | link_name = doc.links[0].get("link_name") |
| 35 | |
| 36 | if link_doctype in ["Customer", "Supplier"]: |
| 37 | gst_category = frappe.db.get_value(link_doctype, {'name': link_name}, ['gst_category']) |
| 38 | |
Sagar Vora | d75095b | 2019-01-23 14:40:01 +0530 | [diff] [blame] | 39 | doc.gstin = doc.gstin.upper().strip() |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 40 | if not doc.gstin or doc.gstin == 'NA': |
| 41 | return |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 42 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 43 | if len(doc.gstin) != 15: |
| 44 | frappe.throw(_("Invalid GSTIN! A GSTIN must have 15 characters.")) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 45 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 46 | if gst_category and gst_category == 'UIN Holders': |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 47 | if not GSTIN_UIN_FORMAT.match(doc.gstin): |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 48 | frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the GSTIN format for UIN Holders or Non-Resident OIDAR Service Providers")) |
| 49 | else: |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 50 | if not GSTIN_FORMAT.match(doc.gstin): |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 51 | frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the format of GSTIN.")) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 52 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 53 | validate_gstin_check_digit(doc.gstin) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 54 | set_gst_state_and_state_number(doc) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 55 | |
Anurag Mishra | 1e396dc | 2021-01-13 14:01:57 +0530 | [diff] [blame] | 56 | if not doc.gst_state: |
| 57 | frappe.throw(_("Please Enter GST state")) |
| 58 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 59 | if doc.gst_state_number != doc.gstin[:2]: |
| 60 | frappe.throw(_("Invalid GSTIN! First 2 digits of GSTIN should match with State number {0}.") |
| 61 | .format(doc.gst_state_number)) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 62 | |
Deepesh Garg | bb8cd1c | 2021-02-22 19:28:45 +0530 | [diff] [blame] | 63 | def validate_pan_for_india(doc, method): |
Nabin Hait | 866cf70 | 2021-02-22 21:35:00 +0530 | [diff] [blame] | 64 | if doc.get('country') != 'India' or not doc.pan: |
Deepesh Garg | bb8cd1c | 2021-02-22 19:28:45 +0530 | [diff] [blame] | 65 | return |
| 66 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 67 | if not PAN_NUMBER_FORMAT.match(doc.pan): |
Deepesh Garg | bb8cd1c | 2021-02-22 19:28:45 +0530 | [diff] [blame] | 68 | frappe.throw(_("Invalid PAN No. The input you've entered doesn't match the format of PAN.")) |
| 69 | |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 70 | def validate_tax_category(doc, method): |
Deepesh Garg | b074334 | 2020-12-17 18:46:59 +0530 | [diff] [blame] | 71 | if doc.get('gst_state') and frappe.db.get_value('Tax Category', {'gst_state': doc.gst_state, 'is_inter_state': doc.is_inter_state}): |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 72 | if doc.is_inter_state: |
| 73 | frappe.throw(_("Inter State tax category for GST State {0} already exists").format(doc.gst_state)) |
| 74 | else: |
| 75 | frappe.throw(_("Intra State tax category for GST State {0} already exists").format(doc.gst_state)) |
| 76 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 77 | def update_gst_category(doc, method): |
| 78 | for link in doc.links: |
| 79 | if link.link_doctype in ['Customer', 'Supplier']: |
| 80 | if doc.get('gstin'): |
| 81 | frappe.db.sql(""" |
| 82 | UPDATE `tab{0}` SET gst_category = %s WHERE name = %s AND gst_category = 'Unregistered' |
| 83 | """.format(link.link_doctype), ("Registered Regular", link.link_name)) #nosec |
| 84 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 85 | def set_gst_state_and_state_number(doc): |
| 86 | if not doc.gst_state: |
| 87 | if not doc.state: |
| 88 | return |
| 89 | state = doc.state.lower() |
| 90 | states_lowercase = {s.lower():s for s in states} |
| 91 | if state in states_lowercase: |
| 92 | doc.gst_state = states_lowercase[state] |
| 93 | else: |
| 94 | return |
| 95 | |
| 96 | doc.gst_state_number = state_numbers[doc.gst_state] |
| 97 | |
| 98 | def validate_gstin_check_digit(gstin, label='GSTIN'): |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 99 | ''' Function to validate the check digit of the GSTIN.''' |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 100 | factor = 1 |
| 101 | total = 0 |
| 102 | code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 103 | mod = len(code_point_chars) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 104 | input_chars = gstin[:-1] |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 105 | for char in input_chars: |
| 106 | digit = factor * code_point_chars.find(char) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 107 | digit = (digit // mod) + (digit % mod) |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 108 | total += digit |
| 109 | factor = 2 if factor == 1 else 1 |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 110 | if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]: |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 111 | frappe.throw(_("""Invalid {0}! The check digit validation has failed. Please ensure you've typed the {0} correctly.""").format(label)) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 112 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 113 | def get_itemised_tax_breakup_header(item_doctype, tax_accounts): |
| 114 | if frappe.get_meta(item_doctype).has_field('gst_hsn_code'): |
| 115 | return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts |
| 116 | else: |
| 117 | return [_("Item"), _("Taxable Amount")] + tax_accounts |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 119 | def get_itemised_tax_breakup_data(doc, account_wise=False): |
| 120 | itemised_tax = get_itemised_tax(doc.taxes, with_tax_account=account_wise) |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 121 | |
| 122 | itemised_taxable_amount = get_itemised_taxable_amount(doc.items) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 123 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 124 | if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'): |
| 125 | return itemised_tax, itemised_taxable_amount |
| 126 | |
| 127 | item_hsn_map = frappe._dict() |
| 128 | for d in doc.items: |
| 129 | item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code")) |
| 130 | |
| 131 | hsn_tax = {} |
| 132 | for item, taxes in itemised_tax.items(): |
| 133 | hsn_code = item_hsn_map.get(item) |
| 134 | hsn_tax.setdefault(hsn_code, frappe._dict()) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 135 | for tax_desc, tax_detail in taxes.items(): |
| 136 | key = tax_desc |
| 137 | if account_wise: |
| 138 | key = tax_detail.get('tax_account') |
| 139 | hsn_tax[hsn_code].setdefault(key, {"tax_rate": 0, "tax_amount": 0}) |
| 140 | hsn_tax[hsn_code][key]["tax_rate"] = tax_detail.get("tax_rate") |
| 141 | hsn_tax[hsn_code][key]["tax_amount"] += tax_detail.get("tax_amount") |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 142 | |
| 143 | # set taxable amount |
| 144 | hsn_taxable_amount = frappe._dict() |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 145 | for item in itemised_taxable_amount: |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 146 | hsn_code = item_hsn_map.get(item) |
| 147 | hsn_taxable_amount.setdefault(hsn_code, 0) |
| 148 | hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item) |
| 149 | |
| 150 | return hsn_tax, hsn_taxable_amount |
| 151 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 152 | def set_place_of_supply(doc, method=None): |
| 153 | doc.place_of_supply = get_place_of_supply(doc, doc.doctype) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 154 | |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 155 | def validate_document_name(doc, method=None): |
| 156 | """Validate GST invoice number requirements.""" |
| 157 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 158 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 159 | # Date was chosen as start of next FY to avoid irritating current users. |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 160 | if country != "India" or getdate(doc.posting_date) < getdate("2021-04-01"): |
| 161 | return |
| 162 | |
| 163 | if len(doc.name) > 16: |
| 164 | frappe.throw(_("Maximum length of document number should be 16 characters as per GST rules. Please change the naming series.")) |
| 165 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 166 | if not GST_INVOICE_NUMBER_FORMAT.match(doc.name): |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 167 | frappe.throw(_("Document name should only contain alphanumeric values, dash(-) and slash(/) characters as per GST rules. Please change the naming series.")) |
| 168 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 169 | # don't remove this function it is used in tests |
| 170 | def test_method(): |
| 171 | '''test function''' |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 172 | return 'overridden' |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 173 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 174 | def get_place_of_supply(party_details, doctype): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 175 | if not frappe.get_meta('Address').has_field('gst_state'): return |
| 176 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 177 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): |
Deepesh Garg | eacfd79 | 2020-10-30 22:12:24 +0530 | [diff] [blame] | 178 | address_name = party_details.customer_address or party_details.shipping_address_name |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 179 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 180 | address_name = party_details.shipping_address or party_details.supplier_address |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 181 | |
| 182 | if address_name: |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 183 | address = frappe.db.get_value("Address", address_name, ["gst_state", "gst_state_number", "gstin"], as_dict=1) |
Rohit Waghchaure | b6a735e | 2018-10-11 10:40:34 +0530 | [diff] [blame] | 184 | if address and address.gst_state and address.gst_state_number: |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 185 | party_details.gstin = address.gstin |
Nabin Hait | 2390da6 | 2018-08-30 16:16:35 +0530 | [diff] [blame] | 186 | return cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 187 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 188 | @frappe.whitelist() |
pateljannat | 1d5d863 | 2020-11-19 20:11:45 +0530 | [diff] [blame] | 189 | def get_regional_address_details(party_details, doctype, company): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 190 | if isinstance(party_details, string_types): |
| 191 | party_details = json.loads(party_details) |
| 192 | party_details = frappe._dict(party_details) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 193 | |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 194 | update_party_details(party_details, doctype) |
| 195 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 196 | party_details.place_of_supply = get_place_of_supply(party_details, doctype) |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 197 | |
| 198 | if is_internal_transfer(party_details, doctype): |
| 199 | party_details.taxes_and_charges = '' |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 200 | party_details.taxes = [] |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 201 | return party_details |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 202 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 203 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 204 | master_doctype = "Sales Taxes and Charges Template" |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 205 | |
| 206 | get_tax_template_for_sez(party_details, master_doctype, company, 'Customer') |
| 207 | get_tax_template_based_on_category(master_doctype, company, party_details) |
| 208 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 209 | if party_details.get('taxes_and_charges'): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 210 | return party_details |
| 211 | |
| 212 | if not party_details.company_gstin: |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 213 | return party_details |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 214 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 215 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 216 | master_doctype = "Purchase Taxes and Charges Template" |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 217 | get_tax_template_for_sez(party_details, master_doctype, company, 'Supplier') |
| 218 | get_tax_template_based_on_category(master_doctype, company, party_details) |
| 219 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 220 | if party_details.get('taxes_and_charges'): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 221 | return party_details |
| 222 | |
| 223 | if not party_details.supplier_gstin: |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 224 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 225 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 226 | if not party_details.place_of_supply: return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 227 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 228 | if not party_details.company_gstin: return party_details |
deepeshgarg007 | c58dc87 | 2019-12-12 14:55:57 +0530 | [diff] [blame] | 229 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 230 | if ((doctype in ("Sales Invoice", "Delivery Note", "Sales Order") and party_details.company_gstin |
| 231 | and party_details.company_gstin[:2] != party_details.place_of_supply[:2]) or (doctype in ("Purchase Invoice", |
| 232 | "Purchase Order", "Purchase Receipt") and party_details.supplier_gstin and party_details.supplier_gstin[:2] != party_details.place_of_supply[:2])): |
| 233 | default_tax = get_tax_template(master_doctype, company, 1, party_details.company_gstin[:2]) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 234 | else: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 235 | default_tax = get_tax_template(master_doctype, company, 0, party_details.company_gstin[:2]) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 236 | |
| 237 | if not default_tax: |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 238 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 239 | party_details["taxes_and_charges"] = default_tax |
| 240 | party_details.taxes = get_taxes_and_charges(master_doctype, default_tax) |
| 241 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 242 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 243 | |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 244 | def update_party_details(party_details, doctype): |
| 245 | for address_field in ['shipping_address', 'company_address', 'supplier_address', 'shipping_address_name', 'customer_address']: |
| 246 | if party_details.get(address_field): |
| 247 | party_details.update(get_fetch_values(doctype, address_field, party_details.get(address_field))) |
| 248 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 249 | def is_internal_transfer(party_details, doctype): |
| 250 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): |
| 251 | destination_gstin = party_details.company_gstin |
| 252 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 253 | destination_gstin = party_details.supplier_gstin |
| 254 | |
| 255 | if party_details.gstin == destination_gstin: |
| 256 | return True |
| 257 | else: |
| 258 | False |
| 259 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 260 | def get_tax_template_based_on_category(master_doctype, company, party_details): |
| 261 | if not party_details.get('tax_category'): |
| 262 | return |
| 263 | |
| 264 | default_tax = frappe.db.get_value(master_doctype, {'company': company, 'tax_category': party_details.get('tax_category')}, |
| 265 | 'name') |
| 266 | |
| 267 | if default_tax: |
| 268 | party_details["taxes_and_charges"] = default_tax |
| 269 | party_details.taxes = get_taxes_and_charges(master_doctype, default_tax) |
| 270 | |
| 271 | def get_tax_template(master_doctype, company, is_inter_state, state_code): |
| 272 | tax_categories = frappe.get_all('Tax Category', fields = ['name', 'is_inter_state', 'gst_state'], |
| 273 | filters = {'is_inter_state': is_inter_state}) |
| 274 | |
| 275 | default_tax = '' |
| 276 | |
| 277 | for tax_category in tax_categories: |
| 278 | if tax_category.gst_state == number_state_mapping[state_code] or \ |
| 279 | (not default_tax and not tax_category.gst_state): |
| 280 | default_tax = frappe.db.get_value(master_doctype, |
Deepesh Garg | 59ccb64 | 2020-11-05 16:29:34 +0530 | [diff] [blame] | 281 | {'company': company, 'disabled': 0, 'tax_category': tax_category.name}, 'name') |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 282 | return default_tax |
| 283 | |
| 284 | def get_tax_template_for_sez(party_details, master_doctype, company, party_type): |
| 285 | |
| 286 | gst_details = frappe.db.get_value(party_type, {'name': party_details.get(frappe.scrub(party_type))}, |
| 287 | ['gst_category', 'export_type'], as_dict=1) |
| 288 | |
| 289 | if gst_details: |
| 290 | if gst_details.gst_category == 'SEZ' and gst_details.export_type == 'With Payment of Tax': |
| 291 | default_tax = frappe.db.get_value(master_doctype, {"company": company, "is_inter_state":1, "disabled":0, |
| 292 | "gst_state": number_state_mapping[party_details.company_gstin[:2]]}) |
| 293 | |
| 294 | party_details["taxes_and_charges"] = default_tax |
| 295 | party_details.taxes = get_taxes_and_charges(master_doctype, default_tax) |
| 296 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 297 | |
| 298 | def calculate_annual_eligible_hra_exemption(doc): |
Nabin Hait | 10df3d5 | 2020-05-14 17:15:16 +0530 | [diff] [blame] | 299 | basic_component, hra_component = frappe.db.get_value('Company', doc.company, ["basic_component", "hra_component"]) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 300 | if not (basic_component and hra_component): |
| 301 | frappe.throw(_("Please mention Basic and HRA component in Company")) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 302 | annual_exemption, monthly_exemption, hra_amount = 0, 0, 0 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 303 | if hra_component and basic_component: |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 304 | assignment = get_salary_assignment(doc.employee, nowdate()) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 305 | if assignment: |
| 306 | hra_component_exists = frappe.db.exists("Salary Detail", { |
| 307 | "parent": assignment.salary_structure, |
| 308 | "salary_component": hra_component, |
| 309 | "parentfield": "earnings", |
| 310 | "parenttype": "Salary Structure" |
| 311 | }) |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 312 | |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 313 | if hra_component_exists: |
| 314 | basic_amount, hra_amount = get_component_amt_from_salary_slip(doc.employee, |
| 315 | assignment.salary_structure, basic_component, hra_component) |
| 316 | if hra_amount: |
| 317 | if doc.monthly_house_rent: |
| 318 | annual_exemption = calculate_hra_exemption(assignment.salary_structure, |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 319 | basic_amount, hra_amount, doc.monthly_house_rent, doc.rented_in_metro_city) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 320 | if annual_exemption > 0: |
| 321 | monthly_exemption = annual_exemption / 12 |
| 322 | else: |
| 323 | annual_exemption = 0 |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 324 | |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 325 | elif doc.docstatus == 1: |
| 326 | frappe.throw(_("Salary Structure must be submitted before submission of Tax Ememption Declaration")) |
| 327 | |
| 328 | return frappe._dict({ |
| 329 | "hra_amount": hra_amount, |
| 330 | "annual_exemption": annual_exemption, |
| 331 | "monthly_exemption": monthly_exemption |
| 332 | }) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 333 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 334 | def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component): |
Anurag Mishra | 33793d4 | 2020-04-29 11:48:41 +0530 | [diff] [blame] | 335 | salary_slip = make_salary_slip(salary_structure, employee=employee, for_preview=1, ignore_permissions=True) |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 336 | basic_amt, hra_amt = 0, 0 |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 337 | for earning in salary_slip.earnings: |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 338 | if earning.salary_component == basic_component: |
| 339 | basic_amt = earning.amount |
| 340 | elif earning.salary_component == hra_component: |
| 341 | hra_amt = earning.amount |
| 342 | if basic_amt and hra_amt: |
| 343 | return basic_amt, hra_amt |
Ranjith Kurungadam | 14e94f8 | 2018-07-16 16:12:46 +0530 | [diff] [blame] | 344 | return basic_amt, hra_amt |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 345 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 346 | def calculate_hra_exemption(salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city): |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 347 | # TODO make this configurable |
| 348 | exemptions = [] |
| 349 | frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency") |
| 350 | # case 1: The actual amount allotted by the employer as the HRA. |
| 351 | exemptions.append(get_annual_component_pay(frequency, monthly_hra)) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 352 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 353 | actual_annual_rent = monthly_house_rent * 12 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 354 | annual_basic = get_annual_component_pay(frequency, basic) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 355 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 356 | # case 2: Actual rent paid less 10% of the basic salary. |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 357 | exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1)) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 358 | # case 3: 50% of the basic salary, if the employee is staying in a metro city (40% for a non-metro city). |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 359 | exemptions.append(annual_basic * 0.5 if rented_in_metro_city else annual_basic * 0.4) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 360 | # return minimum of 3 cases |
| 361 | return min(exemptions) |
| 362 | |
| 363 | def get_annual_component_pay(frequency, amount): |
| 364 | if frequency == "Daily": |
| 365 | return amount * 365 |
| 366 | elif frequency == "Weekly": |
| 367 | return amount * 52 |
| 368 | elif frequency == "Fortnightly": |
| 369 | return amount * 26 |
| 370 | elif frequency == "Monthly": |
| 371 | return amount * 12 |
| 372 | elif frequency == "Bimonthly": |
| 373 | return amount * 6 |
| 374 | |
| 375 | def validate_house_rent_dates(doc): |
| 376 | if not doc.rented_to_date or not doc.rented_from_date: |
| 377 | frappe.throw(_("House rented dates required for exemption calculation")) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 378 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 379 | if date_diff(doc.rented_to_date, doc.rented_from_date) < 14: |
| 380 | frappe.throw(_("House rented dates should be atleast 15 days apart")) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 381 | |
| 382 | proofs = frappe.db.sql(""" |
| 383 | select name |
| 384 | from `tabEmployee Tax Exemption Proof Submission` |
| 385 | where |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 386 | docstatus=1 and employee=%(employee)s and payroll_period=%(payroll_period)s |
| 387 | and (rented_from_date between %(from_date)s and %(to_date)s or rented_to_date between %(from_date)s and %(to_date)s) |
| 388 | """, { |
| 389 | "employee": doc.employee, |
| 390 | "payroll_period": doc.payroll_period, |
| 391 | "from_date": doc.rented_from_date, |
| 392 | "to_date": doc.rented_to_date |
| 393 | }) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 394 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 395 | if proofs: |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 396 | frappe.throw(_("House rent paid days overlapping with {0}").format(proofs[0][0])) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 397 | |
| 398 | def calculate_hra_exemption_for_period(doc): |
| 399 | monthly_rent, eligible_hra = 0, 0 |
| 400 | if doc.house_rent_payment_amount: |
| 401 | validate_house_rent_dates(doc) |
| 402 | # TODO receive rented months or validate dates are start and end of months? |
| 403 | # Calc monthly rent, round to nearest .5 |
| 404 | factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1)/30 |
| 405 | factor = round(factor * 2)/2 |
| 406 | monthly_rent = doc.house_rent_payment_amount / factor |
| 407 | # update field used by calculate_annual_eligible_hra_exemption |
| 408 | doc.monthly_house_rent = monthly_rent |
| 409 | exemptions = calculate_annual_eligible_hra_exemption(doc) |
| 410 | |
| 411 | if exemptions["monthly_exemption"]: |
| 412 | # calc total exemption amount |
| 413 | eligible_hra = exemptions["monthly_exemption"] * factor |
Ranjith Kurungadam | 4f9744a | 2018-06-20 11:10:56 +0530 | [diff] [blame] | 414 | exemptions["monthly_house_rent"] = monthly_rent |
| 415 | exemptions["total_eligible_hra_exemption"] = eligible_hra |
| 416 | return exemptions |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 417 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 418 | def get_ewb_data(dt, dn): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 419 | |
| 420 | ewaybills = [] |
| 421 | for doc_name in dn: |
| 422 | doc = frappe.get_doc(dt, doc_name) |
| 423 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 424 | validate_doc(doc) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 425 | |
| 426 | data = frappe._dict({ |
| 427 | "transporterId": "", |
| 428 | "TotNonAdvolVal": 0, |
| 429 | }) |
| 430 | |
| 431 | data.userGstin = data.fromGstin = doc.company_gstin |
| 432 | data.supplyType = 'O' |
| 433 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 434 | if dt == 'Delivery Note': |
| 435 | data.subSupplyType = 1 |
| 436 | elif doc.gst_category in ['Registered Regular', 'SEZ']: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 437 | data.subSupplyType = 1 |
| 438 | elif doc.gst_category in ['Overseas', 'Deemed Export']: |
| 439 | data.subSupplyType = 3 |
| 440 | else: |
Deepesh Garg | cce3ac9 | 2020-02-02 21:25:58 +0530 | [diff] [blame] | 441 | frappe.throw(_('Unsupported GST Category for E-Way Bill JSON generation')) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 442 | |
| 443 | data.docType = 'INV' |
| 444 | data.docDate = frappe.utils.formatdate(doc.posting_date, 'dd/mm/yyyy') |
| 445 | |
| 446 | company_address = frappe.get_doc('Address', doc.company_address) |
| 447 | billing_address = frappe.get_doc('Address', doc.customer_address) |
| 448 | |
| 449 | shipping_address = frappe.get_doc('Address', doc.shipping_address_name) |
| 450 | |
| 451 | data = get_address_details(data, doc, company_address, billing_address) |
| 452 | |
| 453 | data.itemList = [] |
| 454 | data.totalValue = doc.total |
| 455 | |
| 456 | data = get_item_list(data, doc) |
| 457 | |
| 458 | disable_rounded = frappe.db.get_single_value('Global Defaults', 'disable_rounded_total') |
| 459 | data.totInvValue = doc.grand_total if disable_rounded else doc.rounded_total |
| 460 | |
| 461 | data = get_transport_details(data, doc) |
| 462 | |
| 463 | fields = { |
| 464 | "/. -": { |
| 465 | 'docNo': doc.name, |
| 466 | 'fromTrdName': doc.company, |
| 467 | 'toTrdName': doc.customer_name, |
| 468 | 'transDocNo': doc.lr_no, |
| 469 | }, |
| 470 | "@#/,&. -": { |
| 471 | 'fromAddr1': company_address.address_line1, |
| 472 | 'fromAddr2': company_address.address_line2, |
| 473 | 'fromPlace': company_address.city, |
| 474 | 'toAddr1': shipping_address.address_line1, |
| 475 | 'toAddr2': shipping_address.address_line2, |
| 476 | 'toPlace': shipping_address.city, |
| 477 | 'transporterName': doc.transporter_name |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | for allowed_chars, field_map in fields.items(): |
| 482 | for key, value in field_map.items(): |
| 483 | if not value: |
| 484 | data[key] = '' |
| 485 | else: |
| 486 | data[key] = re.sub(r'[^\w' + allowed_chars + ']', '', value) |
| 487 | |
| 488 | ewaybills.append(data) |
| 489 | |
| 490 | data = { |
| 491 | 'version': '1.0.1118', |
| 492 | 'billLists': ewaybills |
| 493 | } |
| 494 | |
| 495 | return data |
| 496 | |
| 497 | @frappe.whitelist() |
| 498 | def generate_ewb_json(dt, dn): |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 499 | dn = json.loads(dn) |
| 500 | return get_ewb_data(dt, dn) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 501 | |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 502 | @frappe.whitelist() |
| 503 | def download_ewb_json(): |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 504 | data = json.loads(frappe.local.form_dict.data) |
| 505 | frappe.local.response.filecontent = json.dumps(data, indent=4, sort_keys=True) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 506 | frappe.local.response.type = 'download' |
| 507 | |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 508 | filename_prefix = 'Bulk' |
| 509 | docname = frappe.local.form_dict.docname |
| 510 | if docname: |
| 511 | if docname.startswith('['): |
| 512 | docname = json.loads(docname) |
| 513 | if len(docname) == 1: |
| 514 | docname = docname[0] |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 515 | |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 516 | if not isinstance(docname, list): |
| 517 | # removes characters not allowed in a filename (https://stackoverflow.com/a/38766141/4767738) |
| 518 | filename_prefix = re.sub('[^\w_.)( -]', '', docname) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 519 | |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 520 | frappe.local.response.filename = '{0}_e-WayBill_Data_{1}.json'.format(filename_prefix, frappe.utils.random_string(5)) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 521 | |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 522 | @frappe.whitelist() |
| 523 | def get_gstins_for_company(company): |
| 524 | company_gstins =[] |
| 525 | if company: |
| 526 | company_gstins = frappe.db.sql("""select |
| 527 | distinct `tabAddress`.gstin |
| 528 | from |
| 529 | `tabAddress`, `tabDynamic Link` |
| 530 | where |
| 531 | `tabDynamic Link`.parent = `tabAddress`.name and |
| 532 | `tabDynamic Link`.parenttype = 'Address' and |
| 533 | `tabDynamic Link`.link_doctype = 'Company' and |
Don-Leopardo | 2b6a20a | 2020-03-16 14:06:44 -0300 | [diff] [blame] | 534 | `tabDynamic Link`.link_name = %(company)s""", {"company": company}) |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 535 | return company_gstins |
| 536 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 537 | def get_address_details(data, doc, company_address, billing_address): |
| 538 | data.fromPincode = validate_pincode(company_address.pincode, 'Company Address') |
| 539 | data.fromStateCode = data.actualFromStateCode = validate_state_code( |
| 540 | company_address.gst_state_number, 'Company Address') |
| 541 | |
| 542 | if not doc.billing_address_gstin or len(doc.billing_address_gstin) < 15: |
| 543 | data.toGstin = 'URP' |
| 544 | set_gst_state_and_state_number(billing_address) |
| 545 | else: |
| 546 | data.toGstin = doc.billing_address_gstin |
| 547 | |
| 548 | data.toPincode = validate_pincode(billing_address.pincode, 'Customer Address') |
| 549 | data.toStateCode = validate_state_code(billing_address.gst_state_number, 'Customer Address') |
| 550 | |
| 551 | if doc.customer_address != doc.shipping_address_name: |
| 552 | data.transType = 2 |
| 553 | shipping_address = frappe.get_doc('Address', doc.shipping_address_name) |
| 554 | set_gst_state_and_state_number(shipping_address) |
| 555 | data.toPincode = validate_pincode(shipping_address.pincode, 'Shipping Address') |
| 556 | data.actualToStateCode = validate_state_code(shipping_address.gst_state_number, 'Shipping Address') |
| 557 | else: |
| 558 | data.transType = 1 |
| 559 | data.actualToStateCode = data.toStateCode |
| 560 | shipping_address = billing_address |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 561 | |
Smit Vora | bbe4933 | 2020-11-18 20:58:59 +0530 | [diff] [blame] | 562 | if doc.gst_category == 'SEZ': |
| 563 | data.toStateCode = 99 |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 564 | |
| 565 | return data |
| 566 | |
| 567 | def get_item_list(data, doc): |
| 568 | for attr in ['cgstValue', 'sgstValue', 'igstValue', 'cessValue', 'OthValue']: |
| 569 | data[attr] = 0 |
| 570 | |
| 571 | gst_accounts = get_gst_accounts(doc.company, account_wise=True) |
| 572 | tax_map = { |
| 573 | 'sgst_account': ['sgstRate', 'sgstValue'], |
| 574 | 'cgst_account': ['cgstRate', 'cgstValue'], |
| 575 | 'igst_account': ['igstRate', 'igstValue'], |
| 576 | 'cess_account': ['cessRate', 'cessValue'] |
| 577 | } |
| 578 | item_data_attrs = ['sgstRate', 'cgstRate', 'igstRate', 'cessRate', 'cessNonAdvol'] |
| 579 | hsn_wise_charges, hsn_taxable_amount = get_itemised_tax_breakup_data(doc, account_wise=True) |
| 580 | for hsn_code, taxable_amount in hsn_taxable_amount.items(): |
| 581 | item_data = frappe._dict() |
| 582 | if not hsn_code: |
| 583 | frappe.throw(_('GST HSN Code does not exist for one or more items')) |
| 584 | item_data.hsnCode = int(hsn_code) |
| 585 | item_data.taxableAmount = taxable_amount |
| 586 | item_data.qtyUnit = "" |
| 587 | for attr in item_data_attrs: |
| 588 | item_data[attr] = 0 |
| 589 | |
| 590 | for account, tax_detail in hsn_wise_charges.get(hsn_code, {}).items(): |
| 591 | account_type = gst_accounts.get(account, '') |
| 592 | for tax_acc, attrs in tax_map.items(): |
| 593 | if account_type == tax_acc: |
| 594 | item_data[attrs[0]] = tax_detail.get('tax_rate') |
| 595 | data[attrs[1]] += tax_detail.get('tax_amount') |
| 596 | break |
| 597 | else: |
| 598 | data.OthValue += tax_detail.get('tax_amount') |
| 599 | |
| 600 | data.itemList.append(item_data) |
| 601 | |
| 602 | # Tax amounts rounded to 2 decimals to avoid exceeding max character limit |
| 603 | for attr in ['sgstValue', 'cgstValue', 'igstValue', 'cessValue']: |
| 604 | data[attr] = flt(data[attr], 2) |
| 605 | |
| 606 | return data |
| 607 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 608 | def validate_doc(doc): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 609 | if doc.docstatus != 1: |
Deepesh Garg | cce3ac9 | 2020-02-02 21:25:58 +0530 | [diff] [blame] | 610 | frappe.throw(_('E-Way Bill JSON can only be generated from submitted document')) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 611 | |
| 612 | if doc.is_return: |
Deepesh Garg | cce3ac9 | 2020-02-02 21:25:58 +0530 | [diff] [blame] | 613 | frappe.throw(_('E-Way Bill JSON cannot be generated for Sales Return as of now')) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 614 | |
| 615 | if doc.ewaybill: |
| 616 | frappe.throw(_('e-Way Bill already exists for this document')) |
| 617 | |
| 618 | reqd_fields = ['company_gstin', 'company_address', 'customer_address', |
| 619 | 'shipping_address_name', 'mode_of_transport', 'distance'] |
| 620 | |
| 621 | for fieldname in reqd_fields: |
| 622 | if not doc.get(fieldname): |
Deepesh Garg | cce3ac9 | 2020-02-02 21:25:58 +0530 | [diff] [blame] | 623 | frappe.throw(_('{} is required to generate E-Way Bill JSON').format( |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 624 | doc.meta.get_label(fieldname) |
Suraj Shetty | da2c69e | 2020-01-29 15:34:06 +0530 | [diff] [blame] | 625 | )) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 626 | |
| 627 | if len(doc.company_gstin) < 15: |
| 628 | frappe.throw(_('You must be a registered supplier to generate e-Way Bill')) |
| 629 | |
| 630 | def get_transport_details(data, doc): |
| 631 | if doc.distance > 4000: |
| 632 | frappe.throw(_('Distance cannot be greater than 4000 kms')) |
| 633 | |
| 634 | data.transDistance = int(round(doc.distance)) |
| 635 | |
| 636 | transport_modes = { |
| 637 | 'Road': 1, |
| 638 | 'Rail': 2, |
| 639 | 'Air': 3, |
| 640 | 'Ship': 4 |
| 641 | } |
| 642 | |
| 643 | vehicle_types = { |
| 644 | 'Regular': 'R', |
| 645 | 'Over Dimensional Cargo (ODC)': 'O' |
| 646 | } |
| 647 | |
| 648 | data.transMode = transport_modes.get(doc.mode_of_transport) |
| 649 | |
| 650 | if doc.mode_of_transport == 'Road': |
| 651 | if not doc.gst_transporter_id and not doc.vehicle_no: |
| 652 | frappe.throw(_('Either GST Transporter ID or Vehicle No is required if Mode of Transport is Road')) |
| 653 | if doc.vehicle_no: |
| 654 | data.vehicleNo = doc.vehicle_no.replace(' ', '') |
| 655 | if not doc.gst_vehicle_type: |
| 656 | frappe.throw(_('Vehicle Type is required if Mode of Transport is Road')) |
| 657 | else: |
| 658 | data.vehicleType = vehicle_types.get(doc.gst_vehicle_type) |
| 659 | else: |
| 660 | if not doc.lr_no or not doc.lr_date: |
| 661 | frappe.throw(_('Transport Receipt No and Date are mandatory for your chosen Mode of Transport')) |
| 662 | |
| 663 | if doc.lr_no: |
| 664 | data.transDocNo = doc.lr_no |
| 665 | |
| 666 | if doc.lr_date: |
| 667 | data.transDocDate = frappe.utils.formatdate(doc.lr_date, 'dd/mm/yyyy') |
| 668 | |
| 669 | if doc.gst_transporter_id: |
karthikeyan5 | ca46bed | 2020-05-30 15:00:56 +0530 | [diff] [blame] | 670 | if doc.gst_transporter_id[0:2] != "88": |
| 671 | validate_gstin_check_digit(doc.gst_transporter_id, label='GST Transporter ID') |
| 672 | data.transporterId = doc.gst_transporter_id |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 673 | |
| 674 | return data |
| 675 | |
| 676 | |
| 677 | def validate_pincode(pincode, address): |
| 678 | pin_not_found = "Pin Code doesn't exist for {}" |
| 679 | incorrect_pin = "Pin Code for {} is incorrecty formatted. It must be 6 digits (without spaces)" |
| 680 | |
| 681 | if not pincode: |
| 682 | frappe.throw(_(pin_not_found.format(address))) |
| 683 | |
| 684 | pincode = pincode.replace(' ', '') |
| 685 | if not pincode.isdigit() or len(pincode) != 6: |
| 686 | frappe.throw(_(incorrect_pin.format(address))) |
| 687 | else: |
| 688 | return int(pincode) |
| 689 | |
| 690 | def validate_state_code(state_code, address): |
| 691 | no_state_code = "GST State Code not found for {0}. Please set GST State in {0}" |
| 692 | if not state_code: |
| 693 | frappe.throw(_(no_state_code.format(address))) |
| 694 | else: |
| 695 | return int(state_code) |
| 696 | |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 697 | @frappe.whitelist() |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 698 | def get_gst_accounts(company, account_wise=False): |
| 699 | gst_accounts = frappe._dict() |
| 700 | gst_settings_accounts = frappe.get_all("GST Account", |
| 701 | filters={"parent": "GST Settings", "company": company}, |
| 702 | fields=["cgst_account", "sgst_account", "igst_account", "cess_account"]) |
| 703 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 704 | if not gst_settings_accounts and not frappe.flags.in_test: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 705 | frappe.throw(_("Please set GST Accounts in GST Settings")) |
| 706 | |
| 707 | for d in gst_settings_accounts: |
| 708 | for acc, val in d.items(): |
| 709 | if not account_wise: |
| 710 | gst_accounts.setdefault(acc, []).append(val) |
| 711 | elif val: |
| 712 | gst_accounts[val] = acc |
| 713 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 714 | return gst_accounts |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 715 | |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 716 | def update_grand_total_for_rcm(doc, method): |
Deepesh Garg | 52c319c | 2020-07-15 23:57:03 +0530 | [diff] [blame] | 717 | country = frappe.get_cached_value('Company', doc.company, 'country') |
| 718 | |
| 719 | if country != 'India': |
| 720 | return |
| 721 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 722 | gst_tax, base_gst_tax = get_gst_tax_amount(doc) |
| 723 | |
| 724 | if not base_gst_tax: |
Deepesh Garg | 8aed48f | 2020-08-19 18:30:18 +0530 | [diff] [blame] | 725 | return |
| 726 | |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 727 | if doc.reverse_charge == 'Y': |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 728 | doc.taxes_and_charges_added -= gst_tax |
| 729 | doc.total_taxes_and_charges -= gst_tax |
Deepesh Garg | 1c14606 | 2020-08-18 19:32:52 +0530 | [diff] [blame] | 730 | doc.base_taxes_and_charges_added -= base_gst_tax |
| 731 | doc.base_total_taxes_and_charges -= base_gst_tax |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 732 | |
Deepesh Garg | 1c14606 | 2020-08-18 19:32:52 +0530 | [diff] [blame] | 733 | update_totals(gst_tax, base_gst_tax, doc) |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 734 | |
Deepesh Garg | 1c14606 | 2020-08-18 19:32:52 +0530 | [diff] [blame] | 735 | def update_totals(gst_tax, base_gst_tax, doc): |
| 736 | doc.base_grand_total -= base_gst_tax |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 737 | doc.grand_total -= gst_tax |
| 738 | |
| 739 | if doc.meta.get_field("rounded_total"): |
| 740 | if doc.is_rounded_total_disabled(): |
| 741 | doc.outstanding_amount = doc.grand_total |
| 742 | else: |
| 743 | doc.rounded_total = round_based_on_smallest_currency_fraction(doc.grand_total, |
| 744 | doc.currency, doc.precision("rounded_total")) |
| 745 | |
| 746 | doc.rounding_adjustment += flt(doc.rounded_total - doc.grand_total, |
| 747 | doc.precision("rounding_adjustment")) |
| 748 | |
Deepesh Garg | 1882735 | 2020-07-17 11:31:15 +0530 | [diff] [blame] | 749 | doc.outstanding_amount = doc.rounded_total or doc.grand_total |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 750 | |
| 751 | doc.in_words = money_in_words(doc.grand_total, doc.currency) |
Deepesh Garg | 1c14606 | 2020-08-18 19:32:52 +0530 | [diff] [blame] | 752 | doc.base_in_words = money_in_words(doc.base_grand_total, erpnext.get_company_currency(doc.company)) |
Deepesh Garg | 1882735 | 2020-07-17 11:31:15 +0530 | [diff] [blame] | 753 | doc.set_payment_schedule() |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 754 | |
| 755 | def make_regional_gl_entries(gl_entries, doc): |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 756 | country = frappe.get_cached_value('Company', doc.company, 'country') |
| 757 | |
| 758 | if country != 'India': |
Deepesh Garg | 8aed48f | 2020-08-19 18:30:18 +0530 | [diff] [blame] | 759 | return gl_entries |
| 760 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 761 | gst_tax, base_gst_tax = get_gst_tax_amount(doc) |
| 762 | |
| 763 | if not base_gst_tax: |
| 764 | return gl_entries |
| 765 | |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 766 | if doc.reverse_charge == 'Y': |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 767 | gst_accounts = get_gst_accounts(doc.company) |
| 768 | gst_account_list = gst_accounts.get('cgst_account') + gst_accounts.get('sgst_account') \ |
| 769 | + gst_accounts.get('igst_account') |
| 770 | |
| 771 | for tax in doc.get('taxes'): |
| 772 | if tax.category not in ("Total", "Valuation and Total"): |
| 773 | continue |
| 774 | |
Deepesh Garg | afd2dd3 | 2020-08-20 16:31:38 +0530 | [diff] [blame] | 775 | dr_or_cr = "credit" if tax.add_deduct_tax == "Add" else "debit" |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 776 | if flt(tax.base_tax_amount_after_discount_amount) and tax.account_head in gst_account_list: |
| 777 | account_currency = get_account_currency(tax.account_head) |
| 778 | |
| 779 | gl_entries.append(doc.get_gl_dict( |
| 780 | { |
| 781 | "account": tax.account_head, |
| 782 | "cost_center": tax.cost_center, |
| 783 | "posting_date": doc.posting_date, |
| 784 | "against": doc.supplier, |
Deepesh Garg | afd2dd3 | 2020-08-20 16:31:38 +0530 | [diff] [blame] | 785 | dr_or_cr: tax.base_tax_amount_after_discount_amount, |
| 786 | dr_or_cr + "_in_account_currency": tax.base_tax_amount_after_discount_amount \ |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 787 | if account_currency==doc.company_currency \ |
| 788 | else tax.tax_amount_after_discount_amount |
| 789 | }, account_currency, item=tax) |
| 790 | ) |
| 791 | |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 792 | return gl_entries |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 793 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 794 | def get_gst_tax_amount(doc): |
| 795 | gst_accounts = get_gst_accounts(doc.company) |
| 796 | gst_account_list = gst_accounts.get('cgst_account', []) + gst_accounts.get('sgst_account', []) \ |
| 797 | + gst_accounts.get('igst_account', []) |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 798 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 799 | base_gst_tax = 0 |
| 800 | gst_tax = 0 |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 801 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 802 | for tax in doc.get('taxes'): |
| 803 | if tax.category not in ("Total", "Valuation and Total"): |
| 804 | continue |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 805 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 806 | if flt(tax.base_tax_amount_after_discount_amount) and tax.account_head in gst_account_list: |
| 807 | base_gst_tax += tax.base_tax_amount_after_discount_amount |
| 808 | gst_tax += tax.tax_amount_after_discount_amount |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 809 | |
Deepesh Garg | 93f925f | 2021-03-15 18:04:42 +0530 | [diff] [blame] | 810 | return gst_tax, base_gst_tax |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 811 | |
| 812 | @frappe.whitelist() |
| 813 | def get_regional_round_off_accounts(company, account_list): |
| 814 | country = frappe.get_cached_value('Company', company, 'country') |
| 815 | |
| 816 | if country != 'India': |
| 817 | return |
| 818 | |
| 819 | if isinstance(account_list, string_types): |
| 820 | account_list = json.loads(account_list) |
| 821 | |
| 822 | if not frappe.db.get_single_value('GST Settings', 'round_off_gst_values'): |
| 823 | return |
| 824 | |
| 825 | gst_accounts = get_gst_accounts(company) |
walstanb | 52403c5 | 2021-03-27 10:13:27 +0530 | [diff] [blame] | 826 | |
| 827 | gst_account_list = [] |
| 828 | for account in ['cgst_account', 'sgst_account', 'igst_account']: |
walstanb | ab673d9 | 2021-03-27 12:52:23 +0530 | [diff] [blame] | 829 | if account in gst_accounts: |
walstanb | 52403c5 | 2021-03-27 10:13:27 +0530 | [diff] [blame] | 830 | gst_account_list += gst_accounts.get(account) |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 831 | |
| 832 | account_list.extend(gst_account_list) |
| 833 | |
| 834 | return account_list |