blob: eccce2b574fa4276291a111f60a289d202123e2c [file] [log] [blame]
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05301import frappe, re
2from frappe import _
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +05303from frappe.utils import cstr, flt, date_diff, getdate
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05304from erpnext.regional.india import states, state_numbers
Nabin Haitb962fc12017-07-17 18:02:31 +05305from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
Shreya Shah4fa600a2018-06-05 11:27:53 +05306from erpnext.controllers.accounts_controller import get_taxes_and_charges
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +05307from erpnext.hr.utils import get_salary_assignment
8from erpnext.hr.doctype.salary_structure.salary_structure import make_salary_slip
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05309
10def validate_gstin_for_india(doc, method):
11 if not hasattr(doc, 'gstin'):
12 return
13
14 if doc.gstin:
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053015 doc.gstin = doc.gstin.upper()
rohitwaghchaure2c111b72018-04-11 15:50:06 +053016 if doc.gstin not in ["NA", "na"]:
Aditya Duggalf1bd39c2017-06-29 14:25:19 +053017 p = re.compile("[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9A-Za-z]{1}[Z]{1}[0-9a-zA-Z]{1}")
18 if not p.match(doc.gstin):
19 frappe.throw(_("Invalid GSTIN or Enter NA for Unregistered"))
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053020
Nabin Hait35cd1d32017-11-28 13:01:01 +053021 if not doc.gst_state:
22 if doc.state in states:
23 doc.gst_state = doc.state
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053024
Nabin Hait35cd1d32017-11-28 13:01:01 +053025 if doc.gst_state:
26 doc.gst_state_number = state_numbers[doc.gst_state]
27 if doc.gstin and doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]:
28 frappe.throw(_("First 2 digits of GSTIN should match with State number {0}")
29 .format(doc.gst_state_number))
Rushabh Mehta7231f292017-07-13 15:00:56 +053030
Nabin Haitb962fc12017-07-17 18:02:31 +053031def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
32 if frappe.get_meta(item_doctype).has_field('gst_hsn_code'):
33 return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts
34 else:
35 return [_("Item"), _("Taxable Amount")] + tax_accounts
Nabin Haitb95ecd72018-02-16 13:19:04 +053036
Nabin Haitb962fc12017-07-17 18:02:31 +053037def get_itemised_tax_breakup_data(doc):
38 itemised_tax = get_itemised_tax(doc.taxes)
39
40 itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
Nabin Haitb95ecd72018-02-16 13:19:04 +053041
Nabin Haitb962fc12017-07-17 18:02:31 +053042 if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'):
43 return itemised_tax, itemised_taxable_amount
44
45 item_hsn_map = frappe._dict()
46 for d in doc.items:
47 item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code"))
48
49 hsn_tax = {}
50 for item, taxes in itemised_tax.items():
51 hsn_code = item_hsn_map.get(item)
52 hsn_tax.setdefault(hsn_code, frappe._dict())
53 for tax_account, tax_detail in taxes.items():
54 hsn_tax[hsn_code].setdefault(tax_account, {"tax_rate": 0, "tax_amount": 0})
55 hsn_tax[hsn_code][tax_account]["tax_rate"] = tax_detail.get("tax_rate")
56 hsn_tax[hsn_code][tax_account]["tax_amount"] += tax_detail.get("tax_amount")
57
58 # set taxable amount
59 hsn_taxable_amount = frappe._dict()
60 for item, taxable_amount in itemised_taxable_amount.items():
61 hsn_code = item_hsn_map.get(item)
62 hsn_taxable_amount.setdefault(hsn_code, 0)
63 hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item)
64
65 return hsn_tax, hsn_taxable_amount
66
Shreya Shah4fa600a2018-06-05 11:27:53 +053067def set_place_of_supply(doc, method=None):
68 doc.place_of_supply = get_place_of_supply(doc, doc.doctype)
Nabin Haitb95ecd72018-02-16 13:19:04 +053069
Rushabh Mehta7231f292017-07-13 15:00:56 +053070# don't remove this function it is used in tests
71def test_method():
72 '''test function'''
Nabin Haitb95ecd72018-02-16 13:19:04 +053073 return 'overridden'
Shreya Shah4fa600a2018-06-05 11:27:53 +053074
75def get_place_of_supply(out, doctype):
76 if not frappe.get_meta('Address').has_field('gst_state'): return
77
78 if doctype in ("Sales Invoice", "Delivery Note"):
79 address_name = out.shipping_address_name or out.customer_address
80 elif doctype == "Purchase Invoice":
81 address_name = out.shipping_address or out.supplier_address
82
83 if address_name:
84 address = frappe.db.get_value("Address", address_name, ["gst_state", "gst_state_number"], as_dict=1)
Nabin Hait2390da62018-08-30 16:16:35 +053085 if address.gst_state and address.gst_state_number:
86 return cstr(address.gst_state_number) + "-" + cstr(address.gst_state)
Shreya Shah4fa600a2018-06-05 11:27:53 +053087
88def get_regional_address_details(out, doctype, company):
Shreya Shah4fa600a2018-06-05 11:27:53 +053089 out.place_of_supply = get_place_of_supply(out, doctype)
90
91 if not out.place_of_supply: return
92
93 if doctype in ("Sales Invoice", "Delivery Note"):
94 master_doctype = "Sales Taxes and Charges Template"
Shreya2ad81722018-06-05 16:49:29 +053095 if not out.company_gstin:
Shreya Shah4fa600a2018-06-05 11:27:53 +053096 return
Shreya2ad81722018-06-05 16:49:29 +053097 elif doctype == "Purchase Invoice":
Shreya Shah4fa600a2018-06-05 11:27:53 +053098 master_doctype = "Purchase Taxes and Charges Template"
Shreya2ad81722018-06-05 16:49:29 +053099 if not out.supplier_gstin:
Shreya Shah4fa600a2018-06-05 11:27:53 +0530100 return
101
Rohit Waghchaurede82ad12018-06-07 13:20:57 +0530102 if ((doctype in ("Sales Invoice", "Delivery Note") and out.company_gstin
103 and out.company_gstin[:2] != out.place_of_supply[:2]) or (doctype == "Purchase Invoice"
104 and out.supplier_gstin and out.supplier_gstin[:2] != out.place_of_supply[:2])):
Shreya Shah4fa600a2018-06-05 11:27:53 +0530105 default_tax = frappe.db.get_value(master_doctype, {"company": company, "is_inter_state":1, "disabled":0})
106 else:
107 default_tax = frappe.db.get_value(master_doctype, {"company": company, "disabled":0, "is_default": 1})
108
109 if not default_tax:
110 return
111 out["taxes_and_charges"] = default_tax
112 out.taxes = get_taxes_and_charges(master_doctype, default_tax)
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530113
114def calculate_annual_eligible_hra_exemption(doc):
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530115 basic_component = frappe.get_cached_value('Company', doc.company, "basic_component")
116 hra_component = frappe.get_cached_value('Company', doc.company, "hra_component")
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530117 annual_exemption, monthly_exemption, hra_amount = 0, 0, 0
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530118 if hra_component and basic_component:
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530119 assignment = get_salary_assignment(doc.employee, getdate())
120 if assignment and frappe.db.exists("Salary Detail", {
121 "parent": assignment.salary_structure,
122 "salary_component": hra_component, "parentfield": "earnings"}):
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530123 basic_amount, hra_amount = get_component_amt_from_salary_slip(doc.employee,
124 assignment.salary_structure, basic_component, hra_component)
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530125 if hra_amount:
126 if doc.monthly_house_rent:
127 annual_exemption = calculate_hra_exemption(assignment.salary_structure,
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530128 basic_amount, hra_amount, doc.monthly_house_rent,
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530129 doc.rented_in_metro_city)
130 if annual_exemption > 0:
131 monthly_exemption = annual_exemption / 12
132 else:
133 annual_exemption = 0
134 return {"hra_amount": hra_amount, "annual_exemption": annual_exemption, "monthly_exemption": monthly_exemption}
135
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530136def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component):
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530137 salary_slip = make_salary_slip(salary_structure, employee=employee)
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530138 basic_amt, hra_amt = 0, 0
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530139 for earning in salary_slip.earnings:
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530140 if earning.salary_component == basic_component:
141 basic_amt = earning.amount
142 elif earning.salary_component == hra_component:
143 hra_amt = earning.amount
144 if basic_amt and hra_amt:
145 return basic_amt, hra_amt
Ranjith Kurungadam14e94f82018-07-16 16:12:46 +0530146 return basic_amt, hra_amt
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530147
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530148def calculate_hra_exemption(salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city):
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530149 # TODO make this configurable
150 exemptions = []
151 frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency")
152 # case 1: The actual amount allotted by the employer as the HRA.
153 exemptions.append(get_annual_component_pay(frequency, monthly_hra))
154 actual_annual_rent = monthly_house_rent * 12
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530155 annual_basic = get_annual_component_pay(frequency, basic)
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530156 # case 2: Actual rent paid less 10% of the basic salary.
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530157 exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1))
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530158 # case 3: 50% of the basic salary, if the employee is staying in a metro city (40% for a non-metro city).
Ranjith Kurungadamb1a756c2018-07-01 16:42:38 +0530159 exemptions.append(annual_basic * 0.5 if rented_in_metro_city else annual_basic * 0.4)
Ranjith Kurungadama8e047a2018-06-14 17:56:16 +0530160 # return minimum of 3 cases
161 return min(exemptions)
162
163def get_annual_component_pay(frequency, amount):
164 if frequency == "Daily":
165 return amount * 365
166 elif frequency == "Weekly":
167 return amount * 52
168 elif frequency == "Fortnightly":
169 return amount * 26
170 elif frequency == "Monthly":
171 return amount * 12
172 elif frequency == "Bimonthly":
173 return amount * 6
174
175def validate_house_rent_dates(doc):
176 if not doc.rented_to_date or not doc.rented_from_date:
177 frappe.throw(_("House rented dates required for exemption calculation"))
178 if date_diff(doc.rented_to_date, doc.rented_from_date) < 14:
179 frappe.throw(_("House rented dates should be atleast 15 days apart"))
180 proofs = frappe.db.sql("""select name from `tabEmployee Tax Exemption Proof Submission`
181 where docstatus=1 and employee='{0}' and payroll_period='{1}' and
182 (rented_from_date between '{2}' and '{3}' or rented_to_date between
183 '{2}' and '{3}')""".format(doc.employee, doc.payroll_period,
184 doc.rented_from_date, doc.rented_to_date))
185 if proofs:
186 frappe.throw(_("House rent paid days overlap with {0}").format(proofs[0][0]))
187
188def calculate_hra_exemption_for_period(doc):
189 monthly_rent, eligible_hra = 0, 0
190 if doc.house_rent_payment_amount:
191 validate_house_rent_dates(doc)
192 # TODO receive rented months or validate dates are start and end of months?
193 # Calc monthly rent, round to nearest .5
194 factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1)/30
195 factor = round(factor * 2)/2
196 monthly_rent = doc.house_rent_payment_amount / factor
197 # update field used by calculate_annual_eligible_hra_exemption
198 doc.monthly_house_rent = monthly_rent
199 exemptions = calculate_annual_eligible_hra_exemption(doc)
200
201 if exemptions["monthly_exemption"]:
202 # calc total exemption amount
203 eligible_hra = exemptions["monthly_exemption"] * factor
Ranjith Kurungadam4f9744a2018-06-20 11:10:56 +0530204 exemptions["monthly_house_rent"] = monthly_rent
205 exemptions["total_eligible_hra_exemption"] = eligible_hra
206 return exemptions