Skip to content

Commit

Permalink
fix: set filing preference where it is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanket322 committed Nov 8, 2024
1 parent 5779790 commit 1ee0d05
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions india_compliance/gst_india/utils/gstin_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import frappe
from frappe import _
from frappe.query_builder import Case
from frappe.utils import cint, get_last_day, getdate

from india_compliance.exceptions import GSPServerError
Expand Down Expand Up @@ -402,6 +403,57 @@ def create_gst_return_log_for_quarter(gstin, log_names, filing_preference):
}
).insert()

update_log_without_filing_preference()


def update_log_without_filing_preference():
gst_return_logs = frappe.get_all(
"GST Return Log",
filters={"filing_preference": ["is", "not set"]},
fields=["name", "return_period", "gstin"],
)

if not gst_return_logs:
return

filing_preferences = {}
logs_to_update = {}

for log in gst_return_logs:
return_period = log.get("return_period")
gstin = log.get("gstin")
financial_year = get_fy(return_period)
key = f"{financial_year}:{gstin}"

if key not in filing_preferences:
start_date = getdate(f"{return_period[2:]}-{return_period[:2]}-01")
api = GSTR1API(company_gstin=gstin)
filing_preferences[key] = api.get_filing_preference(
date=start_date
).response

month = cint(return_period[:2])
quarter = (month - 1) // 3
preference_data = filing_preferences.get(key)
logs_to_update[log.get("name")] = (
"Quarterly"
if preference_data[quarter].get("preference") == "Q"
else "Monthly"
)

gst_return_log = frappe.qb.DocType("GST Return Log")
case_conditions = Case()

for name, preference_data in logs_to_update.items():
case_conditions.when(gst_return_log.name == name, preference_data)

(
frappe.qb.update(gst_return_log)
.set(gst_return_log.filing_preference, case_conditions)
.where(gst_return_log.name.isin(list(logs_to_update.keys())))
.run()
)


def get_fy(period, year_increment=0):
month, year = period[:2], period[2:]
Expand Down

0 comments on commit 1ee0d05

Please sign in to comment.