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