CardController.php 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\service\coupon\CardService;
  4. use app\admin\service\coupon\CouponDetailService;
  5. use app\admin\service\coupon\CouponService;
  6. use app\admin\service\member\MemberService;
  7. use app\admin\service\order\OrderService;
  8. use app\admin\service\order\OrderSheetService;
  9. use app\admin\service\order\PayDetailService;
  10. use app\admin\validate\order\OrderValidate;
  11. use app\controller\Curd;
  12. use app\model\Appointment;
  13. use app\model\Card;
  14. use app\model\ClientConfig;
  15. use app\model\Coupon;
  16. use app\model\CouponDetail;
  17. use app\model\Goods;
  18. use app\model\GoodsComponent;
  19. use app\model\GoodsRunning;
  20. use app\model\GoodsSku;
  21. use app\model\Member;
  22. use app\model\MemberAccount;
  23. use app\model\MemberBenefit;
  24. use app\model\MemberRole;
  25. use app\model\Order;
  26. use app\model\OrderExpress;
  27. use app\model\OrderReturn;
  28. use app\model\OrderSheet;
  29. use app\model\PayDetail;
  30. use app\model\Supplier;
  31. use app\model\SysDept;
  32. use app\model\SysUser;
  33. use support\Db;
  34. use support\exception\BusinessException;
  35. use support\Log;
  36. use support\Redis;
  37. use support\Request;
  38. use support\Response;
  39. use Tinywan\Jwt\JwtToken;
  40. use Webman\Event\Event;
  41. use Yansongda\Pay\Pay;
  42. class CardController extends Curd
  43. {
  44. public function __construct()
  45. {
  46. $this->model = new Order();
  47. $this->validate = true;
  48. $this->validateClass = new OrderValidate();
  49. }
  50. /**
  51. * @Desc 列表
  52. * @Author Gorden
  53. * @Date 2024/3/28 15:01
  54. *
  55. * @param Request $request
  56. * @return Response
  57. * @throws \support\exception\BusinessException
  58. */
  59. public function select(Request $request): Response
  60. {
  61. $timeType = $request->get('time_type', 'add');
  62. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  63. $where['order_classify'] = 'CARD';
  64. if (!empty($where['order_addtimes']) && $timeType == 'add') {
  65. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  66. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  67. }
  68. $order = $request->get('order', 'desc');
  69. $orderId = $request->get('orderId');
  70. $field = $field ?? 'order_addtimes';
  71. $orderIds = [];
  72. if (!empty($orderId)) {
  73. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  74. ->where('order_classify', 'CARD')
  75. ->pluck('order_id')
  76. ->toArray();
  77. }
  78. $goodsName = trim($request->get('goods_name', ''));
  79. if (!empty($goodsName)) {
  80. $goodsIds = Goods::where('goods_classify', 'CARD')
  81. ->where('goods_name', 'like', '%' . $goodsName . '%')
  82. ->pluck('goods_id')
  83. ->toArray();
  84. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  85. if (!empty($where['order_id'])) {
  86. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  87. } else {
  88. $orderIds = $goodsOrderIds;
  89. }
  90. }
  91. // 付款时间
  92. $payOrderIds = [];
  93. if ($timeType == 'pay' && !empty($where['order_addtimes'])) {
  94. $payTimeStart = strtotime($where['order_addtimes'][0]);
  95. $payTimeEnd = strtotime($where['order_addtimes'][1]);
  96. unset($where['order_addtimes']);
  97. $payOrderIds = PayDetailService::getPayOrderId($payTimeStart, $payTimeEnd, ['CARD']);
  98. }
  99. if ((!empty($orderId) || !empty($goodsName)) && $timeType == 'pay') {
  100. $orderIds = array_intersect($orderIds, $payOrderIds);
  101. $where['order_id'] = ['in', implode(',', $orderIds)];
  102. } elseif (!empty($orderId) || !empty($goodsName)) {
  103. $where['order_id'] = ['in', implode(',', $orderIds)];
  104. } elseif ($timeType == 'pay') {
  105. $where['order_id'] = ['in', implode(',', $payOrderIds)];
  106. }
  107. $query = $this->doSelect($where, $field, $order);
  108. return $this->doFormat($query, $format, $limit);
  109. }
  110. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  111. {
  112. $model = $this->model->with([
  113. 'sheets' => function ($query) {
  114. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  115. },
  116. 'member' => function ($query) {
  117. $query->select('member_id', 'member_mobile', 'join_invite_member_id');
  118. },
  119. 'cert' => function ($query) {
  120. $query->select('join_cert_member_id', 'member_cert_name');
  121. },
  122. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
  123. ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
  124. foreach ($where as $column => $value) {
  125. if (is_array($value)) {
  126. if ($value[0] === 'like' || $value[0] === 'not like') {
  127. $model = $model->where($column, $value[0], "%$value[1]%");
  128. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  129. $model = $model->where($column, $value[0], $value[1]);
  130. } elseif ($value[0] == 'in' && !empty($value[1])) {
  131. $valArr = $value[1];
  132. if (is_string($value[1])) {
  133. $valArr = explode(",", trim($value[1]));
  134. }
  135. $model = $model->whereIn($column, $valArr);
  136. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  137. $valArr = $value[1];
  138. if (is_string($value[1])) {
  139. $valArr = explode(",", trim($value[1]));
  140. }
  141. $model = $model->whereNotIn($column, $valArr);
  142. } elseif ($value[0] == 'null') {
  143. $model = $model->whereNull($column);
  144. } elseif ($value[0] == 'not null') {
  145. $model = $model->whereNotNull($column);
  146. } elseif ($value[0] !== '' || $value[1] !== '') {
  147. $model = $model->whereBetween($column, $value);
  148. }
  149. } else {
  150. $model = $model->where($column, $value);
  151. }
  152. }
  153. if ($field) {
  154. $model = $model->orderBy($field, $order);
  155. }
  156. $model = $model->select('order.*', 'order_express.join_express_order_id', 'order_express.order_express_type', 'order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status', 'order_return.order_return_apply_json', 'order_return.order_return_remark');
  157. return $model;
  158. }
  159. public function afterQuery($items)
  160. {
  161. foreach ($items as &$item) {
  162. $sheetDeng = '';
  163. $item['sheet'] = $item['sheets'][0] ?? [];
  164. if (!empty($item['sheet'])) {
  165. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  166. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  167. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  168. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  169. }
  170. unset($item['sheets']);
  171. if (isset($item['orders_return_id'])) {
  172. $item['return'] = [
  173. 'orders_return_id' => $item['orders_return_id'],
  174. 'join_return_order_id' => $item['join_return_order_id'],
  175. 'order_return_status' => $item['order_return_status'],
  176. 'order_return_apply_json' => $item['order_return_apply_json'],
  177. 'order_return_remark' => $item['order_return_remark']
  178. ];
  179. }
  180. if (isset($item['join_express_order_id'])) {
  181. $item['express'] = [
  182. 'join_express_order_id' => $item['join_express_order_id'],
  183. 'order_express_type' => $item['order_express_type']
  184. ];
  185. unset($item['join_express_order_id'], $item['order_express_type']);
  186. }
  187. if (!empty($item['member']['join_invite_member_id'])) {
  188. $inviteMember = Member::with([
  189. 'cert' => function ($query) {
  190. $query->select('join_cert_member_id', 'member_cert_name');
  191. }
  192. ])->where('member_id', $item['member']['join_invite_member_id'])
  193. ->first();
  194. $inviteMobile = $inviteMember['member_mobile'] ?? '';
  195. $inviteCertName = !empty($inviteMember['cert']) && !empty($inviteMember['cert']['member_cert_name']) ? $inviteMember['cert']['member_cert_name'] : '';
  196. $item['invite_name'] = MemberService::getMemberCertName($inviteMobile, $inviteCertName, '');
  197. }
  198. $item['payDetail'] = PayDetailService::getPayWay($item['order_groupby'], $item['order_id']);
  199. }
  200. return $items;
  201. }
  202. /**
  203. * @Desc 获取卡列表
  204. * @Author Gorden
  205. * @Date 2024/11/22 15:33
  206. *
  207. * @param Request $request
  208. * @return Response
  209. */
  210. public function getCardList(Request $request)
  211. {
  212. $orderId = $request->get('order_id');
  213. $deptId = $request->get('dept_id', '');
  214. $goodsId = OrderSheet::where('join_sheet_order_id', $orderId)->value('join_sheet_goods_id');
  215. $goods = Goods::where('goods_id', $goodsId)->select('goods_attribute_json')->first();
  216. $cards = [];
  217. if (!empty($goods->goods_attribute_json)) {
  218. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  219. if (!empty($goodsAttributeJson['card_main_id'])) {
  220. $cards = Card::where('join_card_main_id', $goodsAttributeJson['card_main_id'])
  221. ->where('card_status', 'WAITING')
  222. ->when(!empty($deptId), function ($query) use ($deptId) {
  223. $query->where('join_card_dept_id', $deptId);
  224. })
  225. ->where('is_issue', 'Y')
  226. ->select('card_id', 'join_card_dept_id')
  227. ->get()
  228. ->toArray();
  229. }
  230. }
  231. return json_success('success', $cards);
  232. }
  233. /**
  234. * @Desc 下单+支付
  235. * @Author Gorden
  236. * @Date 2024/9/23 16:15
  237. *
  238. * @param Request $request
  239. * @return Response
  240. */
  241. public function insert(Request $request): Response
  242. {
  243. $params = $request->post();
  244. // 判断餐品是否连带着服务或实体
  245. $goodsClassifys = array_unique(array_column($params['goodsContentList'], 'goods_classify'));
  246. $premises = [];
  247. if (!empty($params['dept_premises_id'])) {
  248. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  249. }
  250. try {
  251. // 下单账户
  252. if (empty($params['join_order_member_id']) && !empty($params['mobile'])) {
  253. if (Member::where('member_mobile', $params['mobile'])->exists()) {
  254. throw new BusinessException('会员已存在');
  255. }
  256. $params['join_order_member_id'] = $params['member_id'] = 'MR' . date('ymdHi') . random_string(4, 'up');
  257. // 创建会员
  258. MemberService::createMember($params);
  259. } else if (empty($params['join_order_member_id']) && empty($params['mobile'])) {
  260. $params['join_order_member_id'] = Member::where('member_mobile', '0000')->value('member_id');
  261. }
  262. if (empty($params['join_order_member_id'])) {
  263. throw new BusinessException('检查下单账户');
  264. }
  265. // 带着优惠券,要发券
  266. if (!empty($params['custom_member_id'])) {
  267. // 把券从000000加到真实账户
  268. CouponDetailService::transCoupon('000000', $params['join_order_member_id']);
  269. }
  270. } catch (BusinessException $e) {
  271. Log::error('创建订单失败', ['msg' => $e->getMessage()]);
  272. _syslog("订单", $e->getMessage());
  273. return json_fail($e->getMessage());
  274. } catch (\Exception $e) {
  275. Log::error('创建订单失败', ['msg' => $e->getMessage()]);
  276. _syslog("订单", "创建订单失败");
  277. return json_fail('创建订单失败');
  278. }
  279. $params['goods_classify'] = $goodsClassifys[0];
  280. Db::beginTransaction();
  281. try {
  282. // 使用优惠券
  283. $couponUseJson = [];
  284. if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  285. $couponResult = OrderService::payUseCoupon('insert', $params['settlement_now'], $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  286. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  287. throw new BusinessException("计算优惠后,实付金额错误!");
  288. }
  289. // 组装优惠券使用数据,存主表优惠里
  290. if (!empty($couponResult['use_coupon_json'])) {
  291. $couponUseJson = $couponResult['use_coupon_json'];
  292. }
  293. }
  294. // 存储优惠信息
  295. $params['order_discount_json'] = json_encode($this->discountRecord($couponUseJson, $params));
  296. // 验证库存
  297. foreach ($params['goodsContentList'] as $goods) {
  298. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  299. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  300. if ($goodsRunning->goods_running_storage < 0) {
  301. throw new BusinessException('库存不足');
  302. }
  303. }
  304. // 验证线下付款密码
  305. if ($params['settlement_now'] == 'Y' && $params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  306. $password = $params['offline_password'];
  307. if ($password != '666888') {
  308. throw new BusinessException('密码错误,请重新输入');
  309. }
  310. }
  311. $qrcodePayAmount = 0;
  312. $params['orderId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  313. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  314. $systemStatus = 'SENDING'; // 待发货
  315. if ($params['settlement_now'] == 'Y' && ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY')) {
  316. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  317. $params['pay_category'] = $params['pay_category_sub'];
  318. }
  319. $params['order_status_system'] = $systemStatus;
  320. $params['order_status_payment'] = 'SUCCESS';
  321. }
  322. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && $params['settlement_now'] == 'Y' && !empty($params['qrcode_nbr'])) { // 付款码
  323. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount <= 0) {
  324. $params['order_status_system'] = $systemStatus;
  325. $params['order_status_payment'] = 'SUCCESS';
  326. }
  327. // 不组合或者组合后需要付款码的金额>0
  328. if ($params['pay_constitute'] == 'N' || ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0)) {
  329. $result = OrderService::qrcodePay($params);
  330. $result = json_encode($result);
  331. $params['pay_json_response'] = $result;
  332. $result = json_decode($result, true);
  333. $prefix = substr($params['qrcode_nbr'], 0, 2);
  334. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  335. $params['pay_category'] = 'WXPAY';
  336. if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) {
  337. $params['order_status_system'] = 'PAYING';
  338. $params['order_status_payment'] = 'PENDING';
  339. $params['order_is_complete'] = 'N';
  340. } else {
  341. $params['order_status_system'] = $systemStatus;
  342. $params['order_status_payment'] = 'SUCCESS';
  343. }
  344. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  345. $params['pay_category'] = 'ALIPAY';
  346. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  347. $params['order_status_system'] = 'PAYING';
  348. $params['order_status_payment'] = 'PENDING';
  349. $params['order_is_complete'] = 'N';
  350. } else {
  351. $params['order_status_system'] = $systemStatus;
  352. $params['order_status_payment'] = 'SUCCESS';
  353. }
  354. } else {
  355. throw new BusinessException('付款码无效');
  356. }
  357. }
  358. }
  359. $orderConfigJson = [];
  360. // 优惠
  361. if (!empty($params['preferential'])) {
  362. $orderConfigJson['preferential'] = $params['preferential'];
  363. }
  364. // 配送方式
  365. if (isset($params['delivery']) && ($params['delivery'] == 'PICKUP' || $params['delivery'] == 'ARRIVAL')) { // 自提/ 到店
  366. $orderConfigJson['premises'] = $params['dept_premises_id'];
  367. }
  368. if (isset($params['delivery']) && in_array($params['delivery'], ['PICKUP', 'ARRIVAL']) && !empty($params['dept_premises_id'])) {
  369. $params['submit_premises_id'] = $premises->dept_id;
  370. }
  371. $params['order_config_json'] = json_encode($orderConfigJson);
  372. // 写入订单
  373. $this->insertMain($params);
  374. Db::commit();
  375. // 触发事件
  376. if (!empty($params['order_is_complete']) && $params['order_is_complete'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
  377. Event::dispatch('order.complete', $params);
  378. }
  379. // 触发事件
  380. if ($params['order_status_payment'] == 'SUCCESS') {
  381. // 上级提成
  382. OrderService::splitOrderCommission($params);
  383. // 入收支明细表
  384. $params['inout_category'] = '购买储值卡';
  385. Event::dispatch('statistics.inout.in', $params);
  386. }
  387. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
  388. _syslog("订单", "支付异常,检查是否有轮询");
  389. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  390. }
  391. _syslog("订单", "创建订单成功");
  392. return json_success('创建订单成功');
  393. } catch (BusinessException $e) {
  394. Db::rollBack();
  395. Log::error('创建订单失败', ['msg' => $e->getMessage()]);
  396. _syslog("订单", $e->getMessage());
  397. return json_fail($e->getMessage());
  398. } catch (\Exception $e) {
  399. Db::rollBack();
  400. Log::error('创建订单失败', ['msg' => $e->getMessage()]);
  401. _syslog("订单", "创建订单失败");
  402. return json_fail('创建订单失败');
  403. }
  404. }
  405. public function pay(Request $request)
  406. {
  407. $params = $request->post();
  408. // 余额、福利、储值卡 验证短信
  409. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['CASH', 'CARD', 'WELFARE'])) {
  410. $code = $params['sms_code'];
  411. if (!$code) {
  412. return json_fail("验证码错误,请重新输入");
  413. }
  414. $member = Member::find($params['join_order_member_id']);
  415. $mobile = $member->member_mobile;
  416. $key = "SMS:CODE:ORDER_PAY:" . $mobile;
  417. $redisCode = Redis::get($key);
  418. if ($redisCode != $code && $code != '123654879') {
  419. return json_fail("验证码错误,请重新输入");
  420. }
  421. Redis::del($key);
  422. }
  423. // 验证线下付款密码
  424. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  425. $password = $params['offline_password'];
  426. if ($password != '666888') {
  427. return json_fail("密码错误,请重新输入");
  428. }
  429. }
  430. $order = Order::where('order_id', $params['order_id'])->first();
  431. $oldOrderStatus = $order->order_status_system;
  432. if (!$order) {
  433. return json_fail('订单异常');
  434. }
  435. if ($order->order_status_system != 'PAYING') {
  436. return json_fail('订单不是可支付状态');
  437. }
  438. if (!empty($order->order_config_json)) {
  439. $orderConfigJson = json_decode($order->order_config_json, true);
  440. if (isset($orderConfigJson['premises'])) {
  441. $premises = SysDept::where('dept_name', $orderConfigJson['premises'])->first();
  442. if (!empty($premises)) {
  443. $params['submit_premises_id'] = $premises->dept_id;
  444. }
  445. }
  446. }
  447. $params['orderId'] = $params['order_id'];
  448. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  449. $order->order_groupby = $params['orderGroupId'];
  450. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
  451. ->select('goods_id', 'goods_name', 'goods_classify')
  452. ->first();
  453. if (!$goods) {
  454. return json_fail('产品数据异常');
  455. }
  456. $goods = $goods->toArray();
  457. $params['goods_classify'] = $goods['goods_classify'] ?? '';
  458. $systemStatus = 'SENDING'; // 待发货
  459. // 立即结算
  460. if (in_array($params['goods_classify'], ['MEALS', 'VIP'])) {
  461. $order->order_is_complete = 'Y';
  462. $systemStatus = 'DONE';
  463. }
  464. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $params['delivery'] == 'ARRIVAL') {
  465. $params['order_is_complete'] = 'N';
  466. $systemStatus = "WAITING";
  467. }
  468. Db::beginTransaction();
  469. try {
  470. // 使用优惠券
  471. $couponUseJson = [];
  472. $discountJson = [];
  473. if (!empty($order->order_discount_json)) {
  474. $discountJson = json_decode($order->order_discount_json, true);
  475. foreach ($discountJson as $item) {
  476. if (isset($item['coupon_value']) && sprintf('%.2f', (floatval($order->order_amount_total) - floatval($params['order_amount_pay']))) != sprintf('%.2f', $item['coupon_value'])) {
  477. throw new BusinessException("计算优惠后,实付金额错误!");
  478. }
  479. }
  480. $couponUseJson = $discountJson;
  481. $this->changeOrderCouponStatus($couponUseJson, 'USED');
  482. }
  483. if (empty($discountJson) && !empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  484. $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  485. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  486. throw new BusinessException("计算优惠后,实付金额错误!");
  487. }
  488. // 组装优惠券使用数据,存主表优惠里
  489. if (!empty($couponResult['use_coupon_json'])) {
  490. $couponUseJson = $couponResult['use_coupon_json'];
  491. }
  492. }
  493. // 存储优惠信息
  494. if (empty($discountJson)) {
  495. $order->order_discount_json = json_encode($this->discountRecord($couponUseJson, $params));
  496. }
  497. // 组合支付时,付款码应收金额
  498. $qrcodePayAmount = 0;
  499. if ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY') {
  500. $order->order_status_system = $systemStatus;
  501. $order->order_status_payment = 'SUCCESS';
  502. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  503. $params['pay_category'] = $params['pay_category_sub'];
  504. }
  505. } else if ($params['pay_category'] == 'CASH') { // 余额支付
  506. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  507. ->where('member_account_classify', 'CASH')
  508. ->where('member_account_status', 'ACTIVED')
  509. ->first();
  510. if (!$account) {
  511. throw new BusinessException('账户异常');
  512. }
  513. $amount = $account->member_account_surplus + $account->member_account_added;
  514. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  515. throw new BusinessException('账户余额不足');
  516. }
  517. if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  518. $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  519. $params['order_amount_pay'] = $amount;
  520. }
  521. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  522. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  523. $account->member_account_surplus = 0;
  524. $account->member_account_added = $cut;
  525. } else {
  526. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  527. }
  528. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  529. $account->member_account_update_user_id = JwtToken::getCurrentId();
  530. $account->member_account_updatetimes = time();
  531. $account->save();
  532. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] <= $amount)) {
  533. $order->order_status_system = $systemStatus;
  534. $order->order_status_payment = 'SUCCESS';
  535. }
  536. } else if ($params['pay_category'] == 'VIP') { // 余额支付
  537. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  538. ->where('member_account_classify', 'VIP')
  539. ->where('member_account_status', 'ACTIVED')
  540. ->first();
  541. if (!$account) {
  542. throw new BusinessException('账户异常');
  543. }
  544. $amount = $account->member_account_surplus + $account->member_account_added;
  545. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  546. throw new BusinessException('账户余额不足');
  547. }
  548. if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  549. $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  550. $params['order_amount_pay'] = $amount;
  551. }
  552. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  553. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  554. $account->member_account_surplus = 0;
  555. $account->member_account_added = $cut;
  556. } else {
  557. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  558. }
  559. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  560. $account->member_account_update_user_id = JwtToken::getCurrentId();
  561. $account->member_account_updatetimes = time();
  562. $account->save();
  563. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] <= $amount)) {
  564. $order->order_status_system = $systemStatus;
  565. $order->order_status_payment = 'SUCCESS';
  566. }
  567. } else if ($params['pay_category'] == 'WELFARE') { // 福利账户
  568. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  569. ->where('member_account_classify', 'WELFARE')
  570. ->where('member_account_status', 'ACTIVED')
  571. ->first();
  572. if (!$account) {
  573. throw new BusinessException('账户异常');
  574. }
  575. $account->member_account_surplus = floatval($account->member_account_surplus);
  576. if ($params['pay_constitute'] == 'N' && ($params['order_amount_pay'] > $account->member_account_surplus)) {
  577. throw new BusinessException('账户余额不足');
  578. }
  579. if ($params['pay_constitute'] == 'Y' && ($params['order_amount_pay'] > $account->member_account_surplus)) {
  580. $qrcodePayAmount = $params['order_amount_pay'] - $account->member_account_surplus;
  581. $params['order_amount_pay'] = $account->member_account_surplus;
  582. }
  583. if ($params['pay_constitute'] == 'N' && ($params['order_amount_pay'] <= $account->member_account_surplus)) {
  584. $order->order_status_system = $systemStatus;
  585. $order->order_status_payment = 'SUCCESS';
  586. }
  587. // 福利账户 300 、 700
  588. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS'])) {
  589. $payDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  590. ->where('pay_status', 'SUCCESS')
  591. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  592. ->whereIn('pay_category', ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DESHES'])
  593. ->get()
  594. ->toArray();
  595. $payDetailArray = array_column($payDetails, 'pay_amount', 'join_pay_order_id');
  596. $refundPayDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  597. ->where('pay_status', 'SUCCESS')
  598. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  599. ->where('pay_category', 'REFUND')
  600. ->get()
  601. ->toArray();
  602. $refundPayDetailArray = array_column($refundPayDetails, 'pay_amount', 'join_pay_order_id');
  603. $paySum = 0;
  604. foreach ($payDetailArray as $key => $item) {
  605. if (isset($refundPayDetailArray[$key])) {
  606. $paySum = $paySum + ($item - $refundPayDetailArray[$key]);
  607. continue;
  608. }
  609. $paySum = $paySum + $item;
  610. }
  611. if ($params['pay_constitute'] == 'N' && 700 - $paySum < $params['order_amount_pay']) {
  612. throw new BusinessException('超出福利限额');
  613. } else if ($params['pay_constitute'] == 'Y' && 700 - $paySum < $params['order_amount_pay']) {
  614. $qrcodePayAmount = $params['order_amount_pay'] - (700 - $paySum);
  615. $params['order_amount_pay'] = (700 - $paySum);
  616. }
  617. } else {
  618. $payDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  619. ->where('pay_status', 'SUCCESS')
  620. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  621. ->whereNotIn('pay_category', ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DESHES', 'REFUND', 'RECHARGE'])
  622. ->get()
  623. ->toArray();
  624. $payDetailArray = array_column($payDetails, 'pay_amount', 'join_pay_order_id');
  625. $refundPayDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  626. ->where('pay_status', 'SUCCESS')
  627. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  628. ->where('pay_category', 'REFUND')
  629. ->get()
  630. ->toArray();
  631. $refundPayDetailArray = array_column($refundPayDetails, 'pay_amount', 'join_pay_order_id');
  632. $paySum = 0;
  633. foreach ($payDetailArray as $key => $item) {
  634. if (isset($refundPayDetailArray[$key])) {
  635. $paySum = $paySum + ($item - $refundPayDetailArray[$key]);
  636. continue;
  637. }
  638. $paySum = $paySum + $item;
  639. }
  640. if ($params['pay_constitute'] == 'N' && 300 - $paySum < $params['order_amount_pay']) {
  641. throw new BusinessException('超出福利限额');
  642. } else if ($params['pay_constitute'] == 'Y' && 300 - $paySum < $params['order_amount_pay']) {
  643. $qrcodePayAmount = $params['order_amount_pay'] - (300 - $paySum);
  644. $params['order_amount_pay'] = (300 - $paySum);
  645. }
  646. }
  647. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  648. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  649. $account->member_account_update_user_id = JwtToken::getCurrentId();
  650. $account->member_account_updatetimes = time();
  651. $account->save();
  652. } else if ($params['pay_category'] == 'CARD') { // 储值卡账户
  653. $cardNbr = $params['card_id'];
  654. if (!$cardNbr) {
  655. throw new BusinessException('账户异常');
  656. }
  657. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  658. ->where('member_account_nbr', $cardNbr)
  659. ->where('member_account_status', 'ACTIVED')
  660. ->first();
  661. if (!$account) {
  662. throw new BusinessException('账户异常');
  663. }
  664. $amount = $account->member_account_surplus + $account->member_account_added;
  665. if ($params['order_amount_pay'] > $amount) {
  666. throw new BusinessException('账户余额不足');
  667. }
  668. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  669. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  670. $account->member_account_surplus = 0;
  671. $account->member_account_added = $cut;
  672. } else {
  673. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  674. }
  675. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  676. $account->member_account_update_user_id = JwtToken::getCurrentId();
  677. $account->member_account_updatetimes = time();
  678. $account->save();
  679. $order->order_status_system = $systemStatus;
  680. $order->order_status_payment = 'SUCCESS';
  681. }
  682. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && !empty($params['qrcode_nbr'])) { // 付款码
  683. // 提交过来的支付分类
  684. $submitPayCategory = $params['pay_category'];
  685. // 账户支付的金额
  686. $accountAmount = $params['order_amount_pay'];
  687. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
  688. // 组合支付,改成需要付款码需要支付的金额
  689. $params['order_amount_pay'] = $qrcodePayAmount;
  690. }
  691. // 去支付
  692. $result = OrderService::qrcodePay($params);
  693. $result = json_encode($result);
  694. $params['pay_json_response'] = $result;
  695. $result = json_decode($result, true);
  696. $prefix = substr($params['qrcode_nbr'], 0, 2);
  697. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  698. $params['pay_category'] = 'WXPAY';
  699. if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) {
  700. $order->order_status_system = $oldOrderStatus;
  701. $order->order_status_payment = 'PENDING';
  702. $order->order_is_complete = 'N';
  703. // Db::rollBack();
  704. // return json_fail('支付失败');
  705. } else {
  706. $order->order_status_system = $systemStatus;
  707. $order->order_status_payment = 'SUCCESS';
  708. }
  709. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  710. $params['pay_category'] = 'ALIPAY';
  711. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  712. $order->order_status_system = $oldOrderStatus;
  713. $order->order_status_payment = 'PENDING';
  714. $order->order_is_complete = 'N';
  715. // Db::rollBack();
  716. // return json_fail('支付失败');
  717. } else {
  718. $order->order_status_system = $systemStatus;
  719. $order->order_status_payment = 'SUCCESS';
  720. }
  721. } else {
  722. throw new BusinessException('付款码无效');
  723. }
  724. // 账户支付的金额
  725. $params['order_amount_pay'] = $accountAmount;
  726. }
  727. $orderConfigJson = [];
  728. if (!empty($order->order_config_json)) {
  729. $orderConfigJson = json_decode($order->order_config_json, true);
  730. }
  731. $orderConfigJson['preferential'] = $params['preferential'] ?? '';
  732. $order->order_config_json = json_encode($orderConfigJson);
  733. $order->order_amount_pay = $params['order_amount_pay'] + $qrcodePayAmount;
  734. // 发卡
  735. $cardId = CardService::sendDemandCard($params['join_order_member_id'], $goods, $params);
  736. if ($cardId !== false) {
  737. $order->order_is_complete = 'Y';
  738. $order->order_status_system = 'CONFIRM';
  739. // 更新express表
  740. $this->addCardIdToExpress($order->order_id, $cardId);
  741. }
  742. // 主订单
  743. $order->save();
  744. // sheet
  745. if ($order->order_status_payment == 'SUCCESS') {
  746. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update([
  747. 'order_sheet_status' => $systemStatus,
  748. ]);
  749. }
  750. // payDetail
  751. // 不组合支付,那就只有一条支付记录 先删,再加
  752. PayDetail::where('join_pay_order_id', $order->order_groupby)
  753. ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  754. ->delete();
  755. $payData = [
  756. 'pay_amount' => $params['order_amount_pay']
  757. ];
  758. if ($order->order_status_payment == 'SUCCESS') {
  759. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  760. $payData['pay_status'] = 'SUCCESS';
  761. }
  762. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  763. $payData['pay_prepayid'] = $params['pay_category'];
  764. $payData['pay_json_response'] = $params['pay_json_response'];
  765. } else if ($params['pay_category'] == 'CASH') {
  766. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-CASH';
  767. } else if ($params['pay_category'] == 'WELFARE') {
  768. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-WELFARE';
  769. } else if ($params['pay_category'] == 'VIP') {
  770. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-VIP';
  771. } else if ($params['pay_category'] == 'CARD') {
  772. $payData['pay_prepayid'] = $params['card_id'];
  773. } else if ($params['pay_category'] == 'OFFLINE') {
  774. $payData['pay_prepayid'] = 'OFFLINE';
  775. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  776. $payData['pay_prepayid'] = 'OFFLINE_ALIPAY';
  777. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  778. $payData['pay_prepayid'] = 'OFFLINE_WXPAY';
  779. } else if ($params['pay_category'] == 'MONEY') {
  780. $payData['pay_prepayid'] = 'MONEY';
  781. }
  782. $payData['join_pay_member_id'] = $params['join_order_member_id'];
  783. $payData['join_pay_order_id'] = $order->order_groupby;
  784. $payData['pay_status'] = !empty($payData['pay_status']) && $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING';
  785. $payData['pay_category'] = $params['goods_classify'] ?? '';
  786. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  787. $payData['pay_json_request'] = json_encode($params); // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"}
  788. $payData['pay_json_response'] = !empty($payData['pay_status']) && $payData['pay_status'] == 'SUCCESS' ? (!empty($params['pay_json_response']) ? $params['pay_json_response'] : json_encode(['pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s')])) : '[]';
  789. $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
  790. $payData['pay_addtimes'] = time();
  791. PayDetail::insert($payData);
  792. Db::commit();
  793. // 触发事件
  794. if ($order->order_is_complete == 'Y' && $order->order_status_payment == 'SUCCESS') {
  795. // 完成订单
  796. Event::dispatch('order.complete', $params);
  797. }
  798. if ($order->order_status_payment == 'SUCCESS') {
  799. // 加销售量
  800. OrderSheetService::addGoodsSales($params['orderId']);
  801. // 上级提成
  802. Event::dispatch('commission.order', $params);
  803. // 入收支明细表
  804. $params['inout_category'] = '标准订单';
  805. Event::dispatch('statistics.inout.in', $params);
  806. }
  807. // 打小票
  808. if ($order->order_status_payment == 'SUCCESS') {
  809. if (!empty($premises) && !empty($premises->dept_id)) {
  810. $voteData = [
  811. 'func' => 'procActionToPrinter',
  812. 'sign' => '',
  813. 'data' => [
  814. 'printer_premises' => $premises->dept_id,
  815. 'printer_device' => ["收银"],
  816. 'printer_action' => 'ExecPrintOrder',
  817. 'printer_data' => [
  818. 'order_id' => $params['orderId'],
  819. 'order_batch' => ''
  820. ]
  821. ]
  822. ];
  823. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  824. }
  825. }
  826. if ($order->order_status_payment != 'SUCCESS') {
  827. _syslog("订单", "支付异常,检查是否有轮询");
  828. // 恢复优惠券到已占用
  829. if (!is_array($couponUseJson)) {
  830. $couponUseJson = json_decode($couponUseJson, true);
  831. }
  832. // 如果下单时就填了,不用恢复
  833. if (empty($discountJson)) {
  834. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  835. }
  836. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  837. }
  838. _syslog("订单", "订单支付成功");
  839. return json_success('支付成功');
  840. } catch (BusinessException $e) {
  841. Db::rollBack();
  842. _syslog("订单", "订单支付失败:" . $e->getMessage());
  843. Log::error('订单支付失败', ['msg' => $e->getMessage()]);
  844. return json_fail("支付失败:" . $e->getMessage());
  845. } catch (\Exception $e) {
  846. Db::rollBack();
  847. _syslog("订单", "订单支付失败");
  848. Log::error('订单支付失败', ['msg' => $e->getMessage()]);
  849. return json_fail('支付失败');
  850. }
  851. }
  852. /**
  853. * @Desc
  854. * @Author Gorden
  855. * @Date 2024/6/7 10:30
  856. *
  857. * @param $params
  858. * @return void
  859. * @throws BusinessException
  860. */
  861. public function insertMain($params, $wxAndAliPayStatus = 'Y')
  862. {
  863. try {
  864. if (empty($params['order_extend_json'])) {
  865. $params['order_extend_json'] = [];
  866. } else {
  867. if (is_json($params['order_extend_json'])) {
  868. $params['order_extend_json'] = json_decode($params['order_extend_json'], true);
  869. }
  870. }
  871. // 账户扣款
  872. if ($wxAndAliPayStatus == 'Y' && !empty($params['waitToPayAccount'])) {
  873. foreach ($params['waitToPayAccount'] as $account) {
  874. $this->deductAmount($account['nbr'], $account['amount']);
  875. }
  876. }
  877. // 推荐人
  878. $params['order_extend_json']['referee'] = $params['referee'] ?? '';
  879. $orderAmount = [];
  880. foreach ($params['goodsContentList'] as $goods) {
  881. if (floatval($goods['goods_sales_price']) > 0) {
  882. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $params['order_amount_total']) * $params['order_amount_pay'], 2);
  883. } else {
  884. $amountPay = 0;
  885. }
  886. $discountJson = [];
  887. if (!empty($params['order_discount_json'])) {
  888. $discountJson = json_decode($params['order_discount_json'], true);
  889. foreach ($discountJson as &$item) {
  890. if (!empty($item['coupon_id'])) {
  891. $orderAmount = OrderService::countAndAmount($params['goodsContentList'], $item['coupon_id']);
  892. $payAmountUseCoupon = $params['order_amount_pay'] - ($params['order_amount_total'] - $orderAmount['amount']);
  893. if (in_array($goods['goods_id'], $orderAmount['goodsIds'])) {
  894. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $orderAmount['amount']) * $payAmountUseCoupon, 2);
  895. }
  896. } else {
  897. $orderAmount['amount'] = $params['order_amount_total'];
  898. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $params['order_amount_total']) * $params['order_amount_pay'], 2);
  899. }
  900. if (!empty($item['coupon_value'])) {
  901. $item['coupon_value'] = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $orderAmount['amount']) * $item['coupon_value'], 2);
  902. }
  903. }
  904. }
  905. $orderId = 'OD' . date('ymdHi') . random_string(4, 'up');
  906. // 发卡
  907. $orderIsComplete = $params['order_is_complete'] ?? 'N';
  908. $orderStatusSystem = $params['order_status_system'];
  909. $cardId = '';
  910. if ($params['order_status_payment'] == 'SUCCESS') {
  911. $cardId = CardService::sendDemandCard($params['join_order_member_id'], $goods, $params);
  912. if ($cardId !== false) {
  913. $orderIsComplete = 'Y';
  914. $orderStatusSystem = 'CONFIRM';
  915. }
  916. }
  917. $data = [
  918. 'order_id' => $orderId,
  919. 'order_groupby' => $params['orderGroupId'],
  920. 'join_order_member_id' => $params['join_order_member_id'],
  921. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  922. 'order_amount_total' => floatval($goods['goods_sales_price']) * $goods['nbr'],
  923. 'order_amount_pay' => $amountPay,
  924. 'order_category' => $goods['goods_classify'],
  925. 'order_classify' => $goods['goods_classify'],
  926. 'order_is_complete' => $orderIsComplete,
  927. 'order_status_system' => $orderStatusSystem,
  928. 'order_status_payment' => $params['order_status_payment'],
  929. 'order_status_storage' => $params['order_status_storage'],
  930. 'order_platform' => $params['order_platform'],
  931. 'order_remark' => $params['order_remark'] ?? '',
  932. 'order_discount_json' => (!empty($orderAmount['goodsIds']) && in_array($goods['goods_id'], $orderAmount['goodsIds'])) || (empty($orderAmount['goodsIds']) && !empty($orderAmount['amount'])) ? json_encode($discountJson) : '[]',
  933. 'order_config_json' => $params['order_config_json'] ?? '[]',
  934. 'order_express_json' => $params['order_express_json'] ?? '[]',
  935. 'order_extend_json' => $params['order_extend_json'] ? json_encode($params['order_extend_json']) : '[]',
  936. 'order_addtimes' => time(),
  937. 'order_add_user_id' => JwtToken::getCurrentId()
  938. ];
  939. Order::insert($data);
  940. $sheetIds = $this->insertSheetOne($params, $goods, $orderId, $amountPay);
  941. $this->insertPayDetailOne($params, $orderId, $amountPay, $goods);
  942. $params['order_express_goods'] = json_encode(['sheet' => $sheetIds]);
  943. $this->insertExpressOne($params, $orderId, $cardId);
  944. }
  945. } catch (\Exception $e) {
  946. dump($e->getMessage(), $e->getFile(), $e->getLine());
  947. throw new BusinessException('订单创建信息失败');
  948. }
  949. }
  950. /**
  951. * @Desc
  952. * @Author Gorden
  953. * @Date 2024/6/7 10:25
  954. *
  955. * @param $params
  956. * @return void
  957. * @throws BusinessException
  958. */
  959. public function insertSheet($params)
  960. {
  961. try {
  962. $orderSheetIds = [];
  963. foreach ($params['goodsContentList'] as $goods) {
  964. //{"unit": "份", "table": null, "premises": "15"}
  965. $price = floatval($goods['goods_sales_price']);
  966. $extendJson['unit'] = $goods['sku_name'];
  967. if (isset($params['submit_premises_id'])) {
  968. $extendJson['premises'] = $params['submit_premises_id'];
  969. }
  970. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  971. $extendJson['table'] = null;
  972. }
  973. $orderSheetStatus = 'PAYING';
  974. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
  975. if (floatval($params['order_amount_pay']) >= floatval($params['order_amount_total'])) {
  976. $orderSheetStatus = 'DONE';
  977. } else {
  978. $orderSheetStatus = 'BEING';
  979. }
  980. }
  981. $data = [
  982. 'join_sheet_member_id' => $params['join_order_member_id'],
  983. 'join_sheet_order_id' => $params['orderId'],
  984. 'join_sheet_goods_id' => $goods['goods_id'],
  985. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  986. 'order_sheet_status' => $orderSheetStatus,
  987. 'order_sheet_category' => 'CARD',
  988. 'order_sheet_num' => $goods['nbr'],
  989. 'order_sheet_price' => $goods['goods_sales_price'],
  990. 'order_sheet_price_pay' => $goods['goods_sales_price'],
  991. 'order_sheet_amount' => $price * $goods['nbr'],
  992. 'order_sheet_pay' => $price * $goods['nbr'],
  993. 'order_sheet_task_status' => 'NONE',
  994. 'order_sheet_remark' => $params['order_remark'] ?? '',
  995. 'order_sheet_addtimes' => time(),
  996. 'order_sheet_extend_json' => json_encode($extendJson)
  997. ];
  998. $orderSheetId = OrderSheet::insertGetId($data);
  999. $orderSheetIds[] = $orderSheetId;
  1000. // 减库存,规格和总库存
  1001. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  1002. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  1003. if ($goodsRunning->goods_running_storage < 0) {
  1004. throw new BusinessException('库存不足');
  1005. }
  1006. if ($params['order_status_payment'] == 'SUCCESS') {
  1007. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  1008. }
  1009. $goodsRunning->save();
  1010. }
  1011. return $orderSheetIds;
  1012. } catch (\support\exception\BusinessException $e) {
  1013. dump($e->getMessage() . '||' . $e->getLine());
  1014. throw new BusinessException($e->getMessage());
  1015. } catch (\Exception $e) {
  1016. dump($e->getMessage() . '||' . $e->getLine());
  1017. throw new BusinessException('订单创建失败');
  1018. }
  1019. }
  1020. /**
  1021. * @Desc
  1022. * @Author Gorden
  1023. * @Date 2024/6/7 10:35
  1024. *
  1025. * @param $params
  1026. * @return void
  1027. * @throws BusinessException
  1028. */
  1029. public function insertPayDetail($params)
  1030. {
  1031. try {
  1032. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1033. $payPrepayid = $params['pay_category'];
  1034. } else if ($params['pay_category'] == 'OFFLINE') {
  1035. $payPrepayid = 'OFFLINE';
  1036. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1037. $payPrepayid = 'OFFLINE_ALIPAY';
  1038. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1039. $payPrepayid = 'OFFLINE_WXPAY';
  1040. } else if ($params['pay_category'] == 'MONEY') {
  1041. $payPrepayid = 'MONEY';
  1042. } else {
  1043. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  1044. }
  1045. $data = [
  1046. 'join_pay_member_id' => $params['join_order_member_id'],
  1047. 'join_pay_order_id' => $params['orderGroupId'],
  1048. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  1049. 'pay_category' => $params['goods_classify'],
  1050. 'pay_amount' => $params['order_amount_pay'],
  1051. 'pay_prepayid' => $payPrepayid,
  1052. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1053. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
  1054. 'pay_json_request' => json_encode($params),
  1055. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  1056. 'pay_remark' => $params['order_remark'] ?? '',
  1057. 'pay_addtimes' => time(),
  1058. ];
  1059. PayDetail::insert($data);
  1060. } catch (\Exception $e) {
  1061. dump($e->getMessage());
  1062. throw new BusinessException('创建支付记录失败');
  1063. }
  1064. }
  1065. /**
  1066. * @Desc 订单商品详情
  1067. * @Author Gorden
  1068. * @Date 2024/3/29 8:50
  1069. *
  1070. * @param Request $request
  1071. * @return Response
  1072. */
  1073. public function sheet(Request $request)
  1074. {
  1075. try {
  1076. $orderId = $request->get('order_id');
  1077. $orderSheet = OrderSheet::with([
  1078. 'member' => function ($query) {
  1079. $query->select('member_id', 'member_mobile', 'member_is_owner', 'join_member_role_id', 'member_is_vip', 'member_is_partner', 'member_is_referrer', 'join_invite_member_id', 'member_is_franchisee');
  1080. },
  1081. 'goods' => function ($query) {
  1082. $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify', 'goods_if_express');
  1083. },
  1084. 'memberInfo',
  1085. 'cert',
  1086. 'sku' => function ($query) {
  1087. $query->where('goods_sku_status', 'ON')
  1088. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price');
  1089. },
  1090. 'skus',
  1091. 'refund' => function ($query) {
  1092. $query->select('join_return_order_id', 'order_return_status');
  1093. }
  1094. ])->where('join_sheet_order_id', $orderId)
  1095. ->orderBy('order_sheet_id', 'DESC')
  1096. ->get()
  1097. ->toArray();
  1098. $order = Order::where('order_id', $orderId)->first();
  1099. $express = OrderExpress::where('join_express_order_id', $orderId)->first();
  1100. $sheetAmount = 0;
  1101. foreach ($orderSheet as &$item) {
  1102. $sheetAmount += $item['order_sheet_amount'];
  1103. $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
  1104. if (!empty($item['goods']) && $item['goods']['goods_classify'] == 'PACKAGE') {
  1105. $components = GoodsComponent::with('goods')
  1106. ->where('join_component_master_goods_id', $item['goods']['goods_id'])
  1107. ->select('join_component_master_goods_id', 'join_component_goods_id', 'goods_component_price',
  1108. 'goods_component_price', 'goods_component_json')
  1109. ->get()
  1110. ->toArray();
  1111. $goodsArr = [];
  1112. foreach ($components as $component) {
  1113. $configJson = !empty($component['goods_component_json']) ? json_decode($component['goods_component_json'], true) : [];
  1114. if (!empty($component['goods'])) {
  1115. $supplierName = Supplier::where('supplier_id', $component['goods']['join_goods_supplier_id'])->value('supplier_name');
  1116. $benefit = MemberBenefit::where('join_benefit_member_id', $item['join_sheet_member_id'])
  1117. ->where('join_benefit_goods_id', $component['goods']['goods_id'])
  1118. ->where('join_benefit_order_id', $orderId)
  1119. ->first();
  1120. $goodsArr[] = [
  1121. 'goods_name' => $component['goods']['goods_name'],
  1122. 'goods_cover' => getenv('STORAGE_DOMAIN') . $component['goods']['goods_cover'],
  1123. 'supplier_name' => $supplierName,
  1124. 'nbr' => $configJson['nbr'] ?? 0,
  1125. 'used' => !empty($benefit->member_benefit_used_count) ? intval($benefit->member_benefit_used_count) : ''
  1126. ];
  1127. }
  1128. }
  1129. $item['goods']['components'] = $goodsArr;
  1130. }
  1131. if (!empty($item['sku'])) {
  1132. if (!empty($item['sku']['goods_sku_specs_json'])) {
  1133. $specsJson = json_decode($item['sku']['goods_sku_specs_json'], true);
  1134. $skuName = '';
  1135. foreach ($specsJson as $specsKey => $skuSpecs) {
  1136. if (is_array($skuSpecs)) {
  1137. $skuName = $skuName . ' ' . implode(' ', $skuSpecs) . ';';
  1138. } else {
  1139. $skuName = $skuName . ' ' . $skuSpecs . ';';
  1140. }
  1141. }
  1142. $item['sku']['goods_sku_title'] = rtrim($skuName, ';');
  1143. }
  1144. }
  1145. if (!empty($item['skus'])) {
  1146. foreach ($item['skus'] as $key => $skus) {
  1147. if (!empty($skus['goods_sku_specs_json'])) {
  1148. $item['skus'][$key]['goods_sku_specs_json'] = json_decode($skus['goods_sku_specs_json']);
  1149. $skuName = '';
  1150. foreach ($item['skus'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  1151. $keyStr = ($specsKey == '规格') ? '' : ($specsKey . ':');
  1152. if (is_array($skuSpecs)) {
  1153. $skuName = $skuName . $keyStr . ' ' . implode(' ', $skuSpecs) . '; ';
  1154. } else {
  1155. $skuName = $skuName . $keyStr . ' ' . $skuSpecs . '; ';
  1156. }
  1157. }
  1158. $item['skus'][$key]['sku_name'] = rtrim($skuName, '; ');
  1159. }
  1160. }
  1161. } else {
  1162. $item['skus'] = [];
  1163. }
  1164. if (in_array($item['goods']['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $order->order_status_system == 'DONE') {
  1165. $item['appontment'] = [];
  1166. $appontments = Appointment::where('join_appointment_order_id', $orderId)
  1167. ->where('appointment_status', 'DONE')
  1168. ->select('appointment_status', 'appointment_id', 'appointment_done_datetime', 'appointment_done_json', 'appointment_apply_json')
  1169. ->get()
  1170. ->toArray();
  1171. foreach ($appontments as $appontment) {
  1172. $doneJson = [];
  1173. $username = '';
  1174. if (!empty($appontment['appointment_done_json'])) {
  1175. $doneJson = json_decode($appontment['appointment_done_json'], true);
  1176. if (isset($doneJson['charge'])) {
  1177. $username = SysUser::where('user_id', $doneJson['charge']['charge_user_id'])->value('user_name');
  1178. }
  1179. }
  1180. $person = 1;
  1181. if (!empty($appontment->appointment_apply_json)) {
  1182. $applyJson = json_decode($appontment->appointment_apply_json, true);
  1183. if (isset($applyJson['person'])) {
  1184. $person = $applyJson['person'];
  1185. }
  1186. }
  1187. $item['appontment'][] = [
  1188. 'member' => ($item['cert'] ? $item['cert']['member_cert_name'] . '-' : '') . ($item['member'] ? $item['member']['member_mobile'] : ''),
  1189. 'goods_name' => $item['goods']['goods_name'],
  1190. 'premisses' => isset($doneJson['charge']) ? $doneJson['charge']['charge_premises'] : '',
  1191. 'username' => $username,
  1192. 'nbr' => $person,
  1193. 'done_time' => $appontment['appointment_done_datetime'],
  1194. 'appointment_status' => $appontment['appointment_status'],
  1195. ];
  1196. }
  1197. }
  1198. if (!empty($item['order_sheet_extend_json']) && !$express) {
  1199. $extendJson = json_decode($item['order_sheet_extend_json'], true);
  1200. if (isset($extendJson['address_id'])) {
  1201. $address = ClientConfig::where('client_config_id', $extendJson['address_id'])->first();
  1202. if (!empty($address)) {
  1203. $valJson = json_decode($address->client_config_val_json, true);
  1204. $express = [
  1205. 'order_express_address' => $valJson['address'] . ($valJson['numbers'] ?? ''),
  1206. 'order_express_city' => $valJson['city'],
  1207. 'order_express_mobile' => $valJson['mobile'],
  1208. 'order_express_person' => $valJson['person']
  1209. ];
  1210. }
  1211. }
  1212. }
  1213. $item['member']['level'] = '普通会员';
  1214. if (!empty($item['member']['join_member_role_id'])) {
  1215. $item['member']['level'] = MemberRole::where('member_role_id', $item['member']['join_member_role_id'])->value('member_role_name');
  1216. }
  1217. $item['member_info']['member_info_nickname'] = MemberService::getNickname(!empty($item['member_info']) ? $item['member_info']['member_info_nickname'] : '', (!empty($item['member']) ? $item['member']['member_mobile'] : ''));
  1218. $item['member_info']['member_info_headimg'] = MemberService::getAvatarUrl(!empty($item['member_info']) && !empty($item['member_info']['member_info_headimg']) ? $item['member_info']['member_info_headimg'] : '');
  1219. // 推荐人
  1220. if (!empty($item['member']['join_invite_member_id'])) {
  1221. $inviteMember = Member::with([
  1222. 'cert' => function ($query) {
  1223. $query->select('join_cert_member_id', 'member_cert_name');
  1224. }
  1225. ])->where('member_id', $item['member']['join_invite_member_id'])
  1226. ->first();
  1227. $inviteMobile = $inviteMember['member_mobile'] ?? '';
  1228. $inviteCertName = !empty($inviteMember['cert']) && !empty($inviteMember['cert']['member_cert_name']) ? $inviteMember['cert']['member_cert_name'] : '';
  1229. $order->invite_name = MemberService::getMemberCertName($inviteMobile, $inviteCertName, '');
  1230. }
  1231. }
  1232. $order->sheet_amount = number_format($sheetAmount, 2);
  1233. $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $order->order_id)
  1234. ->where('pay_category', 'CARD')
  1235. ->where('pay_status', 'SUCCESS')
  1236. ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json')
  1237. ->orderBy('pay_addtimes', 'DESC')
  1238. ->get();
  1239. $order->pay_amount_total = 0;
  1240. foreach ($payDetails as &$payDetail) {
  1241. $categoryArray = explode('-', $payDetail->pay_prepayid);
  1242. if (isset($categoryArray[1])) {
  1243. $payDetail->pay_category = $categoryArray[1];
  1244. } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) {
  1245. $payDetail->pay_category = $categoryArray[0];
  1246. }
  1247. if (!empty($payDetail->pay_extend_json)) {
  1248. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  1249. $order->cancel_times = $payExtendJson['cancel_times'] ?? '';
  1250. if (isset($payExtendJson['ticket'])) {
  1251. $payDetail->ticket = str_replace('/thumb', '', getenv("STORAGE_DOMAIN") . $payExtendJson['ticket']);
  1252. }
  1253. }
  1254. $order->pay_amount_total += $payDetail->pay_amount;
  1255. }
  1256. $refund = OrderReturn::where('join_return_order_id', $orderId)
  1257. ->select('order_return_status', 'order_return_apply_datetime', 'order_return_apply_json', 'order_return_accept_datetime', 'order_return_refund_json', 'order_return_extend_json')
  1258. ->first();
  1259. if (!empty($refund->order_return_refund_json)) {
  1260. $returnRefundJson = json_decode($refund->order_return_refund_json, true);
  1261. if (isset($returnRefundJson['user_id'])) {
  1262. $userName = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name');
  1263. $returnRefundJson['person'] = $userName;
  1264. unset($returnRefundJson['user_id']);
  1265. }
  1266. $refund->order_return_refund_json = json_encode($returnRefundJson);
  1267. }
  1268. if (!empty($order->order_config_json)) {
  1269. $orderConfigJson = json_decode($order->order_config_json, true);
  1270. if (isset($orderConfigJson['reach'])) {
  1271. $order->reach = $orderConfigJson['reach'];
  1272. }
  1273. if (isset($orderConfigJson['table'])) {
  1274. $order->table = $orderConfigJson['table'];
  1275. }
  1276. if (isset($orderConfigJson['eat'])) {
  1277. $order->eat = $orderConfigJson['eat'];
  1278. }
  1279. if (isset($orderConfigJson['express'])) {
  1280. $order->express = $orderConfigJson['express'];
  1281. }
  1282. if (isset($orderConfigJson['premises'])) {
  1283. $order->premises = $orderConfigJson['premises'];
  1284. }
  1285. if (isset($orderConfigJson['tableid'])) {
  1286. $order->dept_table_id = $orderConfigJson['tableid'];
  1287. }
  1288. }
  1289. if (!empty($order->order_extend_json)) {
  1290. $orderExtendJson = json_decode($order->order_extend_json, true);
  1291. $order->referee = $orderExtendJson['referee'] ?? '';
  1292. if (isset($orderExtendJson['cancel_times'])) {
  1293. $order->cancel_times = $orderExtendJson['cancel_times'];
  1294. }
  1295. if (isset($orderExtendJson['free_remark'])) {
  1296. $order->free_remark = $orderExtendJson['free_remark'];
  1297. }
  1298. if (isset($orderExtendJson['dept_id'])) {
  1299. $order->dept_id = $orderExtendJson['dept_id'];
  1300. }
  1301. if (isset($orderExtendJson['dept_name'])) {
  1302. $order->premises = $orderExtendJson['dept_name'];
  1303. }
  1304. if (isset($orderExtendJson['referee'])) {
  1305. $order->invite_name = $orderExtendJson['referee'];
  1306. }
  1307. }
  1308. $discount = ['coupon_name' => '', 'classify' => '', 'value' => 0];
  1309. if (!empty($order->order_discount_json)) {
  1310. $orderDiscountJson = json_decode($order->order_discount_json, true);
  1311. foreach ($orderDiscountJson as $discountItem) {
  1312. if (!empty($discountItem['coupon_id'])) {
  1313. $coupon = Coupon::where('coupon_id', $discountItem['coupon_id'])
  1314. ->select('coupon_name', 'coupon_classify', 'coupon_category', 'coupon_value', 'coupon_minimum_limit')
  1315. ->first();
  1316. if (!$coupon) {
  1317. continue;
  1318. }
  1319. $classify = CouponService::couponClassifyInfo($coupon->coupon_classify, $coupon->coupon_category, $coupon->coupon_value, $coupon->coupon_minimum_limit);
  1320. $discount['coupon_name'] .= $coupon->coupon_classify . ':' . $coupon->coupon_name . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  1321. }
  1322. if ($discountItem['coupon_classify'] == '退款') {
  1323. continue;
  1324. }
  1325. if (empty($discountItem['coupon_id']) && !empty($discountItem['coupon_classify'])) {
  1326. if (!empty($discountItem['coupon_detail_id'])) {
  1327. $discount['classify'] .= $discountItem['coupon_detail_id'][0] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  1328. } else {
  1329. $discount['classify'] .= $discountItem['coupon_classify'] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  1330. }
  1331. }
  1332. if (!empty($discountItem['coupon_value'])) {
  1333. $discount['value'] += $discountItem['coupon_value'];
  1334. }
  1335. }
  1336. if (!empty($discount['coupon_name'])) {
  1337. $discount['coupon_name'] = rtrim($discount['coupon_name'], ', ');
  1338. }
  1339. if (!empty($discount['classify'])) {
  1340. $discount['classify'] = rtrim($discount['classify'], ', ');
  1341. }
  1342. }
  1343. $discount['value'] = sprintf("%.2f", $discount['value']);
  1344. $order->discount = $discount;
  1345. $order->premises = $order->premises ?? '';
  1346. $data = [
  1347. 'order' => $order,
  1348. 'refund' => json_decode(json_encode($refund)),
  1349. 'sheet' => json_decode(json_encode($orderSheet)),
  1350. 'express' => json_decode(json_encode($express)),
  1351. 'payDetails' => json_decode(json_encode($payDetails))
  1352. ];
  1353. return json_success('', $data);
  1354. } catch (\Exception $e) {
  1355. dump($e->getMessage());
  1356. }
  1357. }
  1358. /**
  1359. * @Desc 获取分期的订单
  1360. * @Author Gorden
  1361. * @Date 2024/9/10 15:14
  1362. *
  1363. * @param Request $request
  1364. * @return Response
  1365. */
  1366. public function getPaidOrder(Request $request)
  1367. {
  1368. $memberId = $request->get('member_id');
  1369. if (!$memberId) {
  1370. return json_fail('参数异常');
  1371. }
  1372. $order = Order::where('join_order_member_id', $memberId)
  1373. ->where('order_category', 'CARD')
  1374. ->where('order_status_system', 'PAYING')
  1375. ->select('order_id', 'order_amount_total', 'order_amount_paid', 'order_amount_pay', 'order_extend_json')
  1376. ->first();
  1377. return json_success('', $order);
  1378. }
  1379. /**
  1380. * @Desc 上传支付凭证
  1381. * @Author Gorden
  1382. * @Date 2024/9/12 10:14
  1383. *
  1384. * @param Request $request
  1385. * @return Response
  1386. */
  1387. public function uploadTicket(Request $request)
  1388. {
  1389. $payId = $request->post('pay_id');
  1390. $url = $request->post('url');
  1391. if (!$payId || !$url) {
  1392. return json_fail("参数异常");
  1393. }
  1394. $url = str_replace(getenv("STORAGE_DOMAIN"), '', $url);
  1395. try {
  1396. $payDetail = PayDetail::where('pay_id', $payId)->first();
  1397. if (!$payDetail) {
  1398. return json_fail('数据异常');
  1399. }
  1400. $extendJson = [];
  1401. if (!empty($payDetail->pay_extend_json)) {
  1402. $extendJson = json_decode($payDetail->pay_extend_json, true);
  1403. }
  1404. $extendJson['ticket'] = $url;
  1405. $payDetail->pay_extend_json = json_encode($extendJson);
  1406. dump($payDetail->save());
  1407. return json_success('success');
  1408. } catch (\Exception $e) {
  1409. return json_fail("上传附件失败");
  1410. }
  1411. }
  1412. /**
  1413. * @Desc 查询订单状态
  1414. * @Author Gorden
  1415. * @Date 2024/8/19 11:55
  1416. *
  1417. * @param Request $request
  1418. * @return Response|void
  1419. */
  1420. public function getOrderPayStatus(Request $request)
  1421. {
  1422. $groupId = $request->get('group_id');
  1423. if (!$groupId) {
  1424. return json_fail('参数异常');
  1425. }
  1426. $order = Order::where('order_groupby', $groupId)->first();
  1427. if (empty($order)) {
  1428. return json_fail('订单不存在');
  1429. }
  1430. $order = $order->toArray();
  1431. $sheet = OrderSheet::where('join_sheet_order_id', $order['order_id'])
  1432. ->select('order_sheet_id', 'join_sheet_goods_id', 'join_sheet_goods_sku_id')
  1433. ->first();
  1434. $payDetailType = PayDetail::where('join_pay_order_id', $groupId)->pluck('pay_prepayid')->toArray();
  1435. try {
  1436. Db::beginTransaction();
  1437. $payStatus = 'N';
  1438. $payAmount = 0;
  1439. if (in_array('WXPAY', $payDetailType)) {
  1440. $result = Pay::wechat(config('payment.wxpay'))->find($groupId, 'pos');
  1441. $result = json_decode(json_encode($result), true);
  1442. // $result = '{"return_code":"SUCCESS","return_msg":"OK","result_code":"SUCCESS","mch_id":"1680393367","appid":"wxc6274da7198e3eb4","openid":"o3JAn6Ii_bAlxS-jbNEC4WnPhdwM","is_subscribe":"N","trade_type":"MICROPAY","trade_state":"SUCCESS","bank_type":"OTHERS","total_fee":"50","fee_type":"CNY","cash_fee":"1000","cash_fee_type":"CNY","transaction_id":"4200067718202409250802875650","out_trade_no":"OD24092518408RV7","attach":[],"time_end":"20240925184009","trade_state_desc":"支付成功","nonce_str":"OeGOkjch4eaV5qIt","sign":"6DCB3BFC594EBC018A2BEE2C3DFEA4E3"}';
  1443. // $result = json_decode($result, true);
  1444. 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') {
  1445. $payStatus = 'Y';
  1446. $payAmount = ($result['total_fee'] / 100);
  1447. $orderUpdateData['order_status_payment'] = 'PENDING';
  1448. $orderUpdateData['order_amount_paid'] = $order['order_amount_paid'] + $payAmount;
  1449. $orderUpdateData['order_amount_pay'] = $order['order_amount_pay'] + $payAmount;
  1450. if ($order['order_amount_paid'] + $payAmount >= $order['order_amount_total']) {
  1451. $orderUpdateData['order_status_payment'] = 'SUCCESS';
  1452. $orderUpdateData['order_status_system'] = 'DONE';
  1453. $orderUpdateData['order_is_complete'] = 'Y';
  1454. }
  1455. // 主订单
  1456. Order::where('order_groupby', $groupId)->update($orderUpdateData);
  1457. // Sheet
  1458. OrderSheet::where('join_sheet_order_id', $order['order_id'])->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
  1459. // 支付记录
  1460. PayDetail::where('join_pay_order_id', $order['order_groupby'])->where('pay_prepayid', 'WXPAY')->update([
  1461. 'pay_status' => 'SUCCESS',
  1462. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1463. 'pay_json_response' => json_encode($result)
  1464. ]);
  1465. }
  1466. } else if (in_array('ALIPAY', $payDetailType)) {
  1467. $result = Pay::alipay(config('payment.alipay'))->find($groupId);
  1468. $result = json_decode(json_encode($result), true);
  1469. // $result = '{"code":"10000","msg":"Success","buyer_logon_id":"138******93","buyer_pay_amount":"5.87","fund_bill_list":[{"amount":"5.69","fund_channel":"ALIPAYACCOUNT"},{"amount":"0.18","fund_channel":"COUPON"},{"amount":"0.13","fund_channel":"DISCOUNT"}],"invoice_amount":"5.69","out_trade_no":"OD2409251550Q07A","point_amount":"0.00","receipt_amount":"0.01","send_pay_date":"2024-09-25 15:50:22","total_amount":"0.01","trade_no":"2024092523001439431419750998","trade_status":"TRADE_SUCCESS","buyer_open_id":"04309N2aVhSZz4cKwN_DN2DQa7ekM3z5n8kscQHsmIZOJsf"}';
  1470. // $result = json_decode($result, true);
  1471. if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
  1472. $payStatus = 'Y';
  1473. $payAmount = $result['total_amount'];
  1474. $orderUpdateData['order_status_payment'] = 'PENDING';
  1475. $orderUpdateData['order_amount_paid'] = $order['order_amount_paid'] + $payAmount;
  1476. $orderUpdateData['order_amount_pay'] = $order['order_amount_pay'] + $payAmount;
  1477. if ($order['order_amount_paid'] + $payAmount >= $order['order_amount_total']) {
  1478. $orderUpdateData['order_status_payment'] = 'SUCCESS';
  1479. $orderUpdateData['order_status_system'] = 'DONE';
  1480. $orderUpdateData['order_is_complete'] = 'Y';
  1481. }
  1482. // 主订单
  1483. Order::where('order_groupby', $groupId)->update($orderUpdateData);
  1484. // Sheet
  1485. OrderSheet::where('join_sheet_order_id', $order['order_id'])->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
  1486. // 支付记录
  1487. PayDetail::where('join_pay_order_id', $order['order_groupby'])->where('pay_prepayid', 'ALIPAY')->update([
  1488. 'pay_status' => 'SUCCESS',
  1489. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1490. 'pay_json_response' => json_encode($result)
  1491. ]);
  1492. }
  1493. }
  1494. if ($payStatus == 'Y') {
  1495. Db::commit();
  1496. // 入收支明细表
  1497. $params['orderId'] = $order['order_id'];
  1498. $params['inout_category'] = '购买储值卡';
  1499. Event::dispatch('statistics.inout.in', $params);
  1500. return json_success('success');
  1501. }
  1502. Db::rollBack();
  1503. return json_fail('没有查询到记录');
  1504. } catch (BusinessException $e) {
  1505. Db::rollBack();
  1506. _syslog("支付轮询", '订单支付异常:' . $e->getMessage());
  1507. return json_fail($e->getMessage());
  1508. } catch (\Exception $e) {
  1509. dump($e->getMessage());
  1510. Db::rollBack();
  1511. return json_fail('查询失败');
  1512. }
  1513. }
  1514. /**
  1515. * @Desc 单条入库
  1516. * @Author Gorden
  1517. * @Date 2024/9/23 10:19
  1518. *
  1519. * @param $params
  1520. * @return array
  1521. * @throws BusinessException
  1522. */
  1523. public function insertSheetOne($params, $goods, $orderId, $amountPay)
  1524. {
  1525. try {
  1526. $orderSheetIds = [];
  1527. // foreach ($params['goodsContentList'] as $goods) {
  1528. //{"unit": "份", "table": null, "premises": "15"}
  1529. $price = floatval($goods['goods_sales_price']);
  1530. $extendJson['unit'] = $goods['sku_name'];
  1531. if (isset($params['submit_premises_id'])) {
  1532. $extendJson['premises'] = $params['submit_premises_id'];
  1533. }
  1534. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  1535. $extendJson['table'] = null;
  1536. }
  1537. $data = [
  1538. 'join_sheet_member_id' => $params['join_order_member_id'],
  1539. 'join_sheet_order_id' => $orderId,
  1540. 'join_sheet_goods_id' => $goods['goods_id'],
  1541. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  1542. 'order_sheet_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  1543. 'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL',
  1544. 'order_sheet_num' => $goods['nbr'],
  1545. 'order_sheet_price' => $goods['goods_sales_price'],
  1546. 'order_sheet_price_pay' => round($amountPay / $goods['nbr'], 2),
  1547. 'order_sheet_amount' => $price * $goods['nbr'],
  1548. 'order_sheet_pay' => $amountPay,
  1549. 'order_sheet_task_status' => 'NONE',
  1550. 'order_sheet_remark' => $params['order_remark'] ?? '',
  1551. 'order_sheet_addtimes' => time(),
  1552. 'order_sheet_extend_json' => json_encode($extendJson)
  1553. ];
  1554. $orderSheetId = OrderSheet::insertGetId($data);
  1555. $orderSheetIds[] = $orderSheetId;
  1556. // 减库存,规格和总库存
  1557. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  1558. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  1559. if ($goodsRunning->goods_running_storage < 0) {
  1560. throw new BusinessException('库存不足');
  1561. }
  1562. if ($params['order_status_payment'] == 'SUCCESS') {
  1563. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  1564. }
  1565. $goodsRunning->save();
  1566. // }
  1567. return $orderSheetIds;
  1568. } catch (\support\exception\BusinessException $e) {
  1569. dump($e->getMessage() . '||' . $e->getLine());
  1570. throw new BusinessException($e->getMessage());
  1571. } catch (\Exception $e) {
  1572. dump($e->getMessage() . '||' . $e->getLine());
  1573. throw new BusinessException('订单创建失败');
  1574. }
  1575. }
  1576. public function saveExpress($params, $orderId)
  1577. {
  1578. try {
  1579. $express = new OrderExpress();
  1580. $express->order_express_type = $params['order_express_type'];
  1581. $express->join_express_order_id = $orderId;
  1582. $express->join_express_dept_id = $params['submit_premises_id'] ?? 0;
  1583. $express->order_express_goods = $params['order_express_goods'];
  1584. $express->order_express_city = $params['order_express_city'];
  1585. $express->order_express_address = $params['order_express_address'];
  1586. $express->order_express_mobile = $params['order_express_mobile'] ?? '';
  1587. $express->order_express_telephone = $params['order_express_telephone'] ?? '';
  1588. $express->order_express_person = $params['order_express_person'] ?? '';
  1589. $express->order_express_company = $params['dept_premises_id'] ?? '';
  1590. $express->order_express_extend_json = $params['order_express_extend_json'] ?? '[]';
  1591. $express->order_express_addtimes = time();
  1592. $express->save();
  1593. } catch (\Exception $e) {
  1594. dump($e->getMessage());
  1595. throw new BusinessException('物流信息保存失败');
  1596. }
  1597. }
  1598. /**
  1599. * @Desc 单条入库
  1600. * @Author Gorden
  1601. * @Date 2024/6/7 10:35
  1602. *
  1603. * @param $params
  1604. * @return void
  1605. * @throws BusinessException
  1606. */
  1607. public function insertPayDetailOne($params, $orderId, $amountPay, $goods)
  1608. {
  1609. try {
  1610. if (!empty($params['pay_detail_item'])) {
  1611. foreach ($params['pay_detail_item'] as $item) {
  1612. $data = [
  1613. 'join_pay_member_id' => $item['join_order_member_id'],
  1614. 'join_pay_order_id' => $item['orderGroupId'],
  1615. 'pay_status' => $item['settlement_now'] == 'Y' && $item['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  1616. 'pay_category' => $goods['goods_classify'],
  1617. 'pay_amount' => round(($amountPay / $params['order_amount_pay']) * $item['order_amount_pay'], 2),
  1618. 'pay_prepayid' => $item['pay_category'],
  1619. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1620. 'join_pay_object_json' => !empty($item['orderId']) ? json_encode(['order_id' => $orderId]) : '[]',
  1621. 'pay_json_request' => json_encode($item),
  1622. 'pay_json_response' => $item['pay_json_response'] ?? '[]',
  1623. 'pay_remark' => $item['order_remark'] ?? '',
  1624. 'pay_addtimes' => time(),
  1625. ];
  1626. PayDetail::insert($data);
  1627. }
  1628. } else {
  1629. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1630. $payPrepayid = $params['pay_category'];
  1631. } else if ($params['pay_category'] == 'OFFLINE') {
  1632. $payPrepayid = 'OFFLINE';
  1633. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1634. $payPrepayid = 'OFFLINE_ALIPAY';
  1635. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1636. $payPrepayid = 'OFFLINE_WXPAY';
  1637. } else if ($params['pay_category'] == 'MONEY') {
  1638. $payPrepayid = 'MONEY';
  1639. } else if ($params['pay_category'] == 'CARD') {
  1640. $payPrepayid = $params['card_id'];
  1641. } else {
  1642. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  1643. }
  1644. $data = [
  1645. 'join_pay_member_id' => $params['join_order_member_id'],
  1646. 'join_pay_order_id' => $params['orderGroupId'],
  1647. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  1648. 'pay_category' => $goods['goods_classify'],
  1649. 'pay_amount' => $amountPay,
  1650. 'pay_prepayid' => $payPrepayid,
  1651. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1652. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $orderId]) : '[]',
  1653. 'pay_json_request' => json_encode($params),
  1654. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  1655. 'pay_remark' => $params['order_remark'] ?? '',
  1656. 'pay_addtimes' => time(),
  1657. ];
  1658. PayDetail::insert($data);
  1659. }
  1660. } catch (\Exception $e) {
  1661. dump($e->getMessage());
  1662. throw new BusinessException('创建支付记录失败');
  1663. }
  1664. }
  1665. /**
  1666. * @Desc 入配送记录
  1667. * @Author Gorden
  1668. * @Date 2024/9/23 10:45
  1669. *
  1670. * @param $params
  1671. * @return void
  1672. * @throws BusinessException
  1673. */
  1674. public function insertExpressOne($params, $orderId, $cardId = '')
  1675. {
  1676. if (isset($params['delivery']) && $params['delivery'] == 'LOGISTICS') {
  1677. $params['order_express_type'] = '配送';
  1678. // 入配送
  1679. $this->saveExpress($params, $orderId);
  1680. } else if (isset($params['delivery']) && $params['delivery'] == 'PICKUP') {
  1681. $params['order_express_type'] = '自提';
  1682. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  1683. if (!$premises) {
  1684. throw new BusinessException('自提门店不存在');
  1685. }
  1686. $params['order_express_city'] = $premises->dept_city;
  1687. $params['order_express_address'] = $premises->dept_address;
  1688. $params['order_express_telephone'] = $premises->dept_telephone;
  1689. $orderExpressExtendJson = ['pick_code' => random_string(4, 'number')];
  1690. if (!empty($cardId)) {
  1691. $orderExpressExtendJson['card_id'] = $cardId;
  1692. }
  1693. $params['order_express_extend_json'] = json_encode($orderExpressExtendJson);
  1694. $this->saveExpress($params, $orderId);
  1695. } else if (isset($params['delivery']) && $params['delivery'] == 'ARRIVAL') {
  1696. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  1697. $params['order_express_type'] = '到店';
  1698. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  1699. $params['order_express_type'] = '堂食';
  1700. }
  1701. if (!$premises) {
  1702. throw new BusinessException('门店不存在');
  1703. }
  1704. $params['order_express_city'] = $premises->dept_city;
  1705. $params['order_express_address'] = $premises->dept_address;
  1706. $params['order_express_telephone'] = $premises->dept_telephone;
  1707. $this->saveExpress($params, $orderId);
  1708. }
  1709. }
  1710. private function discountRecord($orderDiscountJson, $params)
  1711. {
  1712. $json = [];
  1713. if (!empty($orderDiscountJson)) {
  1714. $json = json_decode($orderDiscountJson, true);
  1715. }
  1716. try {
  1717. if (!empty($params['preferential']) && in_array('wipe', $params['preferential'])) {
  1718. if (intval($params['order_amount_total'] / 10) * 10 != $params['order_amount_pay']) {
  1719. throw new BusinessException("抹零后实际支付金额错误");
  1720. }
  1721. $params['order_discount_amount'] = $params['order_amount_total'] - $params['order_amount_pay'];
  1722. $couponClassifyDesc = $couponClassify = '抹零';
  1723. } elseif (!empty($params['preferential']) && in_array('custom', $params['preferential'])) {
  1724. if (sprintf("%.2f", $params['order_amount_total'] - $params['order_discount_amount']) != sprintf("%.2f", $params['order_amount_pay'])) {
  1725. throw new BusinessException("餐厅前台优惠后实际支付金额错误");
  1726. }
  1727. $couponClassifyDesc = $couponClassify = '餐厅前台优惠';
  1728. } else if (!empty($params['preferential']) && intval($params['preferential'][0]) < 100 && intval($params['preferential'][0]) >= 50) {
  1729. if (($params['order_amount_total'] * intval($params['preferential'][0])) / 100 != $params['order_amount_pay']) {
  1730. throw new BusinessException("折扣后实际支付金额错误");
  1731. }
  1732. $couponClassify = '折扣';
  1733. $couponClassifyDesc = intval($params['preferential'][0]) / 10 . '折';
  1734. $params['order_discount_amount'] = $params['order_amount_total'] - $params['order_amount_pay'];
  1735. } else {
  1736. return $json;
  1737. }
  1738. $json[date('Y-m-d H:i:s')] = [
  1739. 'coupon_id' => $params['coupon_id'] ?? null,
  1740. 'coupon_value' => $params['order_discount_amount'] ?? '',
  1741. 'coupon_classify' => $couponClassify,
  1742. 'coupon_detail_id' => [$couponClassifyDesc],
  1743. 'coupon_classify_en' => $params['preferential']
  1744. ];
  1745. return $json;
  1746. } catch (BusinessException $e) {
  1747. throw new BusinessException($e->getMessage());
  1748. } catch (\Exception $e) {
  1749. dump($e->getMessage());
  1750. throw new BusinessException("优惠数据错误");
  1751. }
  1752. }
  1753. /**
  1754. * @Desc 修改优惠券状态
  1755. * @Author Gorden
  1756. * @Date 2024/9/19 9:03
  1757. *
  1758. * @param $coupon
  1759. * @param $status
  1760. * @return void
  1761. */
  1762. private function changeOrderCouponStatus($coupon, $status)
  1763. {
  1764. if (!empty($coupon) && is_array($coupon)) {
  1765. $updateData['coupon_detail_status'] = $status;
  1766. if ($status == 'ACTIVED' || $status == 'WAITING') {
  1767. $updateData['coupon_detail_used_datetime'] = '';
  1768. } elseif ($status == 'USED') {
  1769. $updateData['coupon_detail_used_datetime'] = date('Y-m-d H:i:s');
  1770. }
  1771. foreach ($coupon as $item) {
  1772. if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
  1773. foreach ($item['coupon_detail_id'] as $detailId) {
  1774. CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
  1775. ->where('coupon_detail_id', $detailId)->update($updateData);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. }
  1781. /**
  1782. * @Desc 物流表加储值卡号
  1783. * @Author Gorden
  1784. * @Date 2024/12/9 16:38
  1785. *
  1786. * @param $orderId
  1787. * @param $cardId
  1788. * @return void
  1789. */
  1790. private function addCardIdToExpress($orderId, $cardId)
  1791. {
  1792. $express = OrderExpress::where('join_express_order_id', $orderId)->first();
  1793. $expressExtendJson = [];
  1794. if (!empty($express->order_express_extend_json)) {
  1795. $expressExtendJson = json_decode($express->order_express_extend_json, true);
  1796. $expressExtendJson['card_id'] = $cardId;
  1797. }
  1798. $express->order_express_extend_json = json_encode($expressExtendJson);
  1799. $express->save();
  1800. }
  1801. }