Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import json |
| 2 | import re |
| 3 | |
| 4 | import frappe |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 5 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | from frappe.model.utils import get_fetch_values |
Rucha Mahabal | ab8df98 | 2022-06-10 18:22:07 +0530 | [diff] [blame] | 7 | from frappe.utils import cint, cstr, date_diff, flt, getdate |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 9 | from erpnext.controllers.accounts_controller import get_taxes_and_charges |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | from erpnext.regional.india import number_state_mapping, state_numbers, states |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 12 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | GST_INVOICE_NUMBER_FORMAT = re.compile(r"^[a-zA-Z0-9\-/]+$") # alphanumeric and - / |
| 14 | GSTIN_FORMAT = re.compile( |
| 15 | "^[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}$" |
| 16 | ) |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 17 | GSTIN_UIN_FORMAT = re.compile("^[0-9]{4}[A-Z]{3}[0-9]{5}[0-9A-Z]{3}") |
| 18 | PAN_NUMBER_FORMAT = re.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}") |
| 19 | |
| 20 | |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 21 | def validate_gstin_for_india(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | if hasattr(doc, "gst_state"): |
Deepesh Garg | 363e676 | 2022-03-21 16:28:17 +0530 | [diff] [blame] | 23 | set_gst_state_and_state_number(doc) |
| 24 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | if not hasattr(doc, "gstin") or not doc.gstin: |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 26 | return |
| 27 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 28 | gst_category = [] |
| 29 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | if hasattr(doc, "gst_category"): |
Anuja Pawar | 59c31bb | 2021-10-22 19:26:31 +0530 | [diff] [blame] | 31 | if len(doc.links): |
| 32 | link_doctype = doc.links[0].get("link_doctype") |
| 33 | link_name = doc.links[0].get("link_name") |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 34 | |
Anuja Pawar | 59c31bb | 2021-10-22 19:26:31 +0530 | [diff] [blame] | 35 | if link_doctype in ["Customer", "Supplier"]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 36 | gst_category = frappe.db.get_value(link_doctype, {"name": link_name}, ["gst_category"]) |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 37 | |
Sagar Vora | d75095b | 2019-01-23 14:40:01 +0530 | [diff] [blame] | 38 | doc.gstin = doc.gstin.upper().strip() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 39 | if not doc.gstin or doc.gstin == "NA": |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 40 | return |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 41 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 42 | if len(doc.gstin) != 15: |
Saqib | 9320316 | 2021-04-12 17:55:46 +0530 | [diff] [blame] | 43 | frappe.throw(_("A GSTIN must have 15 characters."), title=_("Invalid GSTIN")) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 44 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 45 | if gst_category and gst_category == "UIN Holders": |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 46 | if not GSTIN_UIN_FORMAT.match(doc.gstin): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | frappe.throw( |
| 48 | _( |
| 49 | "The input you've entered doesn't match the GSTIN format for UIN Holders or Non-Resident OIDAR Service Providers" |
| 50 | ), |
| 51 | title=_("Invalid GSTIN"), |
| 52 | ) |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 53 | else: |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 54 | if not GSTIN_FORMAT.match(doc.gstin): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | frappe.throw( |
| 56 | _("The input you've entered doesn't match the format of GSTIN."), title=_("Invalid GSTIN") |
| 57 | ) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 58 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 59 | validate_gstin_check_digit(doc.gstin) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 60 | |
Anurag Mishra | 1e396dc | 2021-01-13 14:01:57 +0530 | [diff] [blame] | 61 | if not doc.gst_state: |
Saqib | 9320316 | 2021-04-12 17:55:46 +0530 | [diff] [blame] | 62 | frappe.throw(_("Please enter GST state"), title=_("Invalid State")) |
Anurag Mishra | 1e396dc | 2021-01-13 14:01:57 +0530 | [diff] [blame] | 63 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 64 | if doc.gst_state_number != doc.gstin[:2]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 65 | frappe.throw( |
| 66 | _("First 2 digits of GSTIN should match with State number {0}.").format(doc.gst_state_number), |
| 67 | title=_("Invalid GSTIN"), |
| 68 | ) |
| 69 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 70 | |
Deepesh Garg | bb8cd1c | 2021-02-22 19:28:45 +0530 | [diff] [blame] | 71 | def validate_pan_for_india(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | if doc.get("country") != "India" or not doc.get("pan"): |
Deepesh Garg | bb8cd1c | 2021-02-22 19:28:45 +0530 | [diff] [blame] | 73 | return |
| 74 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 75 | if not PAN_NUMBER_FORMAT.match(doc.pan): |
Deepesh Garg | bb8cd1c | 2021-02-22 19:28:45 +0530 | [diff] [blame] | 76 | frappe.throw(_("Invalid PAN No. The input you've entered doesn't match the format of PAN.")) |
| 77 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 78 | |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 79 | def validate_tax_category(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | if doc.get("gst_state") and frappe.db.get_value( |
| 81 | "Tax Category", |
| 82 | { |
| 83 | "gst_state": doc.gst_state, |
| 84 | "is_inter_state": doc.is_inter_state, |
| 85 | "is_reverse_charge": doc.is_reverse_charge, |
| 86 | }, |
| 87 | ): |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 88 | if doc.is_inter_state: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | frappe.throw( |
| 90 | _("Inter State tax category for GST State {0} already exists").format(doc.gst_state) |
| 91 | ) |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 92 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | frappe.throw( |
| 94 | _("Intra State tax category for GST State {0} already exists").format(doc.gst_state) |
| 95 | ) |
| 96 | |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 97 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 98 | def update_gst_category(doc, method): |
Deepesh Garg | a38aca5 | 2021-11-19 11:03:13 +0530 | [diff] [blame] | 99 | for link in doc.links: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 100 | if link.link_doctype in ["Customer", "Supplier"]: |
Deepesh Garg | a38aca5 | 2021-11-19 11:03:13 +0530 | [diff] [blame] | 101 | meta = frappe.get_meta(link.link_doctype) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 102 | if doc.get("gstin") and meta.has_field("gst_category"): |
| 103 | frappe.db.set_value( |
| 104 | link.link_doctype, |
| 105 | {"name": link.link_name, "gst_category": "Unregistered"}, |
| 106 | "gst_category", |
| 107 | "Registered Regular", |
| 108 | ) |
| 109 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 110 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 111 | def set_gst_state_and_state_number(doc): |
Deepesh Garg | 363e676 | 2022-03-21 16:28:17 +0530 | [diff] [blame] | 112 | if not doc.gst_state and doc.state: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 113 | state = doc.state.lower() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | states_lowercase = {s.lower(): s for s in states} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 115 | if state in states_lowercase: |
| 116 | doc.gst_state = states_lowercase[state] |
| 117 | else: |
| 118 | return |
Deepesh Garg | 363e676 | 2022-03-21 16:28:17 +0530 | [diff] [blame] | 119 | doc.gst_state_number = state_numbers.get(doc.gst_state) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 120 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 121 | |
| 122 | def validate_gstin_check_digit(gstin, label="GSTIN"): |
| 123 | """Function to validate the check digit of the GSTIN.""" |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 124 | factor = 1 |
| 125 | total = 0 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 126 | code_point_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 127 | mod = len(code_point_chars) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 128 | input_chars = gstin[:-1] |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 129 | for char in input_chars: |
| 130 | digit = factor * code_point_chars.find(char) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 131 | digit = (digit // mod) + (digit % mod) |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 132 | total += digit |
| 133 | factor = 2 if factor == 1 else 1 |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 134 | if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 135 | frappe.throw( |
| 136 | _( |
| 137 | """Invalid {0}! The check digit validation has failed. Please ensure you've typed the {0} correctly.""" |
| 138 | ).format(label) |
| 139 | ) |
| 140 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 141 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 142 | def get_itemised_tax_breakup_header(item_doctype, tax_accounts): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 143 | hsn_wise_in_gst_settings = frappe.db.get_single_value("GST Settings", "hsn_wise_tax_breakup") |
| 144 | if frappe.get_meta(item_doctype).has_field("gst_hsn_code") and hsn_wise_in_gst_settings: |
Subin Tom | 530de12 | 2021-10-11 17:33:41 +0530 | [diff] [blame] | 145 | return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts |
| 146 | else: |
| 147 | return [_("Item"), _("Taxable Amount")] + tax_accounts |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 148 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 149 | |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 150 | def get_itemised_tax_breakup_data(doc, account_wise=False, hsn_wise=False): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 151 | itemised_tax = get_itemised_tax(doc.taxes, with_tax_account=account_wise) |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 152 | |
| 153 | itemised_taxable_amount = get_itemised_taxable_amount(doc.items) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 154 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 155 | if not frappe.get_meta(doc.doctype + " Item").has_field("gst_hsn_code"): |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 156 | return itemised_tax, itemised_taxable_amount |
| 157 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 158 | hsn_wise_in_gst_settings = frappe.db.get_single_value("GST Settings", "hsn_wise_tax_breakup") |
Subin Tom | 530de12 | 2021-10-11 17:33:41 +0530 | [diff] [blame] | 159 | |
| 160 | tax_breakup_hsn_wise = hsn_wise or hsn_wise_in_gst_settings |
| 161 | if tax_breakup_hsn_wise: |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 162 | item_hsn_map = frappe._dict() |
| 163 | for d in doc.items: |
| 164 | item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code")) |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 165 | |
| 166 | hsn_tax = {} |
| 167 | for item, taxes in itemised_tax.items(): |
Subin Tom | 530de12 | 2021-10-11 17:33:41 +0530 | [diff] [blame] | 168 | item_or_hsn = item if not tax_breakup_hsn_wise else item_hsn_map.get(item) |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 169 | hsn_tax.setdefault(item_or_hsn, frappe._dict()) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 170 | for tax_desc, tax_detail in taxes.items(): |
| 171 | key = tax_desc |
| 172 | if account_wise: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 173 | key = tax_detail.get("tax_account") |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 174 | hsn_tax[item_or_hsn].setdefault(key, {"tax_rate": 0, "tax_amount": 0}) |
| 175 | hsn_tax[item_or_hsn][key]["tax_rate"] = tax_detail.get("tax_rate") |
| 176 | hsn_tax[item_or_hsn][key]["tax_amount"] += tax_detail.get("tax_amount") |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 177 | |
| 178 | # set taxable amount |
| 179 | hsn_taxable_amount = frappe._dict() |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 180 | for item in itemised_taxable_amount: |
Subin Tom | 530de12 | 2021-10-11 17:33:41 +0530 | [diff] [blame] | 181 | item_or_hsn = item if not tax_breakup_hsn_wise else item_hsn_map.get(item) |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 182 | hsn_taxable_amount.setdefault(item_or_hsn, 0) |
| 183 | hsn_taxable_amount[item_or_hsn] += itemised_taxable_amount.get(item) |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 184 | |
| 185 | return hsn_tax, hsn_taxable_amount |
| 186 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 187 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 188 | def set_place_of_supply(doc, method=None): |
| 189 | doc.place_of_supply = get_place_of_supply(doc, doc.doctype) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 190 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 191 | |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 192 | def validate_document_name(doc, method=None): |
| 193 | """Validate GST invoice number requirements.""" |
Nabin Hait | 10c6137 | 2021-04-13 15:46:01 +0530 | [diff] [blame] | 194 | |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 195 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 196 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 197 | # 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] | 198 | if country != "India" or getdate(doc.posting_date) < getdate("2021-04-01"): |
| 199 | return |
| 200 | |
| 201 | if len(doc.name) > 16: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 202 | frappe.throw( |
| 203 | _( |
| 204 | "Maximum length of document number should be 16 characters as per GST rules. Please change the naming series." |
| 205 | ) |
| 206 | ) |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 207 | |
Ankush Menat | 7c4c42a | 2021-03-03 14:56:19 +0530 | [diff] [blame] | 208 | if not GST_INVOICE_NUMBER_FORMAT.match(doc.name): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 209 | frappe.throw( |
| 210 | _( |
| 211 | "Document name should only contain alphanumeric values, dash(-) and slash(/) characters as per GST rules. Please change the naming series." |
| 212 | ) |
| 213 | ) |
| 214 | |
Ankush Menat | a44df63 | 2021-03-01 17:12:53 +0530 | [diff] [blame] | 215 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 216 | # don't remove this function it is used in tests |
| 217 | def test_method(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 218 | """test function""" |
| 219 | return "overridden" |
| 220 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 221 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 222 | def get_place_of_supply(party_details, doctype): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 223 | if not frappe.get_meta("Address").has_field("gst_state"): |
| 224 | return |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 225 | |
Deepesh Garg | 03952f8 | 2022-03-23 18:47:58 +0530 | [diff] [blame] | 226 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order", "Quotation"): |
Deepesh Garg | eacfd79 | 2020-10-30 22:12:24 +0530 | [diff] [blame] | 227 | address_name = party_details.customer_address or party_details.shipping_address_name |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 228 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 229 | address_name = party_details.shipping_address or party_details.supplier_address |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 230 | |
| 231 | if address_name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 232 | address = frappe.db.get_value( |
| 233 | "Address", address_name, ["gst_state", "gst_state_number", "gstin"], as_dict=1 |
| 234 | ) |
Rohit Waghchaure | b6a735e | 2018-10-11 10:40:34 +0530 | [diff] [blame] | 235 | if address and address.gst_state and address.gst_state_number: |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 236 | party_details.gstin = address.gstin |
Nabin Hait | 2390da6 | 2018-08-30 16:16:35 +0530 | [diff] [blame] | 237 | return cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 238 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 239 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 240 | @frappe.whitelist() |
pateljannat | 1d5d863 | 2020-11-19 20:11:45 +0530 | [diff] [blame] | 241 | def get_regional_address_details(party_details, doctype, company): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 242 | if isinstance(party_details, str): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 243 | party_details = json.loads(party_details) |
| 244 | party_details = frappe._dict(party_details) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 245 | |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 246 | update_party_details(party_details, doctype) |
| 247 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 248 | party_details.place_of_supply = get_place_of_supply(party_details, doctype) |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 249 | |
| 250 | if is_internal_transfer(party_details, doctype): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 251 | party_details.taxes_and_charges = "" |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 252 | party_details.taxes = [] |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 253 | return party_details |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 254 | |
Deepesh Garg | 03952f8 | 2022-03-23 18:47:58 +0530 | [diff] [blame] | 255 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order", "Quotation"): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 256 | master_doctype = "Sales Taxes and Charges Template" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 257 | tax_template_by_category = get_tax_template_based_on_category( |
| 258 | master_doctype, company, party_details |
| 259 | ) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 260 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 261 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 262 | master_doctype = "Purchase Taxes and Charges Template" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 263 | tax_template_by_category = get_tax_template_based_on_category( |
| 264 | master_doctype, company, party_details |
| 265 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 266 | |
Deepesh Garg | 35e2bd8 | 2021-12-02 17:17:56 +0530 | [diff] [blame] | 267 | if tax_template_by_category: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 268 | party_details["taxes_and_charges"] = tax_template_by_category |
Deepesh Garg | 532961f | 2022-03-30 19:33:44 +0530 | [diff] [blame] | 269 | party_details["taxes"] = get_taxes_and_charges(master_doctype, tax_template_by_category) |
Deepesh Garg | 466e549 | 2022-01-02 17:53:15 +0530 | [diff] [blame] | 270 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 271 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 272 | if not party_details.place_of_supply: |
| 273 | return party_details |
| 274 | if not party_details.company_gstin: |
| 275 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 276 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 277 | if ( |
| 278 | doctype in ("Sales Invoice", "Delivery Note", "Sales Order") |
| 279 | and party_details.company_gstin |
| 280 | and party_details.company_gstin[:2] != party_details.place_of_supply[:2] |
| 281 | ) or ( |
| 282 | doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt") |
| 283 | and party_details.supplier_gstin |
| 284 | and party_details.supplier_gstin[:2] != party_details.place_of_supply[:2] |
| 285 | ): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 286 | 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] | 287 | else: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 288 | 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] | 289 | |
| 290 | if not default_tax: |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 291 | return party_details |
Deepesh Garg | 35e2bd8 | 2021-12-02 17:17:56 +0530 | [diff] [blame] | 292 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 293 | party_details["taxes_and_charges"] = default_tax |
Deepesh Garg | 532961f | 2022-03-30 19:33:44 +0530 | [diff] [blame] | 294 | party_details["taxes"] = get_taxes_and_charges(master_doctype, default_tax) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 295 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 296 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 297 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 298 | |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 299 | def update_party_details(party_details, doctype): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 300 | for address_field in [ |
| 301 | "shipping_address", |
| 302 | "company_address", |
| 303 | "supplier_address", |
| 304 | "shipping_address_name", |
| 305 | "customer_address", |
| 306 | ]: |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 307 | if party_details.get(address_field): |
| 308 | party_details.update(get_fetch_values(doctype, address_field, party_details.get(address_field))) |
| 309 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 310 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 311 | def is_internal_transfer(party_details, doctype): |
Deepesh Garg | 03952f8 | 2022-03-23 18:47:58 +0530 | [diff] [blame] | 312 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order", "Quotation"): |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 313 | destination_gstin = party_details.company_gstin |
| 314 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 315 | destination_gstin = party_details.supplier_gstin |
| 316 | |
Deepesh Garg | da47fe2 | 2021-09-30 13:28:53 +0530 | [diff] [blame] | 317 | if not destination_gstin or party_details.gstin: |
| 318 | return False |
| 319 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 320 | if party_details.gstin == destination_gstin: |
| 321 | return True |
| 322 | else: |
| 323 | False |
| 324 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 325 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 326 | def get_tax_template_based_on_category(master_doctype, company, party_details): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 327 | if not party_details.get("tax_category"): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 328 | return |
| 329 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 330 | default_tax = frappe.db.get_value( |
| 331 | master_doctype, {"company": company, "tax_category": party_details.get("tax_category")}, "name" |
| 332 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 333 | |
Deepesh Garg | 35e2bd8 | 2021-12-02 17:17:56 +0530 | [diff] [blame] | 334 | return default_tax |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 335 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 336 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 337 | def get_tax_template(master_doctype, company, is_inter_state, state_code): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 338 | tax_categories = frappe.get_all( |
| 339 | "Tax Category", |
| 340 | fields=["name", "is_inter_state", "gst_state"], |
Deepesh Garg | 9a6a181 | 2022-04-01 14:46:26 +0530 | [diff] [blame] | 341 | filters={"is_inter_state": is_inter_state, "is_reverse_charge": 0, "disabled": 0}, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 342 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 343 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 344 | default_tax = "" |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 345 | |
| 346 | for tax_category in tax_categories: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 347 | if tax_category.gst_state == number_state_mapping[state_code] or ( |
| 348 | not default_tax and not tax_category.gst_state |
| 349 | ): |
| 350 | default_tax = frappe.db.get_value( |
| 351 | master_doctype, {"company": company, "disabled": 0, "tax_category": tax_category.name}, "name" |
| 352 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 353 | return default_tax |
| 354 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 355 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 356 | def get_ewb_data(dt, dn): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 357 | |
| 358 | ewaybills = [] |
| 359 | for doc_name in dn: |
| 360 | doc = frappe.get_doc(dt, doc_name) |
| 361 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 362 | validate_doc(doc) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 363 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 364 | data = frappe._dict( |
| 365 | { |
| 366 | "transporterId": "", |
| 367 | "TotNonAdvolVal": 0, |
| 368 | } |
| 369 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 370 | |
| 371 | data.userGstin = data.fromGstin = doc.company_gstin |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 372 | data.supplyType = "O" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 373 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 374 | if dt == "Delivery Note": |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 375 | data.subSupplyType = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 376 | elif doc.gst_category in ["Registered Regular", "SEZ"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 377 | data.subSupplyType = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 378 | elif doc.gst_category in ["Overseas", "Deemed Export"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 379 | data.subSupplyType = 3 |
| 380 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 381 | frappe.throw(_("Unsupported GST Category for E-Way Bill JSON generation")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 382 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 383 | data.docType = "INV" |
| 384 | data.docDate = frappe.utils.formatdate(doc.posting_date, "dd/mm/yyyy") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 385 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 386 | company_address = frappe.get_doc("Address", doc.company_address) |
| 387 | billing_address = frappe.get_doc("Address", doc.customer_address) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 388 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 389 | # added dispatch address |
| 390 | dispatch_address = ( |
| 391 | frappe.get_doc("Address", doc.dispatch_address_name) |
| 392 | if doc.dispatch_address_name |
| 393 | else company_address |
| 394 | ) |
| 395 | shipping_address = frappe.get_doc("Address", doc.shipping_address_name) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 396 | |
Subin Tom | 5265ba3 | 2021-07-13 14:58:17 +0530 | [diff] [blame] | 397 | data = get_address_details(data, doc, company_address, billing_address, dispatch_address) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 398 | |
| 399 | data.itemList = [] |
Smit Vora | e2d866d | 2021-11-01 15:55:19 +0530 | [diff] [blame] | 400 | data.totalValue = doc.net_total |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 401 | |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 402 | data = get_item_list(data, doc, hsn_wise=True) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 403 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 404 | disable_rounded = frappe.db.get_single_value("Global Defaults", "disable_rounded_total") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 405 | data.totInvValue = doc.grand_total if disable_rounded else doc.rounded_total |
| 406 | |
| 407 | data = get_transport_details(data, doc) |
| 408 | |
| 409 | fields = { |
| 410 | "/. -": { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 411 | "docNo": doc.name, |
| 412 | "fromTrdName": doc.company, |
| 413 | "toTrdName": doc.customer_name, |
| 414 | "transDocNo": doc.lr_no, |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 415 | }, |
| 416 | "@#/,&. -": { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 417 | "fromAddr1": company_address.address_line1, |
| 418 | "fromAddr2": company_address.address_line2, |
| 419 | "fromPlace": company_address.city, |
| 420 | "toAddr1": shipping_address.address_line1, |
| 421 | "toAddr2": shipping_address.address_line2, |
| 422 | "toPlace": shipping_address.city, |
| 423 | "transporterName": doc.transporter_name, |
| 424 | }, |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | for allowed_chars, field_map in fields.items(): |
| 428 | for key, value in field_map.items(): |
| 429 | if not value: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 430 | data[key] = "" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 431 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 432 | data[key] = re.sub(r"[^\w" + allowed_chars + "]", "", value) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 433 | |
| 434 | ewaybills.append(data) |
| 435 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 436 | data = {"version": "1.0.0421", "billLists": ewaybills} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 437 | |
| 438 | return data |
| 439 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 440 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 441 | @frappe.whitelist() |
| 442 | def generate_ewb_json(dt, dn): |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 443 | dn = json.loads(dn) |
| 444 | return get_ewb_data(dt, dn) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 445 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 446 | |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 447 | @frappe.whitelist() |
| 448 | def download_ewb_json(): |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 449 | data = json.loads(frappe.local.form_dict.data) |
| 450 | frappe.local.response.filecontent = json.dumps(data, indent=4, sort_keys=True) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 451 | frappe.local.response.type = "download" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 452 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 453 | filename_prefix = "Bulk" |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 454 | docname = frappe.local.form_dict.docname |
| 455 | if docname: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 456 | if docname.startswith("["): |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 457 | docname = json.loads(docname) |
| 458 | if len(docname) == 1: |
| 459 | docname = docname[0] |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 460 | |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 461 | if not isinstance(docname, list): |
| 462 | # removes characters not allowed in a filename (https://stackoverflow.com/a/38766141/4767738) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 463 | filename_prefix = re.sub(r"[^\w_.)( -]", "", docname) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 464 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 465 | frappe.local.response.filename = "{0}_e-WayBill_Data_{1}.json".format( |
| 466 | filename_prefix, frappe.utils.random_string(5) |
| 467 | ) |
| 468 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 469 | |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 470 | @frappe.whitelist() |
| 471 | def get_gstins_for_company(company): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 472 | company_gstins = [] |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 473 | if company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 474 | company_gstins = frappe.db.sql( |
| 475 | """select |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 476 | distinct `tabAddress`.gstin |
| 477 | from |
| 478 | `tabAddress`, `tabDynamic Link` |
| 479 | where |
| 480 | `tabDynamic Link`.parent = `tabAddress`.name and |
| 481 | `tabDynamic Link`.parenttype = 'Address' and |
| 482 | `tabDynamic Link`.link_doctype = 'Company' and |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 483 | `tabDynamic Link`.link_name = %(company)s""", |
| 484 | {"company": company}, |
| 485 | ) |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 486 | return company_gstins |
| 487 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 488 | |
Subin Tom | 5265ba3 | 2021-07-13 14:58:17 +0530 | [diff] [blame] | 489 | def get_address_details(data, doc, company_address, billing_address, dispatch_address): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 490 | data.fromPincode = validate_pincode(company_address.pincode, "Company Address") |
| 491 | data.fromStateCode = validate_state_code(company_address.gst_state_number, "Company Address") |
| 492 | data.actualFromStateCode = validate_state_code( |
| 493 | dispatch_address.gst_state_number, "Dispatch Address" |
| 494 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 495 | |
| 496 | if not doc.billing_address_gstin or len(doc.billing_address_gstin) < 15: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 497 | data.toGstin = "URP" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 498 | set_gst_state_and_state_number(billing_address) |
| 499 | else: |
| 500 | data.toGstin = doc.billing_address_gstin |
| 501 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 502 | data.toPincode = validate_pincode(billing_address.pincode, "Customer Address") |
| 503 | data.toStateCode = validate_state_code(billing_address.gst_state_number, "Customer Address") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 504 | |
| 505 | if doc.customer_address != doc.shipping_address_name: |
| 506 | data.transType = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 507 | shipping_address = frappe.get_doc("Address", doc.shipping_address_name) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 508 | set_gst_state_and_state_number(shipping_address) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 509 | data.toPincode = validate_pincode(shipping_address.pincode, "Shipping Address") |
| 510 | data.actualToStateCode = validate_state_code( |
| 511 | shipping_address.gst_state_number, "Shipping Address" |
| 512 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 513 | else: |
| 514 | data.transType = 1 |
| 515 | data.actualToStateCode = data.toStateCode |
| 516 | shipping_address = billing_address |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 517 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 518 | if doc.gst_category == "SEZ": |
Smit Vora | bbe4933 | 2020-11-18 20:58:59 +0530 | [diff] [blame] | 519 | data.toStateCode = 99 |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 520 | |
| 521 | return data |
| 522 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 523 | |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 524 | def get_item_list(data, doc, hsn_wise=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 525 | for attr in ["cgstValue", "sgstValue", "igstValue", "cessValue", "OthValue"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 526 | data[attr] = 0 |
| 527 | |
| 528 | gst_accounts = get_gst_accounts(doc.company, account_wise=True) |
| 529 | tax_map = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 530 | "sgst_account": ["sgstRate", "sgstValue"], |
| 531 | "cgst_account": ["cgstRate", "cgstValue"], |
| 532 | "igst_account": ["igstRate", "igstValue"], |
| 533 | "cess_account": ["cessRate", "cessValue"], |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 534 | } |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 535 | item_data_attrs = ["sgstRate", "cgstRate", "igstRate", "cessRate", "cessNonAdvol"] |
| 536 | hsn_wise_charges, hsn_taxable_amount = get_itemised_tax_breakup_data( |
| 537 | doc, account_wise=True, hsn_wise=hsn_wise |
| 538 | ) |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 539 | for item_or_hsn, taxable_amount in hsn_taxable_amount.items(): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 540 | item_data = frappe._dict() |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 541 | if not item_or_hsn: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 542 | frappe.throw(_("GST HSN Code does not exist for one or more items")) |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 543 | item_data.hsnCode = int(item_or_hsn) if hsn_wise else item_or_hsn |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 544 | item_data.taxableAmount = taxable_amount |
| 545 | item_data.qtyUnit = "" |
| 546 | for attr in item_data_attrs: |
| 547 | item_data[attr] = 0 |
| 548 | |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 549 | for account, tax_detail in hsn_wise_charges.get(item_or_hsn, {}).items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 550 | account_type = gst_accounts.get(account, "") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 551 | for tax_acc, attrs in tax_map.items(): |
| 552 | if account_type == tax_acc: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 553 | item_data[attrs[0]] = tax_detail.get("tax_rate") |
| 554 | data[attrs[1]] += tax_detail.get("tax_amount") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 555 | break |
| 556 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 557 | data.OthValue += tax_detail.get("tax_amount") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 558 | |
| 559 | data.itemList.append(item_data) |
| 560 | |
| 561 | # Tax amounts rounded to 2 decimals to avoid exceeding max character limit |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 562 | for attr in ["sgstValue", "cgstValue", "igstValue", "cessValue"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 563 | data[attr] = flt(data[attr], 2) |
| 564 | |
| 565 | return data |
| 566 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 567 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 568 | def validate_doc(doc): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 569 | if doc.docstatus != 1: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 570 | 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] | 571 | |
| 572 | if doc.is_return: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 573 | 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] | 574 | |
| 575 | if doc.ewaybill: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 576 | frappe.throw(_("e-Way Bill already exists for this document")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 577 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 578 | reqd_fields = [ |
| 579 | "company_gstin", |
| 580 | "company_address", |
| 581 | "customer_address", |
| 582 | "shipping_address_name", |
| 583 | "mode_of_transport", |
| 584 | "distance", |
| 585 | ] |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 586 | |
| 587 | for fieldname in reqd_fields: |
| 588 | if not doc.get(fieldname): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 589 | frappe.throw( |
| 590 | _("{} is required to generate E-Way Bill JSON").format(doc.meta.get_label(fieldname)) |
| 591 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 592 | |
| 593 | if len(doc.company_gstin) < 15: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 594 | frappe.throw(_("You must be a registered supplier to generate e-Way Bill")) |
| 595 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 596 | |
| 597 | def get_transport_details(data, doc): |
| 598 | if doc.distance > 4000: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 599 | frappe.throw(_("Distance cannot be greater than 4000 kms")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 600 | |
| 601 | data.transDistance = int(round(doc.distance)) |
| 602 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 603 | transport_modes = {"Road": 1, "Rail": 2, "Air": 3, "Ship": 4} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 604 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 605 | vehicle_types = {"Regular": "R", "Over Dimensional Cargo (ODC)": "O"} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 606 | |
| 607 | data.transMode = transport_modes.get(doc.mode_of_transport) |
| 608 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 609 | if doc.mode_of_transport == "Road": |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 610 | if not doc.gst_transporter_id and not doc.vehicle_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 611 | frappe.throw( |
| 612 | _("Either GST Transporter ID or Vehicle No is required if Mode of Transport is Road") |
| 613 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 614 | if doc.vehicle_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 615 | data.vehicleNo = doc.vehicle_no.replace(" ", "") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 616 | if not doc.gst_vehicle_type: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 617 | frappe.throw(_("Vehicle Type is required if Mode of Transport is Road")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 618 | else: |
| 619 | data.vehicleType = vehicle_types.get(doc.gst_vehicle_type) |
| 620 | else: |
| 621 | if not doc.lr_no or not doc.lr_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 622 | frappe.throw(_("Transport Receipt No and Date are mandatory for your chosen Mode of Transport")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 623 | |
| 624 | if doc.lr_no: |
| 625 | data.transDocNo = doc.lr_no |
| 626 | |
| 627 | if doc.lr_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 628 | data.transDocDate = frappe.utils.formatdate(doc.lr_date, "dd/mm/yyyy") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 629 | |
| 630 | if doc.gst_transporter_id: |
karthikeyan5 | ca46bed | 2020-05-30 15:00:56 +0530 | [diff] [blame] | 631 | if doc.gst_transporter_id[0:2] != "88": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 632 | validate_gstin_check_digit(doc.gst_transporter_id, label="GST Transporter ID") |
karthikeyan5 | ca46bed | 2020-05-30 15:00:56 +0530 | [diff] [blame] | 633 | data.transporterId = doc.gst_transporter_id |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 634 | |
| 635 | return data |
| 636 | |
| 637 | |
| 638 | def validate_pincode(pincode, address): |
| 639 | pin_not_found = "Pin Code doesn't exist for {}" |
| 640 | incorrect_pin = "Pin Code for {} is incorrecty formatted. It must be 6 digits (without spaces)" |
| 641 | |
| 642 | if not pincode: |
| 643 | frappe.throw(_(pin_not_found.format(address))) |
| 644 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 645 | pincode = pincode.replace(" ", "") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 646 | if not pincode.isdigit() or len(pincode) != 6: |
| 647 | frappe.throw(_(incorrect_pin.format(address))) |
| 648 | else: |
| 649 | return int(pincode) |
| 650 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 651 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 652 | def validate_state_code(state_code, address): |
| 653 | no_state_code = "GST State Code not found for {0}. Please set GST State in {0}" |
| 654 | if not state_code: |
| 655 | frappe.throw(_(no_state_code.format(address))) |
| 656 | else: |
| 657 | return int(state_code) |
| 658 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 659 | |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 660 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 661 | def get_gst_accounts( |
| 662 | company=None, account_wise=False, only_reverse_charge=0, only_non_reverse_charge=0 |
| 663 | ): |
| 664 | filters = {"parent": "GST Settings"} |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 665 | |
| 666 | if company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 667 | filters.update({"company": company}) |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 668 | if only_reverse_charge: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 669 | filters.update({"is_reverse_charge_account": 1}) |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 670 | elif only_non_reverse_charge: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 671 | filters.update({"is_reverse_charge_account": 0}) |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 672 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 673 | gst_accounts = frappe._dict() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 674 | gst_settings_accounts = frappe.get_all( |
| 675 | "GST Account", |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 676 | filters=filters, |
Saqib Ansari | 45fca6b | 2022-04-07 13:09:05 +0530 | [diff] [blame] | 677 | fields=["cgst_account", "sgst_account", "igst_account", "cess_account", "utgst_account"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 678 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 679 | |
Deepesh Garg | 4427390 | 2021-05-20 17:19:24 +0530 | [diff] [blame] | 680 | if not gst_settings_accounts and not frappe.flags.in_test and not frappe.flags.in_migrate: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 681 | frappe.throw(_("Please set GST Accounts in GST Settings")) |
| 682 | |
| 683 | for d in gst_settings_accounts: |
| 684 | for acc, val in d.items(): |
| 685 | if not account_wise: |
| 686 | gst_accounts.setdefault(acc, []).append(val) |
| 687 | elif val: |
| 688 | gst_accounts[val] = acc |
| 689 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 690 | return gst_accounts |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 691 | |
Deepesh Garg | 52c319c | 2020-07-15 23:57:03 +0530 | [diff] [blame] | 692 | |
Deepesh Garg | 4817521 | 2022-05-13 18:08:32 +0530 | [diff] [blame] | 693 | def validate_sez_and_export_invoices(doc, method): |
| 694 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 695 | |
| 696 | if country != "India": |
| 697 | return |
| 698 | |
| 699 | if ( |
| 700 | doc.get("gst_category") in ("SEZ", "Overseas") |
| 701 | and doc.get("export_type") == "Without Payment of Tax" |
| 702 | ): |
| 703 | gst_accounts = get_gst_accounts(doc.company) |
| 704 | |
| 705 | for tax in doc.get("taxes"): |
| 706 | for tax in doc.get("taxes"): |
| 707 | if ( |
| 708 | tax.account_head |
| 709 | in gst_accounts.get("igst_account", []) |
| 710 | + gst_accounts.get("sgst_account", []) |
| 711 | + gst_accounts.get("cgst_account", []) |
| 712 | and tax.tax_amount_after_discount_amount |
| 713 | ): |
| 714 | frappe.throw(_("GST cannot be applied on SEZ or Export invoices without payment of tax")) |
| 715 | |
| 716 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 717 | def validate_reverse_charge_transaction(doc, method): |
| 718 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 719 | |
| 720 | if country != "India": |
Deepesh Garg | 52c319c | 2020-07-15 23:57:03 +0530 | [diff] [blame] | 721 | return |
| 722 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 723 | base_gst_tax = 0 |
| 724 | base_reverse_charge_booked = 0 |
Deepesh Garg | 8aed48f | 2020-08-19 18:30:18 +0530 | [diff] [blame] | 725 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 726 | if doc.reverse_charge == "Y": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 727 | gst_accounts = get_gst_accounts(doc.company, only_reverse_charge=1) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 728 | reverse_charge_accounts = ( |
| 729 | gst_accounts.get("cgst_account") |
| 730 | + gst_accounts.get("sgst_account") |
| 731 | + gst_accounts.get("igst_account") |
| 732 | ) |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 733 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 734 | gst_accounts = get_gst_accounts(doc.company, only_non_reverse_charge=1) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 735 | non_reverse_charge_accounts = ( |
| 736 | gst_accounts.get("cgst_account") |
| 737 | + gst_accounts.get("sgst_account") |
| 738 | + gst_accounts.get("igst_account") |
| 739 | ) |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 740 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 741 | for tax in doc.get("taxes"): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 742 | if tax.account_head in non_reverse_charge_accounts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 743 | if tax.add_deduct_tax == "Add": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 744 | base_gst_tax += tax.base_tax_amount_after_discount_amount |
| 745 | else: |
| 746 | base_gst_tax += tax.base_tax_amount_after_discount_amount |
| 747 | elif tax.account_head in reverse_charge_accounts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 748 | if tax.add_deduct_tax == "Add": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 749 | base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount |
| 750 | else: |
| 751 | base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 752 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 753 | if base_gst_tax != base_reverse_charge_booked: |
| 754 | msg = _("Booked reverse charge is not equal to applied tax amount") |
| 755 | msg += "<br>" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 756 | msg += _( |
| 757 | "Please refer {gst_document_link} to learn more about how to setup and create reverse charge invoice" |
| 758 | ).format( |
| 759 | gst_document_link='<a href="https://docs.erpnext.com/docs/user/manual/en/regional/india/gst-setup">GST Documentation</a>' |
| 760 | ) |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 761 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 762 | frappe.throw(msg) |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 763 | |
Deepesh Garg | 4817521 | 2022-05-13 18:08:32 +0530 | [diff] [blame] | 764 | doc.eligibility_for_itc = "ITC on Reverse Charge" |
| 765 | |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 766 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 767 | def update_itc_availed_fields(doc, method): |
| 768 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 769 | |
| 770 | if country != "India": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 771 | return |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 772 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 773 | # Initialize values |
| 774 | doc.itc_integrated_tax = doc.itc_state_tax = doc.itc_central_tax = doc.itc_cess_amount = 0 |
| 775 | gst_accounts = get_gst_accounts(doc.company, only_non_reverse_charge=1) |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 776 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 777 | for tax in doc.get("taxes"): |
| 778 | if tax.account_head in gst_accounts.get("igst_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 779 | doc.itc_integrated_tax += flt(tax.base_tax_amount_after_discount_amount) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 780 | if tax.account_head in gst_accounts.get("sgst_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 781 | doc.itc_state_tax += flt(tax.base_tax_amount_after_discount_amount) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 782 | if tax.account_head in gst_accounts.get("cgst_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 783 | doc.itc_central_tax += flt(tax.base_tax_amount_after_discount_amount) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 784 | if tax.account_head in gst_accounts.get("cess_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 785 | doc.itc_cess_amount += flt(tax.base_tax_amount_after_discount_amount) |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 786 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 787 | |
Deepesh Garg | 8b644d8 | 2021-07-15 15:36:54 +0530 | [diff] [blame] | 788 | def update_place_of_supply(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 789 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 790 | if country != "India": |
Deepesh Garg | 8b644d8 | 2021-07-15 15:36:54 +0530 | [diff] [blame] | 791 | return |
| 792 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 793 | address = frappe.db.get_value( |
| 794 | "Address", doc.get("customer_address"), ["gst_state", "gst_state_number"], as_dict=1 |
| 795 | ) |
Deepesh Garg | 8b644d8 | 2021-07-15 15:36:54 +0530 | [diff] [blame] | 796 | if address and address.gst_state and address.gst_state_number: |
| 797 | doc.place_of_supply = cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
| 798 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 799 | |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 800 | @frappe.whitelist() |
| 801 | def get_regional_round_off_accounts(company, account_list): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 802 | country = frappe.get_cached_value("Company", company, "country") |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 803 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 804 | if country != "India": |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 805 | return |
| 806 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 807 | if isinstance(account_list, str): |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 808 | account_list = json.loads(account_list) |
| 809 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 810 | if not frappe.db.get_single_value("GST Settings", "round_off_gst_values"): |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 811 | return |
| 812 | |
| 813 | gst_accounts = get_gst_accounts(company) |
walstanb | 52403c5 | 2021-03-27 10:13:27 +0530 | [diff] [blame] | 814 | |
| 815 | gst_account_list = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 816 | for account in ["cgst_account", "sgst_account", "igst_account"]: |
walstanb | ab673d9 | 2021-03-27 12:52:23 +0530 | [diff] [blame] | 817 | if account in gst_accounts: |
walstanb | 52403c5 | 2021-03-27 10:13:27 +0530 | [diff] [blame] | 818 | gst_account_list += gst_accounts.get(account) |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 819 | |
| 820 | account_list.extend(gst_account_list) |
| 821 | |
| 822 | return account_list |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 823 | |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 824 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 825 | def update_taxable_values(doc, method): |
| 826 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 827 | |
| 828 | if country != "India": |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 829 | return |
| 830 | |
| 831 | gst_accounts = get_gst_accounts(doc.company) |
| 832 | |
| 833 | # Only considering sgst account to avoid inflating taxable value |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 834 | gst_account_list = ( |
| 835 | gst_accounts.get("sgst_account", []) |
| 836 | + gst_accounts.get("sgst_account", []) |
| 837 | + gst_accounts.get("igst_account", []) |
| 838 | ) |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 839 | |
| 840 | additional_taxes = 0 |
| 841 | total_charges = 0 |
| 842 | item_count = 0 |
| 843 | considered_rows = [] |
| 844 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 845 | for tax in doc.get("taxes"): |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 846 | prev_row_id = cint(tax.row_id) - 1 |
| 847 | if tax.account_head in gst_account_list and prev_row_id not in considered_rows: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 848 | if tax.charge_type == "On Previous Row Amount": |
| 849 | additional_taxes += doc.get("taxes")[prev_row_id].tax_amount_after_discount_amount |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 850 | considered_rows.append(prev_row_id) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 851 | if tax.charge_type == "On Previous Row Total": |
| 852 | additional_taxes += doc.get("taxes")[prev_row_id].base_total - doc.base_net_total |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 853 | considered_rows.append(prev_row_id) |
| 854 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 855 | for item in doc.get("items"): |
Deepesh Garg | 4afda3c | 2021-06-01 13:13:04 +0530 | [diff] [blame] | 856 | proportionate_value = item.base_net_amount if doc.base_net_total else item.qty |
| 857 | total_value = doc.base_net_total if doc.base_net_total else doc.total_qty |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 858 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 859 | applicable_charges = flt( |
| 860 | flt( |
| 861 | proportionate_value * (flt(additional_taxes) / flt(total_value)), |
| 862 | item.precision("taxable_value"), |
| 863 | ) |
| 864 | ) |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 865 | item.taxable_value = applicable_charges + proportionate_value |
| 866 | total_charges += applicable_charges |
| 867 | item_count += 1 |
| 868 | |
| 869 | if total_charges != additional_taxes: |
| 870 | diff = additional_taxes - total_charges |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 871 | doc.get("items")[item_count - 1].taxable_value += diff |
| 872 | |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 873 | |
| 874 | def get_depreciation_amount(asset, depreciable_value, row): |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 875 | if row.depreciation_method in ("Straight Line", "Manual"): |
GangaManoj | 2b93e54 | 2021-06-19 13:45:37 +0530 | [diff] [blame] | 876 | # if the Depreciation Schedule is being prepared for the first time |
GangaManoj | da8da9f | 2021-06-19 14:00:26 +0530 | [diff] [blame] | 877 | if not asset.flags.increase_in_asset_life: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 878 | depreciation_amount = ( |
| 879 | flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life) |
| 880 | ) / flt(row.total_number_of_depreciations) |
GangaManoj | 2b93e54 | 2021-06-19 13:45:37 +0530 | [diff] [blame] | 881 | |
| 882 | # if the Depreciation Schedule is being modified after Asset Repair |
| 883 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 884 | depreciation_amount = ( |
| 885 | flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) |
| 886 | ) / (date_diff(asset.to_date, asset.available_for_use_date) / 365) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 887 | |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 888 | else: |
| 889 | rate_of_depreciation = row.rate_of_depreciation |
| 890 | # if its the first depreciation |
| 891 | if depreciable_value == asset.gross_purchase_amount: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 892 | if row.finance_book and frappe.db.get_value("Finance Book", row.finance_book, "for_income_tax"): |
Saqib | 424efd4 | 2021-09-28 18:12:02 +0530 | [diff] [blame] | 893 | # as per IT act, if the asset is purchased in the 2nd half of fiscal year, then rate is divided by 2 |
| 894 | diff = date_diff(row.depreciation_start_date, asset.available_for_use_date) |
| 895 | if diff <= 180: |
| 896 | rate_of_depreciation = rate_of_depreciation / 2 |
| 897 | frappe.msgprint( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 898 | _( |
| 899 | "As per IT Act, the rate of depreciation for the first depreciation entry is reduced by 50%." |
| 900 | ) |
| 901 | ) |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 902 | |
| 903 | depreciation_amount = flt(depreciable_value * (flt(rate_of_depreciation) / 100)) |
| 904 | |
Saqib | 3a50490 | 2021-08-03 15:57:11 +0530 | [diff] [blame] | 905 | return depreciation_amount |
| 906 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 907 | |
Saqib | 3a50490 | 2021-08-03 15:57:11 +0530 | [diff] [blame] | 908 | def set_item_tax_from_hsn_code(item): |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 909 | if not item.taxes and item.gst_hsn_code: |
Saqib | 3a50490 | 2021-08-03 15:57:11 +0530 | [diff] [blame] | 910 | hsn_doc = frappe.get_doc("GST HSN Code", item.gst_hsn_code) |
| 911 | |
| 912 | for tax in hsn_doc.taxes: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 913 | item.append( |
| 914 | "taxes", |
| 915 | { |
| 916 | "item_tax_template": tax.item_tax_template, |
| 917 | "tax_category": tax.tax_category, |
| 918 | "valid_from": tax.valid_from, |
| 919 | }, |
| 920 | ) |
| 921 | |
Deepesh Garg | 2b2572b | 2021-08-20 14:40:12 +0530 | [diff] [blame] | 922 | |
| 923 | def delete_gst_settings_for_company(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 924 | if doc.country != "India": |
Deepesh Garg | 2b2572b | 2021-08-20 14:40:12 +0530 | [diff] [blame] | 925 | return |
| 926 | |
| 927 | gst_settings = frappe.get_doc("GST Settings") |
| 928 | records_to_delete = [] |
| 929 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 930 | for d in reversed(gst_settings.get("gst_accounts")): |
Deepesh Garg | 2b2572b | 2021-08-20 14:40:12 +0530 | [diff] [blame] | 931 | if d.company == doc.name: |
| 932 | records_to_delete.append(d) |
| 933 | |
| 934 | for d in records_to_delete: |
| 935 | gst_settings.remove(d) |
| 936 | |
| 937 | gst_settings.save() |