CouponController.php 13 KB

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