Aditya Hase | f3c22f3 | 2019-01-22 18:22:20 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 2 | import frappe, re, json |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 3 | from frappe import _ |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 4 | from frappe.utils import cstr, flt, date_diff, nowdate |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 5 | from erpnext.regional.india import states, state_numbers |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 6 | 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] | 7 | from erpnext.controllers.accounts_controller import get_taxes_and_charges |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 8 | from erpnext.hr.utils import get_salary_assignment |
| 9 | from erpnext.hr.doctype.salary_structure.salary_structure import make_salary_slip |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 10 | |
| 11 | def validate_gstin_for_india(doc, method): |
rushin29 | 08a209b | 2019-03-15 15:28:50 +0530 | [diff] [blame] | 12 | if hasattr(doc, 'gst_state') and doc.gst_state: |
| 13 | doc.gst_state_number = state_numbers[doc.gst_state] |
FinByz Tech Pvt. Ltd | 237a871 | 2019-01-22 20:49:06 +0530 | [diff] [blame] | 14 | if not hasattr(doc, 'gstin') or not doc.gstin: |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 15 | return |
| 16 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 17 | gst_category = [] |
| 18 | |
| 19 | if len(doc.links): |
| 20 | link_doctype = doc.links[0].get("link_doctype") |
| 21 | link_name = doc.links[0].get("link_name") |
| 22 | |
| 23 | if link_doctype in ["Customer", "Supplier"]: |
| 24 | gst_category = frappe.db.get_value(link_doctype, {'name': link_name}, ['gst_category']) |
| 25 | |
Sagar Vora | d75095b | 2019-01-23 14:40:01 +0530 | [diff] [blame] | 26 | doc.gstin = doc.gstin.upper().strip() |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 27 | if not doc.gstin or doc.gstin == 'NA': |
| 28 | return |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 29 | |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 30 | if len(doc.gstin) != 15: |
| 31 | frappe.throw(_("Invalid GSTIN! A GSTIN must have 15 characters.")) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 32 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 33 | if gst_category and gst_category == 'UIN Holders': |
| 34 | p = re.compile("^[0-9]{4}[A-Z]{3}[0-9]{5}[0-9A-Z]{3}") |
| 35 | if not p.match(doc.gstin): |
| 36 | frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the GSTIN format for UIN Holders or Non-Resident OIDAR Service Providers")) |
| 37 | else: |
| 38 | 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}$") |
| 39 | if not p.match(doc.gstin): |
| 40 | 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] | 41 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 42 | validate_gstin_check_digit(doc.gstin) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 43 | set_gst_state_and_state_number(doc) |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 44 | |
Deepesh Garg | 459155f | 2019-06-14 12:01:34 +0530 | [diff] [blame] | 45 | if doc.gst_state_number != doc.gstin[:2]: |
| 46 | frappe.throw(_("Invalid GSTIN! First 2 digits of GSTIN should match with State number {0}.") |
| 47 | .format(doc.gst_state_number)) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 49 | def set_gst_state_and_state_number(doc): |
| 50 | if not doc.gst_state: |
| 51 | if not doc.state: |
| 52 | return |
| 53 | state = doc.state.lower() |
| 54 | states_lowercase = {s.lower():s for s in states} |
| 55 | if state in states_lowercase: |
| 56 | doc.gst_state = states_lowercase[state] |
| 57 | else: |
| 58 | return |
| 59 | |
| 60 | doc.gst_state_number = state_numbers[doc.gst_state] |
| 61 | |
| 62 | def validate_gstin_check_digit(gstin, label='GSTIN'): |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 63 | ''' Function to validate the check digit of the GSTIN.''' |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 64 | factor = 1 |
| 65 | total = 0 |
| 66 | code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 67 | mod = len(code_point_chars) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 68 | input_chars = gstin[:-1] |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 69 | for char in input_chars: |
| 70 | digit = factor * code_point_chars.find(char) |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 71 | digit = (digit // mod) + (digit % mod) |
karthikeyan5 | 2825b92 | 2019-01-09 19:15:10 +0530 | [diff] [blame] | 72 | total += digit |
| 73 | factor = 2 if factor == 1 else 1 |
Sagar Vora | 07cf4e8 | 2019-01-10 11:07:51 +0530 | [diff] [blame] | 74 | if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 75 | frappe.throw(_("Invalid {0}! The check digit validation has failed. " + |
| 76 | "Please ensure you've typed the {0} correctly.".format(label))) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 77 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 78 | def get_itemised_tax_breakup_header(item_doctype, tax_accounts): |
| 79 | if frappe.get_meta(item_doctype).has_field('gst_hsn_code'): |
| 80 | return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts |
| 81 | else: |
| 82 | return [_("Item"), _("Taxable Amount")] + tax_accounts |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 83 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 84 | def get_itemised_tax_breakup_data(doc, account_wise=False): |
| 85 | itemised_tax = get_itemised_tax(doc.taxes, with_tax_account=account_wise) |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 86 | |
| 87 | itemised_taxable_amount = get_itemised_taxable_amount(doc.items) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 88 | |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 89 | if not frappe.get_meta(doc.doctype + " Item").has_field('gst_hsn_code'): |
| 90 | return itemised_tax, itemised_taxable_amount |
| 91 | |
| 92 | item_hsn_map = frappe._dict() |
| 93 | for d in doc.items: |
| 94 | item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code")) |
| 95 | |
| 96 | hsn_tax = {} |
| 97 | for item, taxes in itemised_tax.items(): |
| 98 | hsn_code = item_hsn_map.get(item) |
| 99 | hsn_tax.setdefault(hsn_code, frappe._dict()) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 100 | for tax_desc, tax_detail in taxes.items(): |
| 101 | key = tax_desc |
| 102 | if account_wise: |
| 103 | key = tax_detail.get('tax_account') |
| 104 | hsn_tax[hsn_code].setdefault(key, {"tax_rate": 0, "tax_amount": 0}) |
| 105 | hsn_tax[hsn_code][key]["tax_rate"] = tax_detail.get("tax_rate") |
| 106 | hsn_tax[hsn_code][key]["tax_amount"] += tax_detail.get("tax_amount") |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 107 | |
| 108 | # set taxable amount |
| 109 | hsn_taxable_amount = frappe._dict() |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 110 | for item in itemised_taxable_amount: |
Nabin Hait | b962fc1 | 2017-07-17 18:02:31 +0530 | [diff] [blame] | 111 | hsn_code = item_hsn_map.get(item) |
| 112 | hsn_taxable_amount.setdefault(hsn_code, 0) |
| 113 | hsn_taxable_amount[hsn_code] += itemised_taxable_amount.get(item) |
| 114 | |
| 115 | return hsn_tax, hsn_taxable_amount |
| 116 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 117 | def set_place_of_supply(doc, method=None): |
| 118 | doc.place_of_supply = get_place_of_supply(doc, doc.doctype) |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 119 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 120 | # don't remove this function it is used in tests |
| 121 | def test_method(): |
| 122 | '''test function''' |
Nabin Hait | b95ecd7 | 2018-02-16 13:19:04 +0530 | [diff] [blame] | 123 | return 'overridden' |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 124 | |
| 125 | def get_place_of_supply(out, doctype): |
| 126 | if not frappe.get_meta('Address').has_field('gst_state'): return |
| 127 | |
| 128 | if doctype in ("Sales Invoice", "Delivery Note"): |
| 129 | address_name = out.shipping_address_name or out.customer_address |
| 130 | elif doctype == "Purchase Invoice": |
| 131 | address_name = out.shipping_address or out.supplier_address |
| 132 | |
| 133 | if address_name: |
| 134 | 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] | 135 | if address and address.gst_state and address.gst_state_number: |
Nabin Hait | 2390da6 | 2018-08-30 16:16:35 +0530 | [diff] [blame] | 136 | return cstr(address.gst_state_number) + "-" + cstr(address.gst_state) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 137 | |
| 138 | def get_regional_address_details(out, doctype, company): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 139 | out.place_of_supply = get_place_of_supply(out, doctype) |
| 140 | |
| 141 | if not out.place_of_supply: return |
| 142 | |
| 143 | if doctype in ("Sales Invoice", "Delivery Note"): |
| 144 | master_doctype = "Sales Taxes and Charges Template" |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 145 | if not out.company_gstin: |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 146 | return |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 147 | elif doctype == "Purchase Invoice": |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 148 | master_doctype = "Purchase Taxes and Charges Template" |
Shreya | 2ad8172 | 2018-06-05 16:49:29 +0530 | [diff] [blame] | 149 | if not out.supplier_gstin: |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 150 | return |
| 151 | |
Rohit Waghchaure | de82ad1 | 2018-06-07 13:20:57 +0530 | [diff] [blame] | 152 | if ((doctype in ("Sales Invoice", "Delivery Note") and out.company_gstin |
| 153 | and out.company_gstin[:2] != out.place_of_supply[:2]) or (doctype == "Purchase Invoice" |
| 154 | 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] | 155 | default_tax = frappe.db.get_value(master_doctype, {"company": company, "is_inter_state":1, "disabled":0}) |
| 156 | else: |
| 157 | default_tax = frappe.db.get_value(master_doctype, {"company": company, "disabled":0, "is_default": 1}) |
| 158 | |
| 159 | if not default_tax: |
| 160 | return |
| 161 | out["taxes_and_charges"] = default_tax |
| 162 | out.taxes = get_taxes_and_charges(master_doctype, default_tax) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 163 | |
| 164 | def calculate_annual_eligible_hra_exemption(doc): |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 165 | basic_component = frappe.get_cached_value('Company', doc.company, "basic_component") |
| 166 | hra_component = frappe.get_cached_value('Company', doc.company, "hra_component") |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 167 | if not (basic_component and hra_component): |
| 168 | frappe.throw(_("Please mention Basic and HRA component in Company")) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 169 | annual_exemption, monthly_exemption, hra_amount = 0, 0, 0 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 170 | if hra_component and basic_component: |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 171 | assignment = get_salary_assignment(doc.employee, nowdate()) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 172 | if assignment: |
| 173 | hra_component_exists = frappe.db.exists("Salary Detail", { |
| 174 | "parent": assignment.salary_structure, |
| 175 | "salary_component": hra_component, |
| 176 | "parentfield": "earnings", |
| 177 | "parenttype": "Salary Structure" |
| 178 | }) |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 179 | |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 180 | if hra_component_exists: |
| 181 | basic_amount, hra_amount = get_component_amt_from_salary_slip(doc.employee, |
| 182 | assignment.salary_structure, basic_component, hra_component) |
| 183 | if hra_amount: |
| 184 | if doc.monthly_house_rent: |
| 185 | annual_exemption = calculate_hra_exemption(assignment.salary_structure, |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 186 | basic_amount, hra_amount, doc.monthly_house_rent, doc.rented_in_metro_city) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 187 | if annual_exemption > 0: |
| 188 | monthly_exemption = annual_exemption / 12 |
| 189 | else: |
| 190 | annual_exemption = 0 |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 191 | |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 192 | elif doc.docstatus == 1: |
| 193 | frappe.throw(_("Salary Structure must be submitted before submission of Tax Ememption Declaration")) |
| 194 | |
| 195 | return frappe._dict({ |
| 196 | "hra_amount": hra_amount, |
| 197 | "annual_exemption": annual_exemption, |
| 198 | "monthly_exemption": monthly_exemption |
| 199 | }) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 200 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 201 | def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component): |
Nabin Hait | 6b9d64c | 2019-05-16 11:23:04 +0530 | [diff] [blame] | 202 | salary_slip = make_salary_slip(salary_structure, employee=employee, for_preview=1) |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 203 | basic_amt, hra_amt = 0, 0 |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 204 | for earning in salary_slip.earnings: |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 205 | if earning.salary_component == basic_component: |
| 206 | basic_amt = earning.amount |
| 207 | elif earning.salary_component == hra_component: |
| 208 | hra_amt = earning.amount |
| 209 | if basic_amt and hra_amt: |
| 210 | return basic_amt, hra_amt |
Ranjith Kurungadam | 14e94f8 | 2018-07-16 16:12:46 +0530 | [diff] [blame] | 211 | return basic_amt, hra_amt |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 212 | |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 213 | 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] | 214 | # TODO make this configurable |
| 215 | exemptions = [] |
| 216 | frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency") |
| 217 | # case 1: The actual amount allotted by the employer as the HRA. |
| 218 | exemptions.append(get_annual_component_pay(frequency, monthly_hra)) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 219 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 220 | actual_annual_rent = monthly_house_rent * 12 |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 221 | annual_basic = get_annual_component_pay(frequency, basic) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 222 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 223 | # case 2: Actual rent paid less 10% of the basic salary. |
Ranjith Kurungadam | b1a756c | 2018-07-01 16:42:38 +0530 | [diff] [blame] | 224 | exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1)) |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 225 | # 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] | 226 | 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] | 227 | # return minimum of 3 cases |
| 228 | return min(exemptions) |
| 229 | |
| 230 | def get_annual_component_pay(frequency, amount): |
| 231 | if frequency == "Daily": |
| 232 | return amount * 365 |
| 233 | elif frequency == "Weekly": |
| 234 | return amount * 52 |
| 235 | elif frequency == "Fortnightly": |
| 236 | return amount * 26 |
| 237 | elif frequency == "Monthly": |
| 238 | return amount * 12 |
| 239 | elif frequency == "Bimonthly": |
| 240 | return amount * 6 |
| 241 | |
| 242 | def validate_house_rent_dates(doc): |
| 243 | if not doc.rented_to_date or not doc.rented_from_date: |
| 244 | frappe.throw(_("House rented dates required for exemption calculation")) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 245 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 246 | if date_diff(doc.rented_to_date, doc.rented_from_date) < 14: |
| 247 | frappe.throw(_("House rented dates should be atleast 15 days apart")) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 248 | |
| 249 | proofs = frappe.db.sql(""" |
| 250 | select name |
| 251 | from `tabEmployee Tax Exemption Proof Submission` |
| 252 | where |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 253 | docstatus=1 and employee=%(employee)s and payroll_period=%(payroll_period)s |
| 254 | and (rented_from_date between %(from_date)s and %(to_date)s or rented_to_date between %(from_date)s and %(to_date)s) |
| 255 | """, { |
| 256 | "employee": doc.employee, |
| 257 | "payroll_period": doc.payroll_period, |
| 258 | "from_date": doc.rented_from_date, |
| 259 | "to_date": doc.rented_to_date |
| 260 | }) |
Nabin Hait | 04e7bf4 | 2019-04-25 18:44:10 +0530 | [diff] [blame] | 261 | |
Ranjith Kurungadam | a8e047a | 2018-06-14 17:56:16 +0530 | [diff] [blame] | 262 | if proofs: |
Nabin Hait | 49446ba | 2019-04-25 19:54:20 +0530 | [diff] [blame] | 263 | 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] | 264 | |
| 265 | def calculate_hra_exemption_for_period(doc): |
| 266 | monthly_rent, eligible_hra = 0, 0 |
| 267 | if doc.house_rent_payment_amount: |
| 268 | validate_house_rent_dates(doc) |
| 269 | # TODO receive rented months or validate dates are start and end of months? |
| 270 | # Calc monthly rent, round to nearest .5 |
| 271 | factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1)/30 |
| 272 | factor = round(factor * 2)/2 |
| 273 | monthly_rent = doc.house_rent_payment_amount / factor |
| 274 | # update field used by calculate_annual_eligible_hra_exemption |
| 275 | doc.monthly_house_rent = monthly_rent |
| 276 | exemptions = calculate_annual_eligible_hra_exemption(doc) |
| 277 | |
| 278 | if exemptions["monthly_exemption"]: |
| 279 | # calc total exemption amount |
| 280 | eligible_hra = exemptions["monthly_exemption"] * factor |
Ranjith Kurungadam | 4f9744a | 2018-06-20 11:10:56 +0530 | [diff] [blame] | 281 | exemptions["monthly_house_rent"] = monthly_rent |
| 282 | exemptions["total_eligible_hra_exemption"] = eligible_hra |
| 283 | return exemptions |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 284 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 285 | def get_ewb_data(dt, dn): |
| 286 | if dt != 'Sales Invoice': |
| 287 | frappe.throw(_('e-Way Bill JSON can only be generated from Sales Invoice')) |
| 288 | |
| 289 | dn = dn.split(',') |
| 290 | |
| 291 | ewaybills = [] |
| 292 | for doc_name in dn: |
| 293 | doc = frappe.get_doc(dt, doc_name) |
| 294 | |
| 295 | validate_sales_invoice(doc) |
| 296 | |
| 297 | data = frappe._dict({ |
| 298 | "transporterId": "", |
| 299 | "TotNonAdvolVal": 0, |
| 300 | }) |
| 301 | |
| 302 | data.userGstin = data.fromGstin = doc.company_gstin |
| 303 | data.supplyType = 'O' |
| 304 | |
| 305 | if doc.gst_category in ['Registered Regular', 'SEZ']: |
| 306 | data.subSupplyType = 1 |
| 307 | elif doc.gst_category in ['Overseas', 'Deemed Export']: |
| 308 | data.subSupplyType = 3 |
| 309 | else: |
| 310 | frappe.throw(_('Unsupported GST Category for e-Way Bill JSON generation')) |
| 311 | |
| 312 | data.docType = 'INV' |
| 313 | data.docDate = frappe.utils.formatdate(doc.posting_date, 'dd/mm/yyyy') |
| 314 | |
| 315 | company_address = frappe.get_doc('Address', doc.company_address) |
| 316 | billing_address = frappe.get_doc('Address', doc.customer_address) |
| 317 | |
| 318 | shipping_address = frappe.get_doc('Address', doc.shipping_address_name) |
| 319 | |
| 320 | data = get_address_details(data, doc, company_address, billing_address) |
| 321 | |
| 322 | data.itemList = [] |
| 323 | data.totalValue = doc.total |
| 324 | |
| 325 | data = get_item_list(data, doc) |
| 326 | |
| 327 | disable_rounded = frappe.db.get_single_value('Global Defaults', 'disable_rounded_total') |
| 328 | data.totInvValue = doc.grand_total if disable_rounded else doc.rounded_total |
| 329 | |
| 330 | data = get_transport_details(data, doc) |
| 331 | |
| 332 | fields = { |
| 333 | "/. -": { |
| 334 | 'docNo': doc.name, |
| 335 | 'fromTrdName': doc.company, |
| 336 | 'toTrdName': doc.customer_name, |
| 337 | 'transDocNo': doc.lr_no, |
| 338 | }, |
| 339 | "@#/,&. -": { |
| 340 | 'fromAddr1': company_address.address_line1, |
| 341 | 'fromAddr2': company_address.address_line2, |
| 342 | 'fromPlace': company_address.city, |
| 343 | 'toAddr1': shipping_address.address_line1, |
| 344 | 'toAddr2': shipping_address.address_line2, |
| 345 | 'toPlace': shipping_address.city, |
| 346 | 'transporterName': doc.transporter_name |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | for allowed_chars, field_map in fields.items(): |
| 351 | for key, value in field_map.items(): |
| 352 | if not value: |
| 353 | data[key] = '' |
| 354 | else: |
| 355 | data[key] = re.sub(r'[^\w' + allowed_chars + ']', '', value) |
| 356 | |
| 357 | ewaybills.append(data) |
| 358 | |
| 359 | data = { |
| 360 | 'version': '1.0.1118', |
| 361 | 'billLists': ewaybills |
| 362 | } |
| 363 | |
| 364 | return data |
| 365 | |
| 366 | @frappe.whitelist() |
| 367 | def generate_ewb_json(dt, dn): |
| 368 | |
| 369 | data = get_ewb_data(dt, dn) |
| 370 | |
| 371 | frappe.local.response.filecontent = json.dumps(data, indent=4, sort_keys=True) |
| 372 | frappe.local.response.type = 'download' |
| 373 | |
| 374 | if len(data['billLists']) > 1: |
| 375 | doc_name = 'Bulk' |
| 376 | else: |
| 377 | doc_name = dn |
| 378 | |
| 379 | frappe.local.response.filename = '{0}_e-WayBill_Data_{1}.json'.format(doc_name, frappe.utils.random_string(5)) |
| 380 | |
Prasann Shah | 829172c | 2019-06-06 12:08:09 +0530 | [diff] [blame] | 381 | @frappe.whitelist() |
| 382 | def get_gstins_for_company(company): |
| 383 | company_gstins =[] |
| 384 | if company: |
| 385 | company_gstins = frappe.db.sql("""select |
| 386 | distinct `tabAddress`.gstin |
| 387 | from |
| 388 | `tabAddress`, `tabDynamic Link` |
| 389 | where |
| 390 | `tabDynamic Link`.parent = `tabAddress`.name and |
| 391 | `tabDynamic Link`.parenttype = 'Address' and |
| 392 | `tabDynamic Link`.link_doctype = 'Company' and |
| 393 | `tabDynamic Link`.link_name = '{0}'""".format(company)) |
| 394 | return company_gstins |
| 395 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 396 | def get_address_details(data, doc, company_address, billing_address): |
| 397 | data.fromPincode = validate_pincode(company_address.pincode, 'Company Address') |
| 398 | data.fromStateCode = data.actualFromStateCode = validate_state_code( |
| 399 | company_address.gst_state_number, 'Company Address') |
| 400 | |
| 401 | if not doc.billing_address_gstin or len(doc.billing_address_gstin) < 15: |
| 402 | data.toGstin = 'URP' |
| 403 | set_gst_state_and_state_number(billing_address) |
| 404 | else: |
| 405 | data.toGstin = doc.billing_address_gstin |
| 406 | |
| 407 | data.toPincode = validate_pincode(billing_address.pincode, 'Customer Address') |
| 408 | data.toStateCode = validate_state_code(billing_address.gst_state_number, 'Customer Address') |
| 409 | |
| 410 | if doc.customer_address != doc.shipping_address_name: |
| 411 | data.transType = 2 |
| 412 | shipping_address = frappe.get_doc('Address', doc.shipping_address_name) |
| 413 | set_gst_state_and_state_number(shipping_address) |
| 414 | data.toPincode = validate_pincode(shipping_address.pincode, 'Shipping Address') |
| 415 | data.actualToStateCode = validate_state_code(shipping_address.gst_state_number, 'Shipping Address') |
| 416 | else: |
| 417 | data.transType = 1 |
| 418 | data.actualToStateCode = data.toStateCode |
| 419 | shipping_address = billing_address |
| 420 | |
| 421 | return data |
| 422 | |
| 423 | def get_item_list(data, doc): |
| 424 | for attr in ['cgstValue', 'sgstValue', 'igstValue', 'cessValue', 'OthValue']: |
| 425 | data[attr] = 0 |
| 426 | |
| 427 | gst_accounts = get_gst_accounts(doc.company, account_wise=True) |
| 428 | tax_map = { |
| 429 | 'sgst_account': ['sgstRate', 'sgstValue'], |
| 430 | 'cgst_account': ['cgstRate', 'cgstValue'], |
| 431 | 'igst_account': ['igstRate', 'igstValue'], |
| 432 | 'cess_account': ['cessRate', 'cessValue'] |
| 433 | } |
| 434 | item_data_attrs = ['sgstRate', 'cgstRate', 'igstRate', 'cessRate', 'cessNonAdvol'] |
| 435 | hsn_wise_charges, hsn_taxable_amount = get_itemised_tax_breakup_data(doc, account_wise=True) |
| 436 | for hsn_code, taxable_amount in hsn_taxable_amount.items(): |
| 437 | item_data = frappe._dict() |
| 438 | if not hsn_code: |
| 439 | frappe.throw(_('GST HSN Code does not exist for one or more items')) |
| 440 | item_data.hsnCode = int(hsn_code) |
| 441 | item_data.taxableAmount = taxable_amount |
| 442 | item_data.qtyUnit = "" |
| 443 | for attr in item_data_attrs: |
| 444 | item_data[attr] = 0 |
| 445 | |
| 446 | for account, tax_detail in hsn_wise_charges.get(hsn_code, {}).items(): |
| 447 | account_type = gst_accounts.get(account, '') |
| 448 | for tax_acc, attrs in tax_map.items(): |
| 449 | if account_type == tax_acc: |
| 450 | item_data[attrs[0]] = tax_detail.get('tax_rate') |
| 451 | data[attrs[1]] += tax_detail.get('tax_amount') |
| 452 | break |
| 453 | else: |
| 454 | data.OthValue += tax_detail.get('tax_amount') |
| 455 | |
| 456 | data.itemList.append(item_data) |
| 457 | |
| 458 | # Tax amounts rounded to 2 decimals to avoid exceeding max character limit |
| 459 | for attr in ['sgstValue', 'cgstValue', 'igstValue', 'cessValue']: |
| 460 | data[attr] = flt(data[attr], 2) |
| 461 | |
| 462 | return data |
| 463 | |
| 464 | def validate_sales_invoice(doc): |
| 465 | if doc.docstatus != 1: |
| 466 | frappe.throw(_('e-Way Bill JSON can only be generated from submitted document')) |
| 467 | |
| 468 | if doc.is_return: |
| 469 | frappe.throw(_('e-Way Bill JSON cannot be generated for Sales Return as of now')) |
| 470 | |
| 471 | if doc.ewaybill: |
| 472 | frappe.throw(_('e-Way Bill already exists for this document')) |
| 473 | |
| 474 | reqd_fields = ['company_gstin', 'company_address', 'customer_address', |
| 475 | 'shipping_address_name', 'mode_of_transport', 'distance'] |
| 476 | |
| 477 | for fieldname in reqd_fields: |
| 478 | if not doc.get(fieldname): |
| 479 | frappe.throw(_('{} is required to generate e-Way Bill JSON'.format( |
| 480 | doc.meta.get_label(fieldname) |
| 481 | ))) |
| 482 | |
| 483 | if len(doc.company_gstin) < 15: |
| 484 | frappe.throw(_('You must be a registered supplier to generate e-Way Bill')) |
| 485 | |
| 486 | def get_transport_details(data, doc): |
| 487 | if doc.distance > 4000: |
| 488 | frappe.throw(_('Distance cannot be greater than 4000 kms')) |
| 489 | |
| 490 | data.transDistance = int(round(doc.distance)) |
| 491 | |
| 492 | transport_modes = { |
| 493 | 'Road': 1, |
| 494 | 'Rail': 2, |
| 495 | 'Air': 3, |
| 496 | 'Ship': 4 |
| 497 | } |
| 498 | |
| 499 | vehicle_types = { |
| 500 | 'Regular': 'R', |
| 501 | 'Over Dimensional Cargo (ODC)': 'O' |
| 502 | } |
| 503 | |
| 504 | data.transMode = transport_modes.get(doc.mode_of_transport) |
| 505 | |
| 506 | if doc.mode_of_transport == 'Road': |
| 507 | if not doc.gst_transporter_id and not doc.vehicle_no: |
| 508 | frappe.throw(_('Either GST Transporter ID or Vehicle No is required if Mode of Transport is Road')) |
| 509 | if doc.vehicle_no: |
| 510 | data.vehicleNo = doc.vehicle_no.replace(' ', '') |
| 511 | if not doc.gst_vehicle_type: |
| 512 | frappe.throw(_('Vehicle Type is required if Mode of Transport is Road')) |
| 513 | else: |
| 514 | data.vehicleType = vehicle_types.get(doc.gst_vehicle_type) |
| 515 | else: |
| 516 | if not doc.lr_no or not doc.lr_date: |
| 517 | frappe.throw(_('Transport Receipt No and Date are mandatory for your chosen Mode of Transport')) |
| 518 | |
| 519 | if doc.lr_no: |
| 520 | data.transDocNo = doc.lr_no |
| 521 | |
| 522 | if doc.lr_date: |
| 523 | data.transDocDate = frappe.utils.formatdate(doc.lr_date, 'dd/mm/yyyy') |
| 524 | |
| 525 | if doc.gst_transporter_id: |
| 526 | validate_gstin_check_digit(doc.gst_transporter_id, label='GST Transporter ID') |
| 527 | data.transporterId = doc.gst_transporter_id |
| 528 | |
| 529 | return data |
| 530 | |
| 531 | |
| 532 | def validate_pincode(pincode, address): |
| 533 | pin_not_found = "Pin Code doesn't exist for {}" |
| 534 | incorrect_pin = "Pin Code for {} is incorrecty formatted. It must be 6 digits (without spaces)" |
| 535 | |
| 536 | if not pincode: |
| 537 | frappe.throw(_(pin_not_found.format(address))) |
| 538 | |
| 539 | pincode = pincode.replace(' ', '') |
| 540 | if not pincode.isdigit() or len(pincode) != 6: |
| 541 | frappe.throw(_(incorrect_pin.format(address))) |
| 542 | else: |
| 543 | return int(pincode) |
| 544 | |
| 545 | def validate_state_code(state_code, address): |
| 546 | no_state_code = "GST State Code not found for {0}. Please set GST State in {0}" |
| 547 | if not state_code: |
| 548 | frappe.throw(_(no_state_code.format(address))) |
| 549 | else: |
| 550 | return int(state_code) |
| 551 | |
| 552 | def get_gst_accounts(company, account_wise=False): |
| 553 | gst_accounts = frappe._dict() |
| 554 | gst_settings_accounts = frappe.get_all("GST Account", |
| 555 | filters={"parent": "GST Settings", "company": company}, |
| 556 | fields=["cgst_account", "sgst_account", "igst_account", "cess_account"]) |
| 557 | |
| 558 | if not gst_settings_accounts: |
| 559 | frappe.throw(_("Please set GST Accounts in GST Settings")) |
| 560 | |
| 561 | for d in gst_settings_accounts: |
| 562 | for acc, val in d.items(): |
| 563 | if not account_wise: |
| 564 | gst_accounts.setdefault(acc, []).append(val) |
| 565 | elif val: |
| 566 | gst_accounts[val] = acc |
| 567 | |
| 568 | |
| 569 | return gst_accounts |