Skip to content

Commit

Permalink
fix: changes as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
ljain112 committed Feb 23, 2024
1 parent 8d443dd commit b4c0261
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 194 deletions.
15 changes: 0 additions & 15 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ frappe.ui.form.on("GST Settings", {
// sets latest values in frappe.boot for current user
// other users will still need to refresh page
Object.assign(gst_settings, frm.doc);

let n = frm.doc.e_invoice_applicable_companies.length;
let checked = frm.doc.apply_e_invoice_only_for_selected_companies

for (let i = 0; i < n; i++) {
frappe.call({
method: 'india_compliance.patches.post_install.add_einvoice_status_field.after_changing_gst_settings_change_status',
args: {
company: checked === 1 ? frm.doc.e_invoice_applicable_companies[i].company : "",
e_invoice_applicable_from: checked === 1 ? frm.doc.e_invoice_applicable_companies[i].applicable_from :
frm.doc.e_invoice_applicable_from
}
})
if(checked === 0) break;
}
},
});

Expand Down
94 changes: 94 additions & 0 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.query_builder.functions import IfNull
from frappe.utils import getdate

from india_compliance.gst_india.constants import GST_ACCOUNT_FIELDS, GST_PARTY_TYPES
Expand All @@ -18,6 +19,7 @@
)
from india_compliance.gst_india.utils import can_enable_api, is_api_enabled
from india_compliance.gst_india.utils.custom_fields import toggle_custom_fields
from india_compliance.gst_india.utils.e_invoice import get_e_invoice_applicability_date
from india_compliance.gst_india.utils.gstin_info import get_gstin_info

E_INVOICE_START_DATE = "2021-01-01"
Expand All @@ -39,6 +41,18 @@ def validate(self):
self.validate_credentials()
self.clear_api_auth_session()
self.update_retry_e_invoice_e_waybill_scheduled_job()
self.update_e_invoice_status()

def update_e_invoice_status(self):
if not (
self.has_value_changed("enable_e_invoice")
or self.has_value_changed("e_invoice_applicable_from")
or self.has_value_changed("apply_e_invoice_only_for_selected_companies")
or not self.is_child_table_same("e_invoice_applicable_companies")
):
return

frappe.enqueue(update_e_invoice_status, queue="long", timeout=6000)

def clear_api_auth_session(self):
if self.has_value_changed("api_secret") and self.api_secret:
Expand Down Expand Up @@ -285,3 +299,83 @@ def update_gst_category():
)

frappe.db.set_global("has_missing_gst_category", None)


def update_e_invoice_status():
"""
- Update e-Invoice status based on Applicability
- Update "Pending" and "Not Applicable" Status
"""

gst_settings = frappe.get_cached_doc("GST Settings")
if not gst_settings.enable_e_invoice:
return update_not_applicable_status()

if not gst_settings.apply_e_invoice_only_for_selected_companies:
e_invoice_applicability_date = get_e_invoice_applicability_date(
{}, gst_settings, throw=False
)
update_pending_status(e_invoice_applicability_date)
update_not_applicable_status(e_invoice_applicability_date)
return

companies = frappe.get_all("Company", filters={"country": "India"}, pluck="name")

for company in companies:
e_invoice_applicability_date = get_e_invoice_applicability_date(
frappe._dict({"company": company}), gst_settings, throw=False
)

update_pending_status(e_invoice_applicability_date, company)
update_not_applicable_status(e_invoice_applicability_date, company)


def update_pending_status(e_invoice_applicability_date, company=None):
if not e_invoice_applicability_date:
return

sales_invoice = frappe.qb.DocType("Sales Invoice")
sales_invoice_item = frappe.qb.DocType("Sales Invoice Item")

query = (
frappe.qb.update(sales_invoice)
.join(sales_invoice_item)
.on(sales_invoice_item.parent == sales_invoice.name)
.set(sales_invoice.einvoice_status, "Pending")
.where(
IfNull(sales_invoice.billing_address_gstin, "")
!= IfNull(sales_invoice.company_gstin, "")
)
.where(IfNull(sales_invoice.irn, "") == "")
.where(sales_invoice_item.gst_treatment.isin(("Taxable", "Zero-Rated")))
.where(
(IfNull(sales_invoice.place_of_supply, "") == "96-Other Countries")
| (IfNull(sales_invoice.billing_address_gstin, "") != "")
)
.where(sales_invoice.posting_date >= e_invoice_applicability_date)
.where(sales_invoice.docstatus == 1)
.where(IfNull(sales_invoice.company_gstin, "") != "")
.where(sales_invoice.is_opening != "Yes")
)

if company:
query = query.where(sales_invoice.company == company)

query.run()


def update_not_applicable_status(e_invoice_applicability_date=None, company=None):
sales_invoice = frappe.qb.DocType("Sales Invoice")
query = (
frappe.qb.update(sales_invoice)
.set(sales_invoice.einvoice_status, "Not Applicable")
.where(IfNull(sales_invoice.einvoice_status, "") == "Pending")
.where(sales_invoice.docstatus == 1)
)
if e_invoice_applicability_date:
query = query.where(sales_invoice.posting_date < e_invoice_applicability_date)

