2021-09-02 11:14:59 +00:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
import frappe
|
2017-06-21 11:52:38 +00:00
|
|
|
from frappe import _
|
2021-09-02 11:14:59 +00:00
|
|
|
from frappe.model.utils import get_fetch_values
|
|
|
|
from frappe.utils import cint, cstr, date_diff, flt, getdate, nowdate
|
|
|
|
|
2018-06-05 05:57:53 +00:00
|
|
|
from erpnext.controllers.accounts_controller import get_taxes_and_charges
|
2021-09-02 11:14:59 +00:00
|
|
|
from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
|
2018-06-14 12:26:16 +00:00
|
|
|
from erpnext.hr.utils import get_salary_assignment
|
2020-06-19 13:47:57 +00:00
|
|
|
from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip
|
2021-09-02 11:14:59 +00:00
|
|
|
from erpnext.regional.india import number_state_mapping, state_numbers, states
|
2021-03-03 09:26:19 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
GST_INVOICE_NUMBER_FORMAT = re.compile(r"^[a-zA-Z0-9\-/]+$") # alphanumeric and - /
|
|
|
|
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}$"
|
|
|
|
)
|
2021-03-03 09:26:19 +00:00
|
|
|
GSTIN_UIN_FORMAT = re.compile("^[0-9]{4}[A-Z]{3}[0-9]{5}[0-9A-Z]{3}")
|
|
|
|
PAN_NUMBER_FORMAT = re.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}")
|
|
|
|
|
|
|
|
|
2017-06-21 11:52:38 +00:00
|
|
|
def validate_gstin_for_india(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
if hasattr(doc, "gst_state"):
|
2022-03-21 10:58:17 +00:00
|
|
|
set_gst_state_and_state_number(doc)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not hasattr(doc, "gstin") or not doc.gstin:
|
2017-06-21 11:52:38 +00:00
|
|
|
return
|
|
|
|
|
2019-06-14 06:31:34 +00:00
|
|
|
gst_category = []
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if hasattr(doc, "gst_category"):
|
2021-10-22 13:56:31 +00:00
|
|
|
if len(doc.links):
|
|
|
|
link_doctype = doc.links[0].get("link_doctype")
|
|
|
|
link_name = doc.links[0].get("link_name")
|
2019-06-14 06:31:34 +00:00
|
|
|
|
2021-10-22 13:56:31 +00:00
|
|
|
if link_doctype in ["Customer", "Supplier"]:
|
2022-03-28 13:22:46 +00:00
|
|
|
gst_category = frappe.db.get_value(link_doctype, {"name": link_name}, ["gst_category"])
|
2019-06-14 06:31:34 +00:00
|
|
|
|
2019-01-23 09:10:01 +00:00
|
|
|
doc.gstin = doc.gstin.upper().strip()
|
2022-03-28 13:22:46 +00:00
|
|
|
if not doc.gstin or doc.gstin == "NA":
|
2019-01-10 05:37:51 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if len(doc.gstin) != 15:
|
2021-04-12 12:25:46 +00:00
|
|
|
frappe.throw(_("A GSTIN must have 15 characters."), title=_("Invalid GSTIN"))
|
2019-01-10 05:37:51 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if gst_category and gst_category == "UIN Holders":
|
2021-03-03 09:26:19 +00:00
|
|
|
if not GSTIN_UIN_FORMAT.match(doc.gstin):
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_(
|
|
|
|
"The input you've entered doesn't match the GSTIN format for UIN Holders or Non-Resident OIDAR Service Providers"
|
|
|
|
),
|
|
|
|
title=_("Invalid GSTIN"),
|
|
|
|
)
|
2019-06-14 06:31:34 +00:00
|
|
|
else:
|
2021-03-03 09:26:19 +00:00
|
|
|
if not GSTIN_FORMAT.match(doc.gstin):
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("The input you've entered doesn't match the format of GSTIN."), title=_("Invalid GSTIN")
|
|
|
|
)
|
2017-06-21 11:52:38 +00:00
|
|
|
|
2019-06-14 06:31:34 +00:00
|
|
|
validate_gstin_check_digit(doc.gstin)
|
2017-06-21 11:52:38 +00:00
|
|
|
|
2021-01-13 08:31:57 +00:00
|
|
|
if not doc.gst_state:
|
2021-04-12 12:25:46 +00:00
|
|
|
frappe.throw(_("Please enter GST state"), title=_("Invalid State"))
|
2021-01-13 08:31:57 +00:00
|
|
|
|
2019-06-14 06:31:34 +00:00
|
|
|
if doc.gst_state_number != doc.gstin[:2]:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("First 2 digits of GSTIN should match with State number {0}.").format(doc.gst_state_number),
|
|
|
|
title=_("Invalid GSTIN"),
|
|
|
|
)
|
|
|
|
|
2019-01-09 13:45:10 +00:00
|
|
|
|
2021-02-22 13:58:45 +00:00
|
|
|
def validate_pan_for_india(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.get("country") != "India" or not doc.get("pan"):
|
2021-02-22 13:58:45 +00:00
|
|
|
return
|
|
|
|
|
2021-03-03 09:26:19 +00:00
|
|
|
if not PAN_NUMBER_FORMAT.match(doc.pan):
|
2021-02-22 13:58:45 +00:00
|
|
|
frappe.throw(_("Invalid PAN No. The input you've entered doesn't match the format of PAN."))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-11-24 02:39:17 +00:00
|
|
|
def validate_tax_category(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.get("gst_state") and frappe.db.get_value(
|
|
|
|
"Tax Category",
|
|
|
|
{
|
|
|
|
"gst_state": doc.gst_state,
|
|
|
|
"is_inter_state": doc.is_inter_state,
|
|
|
|
"is_reverse_charge": doc.is_reverse_charge,
|
|
|
|
},
|
|
|
|
):
|
2020-11-24 02:39:17 +00:00
|
|
|
if doc.is_inter_state:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Inter State tax category for GST State {0} already exists").format(doc.gst_state)
|
|
|
|
)
|
2020-11-24 02:39:17 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Intra State tax category for GST State {0} already exists").format(doc.gst_state)
|
|
|
|
)
|
|
|
|
|
2020-11-24 02:39:17 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
def update_gst_category(doc, method):
|
2021-11-19 05:33:13 +00:00
|
|
|
for link in doc.links:
|
2022-03-28 13:22:46 +00:00
|
|
|
if link.link_doctype in ["Customer", "Supplier"]:
|
2021-11-19 05:33:13 +00:00
|
|
|
meta = frappe.get_meta(link.link_doctype)
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.get("gstin") and meta.has_field("gst_category"):
|
|
|
|
frappe.db.set_value(
|
|
|
|
link.link_doctype,
|
|
|
|
{"name": link.link_name, "gst_category": "Unregistered"},
|
|
|
|
"gst_category",
|
|
|
|
"Registered Regular",
|
|
|
|
)
|
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
def set_gst_state_and_state_number(doc):
|
2022-03-21 10:58:17 +00:00
|
|
|
if not doc.gst_state and doc.state:
|
2019-07-03 05:04:31 +00:00
|
|
|
state = doc.state.lower()
|
2022-03-28 13:22:46 +00:00
|
|
|
states_lowercase = {s.lower(): s for s in states}
|
2019-07-03 05:04:31 +00:00
|
|
|
if state in states_lowercase:
|
|
|
|
doc.gst_state = states_lowercase[state]
|
|
|
|
else:
|
|
|
|
return
|
2022-03-21 10:58:17 +00:00
|
|
|
doc.gst_state_number = state_numbers.get(doc.gst_state)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def validate_gstin_check_digit(gstin, label="GSTIN"):
|
|
|
|
"""Function to validate the check digit of the GSTIN."""
|
2019-01-09 13:45:10 +00:00
|
|
|
factor = 1
|
|
|
|
total = 0
|
2022-03-28 13:22:46 +00:00
|
|
|
code_point_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2019-01-09 13:45:10 +00:00
|
|
|
mod = len(code_point_chars)
|
2019-01-10 05:37:51 +00:00
|
|
|
input_chars = gstin[:-1]
|
2019-01-09 13:45:10 +00:00
|
|
|
for char in input_chars:
|
|
|
|
digit = factor * code_point_chars.find(char)
|
2019-01-10 05:37:51 +00:00
|
|
|
digit = (digit // mod) + (digit % mod)
|
2019-01-09 13:45:10 +00:00
|
|
|
total += digit
|
|
|
|
factor = 2 if factor == 1 else 1
|
2019-01-10 05:37:51 +00:00
|
|
|
if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_(
|
|
|
|
"""Invalid {0}! The check digit validation has failed. Please ensure you've typed the {0} correctly."""
|
|
|
|
).format(label)
|
|
|
|
)
|
|
|
|
|
2017-07-13 09:30:56 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
|
2022-03-28 13:22:46 +00:00
|
|
|
hsn_wise_in_gst_settings = frappe.db.get_single_value("GST Settings", "hsn_wise_tax_breakup")
|
|
|
|
if frappe.get_meta(item_doctype).has_field("gst_hsn_code") and hsn_wise_in_gst_settings:
|
2021-10-11 12:03:41 +00:00
|
|
|
return [_("HSN/SAC"), _("Taxable Amount")] + tax_accounts
|
|
|
|
else:
|
|
|
|
return [_("Item"), _("Taxable Amount")] + tax_accounts
|
2018-02-16 07:49:04 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-09-17 05:09:03 +00:00
|
|
|
def get_itemised_tax_breakup_data(doc, account_wise=False, hsn_wise=False):
|
2019-07-03 05:04:31 +00:00
|
|
|
itemised_tax = get_itemised_tax(doc.taxes, with_tax_account=account_wise)
|
2017-07-17 12:32:31 +00:00
|
|
|
|
|
|
|
itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
|
2018-02-16 07:49:04 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not frappe.get_meta(doc.doctype + " Item").has_field("gst_hsn_code"):
|
2017-07-17 12:32:31 +00:00
|
|
|
return itemised_tax, itemised_taxable_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
hsn_wise_in_gst_settings = frappe.db.get_single_value("GST Settings", "hsn_wise_tax_breakup")
|
2021-10-11 12:03:41 +00:00
|
|
|
|
|
|
|
tax_breakup_hsn_wise = hsn_wise or hsn_wise_in_gst_settings
|
|
|
|
if tax_breakup_hsn_wise:
|
2021-09-17 05:09:03 +00:00
|
|
|
item_hsn_map = frappe._dict()
|
|
|
|
for d in doc.items:
|
|
|
|
item_hsn_map.setdefault(d.item_code or d.item_name, d.get("gst_hsn_code"))
|
2017-07-17 12:32:31 +00:00
|
|
|
|
|
|
|
hsn_tax = {}
|
|
|
|
for item, taxes in itemised_tax.items():
|
2021-10-11 12:03:41 +00:00
|
|
|
item_or_hsn = item if not tax_breakup_hsn_wise else item_hsn_map.get(item)
|
2021-09-17 05:09:03 +00:00
|
|
|
hsn_tax.setdefault(item_or_hsn, frappe._dict())
|
2019-07-03 05:04:31 +00:00
|
|
|
for tax_desc, tax_detail in taxes.items():
|
|
|
|
key = tax_desc
|
|
|
|
if account_wise:
|
2022-03-28 13:22:46 +00:00
|
|
|
key = tax_detail.get("tax_account")
|
2021-09-17 05:09:03 +00:00
|
|
|
hsn_tax[item_or_hsn].setdefault(key, {"tax_rate": 0, "tax_amount": 0})
|
|
|
|
hsn_tax[item_or_hsn][key]["tax_rate"] = tax_detail.get("tax_rate")
|
|
|
|
hsn_tax[item_or_hsn][key]["tax_amount"] += tax_detail.get("tax_amount")
|
2017-07-17 12:32:31 +00:00
|
|
|
|
|
|
|
# set taxable amount
|
|
|
|
hsn_taxable_amount = frappe._dict()
|
2019-07-03 05:04:31 +00:00
|
|
|
for item in itemised_taxable_amount:
|
2021-10-11 12:03:41 +00:00
|
|
|
item_or_hsn = item if not tax_breakup_hsn_wise else item_hsn_map.get(item)
|
2021-09-17 05:09:03 +00:00
|
|
|
hsn_taxable_amount.setdefault(item_or_hsn, 0)
|
|
|
|
hsn_taxable_amount[item_or_hsn] += itemised_taxable_amount.get(item)
|
2017-07-17 12:32:31 +00:00
|
|
|
|
|
|
|
return hsn_tax, hsn_taxable_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-05 05:57:53 +00:00
|
|
|
def set_place_of_supply(doc, method=None):
|
|
|
|
doc.place_of_supply = get_place_of_supply(doc, doc.doctype)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-03-01 11:42:53 +00:00
|
|
|
def validate_document_name(doc, method=None):
|
|
|
|
"""Validate GST invoice number requirements."""
|
2021-04-13 10:16:01 +00:00
|
|
|
|
2021-03-01 11:42:53 +00:00
|
|
|
country = frappe.get_cached_value("Company", doc.company, "country")
|
|
|
|
|
2021-03-03 09:26:19 +00:00
|
|
|
# Date was chosen as start of next FY to avoid irritating current users.
|
2021-03-01 11:42:53 +00:00
|
|
|
if country != "India" or getdate(doc.posting_date) < getdate("2021-04-01"):
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(doc.name) > 16:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_(
|
|
|
|
"Maximum length of document number should be 16 characters as per GST rules. Please change the naming series."
|
|
|
|
)
|
|
|
|
)
|
2021-03-01 11:42:53 +00:00
|
|
|
|
2021-03-03 09:26:19 +00:00
|
|
|
if not GST_INVOICE_NUMBER_FORMAT.match(doc.name):
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_(
|
|
|
|
"Document name should only contain alphanumeric values, dash(-) and slash(/) characters as per GST rules. Please change the naming series."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-03-01 11:42:53 +00:00
|
|
|
|
2018-06-05 05:57:53 +00:00
|
|
|
# don't remove this function it is used in tests
|
|
|
|
def test_method():
|
2022-03-28 13:22:46 +00:00
|
|
|
"""test function"""
|
|
|
|
return "overridden"
|
|
|
|
|
2018-06-05 05:57:53 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
def get_place_of_supply(party_details, doctype):
|
2022-03-28 13:22:46 +00:00
|
|
|
if not frappe.get_meta("Address").has_field("gst_state"):
|
|
|
|
return
|
2018-02-16 07:49:04 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"):
|
2020-10-30 16:42:24 +00:00
|
|
|
address_name = party_details.customer_address or party_details.shipping_address_name
|
2019-12-10 10:25:05 +00:00
|
|
|
elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"):
|
|
|
|
address_name = party_details.shipping_address or party_details.supplier_address
|
2018-02-16 07:49:04 +00:00
|
|
|
|
|
|
|
if address_name:
|
2022-03-28 13:22:46 +00:00
|
|
|
address = frappe.db.get_value(
|
|
|
|
"Address", address_name, ["gst_state", "gst_state_number", "gstin"], as_dict=1
|
|
|
|
)
|
2018-10-11 05:10:34 +00:00
|
|
|
if address and address.gst_state and address.gst_state_number:
|
2020-02-18 06:58:41 +00:00
|
|
|
party_details.gstin = address.gstin
|
2018-08-30 10:46:35 +00:00
|
|
|
return cstr(address.gst_state_number) + "-" + cstr(address.gst_state)
|
2018-02-16 07:49:04 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
@frappe.whitelist()
|
2020-11-19 14:41:45 +00:00
|
|
|
def get_regional_address_details(party_details, doctype, company):
|
2021-11-04 14:18:32 +00:00
|
|
|
if isinstance(party_details, str):
|
2019-12-10 10:25:05 +00:00
|
|
|
party_details = json.loads(party_details)
|
|
|
|
party_details = frappe._dict(party_details)
|
2018-06-05 05:57:53 +00:00
|
|
|
|
2020-12-04 12:37:46 +00:00
|
|
|
update_party_details(party_details, doctype)
|
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
party_details.place_of_supply = get_place_of_supply(party_details, doctype)
|
2020-02-18 06:58:41 +00:00
|
|
|
|
|
|
|
if is_internal_transfer(party_details, doctype):
|
2022-03-28 13:22:46 +00:00
|
|
|
party_details.taxes_and_charges = ""
|
2021-01-28 07:39:56 +00:00
|
|
|
party_details.taxes = []
|
2020-11-19 06:07:08 +00:00
|
|
|
return party_details
|
2020-02-18 06:58:41 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"):
|
2018-06-05 05:57:53 +00:00
|
|
|
master_doctype = "Sales Taxes and Charges Template"
|
2022-03-28 13:22:46 +00:00
|
|
|
tax_template_by_category = get_tax_template_based_on_category(
|
|
|
|
master_doctype, company, party_details
|
|
|
|
)
|
2019-12-10 10:25:05 +00:00
|
|
|
|
|
|
|
elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"):
|
2018-06-05 05:57:53 +00:00
|
|
|
master_doctype = "Purchase Taxes and Charges Template"
|
2022-03-28 13:22:46 +00:00
|
|
|
tax_template_by_category = get_tax_template_based_on_category(
|
|
|
|
master_doctype, company, party_details
|
|
|
|
)
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2021-12-02 11:47:56 +00:00
|
|
|
if tax_template_by_category:
|
2022-03-28 13:22:46 +00:00
|
|
|
party_details["taxes_and_charges"] = tax_template_by_category
|
2022-01-02 12:23:15 +00:00
|
|
|
return party_details
|
2018-06-05 05:57:53 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not party_details.place_of_supply:
|
|
|
|
return party_details
|
|
|
|
if not party_details.company_gstin:
|
|
|
|
return party_details
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if (
|
|
|
|
doctype in ("Sales Invoice", "Delivery Note", "Sales Order")
|
|
|
|
and party_details.company_gstin
|
|
|
|
and party_details.company_gstin[:2] != party_details.place_of_supply[:2]
|
|
|
|
) or (
|
|
|
|
doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt")
|
|
|
|
and party_details.supplier_gstin
|
|
|
|
and party_details.supplier_gstin[:2] != party_details.place_of_supply[:2]
|
|
|
|
):
|
2019-12-10 10:25:05 +00:00
|
|
|
default_tax = get_tax_template(master_doctype, company, 1, party_details.company_gstin[:2])
|
2018-06-05 05:57:53 +00:00
|
|
|
else:
|
2019-12-10 10:25:05 +00:00
|
|
|
default_tax = get_tax_template(master_doctype, company, 0, party_details.company_gstin[:2])
|
2018-06-05 05:57:53 +00:00
|
|
|
|
|
|
|
if not default_tax:
|
2020-11-19 06:07:08 +00:00
|
|
|
return party_details
|
2021-12-02 11:47:56 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
party_details["taxes_and_charges"] = default_tax
|
|
|
|
party_details.taxes = get_taxes_and_charges(master_doctype, default_tax)
|
|
|
|
|
2020-11-19 06:07:08 +00:00
|
|
|
return party_details
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-12-04 12:37:46 +00:00
|
|
|
def update_party_details(party_details, doctype):
|
2022-03-28 13:22:46 +00:00
|
|
|
for address_field in [
|
|
|
|
"shipping_address",
|
|
|
|
"company_address",
|
|
|
|
"supplier_address",
|
|
|
|
"shipping_address_name",
|
|
|
|
"customer_address",
|
|
|
|
]:
|
2020-12-04 12:37:46 +00:00
|
|
|
if party_details.get(address_field):
|
|
|
|
party_details.update(get_fetch_values(doctype, address_field, party_details.get(address_field)))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-02-18 06:58:41 +00:00
|
|
|
def is_internal_transfer(party_details, doctype):
|
|
|
|
if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"):
|
|
|
|
destination_gstin = party_details.company_gstin
|
|
|
|
elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"):
|
|
|
|
destination_gstin = party_details.supplier_gstin
|
|
|
|
|
2021-09-30 07:58:53 +00:00
|
|
|
if not destination_gstin or party_details.gstin:
|
|
|
|
return False
|
|
|
|
|
2020-02-18 06:58:41 +00:00
|
|
|
if party_details.gstin == destination_gstin:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
False
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
def get_tax_template_based_on_category(master_doctype, company, party_details):
|
2022-03-28 13:22:46 +00:00
|
|
|
if not party_details.get("tax_category"):
|
2019-12-10 10:25:05 +00:00
|
|
|
return
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
default_tax = frappe.db.get_value(
|
|
|
|
master_doctype, {"company": company, "tax_category": party_details.get("tax_category")}, "name"
|
|
|
|
)
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2021-12-02 11:47:56 +00:00
|
|
|
return default_tax
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-12-10 10:25:05 +00:00
|
|
|
def get_tax_template(master_doctype, company, is_inter_state, state_code):
|
2022-03-28 13:22:46 +00:00
|
|
|
tax_categories = frappe.get_all(
|
|
|
|
"Tax Category",
|
|
|
|
fields=["name", "is_inter_state", "gst_state"],
|
|
|
|
filters={"is_inter_state": is_inter_state, "is_reverse_charge": 0},
|
|
|
|
)
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
default_tax = ""
|
2019-12-10 10:25:05 +00:00
|
|
|
|
|
|
|
for tax_category in tax_categories:
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax_category.gst_state == number_state_mapping[state_code] or (
|
|
|
|
not default_tax and not tax_category.gst_state
|
|
|
|
):
|
|
|
|
default_tax = frappe.db.get_value(
|
|
|
|
master_doctype, {"company": company, "disabled": 0, "tax_category": tax_category.name}, "name"
|
|
|
|
)
|
2019-12-10 10:25:05 +00:00
|
|
|
return default_tax
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
def calculate_annual_eligible_hra_exemption(doc):
|
2022-03-28 13:22:46 +00:00
|
|
|
basic_component, hra_component = frappe.db.get_value(
|
|
|
|
"Company", doc.company, ["basic_component", "hra_component"]
|
|
|
|
)
|
2019-04-25 13:14:10 +00:00
|
|
|
if not (basic_component and hra_component):
|
|
|
|
frappe.throw(_("Please mention Basic and HRA component in Company"))
|
2018-06-14 12:26:16 +00:00
|
|
|
annual_exemption, monthly_exemption, hra_amount = 0, 0, 0
|
2018-07-01 11:12:38 +00:00
|
|
|
if hra_component and basic_component:
|
2019-04-25 13:14:10 +00:00
|
|
|
assignment = get_salary_assignment(doc.employee, nowdate())
|
|
|
|
if assignment:
|
2022-03-28 13:22:46 +00:00
|
|
|
hra_component_exists = frappe.db.exists(
|
|
|
|
"Salary Detail",
|
|
|
|
{
|
|
|
|
"parent": assignment.salary_structure,
|
|
|
|
"salary_component": hra_component,
|
|
|
|
"parentfield": "earnings",
|
|
|
|
"parenttype": "Salary Structure",
|
|
|
|
},
|
|
|
|
)
|
2019-05-16 05:53:04 +00:00
|
|
|
|
2019-04-25 13:14:10 +00:00
|
|
|
if hra_component_exists:
|
2022-03-28 13:22:46 +00:00
|
|
|
basic_amount, hra_amount = get_component_amt_from_salary_slip(
|
|
|
|
doc.employee, assignment.salary_structure, basic_component, hra_component
|
|
|
|
)
|
2019-04-25 13:14:10 +00:00
|
|
|
if hra_amount:
|
|
|
|
if doc.monthly_house_rent:
|
2022-03-28 13:22:46 +00:00
|
|
|
annual_exemption = calculate_hra_exemption(
|
|
|
|
assignment.salary_structure,
|
|
|
|
basic_amount,
|
|
|
|
hra_amount,
|
|
|
|
doc.monthly_house_rent,
|
|
|
|
doc.rented_in_metro_city,
|
|
|
|
)
|
2019-04-25 13:14:10 +00:00
|
|
|
if annual_exemption > 0:
|
|
|
|
monthly_exemption = annual_exemption / 12
|
|
|
|
else:
|
|
|
|
annual_exemption = 0
|
2019-05-16 05:53:04 +00:00
|
|
|
|
2019-04-25 13:14:10 +00:00
|
|
|
elif doc.docstatus == 1:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Salary Structure must be submitted before submission of Tax Ememption Declaration")
|
|
|
|
)
|
|
|
|
|
|
|
|
return frappe._dict(
|
|
|
|
{
|
|
|
|
"hra_amount": hra_amount,
|
|
|
|
"annual_exemption": annual_exemption,
|
|
|
|
"monthly_exemption": monthly_exemption,
|
|
|
|
}
|
|
|
|
)
|
2019-04-25 13:14:10 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
|
2018-07-01 11:12:38 +00:00
|
|
|
def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component):
|
2022-03-28 13:22:46 +00:00
|
|
|
salary_slip = make_salary_slip(
|
|
|
|
salary_structure, employee=employee, for_preview=1, ignore_permissions=True
|
|
|
|
)
|
2018-07-01 11:12:38 +00:00
|
|
|
basic_amt, hra_amt = 0, 0
|
2018-06-14 12:26:16 +00:00
|
|
|
for earning in salary_slip.earnings:
|
2018-07-01 11:12:38 +00:00
|
|
|
if earning.salary_component == basic_component:
|
|
|
|
basic_amt = earning.amount
|
|
|
|
elif earning.salary_component == hra_component:
|
|
|
|
hra_amt = earning.amount
|
|
|
|
if basic_amt and hra_amt:
|
|
|
|
return basic_amt, hra_amt
|
2018-07-16 10:42:46 +00:00
|
|
|
return basic_amt, hra_amt
|
2018-07-01 11:12:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def calculate_hra_exemption(
|
|
|
|
salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city
|
|
|
|
):
|
2018-06-14 12:26:16 +00:00
|
|
|
# TODO make this configurable
|
|
|
|
exemptions = []
|
|
|
|
frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency")
|
|
|
|
# case 1: The actual amount allotted by the employer as the HRA.
|
|
|
|
exemptions.append(get_annual_component_pay(frequency, monthly_hra))
|
2019-04-25 13:14:10 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
actual_annual_rent = monthly_house_rent * 12
|
2018-07-01 11:12:38 +00:00
|
|
|
annual_basic = get_annual_component_pay(frequency, basic)
|
2019-04-25 13:14:10 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
# case 2: Actual rent paid less 10% of the basic salary.
|
2018-07-01 11:12:38 +00:00
|
|
|
exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1))
|
2018-06-14 12:26:16 +00:00
|
|
|
# case 3: 50% of the basic salary, if the employee is staying in a metro city (40% for a non-metro city).
|
2018-07-01 11:12:38 +00:00
|
|
|
exemptions.append(annual_basic * 0.5 if rented_in_metro_city else annual_basic * 0.4)
|
2018-06-14 12:26:16 +00:00
|
|
|
# return minimum of 3 cases
|
|
|
|
return min(exemptions)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
def get_annual_component_pay(frequency, amount):
|
|
|
|
if frequency == "Daily":
|
|
|
|
return amount * 365
|
|
|
|
elif frequency == "Weekly":
|
|
|
|
return amount * 52
|
|
|
|
elif frequency == "Fortnightly":
|
|
|
|
return amount * 26
|
|
|
|
elif frequency == "Monthly":
|
|
|
|
return amount * 12
|
|
|
|
elif frequency == "Bimonthly":
|
|
|
|
return amount * 6
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
def validate_house_rent_dates(doc):
|
|
|
|
if not doc.rented_to_date or not doc.rented_from_date:
|
|
|
|
frappe.throw(_("House rented dates required for exemption calculation"))
|
2019-04-25 13:14:10 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
if date_diff(doc.rented_to_date, doc.rented_from_date) < 14:
|
|
|
|
frappe.throw(_("House rented dates should be atleast 15 days apart"))
|
2019-04-25 13:14:10 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
proofs = frappe.db.sql(
|
|
|
|
"""
|
2019-04-25 13:14:10 +00:00
|
|
|
select name
|
|
|
|
from `tabEmployee Tax Exemption Proof Submission`
|
|
|
|
where
|
2019-04-25 14:24:20 +00:00
|
|
|
docstatus=1 and employee=%(employee)s and payroll_period=%(payroll_period)s
|
|
|
|
and (rented_from_date between %(from_date)s and %(to_date)s or rented_to_date between %(from_date)s and %(to_date)s)
|
2022-03-28 13:22:46 +00:00
|
|
|
""",
|
|
|
|
{
|
|
|
|
"employee": doc.employee,
|
|
|
|
"payroll_period": doc.payroll_period,
|
|
|
|
"from_date": doc.rented_from_date,
|
|
|
|
"to_date": doc.rented_to_date,
|
|
|
|
},
|
|
|
|
)
|
2019-04-25 13:14:10 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
if proofs:
|
2019-04-25 14:24:20 +00:00
|
|
|
frappe.throw(_("House rent paid days overlapping with {0}").format(proofs[0][0]))
|
2018-06-14 12:26:16 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-14 12:26:16 +00:00
|
|
|
def calculate_hra_exemption_for_period(doc):
|
|
|
|
monthly_rent, eligible_hra = 0, 0
|
|
|
|
if doc.house_rent_payment_amount:
|
|
|
|
validate_house_rent_dates(doc)
|
|
|
|
# TODO receive rented months or validate dates are start and end of months?
|
|
|
|
# Calc monthly rent, round to nearest .5
|
2022-03-28 13:22:46 +00:00
|
|
|
factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1) / 30
|
|
|
|
factor = round(factor * 2) / 2
|
2018-06-14 12:26:16 +00:00
|
|
|
monthly_rent = doc.house_rent_payment_amount / factor
|
|
|
|
# update field used by calculate_annual_eligible_hra_exemption
|
|
|
|
doc.monthly_house_rent = monthly_rent
|
|
|
|
exemptions = calculate_annual_eligible_hra_exemption(doc)
|
|
|
|
|
|
|
|
if exemptions["monthly_exemption"]:
|
|
|
|
# calc total exemption amount
|
|
|
|
eligible_hra = exemptions["monthly_exemption"] * factor
|
2018-06-20 05:40:56 +00:00
|
|
|
exemptions["monthly_house_rent"] = monthly_rent
|
|
|
|
exemptions["total_eligible_hra_exemption"] = eligible_hra
|
|
|
|
return exemptions
|
2019-06-06 06:38:09 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
def get_ewb_data(dt, dn):
|
|
|
|
|
|
|
|
ewaybills = []
|
|
|
|
for doc_name in dn:
|
|
|
|
doc = frappe.get_doc(dt, doc_name)
|
|
|
|
|
2020-02-18 06:58:41 +00:00
|
|
|
validate_doc(doc)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
data = frappe._dict(
|
|
|
|
{
|
|
|
|
"transporterId": "",
|
|
|
|
"TotNonAdvolVal": 0,
|
|
|
|
}
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
data.userGstin = data.fromGstin = doc.company_gstin
|
2022-03-28 13:22:46 +00:00
|
|
|
data.supplyType = "O"
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if dt == "Delivery Note":
|
2020-02-18 06:58:41 +00:00
|
|
|
data.subSupplyType = 1
|
2022-03-28 13:22:46 +00:00
|
|
|
elif doc.gst_category in ["Registered Regular", "SEZ"]:
|
2019-07-03 05:04:31 +00:00
|
|
|
data.subSupplyType = 1
|
2022-03-28 13:22:46 +00:00
|
|
|
elif doc.gst_category in ["Overseas", "Deemed Export"]:
|
2019-07-03 05:04:31 +00:00
|
|
|
data.subSupplyType = 3
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("Unsupported GST Category for E-Way Bill JSON generation"))
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
data.docType = "INV"
|
|
|
|
data.docDate = frappe.utils.formatdate(doc.posting_date, "dd/mm/yyyy")
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
company_address = frappe.get_doc("Address", doc.company_address)
|
|
|
|
billing_address = frappe.get_doc("Address", doc.customer_address)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
# added dispatch address
|
|
|
|
dispatch_address = (
|
|
|
|
frappe.get_doc("Address", doc.dispatch_address_name)
|
|
|
|
if doc.dispatch_address_name
|
|
|
|
else company_address
|
|
|
|
)
|
|
|
|
shipping_address = frappe.get_doc("Address", doc.shipping_address_name)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2021-07-13 09:28:17 +00:00
|
|
|
data = get_address_details(data, doc, company_address, billing_address, dispatch_address)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
data.itemList = []
|
2021-11-01 10:25:19 +00:00
|
|
|
data.totalValue = doc.net_total
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2021-09-17 05:09:03 +00:00
|
|
|
data = get_item_list(data, doc, hsn_wise=True)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
disable_rounded = frappe.db.get_single_value("Global Defaults", "disable_rounded_total")
|
2019-07-03 05:04:31 +00:00
|
|
|
data.totInvValue = doc.grand_total if disable_rounded else doc.rounded_total
|
|
|
|
|
|
|
|
data = get_transport_details(data, doc)
|
|
|
|
|
|
|
|
fields = {
|
|
|
|
"/. -": {
|
2022-03-28 13:22:46 +00:00
|
|
|
"docNo": doc.name,
|
|
|
|
"fromTrdName": doc.company,
|
|
|
|
"toTrdName": doc.customer_name,
|
|
|
|
"transDocNo": doc.lr_no,
|
2019-07-03 05:04:31 +00:00
|
|
|
},
|
|
|
|
"@#/,&. -": {
|
2022-03-28 13:22:46 +00:00
|
|
|
"fromAddr1": company_address.address_line1,
|
|
|
|
"fromAddr2": company_address.address_line2,
|
|
|
|
"fromPlace": company_address.city,
|
|
|
|
"toAddr1": shipping_address.address_line1,
|
|
|
|
"toAddr2": shipping_address.address_line2,
|
|
|
|
"toPlace": shipping_address.city,
|
|
|
|
"transporterName": doc.transporter_name,
|
|
|
|
},
|
2019-07-03 05:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for allowed_chars, field_map in fields.items():
|
|
|
|
for key, value in field_map.items():
|
|
|
|
if not value:
|
2022-03-28 13:22:46 +00:00
|
|
|
data[key] = ""
|
2019-07-03 05:04:31 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
data[key] = re.sub(r"[^\w" + allowed_chars + "]", "", value)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
ewaybills.append(data)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
data = {"version": "1.0.0421", "billLists": ewaybills}
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def generate_ewb_json(dt, dn):
|
2020-04-27 05:20:40 +00:00
|
|
|
dn = json.loads(dn)
|
|
|
|
return get_ewb_data(dt, dn)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-04-27 05:20:40 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def download_ewb_json():
|
2020-07-11 12:14:20 +00:00
|
|
|
data = json.loads(frappe.local.form_dict.data)
|
|
|
|
frappe.local.response.filecontent = json.dumps(data, indent=4, sort_keys=True)
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.local.response.type = "download"
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
filename_prefix = "Bulk"
|
2020-07-11 12:14:20 +00:00
|
|
|
docname = frappe.local.form_dict.docname
|
|
|
|
if docname:
|
2022-03-28 13:22:46 +00:00
|
|
|
if docname.startswith("["):
|
2020-07-11 12:14:20 +00:00
|
|
|
docname = json.loads(docname)
|
|
|
|
if len(docname) == 1:
|
|
|
|
docname = docname[0]
|
2020-04-27 05:20:40 +00:00
|
|
|
|
2020-07-11 12:14:20 +00:00
|
|
|
if not isinstance(docname, list):
|
|
|
|
# removes characters not allowed in a filename (https://stackoverflow.com/a/38766141/4767738)
|
2022-03-28 13:22:46 +00:00
|
|
|
filename_prefix = re.sub(r"[^\w_.)( -]", "", docname)
|
|
|
|
|
|
|
|
frappe.local.response.filename = "{0}_e-WayBill_Data_{1}.json".format(
|
|
|
|
filename_prefix, frappe.utils.random_string(5)
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
|
2019-06-06 06:38:09 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_gstins_for_company(company):
|
2022-03-28 13:22:46 +00:00
|
|
|
company_gstins = []
|
2019-06-06 06:38:09 +00:00
|
|
|
if company:
|
2022-03-28 13:22:46 +00:00
|
|
|
company_gstins = frappe.db.sql(
|
|
|
|
"""select
|
2019-06-06 06:38:09 +00:00
|
|
|
distinct `tabAddress`.gstin
|
|
|
|
from
|
|
|
|
`tabAddress`, `tabDynamic Link`
|
|
|
|
where
|
|
|
|
`tabDynamic Link`.parent = `tabAddress`.name and
|
|
|
|
`tabDynamic Link`.parenttype = 'Address' and
|
|
|
|
`tabDynamic Link`.link_doctype = 'Company' and
|
2022-03-28 13:22:46 +00:00
|
|
|
`tabDynamic Link`.link_name = %(company)s""",
|
|
|
|
{"company": company},
|
|
|
|
)
|
2019-06-06 06:38:09 +00:00
|
|
|
return company_gstins
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-07-13 09:28:17 +00:00
|
|
|
def get_address_details(data, doc, company_address, billing_address, dispatch_address):
|
2022-03-28 13:22:46 +00:00
|
|
|
data.fromPincode = validate_pincode(company_address.pincode, "Company Address")
|
|
|
|
data.fromStateCode = validate_state_code(company_address.gst_state_number, "Company Address")
|
|
|
|
data.actualFromStateCode = validate_state_code(
|
|
|
|
dispatch_address.gst_state_number, "Dispatch Address"
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if not doc.billing_address_gstin or len(doc.billing_address_gstin) < 15:
|
2022-03-28 13:22:46 +00:00
|
|
|
data.toGstin = "URP"
|
2019-07-03 05:04:31 +00:00
|
|
|
set_gst_state_and_state_number(billing_address)
|
|
|
|
else:
|
|
|
|
data.toGstin = doc.billing_address_gstin
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
data.toPincode = validate_pincode(billing_address.pincode, "Customer Address")
|
|
|
|
data.toStateCode = validate_state_code(billing_address.gst_state_number, "Customer Address")
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if doc.customer_address != doc.shipping_address_name:
|
|
|
|
data.transType = 2
|
2022-03-28 13:22:46 +00:00
|
|
|
shipping_address = frappe.get_doc("Address", doc.shipping_address_name)
|
2019-07-03 05:04:31 +00:00
|
|
|
set_gst_state_and_state_number(shipping_address)
|
2022-03-28 13:22:46 +00:00
|
|
|
data.toPincode = validate_pincode(shipping_address.pincode, "Shipping Address")
|
|
|
|
data.actualToStateCode = validate_state_code(
|
|
|
|
shipping_address.gst_state_number, "Shipping Address"
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
else:
|
|
|
|
data.transType = 1
|
|
|
|
data.actualToStateCode = data.toStateCode
|
|
|
|
shipping_address = billing_address
|
2020-11-24 02:39:17 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.gst_category == "SEZ":
|
2020-11-18 15:28:59 +00:00
|
|
|
data.toStateCode = 99
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-09-17 05:09:03 +00:00
|
|
|
def get_item_list(data, doc, hsn_wise=False):
|
2022-03-28 13:22:46 +00:00
|
|
|
for attr in ["cgstValue", "sgstValue", "igstValue", "cessValue", "OthValue"]:
|
2019-07-03 05:04:31 +00:00
|
|
|
data[attr] = 0
|
|
|
|
|
|
|
|
gst_accounts = get_gst_accounts(doc.company, account_wise=True)
|
|
|
|
tax_map = {
|
2022-03-28 13:22:46 +00:00
|
|
|
"sgst_account": ["sgstRate", "sgstValue"],
|
|
|
|
"cgst_account": ["cgstRate", "cgstValue"],
|
|
|
|
"igst_account": ["igstRate", "igstValue"],
|
|
|
|
"cess_account": ["cessRate", "cessValue"],
|
2019-07-03 05:04:31 +00:00
|
|
|
}
|
2022-03-28 13:22:46 +00:00
|
|
|
item_data_attrs = ["sgstRate", "cgstRate", "igstRate", "cessRate", "cessNonAdvol"]
|
|
|
|
hsn_wise_charges, hsn_taxable_amount = get_itemised_tax_breakup_data(
|
|
|
|
doc, account_wise=True, hsn_wise=hsn_wise
|
|
|
|
)
|
2021-11-14 02:40:53 +00:00
|
|
|
for item_or_hsn, taxable_amount in hsn_taxable_amount.items():
|
2019-07-03 05:04:31 +00:00
|
|
|
item_data = frappe._dict()
|
2021-11-14 02:40:53 +00:00
|
|
|
if not item_or_hsn:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("GST HSN Code does not exist for one or more items"))
|
2021-11-14 02:40:53 +00:00
|
|
|
item_data.hsnCode = int(item_or_hsn) if hsn_wise else item_or_hsn
|
2019-07-03 05:04:31 +00:00
|
|
|
item_data.taxableAmount = taxable_amount
|
|
|
|
item_data.qtyUnit = ""
|
|
|
|
for attr in item_data_attrs:
|
|
|
|
item_data[attr] = 0
|
|
|
|
|
2021-11-14 02:40:53 +00:00
|
|
|
for account, tax_detail in hsn_wise_charges.get(item_or_hsn, {}).items():
|
2022-03-28 13:22:46 +00:00
|
|
|
account_type = gst_accounts.get(account, "")
|
2019-07-03 05:04:31 +00:00
|
|
|
for tax_acc, attrs in tax_map.items():
|
|
|
|
if account_type == tax_acc:
|
2022-03-28 13:22:46 +00:00
|
|
|
item_data[attrs[0]] = tax_detail.get("tax_rate")
|
|
|
|
data[attrs[1]] += tax_detail.get("tax_amount")
|
2019-07-03 05:04:31 +00:00
|
|
|
break
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
data.OthValue += tax_detail.get("tax_amount")
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
data.itemList.append(item_data)
|
|
|
|
|
|
|
|
# Tax amounts rounded to 2 decimals to avoid exceeding max character limit
|
2022-03-28 13:22:46 +00:00
|
|
|
for attr in ["sgstValue", "cgstValue", "igstValue", "cessValue"]:
|
2019-07-03 05:04:31 +00:00
|
|
|
data[attr] = flt(data[attr], 2)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-02-18 06:58:41 +00:00
|
|
|
def validate_doc(doc):
|
2019-07-03 05:04:31 +00:00
|
|
|
if doc.docstatus != 1:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("E-Way Bill JSON can only be generated from submitted document"))
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if doc.is_return:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("E-Way Bill JSON cannot be generated for Sales Return as of now"))
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if doc.ewaybill:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("e-Way Bill already exists for this document"))
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
reqd_fields = [
|
|
|
|
"company_gstin",
|
|
|
|
"company_address",
|
|
|
|
"customer_address",
|
|
|
|
"shipping_address_name",
|
|
|
|
"mode_of_transport",
|
|
|
|
"distance",
|
|
|
|
]
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
for fieldname in reqd_fields:
|
|
|
|
if not doc.get(fieldname):
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("{} is required to generate E-Way Bill JSON").format(doc.meta.get_label(fieldname))
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if len(doc.company_gstin) < 15:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("You must be a registered supplier to generate e-Way Bill"))
|
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
def get_transport_details(data, doc):
|
|
|
|
if doc.distance > 4000:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("Distance cannot be greater than 4000 kms"))
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
data.transDistance = int(round(doc.distance))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
transport_modes = {"Road": 1, "Rail": 2, "Air": 3, "Ship": 4}
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
vehicle_types = {"Regular": "R", "Over Dimensional Cargo (ODC)": "O"}
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
data.transMode = transport_modes.get(doc.mode_of_transport)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.mode_of_transport == "Road":
|
2019-07-03 05:04:31 +00:00
|
|
|
if not doc.gst_transporter_id and not doc.vehicle_no:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Either GST Transporter ID or Vehicle No is required if Mode of Transport is Road")
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
if doc.vehicle_no:
|
2022-03-28 13:22:46 +00:00
|
|
|
data.vehicleNo = doc.vehicle_no.replace(" ", "")
|
2019-07-03 05:04:31 +00:00
|
|
|
if not doc.gst_vehicle_type:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("Vehicle Type is required if Mode of Transport is Road"))
|
2019-07-03 05:04:31 +00:00
|
|
|
else:
|
|
|
|
data.vehicleType = vehicle_types.get(doc.gst_vehicle_type)
|
|
|
|
else:
|
|
|
|
if not doc.lr_no or not doc.lr_date:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("Transport Receipt No and Date are mandatory for your chosen Mode of Transport"))
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if doc.lr_no:
|
|
|
|
data.transDocNo = doc.lr_no
|
|
|
|
|
|
|
|
if doc.lr_date:
|
2022-03-28 13:22:46 +00:00
|
|
|
data.transDocDate = frappe.utils.formatdate(doc.lr_date, "dd/mm/yyyy")
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if doc.gst_transporter_id:
|
2020-05-30 09:30:56 +00:00
|
|
|
if doc.gst_transporter_id[0:2] != "88":
|
2022-03-28 13:22:46 +00:00
|
|
|
validate_gstin_check_digit(doc.gst_transporter_id, label="GST Transporter ID")
|
2020-05-30 09:30:56 +00:00
|
|
|
data.transporterId = doc.gst_transporter_id
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def validate_pincode(pincode, address):
|
|
|
|
pin_not_found = "Pin Code doesn't exist for {}"
|
|
|
|
incorrect_pin = "Pin Code for {} is incorrecty formatted. It must be 6 digits (without spaces)"
|
|
|
|
|
|
|
|
if not pincode:
|
|
|
|
frappe.throw(_(pin_not_found.format(address)))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
pincode = pincode.replace(" ", "")
|
2019-07-03 05:04:31 +00:00
|
|
|
if not pincode.isdigit() or len(pincode) != 6:
|
|
|
|
frappe.throw(_(incorrect_pin.format(address)))
|
|
|
|
else:
|
|
|
|
return int(pincode)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
def validate_state_code(state_code, address):
|
|
|
|
no_state_code = "GST State Code not found for {0}. Please set GST State in {0}"
|
|
|
|
if not state_code:
|
|
|
|
frappe.throw(_(no_state_code.format(address)))
|
|
|
|
else:
|
|
|
|
return int(state_code)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-02 15:48:29 +00:00
|
|
|
@frappe.whitelist()
|
2022-03-28 13:22:46 +00:00
|
|
|
def get_gst_accounts(
|
|
|
|
company=None, account_wise=False, only_reverse_charge=0, only_non_reverse_charge=0
|
|
|
|
):
|
|
|
|
filters = {"parent": "GST Settings"}
|
2021-05-14 06:47:41 +00:00
|
|
|
|
|
|
|
if company:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters.update({"company": company})
|
2021-05-14 06:47:41 +00:00
|
|
|
if only_reverse_charge:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters.update({"is_reverse_charge_account": 1})
|
2021-05-14 06:47:41 +00:00
|
|
|
elif only_non_reverse_charge:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters.update({"is_reverse_charge_account": 0})
|
2021-05-14 06:47:41 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
gst_accounts = frappe._dict()
|
2022-03-28 13:22:46 +00:00
|
|
|
gst_settings_accounts = frappe.get_all(
|
|
|
|
"GST Account",
|
2021-05-14 06:47:41 +00:00
|
|
|
filters=filters,
|
2022-03-28 13:22:46 +00:00
|
|
|
fields=["cgst_account", "sgst_account", "igst_account", "cess_account"],
|
|
|
|
)
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2021-05-20 11:49:24 +00:00
|
|
|
if not gst_settings_accounts and not frappe.flags.in_test and not frappe.flags.in_migrate:
|
2019-07-03 05:04:31 +00:00
|
|
|
frappe.throw(_("Please set GST Accounts in GST Settings"))
|
|
|
|
|
|
|
|
for d in gst_settings_accounts:
|
|
|
|
for acc, val in d.items():
|
|
|
|
if not account_wise:
|
|
|
|
gst_accounts.setdefault(acc, []).append(val)
|
|
|
|
elif val:
|
|
|
|
gst_accounts[val] = acc
|
|
|
|
|
|
|
|
return gst_accounts
|
2020-06-03 05:29:37 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
def validate_reverse_charge_transaction(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
country = frappe.get_cached_value("Company", doc.company, "country")
|
2020-07-15 18:27:03 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if country != "India":
|
2020-07-15 18:27:03 +00:00
|
|
|
return
|
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
base_gst_tax = 0
|
|
|
|
base_reverse_charge_booked = 0
|
2020-08-19 13:00:18 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.reverse_charge == "Y":
|
2021-05-14 06:47:41 +00:00
|
|
|
gst_accounts = get_gst_accounts(doc.company, only_reverse_charge=1)
|
2022-03-28 13:22:46 +00:00
|
|
|
reverse_charge_accounts = (
|
|
|
|
gst_accounts.get("cgst_account")
|
|
|
|
+ gst_accounts.get("sgst_account")
|
|
|
|
+ gst_accounts.get("igst_account")
|
|
|
|
)
|
2020-07-02 15:48:29 +00:00
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
gst_accounts = get_gst_accounts(doc.company, only_non_reverse_charge=1)
|
2022-03-28 13:22:46 +00:00
|
|
|
non_reverse_charge_accounts = (
|
|
|
|
gst_accounts.get("cgst_account")
|
|
|
|
+ gst_accounts.get("sgst_account")
|
|
|
|
+ gst_accounts.get("igst_account")
|
|
|
|
)
|
2020-07-02 15:48:29 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for tax in doc.get("taxes"):
|
2021-05-14 06:47:41 +00:00
|
|
|
if tax.account_head in non_reverse_charge_accounts:
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.add_deduct_tax == "Add":
|
2021-05-14 06:47:41 +00:00
|
|
|
base_gst_tax += tax.base_tax_amount_after_discount_amount
|
|
|
|
else:
|
|
|
|
base_gst_tax += tax.base_tax_amount_after_discount_amount
|
|
|
|
elif tax.account_head in reverse_charge_accounts:
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.add_deduct_tax == "Add":
|
2021-05-14 06:47:41 +00:00
|
|
|
base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount
|
|
|
|
else:
|
|
|
|
base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount
|
2020-07-02 15:48:29 +00:00
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
if base_gst_tax != base_reverse_charge_booked:
|
|
|
|
msg = _("Booked reverse charge is not equal to applied tax amount")
|
|
|
|
msg += "<br>"
|
2022-03-28 13:22:46 +00:00
|
|
|
msg += _(
|
|
|
|
"Please refer {gst_document_link} to learn more about how to setup and create reverse charge invoice"
|
|
|
|
).format(
|
|
|
|
gst_document_link='<a href="https://docs.erpnext.com/docs/user/manual/en/regional/india/gst-setup">GST Documentation</a>'
|
|
|
|
)
|
2020-07-02 15:48:29 +00:00
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
frappe.throw(msg)
|
2020-07-02 15:48:29 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
def update_itc_availed_fields(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
country = frappe.get_cached_value("Company", doc.company, "country")
|
2020-06-03 05:29:37 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if country != "India":
|
2021-05-14 06:47:41 +00:00
|
|
|
return
|
2020-06-03 05:29:37 +00:00
|
|
|
|
2021-05-14 06:47:41 +00:00
|
|
|
# Initialize values
|
|
|
|
doc.itc_integrated_tax = doc.itc_state_tax = doc.itc_central_tax = doc.itc_cess_amount = 0
|
|
|
|
gst_accounts = get_gst_accounts(doc.company, only_non_reverse_charge=1)
|
2021-02-19 09:00:23 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for tax in doc.get("taxes"):
|
|
|
|
if tax.account_head in gst_accounts.get("igst_account", []):
|
2021-05-14 06:47:41 +00:00
|
|
|
doc.itc_integrated_tax += flt(tax.base_tax_amount_after_discount_amount)
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.account_head in gst_accounts.get("sgst_account", []):
|
2021-05-14 06:47:41 +00:00
|
|
|
doc.itc_state_tax += flt(tax.base_tax_amount_after_discount_amount)
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.account_head in gst_accounts.get("cgst_account", []):
|
2021-05-14 06:47:41 +00:00
|
|
|
doc.itc_central_tax += flt(tax.base_tax_amount_after_discount_amount)
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.account_head in gst_accounts.get("cess_account", []):
|
2021-05-14 06:47:41 +00:00
|
|
|
doc.itc_cess_amount += flt(tax.base_tax_amount_after_discount_amount)
|
2021-03-16 07:39:59 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-07-15 10:06:54 +00:00
|
|
|
def update_place_of_supply(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
country = frappe.get_cached_value("Company", doc.company, "country")
|
|
|
|
if country != "India":
|
2021-07-15 10:06:54 +00:00
|
|
|
return
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
address = frappe.db.get_value(
|
|
|
|
"Address", doc.get("customer_address"), ["gst_state", "gst_state_number"], as_dict=1
|
|
|
|
)
|
2021-07-15 10:06:54 +00:00
|
|
|
if address and address.gst_state and address.gst_state_number:
|
|
|
|
doc.place_of_supply = cstr(address.gst_state_number) + "-" + cstr(address.gst_state)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-03-16 07:39:59 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_regional_round_off_accounts(company, account_list):
|
2022-03-28 13:22:46 +00:00
|
|
|
country = frappe.get_cached_value("Company", company, "country")
|
2021-03-16 07:39:59 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if country != "India":
|
2021-03-16 07:39:59 +00:00
|
|
|
return
|
|
|
|
|
2021-11-04 14:18:32 +00:00
|
|
|
if isinstance(account_list, str):
|
2021-03-16 07:39:59 +00:00
|
|
|
account_list = json.loads(account_list)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not frappe.db.get_single_value("GST Settings", "round_off_gst_values"):
|
2021-03-16 07:39:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
gst_accounts = get_gst_accounts(company)
|
2021-03-27 04:43:27 +00:00
|
|
|
|
|
|
|
gst_account_list = []
|
2022-03-28 13:22:46 +00:00
|
|
|
for account in ["cgst_account", "sgst_account", "igst_account"]:
|
2021-03-27 07:22:23 +00:00
|
|
|
if account in gst_accounts:
|
2021-03-27 04:43:27 +00:00
|
|
|
gst_account_list += gst_accounts.get(account)
|
2021-03-16 07:39:59 +00:00
|
|
|
|
|
|
|
account_list.extend(gst_account_list)
|
|
|
|
|
|
|
|
return account_list
|
2021-04-12 05:25:43 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-12 05:25:43 +00:00
|
|
|
def update_taxable_values(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
country = frappe.get_cached_value("Company", doc.company, "country")
|
2021-04-12 05:25:43 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if country != "India":
|
2021-04-12 05:25:43 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
gst_accounts = get_gst_accounts(doc.company)
|
|
|
|
|
|
|
|
# Only considering sgst account to avoid inflating taxable value
|
2022-03-28 13:22:46 +00:00
|
|
|
gst_account_list = (
|
|
|
|
gst_accounts.get("sgst_account", [])
|
|
|
|
+ gst_accounts.get("sgst_account", [])
|
|
|
|
+ gst_accounts.get("igst_account", [])
|
|
|
|
)
|
2021-04-12 05:25:43 +00:00
|
|
|
|
|
|
|
additional_taxes = 0
|
|
|
|
total_charges = 0
|
|
|
|
item_count = 0
|
|
|
|
considered_rows = []
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for tax in doc.get("taxes"):
|
2021-04-12 05:25:43 +00:00
|
|
|
prev_row_id = cint(tax.row_id) - 1
|
|
|
|
if tax.account_head in gst_account_list and prev_row_id not in considered_rows:
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.charge_type == "On Previous Row Amount":
|
|
|
|
additional_taxes += doc.get("taxes")[prev_row_id].tax_amount_after_discount_amount
|
2021-04-12 05:25:43 +00:00
|
|
|
considered_rows.append(prev_row_id)
|
2022-03-28 13:22:46 +00:00
|
|
|
if tax.charge_type == "On Previous Row Total":
|
|
|
|
additional_taxes += doc.get("taxes")[prev_row_id].base_total - doc.base_net_total
|
2021-04-12 05:25:43 +00:00
|
|
|
considered_rows.append(prev_row_id)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for item in doc.get("items"):
|
2021-06-01 07:43:04 +00:00
|
|
|
proportionate_value = item.base_net_amount if doc.base_net_total else item.qty
|
|
|
|
total_value = doc.base_net_total if doc.base_net_total else doc.total_qty
|
2021-04-12 05:25:43 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
applicable_charges = flt(
|
|
|
|
flt(
|
|
|
|
proportionate_value * (flt(additional_taxes) / flt(total_value)),
|
|
|
|
item.precision("taxable_value"),
|
|
|
|
)
|
|
|
|
)
|
2021-04-12 05:25:43 +00:00
|
|
|
item.taxable_value = applicable_charges + proportionate_value
|
|
|
|
total_charges += applicable_charges
|
|
|
|
item_count += 1
|
|
|
|
|
|
|
|
if total_charges != additional_taxes:
|
|
|
|
diff = additional_taxes - total_charges
|
2022-03-28 13:22:46 +00:00
|
|
|
doc.get("items")[item_count - 1].taxable_value += diff
|
|
|
|
|
2021-05-10 07:06:56 +00:00
|
|
|
|
|
|
|
def get_depreciation_amount(asset, depreciable_value, row):
|
|
|
|
if row.depreciation_method in ("Straight Line", "Manual"):
|
2021-06-19 08:15:37 +00:00
|
|
|
# if the Depreciation Schedule is being prepared for the first time
|
2021-06-19 08:30:26 +00:00
|
|
|
if not asset.flags.increase_in_asset_life:
|
2022-03-28 13:22:46 +00:00
|
|
|
depreciation_amount = (
|
|
|
|
flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life)
|
|
|
|
) / flt(row.total_number_of_depreciations)
|
2021-06-19 08:15:37 +00:00
|
|
|
|
|
|
|
# if the Depreciation Schedule is being modified after Asset Repair
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
depreciation_amount = (
|
|
|
|
flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)
|
|
|
|
) / (date_diff(asset.to_date, asset.available_for_use_date) / 365)
|
2021-08-19 08:11:10 +00:00
|
|
|
|
2021-05-10 07:06:56 +00:00
|
|
|
else:
|
|
|
|
rate_of_depreciation = row.rate_of_depreciation
|
|
|
|
# if its the first depreciation
|
|
|
|
if depreciable_value == asset.gross_purchase_amount:
|
2022-03-28 13:22:46 +00:00
|
|
|
if row.finance_book and frappe.db.get_value("Finance Book", row.finance_book, "for_income_tax"):
|
2021-09-28 12:42:02 +00:00
|
|
|
# as per IT act, if the asset is purchased in the 2nd half of fiscal year, then rate is divided by 2
|
|
|
|
diff = date_diff(row.depreciation_start_date, asset.available_for_use_date)
|
|
|
|
if diff <= 180:
|
|
|
|
rate_of_depreciation = rate_of_depreciation / 2
|
|
|
|
frappe.msgprint(
|
2022-03-28 13:22:46 +00:00
|
|
|
_(
|
|
|
|
"As per IT Act, the rate of depreciation for the first depreciation entry is reduced by 50%."
|
|
|
|
)
|
|
|
|
)
|
2021-05-10 07:06:56 +00:00
|
|
|
|
|
|
|
depreciation_amount = flt(depreciable_value * (flt(rate_of_depreciation) / 100))
|
|
|
|
|
2021-08-03 10:27:11 +00:00
|
|
|
return depreciation_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-08-03 10:27:11 +00:00
|
|
|
def set_item_tax_from_hsn_code(item):
|
2021-08-19 08:11:10 +00:00
|
|
|
if not item.taxes and item.gst_hsn_code:
|
2021-08-03 10:27:11 +00:00
|
|
|
hsn_doc = frappe.get_doc("GST HSN Code", item.gst_hsn_code)
|
|
|
|
|
|
|
|
for tax in hsn_doc.taxes:
|
2022-03-28 13:22:46 +00:00
|
|
|
item.append(
|
|
|
|
"taxes",
|
|
|
|
{
|
|
|
|
"item_tax_template": tax.item_tax_template,
|
|
|
|
"tax_category": tax.tax_category,
|
|
|
|
"valid_from": tax.valid_from,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-08-20 09:10:12 +00:00
|
|
|
|
|
|
|
def delete_gst_settings_for_company(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
if doc.country != "India":
|
2021-08-20 09:10:12 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
gst_settings = frappe.get_doc("GST Settings")
|
|
|
|
records_to_delete = []
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for d in reversed(gst_settings.get("gst_accounts")):
|
2021-08-20 09:10:12 +00:00
|
|
|
if d.company == doc.name:
|
|
|
|
records_to_delete.append(d)
|
|
|
|
|
|
|
|
for d in records_to_delete:
|
|
|
|
gst_settings.remove(d)
|
|
|
|
|
|
|
|
gst_settings.save()
|