@@ -133,38 +133,41 @@ public function subTotalDiscountedAmount(): Money
133133     */ 
134134    public  function  totalTaxAmount (): Money 
135135    {
136-         if  (empty ($ this items )) {
137-             return  Money::of (0 , $ this getCurrency ());
138-         }
139- 
140136        $ totalDiscount$ this totalDiscountAmount ();
141137
142138        /** 
143-          * Taxes must be calculated on the discounted subtotal. 
144-          * Since discounts apply  at the invoice level and  taxes at the item level, 
145-          * we allocate the discount across items before computing taxes. 
139+          * Taxes must be calculated based  on the discounted subtotal. 
140+          * Since discounts are applied  at the invoice level, but  taxes are calculated  at the item level, 
141+          * we allocate the total  discount proportionally  across individual  items before computing taxes. 
146142         */ 
147-         $ allocatedDiscounts $ totalDiscount -> allocate (... array_map (
143+         $ ratios array_map (
148144            fn  ($ item$ itemsubTotalAmount ()->abs ()->getMinorAmount ()->toInt (),
149145            $ this items 
150-         ));
146+         );
147+ 
148+         if  (array_sum ($ ratios0 ) {
149+             return  Money::of (0 , $ this getCurrency ());
150+         }
151+ 
152+         $ allocatedDiscounts$ totalDiscountallocate (...$ ratios
151153
152154        $ totalTaxAmountof (0 , $ this getCurrency ());
153155
154156        foreach  ($ this items  as  $ index$ item
155157
156158            if  ($ itemunit_tax ) {
157159                /** 
158-                  * When unit_tax is defined, the amount is considered right 
159-                  * and the discount is not apply 
160+                  * When unit_tax is defined, the amount is considered correct 
160161                 */ 
161162                $ itemTaxAmount$ itemunit_tax ->multipliedBy ($ itemquantity );
162163            } elseif  ($ itemtax_percentage ) {
164+ 
163165                $ itemDiscount$ allocatedDiscounts$ index
164166
165167                $ itemTaxAmount$ itemsubTotalAmount ()
166168                    ->minus ($ itemDiscount
167169                    ->multipliedBy ($ itemtax_percentage  / 100.0 , roundingMode: RoundingMode::HALF_EVEN );
170+ 
168171            } else  {
169172                $ itemTaxAmountof (0 , $ totalTaxAmountgetCurrency ());
170173            }
0 commit comments