@@ -421,6 +421,7 @@ def process(self):
421
421
422
422
if grouped_by_invoice :
423
423
buying_amount = 0
424
+ base_amount = 0
424
425
425
426
for row in reversed (self .si_list ):
426
427
if self .filters .get ("group_by" ) == "Monthly" :
@@ -461,12 +462,11 @@ def process(self):
461
462
else :
462
463
row .buying_amount = flt (self .get_buying_amount (row , row .item_code ), self .currency_precision )
463
464
464
- if grouped_by_invoice :
465
- if row .indent == 1.0 :
466
- buying_amount += row .buying_amount
467
- elif row .indent == 0.0 :
468
- row .buying_amount = buying_amount
469
- buying_amount = 0
465
+ if grouped_by_invoice and row .indent == 0.0 :
466
+ row .buying_amount = buying_amount
467
+ row .base_amount = base_amount
468
+ buying_amount = 0
469
+ base_amount = 0
470
470
471
471
# get buying rate
472
472
if flt (row .qty ):
@@ -476,11 +476,19 @@ def process(self):
476
476
if self .is_not_invoice_row (row ):
477
477
row .buying_rate , row .base_rate = 0.0 , 0.0
478
478
479
+ if self .is_not_invoice_row (row ):
480
+ self .update_return_invoices (row )
481
+
482
+ if grouped_by_invoice and row .indent == 1.0 :
483
+ buying_amount += row .buying_amount
484
+ base_amount += row .base_amount
485
+
479
486
# calculate gross profit
480
487
row .gross_profit = flt (row .base_amount - row .buying_amount , self .currency_precision )
481
488
if row .base_amount :
482
489
row .gross_profit_percent = flt (
483
- (row .gross_profit / row .base_amount ) * 100.0 , self .currency_precision
490
+ (row .gross_profit / row .base_amount ) * 100.0 ,
491
+ self .currency_precision ,
484
492
)
485
493
else :
486
494
row .gross_profit_percent = 0.0
@@ -491,33 +499,29 @@ def process(self):
491
499
if self .grouped :
492
500
self .get_average_rate_based_on_group_by ()
493
501
502
+ def update_return_invoices (self , row ):
503
+ if row .parent in self .returned_invoices and row .item_code in self .returned_invoices [row .parent ]:
504
+ returned_item_rows = self .returned_invoices [row .parent ][row .item_code ]
505
+ for returned_item_row in returned_item_rows :
506
+ # returned_items 'qty' should be stateful
507
+ if returned_item_row .qty != 0 :
508
+ if row .qty >= abs (returned_item_row .qty ):
509
+ row .qty += returned_item_row .qty
510
+ row .base_amount += flt (returned_item_row .base_amount , self .currency_precision )
511
+ returned_item_row .qty = 0
512
+ returned_item_row .base_amount = 0
513
+
514
+ else :
515
+ row .qty = 0
516
+ row .base_amount = 0
517
+ returned_item_row .qty += row .qty
518
+ returned_item_row .base_amount += row .base_amount
519
+
520
+ row .buying_amount = flt (flt (row .qty ) * flt (row .buying_rate ), self .currency_precision )
521
+
494
522
def get_average_rate_based_on_group_by (self ):
495
523
for key in list (self .grouped ):
496
- if self .filters .get ("group_by" ) == "Invoice" :
497
- for row in self .grouped [key ]:
498
- if row .indent == 1.0 :
499
- if (
500
- row .parent in self .returned_invoices
501
- and row .item_code in self .returned_invoices [row .parent ]
502
- ):
503
- returned_item_rows = self .returned_invoices [row .parent ][row .item_code ]
504
- for returned_item_row in returned_item_rows :
505
- # returned_items 'qty' should be stateful
506
- if returned_item_row .qty != 0 :
507
- if row .qty >= abs (returned_item_row .qty ):
508
- row .qty += returned_item_row .qty
509
- returned_item_row .qty = 0
510
- else :
511
- row .qty = 0
512
- returned_item_row .qty += row .qty
513
- row .base_amount += flt (returned_item_row .base_amount , self .currency_precision )
514
- row .buying_amount = flt (
515
- flt (row .qty ) * flt (row .buying_rate ), self .currency_precision
516
- )
517
- if flt (row .qty ) or row .base_amount :
518
- row = self .set_average_rate (row )
519
- self .grouped_data .append (row )
520
- elif self .filters .get ("group_by" ) == "Payment Term" :
524
+ if self .filters .get ("group_by" ) == "Payment Term" :
521
525
for i , row in enumerate (self .grouped [key ]):
522
526
invoice_portion = 0
523
527
@@ -537,7 +541,7 @@ def get_average_rate_based_on_group_by(self):
537
541
538
542
new_row = self .set_average_rate (new_row )
539
543
self .grouped_data .append (new_row )
540
- else :
544
+ elif self . filters . get ( "group_by" ) != "Invoice" :
541
545
for i , row in enumerate (self .grouped [key ]):
542
546
if i == 0 :
543
547
new_row = row
0 commit comments