|
|
@@ -129,56 +129,63 @@ class OwnerPriceOperationService
|
|
|
if (!$rule->items)continue; //不存在子规则跳出
|
|
|
$isDiscount = false; //是否存在满减
|
|
|
$discountIndex = 0;
|
|
|
+ $targetValue = 0;
|
|
|
if ($type=='出库' && $rule->discount_count){
|
|
|
foreach (array_reverse(explode(",",$rule->discount_count),true) as $index=>$discount){
|
|
|
- if ($total >= $rule->discount_count){
|
|
|
+ if ($total >= $discount){
|
|
|
$isDiscount = true; //第一个满足满减条件
|
|
|
$discountIndex = $index;
|
|
|
+ $targetValue = $discount;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //满减存在且未被标记过处理时间或处理时间不为本月,处理历史即时账单
|
|
|
- if ($isDiscount && (!$rule->discount_date || substr($rule->discount_date,0,7)!=date("Y-m"))){
|
|
|
- try{
|
|
|
- DB::beginTransaction();
|
|
|
- $month = date("Y-m");
|
|
|
- $day = date("t",strtotime($month));
|
|
|
- foreach (OwnerFeeDetail::query()->with(["order.logistic","order.shop","order.packages.commodities.commodity","order.batch"])
|
|
|
- ->where("owner_id",$owner_id)
|
|
|
- ->whereBetween("worked_at",[$month."-01",$month."-".$day])->get() as $detail){
|
|
|
- $order = $detail->order;
|
|
|
+ //满减存在
|
|
|
+ if ($isDiscount){
|
|
|
+ //未被标记过处理时间或处理时间不为本月,或上次处理值过期,处理历史即时账单
|
|
|
+ $pivot = DB::selectOne(DB::raw("SELECT * FROM owner_price_operation_owner WHERE owner_price_operation_id = ? AND owner_id = ?"),[$rule->id,$owner_id]);
|
|
|
+ if ($pivot && (!$pivot->discount_date || substr($pivot->discount_date,0,7)!=date("Y-m") || $pivot->target_value < $targetValue)){
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+ $month = date("Y-m");
|
|
|
+ $day = date("t",strtotime($month));
|
|
|
+ foreach (OwnerFeeDetail::query()->with(["order.logistic","order.shop","order.packages.commodities.commodity","order.batch"])
|
|
|
+ ->where("owner_id",$owner_id)
|
|
|
+ ->whereBetween("worked_at",[$month."-01",$month."-".$day])->get() as $detail){
|
|
|
+ $order = $detail->order;
|
|
|
|
|
|
- $logistic_fee = 0;
|
|
|
- $commodities = [];
|
|
|
- foreach ($order->packages as &$package){
|
|
|
- // 四维转二维
|
|
|
- foreach($package->commodities as &$commodity){
|
|
|
- $commodity["commodity_name"] = $commodity->commodity ? $commodity->commodity->name : '';
|
|
|
- $commodity["sku"] = $commodity->commodity ? $commodity->commodity->sku : '';
|
|
|
+ $logistic_fee = 0;
|
|
|
+ $commodities = [];
|
|
|
+ foreach ($order->packages as &$package){
|
|
|
+ // 四维转二维
|
|
|
+ foreach($package->commodities as &$commodity){
|
|
|
+ $commodity["commodity_name"] = $commodity->commodity ? $commodity->commodity->name : '';
|
|
|
+ $commodity["sku"] = $commodity->commodity ? $commodity->commodity->sku : '';
|
|
|
+ }
|
|
|
+ $commodities = array_merge($commodities,$package->commodities->toArray());
|
|
|
}
|
|
|
- $commodities = array_merge($commodities,$package->commodities->toArray());
|
|
|
- }
|
|
|
- if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
|
|
|
+ if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
|
|
|
|
|
|
- $object = ["commodities"=>$commodities,
|
|
|
- "logistic_name"=>($order->logistic ? $order->logistic->name : ''),
|
|
|
- "shop_name"=>($order->shop ? $order->shop->name : ''),
|
|
|
- "order_type"=>$order->order_type,
|
|
|
- "batch_type" => $order->batch ? $order->batch->wms_type : '',
|
|
|
- "owner_id"=>$order->owner_id];
|
|
|
- $mapping = ["packages"=>"commodities","商品名称"=>"commodity_name",
|
|
|
- "承运商"=>"logistic_name", "店铺类型"=>"shop_name",
|
|
|
- "订单类型"=>"order_type","波次类型"=>"batch_type"];
|
|
|
- $money = $this->matchItem($rule->items,$mapping,$object,$units,$owner_id,false,$isDiscount,$discountIndex);
|
|
|
- if ($money>0)$detail->update(["work_fee"=>$money]);
|
|
|
- else LogService::log(__CLASS__,"处理历史即时账单时发生匹配错误","账单主键:".$detail->id."; 错误代码".$money);
|
|
|
- };
|
|
|
- $rule->update(["discount_date"=>date("Y-m-d")]);
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $e){
|
|
|
- DB::rollBack();
|
|
|
- LogService::log(__CLASS__,"处理历史即时账单时发生系统错误","计费模型主键:".$rule->id."; 错误信息".$e->getMessage());
|
|
|
+ $object = ["commodities"=>$commodities,
|
|
|
+ "logistic_name"=>($order->logistic ? $order->logistic->name : ''),
|
|
|
+ "shop_name"=>($order->shop ? $order->shop->name : ''),
|
|
|
+ "order_type"=>$order->order_type,
|
|
|
+ "batch_type" => $order->batch ? $order->batch->wms_type : '',
|
|
|
+ "owner_id"=>$order->owner_id];
|
|
|
+ $mapping = ["packages"=>"commodities","商品名称"=>"commodity_name",
|
|
|
+ "承运商"=>"logistic_name", "店铺类型"=>"shop_name",
|
|
|
+ "订单类型"=>"order_type","波次类型"=>"batch_type"];
|
|
|
+ $money = $this->matchItem($rule->items,$mapping,$object,$units,$owner_id,false,$isDiscount,$discountIndex);
|
|
|
+ if ($money>0)$detail->update(["work_fee"=>$money]);
|
|
|
+ else LogService::log(__CLASS__,"处理历史即时账单时发生匹配错误","账单主键:".$detail->id."; 错误代码".$money);
|
|
|
+ };
|
|
|
+ DB::update(DB::raw("UPDATE owner_price_operation_owner SET discount_date = ?,target_value = ? WHERE owner_price_operation_id = ? AND owner_id = ?"),
|
|
|
+ [date("Y-m-d"),$targetValue,$rule->id,$owner_id]);
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ DB::rollBack();
|
|
|
+ LogService::log(__CLASS__,"处理历史即时账单时发生系统错误","计费模型主键:".$rule->id."; 错误信息".$e->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
if ($rule->strategy == '特征'){//特征策略匹配
|