OrderService.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. <?php
  2. namespace app\admin\service\order;
  3. use app\model\CouponDetail;
  4. use app\model\CouponGoods;
  5. use app\model\Goods;
  6. use app\model\GoodsRunning;
  7. use app\model\GoodsSku;
  8. use app\model\Member;
  9. use app\model\MemberBenefit;
  10. use app\model\Order;
  11. use app\model\OrderSheet;
  12. use app\model\PayDetail;
  13. use app\model\SysDept;
  14. use support\Db;
  15. use support\exception\BusinessException;
  16. use support\Log as SupportLog;
  17. use support\Redis;
  18. use Webman\Event\Event;
  19. use Yansongda\Pay\Exceptions\GatewayException;
  20. use Yansongda\Pay\Log;
  21. use Yansongda\Pay\Pay;
  22. class OrderService
  23. {
  24. /**
  25. * @Desc 自动确认收货
  26. * @Author Gorden
  27. * @Date 2024/4/11 16:09
  28. *
  29. * @return void
  30. */
  31. public static function AutomaticReceipt()
  32. {
  33. try {
  34. Db::beginTransaction();
  35. $timeUnix = strtotime("-7 days");
  36. $orders = Order::where('order_status_system', 'SIGNED')
  37. ->where('order_addtimes', '<', $timeUnix)
  38. ->get();
  39. foreach ($orders as $order) {
  40. // 订单主表
  41. Order::where('order_id', $order->order_id)->update([
  42. 'order_is_complete' => 'Y',
  43. 'order_status_system' => 'CONFIRM',
  44. 'order_status_storage' => 'DONE'
  45. ]);
  46. // 订单详情表
  47. OrderSheet::where('join_sheet_order_id', $order->order_id)->update(['order_sheet_status' => 'DONE']);
  48. // 会员升级
  49. Event::dispatch('order_pay.member_level.up', $order->join_order_member_id);
  50. // 7天后自动完成 order_is_complete=Y
  51. // $redis = Redis::connection();
  52. // $key = Order::AUTOMATIC_COMPLETE_PREFIX . date('Ymd', strtotime("+7 days"));
  53. // $redis->sadd($key, $order->order_id);
  54. }
  55. Db::commit();
  56. } catch (\Exception $e) {
  57. Db::rollBack();
  58. }
  59. }
  60. /**
  61. * @Desc 自动完成订单
  62. * @Author Gorden
  63. * @Date 2024/7/16 9:37
  64. *
  65. * @return void
  66. */
  67. public static function AutomaticComplete()
  68. {
  69. Db::beginTransaction();
  70. try {
  71. $redis = Redis::connection();
  72. $key = Order::AUTOMATIC_COMPLETE_PREFIX . date('Ymd');
  73. $orderIds = $redis->smembers($key);
  74. foreach ($orderIds as $orderId) {
  75. $order = Order::where('order_id', $orderId)
  76. ->select('order_is_complete', 'order_category', 'order_status_system', 'join_order_member_id')
  77. ->first();
  78. if ($order && $order->order_is_complete != 'Y' && $order->order_category != 'RETURN' && in_array($order->order_status_system, ['RECVING', 'SIGNED', 'CONFIRM'])) {
  79. // 更新主表
  80. Order::where('order_id', $orderId)->update(['order_is_complete' => 'Y', 'order_status_system' => 'CONFIRM', 'order_status_storage' => 'DONE']);
  81. // sheet表
  82. OrderSheet::where('join_sheet_order_id', $orderId)->update(['order_sheet_status' => 'DONE']);
  83. // 会员升级
  84. Event::dispatch('order_pay.member_level.up', $order->join_order_member_id);
  85. }
  86. }
  87. $redis->del($key);
  88. Db::commit();
  89. } catch (\Exception $e) {
  90. dump($e->getMessage());
  91. Db::rollBack();
  92. }
  93. }
  94. public static function checkPayingOrder()
  95. {
  96. try {
  97. Db::beginTransaction();
  98. $timeUnix = strtotime("-30 minutes");
  99. $orders = Order::where('order_status_system', 'PAYING')
  100. ->where('order_category', '<>', 'DISHES') // 点餐不自动取消
  101. ->where('order_category', '<>', 'VIP') // 康养城订单不自动取消,有分次付款
  102. ->where('order_category', '<>', 'COMBINE') // 新客专享
  103. ->where('order_category', '<>', 'PARTNER') // 会员合伙人
  104. ->where('order_category', '<>', 'REFERRER') // 推荐官
  105. ->where(function ($query) {
  106. $query->where('order_platform', '<>', 'SYSTEM')->orWhereNull('order_platform');
  107. })
  108. ->where('order_addtimes', '<', $timeUnix)
  109. ->get();
  110. foreach ($orders as $order) {
  111. \support\Log::info("取消的订单", ['order_id' => $order->order_id]);
  112. // 订单主表
  113. Order::where('order_id', $order->order_id)->update([
  114. 'order_is_complete' => 'Y',
  115. 'order_status_system' => 'CANCEL',
  116. 'order_status_payment' => 'CANCEL'
  117. ]);
  118. $sheets = OrderSheet::where('join_sheet_order_id', $order->order_id)->get();
  119. foreach ($sheets as $sheet) {
  120. // 还原库存
  121. $goodsSku = GoodsSku::where('goods_sku_id', $sheet->join_sheet_goods_sku_id)->first();
  122. if (!empty($goodsSku) && !empty($goodsSku->goods_sku_storage_json)) {
  123. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  124. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  125. $skuStorageJson['storage'] = $skuStorageJson['storage'] + $sheet->order_sheet_num;
  126. $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson);
  127. $goodsSku->save();
  128. }
  129. }
  130. $goodsRunning = GoodsRunning::where('join_running_goods_id', $sheet->join_sheet_goods_id)->first();
  131. if (!empty($goodsRunning)) {
  132. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage + $sheet->order_sheet_num;
  133. // $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale - $sheet->order_sheet_num;
  134. $goodsRunning->save();
  135. }
  136. }
  137. // 释放优惠券
  138. if (!empty($order->order_discount_json)) {
  139. $orderDiscountJson = json_decode($order->order_discount_json, true);
  140. foreach ($orderDiscountJson as $discount) {
  141. if (!empty($discount['coupon_id']) && !empty($discount['coupon_detail_id'])) {
  142. foreach ($discount['coupon_detail_id'] as $detailId) {
  143. if (substr($detailId, 0, 4) == 'CUDT') {
  144. // 恢复优惠券
  145. CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => 'ACTIVED']);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. // 订单详情表
  152. OrderSheet::where('join_sheet_order_id', $order->order_id)->update(['order_sheet_status' => 'CANCEL']);
  153. // 支付表
  154. $payDetail = PayDetail::where('join_pay_order_id', $order->order_groupby)->first();
  155. if (!empty($payDetail)) {
  156. $payExtendJson = [];
  157. if (!empty($payDetail->pay_extend_json)) {
  158. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  159. }
  160. $payExtendJson['cancel_times'] = date('Y-m-d H:i:s');
  161. PayDetail::where('join_pay_order_id', $order->order_groupby)->update([
  162. 'pay_status' => 'CANCEL',
  163. 'pay_extend_json' => json_encode($payExtendJson)
  164. ]);
  165. }
  166. }
  167. Db::commit();
  168. } catch (\Exception $e) {
  169. \support\Log::error("取消订单失败:" . $e->getMessage());
  170. Db::rollBack();
  171. }
  172. }
  173. /**
  174. * @Desc 生成核销数据
  175. * @Author Gorden
  176. * @Date 2024/9/9 17:07
  177. *
  178. * @param $params
  179. * @return array
  180. */
  181. public static function generateWriteOffData($params)
  182. {
  183. return [
  184. 'charge' => [
  185. 'charge_amount' => 1,
  186. 'charge_content' => $params['order_remark'] ?? '',
  187. 'charge_user_id' => $params['write_off_member_id'],
  188. 'charge_premises' => $params['dept_premises_id'],
  189. 'charge_waiter' => $params['charge_waiter'] ?? ''
  190. ],
  191. 'member_id' => $params['join_order_member_id']
  192. ];
  193. }
  194. /**
  195. * @Desc 生成核销数据-入order_process
  196. * @Author Gorden
  197. * @Date 2024/9/9 17:07
  198. *
  199. * @param $params
  200. * @return array
  201. */
  202. public static function generateWriteOffDataByOrderProcess($params)
  203. {
  204. return [
  205. 'charge' => [
  206. 'charge_amount' => $params['charge_amount'],
  207. 'charge_content' => $params['order_remark'] ?? '',
  208. 'charge_waiter' => $params['charge_waiter'] ?? '',
  209. 'charge_user_id' => $params['write_off_member_id'],
  210. 'charge_premises' => $params['dept_premises_id'],
  211. 'charge_premises_info' => $params['dept'] ?? ''
  212. ],
  213. 'member_id' => $params['join_order_member_id'],
  214. 'goods_id' => $params['goods_id'] ?? '',
  215. 'goods_sku_id' => $params['goods_sku_id'] ?? '',
  216. 'order_id' => $params['order_id'],
  217. 'platform' => 'SYSTEM',
  218. 'order_code' => random_string(10, 'number'),
  219. 'appointment' => $params['appointment_ids'] ?? '',
  220. ];
  221. }
  222. public static function generateAppointmentApplyData($params)
  223. {
  224. $member = Member::with('cert', 'info')
  225. ->where('member_id', $params['join_order_member_id'])
  226. ->first();
  227. $name = '';
  228. if (!empty($member) && !empty($member->cert) && !empty($member->cert->member_cert_name)) {
  229. $name = $member->cert->member_cert_name;
  230. } else if (!empty($member) && !empty($member->info) && !empty($member->info->member_info_nickname)) {
  231. $name = $member->info->member_info_nickname;
  232. }
  233. return [
  234. 'name' => $name,
  235. 'times' => '',
  236. 'mobile' => !empty($member) ? $member->member_mobile : '',
  237. 'person' => $params['order_sheet_num'] ?? '',
  238. 'premises' => $params['dept_premises_id'] ?? ''
  239. ];
  240. }
  241. /**
  242. * 微信支付宝扫码支付
  243. */
  244. public static function qrcodePay($params)
  245. {
  246. $log = SupportLog::channel('pay');
  247. $log->info("PAY_PARAMS", json_decode(json_encode($params), true));
  248. $params['order_amount_pay'] = floatval($params['order_amount_pay']);
  249. $qrcodeNbr = $params['qrcode_nbr'];
  250. $prefix = substr($qrcodeNbr, 0, 2);
  251. // 模拟数据
  252. // $result = [
  253. // 'return_code'=>'SUCCESS',
  254. // 'result_code' => 'SUCCESS'
  255. // ];
  256. // $result=[
  257. // 'code'=> '10000',
  258. // 'msg' => 'Success'
  259. // ];
  260. // return $result;
  261. // 微信支付
  262. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  263. $payData = [
  264. 'out_trade_no' => $params['orderGroupId'],
  265. 'body' => '万悦康养订单',
  266. 'total_fee' => $params['order_amount_pay'] * 100,
  267. 'auth_code' => $params['qrcode_nbr'],
  268. ];
  269. try {
  270. $config = config('payment.wxpay');
  271. $config['notify_url'] = getenv('NOTIFY_DOMAIN_ADMIN') . '/notify/orderPay/wxpay';
  272. $wxReturn = Pay::wechat($config)->pos($payData);
  273. $log->info("WXPAY_RETURN", json_decode(json_encode($wxReturn), true));
  274. $result = self::findWxpay($params['orderGroupId'], 0);
  275. } catch (GatewayException $g) {
  276. $result = self::findWxpay($params['orderGroupId'], 0);
  277. } catch (\Exception $e) {
  278. $log->error("WXPAY", ['msg' => $e->getMessage()]);
  279. $result = self::findWxpay($params['orderGroupId'], 0);
  280. // throw new BusinessException("支付失败");
  281. }
  282. try {
  283. $log->info("WXPAY_RETURN", json_decode(json_encode($result), true));
  284. } catch (\Exception $e) {
  285. }
  286. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  287. $payData = [
  288. 'out_trade_no' => $params['orderGroupId'],
  289. 'total_amount' => $params['order_amount_pay'],
  290. 'subject' => '万悦康养订单',
  291. 'auth_code' => $params['qrcode_nbr'],
  292. ];
  293. try {
  294. $config = config('payment.alipay');
  295. $config['notify_url'] = getenv('NOTIFY_DOMAIN_ADMIN') . '/notify/orderPay/alipay';
  296. $alipayReturn = Pay::alipay($config)->pos($payData);
  297. $log->info("WXPAY_RETURN", json_decode(json_encode($alipayReturn), true));
  298. $result = self::findAlipay($params['orderGroupId'], 0);
  299. } catch (GatewayException $g) {
  300. $result = self::findAlipay($params['orderGroupId'], 0);
  301. } catch (\Exception $e) {
  302. $log->error("ALIPAY", ['msg' => $e->getMessage()]);
  303. throw new BusinessException("支付失败:" . $e->getMessage());
  304. // $result['key'] = 'value';
  305. }
  306. try {
  307. $log->info("ALIPAY_RETURN", json_decode(json_encode($result), true));
  308. } catch (\Exception $e) {
  309. }
  310. } else {
  311. throw new BusinessException("付款码无效");
  312. }
  313. return $result;
  314. }
  315. /**
  316. * @Desc 查询微信支付
  317. * @Author Gorden
  318. * @Date 2024/8/16 14:55
  319. *
  320. * @param $orderId
  321. * @param $nbr 循环次数
  322. * @return mixed|void
  323. * @throws BusinessException
  324. */
  325. public static function findWxpay($orderId, $nbr = 0)
  326. {
  327. try {
  328. $result = Pay::wechat(config('payment.wxpay'))->find($orderId, 'pos');
  329. $result = json_decode(json_encode($result), true);
  330. SupportLog::channel('pay')->info("FIND_WXPAY_RESULT", $result);
  331. } catch (\Exception $e) {
  332. SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => $e->getMessage()]);
  333. }
  334. if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
  335. SupportLog::channel('pay')->info("FIND_WXPAY_SUCCESS", ['nbr' => $nbr, 'order_id' => $orderId]);
  336. return $result;
  337. } else {
  338. if ($nbr > 0) {
  339. SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => '订单查询失败', 'order_id' => $orderId]);
  340. return ['msg' => '订单查询失败'];
  341. }
  342. sleep(3);
  343. SupportLog::channel('pay')->error("FIND_WXPAY", ['nbr' => $nbr, 'order_id' => $orderId]);
  344. return self::findWxpay($orderId, $nbr + 1);
  345. }
  346. }
  347. /**
  348. * @Desc 查询支付宝订单
  349. * @Author Gorden
  350. * @Date 2024/8/16 15:16
  351. *
  352. * @param $orderId
  353. * @param $nbr 循环次数
  354. * @return mixed|void
  355. * @throws BusinessException
  356. */
  357. public static function findAlipay($orderId, $nbr = 0)
  358. {
  359. try {
  360. $result = Pay::alipay(config('payment.alipay'))->find($orderId);
  361. $result = json_decode(json_encode($result), true);
  362. SupportLog::channel('pay')->info("FIND_ALIPAY_RESULT", $result);
  363. } catch (\Exception $e) {
  364. SupportLog::channel('pay')->error("FIND_ALIPAY", ['msg' => $e->getMessage()]);
  365. }
  366. if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
  367. SupportLog::channel('pay')->info("FIND_ALIPAY_SUCCESS", ['nbr' => $nbr, 'order_id' => $orderId]);
  368. return $result;
  369. } else {
  370. if ($nbr > 0) {
  371. SupportLog::channel('pay')->error("FIND_ALIPAY", ['msg' => '订单查询失败', 'order_id' => $orderId]);
  372. return ['msg' => '订单查询失败'];
  373. }
  374. sleep(3);
  375. SupportLog::channel('pay')->error("FIND_ALIPAY", ['nbr' => $nbr, 'order_id' => $orderId]);
  376. return self::findAlipay($orderId, $nbr + 1);
  377. }
  378. }
  379. /**
  380. * 验证产品库存
  381. */
  382. public static function checkGoodsStorage($params)
  383. {
  384. foreach ($params['goodsContentList'] as $goods) {
  385. // 减库存,规格和总库存
  386. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE', 'COMBINE'])) {
  387. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  388. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  389. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  390. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  391. }
  392. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  393. throw new BusinessException('库存不足');
  394. }
  395. }
  396. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  397. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  398. if ($goodsRunning->goods_running_storage < 0) {
  399. throw new BusinessException('库存不足');
  400. }
  401. }
  402. }
  403. /**
  404. * 下单时结算的组合支付payDetail
  405. */
  406. public static function createPayDetail($params)
  407. {
  408. $insertPayDetailData = [
  409. 'join_pay_member_id' => $params['join_order_member_id'],
  410. 'join_pay_order_id' => $params['orderGroupId'],
  411. 'pay_status' => $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  412. 'pay_category' => !empty($params['submit_goods_classify']) ? $params['submit_goods_classify'] : $params['goods_classify'],
  413. 'pay_amount' => $params['order_amount_pay'],
  414. 'pay_paytimes' => date('Y-m-d H:i:s'),
  415. 'pay_prepayid' => $params['pay_category'],
  416. 'pay_json_request' => json_encode($params),
  417. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  418. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
  419. 'pay_addtimes' => time()
  420. ];
  421. PayDetail::insert($insertPayDetailData);
  422. }
  423. /**
  424. * 组合支付,PayDetail
  425. */
  426. public static function createPayConstituteDetail($params, $payDetail)
  427. {
  428. $qrcodePrepayId = '';
  429. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  430. $qrcodePrepayId = $params['join_order_member_id'] . '-QRCODE';
  431. }
  432. if (!$payDetail || ($payDetail->pay_prepayid != $params['pay_category'] && $payDetail->pay_prepayid != $qrcodePrepayId)) {
  433. $payDetail = new PayDetail();
  434. $payDetail->join_pay_member_id = $params['join_order_member_id'];
  435. $payDetail->join_pay_order_id = $params['orderGroupId'];
  436. $payDetail->pay_category = $params['goods_classify'] ?? '';
  437. $payDetail->pay_prepayid = $params['pay_category'];
  438. $payDetail->pay_json_request = json_encode($params);
  439. $payDetail->pay_addtimes = time();
  440. }
  441. if ($payDetail->pay_prepayid == $qrcodePrepayId) {
  442. $payDetail->pay_prepayid = $params['pay_category'];
  443. }
  444. $payDetail->pay_json_response = $params['pay_json_response'] ?? json_encode([
  445. 'pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s')
  446. ]);
  447. $payDetail->pay_amount = $params['order_amount_pay'];
  448. $payDetail->pay_paytimes = date('Y-m-d H:i:s');
  449. $payDetail->pay_status = $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING';;
  450. $payDetail->join_pay_object_json = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
  451. $payDetail->save();
  452. }
  453. /**
  454. * 组合支付,PayDetail
  455. */
  456. public static function createProductPayConstituteDetail($params)
  457. {
  458. $payDetail = new PayDetail();
  459. $payDetail->join_pay_member_id = $params['join_order_member_id'];
  460. $payDetail->join_pay_order_id = $params['orderGroupId'];
  461. $payDetail->pay_category = $params['goods_classify'] ?? '';
  462. $payDetail->pay_json_request = json_encode($params);
  463. $payDetail->pay_addtimes = time();
  464. $payDetail->pay_prepayid = $params['pay_category'];
  465. $payDetail->pay_json_response = $params['pay_json_response'] ?? json_encode([
  466. 'pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s')
  467. ]);
  468. $payDetail->pay_amount = $params['order_amount_pay'];
  469. $payDetail->pay_paytimes = date('Y-m-d H:i:s');
  470. $payDetail->pay_status = $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING';
  471. $payDetail->join_pay_object_json = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
  472. $payDetail->save();
  473. }
  474. public static function getPayWayByPrepayId($prepayId)
  475. {
  476. $payWay = '';
  477. $categoryArray = explode('-', $prepayId);
  478. if (isset($categoryArray[1])) {
  479. $payWay = $categoryArray[1];
  480. } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) {
  481. $payWay = $categoryArray[0];
  482. }
  483. return $payWay;
  484. }
  485. /**
  486. * @Desc 支付使用优惠券
  487. * @Author Gorden
  488. * @Date 2024/8/28 14:59
  489. *
  490. * @param $memberId
  491. * @param $goods
  492. * @param $coupon
  493. * @param $payAmount
  494. * @return array|mixed
  495. */
  496. public static function payUseCoupon($type, $settlementNow, $memberId, $goods, $coupon, $payAmount)
  497. {
  498. try {
  499. foreach ($coupon as $item) {
  500. if (!in_array(substr($item, 0, 2), ['CU', 'CO'])) {
  501. return [];
  502. }
  503. }
  504. $amountBalance = [
  505. 'pay_amount' => $payAmount,
  506. 'welfare_balance' => 0,
  507. 'cut_balance' => 0,
  508. ];
  509. foreach ($goods as $good) {
  510. $result = OrderService::useCoupon($type, $settlementNow, $memberId, $goods, $good, $coupon, $amountBalance);
  511. $amountBalance = [
  512. 'pay_amount' => $result['pay_amount'],
  513. 'welfare_balance' => $result['welfare_balance'],
  514. 'cut_balance' => $result['cut_balance'],
  515. ];
  516. }
  517. $couponDetailsIds = Redis::sMembers("ORDER:USE:COUPON:" . $memberId);
  518. Redis::del("ORDER:USE:COUPON:" . $memberId);
  519. $useCouponJson = Redis::get("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
  520. Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
  521. return [
  522. 'pay_amount' => $amountBalance['pay_amount'],
  523. 'detail_ids' => $couponDetailsIds,
  524. 'use_coupon_json' => $useCouponJson ?? []
  525. ];
  526. } catch (\Exception $e) {
  527. dump($e->getTrace());
  528. }
  529. }
  530. public static function useCoupon($type, $settlementNow, $memberId, $goods, $good, $coupon, $amountBalance)
  531. {
  532. try {
  533. $cacheKey = "ORDER:USE:COUPON:" . $memberId;
  534. $cacheDiscountKey = "ORDER:USE:COUPON:DISCOUNT:" . $memberId;
  535. $payAmount = $amountBalance['pay_amount'];
  536. $welfareBalance = $amountBalance['welfare_balance'];
  537. $cutBalance = $amountBalance['cut_balance'];
  538. $goodsId = $good['goods_id'];
  539. $money = $good['goods_sales_price'] * $good['nbr'];
  540. $discountData = Redis::get($cacheDiscountKey);
  541. if (empty($discountData)) {
  542. $discountData = [];
  543. } else {
  544. $discountData = json_decode($discountData, true);
  545. }
  546. foreach ($coupon as $couponId) {
  547. $couponDetail = Db::table('coupon_detail')->leftJoin('coupon_goods', 'coupon_goods.join_goods_coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  548. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  549. ->select('coupon_detail.coupon_detail_id', 'coupon_goods.coupon_goods_id', 'coupon_id', 'coupon_classify', 'coupon_value', 'coupon_minimum_limit', 'coupon_category')
  550. ->where('coupon_goods.join_coupon_goods_id', $goodsId)
  551. // ->where('coupon_goods.join_coupon_goods_sku_id', $good['sku_id'])
  552. ->where('join_goods_coupon_id', $couponId)
  553. ->where('coupon_detail.join_coupon_detail_member_id', $memberId)
  554. ->where('coupon_detail.coupon_detail_gain_datetime', '<=', date('Y-m-d H:i:s'))
  555. ->where('coupon_detail.coupon_detail_deadline_datetime', '>=', date('Y-m-d H:i:s'))
  556. ->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  557. // if ($settlementNow == 'Y' && $type == 'pay') {
  558. // $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
  559. // } else {
  560. // $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  561. // }
  562. $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
  563. ->first();
  564. if (!$couponDetail) {
  565. continue;
  566. }
  567. $updateData = [
  568. 'coupon_detail_status' => 'USED',
  569. 'coupon_detail_used_datetime' => date('Y-m-d H:i:s'),
  570. ];
  571. // if ($settlementNow == 'Y') {
  572. // $updateData = [
  573. // 'coupon_detail_status' => 'USED',
  574. // 'coupon_detail_used_datetime' => date('Y-m-d H:i:s'),
  575. // ];
  576. // } else {
  577. // $updateData = [
  578. // 'coupon_detail_status' => 'WAITING',
  579. // ];
  580. // }
  581. // 计算优惠券包含的优惠商品的件数和总价
  582. $countAndAmount = self::countAndAmount($goods, $couponId);
  583. // 如果是计件
  584. if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] < $couponDetail->coupon_minimum_limit) {
  585. continue;
  586. }
  587. if (in_array($couponDetail->coupon_classify, ['立减券', '满减券'])) {
  588. if (Redis::sIsMember($cacheKey, $couponId)) {
  589. continue;
  590. }
  591. Redis::sAdd($cacheKey, $couponId);
  592. if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  593. $payAmount = $payAmount - $couponDetail->coupon_value;
  594. // json记录
  595. $discountData[$couponId] = [
  596. 'coupon_id' => $couponId,
  597. 'coupon_value' => $couponDetail->coupon_value,
  598. 'coupon_classify' => $couponDetail->coupon_classify,
  599. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  600. ];
  601. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  602. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  603. }
  604. } elseif ($couponDetail->coupon_classify == '折扣券') {
  605. if (Redis::sIsMember($cacheKey, $couponId)) {
  606. continue;
  607. }
  608. Redis::sAdd($cacheKey, $couponId);
  609. if ($couponDetail->coupon_category == 'PIECE' || $couponDetail->coupon_category == 'PROMOTION' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  610. $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
  611. $payAmount = $payAmount - $zhekouAmount;
  612. // json记录
  613. $discountData[$couponId] = [
  614. 'coupon_id' => $couponId,
  615. 'coupon_value' => $zhekouAmount,
  616. 'coupon_classify' => $couponDetail->coupon_classify,
  617. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  618. ];
  619. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  620. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  621. }
  622. } elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
  623. if (Redis::sIsMember($cacheKey, $couponId)) {
  624. continue;
  625. }
  626. Redis::sAdd($cacheKey, $couponId);
  627. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  628. if ($good['nbr'] > 1) {
  629. $diyongAmount = $good['goods_sales_price'];
  630. $payAmount = $payAmount - $diyongAmount;
  631. } elseif (ceil($good['nbr']) == 1) {
  632. $diyongAmount = round($good['goods_sales_price'] * $good['nbr'], 2);
  633. $payAmount = $payAmount - $diyongAmount;
  634. }
  635. // json记录
  636. $discountData[$couponId] = [
  637. 'coupon_id' => $couponId,
  638. 'coupon_value' => $diyongAmount,
  639. 'coupon_classify' => $couponDetail->coupon_classify,
  640. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  641. ];
  642. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  643. } elseif ($couponDetail->coupon_classify == '福利券') {
  644. if (Redis::sIsMember($cacheKey, $couponId)) {
  645. continue;
  646. }
  647. Redis::sAdd($cacheKey, $couponId);
  648. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  649. $fuliAmount = 0;
  650. if (!empty($couponDetail->coupon_value)) {
  651. $fuliAmount = $couponDetail->coupon_value;
  652. }
  653. if ($fuliAmount >= $countAndAmount['amount']) {
  654. $preferentialAmount = $countAndAmount['amount'];
  655. } else {
  656. $preferentialAmount = $fuliAmount;
  657. }
  658. $payAmount = $payAmount - $preferentialAmount;
  659. // json记录
  660. $discountData[$couponId] = [
  661. 'coupon_id' => $couponId,
  662. 'coupon_value' => $preferentialAmount,
  663. 'coupon_classify' => $couponDetail->coupon_classify,
  664. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  665. ];
  666. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  667. } elseif (in_array($couponDetail->coupon_classify, ['年卡', '季卡', '月卡'])) {
  668. if (Redis::sIsMember($cacheKey, $couponId) || Redis::sIsMember($cacheKey, $goodsId)) {
  669. continue;
  670. }
  671. Redis::sAdd($cacheKey, $couponId);
  672. Redis::sAdd($cacheKey, $goodsId);
  673. $kaAmount = 0;
  674. if (!empty($discountData[$couponId]['coupon_value'])) {
  675. $kaAmount = $discountData[$couponId]['coupon_value'];
  676. }
  677. if ($good['nbr'] > 1) {
  678. $payAmount = $payAmount - $good['goods_sales_price'];
  679. $kaAmount = $good['goods_sales_price'];
  680. } else {
  681. $payAmount = $payAmount - $good['goods_sales_price'] * $good['nbr'];
  682. $kaAmount = $good['goods_sales_price'] * $good['nbr'];
  683. }
  684. // json记录
  685. $discountData[$couponId] = [
  686. 'coupon_id' => $couponId,
  687. 'coupon_value' => $kaAmount,
  688. 'coupon_classify' => $couponDetail->coupon_classify,
  689. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  690. ];
  691. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  692. }
  693. }
  694. if ($payAmount < 0) {
  695. $payAmount = 0;
  696. }
  697. $amountBalance = [
  698. 'pay_amount' => round($payAmount, 2),
  699. 'welfare_balance' => 0,
  700. 'cut_balance' => 0
  701. ];
  702. return $amountBalance;
  703. } catch (\Exception $e) {
  704. Redis::del("ORDER:USE:COUPON:" . $memberId);
  705. Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
  706. dump($e->getTrace());
  707. }
  708. }
  709. /**
  710. * @Desc 选择优惠券,计算
  711. * @Author Gorden
  712. * @Date 2024/8/28 14:18
  713. *
  714. * @param $memberId
  715. * @param $goods
  716. * @param $good
  717. * @param $coupon
  718. * @param $amountBalance
  719. * @return array|void
  720. */
  721. public static function chooseCoupon($settlementNow, $memberId, $goods, $good, $coupon, $amountBalance, $orderId)
  722. {
  723. $couponDetailIds = [];
  724. if (!empty($orderId)) {
  725. $order = Order::where('order_id', $orderId)->first();
  726. if (!empty($order->order_discount_json)) {
  727. $orderDiscountJson = json_decode($order->order_discount_json, true);
  728. foreach ($orderDiscountJson as $orderCoupon) {
  729. if ($coupon[0] == $orderCoupon['coupon_id']) {
  730. $couponDetailIds = $orderCoupon['coupon_detail_id'];
  731. }
  732. }
  733. }
  734. }
  735. try {
  736. $cacheKey = "ORDER:USE:COUPON:" . $memberId;
  737. $payAmount = $amountBalance['pay_amount'];
  738. $welfareBalance = $amountBalance['welfare_balance'];
  739. $cutBalance = $amountBalance['cut_balance'];
  740. $goodsId = $good['goods_id'];
  741. $money = $good['goods_sales_price'] * $good['nbr'];
  742. foreach ($coupon as $couponId) {
  743. $couponDetail = CouponDetail::leftJoin('coupon_goods', 'coupon_goods.join_goods_coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  744. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  745. ->select('coupon_detail.coupon_detail_id', 'coupon_goods.coupon_goods_id', 'coupon_id', 'coupon_classify', 'coupon_value', 'coupon_minimum_limit', 'coupon_category')
  746. ->whereIn('coupon_detail_id', $couponDetailIds)
  747. ->orWhere(function ($query) use ($goodsId, $couponId, $memberId) {
  748. $query->where('coupon_goods.join_coupon_goods_id', $goodsId)
  749. ->where('join_goods_coupon_id', $couponId)
  750. ->where('coupon_detail.join_coupon_detail_member_id', $memberId)
  751. ->where('coupon_detail.coupon_detail_gain_datetime', '<=', date('Y-m-d H:i:s'))
  752. ->where('coupon_detail.coupon_detail_deadline_datetime', '>=', date('Y-m-d H:i:s'))
  753. ->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  754. });
  755. // ->where('coupon_goods.join_coupon_goods_id', $goodsId)
  756. // ->where('join_goods_coupon_id', $couponId)
  757. // ->where('coupon_detail.join_coupon_detail_member_id', $memberId)
  758. // ->where('coupon_detail.coupon_detail_gain_datetime', '<=', date('Y-m-d H:i:s'))
  759. // ->where('coupon_detail.coupon_detail_deadline_datetime', '>=', date('Y-m-d H:i:s'))
  760. // ->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  761. // if ($settlementNow == 'Y') {
  762. // $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
  763. // } else {
  764. // $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  765. // }
  766. $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
  767. ->first();
  768. if (!$couponDetail) {
  769. continue;
  770. }
  771. // 计算优惠券包含的优惠商品的件数和总价
  772. $countAndAmount = self::countAndAmount($goods, $couponId);
  773. // 如果是计件
  774. if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] < $couponDetail->coupon_minimum_limit) {
  775. continue;
  776. }
  777. if (in_array($couponDetail->coupon_classify, ['立减券', '满减券'])) {
  778. if (Redis::sIsMember($cacheKey, $couponId)) {
  779. continue;
  780. }
  781. Redis::sAdd($cacheKey, $couponId);
  782. if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  783. $payAmount = $payAmount - $couponDetail->coupon_value;
  784. }
  785. }
  786. // elseif ($couponDetail->coupon_classify == '满减券') {
  787. // if (Redis::sIsMember($cacheKey, $couponId)) {
  788. // continue;
  789. // }
  790. // Redis::sAdd($cacheKey, $couponId);
  791. // // 计件
  792. // if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] >= $couponDetail->coupon_minimum_limit) {
  793. // $goodsIds = array_splice($countAndAmount['goodsIds'], 0, $couponDetail->coupon_value);
  794. // $amount = 0;
  795. // $goodsNbr = 0;
  796. // foreach ($goods as $countAndAmountGood) {
  797. // if (in_array($countAndAmountGood['goods_id'], $goodsIds) && $goodsNbr < $couponDetail->coupon_value) {
  798. // if ($goodsNbr + $countAndAmountGood['nbr'] <= $couponDetail->coupon_value) {
  799. // $amount = $amount + round($countAndAmountGood['goods_sales_price'] * $countAndAmountGood['nbr'], 2);
  800. // $goodsNbr += $countAndAmountGood['nbr'];
  801. // } elseif ($goodsNbr + $countAndAmountGood['nbr'] > $couponDetail->coupon_value) {
  802. // $amount = $amount + round($countAndAmountGood['goods_sales_price'] * ($couponDetail->coupon_value - $goodsNbr), 2);
  803. // $goodsNbr = $couponDetail->coupon_value - $goodsNbr;
  804. // }
  805. // }
  806. // }
  807. // $payAmount = $payAmount - $amount;
  808. // }else if ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit) { // 金额
  809. // $payAmount = $payAmount - $couponDetail->coupon_value;
  810. // }
  811. // }
  812. elseif ($couponDetail->coupon_classify == '折扣券') {
  813. if (Redis::sIsMember($cacheKey, $couponId)) {
  814. continue;
  815. }
  816. Redis::sAdd($cacheKey, $couponId);
  817. if ($couponDetail->coupon_category == 'PIECE' || $couponDetail->coupon_category == 'PROMOTION' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  818. $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
  819. $payAmount = $payAmount - $zhekouAmount;
  820. }
  821. } elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
  822. if (Redis::sIsMember($cacheKey, $couponId)) {
  823. continue;
  824. }
  825. Redis::sAdd($cacheKey, $couponId);
  826. if ($good['nbr'] > 1) {
  827. $diyongAmount = $good['goods_sales_price'];
  828. $payAmount = $payAmount - $diyongAmount;
  829. } elseif (ceil($good['nbr']) == 1) {
  830. $diyongAmount = round($good['goods_sales_price'] * $good['nbr'], 2);
  831. $payAmount = $payAmount - $diyongAmount;
  832. }
  833. } elseif ($couponDetail->coupon_classify == '福利券') {
  834. if (Redis::sIsMember($cacheKey, $couponId)) {
  835. continue;
  836. }
  837. Redis::sAdd($cacheKey, $couponId);
  838. $fuliAmount = 0;
  839. if (!empty($couponDetail->coupon_value)) {
  840. $fuliAmount = $couponDetail->coupon_value;
  841. }
  842. if ($fuliAmount >= $countAndAmount['amount']) {
  843. $preferentialAmount = $countAndAmount['amount'];
  844. } else {
  845. $preferentialAmount = $fuliAmount;
  846. }
  847. $payAmount = $payAmount - $preferentialAmount;
  848. } elseif (in_array($couponDetail->coupon_classify, ['年卡', '季卡', '月卡'])) {
  849. if (Redis::sIsMember($cacheKey, $couponId) || Redis::sIsMember($cacheKey, $goodsId)) {
  850. continue;
  851. }
  852. Redis::sAdd($cacheKey, $couponId);
  853. Redis::sAdd($cacheKey, $goodsId);
  854. if ($good['nbr'] > 1) {
  855. $payAmount = $payAmount - $good['goods_sales_price'];
  856. } else {
  857. $payAmount = $payAmount - $good['goods_sales_price'] * $good['nbr'];
  858. }
  859. }
  860. }
  861. if ($payAmount < 0) {
  862. $payAmount = 0;
  863. }
  864. $amountBalance = [
  865. 'pay_amount' => round($payAmount, 2),
  866. 'welfare_balance' => $welfareBalance,
  867. 'cut_balance' => $cutBalance
  868. ];
  869. return $amountBalance;
  870. } catch (\Exception $e) {
  871. Redis::del("ORDER:USE:COUPON:" . $memberId);
  872. Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
  873. Log::error('优惠券使用失败', ['msg' => $e->getMessage()]);
  874. }
  875. }
  876. public static function countAndAmount($goods, $couponId)
  877. {
  878. try {
  879. $goodsIds = array_column($goods, 'goods_id');
  880. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)
  881. ->where('join_goods_coupon_id', $couponId)
  882. // ->select('join_coupon_goods_id', 'join_coupon_goods_sku_id')
  883. ->get()
  884. ->toArray();
  885. $count = 0;
  886. $amount = 0;
  887. $goodsIds = [];
  888. foreach ($couponGoods as $couponGood) {
  889. foreach ($goods as $good) {
  890. // if ($good['goods_id'] == $couponGood['join_coupon_goods_id'] && $good['sku_id'] == $couponGood['join_coupon_goods_sku_id']) {
  891. if ($good['goods_id'] == $couponGood['join_coupon_goods_id']) {
  892. $count += $good['nbr'];
  893. $amount += $good['goods_sales_price'] * $good['nbr'];
  894. $goodsIds[] = $good['goods_id'];
  895. }
  896. }
  897. }
  898. return compact('count', 'amount', 'goodsIds');
  899. } catch (\Exception $e) {
  900. dump($e->getTrace());
  901. }
  902. }
  903. /**
  904. * @Desc
  905. * @Author Gorden
  906. * @Date 2024/9/11 11:11
  907. *
  908. * @param MemberBenefit $benefit
  909. * @return false[]|true[]
  910. */
  911. public static function checkPackageBenefit(MemberBenefit $benefit)
  912. {
  913. $result = ['sheet' => true, 'order' => true];
  914. // 除此权益外,套包的其他权益是否用完了 where('join_benefit_package_id', $benefit->join_benefit_package_id)
  915. $benefits = MemberBenefit::where('join_benefit_order_id', $benefit->join_benefit_order_id)
  916. ->where('member_benefit_id', '<>', $benefit->member_benefit_id)
  917. ->get()
  918. ->toArray();
  919. foreach ($benefits as $benefitItem) {
  920. if ($benefitItem['join_benefit_package_id'] == $benefit->join_benefit_package_id) {
  921. if ($benefitItem['member_benefit_limit_count'] > $benefitItem['member_benefit_used_count']) {
  922. return ['sheet' => false, 'order' => false];
  923. }
  924. }
  925. }
  926. foreach ($benefits as $benefitItem) {
  927. if ($benefitItem['member_benefit_limit_count'] > $benefitItem['member_benefit_used_count']) {
  928. $result['order'] = false;
  929. return $result;
  930. }
  931. }
  932. return $result;
  933. }
  934. /**
  935. * @Desc 拆单提成
  936. * @Author Gorden
  937. * @Date 2024/9/29 16:27
  938. *
  939. * @return void
  940. */
  941. public static function splitOrderCommission($params)
  942. {
  943. $orders = Order::where('order_groupby', $params['orderGroupId'])->get()->toArray();
  944. if (!empty($orders)) {
  945. foreach ($orders as $orderItem) {
  946. $params['member_id'] = $orderItem['join_order_member_id'];
  947. $params['orderId'] = $orderItem['order_id'];
  948. Event::dispatch('commission.order', $params);
  949. }
  950. }
  951. }
  952. public static function splitOrderStatisticsInOut($params)
  953. {
  954. $orders = Order::where('order_groupby', $params['orderGroupId'])->get()->toArray();
  955. if (!empty($orders)) {
  956. foreach ($orders as $orderItem) {
  957. $params['member_id'] = $orderItem['join_order_member_id'];
  958. $params['orderId'] = $orderItem['order_id'];
  959. $params['inout_category'] = '标准订单';
  960. Event::dispatch('statistics.inout.in', $params);
  961. }
  962. }
  963. }
  964. /**
  965. * 检查产品限额
  966. */
  967. public static function checkGoodsLimit($goods, $memberId)
  968. {
  969. foreach ($goods as $good) {
  970. $goodsLimit = 'no';
  971. $goodsModel = Goods::where('goods_id', $good['goods_id'])->select('goods_attribute_json')->first();
  972. if (!empty($goodsModel) && !empty($goodsModel->goods_attribute_json)) {
  973. $goodsAttributeJson = json_decode($goodsModel->goods_attribute_json, true);
  974. if (!empty($goodsAttributeJson['max-limit'])) {
  975. $goodsLimit = $goodsAttributeJson['max-limit'];
  976. if ($goodsLimit != 0 && $goodsLimit != '' && $good['nbr'] > $goodsLimit) {
  977. return false;
  978. }
  979. }
  980. }
  981. $sheetNbr = 0;
  982. if ($goodsLimit != 'no') {
  983. $sheetNbr = OrderSheet::where('join_sheet_member_id', $memberId)
  984. ->where('join_sheet_goods_id', $good['goods_id'])
  985. ->where('order_sheet_status', '<>', 'CANCEL')
  986. ->sum('order_sheet_num');
  987. if ($goodsLimit != 0 && $goodsLimit != '' && $good['nbr'] + $sheetNbr > $goodsLimit) {
  988. return false;
  989. }
  990. }
  991. }
  992. return true;
  993. }
  994. public static $couponClassify = [
  995. 'wipe' => '抹零',
  996. 'custom' => '自定义优惠金额'
  997. ];
  998. public static $payWay = [
  999. 'WXPAY' => '微信支付',
  1000. 'ALIPAY' => '支付宝',
  1001. 'CASH' => '账户余额',
  1002. 'CARD' => '储值卡',
  1003. 'WELFARE' => '福利账户',
  1004. 'MONEY' => '现金',
  1005. 'OFFLINE' => '线下支付',
  1006. 'OFFLINE_WXPAY' => '微信支付(线下)',
  1007. 'OFFLINE_ALIPAY' => '支付宝(线下)',
  1008. 'QRCODE' => '付款码',
  1009. 'NONE' => '付零',
  1010. 'VIP' => 'VIP账户'
  1011. ];
  1012. public static $orderClassify = [
  1013. 'GOODS' => '产品订单',
  1014. 'CHNMED' => '服务订单',
  1015. 'CHNNCD' => '服务订单',
  1016. 'SERVICE' => '服务订单',
  1017. 'DEVICE' => '设备订单',
  1018. 'SPECIAL' => '专题业务订单',
  1019. 'PACKAGE' => '套包订单',
  1020. 'RECHARGE' => '充值订单',
  1021. 'DESHES' => '餐饮订单',
  1022. 'MEALS' => '餐饮订单',
  1023. 'VIP' => '康养城订单',
  1024. 'PARTNER' => '合伙人订单',
  1025. 'REFERRER' => '康养推荐官订单',
  1026. 'COMBINE' => '新客专享订单'
  1027. ];
  1028. /**
  1029. * @Desc 更新订单 sheet
  1030. * @Author Gorden
  1031. * @Date 2024/10/21 11:18
  1032. *
  1033. * @param Order $order
  1034. * @param $status
  1035. * @return void
  1036. */
  1037. public static function updateSheet(Order $order, $status)
  1038. {
  1039. $sheets = OrderSheet::where('join_sheet_order_id', $order->order_id)->get()->toArray();
  1040. foreach ($sheets as $sheet) {
  1041. $sheetPricePay = round(($order->order_amount_pay / $order->order_amount_total) * $sheet['order_sheet_price'], true);
  1042. OrderSheet::where('order_sheet_id', $sheet['order_sheet_id'])->update([
  1043. 'order_sheet_status' => $status,
  1044. 'order_sheet_price_pay' => $sheetPricePay,
  1045. 'order_sheet_pay' => $sheetPricePay * $sheet['order_sheet_num']
  1046. ]);
  1047. }
  1048. }
  1049. }