99 GST_REFUND_TAX_TYPES ,
1010 GST_TAX_RATES ,
1111 GST_TAX_TYPES ,
12+ < << << << HEAD
1213 SUBCONTRACTING_DOCTYPES ,
1314 TAXABLE_GST_TREATMENTS ,
15+ == == == =
16+ >> >> >> > 82699 b04 (refactor : Use GST Details for e - Waybill and e - Invoice Transactions (#3227))
1417)
1518from india_compliance .gst_india .constants .e_waybill import (
1619 TRANSPORT_MODES ,
@@ -72,13 +75,29 @@ def set_transaction_details(self):
7275
7376 total = 0
7477 total_taxable_value = 0
78+ tax_total_keys = tuple (f"total_{ tax } _amount" for tax in GST_TAX_TYPES )
79+
80+ # Initialize all tax totals to 0
81+ self .transaction_details .update ({key : 0 for key in tax_total_keys })
7582
7683 for row in self .doc .items :
7784 total += row .taxable_value
7885
7986 if row .gst_treatment in TAXABLE_GST_TREATMENTS :
8087 total_taxable_value += row .taxable_value
8188
89+ if self .is_purchase_rcm :
90+ continue
91+
92+ # eg: Skip reverse charge tax for e-Waybill
93+ if self .doc .get ("is_reverse_charge" ) and getattr (
94+ self , "exclude_reverse_charge_tax" , False
95+ ):
96+ continue
97+
98+ for tax_key in tax_total_keys :
99+ self .transaction_details [tax_key ] += abs (row .get (tax_key [6 :], 0 ))
100+
82101 self .transaction_details .update (
83102 {
84103 "company_name" : self .sanitize_value (self .doc .company ),
@@ -95,9 +114,7 @@ def set_transaction_details(self):
95114 self .rounded (total - total_taxable_value )
96115 ),
97116 "rounding_adjustment" : rounding_adjustment ,
98- "grand_total" : abs (
99- self .doc .get (grand_total_fieldname )
100- ), # rounded after updating refund amounts
117+ "grand_total" : abs (self .rounded (self .doc .get (grand_total_fieldname ))),
101118 "grand_total_in_foreign_currency" : (
102119 abs (self .rounded (self .doc .grand_total ))
103120 if self .doc .get ("currency" , "INR" ) != "INR"
@@ -113,51 +130,37 @@ def set_transaction_details(self):
113130 "other_charges" : 0 ,
114131 }
115132 )
133+
134+ # Round tax totals
135+ for tax_key in tax_total_keys :
136+ self .transaction_details [tax_key ] = self .rounded (
137+ self .transaction_details [tax_key ]
138+ )
139+
116140 self .update_transaction_details ()
117- self .update_transaction_tax_details ( )
141+ self .update_discount_and_other_charges ( tax_total_keys )
118142
119143 def update_transaction_details (self ):
120- # to be overrridden
144+ # to be overridden
121145 pass
122146
123- def update_transaction_tax_details (self ):
124- tax_total_keys = tuple (f"total_{ tax } _amount" for tax in GST_TAX_TYPES )
125-
126- for key in tax_total_keys :
127- self .transaction_details [key ] = 0
128-
147+ def update_totals_for_refund (self ):
129148 for row in self .doc .taxes :
130- if row .gst_tax_type in GST_REFUND_TAX_TYPES :
131- self .transaction_details .grand_total -= (
132- row .base_tax_amount_after_discount_amount
133- )
134- continue
135-
136- if (
137- not row .tax_amount
138- or self .is_purchase_rcm
139- or row .gst_tax_type not in GST_TAX_TYPES
140- ):
149+ if row .gst_tax_type not in GST_REFUND_TAX_TYPES :
141150 continue
142151
143- # eg: Skip reverse charge tax for e-Waybill
144- if self .doc .get ("is_reverse_charge" ) and getattr (
145- self , "exclude_reverse_charge_tax" , False
146- ):
147- continue
148-
149- tax_key = f"total_{ row .gst_tax_type } _amount"
150- tax_amount = (
151- row .get ("base_tax_amount_after_discount_amount" ) or row .tax_amount
152+ self .transaction_details .grand_total -= (
153+ row .base_tax_amount_after_discount_amount
152154 )
153- self .transaction_details .setdefault (tax_key , 0 )
154- self .transaction_details [tax_key ] += abs (self .rounded (tax_amount ))
155155
156- # Ensure that grand total is rounded as it is updated above
156+ # Ensure that grand total is rounded as it may be updated above
157157 self .transaction_details .grand_total = self .rounded (
158158 self .transaction_details .grand_total
159159 )
160160
161+ def update_discount_and_other_charges (self , tax_total_keys ):
162+ self .update_totals_for_refund () # Ensure grand total is correct for refund
163+
161164 # Other Charges
162165 current_total = 0
163166
@@ -349,7 +352,13 @@ def group_same_items(self):
349352 item = grouped_items .setdefault (
350353 row .item_code ,
351354 frappe ._dict (
352- {** row .as_dict (), "idx" : 0 , "qty" : 0.00 , "taxable_value" : 0.00 }
355+ {
356+ ** row .as_dict (),
357+ "idx" : 0 ,
358+ "qty" : 0.00 ,
359+ "taxable_value" : 0.00 ,
360+ ** {f"{ tax } _amount" : 0.00 for tax in GST_TAX_TYPES },
361+ },
353362 ),
354363 )
355364
@@ -360,6 +369,9 @@ def group_same_items(self):
360369 item .qty += row .qty
361370 item .taxable_value += row .taxable_value
362371
372+ for tax in GST_TAX_TYPES :
373+ item [f"{ tax } _amount" ] += row .get (f"{ tax } _amount" , 0 )
374+
363375 return list (grouped_items .values ())
364376
365377 def set_item_list (self ):
@@ -373,11 +385,17 @@ def update_item_details(self, item_details, item):
373385 pass
374386
375387 def update_item_tax_details (self , item_details , item ):
376- if self .doc .doctype in SUBCONTRACTING_DOCTYPES :
377- self .update_item_tax_details_using_item_gst_details (item_details , item )
388+ for tax in GST_TAX_TYPES :
389+ tax_amount = self .get_progressive_item_tax_amount (
390+ item .get (f"{ tax } _amount" ), tax
391+ )
378392
379- else :
380- self .update_item_tax_details_using_taxes (item_details , item )
393+ item_details .update (
394+ {
395+ f"{ tax } _amount" : tax_amount ,
396+ f"{ tax } _rate" : item .get (f"{ tax } _rate" ),
397+ }
398+ )
381399
382400 tax_rate = sum (
383401 self .rounded (item_details .get (f"{ tax } _rate" , 0 ), 3 )
@@ -401,6 +419,7 @@ def update_item_tax_details(self, item_details, item):
401419 }
402420 )
403421
422+ < << << << HEAD
404423 def update_item_tax_details_using_taxes (self , item_details , item ):
405424 for tax in GST_TAX_TYPES :
406425 item_details .update ({f"{ tax } _amount" : 0 , f"{ tax } _rate" : 0 })
@@ -447,6 +466,8 @@ def update_item_tax_details_using_item_gst_details(self, item_details, item):
447466 }
448467 )
449468
469+ == == == =
470+ >> >> >> > 82699 b04 (refactor : Use GST Details for e - Waybill and e - Invoice Transactions (#3227))
450471 def get_progressive_item_tax_amount (self , amount , tax_type ):
451472 """
452473 Helper function to calculate progressive tax amount for an item to remove
0 commit comments