123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- /**
- *
- *
- * @category xxx
- * @package PSR
- * @subpackage Documentation\API
- * @author xxx <xxx@xxx.com>
- * @license GPL https://xxx.com
- * @link https://xxx.com
- * @ctime: 2020/4/7 10:23
- */
- namespace App\Http\Controllers\V1;
- use App\Http\Requests\CouponUserBagsByOrderRequest;
- use App\Http\Requests\CouponUserBagsRequest;
- use App\Http\Requests\OrderByCouponRequest;
- use App\Models\Coupon;
- use App\Models\CouponsUserBag;
- use App\Models\Order;
- use App\Repositories\CardRidingUserBagsRepository;
- use App\Repositories\CouponUserBagsRepository;
- use App\Repositories\OrderRepository;
- use App\Transformers\CouponsUserBagsTransformer;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Log;
- class CouponController extends BaseController
- {
- public function index(CouponUserBagsRequest $couponUserBagsRequest, CouponUserBagsRepository $couponUserBagsRepository)
- {
- try {
- $status = $couponUserBagsRequest->get('status') ?? '';
- $coupons = $couponUserBagsRepository->getCouponsByUserIdAndStatus($this->user->id, $status, CouponsUserBag::ORDER_TYPE_ALL);
- return $this->response->collection($coupons, CouponsUserBagsTransformer::class);
- } catch (\Exception $e) {
- return $this->errorNoValidation($e->getMessage());
- }
- }
- /**
- * getCouponsByOrder 根据订单no 获取可用优惠券与不可用优惠券
- *
- * @param CouponUserBagsByOrderRequest $couponUserBagsByOrderRequest
- * @param OrderRepository $orderRepository
- * @param CouponUserBagsRepository $couponUserBagsRepository
- * @return void
- * @author Fx
- *
- */
- public function getCouponsByOrder(CouponUserBagsByOrderRequest $couponUserBagsByOrderRequest, OrderRepository $orderRepository, CouponUserBagsRepository $couponUserBagsRepository, CardRidingUserBagsRepository $cardRidingUserBagsRepository)
- {
- try {
- $order_no = $couponUserBagsByOrderRequest->get('order_no');
- $order = $orderRepository->byNo($order_no);
- $order_time_money = $order->time_money; // 只能抵扣时长费用
- $order_dispatch_money = $order->dispatch_money;
- $order_preferential_money = $order->preferential_money;
- $coupons = $couponUserBagsRepository->getCouponsByUserIdAndStatus($this->user->id, CouponsUserBag::STATUS_OK, CouponsUserBag::ORDER_TYPE_NORMAL);
- $datayes = [];
- $score = [];
- $ids = [];
- $datano = [];
- if (count($coupons) !== 0) {
- foreach ($coupons as $v) {
- $v = $v->toArray();
- $cou = $v['coupons_data'];
- $valid_days_end_time = null;
- if ($v['valid_type'] == CouponsUserBag::VALID_TYPE_RELATIVE) {
- $valid_days_end_time = Carbon::make($v['created_at'])->addDays($v['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) {
- $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;
- $is_ok = false;
- $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'],
- ];
- } 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['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['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['preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2);
- $is_ok = true;
- break;
- default:
- $is_ok = false;
- return $this->errorNoValidation('优惠券类型出现错误,请联系管理员');
- }
- 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'], // 订单待支付
- 'preferential_money' => $v['preferential_money'], // 订单总优惠
- 'coupon_preferential_money' => bcsub($v['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 $this->response->array($data);
- }
- $data = [
- 'dataYes' => $datayes,
- 'dataNo' => $datano,
- ];
- return $this->response->array($data);
- } catch (\Exception $exception) {
- return $this->errorNoValidation($exception->getMessage());
- }
- }
- }
|