if company:
company = query.where(sales_invoice.company == company)

query.run()
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"doctype": "Number Card",
"document_type": "Sales Invoice",
"dynamic_filters_json": "[]",
"filters_json": "[[\"Sales Invoice\",\"einvoice_status\",\"=\",\"Pending Cancellation\",false]]",
"filters_json": "[[\"Sales Invoice\",\"einvoice_status\",\"=\",\"Pending Cancellation\",false],[\"Sales Invoice\",\"docstatus\",\"=\",\"2\",false]]",
"function": "Count",
"idx": 0,
"is_public": 1,
"is_standard": 1,
"label": "Active e-Invoice, Cancelled Invoice",
"method": "india_compliance.gst_india.number_card.number_card.get_active_e_invoice_count_for_cancelled_invoices",
"modified": "2024-02-14 16:06:17.807093",
"method": "",
"modified": "2024-02-23 11:37:11.634469",
"modified_by": "Administrator",
"module": "GST India",
"name": "Invoice Cancelled But Not e-Invoice",
Expand Down
57 changes: 0 additions & 57 deletions india_compliance/gst_india/number_card/number_card.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"aggregate_function_based_on": "",
"color": "#ECAD4B",
"creation": "2023-07-25 15:10:39.976867",
"docstatus": 0,
"doctype": "Number Card",
"document_type": "Sales Invoice",
"dynamic_filters_json": "[]",
"filters_config": "",
"filters_json": "null",
"filters_json": "[[\"Sales Invoice\",\"einvoice_status\",\"=\",\"Pending\",false],[\"Sales Invoice\",\"docstatus\",\"=\",\"1\",false]]",
"function": "Count",
"idx": 0,
"is_public": 1,
"is_standard": 1,
"label": "Pending e-Invoices",
"method": "india_compliance.gst_india.number_card.number_card.get_pending_e_invoices_count",
"modified": "2023-08-31 15:33:52.183475",
"method": "",
"modified": "2024-02-23 11:37:43.073988",
"modified_by": "Administrator",
"module": "GST India",
"name": "Pending e-Invoices",
"owner": "Administrator",
"parent_document_type": "",
"report_function": "Sum",
"show_percentage_stats": 1,
"show_percentage_stats": 0,
"stats_time_interval": "Daily",
"type": "Custom"
"type": "Document Type"
}
15 changes: 11 additions & 4 deletions india_compliance/gst_india/overrides/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
)
from india_compliance.gst_india.utils.e_invoice import (
get_e_invoice_info,
if_e_invoice_can_be_cancelled,
validate_e_invoice_applicability,
validate_hsn_codes_for_e_invoice,
validate_if_e_invoice_can_be_cancelled,
)
from india_compliance.gst_india.utils.e_waybill import get_e_waybill_info
from india_compliance.gst_india.utils.transaction_data import (
Expand Down Expand Up @@ -78,7 +78,13 @@ def validate_credit_debit_note(doc):
)


def validate_fields_and_set_status_for_e_invoice(doc, gst_settings):
def validate_fields_and_set_status_for_e_invoice(doc, gst_settings=None):
if doc.docstatus == 2:
return update_e_invoice_status_for_cancelled_invoice(doc)

if not gst_settings:
gst_settings = frappe.get_cached_doc("GST Settings")

if not gst_settings.enable_e_invoice or not validate_e_invoice_applicability(
doc, gst_settings=gst_settings, throw=False
):
Expand All @@ -101,12 +107,12 @@ def validate_fields_and_set_status_for_e_invoice(doc, gst_settings):
doc.einvoice_status = "Pending"


def update_status_for_cancelled_invoice(doc, method=None):
def update_e_invoice_status_for_cancelled_invoice(doc):
if not doc.irn:
return

run_onload(doc) # to set e_invoice_info
if not if_e_invoice_can_be_cancelled(doc, throw=False):
if not validate_if_e_invoice_can_be_cancelled(doc, throw=False):
doc.db_set("einvoice_status", "Pending Cancellation")


Expand Down Expand Up @@ -176,6 +182,7 @@ def on_submit(doc, method=None):


def before_cancel(doc, method=None):
validate_fields_and_set_status_for_e_invoice(doc)
payment_references = frappe.get_all(
"Payment Entry Reference",
filters={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ frappe.query_reports["e-Invoice Summary"] = {
value = `<span class="bold" style="color: var(--text-on-red)">${value}</span>`;
else if (value == "Not Applicable")
value = `<span class="bold" style="color: var(--text-on-grey)">${value}</span>`;
else if (value == "Pending Cancellation")
value = `<span class="bold" style="color: var(--text-on-red)">${value}</span>`;

return value;
},
Expand Down
Loading

0 comments on commit b4c0261

Please sign in to comment.