diff --git a/india_compliance/gst_india/doctype/purchase_reconciliation_tool/__init__.py b/india_compliance/gst_india/doctype/purchase_reconciliation_tool/__init__.py index 03ca4afd2..1a5387916 100644 --- a/india_compliance/gst_india/doctype/purchase_reconciliation_tool/__init__.py +++ b/india_compliance/gst_india/doctype/purchase_reconciliation_tool/__init__.py @@ -301,6 +301,7 @@ def get_query(self, additional_fields=None): .left_join(self.GSTR2_ITEM) .on(self.GSTR2_ITEM.parent == self.GSTR2.name) .where(IfNull(self.GSTR2.match_status, "") != "Amended") + .where(self.GSTR2_ITEM.parenttype == "GST Inward Supply") .groupby(self.GSTR2_ITEM.parent) .select(*fields, ConstantColumn("GST Inward Supply").as_("doctype")) ) @@ -351,7 +352,7 @@ def __init__(self, **kwargs): self.__dict__.update(kwargs) self.PI = frappe.qb.DocType("Purchase Invoice") - self.PI_TAX = frappe.qb.DocType("Purchase Taxes and Charges") + self.PI_ITEM = frappe.qb.DocType("Purchase Invoice Item") def get_all(self, additional_fields=None, names=None, only_names=False): query = self.get_query(additional_fields) @@ -409,32 +410,19 @@ def get_unmatched(self, category): return BaseUtil.get_dict_for_key("supplier_gstin", data) def get_query(self, additional_fields=None, is_return=False): - PI_ITEM = frappe.qb.DocType("Purchase Invoice Item") - fields = self.get_fields(additional_fields, is_return) - pi_item = ( - frappe.qb.from_(PI_ITEM) - .select( - Abs(Sum(PI_ITEM.taxable_value)).as_("taxable_value"), - PI_ITEM.parent, - ) - .groupby(PI_ITEM.parent) - ) query = ( frappe.qb.from_(self.PI) - .left_join(self.PI_TAX) - .on(self.PI_TAX.parent == self.PI.name) - .left_join(pi_item) - .on(pi_item.parent == self.PI.name) + .left_join(self.PI_ITEM) + .on(self.PI_ITEM.parent == self.PI.name) .where(self.PI.docstatus == 1) .where(IfNull(self.PI.reconciliation_status, "") != "Not Applicable") .where(self.PI.is_opening == "NO") - .where(self.PI_TAX.parenttype == "Purchase Invoice") + .where(self.PI_ITEM.parenttype == "Purchase Invoice") .groupby(self.PI.name) .select( *fields, - pi_item.taxable_value, ConstantColumn("Purchase Invoice").as_("doctype"), ) ) @@ -451,7 +439,8 @@ def get_query(self, additional_fields=None, is_return=False): def get_fields(self, additional_fields=None, is_return=False): tax_fields = [ - self.query_tax_amount(tax_type).as_(tax_type) for tax_type in GST_TAX_TYPES + self.query_tax_amount(f"{tax_type}_amount").as_(tax_type) + for tax_type in GST_TAX_TYPES ] fields = [ @@ -461,6 +450,7 @@ def get_fields(self, additional_fields=None, is_return=False): "bill_no", "place_of_supply", "is_reverse_charge", + Abs(Sum(self.PI_ITEM.taxable_value)).as_("taxable_value"), *tax_fields, ] @@ -484,17 +474,8 @@ def get_fields(self, additional_fields=None, is_return=False): return fields - def query_tax_amount(self, gst_tax_type): - return Abs( - Sum( - Case() - .when( - self.PI_TAX.gst_tax_type == gst_tax_type, - self.PI_TAX.base_tax_amount_after_discount_amount, - ) - .else_(0) - ) - ) + def query_tax_amount(self, field): + return Abs(Sum(getattr(self.PI_ITEM, field))) @staticmethod def query_matched_purchase_invoice(from_date=None, to_date=None): @@ -520,7 +501,7 @@ def __init__(self, **kwargs): self.__dict__.update(kwargs) self.BOE = frappe.qb.DocType("Bill of Entry") - self.BOE_TAX = frappe.qb.DocType("Bill of Entry Taxes") + self.BOE_ITEM = frappe.qb.DocType("Bill of Entry Item") self.PI = frappe.qb.DocType("Purchase Invoice") def get_all(self, additional_fields=None, names=None, only_names=False): @@ -577,12 +558,13 @@ def get_query(self, additional_fields=None): query = ( frappe.qb.from_(self.BOE) - .left_join(self.BOE_TAX) - .on(self.BOE_TAX.parent == self.BOE.name) + .left_join(self.BOE_ITEM) + .on(self.BOE_ITEM.parent == self.BOE.name) .join(self.PI) .on(self.BOE.purchase_invoice == self.PI.name) .where(self.BOE.docstatus == 1) .where(IfNull(self.BOE.reconciliation_status, "") != "Not Applicable") + .where(self.BOE_ITEM.parenttype == "Bill of Entry") .groupby(self.BOE.name) .select(*fields, ConstantColumn("Bill of Entry").as_("doctype")) ) @@ -594,7 +576,8 @@ def get_query(self, additional_fields=None): def get_fields(self, additional_fields=None): tax_fields = [ - self.query_tax_amount(tax_type).as_(tax_type) for tax_type in GST_TAX_TYPES + self.query_tax_amount(f"{tax_type}_amount").as_(tax_type) + for tax_type in GST_TAX_TYPES ] fields = [ @@ -629,17 +612,8 @@ def get_fields(self, additional_fields=None): return fields - def query_tax_amount(self, gst_tax_type): - return Abs( - Sum( - Case() - .when( - self.BOE_TAX.gst_tax_type == gst_tax_type, - self.BOE_TAX.tax_amount, - ) - .else_(0) - ) - ) + def query_tax_amount(self, field): + return Abs(Sum(getattr(self.BOE_ITEM, field))) @staticmethod def query_matched_bill_of_entry(from_date=None, to_date=None):