|
|
@@ -105,6 +105,8 @@ class OwnerPriceOperationService
|
|
|
* 三. 2021-01-28 zzd
|
|
|
* 增加满减策略:子策略匹配时不再考虑单,仅件箱换算,满减满足后标记模型修改历史对账单
|
|
|
* 增加按订单计价策略:主匹配模型增加字段量价,该字段存在时视为按单计价,价格为该值
|
|
|
+ * 四. 2021-02-18 zzd
|
|
|
+ * 满减多阶段匹配 满减字段由单值改为字符串多值 匹配时转数组寻找最相近
|
|
|
*/
|
|
|
public function matching($matchObject, $columnMapping, $owner_id, $type = '出库')
|
|
|
{
|
|
|
@@ -126,60 +128,76 @@ class OwnerPriceOperationService
|
|
|
foreach ($rules as $rule){
|
|
|
if (!$rule->items)continue; //不存在子规则跳出
|
|
|
$isDiscount = false; //是否存在满减
|
|
|
- if ($type=='出库' && $rule->discount_count > 0 && $total >= $rule->discount_count)$isDiscount = true;//满足满减条件
|
|
|
- //满减存在且未被标记过处理时间或处理时间不为本月,处理历史即时账单
|
|
|
- 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;
|
|
|
+ $discountIndex = 0;
|
|
|
+ $targetValue = 0;
|
|
|
+ if ($type=='出库' && $rule->discount_count){
|
|
|
+ foreach (array_reverse(explode(",",$rule->discount_count),true) as $index=>$discount){
|
|
|
+ if ($total >= $discount){
|
|
|
+ $isDiscount = true; //第一个满足满减条件
|
|
|
+ $discountIndex = $index;
|
|
|
+ $targetValue = $discount;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //满减存在
|
|
|
+ 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,'出库',$isDiscount);
|
|
|
- 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 == '特征'){//特征策略匹配
|
|
|
$bool = app("FeatureService")->matchFeature($rule->feature,$columnMapping,$matchObject);
|
|
|
if ($bool === true){
|
|
|
- if ($rule->total_price)return ["id"=>$rule->id,"money"=>$isDiscount ? $rule->total_discount_price : $rule->total_price]; //按单计价存在,直接返回单总价或减免总价
|
|
|
- $money = $this->matchItem($rule->items,$columnMapping,$matchObject,$units,$owner_id,$type=='入库' ? true : false,$isDiscount);
|
|
|
+ if ($rule->total_price)return ["id"=>$rule->id,"money"=>$isDiscount ? explode(",",$rule->total_discount_price)[$discountIndex] : $rule->total_price]; //按单计价存在,直接返回单总价或减免总价
|
|
|
+ $money = $this->matchItem($rule->items,$columnMapping,$matchObject,$units,$owner_id,$type=='入库' ? true : false,$isDiscount,$discountIndex);
|
|
|
if ($money>0)return ["id"=>$rule->id,"money"=>$money];
|
|
|
};
|
|
|
}else{//默认策略匹配
|
|
|
- if ($rule->total_price)return ["id"=>$rule->id,"money"=>$isDiscount ? $rule->total_discount_price : $rule->total_price]; //按单计价存在,直接返回单总价或减免总价
|
|
|
- $money = $this->matchItem($rule->items,$columnMapping,$matchObject,$units,$owner_id,$type=='入库' ? true : false,$isDiscount);
|
|
|
+ if ($rule->total_price)return ["id"=>$rule->id,"money"=>$isDiscount ? explode(",",$rule->total_discount_price)[$discountIndex] : $rule->total_price]; //按单计价存在,直接返回单总价或减免总价
|
|
|
+ $money = $this->matchItem($rule->items,$columnMapping,$matchObject,$units,$owner_id,$type=='入库' ? true : false,$isDiscount,$discountIndex);
|
|
|
if ($money>0)return ["id"=>$rule->id,"money"=>$money];
|
|
|
};
|
|
|
}
|
|
|
@@ -213,10 +231,11 @@ class OwnerPriceOperationService
|
|
|
* @param integer $owner_id 货主ID
|
|
|
* @param bool $isIn 是否为入库单
|
|
|
* @param bool $isDiscount 是否为满减单
|
|
|
+ * @param int $discountIndex 阶梯满减所处下标 满减价格此处应为 1,2,3 解析为数组后根据此下标寻找对应值
|
|
|
*
|
|
|
* @return double
|
|
|
*/
|
|
|
- private function matchItem($rules, $columnMapping, $matchObject, $units, $owner_id, $isIn, $isDiscount)
|
|
|
+ private function matchItem($rules, $columnMapping, $matchObject, $units, $owner_id, $isIn, $isDiscount, $discountIndex)
|
|
|
{
|
|
|
$amountColumn = $columnMapping["amount"] ?? "amount";
|
|
|
$packageColumn = $columnMapping["packages"] ?? "packages";
|
|
|
@@ -226,7 +245,7 @@ class OwnerPriceOperationService
|
|
|
|
|
|
$unitName = "";
|
|
|
foreach ($rules as $rule){
|
|
|
- if ($isDiscount)$rule->unit_price = $rule->discount_price; //满足满减条件,单价调整为满减单价
|
|
|
+ if ($isDiscount)$rule->unit_price = explode(",",$rule->discount_price)[$discountIndex]; //满足满减条件,单价调整为满减单价
|
|
|
switch ($rule->strategy){
|
|
|
case "特征":
|
|
|
$inMoney = 0;
|