Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 1 | import frappe, re |
| 2 | from frappe import _ |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 3 | from frappe.utils import cstr, flt, date_diff, getdate |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 4 | from erpnext.regional.india import states, state_numbers |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 5 | from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 6 | from erpnext.controllers.accounts_controller import get_taxes_and_charges |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 7 | from erpnext.hr.utils import get_salary_assignment |
| 8 | from erpnext.hr.doctype.salary_structure.salary_structure import make_salary_slip |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 9 | |
| 10 | def validate_gstin_for_india(doc, method): |
| 11 | if not hasattr(doc, 'gstin'): |
| 12 | return |
| 13 | |
Nabin Hait | 3dd5f55 | 2019-01-16 16:20:05 +0530 | [diff] [blame^] | 14 | doc.gstin = doc.gstin.upper().strip() if doc.gstin else "" |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 15 | if not doc.gstin or doc.gstin == 'NA': |
| 16 | return |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 17 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 18 | if len(doc.gstin) != 15: |
| 19 | frappe.throw(_("Invalid GSTIN! A GSTIN must have 15 characters.")) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 20 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 21 | p = re.compile("^[0-9]{2}[A-Z]{4}[0-9A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}[1-9A-Z]{1}[0-9A-Z]{1}$") |
| 22 | if not p.match(doc.gstin): |
| 23 | frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the format of GSTIN.")) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 24 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 25 | validate_gstin_check_digit(doc.gstin) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 26 | |
| 27 | if not doc.gst_state: |
Sagar Vora | f99e013 | 2019-01-10 11:57:24 +0530 | [diff] [blame] | 28 | if not doc.state: |
| 29 | return |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 30 | state = doc.state.lower() |
| 31 | states_lowercase = {s.lower():s for s in states} |
| 32 | if state in states_lowercase: |
| 33 | doc.gst_state = states_lowercase[state] |
| 34 | else: |
| 35 | return |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 36 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 37 | doc.gst_state_number = state_numbers[doc.gst_state] |
| 38 | if doc.gst_state_number != doc.gstin[:2]: |
| 39 | frappe.throw(_("Invalid GSTIN! First 2 digits of GSTIN should match with State number {0}.") |
| 40 | .format(doc.gst_state_number)) |
| 41 | |
| 42 | def validate_gstin_check_digit(gstin): |
| 43 | ''' Function to validate the check digit of the GSTIN.''' |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 44 | factor = 1 |
| 45 | total = 0 |
| 46 | code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 47 | mod = len(code_point_chars) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 48 | input_chars = gstin[:-1] |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 49 | for char in input_chars: |
| 50 | digit = factor * code_point_chars.find(char) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 51 | digit = (digit // mod) + (digit % mod) |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 52 | total += digit |
| 53 | factor = 2 if factor == 1 else 1 |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 54 | if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]: |
| 55 | frappe.throw(_("Invalid GSTIN! The check digit validation has failed. " + |
| 56 | "Please ensure you've typed the GSTIN correctly.")) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 57 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 58 | def get_itemised_tax_breakup_header(item_doctype, tax_accounts): |
| 59 | if frappe.get_meta(item_doctype).has_field('gst_hsn_code'): |
| 60 | return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts |
| 61 | else: |
| 62 | return [_("Item"), _("Taxable Amount")] + tax_accounts |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 63 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 64 | def get_itemised_tax_breakup_data(doc): |
| 65 | itemised_tax = get_itemised_tax(doc.taxes) |
| 66 | |
| 67 | itemised_taxable_amount = get_itemised_taxable_amount(doc.items) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 68 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 69 | if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'): |
| 70 | return itemised_tax, itemised_taxable_amount |
| 71 | |
| 72 | item_hsn_map = frappe._dict() |
| 73 | for d in doc.items: |
| 74 | item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code")) |
| 75 | |
| 76 | hsn_tax = {} |
| 77 | for item, taxes in itemised_tax.items(): |
| 78 | hsn_code = item_hsn_map.get(item) |
| 79 | hsn_tax.setdefault(hsn_code, frappe._dict()) |
| 80 | for tax_account, tax_detail in taxes.items(): |
| 81 | hsn_tax[hsn_code].setdefault(tax_account, {"tax_rate": 0, "tax_amount": 0}) |
| 82 | hsn_tax[hsn_code][tax_account]["tax_rate"] = tax_detail.get("tax_rate") |
| 83 | hsn_tax[hsn_code][tax_account]["tax_amount"] += tax_detail.get("tax_amount") |
| 84 | |
| 85 | # set taxable amount |
| 86 | hsn_taxable_amount = frappe._dict() |
| 87 | for item, taxable_amount in itemised_taxable_amount.items(): |
| 88 | hsn_code = item_hsn_map.get(item) |
| 89 | hsn_taxable_amount.setdefault(hsn_code, 0) |
| 90 | hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item) |
| 91 | |
| 92 | return hsn_tax, hsn_taxable_amount |
| 93 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 94 | def set_place_of_supply(doc, method=None): |
| 95 | doc.place_of_supply = get_place_of_supply(doc, doc.doctype) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 96 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 97 | # don't remove this function it is used in tests |
| 98 | def test_method(): |
| 99 | '''test function''' |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 100 | return 'overridden' |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 101 | |
| 102 | def get_place_of_supply(out, doctype): |
| 103 | if not frappe.get_meta('Address').has_field('gst_state'): return |
| 104 | |
| 105 | if doctype in ("Sales Invoice", "Delivery Note"): |
| 106 | address_name = out.shipping_address_name or out.customer_address |
| 107 | elif doctype == "Purchase Invoice": |
| 108 | address_name = out.shipping_address or out.supplier_address |
| 109 | |
| 110 | if address_name: |
| 111 | address = frappe.db.get_value("Address", address_name, ["gst_state", "gst_state_number"], as_dict=1) |
Rohit Waghchaure | b6a735e | 2018-10-11 10:40:34 +0530 | [diff] [blame] | 112 | if address and address.gst_state and address.gst_state_number: |
Nabin Hait | 2390da6 | 2018-08-30 16:16:35 +0530 | [diff] [blame] | 113 | return cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 114 | |
| 115 | def get_regional_address_details(out, doctype, company): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 116 | out.place_of_supply = get_place_of_supply(out, doctype) |
| 117 | |
| 118 | if not out.place_of_supply: return |
| 119 | |
| 120 | if doctype in ("Sales Invoice", "Delivery Note"): |
| 121 | master_doctype = "Sales Taxes and Charges Template" |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 122 | if not out.company_gstin: |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 123 | return |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 124 | elif doctype == "Purchase Invoice": |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 125 | master_doctype = "Purchase Taxes and Charges Template" |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 126 | if not out.supplier_gstin: |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 127 | return |
| 128 | |
Rohit Waghchaure | de82ad1 | 2018-06-07 13:20:57 +0530 | [diff] [blame] | 129 | if ((doctype in ("Sales Invoice", "Delivery Note") and out.company_gstin |
| 130 | and out.company_gstin[:2] != out.place_of_supply[:2]) or (doctype == "Purchase Invoice" |
| 131 | and out.supplier_gstin and out.supplier_gstin[:2] != out.place_of_supply[:2])): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 132 | default_tax = frappe.db.get_value(master_doctype, {"company": company, "is_inter_state":1, "disabled":0}) |
| 133 | else: |
| 134 | default_tax = frappe.db.get_value(master_doctype, {"company": company, "disabled":0, "is_default": 1}) |
| 135 | |
| 136 | if not default_tax: |
| 137 | return |
| 138 | out["taxes_and_charges"] = default_tax |
| 139 | out.taxes = get_taxes_and_charges(master_doctype, default_tax) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 140 | |
| 141 | def calculate_annual_eligible_hra_exemption(doc): |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 142 | basic_component = frappe.get_cached_value('Company', doc.company, "basic_component") |
| 143 | hra_component = frappe.get_cached_value('Company', doc.company, "hra_component") |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 144 | annual_exemption, monthly_exemption, hra_amount = 0, 0, 0 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 145 | if hra_component and basic_component: |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 146 | assignment = get_salary_assignment(doc.employee, getdate()) |
| 147 | if assignment and frappe.db.exists("Salary Detail", { |
| 148 | "parent": assignment.salary_structure, |
| 149 | "salary_component": hra_component, "parentfield": "earnings"}): |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 150 | basic_amount, hra_amount = get_component_amt_from_salary_slip(doc.employee, |
| 151 | assignment.salary_structure, basic_component, hra_component) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 152 | if hra_amount: |
| 153 | if doc.monthly_house_rent: |
| 154 | annual_exemption = calculate_hra_exemption(assignment.salary_structure, |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 155 | basic_amount, hra_amount, doc.monthly_house_rent, |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 156 | doc.rented_in_metro_city) |
| 157 | if annual_exemption > 0: |
| 158 | monthly_exemption = annual_exemption / 12 |
| 159 | else: |
| 160 | annual_exemption = 0 |
| 161 | return {"hra_amount": hra_amount, "annual_exemption": annual_exemption, "monthly_exemption": monthly_exemption} |
| 162 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 163 | def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component): |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 164 | salary_slip = make_salary_slip(salary_structure, employee=employee) |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 165 | basic_amt, hra_amt = 0, 0 |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 166 | for earning in salary_slip.earnings: |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 167 | if earning.salary_component == basic_component: |
| 168 | basic_amt = earning.amount |
| 169 | elif earning.salary_component == hra_component: |
| 170 | hra_amt = earning.amount |
| 171 | if basic_amt and hra_amt: |
| 172 | return basic_amt, hra_amt |
Ranjith Kurungadam | 14e94f8 | 2018-07-16 16:12:46 +0530 | [diff] [blame] | 173 | return basic_amt, hra_amt |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 174 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 175 | def calculate_hra_exemption(salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city): |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 176 | # TODO make this configurable |
| 177 | exemptions = [] |
| 178 | frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency") |
| 179 | # case 1: The actual amount allotted by the employer as the HRA. |
| 180 | exemptions.append(get_annual_component_pay(frequency, monthly_hra)) |
| 181 | actual_annual_rent = monthly_house_rent * 12 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 182 | annual_basic = get_annual_component_pay(frequency, basic) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 183 | # case 2: Actual rent paid less 10% of the basic salary. |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 184 | exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1)) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 185 | # 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] | 186 | 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] | 187 | # return minimum of 3 cases |
| 188 | return min(exemptions) |
| 189 | |
| 190 | def get_annual_component_pay(frequency, amount): |
| 191 | if frequency == "Daily": |
| 192 | return amount * 365 |
| 193 | elif frequency == "Weekly": |
| 194 | return amount * 52 |
| 195 | elif frequency == "Fortnightly": |
| 196 | return amount * 26 |
| 197 | elif frequency == "Monthly": |
| 198 | return amount * 12 |
| 199 | elif frequency == "Bimonthly": |
| 200 | return amount * 6 |
| 201 | |
| 202 | def validate_house_rent_dates(doc): |
| 203 | if not doc.rented_to_date or not doc.rented_from_date: |
| 204 | frappe.throw(_("House rented dates required for exemption calculation")) |
| 205 | if date_diff(doc.rented_to_date, doc.rented_from_date) < 14: |
| 206 | frappe.throw(_("House rented dates should be atleast 15 days apart")) |
| 207 | proofs = frappe.db.sql("""select name from `tabEmployee Tax Exemption Proof Submission` |
| 208 | where docstatus=1 and employee='{0}' and payroll_period='{1}' and |
| 209 | (rented_from_date between '{2}' and '{3}' or rented_to_date between |
| 210 | '{2}' and '{3}')""".format(doc.employee, doc.payroll_period, |
| 211 | doc.rented_from_date, doc.rented_to_date)) |
| 212 | if proofs: |
| 213 | frappe.throw(_("House rent paid days overlap with {0}").format(proofs[0][0])) |
| 214 | |
| 215 | def calculate_hra_exemption_for_period(doc): |
| 216 | monthly_rent, eligible_hra = 0, 0 |
| 217 | if doc.house_rent_payment_amount: |
| 218 | validate_house_rent_dates(doc) |
| 219 | # TODO receive rented months or validate dates are start and end of months? |
| 220 | # Calc monthly rent, round to nearest .5 |
| 221 | factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1)/30 |
| 222 | factor = round(factor * 2)/2 |
| 223 | monthly_rent = doc.house_rent_payment_amount / factor |
| 224 | # update field used by calculate_annual_eligible_hra_exemption |
| 225 | doc.monthly_house_rent = monthly_rent |
| 226 | exemptions = calculate_annual_eligible_hra_exemption(doc) |
| 227 | |
| 228 | if exemptions["monthly_exemption"]: |
| 229 | # calc total exemption amount |
| 230 | eligible_hra = exemptions["monthly_exemption"] * factor |
Ranjith Kurungadam | 4f9744a | 2018-06-20 11:10:56 +0530 | [diff] [blame] | 231 | exemptions["monthly_house_rent"] = monthly_rent |
| 232 | exemptions["total_eligible_hra_exemption"] = eligible_hra |
| 233 | return exemptions |