Skip to content
SpencerChang edited this page Jul 24, 2016 · 3 revisions
22026, 'orderDfPrice' => 450, 'orderDfQty' => 3, ); $arr1 = array( array ( 'prodUid' => 22026, 'orderDfPrice' => 450, 'orderDfQty' => 3, ) ,array ( 'prodUid' => 1, 'orderDfPrice' => 100, 'orderDfQty' => 2, ) ); //echo count($arr1); print_r(getOffsetBonus($arr,100)); function getOffsetBonus($orderDetail, $ecoupon) { if ($ecoupon > 0) { $amount = intval(1250); if(isset($orderDetail[0])){ $total_offset = 0; $length = count($orderDetail)-1; for($index = $length;$index>=0;$index--){ // 原始各商品金額 $ori_price = intval($orderDetail[$index]['orderDfQty']) * $orderDetail[$index]['orderDfPrice']; // 尾差加在第一筆 if($index==0){ $final_price = $ori_price - ($ecoupon-$total_offset); }else{ // 計算公式: 去尾數(折扣總價 * 各商品售價 / 折扣前總價) $final_price = floor($amount * $ori_price / ($ecoupon+$amount)); // 計算總折扣 $total_offset += $ori_price - $final_price; } // 折扣後除以商品數量的餘數 $remainder = $final_price % intval($orderDetail[$index]['orderDfQty']); // 回寫 商品金額 $orderDetail[$index]['orderDfPrice'] = floor($final_price / $orderDetail[$index]['orderDfQty']); // 有餘數時處理,原數量減一,新增一個陣列是含餘數的金額 if($remainder != 0){ $orderDetail[$index]['orderDfQty']--; $new_order = array(); $new_order = $orderDetail[$index]; $new_order['orderDfPrice'] = $new_order['orderDfPrice']+$remainder; $new_order['orderDfQty'] = 1; array_push($orderDetail,$new_order); } } }else{ $final_price = (intval($orderDetail['orderDfQty']) * $orderDetail['orderDfPrice'] - $ecoupon); $remainder = $final_price % intval($orderDetail['orderDfQty']); $orderDetail['orderDfPrice'] = floor($final_price / $orderDetail['orderDfQty']); if($remainder != 0){ $orderDetail['orderDfQty']--; $new_order = array(); array_push($new_order,$orderDetail); $orderDetail['orderDfPrice'] = $orderDetail['orderDfPrice']+$remainder; $orderDetail['orderDfQty'] = 1; array_push($new_order,$orderDetail); $orderDetail = $new_order; } } return (object) $orderDetail; } else { return (object) $orderDetail; } }
Clone this wiki locally