| | |
| | | use app\api\model\order\OrderAddress as OrderAddress; |
| | | use app\api\model\plus\coupon\UserCoupon as UserCouponModel; |
| | | use app\api\model\product\Category; |
| | | use app\api\service\consumption\ConsumptionDeductService; |
| | | use app\api\service\points\PointsDeductService; |
| | | use app\common\enum\order\OrderPayTypeEnum; |
| | | use app\common\enum\order\OrderSourceEnum; |
| | | use app\common\enum\order\OrderTypeEnum; |
| | | use app\common\enum\supplier\SupplierType; |
| | | use app\common\model\settings\Setting as SettingModel; |
| | | use app\api\service\points\PointsDeductService; |
| | | use app\api\service\coupon\ProductDeductService; |
| | | use app\common\model\store\Store as StoreModel; |
| | | use app\api\service\user\UserService; |
| | |
| | | 'is_user_grade' => true, // 会员等级折扣 |
| | | 'is_agent' => true, // 商品是否开启分销,最终还是支付成功后判断分销活动是否开启 |
| | | 'is_reduce' => true, //是否满减 |
| | | 'is_use_consumption' => true, // 是否使用消费券抵扣 |
| | | ]; |
| | | |
| | | /** |
| | |
| | | $expressPrice = 0; |
| | | $totalPointsMoney = 0; |
| | | $totalPoints = 0; |
| | | $totalConsumptionMoney = 0; |
| | | $totalConsumption = 0; |
| | | $totalProductReduce = 0; |
| | | $settlementPrice=0; |
| | | $this->commonOrderData = $this->getCommonOrderData(); |
| | | // 供应商 |
| | | foreach ($this->supplierData as &$supplier) { |
| | |
| | | // 设置订单商品总金额(不含优惠折扣) |
| | | $this->setOrderTotalPrice($supplier['productList']); |
| | | $orderTotalPrice += $this->orderData['order_total_price']; |
| | | // 设置订单商品总结算金额 |
| | | $this->setOrderSettlementPrice($supplier['productList']); |
| | | $settlementPrice += $this->orderData['settlement_price']; |
| | | // 先计算商品满减 |
| | | $this->setProductReduce($supplier['productList']); |
| | | $totalProductReduce += $this->orderData['product_reduce_money']; |
| | |
| | | $this->setOrderCouponMoney($couponList, $this->orderData['coupon_id'], $supplier['productList']); |
| | | } |
| | | // 计算可用积分抵扣 |
| | | $this->setOrderPoints($supplier['productList']); |
| | | $this->setOrderPoints($supplier['productList'],$supplier['supplier']); |
| | | $totalPointsMoney += $this->orderData['points_money']; |
| | | $totalPoints += $this->orderData['points_num']; |
| | | // 计算可用消费券抵扣 |
| | | $this->setOrderConsumption($supplier['productList']); |
| | | $totalConsumptionMoney += $this->orderData['consumption_money']; |
| | | $totalConsumption += $this->orderData['consumption_num']; |
| | | // 计算订单商品的实际付款金额 |
| | | $this->setOrderProductPayPrice($supplier['productList']); |
| | | |
| | |
| | | $delivery = $deliveryType[0]; |
| | | } |
| | | |
| | | $this->orderData['delivery'] = $supplier['productList'][0]['is_virtual'] == 1 || $supplier['productList'][0]['is_virtual'] == 2 ?30:$delivery; |
| | | $this->orderData['delivery'] = $supplier['productList'][0]['is_virtual'] == 1 || $supplier['productList'][0]['is_virtual'] == 2||$supplier['productList'][0]['is_virtual'] == 3 ?30:$delivery; |
| | | }else{ |
| | | if($supplier['productList'][0]['is_virtual'] == 1 || $supplier['productList'][0]['is_virtual'] == 2){ |
| | | if($supplier['productList'][0]['is_virtual'] == 1 || $supplier['productList'][0]['is_virtual'] == 2||$supplier['productList'][0]['is_virtual'] == 3){ |
| | | $this->orderData['delivery'] = 30; |
| | | }else { |
| | | $this->orderData['delivery'] = $this->params['supplier'][$supplier['shop_supplier_id']]['delivery']; |
| | | } |
| | | $this->orderData['store_id'] = $this->params['supplier'][$supplier['shop_supplier_id']]['store_id']; |
| | | } |
| | | |
| | | |
| | | // 处理配送方式 |
| | | if ($this->orderData['delivery'] == DeliveryTypeEnum::EXPRESS) { |
| | | $this->setOrderExpress($supplier['productList']); |
| | |
| | | $orderPayPrice = $this->setOrderFinalPrice(); |
| | | // 计算订单积分赠送数量 |
| | | $this->setOrderPointsBonus(); |
| | | // 计算订单消费券赠送数量 |
| | | $this->setOrderConsumptionBonus(); |
| | | //订单数据 |
| | | $this->commonOrderData = array_merge([ |
| | | 'order_total_num' => $orderTotalNum, // 商品总数量 |
| | | 'order_total_price' => helper::number2($orderTotalPrice), // 商品总价 |
| | | 'settlement_price' => helper::number2($settlementPrice), // 商品总结算价 |
| | | 'order_pay_price' => helper::number2($orderPayPrice), // 商品总价,最终支付 |
| | | 'coupon_list' => $couponList, |
| | | 'coupon_id_sys' => $coupon_id, |
| | | 'coupon_money_sys' => 0, |
| | | 'points_money' => $totalPointsMoney, |
| | | 'points_num' => $totalPoints, |
| | | 'consumption_money' => $totalConsumptionMoney, |
| | | 'consumption_num' => $totalConsumption, |
| | | 'is_real_use_points' => $this->params['is_use_points'], |
| | | 'is_real_use_consumption' => $this->params['is_use_consumption'], |
| | | 'product_reduce_money' => $totalProductReduce, |
| | | 'verify_data' => $verify_data, |
| | | 'last_extract' => UserService::getLastExtract($this->user['user_id']), |
| | |
| | | 'is_allow_points' => true, |
| | | // 是否使用积分抵扣 |
| | | 'is_use_points' => $this->params['is_use_points'], |
| | | // 是否使用消费券抵扣 |
| | | 'is_use_consumption' => $this->params['is_use_consumption'], |
| | | // 是否允许使用消费券抵扣 |
| | | 'is_allow_consumption' => true, |
| | | // 支付方式 |
| | | 'pay_type' => isset($this->params['pay_type']) ? $this->params['pay_type'] : OrderPayTypeEnum::WECHAT, |
| | | 'pay_source' => isset($this->params['pay_source']) ? $this->params['pay_source'] : '', |
| | |
| | | { |
| | | // 订单商品的总金额(不含优惠券折扣) |
| | | $this->orderData['order_total_price'] = helper::number2(helper::getArrayColumnSum($productList, 'total_price')); |
| | | } |
| | | /** |
| | | * 设置订单的商品总结算金额(不含优惠折扣) |
| | | */ |
| | | private function setOrderSettlementPrice($productList) |
| | | { |
| | | // 订单商品的总金额(不含优惠券折扣) |
| | | $this->orderData['settlement_price'] = helper::number2(helper::getArrayColumnSum($productList, 'total_settlement_price')); |
| | | } |
| | | |
| | | /** |
| | |
| | | if ($this->orderData['is_allow_points'] && $this->commonOrderData['is_use_points'] && !$this->settledRule['force_points']) { |
| | | $value = helper::bcsub($value, $product['points_money']); |
| | | } |
| | | // 减去消费券抵扣金额 |
| | | if ($this->orderData['is_allow_consumption'] && $this->commonOrderData['is_use_consumption'] && !$this->settledRule['force_points']) { |
| | | $value = helper::bcsub($value, $product['consumption_money']); |
| | | } |
| | | // 减去满减金额 |
| | | if($this->settledRule['is_reduce'] && $this->orderData['reduce']){ |
| | | $value = helper::bcsub($value, $product['fullreduce_money']); |
| | |
| | | } |
| | | } |
| | | |
| | | if(!empty($productList) && $productList[0]['is_virtual'] == 1){ |
| | | if(!empty($productList) && ($productList[0]['is_virtual'] == 1 || $productList[0]['is_virtual'] == 3)){ |
| | | //虚拟商品核销 by yj |
| | | $verify_data = [ |
| | | 'is_verify' => $productList[0]['is_verify'] && empty($productList[0]['virtual_auto']) ? 1 : 0, //必须是手动发货并且支持核销 |
| | |
| | | // 积分兑换扣除用户积分 |
| | | if ($commomOrder['force_points']) { |
| | | $describe = "用户积分兑换消费:{$this->model['order_no']}"; |
| | | $this->user->setIncPoints(-$commomOrder['points_num'], $describe); |
| | | $this->user->setIncPoints(-$commomOrder['points_num'], $describe,0,true,($supplier['supplier']['supplier_type'] == SupplierType::GROUPBUYING?1:0)); |
| | | } else { |
| | | // 积分抵扣情况下扣除用户积分 |
| | | if ($commomOrder['is_allow_points'] && $commomOrder['is_real_use_points'] && $commomOrder['points_num'] > 0) { |
| | | $describe = "用户消费:{$this->model['order_no']}"; |
| | | $this->user->setIncPoints(-$commomOrder['points_num'], $describe); |
| | | $this->user->setIncPoints(-$commomOrder['points_num'], $describe,0,true,($supplier['supplier']['supplier_type'] == SupplierType::GROUPBUYING?1:0)); |
| | | } |
| | | // 消费券抵扣情况下扣除用户消费券 |
| | | if ($commomOrder['is_allow_consumption'] && $commomOrder['is_real_use_consumption'] && $commomOrder['consumption_num'] > 0) { |
| | | $describe = "用户消费:{$this->model['order_no']}"; |
| | | $this->user->setIncConsumption(-$commomOrder['consumption_num'], $describe); |
| | | } |
| | | } |
| | | return $status; |
| | |
| | | 'user_id' => $this->user['user_id'], |
| | | 'order_no' => $this->model->orderNo(), |
| | | 'total_price' => $order['order_total_price'], |
| | | 'settlement_price' => $order['settlement_price'], |
| | | 'order_price' => $order['order_price'], |
| | | 'coupon_id' => $supplier['orderData']['coupon_id'], |
| | | 'coupon_money' => $supplier['orderData']['coupon_money'], |
| | |
| | | 'coupon_money_sys'=>$supplier['orderData']['coupon_money_sys'], |
| | | 'points_money' => $commomOrder['is_real_use_points'] == 1?abs($supplier['orderData']['points_money']):0, |
| | | 'points_num' => $commomOrder['is_real_use_points'] == 1?$supplier['orderData']['points_num']:0, |
| | | 'consumption_money' => $commomOrder['is_real_use_consumption'] == 1?abs($supplier['orderData']['consumption_money']):0, |
| | | 'consumption_num' => $commomOrder['is_real_use_consumption'] == 1?$supplier['orderData']['consumption_num']:0, |
| | | 'pay_price' => $order['order_pay_price'], |
| | | 'delivery_type' => $supplier['orderData']['delivery'], |
| | | 'pay_type' => $commomOrder['pay_type'], |
| | |
| | | 'buyer_remark' => $this->params['supplier'][$supplier['shop_supplier_id']]['remark'], |
| | | 'order_source' => $this->orderSource['source'], |
| | | 'points_bonus' => $supplier['orderData']['points_bonus'], |
| | | 'consumption_bonus' => $supplier['orderData']['consumption_bonus'], |
| | | 'is_agent' => $this->settledRule['is_agent']? 1:0, |
| | | 'shop_supplier_id' => ($supplier['productList'][0]['is_newcomer'] == 0||$supplier['productList'][0]['belonging_shop_supplier_id'] == 0)?$supplier['shop_supplier_id']:$supplier['productList'][0]['belonging_shop_supplier_id'], |
| | | 'supplier_money' => $order['supplier_money'], |
| | |
| | | // 订单商品列表 |
| | | $productList = []; |
| | | foreach ($supplier['productList'] as $product) { |
| | | $product_type = 0; |
| | | if ($product['is_brand'] == 1) { |
| | | $product_type = 10; |
| | | }else if ($supplier['supplier']['supplier_type'] == SupplierType::GROUPBUYING) { |
| | | $product_type = 20; |
| | | } |
| | | $item = [ |
| | | 'order_id' => $status, |
| | | 'user_id' => $this->user['user_id'], |
| | |
| | | 'image_id' => $product['image'][0]['image_id'], |
| | | 'deduct_stock_type' => $product['deduct_stock_type'], |
| | | 'spec_type' => $product['spec_type'], |
| | | 'open_coupon' => $product['open_coupon'], |
| | | 'open_coupons' => $product['open_coupons'], |
| | | 'spec_sku_id' => $product['product_sku']['spec_sku_id'], |
| | | 'product_sku_id' => $product['product_sku']['product_sku_id'], |
| | | 'product_attr' => $product['product_sku']['product_attr'], |
| | |
| | | 'product_no' => $product['product_sku']['product_no'], |
| | | 'product_price' => $product['product_sku']['product_price'], |
| | | 'line_price' => $product['product_sku']['line_price'], |
| | | 'settlement_price' => $product['product_sku']['settlement_price'], |
| | | 'product_weight' => $product['product_sku']['product_weight'], |
| | | 'is_user_grade' => (int)$product['is_user_grade'], |
| | | 'grade_ratio' => $product['grade_ratio'], |
| | |
| | | 'points_money' => isset($product['points_money']) && $commomOrder['is_real_use_points']?abs($product['points_money']):0, |
| | | 'points_num' => isset($product['points_num']) && $commomOrder['is_real_use_points']?$product['points_num']:0, |
| | | 'points_bonus' => isset($product['points_bonus'])?$product['points_bonus']:0, |
| | | 'consumption_money' => isset($product['consumption_money']) && $commomOrder['is_real_use_consumption']?abs($product['consumption_money']):0, |
| | | 'consumption_num' => isset($product['consumption_num']) && $commomOrder['is_real_use_consumption']?$product['consumption_num']:0, |
| | | 'consumption_bonus' => isset($product['consumption_bonus'])?$product['consumption_bonus']:0, |
| | | 'total_num' => $product['total_num'], |
| | | 'total_price' => $product['total_price'], |
| | | 'total_settlement_price' => $product['total_settlement_price'], |
| | | 'total_pay_price' => $product['total_pay_price'], |
| | | 'supplier_money' => $product['supplier_money'], |
| | | 'is_agent' => $product['is_agent'], |
| | |
| | | 'third_money' => $product['third_money'], |
| | | 'fullreduce_money' => isset($product['fullreduce_money'])?$product['fullreduce_money']:0, |
| | | 'virtual_content' => $product['virtual_content'], |
| | | 'service_content' => $product['service_content'], |
| | | 'is_alone_team' => $product['is_alone_team'], |
| | | 'alone_team_equity' => $product['alone_team_equity'], |
| | | 'product_reduce_money' => $product['product_reduce_money'], |
| | |
| | | 'verify_type' => $product['verify_type'], |
| | | 'verify_day' => $product['verify_day'], |
| | | 'is_newcomer' => $product['is_newcomer'], |
| | | 'is_vip' => $product['is_vip'], |
| | | 'is_repurchase' => $product['is_repurchase'], |
| | | 'is_viewpoint_money' => $product['is_viewpoint_money'], |
| | | 'viewpoint_money' => $product['viewpoint_money'], |
| | | 'deduction_price' => empty($product['deduction_price']) ? 0 : $product['deduction_price'],//券商品的抵扣金额 by yj 2024.1.10 |
| | | 'product_type' => $product_type, |
| | | 'is_activation_code' => $product['is_activation_code'], |
| | | 'activation_code_num' => $product['product_sku']['activation_code_num'], |
| | | 'activation_code_exchange' => $product['activation_code_exchange'], |
| | | 'is_gift_pack' => $product['is_gift_pack'], |
| | | 'vip_order_num' => $product['vip_order_num'], |
| | | ]; |
| | | // 记录订单商品来源id |
| | | $item['product_source_id'] = isset($product['product_source_id']) ? $product['product_source_id'] : 0; |
| | |
| | | /** |
| | | * 计算订单可用积分抵扣 |
| | | */ |
| | | private function setOrderPoints($productList) |
| | | private function setOrderPoints($productList,$supplier) |
| | | { |
| | | $this->orderData['points_money'] = 0; |
| | | // 积分抵扣总数量 |
| | | $this->orderData['points_num'] = 0; |
| | | // 允许积分抵扣 |
| | | $this->orderData['is_allow_points'] = false; |
| | | |
| | | // 只有团购类商品允许使用品牌积分 |
| | | $points=$supplier['supplier_type']==SupplierType::GROUPBUYING?$this->user['points']:$this->user['points']-$this->user['brand_points']; |
| | | // 积分商城兑换 |
| | | if (isset($this->settledRule['force_points']) && $this->settledRule['force_points']) { |
| | | // 积分抵扣金额,商品价格-兑换金额 |
| | |
| | | $this->orderData['points_num'] = $productList[0]['points_num']; |
| | | // 允许积分抵扣 |
| | | $this->orderData['is_allow_points'] = true; |
| | | if ($this->user['points'] < $productList[0]['points_num']) { |
| | | if ($points < $productList[0]['points_num']) { |
| | | $this->error = '积分不足,去多赚点积分吧!'; |
| | | return false; |
| | | } |
| | |
| | | // 订单最多可抵扣的积分总数量 |
| | | $maxPointsNumCount = helper::getArrayColumnSum($productList, 'max_points_num'); |
| | | // 实际可抵扣的积分数量 |
| | | $actualPointsNum = min($maxPointsNumCount, $this->user['points']); |
| | | $actualPointsNum = min($maxPointsNumCount, $points); |
| | | if ($actualPointsNum < 1) { |
| | | $this->orderData['points_money'] = 0; |
| | | // 积分抵扣总数量 |
| | |
| | | $product['max_points_num'] = $product['points_num']; |
| | | } else { |
| | | // 商品不允许积分抵扣 |
| | | if (!$product['is_points_discount']) continue; |
| | | if (!$product['is_points_discount']||$product['is_vip']||$product['is_newcomer']||$product['is_repurchase']) continue; |
| | | // 积分抵扣比例 |
| | | $deductionRatio = helper::bcdiv($setting['discount']['max_money_ratio'], 100); |
| | | // 最多可抵扣的金额 |
| | |
| | | } |
| | | // 订单积分赠送数量 |
| | | $supplier['orderData']['points_bonus'] = helper::getArrayColumnSum($supplier['productList'], 'points_bonus'); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 计算订单可用消费券抵扣 |
| | | */ |
| | | private function setOrderConsumption($productList) |
| | | { |
| | | $this->orderData['consumption_money'] = 0; |
| | | // 消费券抵扣总数量 |
| | | $this->orderData['consumption_num'] = 0; |
| | | // 允许消费券抵扣 |
| | | $this->orderData['is_allow_consumption'] = false; |
| | | // 消费券设置 |
| | | $setting = SettingModel::getItem('consumption'); |
| | | // 条件:后台开启下单使用消费券抵扣 |
| | | if (!$setting['is_shopping_discount']) { |
| | | return false; |
| | | } |
| | | // 条件:订单金额满足[?]元 |
| | | if (helper::bccomp($setting['discount']['full_order_price'], $this->orderData['order_total_price']) === 1) { |
| | | return false; |
| | | } |
| | | // 计算订单商品最多可抵扣的消费券数量 |
| | | $this->setOrderConsumptionMaxPointsNum($productList); |
| | | // 订单最多可抵扣的消费券总数量 |
| | | $maxConsumptionNumCount = helper::getArrayColumnSum($productList, 'max_consumption_num'); |
| | | // 实际可抵扣的消费券数量 |
| | | $actualConsumptionNum = min($maxConsumptionNumCount, $this->user['consumer_coupon']); |
| | | if ($actualConsumptionNum < 1) { |
| | | $this->orderData['consumption_money'] = 0; |
| | | // 消费券抵扣总数量 |
| | | $this->orderData['consumption_num'] = 0; |
| | | // 允许消费券抵扣 |
| | | $this->orderData['is_allow_consumption'] = true; |
| | | return false; |
| | | } |
| | | // 计算订单商品实际抵扣的消费券数量和金额 |
| | | $ProductDeduct = new ConsumptionDeductService($productList); |
| | | $ProductDeduct->setProductConsumption($maxConsumptionNumCount, $actualConsumptionNum); |
| | | // 消费券抵扣总金额 |
| | | $orderConsumptionMoney = helper::getArrayColumnSum($productList, 'consumption_money'); |
| | | $this->orderData['consumption_money'] = helper::number2($orderConsumptionMoney); |
| | | // 消费券抵扣总数量 |
| | | $this->orderData['consumption_num'] = $actualConsumptionNum; |
| | | // 允许消费券抵扣 |
| | | $this->orderData['is_allow_consumption'] = true; |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 计算订单商品最多可抵扣的消费券数量 |
| | | */ |
| | | private function setOrderConsumptionMaxPointsNum($productList) |
| | | { |
| | | // 消费券设置 |
| | | $setting = SettingModel::getItem('consumption'); |
| | | foreach ($productList as &$product) { |
| | | // 商品不允许消费券抵扣 |
| | | if (!$product['is_consumption_discount']||$product['is_repurchase']||$product['is_newcomer']||$product['is_vip']) continue; |
| | | // 消费券抵扣比例 |
| | | $deductionRatio = helper::bcdiv($setting['discount']['max_money_ratio'], 100); |
| | | // 最多可抵扣的金额 |
| | | $maxPointsMoney = helper::bcmul($product['total_price'], $deductionRatio); |
| | | // 最多可抵扣的消费券数量 |
| | | $product['max_consumption_num'] = helper::bcdiv($maxPointsMoney, $setting['discount']['discount_ratio'], 0); |
| | | // 如果超过商品最大抵扣数量 |
| | | if($product['max_consumption_discount'] > 0 && $product['max_consumption_num'] > $product['max_consumption_discount'] * $product['total_num']){ |
| | | $product['max_consumption_num'] = $product['max_consumption_discount'] * $product['total_num']; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 计算订单消费券赠送数量 |
| | | */ |
| | | private function setOrderConsumptionBonus() |
| | | { |
| | | // 初始化商品消费券赠送数量 |
| | | foreach ($this->supplierData as &$supplier){ |
| | | foreach ($supplier['productList'] as $product){ |
| | | $product['consumption_bonus'] = 0; |
| | | } |
| | | $supplier['orderData']['consumption_bonus'] = 0; |
| | | } |
| | | // 消费券设置 |
| | | $setting = SettingModel::getItem('consumption'); |
| | | // 条件:后台开启开启购物送消费券 |
| | | if (!$setting['is_shopping_gift']) { |
| | | return false; |
| | | } |
| | | // 设置商品消费券赠送数量 |
| | | foreach ($this->supplierData as &$supplier) { |
| | | foreach ($supplier['productList'] as &$product) { |
| | | // 消费券赠送比例 |
| | | $ratio = $setting['gift_ratio'] / 100; |
| | | // 计算抵扣的消费券数量 |
| | | $product['consumption_bonus'] = !$product['is_consumption_gift'] ? 0 : helper::bcmul($product['total_pay_price'], $ratio, 0); |
| | | } |
| | | // 订单消费券赠送数量 |
| | | $supplier['orderData']['consumption_bonus'] = helper::getArrayColumnSum($supplier['productList'], 'consumption_bonus'); |
| | | } |
| | | |
| | | return true; |
| | |
| | | $service = new FullDeductService; |
| | | $completed = $service->setProductFullreduceMoney($productListTemp, $reduce['reduced_price']); |
| | | // 分配订单商品优惠券抵扣金额 |
| | | foreach ($productList as $key => &$product) { |
| | | $product['fullreduce_money'] = $completed[$key]['fullreduce_money'] / 100; |
| | | if ($completed){ |
| | | // 分配订单商品优惠券抵扣金额 |
| | | foreach ($productList as $key => &$product) { |
| | | $product['fullreduce_money'] = $completed[$key]['fullreduce_money'] / 100; |
| | | } |
| | | } |
| | | return true; |
| | | } |