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