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