CouponController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/4/7 10:23
  12. */
  13. namespace App\Http\Controllers\V1;
  14. use App\Http\Requests\CouponUserBagsByOrderRequest;
  15. use App\Http\Requests\CouponUserBagsRequest;
  16. use App\Http\Requests\OrderByCouponRequest;
  17. use App\Models\Coupon;
  18. use App\Models\CouponsUserBag;
  19. use App\Models\Order;
  20. use App\Repositories\CardRidingUserBagsRepository;
  21. use App\Repositories\CouponUserBagsRepository;
  22. use App\Repositories\OrderRepository;
  23. use App\Transformers\CouponsUserBagsTransformer;
  24. use Carbon\Carbon;
  25. use Illuminate\Support\Facades\Log;
  26. class CouponController extends BaseController
  27. {
  28. public function index(CouponUserBagsRequest $couponUserBagsRequest, CouponUserBagsRepository $couponUserBagsRepository)
  29. {
  30. try {
  31. $status = $couponUserBagsRequest->get('status') ?? '';
  32. $coupons = $couponUserBagsRepository->getCouponsByUserIdAndStatus($this->user->id, $status, CouponsUserBag::ORDER_TYPE_ALL);
  33. return $this->response->collection($coupons, CouponsUserBagsTransformer::class);
  34. } catch (\Exception $e) {
  35. return $this->errorNoValidation($e->getMessage());
  36. }
  37. }
  38. /**
  39. * getCouponsByOrder 根据订单no 获取可用优惠券与不可用优惠券
  40. *
  41. * @param CouponUserBagsByOrderRequest $couponUserBagsByOrderRequest
  42. * @param OrderRepository $orderRepository
  43. * @param CouponUserBagsRepository $couponUserBagsRepository
  44. * @return void
  45. * @author Fx
  46. *
  47. */
  48. public function getCouponsByOrder(CouponUserBagsByOrderRequest $couponUserBagsByOrderRequest, OrderRepository $orderRepository, CouponUserBagsRepository $couponUserBagsRepository, CardRidingUserBagsRepository $cardRidingUserBagsRepository)
  49. {
  50. try {
  51. $order_no = $couponUserBagsByOrderRequest->get('order_no');
  52. $order = $orderRepository->byNo($order_no);
  53. $order_time_money = $order->time_money; // 只能抵扣时长费用
  54. $order_dispatch_money = $order->dispatch_money;
  55. $order_preferential_money = $order->preferential_money;
  56. $coupons = $couponUserBagsRepository->getCouponsByUserIdAndStatus($this->user->id, CouponsUserBag::STATUS_OK, CouponsUserBag::ORDER_TYPE_NORMAL);
  57. $datayes = [];
  58. $score = [];
  59. $ids = [];
  60. $datano = [];
  61. if (count($coupons) !== 0) {
  62. foreach ($coupons as $v) {
  63. $v = $v->toArray();
  64. $cou = $v['coupons_data'];
  65. $valid_days_end_time = null;
  66. if ($v['valid_type'] == CouponsUserBag::VALID_TYPE_RELATIVE) {
  67. $valid_days_end_time = Carbon::make($v['created_at'])->addDays($v['valid_days'])->format('Y-m-d');
  68. }
  69. switch ($cou['type']) {
  70. case Coupon::TYPE_MAN_JIAN: // 满减优惠券
  71. $with_amount = $cou['with_amount']; // 满多少金额
  72. $used_amount = $cou['used_amount']; // 可减少金额
  73. if (bcsub($with_amount, $order_time_money, 2) > 0) {
  74. $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);
  75. $v['total_preferential_money'] = $order_preferential_money;
  76. $is_ok = false;
  77. $datano[] = [
  78. 'id' => $v['id'],
  79. 'get_type_name' => CouponsUserBag::$typeMaps[$v['type']],
  80. 'type' => $cou['type'],
  81. 'type_name' => Coupon::$typeMaps[$cou['type']],
  82. 'valid_type_name' => CouponsUserBag::$validTypeMaps[$v['valid_type']],
  83. 'valid_type' => $v['valid_type'],
  84. 'valid_start_time' => Carbon::make($v['valid_start_time'])->format('Y-m-d'),
  85. 'valid_end_time' => Carbon::make($v['valid_end_time'])->format('Y-m-d'),
  86. 'valid_days' => $v['valid_days'],
  87. 'valid_days_end_time' => $valid_days_end_time,
  88. 'created_at' => Carbon::make($v['created_at'])->format('Y-m-d'),
  89. 'with_amount' => $cou['with_amount'],
  90. 'used_amount' => $cou['used_amount'],
  91. 'discount' => $cou['discount'] / 10,
  92. 'order_type' => $v['order_type'],
  93. 'order_type_name' => CouponsUserBag::$orderTypeMaps[$v['order_type']],
  94. // 'order_wait_pay_money' => $v['order_wait_pay_money'],
  95. // 'preferential_money' => $v['preferential_money'],
  96. ];
  97. } else {
  98. //1.待支付金额 = 时长费 - 之前优惠金额
  99. $v['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2);
  100. //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0)
  101. $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);
  102. // 3.最终待支付金额 = 第二待支付金额 + 调度费
  103. $v['order_wait_pay_money'] = bcadd($v['order_wait_pay_money'], $order_dispatch_money, 2);
  104. // 总优惠金额 = 时长 + 调度 -待支付
  105. $v['preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2);
  106. $is_ok = true;
  107. }
  108. break;
  109. case Coupon::TYPE_WU_MEN_KAN: // 无门槛优惠券
  110. $used_amount = $cou['used_amount']; // 可减少金额
  111. //1.待支付金额 = 时长费 - 之前优惠金额
  112. $v['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2);
  113. //2.待支付金额 = 上边待支付金额 - 优惠券优惠金额(小于0 取0)
  114. $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);
  115. // 3.最终待支付金额 = 第二待支付金额 + 调度费
  116. $v['order_wait_pay_money'] = bcadd($v['order_wait_pay_money'], $order_dispatch_money, 2);
  117. // 总优惠金额 = 时长 + 调度 -待支付
  118. $v['preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2);
  119. $is_ok = true;
  120. break;
  121. case Coupon::TYPE_ZHE_KOU:
  122. $discount = $cou['discount'];
  123. $used_amount = $cou['used_amount']; // 折扣最多可减少金额
  124. //1.待支付金额 = 时长费 - 之前优惠金额
  125. $v['order_wait_pay_money'] = bcsub($order_time_money, $order_preferential_money, 2);
  126. //折扣后得时长费金额 = 时长费 X 折扣
  127. $discount_time_money = bcdiv(bcmul($order_time_money, $discount, 2), 100, 2);
  128. //折扣优惠券 优惠的金额
  129. $max_used_amount = bcsub($order_time_money, $discount_time_money, 2);
  130. if ($max_used_amount > $used_amount) {
  131. //如果折扣优惠券优惠的金额 大于 折扣最多可减少得金额
  132. $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);
  133. } else {
  134. $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);
  135. }
  136. // 3.最终待支付金额 = 第二待支付金额 + 调度费
  137. $v['order_wait_pay_money'] = bcadd($v['order_wait_pay_money'], $order_dispatch_money, 2);
  138. // 总优惠金额 = 时长 + 调度 -待支付
  139. $v['preferential_money'] = bcsub(bcadd($order_time_money, $order_dispatch_money, 2), $v['order_wait_pay_money'], 2);
  140. $is_ok = true;
  141. break;
  142. default:
  143. $is_ok = false;
  144. return $this->errorNoValidation('优惠券类型出现错误,请联系管理员');
  145. }
  146. if ($is_ok) {
  147. $datayes[] = [
  148. 'id' => $v['id'],
  149. 'get_type_name' => CouponsUserBag::$typeMaps[$v['type']],
  150. 'type' => $cou['type'],
  151. 'type_name' => Coupon::$typeMaps[$cou['type']],
  152. 'valid_type_name' => CouponsUserBag::$validTypeMaps[$v['valid_type']],
  153. 'valid_type' => $v['valid_type'],
  154. 'valid_start_time' => Carbon::make($v['valid_start_time'])->format('Y-m-d'),
  155. 'valid_end_time' => Carbon::make($v['valid_end_time'])->format('Y-m-d'),
  156. 'valid_days' => $v['valid_days'],
  157. 'valid_days_end_time' => $valid_days_end_time,
  158. 'created_at' => Carbon::make($v['created_at'])->format('Y-m-d'),
  159. 'with_amount' => $cou['with_amount'],
  160. 'used_amount' => $cou['used_amount'],
  161. 'discount' => $cou['discount'] / 10,
  162. 'order_wait_pay_money' => $v['order_wait_pay_money'], // 订单待支付
  163. 'preferential_money' => $v['preferential_money'], // 订单总优惠
  164. 'coupon_preferential_money' => bcsub($v['preferential_money'], $order_preferential_money, 2), // 订单优惠券优惠
  165. 'order_type' => $v['order_type'], // 可用订单类型
  166. 'order_type_name' => CouponsUserBag::$orderTypeMaps[$v['order_type']], // 可用订单类型
  167. ];
  168. $score[] = $v['order_wait_pay_money'];
  169. $ids[] = $v['id'];
  170. }
  171. }
  172. }
  173. //排序最优
  174. if (!empty($datayes)) {
  175. array_multisort($score, SORT_ASC, $ids, SORT_ASC, $datayes);
  176. }
  177. if (bcsub($order_time_money, $order_preferential_money, 2) <= 0 || $order->status !== Order::STATUS_CLOSE_BIKE) {
  178. // 骑行花费待支付为0 不需要优惠券 直接返回空
  179. $data = [
  180. 'dataYes' => [],
  181. 'dataNo' => array_merge($datayes, $datano),
  182. ];
  183. return $this->response->array($data);
  184. }
  185. $data = [
  186. 'dataYes' => $datayes,
  187. 'dataNo' => $datano,
  188. ];
  189. return $this->response->array($data);
  190. } catch (\Exception $exception) {
  191. return $this->errorNoValidation($exception->getMessage());
  192. }
  193. }
  194. }