@@ -588,22 +588,32 @@ protected static function getPayableItems(array $params, array $contribution): a
588
588
else {
589
589
$ item ['allocation ' ] = round ($ item ['balance ' ] * $ ratio , 2 );
590
590
}
591
-
592
- if (!empty ($ item ['tax_amount ' ])) {
593
- $ item ['tax_allocation ' ] = round ($ item ['tax_amount ' ] * ($ params ['total_amount ' ] / $ contribution ['total_amount ' ]), 2 );
594
- }
595
591
}
596
592
$ payableItems [$ payableItemIndex ] = $ item ;
597
593
}
598
594
595
+ // Custom patch to correct roundoff errors
599
596
if (empty ($ lineItemOverrides ) && !empty ($ ratio ) && isset ($ payableItems [$ payableItemIndex ])) {
600
- $ totalTaxAllocation = array_sum (array_column ($ payableItems , 'tax_allocation ' ));
601
- $ totalAllocation = array_sum (array_column ($ payableItems , 'allocation ' ));
597
+ $ totalTaxAllocation = 0 ;
598
+ $ totalAllocation = 0 ;
599
+ $ lastNonTaxKey = $ payableItemIndex ;
600
+
601
+ foreach ($ payableItems as $ key => $ item ) {
602
+ if ($ item ['financial_item.financial_account_id.is_tax ' ]) {
603
+ $ totalTaxAllocation += $ item ['allocation ' ];
604
+ }
605
+ else {
606
+ $ totalAllocation += $ item ['allocation ' ];
607
+ $ lastNonTaxKey = $ key ;
608
+ }
609
+ }
610
+
602
611
$ total = $ totalTaxAllocation + $ totalAllocation ;
603
612
$ leftPayment = $ params ['total_amount ' ] - $ total ;
604
-
605
- // assign any leftover amount, to the last lineitem
606
- $ payableItems [$ payableItemIndex ]['allocation ' ] += $ leftPayment ;
613
+
614
+ if ($ lastNonTaxKey !== NULL ) {
615
+ $ payableItems [$ lastNonTaxKey ]['allocation ' ] += $ leftPayment ;
616
+ }
607
617
}
608
618
609
619
return $ payableItems ;
0 commit comments