@@ -120,15 +120,33 @@ public function updateItemPrice(int $index, float $newPrice): void
120120 */
121121 public function applyDiscountToItems (array $ indexes , float $ totalDiscount ): void
122122 {
123- $ totalPrice = 0 ;
123+ if (empty ($ indexes ) || $ totalDiscount <= 0 ) {
124+ return ;
125+ }
126+ $ totalPrice = 0.0 ;
124127 foreach ($ indexes as $ i ) {
125128 $ totalPrice += $ this ->items [$ i ]['price ' ] * $ this ->items [$ i ]['qty ' ];
126129 }
130+ if ($ totalPrice <= 0 ) {
131+ return ;
132+ }
133+
134+ $ distributed = 0.0 ;
135+ $ lastIndex = $ indexes [array_key_last ($ indexes )];
127136 foreach ($ indexes as $ i ) {
128137 $ item = &$ this ->items [$ i ];
129- $ shop_name [] = $ item ['name ' ];
130- $ share = round (($ item ['price ' ] * $ item ['qty ' ]) / $ totalPrice , 4 );
131- $ item ['price ' ] -= round (($ totalDiscount * $ share ) / $ item ['qty ' ], 2 );
138+ $ share = ($ item ['price ' ] * $ item ['qty ' ]) / $ totalPrice ;
139+ $ unitReduction = round (($ totalDiscount * $ share ) / $ item ['qty ' ], 2 );
140+ $ distributed += $ unitReduction * $ item ['qty ' ];
141+ $ item ['price ' ] = max (0 , round ($ item ['price ' ] - $ unitReduction , 2 ));
142+ unset($ item );
143+ }
144+
145+ // 尾差补偿,保证分摊总额与 totalDiscount 一致
146+ $ remainder = round ($ totalDiscount - $ distributed , 2 );
147+ if (abs ($ remainder ) > 0 ) {
148+ $ qty = $ this ->items [$ lastIndex ]['qty ' ];
149+ $ this ->items [$ lastIndex ]['price ' ] = max (0 , round ($ this ->items [$ lastIndex ]['price ' ] - ($ remainder / $ qty ), 2 ));
132150 }
133151 }
134152
0 commit comments