Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add filing period selection in gstr-1 beta #2669

Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,24 @@ def generate_gstr1_data(self, filters, callback=None):
"""
data = {}

from india_compliance.gst_india.utils.gstin_info import get_filing_frequency

filing_frequency = get_filing_frequency(
self.company, self.gstin, self.return_period
)
if filing_frequency and filing_frequency != self.is_quarterly:
self.is_quarterly = filing_frequency
filters.is_quarterly = filing_frequency
frappe.db.set_value(
"GST Return Log", self.name, "is_quarterly", filing_frequency
)

# APIs Disabled
if not self.is_gstr1_api_enabled(warn_for_missing_credentials=True):
return self.generate_only_books_data(data, filters, callback)

# APIs Enabled
status = self.get_return_status()
# filing_frequency = self.get_filing_frequency() & update this in filters

if status == "Filed":
gov_data_field = "filed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@
"reqd": 1
},
{
"default": "0",
"fieldname": "is_quarterly",
"fieldtype": "Check",
"fieldtype": "Data",
"label": "Is Quarterly",
"read_only": 1
}
Expand All @@ -205,7 +204,7 @@
"link_fieldname": "reference_docname"
}
],
"modified": "2024-10-18 10:51:57.440489",
"modified": "2024-11-06 17:47:55.754860",
"modified_by": "Administrator",
"module": "GST India",
"name": "GST Return Log",
Expand Down
40 changes: 39 additions & 1 deletion india_compliance/gst_india/doctype/gstr_1_beta/gstr_1_beta.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ frappe.ui.form.on(DOCTYPE, {
frm.set_value("company_gstin", options[0]);
},

company_gstin: render_empty_state,
company_gstin(frm){
render_empty_state(frm);
update_fields_based_on_filing_preference(frm);
},

month_or_quarter(frm) {
render_empty_state(frm);
update_fields_based_on_filing_preference(frm);
},

year(frm) {
Expand Down Expand Up @@ -2123,6 +2127,40 @@ function set_options_for_year(frm) {
frm.set_value("year", current_year.toString());
}

function update_fields_based_on_filing_preference(frm){
frappe.call({
method: "india_compliance.gst_india.doctype.gstr_1_beta.gstr_1_beta.get_filing_preference",
args: {month_or_quarter: frm.doc.month_or_quarter, year: frm.doc.year, company_gstin: frm.doc.company_gstin},
callback: (r) => {
const preference = r.message === undefined ? r.message : cint(r.message);

if(preference === undefined){
frm.set_df_property("is_quarterly", "read_only", 0);
return
}
if(preference === frm.doc.is_quarterly){
frm.set_df_property("is_quarterly", "read_only", 1);
return
}

frm.doc.is_quarterly = preference
frm.set_df_property("is_quarterly", "read_only", 1)
set_options_for_month_or_quarter(frm, set_only_options=true)

if(preference == 1){
const old_month_index = india_compliance.MONTH.indexOf(frm.doc.month_or_quarter)
const quarter = Math.floor(old_month_index / 3)
frm.doc.month_or_quarter = india_compliance.QUARTER[quarter]
}else{
const old_quarter_index = india_compliance.QUARTER.indexOf(frm.doc.month_or_quarter) * 3
frm.doc.month_or_quarter = india_compliance.MONTH[old_quarter_index]
}
frm.refresh_field("month_or_quarter")
frm.refresh_field("is_quarterly")
}
})
}

function set_options_for_month_or_quarter(frm, set_only_options=false) {
/**
* Set options for Month or Quarter based on the year and current date
Expand Down
8 changes: 8 additions & 0 deletions india_compliance/gst_india/doctype/gstr_1_beta/gstr_1_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,11 @@ def get_gstr_1_from_and_to_date(
to_date = get_last_day(from_date)

return from_date, to_date


@frappe.whitelist()
def get_filing_preference(month_or_quarter: str, year: str, company_gstin):
period = get_period(month_or_quarter, year)
return frappe.db.get_value(
"GST Return Log", f"GSTR1-{period}-{company_gstin}", "is_quarterly"
)
76 changes: 69 additions & 7 deletions india_compliance/gst_india/utils/gstin_info.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import json
from datetime import timedelta
from datetime import date, timedelta
from string import whitespace

import frappe
from frappe import _
from frappe.utils import getdate
from frappe.utils import cint, getdate

from india_compliance.exceptions import GSPServerError
from india_compliance.gst_india.api_classes.base import BASE_URL
from india_compliance.gst_india.api_classes.e_invoice import EInvoiceAPI
from india_compliance.gst_india.api_classes.e_waybill import EWaybillAPI
from india_compliance.gst_india.api_classes.public import PublicAPI
from india_compliance.gst_india.api_classes.taxpayer_returns import GSTR1API
from india_compliance.gst_india.doctype.gst_return_log.gst_return_log import (
process_gstr_1_returns_info,
)
from india_compliance.gst_india.utils import parse_datetime, titlecase, validate_gstin
from india_compliance.gst_india.utils.__init__ import get_month_or_quarter_dict

MONTH = list(get_month_or_quarter_dict().keys())[4:]
QUARTER = ["Jan-Mar", "Apr-Jun", "Jul-Sep", "Oct-Dec"]


GST_CATEGORIES = {
"Regular": "Registered Regular",
Expand Down Expand Up @@ -335,12 +341,68 @@ def get_gstr_1_return_status(
return "Not Filed"


def get_filing_frequency(gstin, period):
# is not Q1-M1 ? => fetch if available from Q1-M1
# if not, is it filed for the period or date has surpassed? => if not, then don't fetch and return nothing
def get_filing_frequency(company, gstin, period):
month = cint(period[:2])
year = cint(period[2:])

start_month = (month - 1) // 3 * 3 + 1
quarter = (month - 1) // 3 + 1

start_date = getdate(f"{year}-{start_month}-01")
log_name = f"GSTR1-{start_month:02d}{year}-{gstin}"

# if filed, then fetch from the filed data
pass
if filing_preference := frappe.db.get_value(
"GST Return Log", log_name, "is_quarterly"
Sanket322 marked this conversation as resolved.
Show resolved Hide resolved
):
return filing_preference

filing_due_date = date(year, start_month + 1, 13)
Sanket322 marked this conversation as resolved.
Show resolved Hide resolved
if (
frappe.db.get_value("GST Return Log", log_name, "filing_status") != "Filed"
and getdate() < filing_due_date
):
return

api = GSTR1API(company_gstin=gstin)
response = api.get_filing_preference(date=start_date).response

filing_preference = 1 if response[quarter].get("preference") == "Q" else 0
frappe.enqueue(
create_gst_return_log_for_quarter,
company,
gstin,
start_month,
year,
filing_preference,
)

return filing_preference


def create_gst_return_log_for_quarter(
company_name, gstin, start_month, year, filing_frequency
):
return_periods = []

for month_offset in range(3):
current_month = start_month + month_offset
period = f"{current_month:02d}{year}"
return_periods.append(period)

for period in return_periods:
try:
frappe.get_doc(
{
"doctype": "GST Return Log",
"company": company_name,
"gstin": gstin,
"return_period": period,
"return_type": "GSTR1",
"is_quarterly": filing_frequency,
}
).insert()
except frappe.DuplicateEntryError:
pass


def get_fy(period, year_increment=0):
Expand Down