OrderService.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 = CouponDetail::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. if ($settlementNow == 'Y' && $type == 'pay') {
  557. $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
  558. } else {
  559. $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  560. }
  561. $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
  562. ->first();
  563. if (!$couponDetail) {
  564. continue;
  565. }
  566. if ($settlementNow == 'Y') {
  567. $updateData = [
  568. 'coupon_detail_status' => 'USED',
  569. 'coupon_detail_used_datetime' => date('Y-m-d H:i:s'),
  570. ];
  571. } else {
  572. $updateData = [
  573. 'coupon_detail_status' => 'WAITING',
  574. ];
  575. }
  576. // 计算优惠券包含的优惠商品的件数和总价
  577. $countAndAmount = self::countAndAmount($goods, $couponId);
  578. // 如果是计件
  579. if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] < $couponDetail->coupon_minimum_limit) {
  580. continue;
  581. }
  582. if (in_array($couponDetail->coupon_classify, ['立减券', '满减券'])) {
  583. if (Redis::sIsMember($cacheKey, $couponId)) {
  584. continue;
  585. }
  586. Redis::sAdd($cacheKey, $couponId);
  587. if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  588. $payAmount = $payAmount - $couponDetail->coupon_value;
  589. // json记录
  590. $discountData[$couponId] = [
  591. 'coupon_id' => $couponId,
  592. 'coupon_value' => $couponDetail->coupon_value,
  593. 'coupon_classify' => $couponDetail->coupon_classify,
  594. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  595. ];
  596. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  597. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  598. }
  599. } elseif ($couponDetail->coupon_classify == '折扣券') {
  600. if (Redis::sIsMember($cacheKey, $couponId)) {
  601. continue;
  602. }
  603. Redis::sAdd($cacheKey, $couponId);
  604. if ($couponDetail->coupon_category == 'PIECE' || $couponDetail->coupon_category == 'PROMOTION' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  605. $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
  606. $payAmount = $payAmount - $zhekouAmount;
  607. // json记录
  608. $discountData[$couponId] = [
  609. 'coupon_id' => $couponId,
  610. 'coupon_value' => $zhekouAmount,
  611. 'coupon_classify' => $couponDetail->coupon_classify,
  612. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  613. ];
  614. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  615. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  616. }
  617. } elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
  618. if (Redis::sIsMember($cacheKey, $couponId)) {
  619. continue;
  620. }
  621. Redis::sAdd($cacheKey, $couponId);
  622. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  623. if ($good['nbr'] > 1) {
  624. $diyongAmount = $good['goods_sales_price'];
  625. $payAmount = $payAmount - $diyongAmount;
  626. } elseif (ceil($good['nbr']) == 1) {
  627. $diyongAmount = round($good['goods_sales_price'] * $good['nbr'], 2);
  628. $payAmount = $payAmount - $diyongAmount;
  629. }
  630. // json记录
  631. $discountData[$couponId] = [
  632. 'coupon_id' => $couponId,
  633. 'coupon_value' => $diyongAmount,
  634. 'coupon_classify' => $couponDetail->coupon_classify,
  635. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  636. ];
  637. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  638. } elseif ($couponDetail->coupon_classify == '福利券') {
  639. if (Redis::sIsMember($cacheKey, $couponId)) {
  640. continue;
  641. }
  642. Redis::sAdd($cacheKey, $couponId);
  643. CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
  644. $fuliAmount = 0;
  645. if (!empty($couponDetail->coupon_value)) {
  646. $fuliAmount = $couponDetail->coupon_value;
  647. }
  648. if ($fuliAmount >= $countAndAmount['amount']) {
  649. $preferentialAmount = $countAndAmount['amount'];
  650. } else {
  651. $preferentialAmount = $fuliAmount;
  652. }
  653. $payAmount = $payAmount - $preferentialAmount;
  654. // json记录
  655. $discountData[$couponId] = [
  656. 'coupon_id' => $couponId,
  657. 'coupon_value' => $preferentialAmount,
  658. 'coupon_classify' => $couponDetail->coupon_classify,
  659. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  660. ];
  661. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  662. } elseif (in_array($couponDetail->coupon_classify, ['年卡', '季卡', '月卡'])) {
  663. if (Redis::sIsMember($cacheKey, $couponId) || Redis::sIsMember($cacheKey, $goodsId)) {
  664. continue;
  665. }
  666. Redis::sAdd($cacheKey, $couponId);
  667. Redis::sAdd($cacheKey, $goodsId);
  668. $kaAmount = 0;
  669. if (!empty($discountData[$couponId]['coupon_value'])) {
  670. $kaAmount = $discountData[$couponId]['coupon_value'];
  671. }
  672. if ($good['nbr'] > 1) {
  673. $payAmount = $payAmount - $good['goods_sales_price'];
  674. $kaAmount = $good['goods_sales_price'];
  675. } else {
  676. $payAmount = $payAmount - $good['goods_sales_price'] * $good['nbr'];
  677. $kaAmount = $good['goods_sales_price'] * $good['nbr'];
  678. }
  679. // json记录
  680. $discountData[$couponId] = [
  681. 'coupon_id' => $couponId,
  682. 'coupon_value' => $kaAmount,
  683. 'coupon_classify' => $couponDetail->coupon_classify,
  684. 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
  685. ];
  686. Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
  687. }
  688. }
  689. if ($payAmount < 0) {
  690. $payAmount = 0;
  691. }
  692. $amountBalance = [
  693. 'pay_amount' => round($payAmount, 2),
  694. 'welfare_balance' => 0,
  695. 'cut_balance' => 0
  696. ];
  697. return $amountBalance;
  698. } catch (\Exception $e) {
  699. Redis::del("ORDER:USE:COUPON:" . $memberId);
  700. Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
  701. dump($e->getTrace());
  702. }
  703. }
  704. /**
  705. * @Desc 选择优惠券,计算
  706. * @Author Gorden
  707. * @Date 2024/8/28 14:18
  708. *
  709. * @param $memberId
  710. * @param $goods
  711. * @param $good
  712. * @param $coupon
  713. * @param $amountBalance
  714. * @return array|void
  715. */
  716. public static function chooseCoupon($settlementNow, $memberId, $goods, $good, $coupon, $amountBalance)
  717. {
  718. try {
  719. $cacheKey = "ORDER:USE:COUPON:" . $memberId;
  720. $payAmount = $amountBalance['pay_amount'];
  721. $welfareBalance = $amountBalance['welfare_balance'];
  722. $cutBalance = $amountBalance['cut_balance'];
  723. $goodsId = $good['goods_id'];
  724. $money = $good['goods_sales_price'] * $good['nbr'];
  725. foreach ($coupon as $couponId) {
  726. $couponDetail = CouponDetail::leftJoin('coupon_goods', 'coupon_goods.join_goods_coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  727. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  728. ->select('coupon_detail.coupon_detail_id', 'coupon_goods.coupon_goods_id', 'coupon_id', 'coupon_classify', 'coupon_value', 'coupon_minimum_limit', 'coupon_category')
  729. ->where('coupon_goods.join_coupon_goods_id', $goodsId)
  730. // ->where('coupon_goods.join_coupon_goods_sku_id', $good['sku_id'])
  731. ->where('join_goods_coupon_id', $couponId)
  732. ->where('coupon_detail.join_coupon_detail_member_id', $memberId)
  733. ->where('coupon_detail.coupon_detail_gain_datetime', '<=', date('Y-m-d H:i:s'))
  734. ->where('coupon_detail.coupon_detail_deadline_datetime', '>=', date('Y-m-d H:i:s'));
  735. if ($settlementNow == 'Y') {
  736. $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
  737. } else {
  738. $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  739. }
  740. $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
  741. ->first();
  742. if (!$couponDetail) {
  743. continue;
  744. }
  745. // 计算优惠券包含的优惠商品的件数和总价
  746. $countAndAmount = self::countAndAmount($goods, $couponId);
  747. // 如果是计件
  748. if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] < $couponDetail->coupon_minimum_limit) {
  749. continue;
  750. }
  751. if (in_array($couponDetail->coupon_classify, ['立减券','满减券'])) {
  752. if (Redis::sIsMember($cacheKey, $couponId)) {
  753. continue;
  754. }
  755. Redis::sAdd($cacheKey, $couponId);
  756. if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  757. $payAmount = $payAmount - $couponDetail->coupon_value;
  758. }
  759. }
  760. // elseif ($couponDetail->coupon_classify == '满减券') {
  761. // if (Redis::sIsMember($cacheKey, $couponId)) {
  762. // continue;
  763. // }
  764. // Redis::sAdd($cacheKey, $couponId);
  765. // // 计件
  766. // if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] >= $couponDetail->coupon_minimum_limit) {
  767. // $goodsIds = array_splice($countAndAmount['goodsIds'], 0, $couponDetail->coupon_value);
  768. // $amount = 0;
  769. // $goodsNbr = 0;
  770. // foreach ($goods as $countAndAmountGood) {
  771. // if (in_array($countAndAmountGood['goods_id'], $goodsIds) && $goodsNbr < $couponDetail->coupon_value) {
  772. // if ($goodsNbr + $countAndAmountGood['nbr'] <= $couponDetail->coupon_value) {
  773. // $amount = $amount + round($countAndAmountGood['goods_sales_price'] * $countAndAmountGood['nbr'], 2);
  774. // $goodsNbr += $countAndAmountGood['nbr'];
  775. // } elseif ($goodsNbr + $countAndAmountGood['nbr'] > $couponDetail->coupon_value) {
  776. // $amount = $amount + round($countAndAmountGood['goods_sales_price'] * ($couponDetail->coupon_value - $goodsNbr), 2);
  777. // $goodsNbr = $couponDetail->coupon_value - $goodsNbr;
  778. // }
  779. // }
  780. // }
  781. // $payAmount = $payAmount - $amount;
  782. // }else if ($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. if ($couponDetail->coupon_category == 'PIECE' || $couponDetail->coupon_category == 'PROMOTION' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
  792. $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
  793. $payAmount = $payAmount - $zhekouAmount;
  794. }
  795. } elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
  796. if (Redis::sIsMember($cacheKey, $couponId)) {
  797. continue;
  798. }
  799. Redis::sAdd($cacheKey, $couponId);
  800. if ($good['nbr'] > 1) {
  801. $diyongAmount = $good['goods_sales_price'];
  802. $payAmount = $payAmount - $diyongAmount;
  803. } elseif (ceil($good['nbr']) == 1) {
  804. $diyongAmount = round($good['goods_sales_price'] * $good['nbr'], 2);
  805. $payAmount = $payAmount - $diyongAmount;
  806. }
  807. } elseif ($couponDetail->coupon_classify == '福利券') {
  808. if (Redis::sIsMember($cacheKey, $couponId)) {
  809. continue;
  810. }
  811. Redis::sAdd($cacheKey, $couponId);
  812. $fuliAmount = 0;
  813. if (!empty($couponDetail->coupon_value)) {
  814. $fuliAmount = $couponDetail->coupon_value;
  815. }
  816. if ($fuliAmount >= $countAndAmount['amount']) {
  817. $preferentialAmount = $countAndAmount['amount'];
  818. } else {
  819. $preferentialAmount = $fuliAmount;
  820. }
  821. $payAmount = $payAmount - $preferentialAmount;
  822. } elseif (in_array($couponDetail->coupon_classify, ['年卡', '季卡', '月卡'])) {
  823. if (Redis::sIsMember($cacheKey, $couponId) || Redis::sIsMember($cacheKey, $goodsId)) {
  824. continue;
  825. }
  826. Redis::sAdd($cacheKey, $couponId);
  827. Redis::sAdd($cacheKey, $goodsId);
  828. if ($good['nbr'] > 1) {
  829. $payAmount = $payAmount - $good['goods_sales_price'];
  830. } else {
  831. $payAmount = $payAmount - $good['goods_sales_price'] * $good['nbr'];
  832. }
  833. }
  834. }
  835. if ($payAmount < 0) {
  836. $payAmount = 0;
  837. }
  838. $amountBalance = [
  839. 'pay_amount' => round($payAmount, 2),
  840. 'welfare_balance' => $welfareBalance,
  841. 'cut_balance' => $cutBalance
  842. ];
  843. return $amountBalance;
  844. } catch (\Exception $e) {
  845. Redis::del("ORDER:USE:COUPON:" . $memberId);
  846. Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
  847. dump($e->getTrace());
  848. }
  849. }
  850. public static function countAndAmount($goods, $couponId)
  851. {
  852. try {
  853. $goodsIds = array_column($goods, 'goods_id');
  854. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)
  855. ->where('join_goods_coupon_id', $couponId)
  856. // ->select('join_coupon_goods_id', 'join_coupon_goods_sku_id')
  857. ->get()
  858. ->toArray();
  859. $count = 0;
  860. $amount = 0;
  861. $goodsIds = [];
  862. foreach ($couponGoods as $couponGood) {
  863. foreach ($goods as $good) {
  864. // if ($good['goods_id'] == $couponGood['join_coupon_goods_id'] && $good['sku_id'] == $couponGood['join_coupon_goods_sku_id']) {
  865. if ($good['goods_id'] == $couponGood['join_coupon_goods_id']) {
  866. $count += $good['nbr'];
  867. $amount += $good['goods_sales_price'] * $good['nbr'];
  868. $goodsIds[] = $good['goods_id'];
  869. }
  870. }
  871. }
  872. return compact('count', 'amount', 'goodsIds');
  873. } catch (\Exception $e) {
  874. dump($e->getTrace());
  875. }
  876. }
  877. /**
  878. * @Desc
  879. * @Author Gorden
  880. * @Date 2024/9/11 11:11
  881. *
  882. * @param MemberBenefit $benefit
  883. * @return false[]|true[]
  884. */
  885. public static function checkPackageBenefit(MemberBenefit $benefit)
  886. {
  887. $result = ['sheet' => true, 'order' => true];
  888. // 除此权益外,套包的其他权益是否用完了 where('join_benefit_package_id', $benefit->join_benefit_package_id)
  889. $benefits = MemberBenefit::where('join_benefit_order_id', $benefit->join_benefit_order_id)
  890. ->where('member_benefit_id', '<>', $benefit->member_benefit_id)
  891. ->get()
  892. ->toArray();
  893. foreach ($benefits as $benefitItem) {
  894. if ($benefitItem['join_benefit_package_id'] == $benefit->join_benefit_package_id) {
  895. if ($benefitItem['member_benefit_limit_count'] > $benefitItem['member_benefit_used_count']) {
  896. return ['sheet' => false, 'order' => false];
  897. }
  898. }
  899. }
  900. foreach ($benefits as $benefitItem) {
  901. if ($benefitItem['member_benefit_limit_count'] > $benefitItem['member_benefit_used_count']) {
  902. $result['order'] = false;
  903. return $result;
  904. }
  905. }
  906. return $result;
  907. }
  908. /**
  909. * @Desc 拆单提成
  910. * @Author Gorden
  911. * @Date 2024/9/29 16:27
  912. *
  913. * @return void
  914. */
  915. public static function splitOrderCommission($params)
  916. {
  917. $orders = Order::where('order_groupby', $params['orderGroupId'])->get()->toArray();
  918. if (!empty($orders)) {
  919. foreach ($orders as $orderItem) {
  920. $params['member_id'] = $orderItem['join_order_member_id'];
  921. $params['orderId'] = $orderItem['order_id'];
  922. Event::dispatch('commission.order', $params);
  923. }
  924. }
  925. }
  926. public static function splitOrderStatisticsInOut($params)
  927. {
  928. $orders = Order::where('order_groupby', $params['orderGroupId'])->get()->toArray();
  929. if (!empty($orders)) {
  930. foreach ($orders as $orderItem) {
  931. $params['member_id'] = $orderItem['join_order_member_id'];
  932. $params['orderId'] = $orderItem['order_id'];
  933. $params['inout_category'] = '标准订单';
  934. Event::dispatch('statistics.inout.in', $params);
  935. }
  936. }
  937. }
  938. /**
  939. * 检查产品限额
  940. */
  941. public static function checkGoodsLimit($goods,$memberId)
  942. {
  943. foreach($goods as $good){
  944. $goodsLimit = 'no';
  945. $goodsModel = Goods::where('goods_id',$good['goods_id'])->select('goods_attribute_json')->first();
  946. if (!empty($goodsModel) && !empty($goodsModel->goods_attribute_json)){
  947. $goodsAttributeJson = json_decode($goodsModel->goods_attribute_json,true);
  948. if (!empty($goodsAttributeJson['max-limit'])){
  949. $goodsLimit = $goodsAttributeJson['max-limit'];
  950. if ($goodsLimit != 0 && $goodsLimit != '' && $good['nbr'] > $goodsLimit){
  951. return false;
  952. }
  953. }
  954. }
  955. $sheetNbr = 0;
  956. if ($goodsLimit != 'no'){
  957. $sheetNbr = OrderSheet::where('join_sheet_member_id',$memberId)
  958. ->where('join_sheet_goods_id',$good['goods_id'])
  959. ->where('order_sheet_status','<>','CANCEL')
  960. ->sum('order_sheet_num');
  961. if ($goodsLimit != 0 && $goodsLimit != '' && $good['nbr'] + $sheetNbr > $goodsLimit){
  962. return false;
  963. }
  964. }
  965. }
  966. return true;
  967. }
  968. public static $couponClassify = [
  969. 'wipe' => '抹零',
  970. 'custom' => '自定义优惠金额'
  971. ];
  972. public static $payWay = [
  973. 'WXPAY' => '微信支付',
  974. 'ALIPAY' => '支付宝',
  975. 'CASH' => '账户余额',
  976. 'CARD' => '储值卡',
  977. 'WELFARE' => '福利账户',
  978. 'MONEY' => '现金',
  979. 'OFFLINE' => '线下支付',
  980. 'OFFLINE_WXPAY' => '微信支付(线下)',
  981. 'OFFLINE_ALIPAY' => '支付宝(线下)',
  982. 'QRCODE' => '付款码',
  983. 'NONE' => '付零',
  984. 'VIP' => 'VIP账户'
  985. ];
  986. public static $orderClassify = [
  987. 'GOODS' => '产品订单',
  988. 'CHNMED' => '服务订单',
  989. 'CHNNCD' => '服务订单',
  990. 'SERVICE' => '服务订单',
  991. 'DEVICE' => '设备订单',
  992. 'SPECIAL' => '专题业务订单',
  993. 'PACKAGE' => '套包订单',
  994. 'RECHARGE' => '充值订单',
  995. 'DESHES' => '餐饮订单',
  996. 'VIP' => '康养城订单',
  997. 'PARTNER' => '合伙人订单',
  998. 'REFERRER' => '康养推荐官订单',
  999. 'COMBINE' => '新客专享订单'
  1000. ];
  1001. /**
  1002. * @Desc 更新订单 sheet
  1003. * @Author Gorden
  1004. * @Date 2024/10/21 11:18
  1005. *
  1006. * @param Order $order
  1007. * @param $status
  1008. * @return void
  1009. */
  1010. public static function updateSheet(Order $order, $status)
  1011. {
  1012. $sheets = OrderSheet::where('join_sheet_order_id', $order->order_id)->get()->toArray();
  1013. foreach ($sheets as $sheet) {
  1014. $sheetPricePay = round(($order->order_amount_pay / $order->order_amount_total) * $sheet['order_sheet_price'], true);
  1015. OrderSheet::where('order_sheet_id', $sheet['order_sheet_id'])->update([
  1016. 'order_sheet_status' => $status,
  1017. 'order_sheet_price_pay' => $sheetPricePay,
  1018. 'order_sheet_pay' => $sheetPricePay * $sheet['order_sheet_num']
  1019. ]);
  1020. }
  1021. }
  1022. }