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 | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 228 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): |
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 | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 257 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): |
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 | 466e549 | 2022-01-02 17:53:15 +0530 | [diff] [blame] | 271 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 272 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 273 | if not party_details.place_of_supply: |
| 274 | return party_details |
| 275 | if not party_details.company_gstin: |
| 276 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 277 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 278 | if ( |
| 279 | doctype in ("Sales Invoice", "Delivery Note", "Sales Order") |
| 280 | and party_details.company_gstin |
| 281 | and party_details.company_gstin[:2] != party_details.place_of_supply[:2] |
| 282 | ) or ( |
| 283 | doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt") |
| 284 | and party_details.supplier_gstin |
| 285 | and party_details.supplier_gstin[:2] != party_details.place_of_supply[:2] |
| 286 | ): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 287 | 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] | 288 | else: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 289 | 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] | 290 | |
| 291 | if not default_tax: |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 292 | return party_details |
Deepesh Garg | 35e2bd8 | 2021-12-02 17:17:56 +0530 | [diff] [blame] | 293 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 294 | party_details["taxes_and_charges"] = default_tax |
| 295 | party_details.taxes = get_taxes_and_charges(master_doctype, default_tax) |
| 296 | |
pateljannat | cd05b34 | 2020-11-19 11:37:08 +0530 | [diff] [blame] | 297 | return party_details |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 298 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 300 | def update_party_details(party_details, doctype): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 301 | for address_field in [ |
| 302 | "shipping_address", |
| 303 | "company_address", |
| 304 | "supplier_address", |
| 305 | "shipping_address_name", |
| 306 | "customer_address", |
| 307 | ]: |
Deepesh Garg | a767085 | 2020-12-04 18:07:46 +0530 | [diff] [blame] | 308 | if party_details.get(address_field): |
| 309 | party_details.update(get_fetch_values(doctype, address_field, party_details.get(address_field))) |
| 310 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 311 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 312 | def is_internal_transfer(party_details, doctype): |
| 313 | if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): |
| 314 | destination_gstin = party_details.company_gstin |
| 315 | elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): |
| 316 | destination_gstin = party_details.supplier_gstin |
| 317 | |
Deepesh Garg | da47fe2 | 2021-09-30 13:28:53 +0530 | [diff] [blame] | 318 | if not destination_gstin or party_details.gstin: |
| 319 | return False |
| 320 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 321 | if party_details.gstin == destination_gstin: |
| 322 | return True |
| 323 | else: |
| 324 | False |
| 325 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 326 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 327 | def get_tax_template_based_on_category(master_doctype, company, party_details): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 328 | if not party_details.get("tax_category"): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 329 | return |
| 330 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 331 | default_tax = frappe.db.get_value( |
| 332 | master_doctype, {"company": company, "tax_category": party_details.get("tax_category")}, "name" |
| 333 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 334 | |
Deepesh Garg | 35e2bd8 | 2021-12-02 17:17:56 +0530 | [diff] [blame] | 335 | return default_tax |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 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(master_doctype, company, is_inter_state, state_code): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 339 | tax_categories = frappe.get_all( |
| 340 | "Tax Category", |
| 341 | fields=["name", "is_inter_state", "gst_state"], |
| 342 | filters={"is_inter_state": is_inter_state, "is_reverse_charge": 0}, |
| 343 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 344 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 345 | default_tax = "" |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 346 | |
| 347 | for tax_category in tax_categories: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 348 | if tax_category.gst_state == number_state_mapping[state_code] or ( |
| 349 | not default_tax and not tax_category.gst_state |
| 350 | ): |
| 351 | default_tax = frappe.db.get_value( |
| 352 | master_doctype, {"company": company, "disabled": 0, "tax_category": tax_category.name}, "name" |
| 353 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 354 | return default_tax |
| 355 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 356 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 357 | def calculate_annual_eligible_hra_exemption(doc): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 358 | basic_component, hra_component = frappe.db.get_value( |
| 359 | "Company", doc.company, ["basic_component", "hra_component"] |
| 360 | ) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 361 | if not (basic_component and hra_component): |
| 362 | frappe.throw(_("Please mention Basic and HRA component in Company")) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 363 | annual_exemption, monthly_exemption, hra_amount = 0, 0, 0 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 364 | if hra_component and basic_component: |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 365 | assignment = get_salary_assignment(doc.employee, nowdate()) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 366 | if assignment: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 367 | hra_component_exists = frappe.db.exists( |
| 368 | "Salary Detail", |
| 369 | { |
| 370 | "parent": assignment.salary_structure, |
| 371 | "salary_component": hra_component, |
| 372 | "parentfield": "earnings", |
| 373 | "parenttype": "Salary Structure", |
| 374 | }, |
| 375 | ) |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 376 | |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 377 | if hra_component_exists: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 378 | basic_amount, hra_amount = get_component_amt_from_salary_slip( |
| 379 | doc.employee, assignment.salary_structure, basic_component, hra_component |
| 380 | ) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 381 | if hra_amount: |
| 382 | if doc.monthly_house_rent: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 383 | annual_exemption = calculate_hra_exemption( |
| 384 | assignment.salary_structure, |
| 385 | basic_amount, |
| 386 | hra_amount, |
| 387 | doc.monthly_house_rent, |
| 388 | doc.rented_in_metro_city, |
| 389 | ) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 390 | if annual_exemption > 0: |
| 391 | monthly_exemption = annual_exemption / 12 |
| 392 | else: |
| 393 | annual_exemption = 0 |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 394 | |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 395 | elif doc.docstatus == 1: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 396 | frappe.throw( |
| 397 | _("Salary Structure must be submitted before submission of Tax Ememption Declaration") |
| 398 | ) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 399 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 400 | return frappe._dict( |
| 401 | { |
| 402 | "hra_amount": hra_amount, |
| 403 | "annual_exemption": annual_exemption, |
| 404 | "monthly_exemption": monthly_exemption, |
| 405 | } |
| 406 | ) |
| 407 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 408 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 409 | 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] | 410 | salary_slip = make_salary_slip( |
| 411 | salary_structure, employee=employee, for_preview=1, ignore_permissions=True |
| 412 | ) |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 413 | basic_amt, hra_amt = 0, 0 |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 414 | for earning in salary_slip.earnings: |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 415 | if earning.salary_component == basic_component: |
| 416 | basic_amt = earning.amount |
| 417 | elif earning.salary_component == hra_component: |
| 418 | hra_amt = earning.amount |
| 419 | if basic_amt and hra_amt: |
| 420 | return basic_amt, hra_amt |
Ranjith Kurungadam | 14e94f8 | 2018-07-16 16:12:46 +0530 | [diff] [blame] | 421 | return basic_amt, hra_amt |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 422 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 423 | |
| 424 | def calculate_hra_exemption( |
| 425 | salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city |
| 426 | ): |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 427 | # TODO make this configurable |
| 428 | exemptions = [] |
| 429 | frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency") |
| 430 | # case 1: The actual amount allotted by the employer as the HRA. |
| 431 | exemptions.append(get_annual_component_pay(frequency, monthly_hra)) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 432 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 433 | actual_annual_rent = monthly_house_rent * 12 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 434 | annual_basic = get_annual_component_pay(frequency, basic) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 435 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 436 | # case 2: Actual rent paid less 10% of the basic salary. |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 437 | exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1)) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 438 | # 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] | 439 | 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] | 440 | # return minimum of 3 cases |
| 441 | return min(exemptions) |
| 442 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 443 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 444 | def get_annual_component_pay(frequency, amount): |
| 445 | if frequency == "Daily": |
| 446 | return amount * 365 |
| 447 | elif frequency == "Weekly": |
| 448 | return amount * 52 |
| 449 | elif frequency == "Fortnightly": |
| 450 | return amount * 26 |
| 451 | elif frequency == "Monthly": |
| 452 | return amount * 12 |
| 453 | elif frequency == "Bimonthly": |
| 454 | return amount * 6 |
| 455 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 456 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 457 | def validate_house_rent_dates(doc): |
| 458 | if not doc.rented_to_date or not doc.rented_from_date: |
| 459 | frappe.throw(_("House rented dates required for exemption calculation")) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 460 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 461 | if date_diff(doc.rented_to_date, doc.rented_from_date) < 14: |
| 462 | frappe.throw(_("House rented dates should be atleast 15 days apart")) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 463 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 464 | proofs = frappe.db.sql( |
| 465 | """ |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 466 | select name |
| 467 | from `tabEmployee Tax Exemption Proof Submission` |
| 468 | where |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 469 | docstatus=1 and employee=%(employee)s and payroll_period=%(payroll_period)s |
| 470 | 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] | 471 | """, |
| 472 | { |
| 473 | "employee": doc.employee, |
| 474 | "payroll_period": doc.payroll_period, |
| 475 | "from_date": doc.rented_from_date, |
| 476 | "to_date": doc.rented_to_date, |
| 477 | }, |
| 478 | ) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 479 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 480 | if proofs: |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 481 | 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] | 482 | |
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 | def calculate_hra_exemption_for_period(doc): |
| 485 | monthly_rent, eligible_hra = 0, 0 |
| 486 | if doc.house_rent_payment_amount: |
| 487 | validate_house_rent_dates(doc) |
| 488 | # TODO receive rented months or validate dates are start and end of months? |
| 489 | # Calc monthly rent, round to nearest .5 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 490 | factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1) / 30 |
| 491 | factor = round(factor * 2) / 2 |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 492 | monthly_rent = doc.house_rent_payment_amount / factor |
| 493 | # update field used by calculate_annual_eligible_hra_exemption |
| 494 | doc.monthly_house_rent = monthly_rent |
| 495 | exemptions = calculate_annual_eligible_hra_exemption(doc) |
| 496 | |
| 497 | if exemptions["monthly_exemption"]: |
| 498 | # calc total exemption amount |
| 499 | eligible_hra = exemptions["monthly_exemption"] * factor |
Ranjith Kurungadam | 4f9744a | 2018-06-20 11:10:56 +0530 | [diff] [blame] | 500 | exemptions["monthly_house_rent"] = monthly_rent |
| 501 | exemptions["total_eligible_hra_exemption"] = eligible_hra |
| 502 | return exemptions |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 503 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 504 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 505 | def get_ewb_data(dt, dn): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 506 | |
| 507 | ewaybills = [] |
| 508 | for doc_name in dn: |
| 509 | doc = frappe.get_doc(dt, doc_name) |
| 510 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 511 | validate_doc(doc) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 512 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 513 | data = frappe._dict( |
| 514 | { |
| 515 | "transporterId": "", |
| 516 | "TotNonAdvolVal": 0, |
| 517 | } |
| 518 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 519 | |
| 520 | data.userGstin = data.fromGstin = doc.company_gstin |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 521 | data.supplyType = "O" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 522 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 523 | if dt == "Delivery Note": |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 524 | data.subSupplyType = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 525 | elif doc.gst_category in ["Registered Regular", "SEZ"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 526 | data.subSupplyType = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 527 | elif doc.gst_category in ["Overseas", "Deemed Export"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 528 | data.subSupplyType = 3 |
| 529 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 530 | frappe.throw(_("Unsupported GST Category for E-Way Bill JSON generation")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 531 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 532 | data.docType = "INV" |
| 533 | data.docDate = frappe.utils.formatdate(doc.posting_date, "dd/mm/yyyy") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 534 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 535 | company_address = frappe.get_doc("Address", doc.company_address) |
| 536 | billing_address = frappe.get_doc("Address", doc.customer_address) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 537 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 538 | # added dispatch address |
| 539 | dispatch_address = ( |
| 540 | frappe.get_doc("Address", doc.dispatch_address_name) |
| 541 | if doc.dispatch_address_name |
| 542 | else company_address |
| 543 | ) |
| 544 | shipping_address = frappe.get_doc("Address", doc.shipping_address_name) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 545 | |
Subin Tom | 5265ba3 | 2021-07-13 14:58:17 +0530 | [diff] [blame] | 546 | data = get_address_details(data, doc, company_address, billing_address, dispatch_address) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 547 | |
| 548 | data.itemList = [] |
Smit Vora | e2d866d | 2021-11-01 15:55:19 +0530 | [diff] [blame] | 549 | data.totalValue = doc.net_total |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 550 | |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 551 | data = get_item_list(data, doc, hsn_wise=True) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 552 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 553 | disable_rounded = frappe.db.get_single_value("Global Defaults", "disable_rounded_total") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 554 | data.totInvValue = doc.grand_total if disable_rounded else doc.rounded_total |
| 555 | |
| 556 | data = get_transport_details(data, doc) |
| 557 | |
| 558 | fields = { |
| 559 | "/. -": { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 560 | "docNo": doc.name, |
| 561 | "fromTrdName": doc.company, |
| 562 | "toTrdName": doc.customer_name, |
| 563 | "transDocNo": doc.lr_no, |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 564 | }, |
| 565 | "@#/,&. -": { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 566 | "fromAddr1": company_address.address_line1, |
| 567 | "fromAddr2": company_address.address_line2, |
| 568 | "fromPlace": company_address.city, |
| 569 | "toAddr1": shipping_address.address_line1, |
| 570 | "toAddr2": shipping_address.address_line2, |
| 571 | "toPlace": shipping_address.city, |
| 572 | "transporterName": doc.transporter_name, |
| 573 | }, |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | for allowed_chars, field_map in fields.items(): |
| 577 | for key, value in field_map.items(): |
| 578 | if not value: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 579 | data[key] = "" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 580 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 581 | data[key] = re.sub(r"[^\w" + allowed_chars + "]", "", value) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 582 | |
| 583 | ewaybills.append(data) |
| 584 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 585 | data = {"version": "1.0.0421", "billLists": ewaybills} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 586 | |
| 587 | return data |
| 588 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 589 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 590 | @frappe.whitelist() |
| 591 | def generate_ewb_json(dt, dn): |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 592 | dn = json.loads(dn) |
| 593 | return get_ewb_data(dt, dn) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 594 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 595 | |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 596 | @frappe.whitelist() |
| 597 | def download_ewb_json(): |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 598 | data = json.loads(frappe.local.form_dict.data) |
| 599 | frappe.local.response.filecontent = json.dumps(data, indent=4, sort_keys=True) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 600 | frappe.local.response.type = "download" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 601 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 602 | filename_prefix = "Bulk" |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 603 | docname = frappe.local.form_dict.docname |
| 604 | if docname: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 605 | if docname.startswith("["): |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 606 | docname = json.loads(docname) |
| 607 | if len(docname) == 1: |
| 608 | docname = docname[0] |
Deepesh Garg | 00ea59b | 2020-04-27 10:50:40 +0530 | [diff] [blame] | 609 | |
Sagar Vora | c9b4ba6 | 2020-07-11 17:44:20 +0530 | [diff] [blame] | 610 | if not isinstance(docname, list): |
| 611 | # 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] | 612 | filename_prefix = re.sub(r"[^\w_.)( -]", "", docname) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 613 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 614 | frappe.local.response.filename = "{0}_e-WayBill_Data_{1}.json".format( |
| 615 | filename_prefix, frappe.utils.random_string(5) |
| 616 | ) |
| 617 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 618 | |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 619 | @frappe.whitelist() |
| 620 | def get_gstins_for_company(company): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 621 | company_gstins = [] |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 622 | if company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 623 | company_gstins = frappe.db.sql( |
| 624 | """select |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 625 | distinct `tabAddress`.gstin |
| 626 | from |
| 627 | `tabAddress`, `tabDynamic Link` |
| 628 | where |
| 629 | `tabDynamic Link`.parent = `tabAddress`.name and |
| 630 | `tabDynamic Link`.parenttype = 'Address' and |
| 631 | `tabDynamic Link`.link_doctype = 'Company' and |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 632 | `tabDynamic Link`.link_name = %(company)s""", |
| 633 | {"company": company}, |
| 634 | ) |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 635 | return company_gstins |
| 636 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 637 | |
Subin Tom | 5265ba3 | 2021-07-13 14:58:17 +0530 | [diff] [blame] | 638 | def get_address_details(data, doc, company_address, billing_address, dispatch_address): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 639 | data.fromPincode = validate_pincode(company_address.pincode, "Company Address") |
| 640 | data.fromStateCode = validate_state_code(company_address.gst_state_number, "Company Address") |
| 641 | data.actualFromStateCode = validate_state_code( |
| 642 | dispatch_address.gst_state_number, "Dispatch Address" |
| 643 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 644 | |
| 645 | 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] | 646 | data.toGstin = "URP" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 647 | set_gst_state_and_state_number(billing_address) |
| 648 | else: |
| 649 | data.toGstin = doc.billing_address_gstin |
| 650 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 651 | data.toPincode = validate_pincode(billing_address.pincode, "Customer Address") |
| 652 | data.toStateCode = validate_state_code(billing_address.gst_state_number, "Customer Address") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 653 | |
| 654 | if doc.customer_address != doc.shipping_address_name: |
| 655 | data.transType = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 656 | shipping_address = frappe.get_doc("Address", doc.shipping_address_name) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 657 | set_gst_state_and_state_number(shipping_address) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 658 | data.toPincode = validate_pincode(shipping_address.pincode, "Shipping Address") |
| 659 | data.actualToStateCode = validate_state_code( |
| 660 | shipping_address.gst_state_number, "Shipping Address" |
| 661 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 662 | else: |
| 663 | data.transType = 1 |
| 664 | data.actualToStateCode = data.toStateCode |
| 665 | shipping_address = billing_address |
Deepesh Garg | d07447a | 2020-11-24 08:09:17 +0530 | [diff] [blame] | 666 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 667 | if doc.gst_category == "SEZ": |
Smit Vora | bbe4933 | 2020-11-18 20:58:59 +0530 | [diff] [blame] | 668 | data.toStateCode = 99 |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 669 | |
| 670 | return data |
| 671 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 672 | |
Subin Tom | d49346a | 2021-09-17 10:39:03 +0530 | [diff] [blame] | 673 | def get_item_list(data, doc, hsn_wise=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 674 | for attr in ["cgstValue", "sgstValue", "igstValue", "cessValue", "OthValue"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 675 | data[attr] = 0 |
| 676 | |
| 677 | gst_accounts = get_gst_accounts(doc.company, account_wise=True) |
| 678 | tax_map = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 679 | "sgst_account": ["sgstRate", "sgstValue"], |
| 680 | "cgst_account": ["cgstRate", "cgstValue"], |
| 681 | "igst_account": ["igstRate", "igstValue"], |
| 682 | "cess_account": ["cessRate", "cessValue"], |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 683 | } |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 684 | item_data_attrs = ["sgstRate", "cgstRate", "igstRate", "cessRate", "cessNonAdvol"] |
| 685 | hsn_wise_charges, hsn_taxable_amount = get_itemised_tax_breakup_data( |
| 686 | doc, account_wise=True, hsn_wise=hsn_wise |
| 687 | ) |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 688 | for item_or_hsn, taxable_amount in hsn_taxable_amount.items(): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 689 | item_data = frappe._dict() |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 690 | if not item_or_hsn: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 691 | 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] | 692 | 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] | 693 | item_data.taxableAmount = taxable_amount |
| 694 | item_data.qtyUnit = "" |
| 695 | for attr in item_data_attrs: |
| 696 | item_data[attr] = 0 |
| 697 | |
Smit Vora | 520f33b | 2021-11-14 08:10:53 +0530 | [diff] [blame] | 698 | 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] | 699 | account_type = gst_accounts.get(account, "") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 700 | for tax_acc, attrs in tax_map.items(): |
| 701 | if account_type == tax_acc: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 702 | item_data[attrs[0]] = tax_detail.get("tax_rate") |
| 703 | data[attrs[1]] += tax_detail.get("tax_amount") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 704 | break |
| 705 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 706 | data.OthValue += tax_detail.get("tax_amount") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 707 | |
| 708 | data.itemList.append(item_data) |
| 709 | |
| 710 | # Tax amounts rounded to 2 decimals to avoid exceeding max character limit |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 711 | for attr in ["sgstValue", "cgstValue", "igstValue", "cessValue"]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 712 | data[attr] = flt(data[attr], 2) |
| 713 | |
| 714 | return data |
| 715 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 716 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 717 | def validate_doc(doc): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 718 | if doc.docstatus != 1: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 719 | 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] | 720 | |
| 721 | if doc.is_return: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 722 | 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] | 723 | |
| 724 | if doc.ewaybill: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 725 | frappe.throw(_("e-Way Bill already exists for this document")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 726 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 727 | reqd_fields = [ |
| 728 | "company_gstin", |
| 729 | "company_address", |
| 730 | "customer_address", |
| 731 | "shipping_address_name", |
| 732 | "mode_of_transport", |
| 733 | "distance", |
| 734 | ] |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 735 | |
| 736 | for fieldname in reqd_fields: |
| 737 | if not doc.get(fieldname): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 738 | frappe.throw( |
| 739 | _("{} is required to generate E-Way Bill JSON").format(doc.meta.get_label(fieldname)) |
| 740 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 741 | |
| 742 | if len(doc.company_gstin) < 15: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 743 | frappe.throw(_("You must be a registered supplier to generate e-Way Bill")) |
| 744 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 745 | |
| 746 | def get_transport_details(data, doc): |
| 747 | if doc.distance > 4000: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 748 | frappe.throw(_("Distance cannot be greater than 4000 kms")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 749 | |
| 750 | data.transDistance = int(round(doc.distance)) |
| 751 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 752 | transport_modes = {"Road": 1, "Rail": 2, "Air": 3, "Ship": 4} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 753 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 754 | vehicle_types = {"Regular": "R", "Over Dimensional Cargo (ODC)": "O"} |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 755 | |
| 756 | data.transMode = transport_modes.get(doc.mode_of_transport) |
| 757 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 758 | if doc.mode_of_transport == "Road": |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 759 | if not doc.gst_transporter_id and not doc.vehicle_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 760 | frappe.throw( |
| 761 | _("Either GST Transporter ID or Vehicle No is required if Mode of Transport is Road") |
| 762 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 763 | if doc.vehicle_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 764 | data.vehicleNo = doc.vehicle_no.replace(" ", "") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 765 | if not doc.gst_vehicle_type: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 766 | frappe.throw(_("Vehicle Type is required if Mode of Transport is Road")) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 767 | else: |
| 768 | data.vehicleType = vehicle_types.get(doc.gst_vehicle_type) |
| 769 | else: |
| 770 | if not doc.lr_no or not doc.lr_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 771 | 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] | 772 | |
| 773 | if doc.lr_no: |
| 774 | data.transDocNo = doc.lr_no |
| 775 | |
| 776 | if doc.lr_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 777 | data.transDocDate = frappe.utils.formatdate(doc.lr_date, "dd/mm/yyyy") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 778 | |
| 779 | if doc.gst_transporter_id: |
karthikeyan5 | ca46bed | 2020-05-30 15:00:56 +0530 | [diff] [blame] | 780 | if doc.gst_transporter_id[0:2] != "88": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 781 | validate_gstin_check_digit(doc.gst_transporter_id, label="GST Transporter ID") |
karthikeyan5 | ca46bed | 2020-05-30 15:00:56 +0530 | [diff] [blame] | 782 | data.transporterId = doc.gst_transporter_id |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 783 | |
| 784 | return data |
| 785 | |
| 786 | |
| 787 | def validate_pincode(pincode, address): |
| 788 | pin_not_found = "Pin Code doesn't exist for {}" |
| 789 | incorrect_pin = "Pin Code for {} is incorrecty formatted. It must be 6 digits (without spaces)" |
| 790 | |
| 791 | if not pincode: |
| 792 | frappe.throw(_(pin_not_found.format(address))) |
| 793 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 794 | pincode = pincode.replace(" ", "") |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 795 | if not pincode.isdigit() or len(pincode) != 6: |
| 796 | frappe.throw(_(incorrect_pin.format(address))) |
| 797 | else: |
| 798 | return int(pincode) |
| 799 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 800 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 801 | def validate_state_code(state_code, address): |
| 802 | no_state_code = "GST State Code not found for {0}. Please set GST State in {0}" |
| 803 | if not state_code: |
| 804 | frappe.throw(_(no_state_code.format(address))) |
| 805 | else: |
| 806 | return int(state_code) |
| 807 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 808 | |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 809 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 810 | def get_gst_accounts( |
| 811 | company=None, account_wise=False, only_reverse_charge=0, only_non_reverse_charge=0 |
| 812 | ): |
| 813 | filters = {"parent": "GST Settings"} |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 814 | |
| 815 | if company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 816 | filters.update({"company": company}) |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 817 | if only_reverse_charge: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 818 | filters.update({"is_reverse_charge_account": 1}) |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 819 | elif only_non_reverse_charge: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 820 | filters.update({"is_reverse_charge_account": 0}) |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 821 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 822 | gst_accounts = frappe._dict() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 823 | gst_settings_accounts = frappe.get_all( |
| 824 | "GST Account", |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 825 | filters=filters, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 826 | fields=["cgst_account", "sgst_account", "igst_account", "cess_account"], |
| 827 | ) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 828 | |
Deepesh Garg | 4427390 | 2021-05-20 17:19:24 +0530 | [diff] [blame] | 829 | 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] | 830 | frappe.throw(_("Please set GST Accounts in GST Settings")) |
| 831 | |
| 832 | for d in gst_settings_accounts: |
| 833 | for acc, val in d.items(): |
| 834 | if not account_wise: |
| 835 | gst_accounts.setdefault(acc, []).append(val) |
| 836 | elif val: |
| 837 | gst_accounts[val] = acc |
| 838 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 839 | return gst_accounts |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 840 | |
Deepesh Garg | 52c319c | 2020-07-15 23:57:03 +0530 | [diff] [blame] | 841 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 842 | def validate_reverse_charge_transaction(doc, method): |
| 843 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 844 | |
| 845 | if country != "India": |
Deepesh Garg | 52c319c | 2020-07-15 23:57:03 +0530 | [diff] [blame] | 846 | return |
| 847 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 848 | base_gst_tax = 0 |
| 849 | base_reverse_charge_booked = 0 |
Deepesh Garg | 8aed48f | 2020-08-19 18:30:18 +0530 | [diff] [blame] | 850 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 851 | if doc.reverse_charge == "Y": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 852 | gst_accounts = get_gst_accounts(doc.company, only_reverse_charge=1) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 853 | reverse_charge_accounts = ( |
| 854 | gst_accounts.get("cgst_account") |
| 855 | + gst_accounts.get("sgst_account") |
| 856 | + gst_accounts.get("igst_account") |
| 857 | ) |
Deepesh Garg | 3c004ad | 2020-07-02 21:18:29 +0530 | [diff] [blame] | 858 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 859 | gst_accounts = get_gst_accounts(doc.company, only_non_reverse_charge=1) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 860 | non_reverse_charge_accounts = ( |
| 861 | gst_accounts.get("cgst_account") |
| 862 | + gst_accounts.get("sgst_account") |
| 863 | + gst_accounts.get("igst_account") |
| 864 | ) |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 865 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 866 | for tax in doc.get("taxes"): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 867 | if tax.account_head in non_reverse_charge_accounts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 868 | if tax.add_deduct_tax == "Add": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 869 | base_gst_tax += tax.base_tax_amount_after_discount_amount |
| 870 | else: |
| 871 | base_gst_tax += tax.base_tax_amount_after_discount_amount |
| 872 | elif tax.account_head in reverse_charge_accounts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 873 | if tax.add_deduct_tax == "Add": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 874 | base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount |
| 875 | else: |
| 876 | base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 877 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 878 | if base_gst_tax != base_reverse_charge_booked: |
| 879 | msg = _("Booked reverse charge is not equal to applied tax amount") |
| 880 | msg += "<br>" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 881 | msg += _( |
| 882 | "Please refer {gst_document_link} to learn more about how to setup and create reverse charge invoice" |
| 883 | ).format( |
| 884 | gst_document_link='<a href="https://docs.erpnext.com/docs/user/manual/en/regional/india/gst-setup">GST Documentation</a>' |
| 885 | ) |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 886 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 887 | frappe.throw(msg) |
Deepesh Garg | 24f9a80 | 2020-06-03 10:59:37 +0530 | [diff] [blame] | 888 | |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 889 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 890 | def update_itc_availed_fields(doc, method): |
| 891 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 892 | |
| 893 | if country != "India": |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 894 | return |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 895 | |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 896 | # Initialize values |
| 897 | doc.itc_integrated_tax = doc.itc_state_tax = doc.itc_central_tax = doc.itc_cess_amount = 0 |
| 898 | gst_accounts = get_gst_accounts(doc.company, only_non_reverse_charge=1) |
Deepesh Garg | 6a5ef26 | 2021-02-19 14:30:23 +0530 | [diff] [blame] | 899 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 900 | for tax in doc.get("taxes"): |
| 901 | if tax.account_head in gst_accounts.get("igst_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 902 | doc.itc_integrated_tax += flt(tax.base_tax_amount_after_discount_amount) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 903 | if tax.account_head in gst_accounts.get("sgst_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 904 | doc.itc_state_tax += flt(tax.base_tax_amount_after_discount_amount) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 905 | if tax.account_head in gst_accounts.get("cgst_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 906 | doc.itc_central_tax += flt(tax.base_tax_amount_after_discount_amount) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 907 | if tax.account_head in gst_accounts.get("cess_account", []): |
Deepesh Garg | 55fe85d | 2021-05-14 12:17:41 +0530 | [diff] [blame] | 908 | doc.itc_cess_amount += flt(tax.base_tax_amount_after_discount_amount) |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 909 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 910 | |
Deepesh Garg | 8b644d8 | 2021-07-15 15:36:54 +0530 | [diff] [blame] | 911 | def update_place_of_supply(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 912 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 913 | if country != "India": |
Deepesh Garg | 8b644d8 | 2021-07-15 15:36:54 +0530 | [diff] [blame] | 914 | return |
| 915 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 916 | address = frappe.db.get_value( |
| 917 | "Address", doc.get("customer_address"), ["gst_state", "gst_state_number"], as_dict=1 |
| 918 | ) |
Deepesh Garg | 8b644d8 | 2021-07-15 15:36:54 +0530 | [diff] [blame] | 919 | if address and address.gst_state and address.gst_state_number: |
| 920 | doc.place_of_supply = cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
| 921 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 922 | |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 923 | @frappe.whitelist() |
| 924 | def get_regional_round_off_accounts(company, account_list): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 925 | country = frappe.get_cached_value("Company", company, "country") |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 926 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 927 | if country != "India": |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 928 | return |
| 929 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 930 | if isinstance(account_list, str): |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 931 | account_list = json.loads(account_list) |
| 932 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 933 | 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] | 934 | return |
| 935 | |
| 936 | gst_accounts = get_gst_accounts(company) |
walstanb | 52403c5 | 2021-03-27 10:13:27 +0530 | [diff] [blame] | 937 | |
| 938 | gst_account_list = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 939 | for account in ["cgst_account", "sgst_account", "igst_account"]: |
walstanb | ab673d9 | 2021-03-27 12:52:23 +0530 | [diff] [blame] | 940 | if account in gst_accounts: |
walstanb | 52403c5 | 2021-03-27 10:13:27 +0530 | [diff] [blame] | 941 | gst_account_list += gst_accounts.get(account) |
Deepesh Garg | 004f9e6 | 2021-03-16 13:09:59 +0530 | [diff] [blame] | 942 | |
| 943 | account_list.extend(gst_account_list) |
| 944 | |
| 945 | return account_list |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 946 | |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 947 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 948 | def update_taxable_values(doc, method): |
| 949 | country = frappe.get_cached_value("Company", doc.company, "country") |
| 950 | |
| 951 | if country != "India": |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 952 | return |
| 953 | |
| 954 | gst_accounts = get_gst_accounts(doc.company) |
| 955 | |
| 956 | # Only considering sgst account to avoid inflating taxable value |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 957 | gst_account_list = ( |
| 958 | gst_accounts.get("sgst_account", []) |
| 959 | + gst_accounts.get("sgst_account", []) |
| 960 | + gst_accounts.get("igst_account", []) |
| 961 | ) |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 962 | |
| 963 | additional_taxes = 0 |
| 964 | total_charges = 0 |
| 965 | item_count = 0 |
| 966 | considered_rows = [] |
| 967 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 968 | for tax in doc.get("taxes"): |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 969 | prev_row_id = cint(tax.row_id) - 1 |
| 970 | 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] | 971 | if tax.charge_type == "On Previous Row Amount": |
| 972 | 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] | 973 | considered_rows.append(prev_row_id) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 974 | if tax.charge_type == "On Previous Row Total": |
| 975 | 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] | 976 | considered_rows.append(prev_row_id) |
| 977 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 978 | for item in doc.get("items"): |
Deepesh Garg | 4afda3c | 2021-06-01 13:13:04 +0530 | [diff] [blame] | 979 | proportionate_value = item.base_net_amount if doc.base_net_total else item.qty |
| 980 | 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] | 981 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 982 | applicable_charges = flt( |
| 983 | flt( |
| 984 | proportionate_value * (flt(additional_taxes) / flt(total_value)), |
| 985 | item.precision("taxable_value"), |
| 986 | ) |
| 987 | ) |
Deepesh Garg | c36e48a | 2021-04-12 10:55:43 +0530 | [diff] [blame] | 988 | item.taxable_value = applicable_charges + proportionate_value |
| 989 | total_charges += applicable_charges |
| 990 | item_count += 1 |
| 991 | |
| 992 | if total_charges != additional_taxes: |
| 993 | diff = additional_taxes - total_charges |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 994 | doc.get("items")[item_count - 1].taxable_value += diff |
| 995 | |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 996 | |
| 997 | def get_depreciation_amount(asset, depreciable_value, row): |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 998 | if row.depreciation_method in ("Straight Line", "Manual"): |
GangaManoj | 2b93e54 | 2021-06-19 13:45:37 +0530 | [diff] [blame] | 999 | # if the Depreciation Schedule is being prepared for the first time |
GangaManoj | da8da9f | 2021-06-19 14:00:26 +0530 | [diff] [blame] | 1000 | if not asset.flags.increase_in_asset_life: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1001 | depreciation_amount = ( |
| 1002 | flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life) |
| 1003 | ) / flt(row.total_number_of_depreciations) |
GangaManoj | 2b93e54 | 2021-06-19 13:45:37 +0530 | [diff] [blame] | 1004 | |
| 1005 | # if the Depreciation Schedule is being modified after Asset Repair |
| 1006 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1007 | depreciation_amount = ( |
| 1008 | flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) |
| 1009 | ) / (date_diff(asset.to_date, asset.available_for_use_date) / 365) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 1010 | |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 1011 | else: |
| 1012 | rate_of_depreciation = row.rate_of_depreciation |
| 1013 | # if its the first depreciation |
| 1014 | if depreciable_value == asset.gross_purchase_amount: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1015 | 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] | 1016 | # as per IT act, if the asset is purchased in the 2nd half of fiscal year, then rate is divided by 2 |
| 1017 | diff = date_diff(row.depreciation_start_date, asset.available_for_use_date) |
| 1018 | if diff <= 180: |
| 1019 | rate_of_depreciation = rate_of_depreciation / 2 |
| 1020 | frappe.msgprint( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1021 | _( |
| 1022 | "As per IT Act, the rate of depreciation for the first depreciation entry is reduced by 50%." |
| 1023 | ) |
| 1024 | ) |
Saqib | 9226cd3 | 2021-05-10 12:36:56 +0530 | [diff] [blame] | 1025 | |
| 1026 | depreciation_amount = flt(depreciable_value * (flt(rate_of_depreciation) / 100)) |
| 1027 | |
Saqib | 3a50490 | 2021-08-03 15:57:11 +0530 | [diff] [blame] | 1028 | return depreciation_amount |
| 1029 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1030 | |
Saqib | 3a50490 | 2021-08-03 15:57:11 +0530 | [diff] [blame] | 1031 | def set_item_tax_from_hsn_code(item): |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 1032 | if not item.taxes and item.gst_hsn_code: |
Saqib | 3a50490 | 2021-08-03 15:57:11 +0530 | [diff] [blame] | 1033 | hsn_doc = frappe.get_doc("GST HSN Code", item.gst_hsn_code) |
| 1034 | |
| 1035 | for tax in hsn_doc.taxes: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1036 | item.append( |
| 1037 | "taxes", |
| 1038 | { |
| 1039 | "item_tax_template": tax.item_tax_template, |
| 1040 | "tax_category": tax.tax_category, |
| 1041 | "valid_from": tax.valid_from, |
| 1042 | }, |
| 1043 | ) |
| 1044 | |
Deepesh Garg | 2b2572b | 2021-08-20 14:40:12 +0530 | [diff] [blame] | 1045 | |
| 1046 | def delete_gst_settings_for_company(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1047 | if doc.country != "India": |
Deepesh Garg | 2b2572b | 2021-08-20 14:40:12 +0530 | [diff] [blame] | 1048 | return |
| 1049 | |
| 1050 | gst_settings = frappe.get_doc("GST Settings") |
| 1051 | records_to_delete = [] |
| 1052 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1053 | for d in reversed(gst_settings.get("gst_accounts")): |
Deepesh Garg | 2b2572b | 2021-08-20 14:40:12 +0530 | [diff] [blame] | 1054 | if d.company == doc.name: |
| 1055 | records_to_delete.append(d) |
| 1056 | |
| 1057 | for d in records_to_delete: |
| 1058 | gst_settings.remove(d) |
| 1059 | |
| 1060 | gst_settings.save() |