Skip to content

Commit

Permalink
fix: Merge branch 'gstr-1-automation' into quartely_in_gstr_1_beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanket322 committed Nov 9, 2024
2 parents 1ee0d05 + 29f5f53 commit 4e8365b
Show file tree
Hide file tree
Showing 22 changed files with 1,608 additions and 76 deletions.
11 changes: 7 additions & 4 deletions india_compliance/gst_india/api_classes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def get(self, *args, **kwargs):
def post(self, *args, **kwargs):
return self._make_request("POST", *args, **kwargs)

def put(self, *args, **kwargs):
return self._make_request("PUT", *args, **kwargs)

def _make_request(
self,
method,
Expand All @@ -94,7 +97,7 @@ def _make_request(
json=None,
):
method = method.upper()
if method not in ("GET", "POST"):
if method not in ("GET", "POST", "PUT"):
frappe.throw(_("Invalid method {0}").format(method))

request_args = frappe._dict(
Expand All @@ -108,15 +111,14 @@ def _make_request(
)

log_headers = request_args.headers.copy()

log = frappe._dict(
**self.default_log_values,
url=request_args.url,
data=request_args.params,
request_headers=log_headers,
)

if method == "POST" and json:
if method in ["POST", "PUT"] and json:
request_args.json = json

json_data = json.copy()
Expand All @@ -135,6 +137,7 @@ def _make_request(

response = requests.request(method, **request_args)
if api_request_id := response.headers.get("x-amzn-RequestId"):
self.request_id = api_request_id
log.request_id = api_request_id

try:
Expand Down Expand Up @@ -258,7 +261,7 @@ def handle_http_code(self, status_code, response_json):
raise GatewayTimeoutError

def generate_request_id(self, length=12):
return frappe.generate_hash(length=length)
return f"IC{frappe.generate_hash(length=length - 2)}".upper()

def mask_sensitive_info(self, log):
request_headers = log.request_headers
Expand Down
35 changes: 35 additions & 0 deletions india_compliance/gst_india/api_classes/taxpayer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class TaxpayerAuthenticate(BaseAPI):
# "AUTH4034": "invalid_otp", # Invalid OTP
"AUTH4038": "authorization_failed", # Session Expired
"TEC4002": "invalid_public_key",
"RET13506": "OTP is either expired or incorrect",
"RET00003": "Return Form already ready to be filed", # Actions performed on portal directly
"RET09001": "Latest Summary is not available. Please generate summary and try again.", # Actions performed on portal directly
}

def request_otp(self):
Expand Down Expand Up @@ -189,6 +192,13 @@ def refresh_auth_token(self):
endpoint="authenticate",
)

def initiate_otp_for_evc(self, pan, form_type):
return self.get(
action="EVCOTP",
params={"pan": pan, "form_type": form_type},
endpoint="authenticate",
)

def decrypt_response(self, response):
values = {}

Expand Down Expand Up @@ -317,6 +327,7 @@ def _request(
self,
method,
action=None,
return_type=None,
return_period=None,
params=None,
endpoint=None,
Expand All @@ -331,6 +342,10 @@ def _request(
return response

headers = {"auth-token": auth_token}
if return_type:
headers["rtn_typ"] = return_type
headers["userrole"] = return_type

if return_period:
headers["ret_period"] = return_period

Expand All @@ -353,6 +368,9 @@ def get(self, *args, **kwargs):
def post(self, *args, **kwargs):
return self._request("post", *args, **kwargs)

def put(self, *args, **kwargs):
return self._request("put", *args, **kwargs)

def before_request(self, request_args):
self.encrypt_request(request_args.get("json"))

Expand Down Expand Up @@ -382,6 +400,23 @@ def decrypt_response(self, response):

return response

def encrypt_request(self, json):
if not json:
return

super().encrypt_request(json)

if json.get("data"):
b64_data = b64encode(frappe.as_json(json.get("data")).encode())
json["data"] = aes_encrypt_data(b64_data.decode(), self.session_key)

if json.get("st") == "EVC":
sid_key = json.get("sid").encode()
json["sign"] = hmac_sha256(b64_data, sid_key)

else:
json["hmac"] = hmac_sha256(b64_data, self.session_key)

def handle_error_response(self, response):
success_value = response.get("status_cd") != 0

Expand Down
59 changes: 59 additions & 0 deletions india_compliance/gst_india/api_classes/taxpayer_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ def download_files(self, return_period, token, otp=None):
return_period, token, action="FILEDET", endpoint="returns", otp=otp
)

def get_return_status(self, return_period, reference_id, otp=None):
return self.get(
action="RETSTATUS",
return_period=return_period,
params={"ret_period": return_period, "ref_id": reference_id},
endpoint="returns",
otp=otp,
)

def proceed_to_file(self, return_type, return_period, otp=None):
return self.post(
return_type=return_type,
return_period=return_period,
json={
"action": "RETNEWPTF",
"data": {
"gstin": self.company_gstin,
"ret_period": return_period,
}, # "isnil": "N" / "Y"
},
endpoint="returns/gstrptf",
otp=otp,
)


class GSTR2bAPI(ReturnsAPI):
API_NAME = "GSTR-2B"
Expand Down Expand Up @@ -75,6 +99,7 @@ def setup(self, doc=None, *, company_gstin=None):
super().setup(company_gstin=company_gstin)

def get_gstr_1_data(self, action, return_period, otp=None):
# action: RETSUM for summary
return self.get(
action=action,
return_period=return_period,
Expand All @@ -91,3 +116,37 @@ def get_einvoice_data(self, section, return_period, otp=None):
endpoint="returns/einvoice",
otp=otp,
)

def save_gstr_1_data(self, return_period, data, otp=None):
return self.put(
return_period=return_period,
json={"action": "RETSAVE", "data": data},
endpoint="returns/gstr1",
otp=otp,
)

def reset_gstr_1_data(self, return_period, otp=None):
return self.post(
return_period=return_period,
json={
"action": "RESET",
"data": {
"gstin": self.company_gstin,
"ret_period": return_period,
},
},
endpoint="returns/gstr1",
otp=otp,
)

def file_gstr_1(self, return_period, summary_data, pan, evc_otp):
return self.post(
return_period=return_period,
json={
"action": "RETFILE",
"data": summary_data,
"st": "EVC",
"sid": f"{pan}|{evc_otp}",
},
endpoint="returns/gstr1",
)
10 changes: 1 addition & 9 deletions india_compliance/gst_india/client_scripts/party.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,7 @@ function validate_pan(doctype) {
let { pan } = frm.doc;
if (!pan || pan.length < 10) return;

if (pan.length > 10) {
frappe.throw(__("PAN should be 10 characters long"));
}

pan = pan.trim().toUpperCase();

if (!PAN_REGEX.test(pan)) {
frappe.throw(__("Invalid PAN format"));
}
pan = india_compliance.validate_pan(pan);

frm.doc.pan = pan;
frm.refresh_field("pan");
Expand Down
Loading

0 comments on commit 4e8365b

Please sign in to comment.