From 961abbcd5ad3de2879d9fa64c821fd102ba5bab7 Mon Sep 17 00:00:00 2001 From: Sanket322 Date: Mon, 22 Apr 2024 15:02:52 +0530 Subject: [PATCH] fix: remove extra inward supplies and unlink invoice (cherry picked from commit 196921e0d4e812f05606c9fbb123211c2a8498d3) --- .../gst_inward_supply/gst_inward_supply.py | 5 ++++ india_compliance/gst_india/utils/gstr/gstr.py | 16 ++++++++++++ .../gst_india/utils/gstr/gstr_2a.py | 25 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/india_compliance/gst_india/doctype/gst_inward_supply/gst_inward_supply.py b/india_compliance/gst_india/doctype/gst_inward_supply/gst_inward_supply.py index 4663c1ef62..d2b822d68c 100644 --- a/india_compliance/gst_india/doctype/gst_inward_supply/gst_inward_supply.py +++ b/india_compliance/gst_india/doctype/gst_inward_supply/gst_inward_supply.py @@ -22,6 +22,11 @@ def before_save(self): ): update_docs_for_amendment(self) + def on_trash(self): + frappe.db.set_value( + "Purchase Invoice", self.link_name, "reconciliation_status", "Unreconciled" + ) + def create_inward_supply(transaction): filters = { diff --git a/india_compliance/gst_india/utils/gstr/gstr.py b/india_compliance/gst_india/utils/gstr/gstr.py index 6b7d16eb23..7bb907aec3 100644 --- a/india_compliance/gst_india/utils/gstr/gstr.py +++ b/india_compliance/gst_india/utils/gstr/gstr.py @@ -51,6 +51,7 @@ def __init__(self, company, gstin, return_period, data, gen_date_2b): self.setup() def setup(self): + self.existing_transaction = {} pass def create_transactions(self, category, suppliers): @@ -75,6 +76,20 @@ def create_transactions(self, category, suppliers): doctype="Purchase Reconciliation Tool", ) + if transaction.get("unique_key") in self.existing_transaction: + self.existing_transaction.pop(transaction.get("unique_key")) + + self.delete_missing_transactions() + + def delete_missing_transactions(self): + """ + For GSTR2a, transactions are reflected immediately after it's pushed to GSTR-1. + At times, it may later be removed from GSTR-1. + + In such cases, we need to delete such unfilled transactions not present in the latest data. + """ + return + def get_all_transactions(self, category, suppliers): transactions = [] for supplier in suppliers: @@ -101,6 +116,7 @@ def get_transaction(self, category, supplier, invoice): **self.get_supplier_details(supplier), **self.get_invoice_details(invoice), items=self.get_transaction_items(invoice), + unique_key=(supplier.ctin or "") + "-" + (invoice.inum or ""), ) def get_supplier_details(self, supplier): diff --git a/india_compliance/gst_india/utils/gstr/gstr_2a.py b/india_compliance/gst_india/utils/gstr/gstr_2a.py index ca9767d450..be13c8b4c4 100644 --- a/india_compliance/gst_india/utils/gstr/gstr_2a.py +++ b/india_compliance/gst_india/utils/gstr/gstr_2a.py @@ -16,6 +16,31 @@ class GSTR2a(GSTR): def setup(self): self.all_gstins = set() self.cancelled_gstins = {} + self.existing_transaction = self.get_existing_transaction() + + def get_existing_transaction(self): + category = type(self).__name__[6:] + + gst_is = frappe.qb.DocType("GST Inward Supply") + existing_transactions = ( + frappe.qb.from_(gst_is) + .select(gst_is.name, gst_is.supplier_gstin, gst_is.bill_no) + .where(gst_is.sup_return_period == self.return_period) + .where(gst_is.classification == category) + .where(gst_is.gstr_1_filled == 0) + ).run(as_dict=True) + + return { + transaction.get("supplier_gstin") + + "-" + + transaction.get("bill_no"): transaction.get("name") + for transaction in existing_transactions + } + + def delete_missing_transactions(self): + if self.existing_transaction: + for value in self.existing_transaction.values(): + frappe.delete_doc("GST Inward Supply", value) def get_supplier_details(self, supplier): supplier_details = {