* @license GPL https://xxx.com * @link https://xxx.com * @ctime: 2020/4/7 14:24 */ namespace App\Handlers\Activities; use App\Models\Coupon; use App\Models\CouponsUserBag; use App\Models\Order; use App\Repositories\CouponUserBagsRepository; use Carbon\Carbon; use Illuminate\Support\Facades\Log; class CouponActivityHandler { protected $couponUserBagsRepository = null; public function __construct(CouponUserBagsRepository $couponUserBagsRepository) { $this->couponUserBagsRepository = $couponUserBagsRepository; } /** * main 根据 订单 用优惠券 修改订单待支付 * * @param $order * @param $couponUserBagsId * @return bool * @author Fx * */ public function main(&$order, $couponUserBagsId) { $order_time_money = $order->time_money; // 只能抵扣时长费用 $order_dispatch_money = $order->dispatch_money; $order_preferential_money = $order->preferential_money; $coupon = $this->couponUserBagsRepository->getCouponByUserIdAndID($order->user_id, $couponUserBagsId); if (empty($couponUserBagsId) || empty($coupon)) { $order_wait_pay_money = bcsub(bcadd($order_dispatch_money, $order_time_money, 2), $order->preferential_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_NO; $order->coupon_user_bags_id = 0; return false; } if (bcsub($order_time_money, $order_preferential_money, 2) <= 0) { // 骑行花费待支付为0 不需要优惠券 直接返回空 $order_wait_pay_money = bcsub(bcadd($order_dispatch_money, $order_time_money, 2), $order->preferential_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_NO; $order->coupon_user_bags_id = 0; return false; } $coupon = $coupon->toArray(); $cou = $coupon['coupons_data']; switch ($cou['type']) { case Coupon::TYPE_MAN_JIAN: // 满减优惠券 $with_amount = $cou['with_amount']; // 满多少金额 $used_amount = $cou['used_amount']; // 可减少金额 if (bcsub($with_amount, $order_time_money, 2) > 0) { $order_wait_pay_money = bcsub(bcadd($order_dispatch_money, $order_time_money, 2), $order->preferential_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_NO; $order->coupon_user_bags_id = 0; return false; } else { //1.待支付金额 = 时长费 - 之前优惠金额 $order_wait_pay_money = bcsub($order_time_money, $order_preferential_money, 2); //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0) $order_wait_pay_money = bcsub($order_wait_pay_money, $used_amount, 2) < 0 ? 0.00 : bcsub($order_wait_pay_money, $used_amount, 2); // 3.最终待支付金额 = 第二待支付金额 + 调度费 $order_wait_pay_money = bcadd($order_wait_pay_money, $order_dispatch_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_OK; $order->coupon_user_bags_id = $couponUserBagsId; } break; case Coupon::TYPE_WU_MEN_KAN: // 无门槛优惠券 $used_amount = $cou['used_amount']; // 可减少金额 //1.待支付金额 = 时长费 - 之前优惠金额 $order_wait_pay_money = bcsub($order_time_money, $order_preferential_money, 2); //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0) $order_wait_pay_money = bcsub($order_wait_pay_money, $used_amount, 2) < 0 ? 0.00 : bcsub($order_wait_pay_money, $used_amount, 2); // 3.最终待支付金额 = 第二待支付金额 + 调度费 $order_wait_pay_money = bcadd($order_wait_pay_money, $order_dispatch_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_OK; $order->coupon_user_bags_id = $couponUserBagsId; break; case Coupon::TYPE_ZHE_KOU: $discount = $cou['discount']; $used_amount = $cou['used_amount']; // 折扣最多可减少金额 //1.待支付金额 = 时长费 - 之前优惠金额 $order_wait_pay_money = bcsub($order_time_money, $order_preferential_money, 2); //折扣后得时长费金额 = 时长费 X 折扣 $discount_time_money = bcdiv(bcmul($order_time_money, $discount, 2), 100, 2); //折扣优惠券 优惠的金额 $max_used_amount = bcsub($order_time_money, $discount_time_money, 2); if ($max_used_amount > $used_amount) { //如果折扣优惠券优惠的金额 大于 折扣最多可减少得金额 $order_wait_pay_money = bcsub($order_wait_pay_money, $used_amount, 2) < 0 ? 0.00 : bcsub($order_wait_pay_money, $used_amount, 2); } else { $order_wait_pay_money = bcsub($order_wait_pay_money, $max_used_amount, 2) < 0 ? 0.00 : bcsub($order_wait_pay_money, $max_used_amount, 2); } // 3.最终待支付金额 = 第二待支付金额 + 调度费 $order_wait_pay_money = bcadd($order_wait_pay_money, $order_dispatch_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_OK; $order->coupon_user_bags_id = $couponUserBagsId; break; default: $order_wait_pay_money = bcsub(bcadd($order_dispatch_money, $order_time_money, 2), $order->preferential_money, 2); // $order->pay_money = $order_wait_pay_money; $order->order_money = $order_wait_pay_money; $order->is_coupon = Order::COUPON_NO; $order->coupon_user_bags_id = 0; return false; } } // 根据订单获取所有优惠 第一个为最大优惠 /** * isCouponsByOrder 能否选择优惠券 能返回优惠券 不能返回空 * * @param $order * @return array * @author Fx * */ public function isCouponsByOrder($order) { $order_time_money = $order->time_money; // 只能抵扣时长费用 $order_dispatch_money = $order->dispatch_money; $order_preferential_money = $order->preferential_money; $coupons = $this->couponUserBagsRepository->getCouponsByUserIdAndStatus($order->user_id, CouponsUserBag::STATUS_OK, CouponsUserBag::ORDER_TYPE_NORMAL)->where('coupon_type', '<>', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE); $datayes = []; $score = []; $ids = []; $datano = []; if (count($coupons) !== 0) { foreach ($coupons as $v) { $v = $v->toArray(); $cou = $v['coupons_data']; $valid_days_end_time = $v['valid_end_time']; switch ((int)$v['coupon_type']) { case Coupon::TYPE_MAN_JIAN: // 满减优惠券 $with_amount = $cou['with_amount']; // 满多少金额 $used_amount = $cou['used_amount']; // 可减少金额 if (bcsub($with_amount, $order_time_money, 2) > 0) { $v['order_wait_pay_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $order_preferential_money, 2) < 0 ? 0.00 : bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $order_preferential_money, 2); $v['total_preferential_money'] = $order_preferential_money; $datano[] = [ 'id' => $v['id'], 'get_type_name' => CouponsUserBag::$typeMaps[$v['type']], 'type' => $cou['type'], 'type_name' => Coupon::$typeMaps[$cou['type']], 'valid_type_name' => CouponsUserBag::$validTypeMaps[$v['valid_type']], 'valid_type' => $v['valid_type'], 'valid_start_time' => Carbon::make($v['valid_start_time'])->format('Y-m-d'), 'valid_end_time' => Carbon::make($v['valid_end_time'])->format('Y-m-d'), 'valid_days' => $v['valid_days'], 'valid_days_end_time' => $valid_days_end_time, 'created_at' => Carbon::make($v['created_at'])->format('Y-m-d'), 'with_amount' => $cou['with_amount'], 'used_amount' => $cou['used_amount'], 'discount' => $cou['discount'] / 10, 'order_type' => $v['order_type'], 'order_type_name' => CouponsUserBag::$orderTypeMaps[$v['order_type']], // 'order_wait_pay_money' => $v['order_wait_pay_money'], // 'preferential_money' => $v['preferential_money'], ]; $is_ok = false; } else { //1.待支付金额 = 时长费 - 之前优惠金额 $v['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2); //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0) $v['order_wait_pay_money'] = bcsub($v['order_wait_pay_money'], $used_amount, 2) < 0 ? 0.00 : bcsub($v['order_wait_pay_money'], $used_amount, 2); // 3.最终待支付金额 = 第二待支付金额 + 调度费 $v['order_wait_pay_money'] = bcadd($v['order_wait_pay_money'], $order_dispatch_money, 2); // 总优惠金额 = 时长 + 调度 -待支付 $v['total_preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2); $is_ok = true; } break; case Coupon::TYPE_WU_MEN_KAN: // 无门槛优惠券 $used_amount = $cou['used_amount']; // 可减少金额 //1.待支付金额 = 时长费 - 之前优惠金额 $v['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2); //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0) $v['order_wait_pay_money'] = bcsub($v['order_wait_pay_money'], $used_amount, 2) < 0 ? 0.00 : bcsub($v['order_wait_pay_money'], $used_amount, 2); // 3.最终待支付金额 = 第二待支付金额 + 调度费 $v['order_wait_pay_money'] = bcadd($v['order_wait_pay_money'], $order_dispatch_money, 2); // 总优惠金额 = 时长 + 调度 -待支付 $v['total_preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2); $is_ok = true; break; case Coupon::TYPE_ZHE_KOU: $discount = $cou['discount']; $used_amount = $cou['used_amount']; // 折扣最多可减少金额 //1.待支付金额 = 时长费 - 之前优惠金额 $v['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2); //折扣后得时长费金额 = 时长费 X 折扣 $discount_time_money = bcdiv(bcmul($order_time_money, $discount, 2), 100, 2); //折扣优惠券 优惠的金额 $max_used_amount = bcsub($order_time_money, $discount_time_money, 2); if ($max_used_amount > $used_amount) { //如果折扣优惠券优惠的金额 大于 折扣最多可减少得金额 $v['order_wait_pay_money'] = bcsub($v['order_wait_pay_money'], $used_amount, 2) < 0 ? 0.00 : bcsub($v['order_wait_pay_money'], $used_amount, 2); } else { $v['order_wait_pay_money'] = bcsub($v['order_wait_pay_money'], $max_used_amount, 2) < 0 ? 0.00 : bcsub($v['order_wait_pay_money'], $max_used_amount, 2); } // 3.最终待支付金额 = 第二待支付金额 + 调度费 $v['order_wait_pay_money'] = bcadd($v['order_wait_pay_money'], $order_dispatch_money, 2); // 总优惠金额 = 时长 + 调度 -待支付 $v['total_preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2); $is_ok = true; break; case Coupon::TYPE_DEPOSIT_FREE: // $datano[] = $v; $is_ok = false; break; default: Log::error('优惠券类型出现错误,请联系管理员'); $is_ok = false; return $data = [ 'dataYes' => [], 'dataNo' => [], ]; } if ($is_ok) { $datayes[] = [ 'id' => $v['id'], 'get_type_name' => CouponsUserBag::$typeMaps[$v['type']], 'type' => $cou['type'], 'type_name' => Coupon::$typeMaps[$cou['type']], 'valid_type_name' => CouponsUserBag::$validTypeMaps[$v['valid_type']], 'valid_type' => $v['valid_type'], 'valid_start_time' => Carbon::make($v['valid_start_time'])->format('Y-m-d'), 'valid_end_time' => Carbon::make($v['valid_end_time'])->format('Y-m-d'), 'valid_days' => $v['valid_days'], 'valid_days_end_time' => $valid_days_end_time, 'created_at' => Carbon::make($v['created_at'])->format('Y-m-d'), 'with_amount' => $cou['with_amount'], 'used_amount' => $cou['used_amount'], 'discount' => $cou['discount'] / 10, 'order_wait_pay_money' => $v['order_wait_pay_money'], 'total_preferential_money' => $v['total_preferential_money'], 'coupon_preferential_money' => bcsub($v['total_preferential_money'], $order_preferential_money, 2), 'order_type' => $v['order_type'], 'order_type_name' => CouponsUserBag::$orderTypeMaps[$v['order_type']], ]; $score[] = $v['order_wait_pay_money']; $ids[] = $v['id']; } } } //排序最优 if (!empty($datayes)) { array_multisort($score, SORT_ASC, $ids, SORT_ASC, $datayes); } if (bcsub($order_time_money, $order_preferential_money, 2) <= 0 || $order->status !== Order::STATUS_CLOSE_BIKE) { // 骑行花费待支付为0 不需要优惠券 直接返回空 $data = [ 'dataYes' => [], 'dataNo' => array_merge($datayes, $datano), ]; return $data; } $data = [ 'dataYes' => $datayes, 'dataNo' => $datano, ]; return $data; } /** * getCouponsByOrderAndId 根据订单和用户优惠券id 获取用户优惠券 * * @param $order * @param $couponUserBagsId * @return array * @author Fx * */ public function getCouponsByOrderAndId($order, $couponUserBagsId) { $data = []; if ($order->status !== Order::STATUS_CLOSE_BIKE) { return $data; } $order_time_money = $order->time_money; // 只能抵扣时长费用 $order_dispatch_money = $order->dispatch_money; $order_preferential_money = $order->preferential_money; if (bcsub($order_time_money, $order_preferential_money, 2) <= 0) { // 骑行花费待支付为0 不需要优惠券 直接返回空 return $data; } $coupon = $this->couponUserBagsRepository->getCouponByUserIdAndID($order->user_id, $couponUserBagsId); if (empty($couponUserBagsId) || empty($coupon)) { return $data; } $coupon = $coupon->toArray(); $cou = $coupon['coupons_data']; // $valid_days_end_time = null; // if ($coupon['valid_type'] == CouponsUserBag::VALID_TYPE_RELATIVE) { // $valid_days_end_time = Carbon::make($coupon['created_at'])->addDays($coupon['valid_days'])->format('Y-m-d'); // } switch ($cou['type']) { case Coupon::TYPE_MAN_JIAN: // 满减优惠券 $with_amount = $cou['with_amount']; // 满多少金额 $used_amount = $cou['used_amount']; // 可减少金额 if (bcsub($with_amount, $order_time_money, 2) > 0) { return $data; } else { //1.待支付金额 = 时长费 - 之前优惠金额 $coupon['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2); //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0) $coupon['order_wait_pay_money'] = bcsub($coupon['order_wait_pay_money'], $used_amount, 2) < 0 ? 0.00 : bcsub($coupon['order_wait_pay_money'], $used_amount, 2); // 3.最终待支付金额 = 第二待支付金额 + 调度费 $coupon['order_wait_pay_money'] = bcadd($coupon['order_wait_pay_money'], $order_dispatch_money, 2); // 总优惠金额 = 时长 + 调度 -待支付 $coupon['total_preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $coupon['order_wait_pay_money'], 2); $data['order_wait_pay_money'] = $coupon['order_wait_pay_money']; $data['id'] = $coupon['id']; $data['coupon_preferential_money'] = bcsub($coupon['total_preferential_money'], $order_preferential_money, 2); $data['total_preferential_money'] = $coupon['total_preferential_money']; return $data; } break; case Coupon::TYPE_WU_MEN_KAN: // 无门槛优惠券 $used_amount = $cou['used_amount']; // 可减少金额 //1.待支付金额 = 时长费 - 之前优惠金额 $coupon['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2); //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0) $coupon['order_wait_pay_money'] = bcsub($coupon['order_wait_pay_money'], $used_amount, 2) < 0 ? 0.00 : bcsub($coupon['order_wait_pay_money'], $used_amount, 2); // 3.最终待支付金额 = 第二待支付金额 + 调度费 $coupon['order_wait_pay_money'] = bcadd($coupon['order_wait_pay_money'], $order_dispatch_money, 2); // 总优惠金额 = 时长 + 调度 -待支付 $coupon['total_preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $coupon['order_wait_pay_money'], 2); $data['order_wait_pay_money'] = $coupon['order_wait_pay_money']; $data['id'] = $coupon['id']; $data['coupon_preferential_money'] = bcsub($coupon['total_preferential_money'], $order_preferential_money, 2); $data['total_preferential_money'] = $coupon['total_preferential_money']; return $data; break; case Coupon::TYPE_ZHE_KOU: $discount = $cou['discount']; $used_amount = $cou['used_amount']; // 折扣最多可减少金额 //1.待支付金额 = 时长费 - 之前优惠金额 $coupon['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2); //折扣后得时长费金额 = 时长费 X 折扣 $discount_time_money = bcdiv(bcmul($order_time_money, $discount, 2), 100, 2); //折扣优惠券 优惠的金额 $max_used_amount = bcsub($order_time_money, $discount_time_money, 2); if ($used_amount == 0) { //不限制最大优惠金额 $coupon['order_wait_pay_money'] = bcsub($coupon['order_wait_pay_money'], $max_used_amount, 2) < 0 ? 0.00 : bcsub($coupon['order_wait_pay_money'], $max_used_amount, 2); } else { if ($max_used_amount > $used_amount) { //如果折扣优惠券优惠的金额 大于 折扣最多可减少得金额 $coupon['order_wait_pay_money'] = bcsub($coupon['order_wait_pay_money'], $used_amount, 2) < 0 ? 0.00 : bcsub($coupon['order_wait_pay_money'], $used_amount, 2); } else { $coupon['order_wait_pay_money'] = bcsub($coupon['order_wait_pay_money'], $max_used_amount, 2) < 0 ? 0.00 : bcsub($coupon['order_wait_pay_money'], $max_used_amount, 2); } } // 3.最终待支付金额 = 第二待支付金额 + 调度费 $coupon['order_wait_pay_money'] = bcadd($coupon['order_wait_pay_money'], $order_dispatch_money, 2); // 总优惠金额 = 时长 + 调度 -待支付 $coupon['total_preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $coupon['order_wait_pay_money'], 2); $data['order_wait_pay_money'] = $coupon['order_wait_pay_money']; $data['id'] = $coupon['id']; $data['coupon_preferential_money'] = bcsub($coupon['total_preferential_money'], $order_preferential_money, 2); $data['total_preferential_money'] = $coupon['total_preferential_money']; return $data; break; default: Log::error('优惠券类型出现错误,请联系管理员'); // $data['order_wait_pay_money'] = bcsub(bcadd($order_time_money,$order_dispatch_money,2),$order_preferential_money,2); // $data['id'] = $coupon['id']; // $data['coupon_preferential_money'] = '0.00'; // $data['total_preferential_money'] = $order_preferential_money; return $data; // 返回空 } } }