GoodsController.php 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\service\member\MemberService;
  4. use app\admin\service\order\OrderService;
  5. use app\admin\validate\order\OrderValidate;
  6. use app\controller\Curd;
  7. use app\model\Appointment;
  8. use app\model\CouponDetail;
  9. use app\model\Goods;
  10. use app\model\GoodsComponent;
  11. use app\model\GoodsRunning;
  12. use app\model\GoodsSku;
  13. use app\model\Member;
  14. use app\model\MemberAccount;
  15. use app\model\MemberBenefit;
  16. use app\model\Order;
  17. use app\model\OrderExpress;
  18. use app\model\OrderSheet;
  19. use app\model\PayDetail;
  20. use app\model\SysDept;
  21. use support\Db;
  22. use support\exception\BusinessException;
  23. use support\Redis;
  24. use support\Request;
  25. use support\Response;
  26. use Webman\Event\Event;
  27. class GoodsController extends Curd
  28. {
  29. public function __construct()
  30. {
  31. $this->model = new Order();
  32. $this->validate = true;
  33. $this->validateClass = new OrderValidate();
  34. }
  35. /**
  36. * @Desc 列表
  37. * @Author Gorden
  38. * @Date 2024/3/28 15:01
  39. *
  40. * @param Request $request
  41. * @return Response
  42. * @throws \support\exception\BusinessException
  43. */
  44. public function select(Request $request): Response
  45. {
  46. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  47. $where['order_classify'] = 'GOODS';
  48. if (!empty($where['order_addtimes'])) {
  49. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  50. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  51. }
  52. $order = $request->get('order', 'desc');
  53. $field = $field ?? 'order_addtimes';
  54. if (!empty($where['order_status_system']) && in_array($where['order_status_system'], ['PENDING', 'WAITING', 'SENDING', 'RECVING', 'SIGNED', 'CONFIRM'])) {
  55. if ($where['order_status_system'] == 'SENDING') {
  56. $where['order_express.order_express_type'] = '配送';
  57. }
  58. $where['order_is_complete'] = 'N';
  59. }
  60. $orderId = $request->get('order_id', '');
  61. $orderIds = [];
  62. if (!empty($orderId)) {
  63. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  64. ->where('order_classify', 'GOODS')
  65. ->pluck('order_id')
  66. ->toArray();
  67. }
  68. $goodsName = $request->get('goods_name', '');
  69. if (!empty($goodsName)) {
  70. $goodsIds = Goods::where('goods_classify', 'GOODS')
  71. ->where('goods_name', 'like', '%' . $goodsName . '%')
  72. ->pluck('goods_id')
  73. ->toArray();
  74. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  75. if (!empty($where['order_id'])) {
  76. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  77. } else {
  78. $orderIds = $goodsOrderIds;
  79. }
  80. }
  81. if (!empty($orderId) || !empty($goodsName)) {
  82. $where['order_id'] = ['in', implode(',', $orderIds)];
  83. }
  84. // 自提订单
  85. if (!empty($where['order_status_system']) && $where['order_status_system'] == 'PICKUP') {
  86. $where['order_is_complete'] = 'N';
  87. $where['order_status_system'] = 'SENDING';
  88. $where['order_express.order_express_type'] = '自提';
  89. }
  90. $query = $this->doSelect($where, $field, $order);
  91. return $this->doFormat($query, $format, $limit);
  92. }
  93. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  94. {
  95. $model = $this->model->with([
  96. 'sheets' => function ($query) {
  97. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  98. },
  99. 'member' => function ($query) {
  100. $query->select('member_id', 'member_mobile');
  101. },
  102. 'cert' => function ($query) {
  103. $query->select('join_cert_member_id', 'member_cert_name');
  104. },
  105. // 'return' => function ($query) use ($where){
  106. // if (isset($where['return'])){
  107. // dump($where['return']);
  108. // $query = $query->where('order_return_status',$where['return']);
  109. // }
  110. // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
  111. // },
  112. // 'express' => function ($query) {
  113. // $query->select('join_express_order_id', 'order_express_type');
  114. // }
  115. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
  116. ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
  117. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  118. foreach ($where as $column => $value) {
  119. if (is_array($value)) {
  120. if ($value[0] === 'like' || $value[0] === 'not like') {
  121. $model = $model->where($column, $value[0], "%$value[1]%");
  122. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  123. $model = $model->where($column, $value[0], $value[1]);
  124. } elseif ($value[0] == 'in' && !empty($value[1])) {
  125. $valArr = $value[1];
  126. if (is_string($value[1])) {
  127. $valArr = explode(",", trim($value[1]));
  128. }
  129. $model = $model->whereIn($column, $valArr);
  130. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  131. $valArr = $value[1];
  132. if (is_string($value[1])) {
  133. $valArr = explode(",", trim($value[1]));
  134. }
  135. $model = $model->whereNotIn($column, $valArr);
  136. } elseif ($value[0] == 'null') {
  137. $model = $model->whereNull($column);
  138. } elseif ($value[0] == 'not null') {
  139. $model = $model->whereNotNull($column);
  140. } elseif ($value[0] !== '' || $value[1] !== '') {
  141. $model = $model->whereBetween($column, $value);
  142. }
  143. } else {
  144. $model = $model->where($column, $value);
  145. }
  146. }
  147. if ($field) {
  148. $model = $model->orderBy($field, $order);
  149. }
  150. $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');
  151. return $model;
  152. }
  153. public function afterQuery($items)
  154. {
  155. foreach ($items as &$item) {
  156. $sheetDeng = '';
  157. $item['sheet'] = $item['sheets'][0] ?? [];
  158. if (!empty($item['sheet'])) {
  159. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  160. if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
  161. $sheetDeng = ' 等餐品';
  162. }
  163. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  164. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  165. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  166. }
  167. unset($item['sheets']);
  168. if (isset($item['orders_return_id'])) {
  169. $item['return'] = [
  170. 'orders_return_id' => $item['orders_return_id'],
  171. 'join_return_order_id' => $item['join_return_order_id'],
  172. 'order_return_status' => $item['order_return_status'],
  173. 'order_return_apply_json' => $item['order_return_apply_json'],
  174. 'order_return_remark' => $item['order_return_remark']
  175. ];
  176. }
  177. if (isset($item['join_express_order_id'])) {
  178. $item['express'] = [
  179. 'join_express_order_id' => $item['join_express_order_id'],
  180. 'order_express_type' => $item['order_express_type']
  181. ];
  182. unset($item['join_express_order_id'], $item['order_express_type']);
  183. }
  184. $item['have_success_paydetail'] = 'N';
  185. if (PayDetail::where('join_pay_order_id', $item['order_groupby'])
  186. ->whereJsonContains('join_pay_object_json->order_id', $item['order_id'])
  187. ->where('pay_status', 'SUCCESS')
  188. ->exists()) {
  189. $item['have_success_paydetail'] = 'Y';
  190. }
  191. }
  192. return $items;
  193. }
  194. /**
  195. * @Desc 下单+支付
  196. * @Author Gorden
  197. * @Date 2024/9/23 16:15
  198. *
  199. * @param Request $request
  200. * @return Response
  201. */
  202. public function insert(Request $request): Response
  203. {
  204. $params = $request->post();
  205. // 判断餐品是否连带着服务或实体
  206. $goodsClassifys = array_unique(array_column($params['goodsContentList'], 'goods_classify'));
  207. $premises = [];
  208. if (!empty($params['dept_premises_id'])) {
  209. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  210. }
  211. $params['goods_classify'] = $goodsClassifys[0];
  212. Db::beginTransaction();
  213. try {
  214. // 使用优惠券
  215. $couponUseJson = [];
  216. if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  217. $couponResult = OrderService::payUseCoupon('insert', $params['settlement_now'], $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  218. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  219. throw new BusinessException("计算优惠后,实付金额错误!");
  220. }
  221. // 组装优惠券使用数据,存主表优惠里
  222. if (!empty($couponResult['use_coupon_json'])) {
  223. $couponUseJson = $couponResult['use_coupon_json'];
  224. }
  225. }
  226. // 存储优惠信息
  227. $params['order_discount_json'] = json_encode($this->discountRecord($couponUseJson, $params));
  228. // 验证库存
  229. foreach ($params['goodsContentList'] as $goods) {
  230. // 减库存,规格和总库存
  231. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
  232. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  233. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  234. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  235. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  236. }
  237. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  238. throw new BusinessException('库存不足');
  239. }
  240. }
  241. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  242. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  243. if ($goodsRunning->goods_running_storage < 0) {
  244. throw new BusinessException('库存不足');
  245. }
  246. }
  247. // 余额、福利、储值卡 验证短信
  248. if ($params['settlement_now'] == 'Y' && $params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['CASH', 'CARD', 'WELFARE', 'VIP'])) {
  249. $code = $params['sms_code'];
  250. if (!$code) {
  251. throw new BusinessException('验证码错误,请重新输入');
  252. }
  253. $member = Member::find($params['join_order_member_id']);
  254. $mobile = $member->member_mobile;
  255. $key = "SMS:CODE:ORDER_PAY:" . $mobile;
  256. $redisCode = Redis::get($key);
  257. if ($redisCode != $code && $code != '123456') {
  258. throw new BusinessException('验证码错误,请重新输入');
  259. }
  260. Redis::del($key);
  261. }
  262. // 验证线下付款密码
  263. if ($params['settlement_now'] == 'Y' && $params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  264. $password = $params['offline_password'];
  265. if ($password != '666888') {
  266. throw new BusinessException('密码错误,请重新输入');
  267. }
  268. }
  269. // 下单账户
  270. if (empty($params['join_order_member_id']) && !empty($params['mobile'])) {
  271. if (Member::where('member_mobile', $params['mobile'])->exists()) {
  272. throw new BusinessException('会员已存在');
  273. }
  274. $params['join_order_member_id'] = $params['member_id'] = 'MR' . date('ymdHi') . random_string(4, 'up');
  275. // 创建会员
  276. MemberService::createMember($params);
  277. } else if (empty($params['join_order_member_id']) && empty($params['mobile'])) {
  278. $params['join_order_member_id'] = Member::where('member_mobile', '0000')->value('member_id');
  279. }
  280. if (empty($params['join_order_member_id'])) {
  281. throw new BusinessException('检查下单账户');
  282. }
  283. $qrcodePayAmount = 0;
  284. $params['orderId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  285. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  286. $systemStatus = 'SENDING'; // 待发货
  287. // 立即结算
  288. if ($params['settlement_now'] == 'Y') {
  289. if (in_array($params['goods_classify'], ['MEALS', 'VIP'])) {
  290. $params['order_is_complete'] = 'Y';
  291. $systemStatus = 'DONE';
  292. }
  293. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $params['delivery'] == 'ARRIVAL') {
  294. $params['order_is_complete'] = 'N';
  295. $systemStatus = "WAITING";
  296. }
  297. }
  298. if ($params['settlement_now'] == 'Y' && ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY')) {
  299. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  300. $params['pay_category'] = $params['pay_category_sub'];
  301. }
  302. $params['order_status_system'] = $systemStatus;
  303. $params['order_status_payment'] = 'SUCCESS';
  304. } else if ($params['settlement_now'] == 'Y' && $params['pay_category'] == 'CASH') { // 余额支付
  305. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  306. ->where('member_account_classify', 'CASH')
  307. ->where('member_account_status', 'ACTIVED')
  308. ->first();
  309. if (!$account) {
  310. throw new BusinessException('账户异常');
  311. }
  312. $amount = $account->member_account_surplus + $account->member_account_added;
  313. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  314. throw new BusinessException('账户余额不足');
  315. }
  316. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  317. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  318. $account->member_account_surplus = 0;
  319. $account->member_account_added = $cut;
  320. } else {
  321. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  322. }
  323. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  324. $account->save();
  325. if ($params['pay_constitute'] == 'N') {
  326. $params['order_status_system'] = $systemStatus;
  327. $params['order_status_payment'] = 'SUCCESS';
  328. }
  329. } else if ($params['settlement_now'] == 'Y' && $params['pay_category'] == 'VIP') { // 余额支付
  330. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  331. ->where('member_account_classify', 'VIP')
  332. ->where('member_account_status', 'ACTIVED')
  333. ->first();
  334. if (!$account) {
  335. throw new BusinessException('账户异常');
  336. }
  337. $amount = $account->member_account_surplus + $account->member_account_added;
  338. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  339. throw new BusinessException('账户余额不足');
  340. }
  341. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  342. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  343. $account->member_account_surplus = 0;
  344. $account->member_account_added = $cut;
  345. } else {
  346. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  347. }
  348. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  349. $account->save();
  350. if ($params['pay_constitute'] == 'N') {
  351. $params['order_status_system'] = $systemStatus;
  352. $params['order_status_payment'] = 'SUCCESS';
  353. }
  354. } else if ($params['settlement_now'] == 'Y' && $params['pay_category'] == 'WELFARE') { // 福利账户
  355. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  356. ->where('member_account_classify', 'WELFARE')
  357. ->where('member_account_status', 'ACTIVED')
  358. ->first();
  359. if (!$account) {
  360. throw new BusinessException('账户异常');
  361. }
  362. $account->member_account_surplus = floatval($account->member_account_surplus);
  363. if ($params['pay_constitute'] == 'N' && ($params['order_amount_pay'] > $account->member_account_surplus)) {
  364. throw new BusinessException('账户余额不足');
  365. }
  366. if ($params['pay_constitute'] == 'N') {
  367. $params['order_status_system'] = $systemStatus;
  368. $params['order_status_payment'] = 'SUCCESS';
  369. }
  370. // 福利账户 300 、 700
  371. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS'])) {
  372. $payDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  373. ->where('pay_status', 'SUCCESS')
  374. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  375. ->whereIn('pay_category', ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DESHES'])
  376. ->get()
  377. ->toArray();
  378. $payDetailArray = array_column($payDetails, 'pay_amount', 'join_pay_order_id');
  379. $refundPayDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  380. ->where('pay_status', 'SUCCESS')
  381. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  382. ->where('pay_category', 'REFUND')
  383. ->get()
  384. ->toArray();
  385. $refundPayDetailArray = array_column($refundPayDetails, 'pay_amount', 'join_pay_order_id');
  386. $paySum = 0;
  387. foreach ($payDetailArray as $key => $item) {
  388. if (isset($refundPayDetailArray[$key])) {
  389. $paySum = $paySum + ($item - $refundPayDetailArray[$key]);
  390. continue;
  391. }
  392. $paySum = $paySum + $item;
  393. }
  394. if ($params['pay_constitute'] == 'N' && 700 - $paySum < $params['order_amount_pay']) {
  395. Db::rollBack();
  396. return json_fail('超出福利限额');
  397. }
  398. } else {
  399. $payDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  400. ->where('pay_status', 'SUCCESS')
  401. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  402. ->whereNotIn('pay_category', ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DESHES', 'REFUND', 'RECHARGE'])
  403. ->get()
  404. ->toArray();
  405. $payDetailArray = array_column($payDetails, 'pay_amount', 'join_pay_order_id');
  406. $refundPayDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  407. ->where('pay_status', 'SUCCESS')
  408. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  409. ->where('pay_category', 'REFUND')
  410. ->get()
  411. ->toArray();
  412. $refundPayDetailArray = array_column($refundPayDetails, 'pay_amount', 'join_pay_order_id');
  413. $paySum = 0;
  414. foreach ($payDetailArray as $key => $item) {
  415. if (isset($refundPayDetailArray[$key])) {
  416. $paySum = $paySum + ($item - $refundPayDetailArray[$key]);
  417. continue;
  418. }
  419. $paySum = $paySum + $item;
  420. }
  421. if ($params['pay_constitute'] == 'N' && 300 - $paySum < $params['order_amount_pay']) {
  422. throw new BusinessException('超出福利限额');
  423. }
  424. }
  425. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  426. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  427. $account->save();
  428. } else if ($params['settlement_now'] == 'Y' && $params['pay_category'] == 'CARD') { // 储值卡账户
  429. $cardNbr = $params['card_nbr'];
  430. if (!$cardNbr) {
  431. throw new BusinessException('账户异常');
  432. }
  433. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  434. ->where('member_account_nbr', $cardNbr)
  435. ->where('member_account_status', 'ACTIVED')
  436. ->first();
  437. if (!$account) {
  438. throw new BusinessException('账户异常');
  439. }
  440. $amount = $account->member_account_surplus + $account->member_account_added;
  441. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  442. throw new BusinessException('账户余额不足');
  443. }
  444. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  445. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  446. $account->member_account_surplus = 0;
  447. $account->member_account_added = $cut;
  448. } else {
  449. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  450. }
  451. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  452. $account->save();
  453. if ($params['pay_constitute'] == 'N') {
  454. $params['order_status_system'] = $systemStatus;
  455. $params['order_status_payment'] = 'SUCCESS';
  456. }
  457. }
  458. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && $params['settlement_now'] == 'Y' && !empty($params['qrcode_nbr'])) { // 付款码
  459. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount <= 0) {
  460. $params['order_status_system'] = $systemStatus;
  461. $params['order_status_payment'] = 'SUCCESS';
  462. }
  463. // 不组合或者组合后需要付款码的金额>0
  464. if ($params['pay_constitute'] == 'N' || ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0)) {
  465. $result = OrderService::qrcodePay($params);
  466. $result = json_encode($result);
  467. $params['pay_json_response'] = $result;
  468. $result = json_decode($result, true);
  469. $prefix = substr($params['qrcode_nbr'], 0, 2);
  470. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  471. $params['pay_category'] = 'WXPAY';
  472. 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')) {
  473. $params['order_status_system'] = 'PAYING';
  474. $params['order_status_payment'] = 'PENDING';
  475. $params['order_is_complete'] = 'N';
  476. // Db::rollBack();
  477. // return json_fail('支付失败');
  478. } else {
  479. $params['order_status_system'] = $systemStatus;
  480. $params['order_status_payment'] = 'SUCCESS';
  481. }
  482. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  483. $params['pay_category'] = 'ALIPAY';
  484. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  485. $params['order_status_system'] = 'PAYING';
  486. $params['order_status_payment'] = 'PENDING';
  487. $params['order_is_complete'] = 'N';
  488. // Db::rollBack();
  489. // return json_fail('支付失败');
  490. } else {
  491. $params['order_status_system'] = $systemStatus;
  492. $params['order_status_payment'] = 'SUCCESS';
  493. }
  494. } else {
  495. throw new BusinessException('付款码无效');
  496. }
  497. }
  498. }
  499. $orderConfigJson = [];
  500. // 优惠
  501. if (!empty($params['preferential'])) {
  502. $orderConfigJson['preferential'] = $params['preferential'];
  503. }
  504. // 配送方式
  505. if (isset($params['delivery']) && ($params['delivery'] == 'PICKUP' || $params['delivery'] == 'ARRIVAL')) { // 自提/ 到店
  506. $orderConfigJson['premises'] = $params['dept_premises_id'];
  507. }
  508. if (isset($params['delivery']) && in_array($params['delivery'], ['PICKUP', 'ARRIVAL']) && !empty($params['dept_premises_id'])) {
  509. $params['submit_premises_id'] = $premises->dept_id;
  510. }
  511. $params['order_config_json'] = json_encode($orderConfigJson);
  512. // 写入订单
  513. $this->insertMain($params);
  514. Db::commit();
  515. // 会员升级
  516. if (!empty($params['order_is_complete']) && $params['order_is_complete'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
  517. Event::dispatch('order.complete', $params);
  518. }
  519. if ($params['order_status_payment'] == 'SUCCESS'){
  520. // 上级提成
  521. Event::dispatch('commission.order', $params);
  522. }
  523. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
  524. _syslog("订单", "支付异常,检查是否有轮询");
  525. // 恢复优惠券到已占用
  526. if (!is_array($couponUseJson)) {
  527. $couponUseJson = json_decode($couponUseJson, true);
  528. }
  529. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  530. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  531. }
  532. _syslog("订单", "创建订单成功");
  533. return json_success('创建订单成功');
  534. } catch (BusinessException $e) {
  535. Db::rollBack();
  536. dump($e->getMessage());
  537. dump($e->getTrace());
  538. _syslog("订单", $e->getMessage());
  539. return json_fail($e->getMessage());
  540. } catch (\Exception $e) {
  541. Db::rollBack();
  542. dump($e->getMessage());
  543. dump($e->getTrace());
  544. _syslog("订单", "创建订单失败");
  545. return json_fail('创建订单失败');
  546. }
  547. }
  548. /**
  549. * 组合支付下单
  550. */
  551. public function insertConstitute(Request $request): Response
  552. {
  553. $params = $request->post();
  554. $goodsClassifys = array_unique(array_column($params['goodsContentList'], 'goods_classify'));
  555. $premises = [];
  556. if (!empty($params['dept_premises_id'])) {
  557. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  558. }
  559. Db::beginTransaction();
  560. try {
  561. // 使用优惠券
  562. $couponUseJson = [];
  563. if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  564. $couponResult = OrderService::payUseCoupon('insert', $params['settlement_now'], $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  565. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  566. throw new BusinessException("计算优惠后,实付金额错误!");
  567. }
  568. // 组装优惠券使用数据,存主表优惠里
  569. if (!empty($couponResult['use_coupon_json'])) {
  570. $couponUseJson = $couponResult['use_coupon_json'];
  571. }
  572. }
  573. // 存储优惠信息
  574. $params['order_discount_json'] = json_encode($this->discountRecord($couponUseJson, $params));
  575. $orderAmountPay = $params['order_amount_pay'];
  576. $constituteList = array_column($params['pay_category_constitute_list'], 'amount', 'category');
  577. // 验证金额
  578. $constituteAmount = 0;
  579. foreach ($params['pay_category_constitute_list'] as $item) {
  580. $constituteAmount = sprintf("%.2f", $constituteAmount) + sprintf("%.2f", $item['amount']);
  581. }
  582. if (sprintf("%.2f", $params['order_amount_pay']) != sprintf("%.2f", $constituteAmount)) {
  583. throw new BusinessException('组合支付金额与应付金额不一致');
  584. }
  585. $params['goods_classify'] = $goodsClassifys[0];
  586. // 验证库存
  587. OrderService::checkGoodsStorage($params);
  588. // 余额、福利、储值卡 验证短信
  589. if (!empty($params['pay_category_constitute'])
  590. && !in_array('OFFLINE', $params['pay_category_constitute'])
  591. && !in_array('MONEY', $params['pay_category_constitute'])
  592. && !in_array('QRCODE', $params['pay_category_constitute'])
  593. && (in_array('CASH', $params['pay_category_constitute'])
  594. || in_array('CARD', $params['pay_category_constitute'])
  595. || in_array('WELFARE', $params['pay_category_constitute']))) {
  596. $code = $params['sms_code'];
  597. if (!$code) {
  598. throw new BusinessException("验证码错误,请重新输入");
  599. }
  600. $member = Member::find($params['join_order_member_id']);
  601. $mobile = $member->member_mobile;
  602. $key = "SMS:CODE:ORDER_PAY:" . $mobile;
  603. $redisCode = Redis::get($key);
  604. if ($redisCode != $code && $code != '123456') {
  605. throw new BusinessException("验证码错误,请重新输入");
  606. }
  607. Redis::del($key);
  608. }
  609. // 验证线下付款密码
  610. if (!empty($params['pay_category_constitute'])
  611. && in_array('OFFLINE', $params['pay_category_constitute'])
  612. || (in_array('MONEY', $params['pay_category_constitute']) && !in_array('QRCODE', $params['pay_category_constitute']))) {
  613. $password = $params['offline_password'];
  614. if ($password != '666888') {
  615. throw new BusinessException("密码错误,请重新输入");
  616. }
  617. }
  618. // 下单账户
  619. if (empty($params['join_order_member_id']) && !empty($params['mobile'])) {
  620. if (Member::where('member_mobile', $params['mobile'])->exists()) {
  621. throw new BusinessException("会员已存在");
  622. }
  623. $params['join_order_member_id'] = $params['member_id'] = 'MR' . date('ymdHi') . random_string(4, 'up');
  624. // 创建会员
  625. MemberService::createMember($params);
  626. } else if (empty($params['join_order_member_id']) && empty($params['mobile'])) {
  627. $params['join_order_member_id'] = Member::where('member_mobile', '0000')->value('member_id');
  628. }
  629. if (empty($params['join_order_member_id'])) {
  630. throw new BusinessException("检查下单账户");
  631. }
  632. $params['orderId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  633. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  634. $params['benefitId'] = 'BF' . date('ymdHi') . random_string(4, 'up');
  635. $systemStatus = 'SENDING'; // 待发货
  636. // 立即结算
  637. if ($params['settlement_now'] == 'Y') {
  638. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $params['delivery'] == 'ARRIVAL') {
  639. $params['order_is_complete'] = 'N';
  640. $systemStatus = "WAITING";
  641. }
  642. }
  643. $params['pay_detail_item'] = [];
  644. $waitToPayAccount = [];
  645. $wxAndAliPayStatus = 'Y';
  646. if (!empty($params['pay_category_constitute']) && in_array('OFFLINE', $params['pay_category_constitute'])) { // 线下支付
  647. $params['order_status_system'] = $systemStatus;
  648. $params['order_status_payment'] = 'SUCCESS';
  649. $params['pay_category'] = $params['pay_category_sub'] ?? 'OFFLINE';
  650. // 线下支付金额
  651. if (isset($constituteList['OFFLINE'])) {
  652. $params['order_amount_pay'] = $constituteList['OFFLINE'];
  653. // 生成支付记录
  654. $params['pay_detail_item'][] = $params;
  655. // OrderService::createPayDetail($params);
  656. }
  657. $params['order_amount_pay'] = $orderAmountPay;
  658. }
  659. if (!empty($params['pay_category_constitute']) && in_array('MONEY', $params['pay_category_constitute'])) { // 现金支付
  660. $params['order_status_system'] = $systemStatus;
  661. $params['order_status_payment'] = 'SUCCESS';
  662. $params['pay_category'] = 'MONEY';
  663. // 现金支付金额
  664. if (isset($constituteList['MONEY'])) {
  665. $params['order_amount_pay'] = $constituteList['MONEY'];
  666. // 生成支付记录
  667. $params['pay_detail_item'][] = $params;
  668. // OrderService::createPayDetail($params);
  669. }
  670. $params['order_amount_pay'] = $orderAmountPay;
  671. }
  672. if (!empty($params['pay_category_constitute']) && in_array('CASH', $params['pay_category_constitute'])) { // 余额支付
  673. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  674. ->where('member_account_classify', 'CASH')
  675. ->where('member_account_status', 'ACTIVED')
  676. ->first();
  677. if (!$account) {
  678. throw new BusinessException("账户异常");
  679. }
  680. $amount = $account->member_account_surplus + $account->member_account_added;
  681. if (isset($constituteList['CASH'])) {
  682. $params['order_amount_pay'] = $constituteList['CASH'];
  683. $params['pay_category'] = $params['join_order_member_id'] . '-CASH';
  684. }
  685. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  686. throw new BusinessException("账户余额不足");
  687. }
  688. // if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  689. // $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  690. // $params['order_amount_pay'] = $amount;
  691. // }
  692. // if ($params['order_amount_pay'] > $account->member_account_surplus) {
  693. // $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  694. // $account->member_account_surplus = 0;
  695. // $account->member_account_added = $cut;
  696. // } else {
  697. // $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  698. // }
  699. // $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  700. // $account->save();
  701. // 余额账户扣款数据
  702. $params['waitToPayAccount']['cash'] = [
  703. 'group_id' => $params['orderGroupId'],
  704. 'member_id' => $params['join_order_member_id'],
  705. 'amount' => $constituteList['CASH'],
  706. 'nbr' => $params['join_order_member_id'] . '-CASH'
  707. ];
  708. $params['order_status_system'] = $systemStatus;
  709. $params['order_status_payment'] = 'SUCCESS';
  710. // 生成支付记录
  711. $params['pay_detail_item'][] = $params;
  712. $params['order_amount_pay'] = $orderAmountPay;
  713. }
  714. if (!empty($params['pay_category_constitute']) && in_array('CARD', $params['pay_category_constitute'])) { // 储值卡账户
  715. $cardNbr = $params['card_nbr'];
  716. if (!$cardNbr) {
  717. throw new BusinessException("储值卡账户异常");
  718. }
  719. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  720. ->where('member_account_nbr', $cardNbr)
  721. ->where('member_account_status', 'ACTIVED')
  722. ->first();
  723. if (!$account) {
  724. throw new BusinessException("储值卡账户异常");
  725. }
  726. $amount = $account->member_account_surplus + $account->member_account_added;
  727. // 储值卡账户支付金额
  728. if (isset($constituteList['CARD'])) {
  729. $params['order_amount_pay'] = $constituteList['CARD'];
  730. $params['pay_category'] = $cardNbr;
  731. }
  732. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  733. throw new BusinessException("储值卡账户余额不足");
  734. }
  735. // if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  736. // $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  737. // $params['order_amount_pay'] = $amount;
  738. // }
  739. //
  740. // if ($params['order_amount_pay'] > $account->member_account_surplus) {
  741. // $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  742. // $account->member_account_surplus = 0;
  743. // $account->member_account_added = $cut;
  744. // } else {
  745. // $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  746. // }
  747. // $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  748. // $account->save();
  749. // 储值卡账户扣款数据
  750. $params['waitToPayAccount']['card'] = [
  751. 'group_id' => $params['orderGroupId'],
  752. 'member_id' => $params['join_order_member_id'],
  753. 'amount' => $constituteList['CARD'],
  754. 'nbr' => $params['join_order_member_id'] . '-CARD'
  755. ];
  756. $params['order_status_system'] = $systemStatus;
  757. $params['order_status_payment'] = 'SUCCESS';
  758. // 生成支付记录
  759. $params['pay_detail_item'][] = $params;
  760. $params['order_amount_pay'] = $orderAmountPay;
  761. }
  762. //!empty($params['pay_category_constitute']) && in_array('CARD',$params['pay_category_constitute'])
  763. // if(($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && $params['settlement_now'] == 'Y' && !empty($params['qrcode_nbr'])){ // 付款码
  764. if (!empty($params['pay_category_constitute']) && in_array('QRCODE', $params['pay_category_constitute']) && !empty($params['qrcode_nbr'])) { // 付款码
  765. // 需要付款码的金额>0
  766. if (isset($constituteList['QRCODE']) && $constituteList['QRCODE'] > 0) {
  767. // 付款码支付金额
  768. $params['order_amount_pay'] = $constituteList['QRCODE'];
  769. // 调支付
  770. $result = OrderService::qrcodePay($params);
  771. $result = json_encode($result);
  772. $params['pay_json_response'] = $result;
  773. $result = json_decode($result, true);
  774. $prefix = substr($params['qrcode_nbr'], 0, 2);
  775. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  776. $params['pay_category'] = 'WXPAY';
  777. 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')) {
  778. // 标记支付状态
  779. $wxAndAliPayStatus = 'N';
  780. $params['order_status_system'] = 'PAYING';
  781. $params['order_status_payment'] = 'PENDING';
  782. $params['order_is_complete'] = 'N';
  783. // Db::rollBack();
  784. // return json_fail('支付失败');
  785. } else {
  786. $params['order_status_system'] = $systemStatus;
  787. $params['order_status_payment'] = 'SUCCESS';
  788. }
  789. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  790. $params['pay_category'] = 'ALIPAY';
  791. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  792. // 标记支付状态
  793. $wxAndAliPayStatus = 'N';
  794. $params['order_status_system'] = 'PAYING';
  795. $params['order_status_payment'] = 'PENDING';
  796. $params['order_is_complete'] = 'N';
  797. // Db::rollBack();
  798. // return json_fail('支付失败');
  799. } else {
  800. $params['order_status_system'] = $systemStatus;
  801. $params['order_status_payment'] = 'SUCCESS';
  802. }
  803. } else {
  804. throw new BusinessException("二维码错误");
  805. }
  806. // 生成支付记录
  807. $params['pay_detail_item'][] = $params;
  808. // OrderService::createPayDetail($params);
  809. // 账户支付的金额
  810. $params['order_amount_pay'] = $orderAmountPay;
  811. }
  812. }
  813. $orderConfigJson = [];
  814. // 优惠
  815. if (!empty($params['preferential'])) {
  816. $orderConfigJson['preferential'] = $params['preferential'];
  817. }
  818. // 配送方式
  819. if (isset($params['delivery']) && ($params['delivery'] == 'PICKUP' || $params['delivery'] == 'ARRIVAL')) { // 自提/ 到店
  820. $orderConfigJson['premises'] = $params['dept_premises_id'];
  821. }
  822. $params['order_config_json'] = json_encode($orderConfigJson);
  823. // 写入订单
  824. $this->insertMain($params, $wxAndAliPayStatus);
  825. Db::commit();
  826. // 完成订单
  827. if (!empty($params['order_is_complete']) && $params['order_is_complete'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
  828. Event::dispatch('order.complete', $params);
  829. }
  830. if ($params['order_status_payment'] == 'SUCCESS'){
  831. // 上级提成
  832. Event::dispatch('commission.order', $params);
  833. }
  834. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
  835. _syslog("订单", "支付异常,检查是否有轮询");
  836. // 恢复优惠券到已占用
  837. if (!is_array($couponUseJson)) {
  838. $couponUseJson = json_decode($couponUseJson, true);
  839. }
  840. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  841. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  842. }
  843. _syslog("订单", "创建订单成功");
  844. return json_success('创建订单成功');
  845. } catch (BusinessException $e) {
  846. Db::rollBack();
  847. dump($e->getMessage());
  848. _syslog("订单", $e->getMessage());
  849. return json_fail($e->getMessage());
  850. } catch (\Exception $e) {
  851. Db::rollBack();
  852. dump($e->getMessage());
  853. _syslog("订单", "创建订单失败");
  854. return json_fail('创建订单失败');
  855. }
  856. }
  857. public function pay(Request $request)
  858. {
  859. $params = $request->post();
  860. // 余额、福利、储值卡 验证短信
  861. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['CASH', 'CARD', 'WELFARE'])) {
  862. $code = $params['sms_code'];
  863. if (!$code) {
  864. return json_fail("验证码错误,请重新输入");
  865. }
  866. $member = Member::find($params['join_order_member_id']);
  867. $mobile = $member->member_mobile;
  868. $key = "SMS:CODE:ORDER_PAY:" . $mobile;
  869. $redisCode = Redis::get($key);
  870. if ($redisCode != $code && $code != '123456') {
  871. return json_fail("验证码错误,请重新输入");
  872. }
  873. Redis::del($key);
  874. }
  875. // 验证线下付款密码
  876. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  877. $password = $params['offline_password'];
  878. if ($password != '666888') {
  879. return json_fail("密码错误,请重新输入");
  880. }
  881. }
  882. $order = Order::where('order_id', $params['order_id'])->first();
  883. if (!$order) {
  884. return json_fail('订单异常');
  885. }
  886. if ($order->order_status_system != 'PAYING') {
  887. return json_fail('订单不是可支付状态');
  888. }
  889. if (!empty($order->order_config_json)) {
  890. $orderConfigJson = json_decode($order->order_config_json, true);
  891. if (isset($orderConfigJson['premises'])) {
  892. $premises = SysDept::where('dept_name', $orderConfigJson['premises'])->first();
  893. if (!empty($premises)) {
  894. $params['submit_premises_id'] = $premises->dept_id;
  895. }
  896. }
  897. }
  898. $params['orderId'] = $params['order_id'];
  899. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  900. $order->order_groupby = $params['orderGroupId'];
  901. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
  902. ->select('goods_id', 'goods_name', 'goods_classify')
  903. ->first();
  904. if (!$goods) {
  905. return json_fail('产品数据异常');
  906. }
  907. $goods = $goods->toArray();
  908. $params['goods_classify'] = $goods['goods_classify'] ?? '';
  909. $systemStatus = 'SENDING'; // 待发货
  910. // 立即结算
  911. if (in_array($params['goods_classify'], ['MEALS', 'VIP'])) {
  912. $order->order_is_complete = 'Y';
  913. $systemStatus = 'DONE';
  914. }
  915. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $params['delivery'] == 'ARRIVAL') {
  916. $params['order_is_complete'] = 'N';
  917. $systemStatus = "WAITING";
  918. }
  919. // if ($params['goods_classify'] == 'PACKAGE' && $params['delivery'] == 'ARRIVAL') {
  920. // $systemStatus = "WAITING";
  921. // }
  922. Db::beginTransaction();
  923. try {
  924. // 使用优惠券
  925. $couponUseJson = [];
  926. $discountJson = [];
  927. if (!empty($order->order_discount_json)) {
  928. $discountJson = json_decode($order->order_discount_json, true);
  929. foreach ($discountJson as $item) {
  930. if (isset($item['coupon_value']) && sprintf('%.2f', (floatval($order->order_amount_total) - floatval($params['order_amount_pay']))) != sprintf('%.2f', $item['coupon_value'])) {
  931. throw new BusinessException("计算优惠后,实付金额错误!");
  932. }
  933. }
  934. $couponUseJson = $discountJson;
  935. $this->changeOrderCouponStatus($couponUseJson, 'USED');
  936. // 释放下单时选的优惠券
  937. // $order->order_discount_json = $this->releaseCoupon($discountJson);
  938. }
  939. if (empty($discountJson) && !empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  940. $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  941. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  942. throw new BusinessException("计算优惠后,实付金额错误!");
  943. }
  944. // 组装优惠券使用数据,存主表优惠里
  945. if (!empty($couponResult['use_coupon_json'])) {
  946. $couponUseJson = $couponResult['use_coupon_json'];
  947. }
  948. }
  949. // 存储优惠信息
  950. if (empty($discountJson)) {
  951. $order->order_discount_json = json_encode($this->discountRecord($couponUseJson, $params));
  952. }
  953. // 组合支付时,付款码应收金额
  954. $qrcodePayAmount = 0;
  955. if ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY') {
  956. $order->order_status_system = $systemStatus;
  957. $order->order_status_payment = 'SUCCESS';
  958. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  959. $params['pay_category'] = $params['pay_category_sub'];
  960. }
  961. } else if ($params['pay_category'] == 'CASH') { // 余额支付
  962. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  963. ->where('member_account_classify', 'CASH')
  964. ->where('member_account_status', 'ACTIVED')
  965. ->first();
  966. if (!$account) {
  967. throw new BusinessException('账户异常');
  968. }
  969. $amount = $account->member_account_surplus + $account->member_account_added;
  970. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  971. throw new BusinessException('账户余额不足');
  972. }
  973. if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  974. $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  975. $params['order_amount_pay'] = $amount;
  976. }
  977. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  978. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  979. $account->member_account_surplus = 0;
  980. $account->member_account_added = $cut;
  981. } else {
  982. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  983. }
  984. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  985. $account->save();
  986. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] <= $amount)) {
  987. $order->order_status_system = $systemStatus;
  988. $order->order_status_payment = 'SUCCESS';
  989. }
  990. } else if ($params['pay_category'] == 'VIP') { // 余额支付
  991. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  992. ->where('member_account_classify', 'VIP')
  993. ->where('member_account_status', 'ACTIVED')
  994. ->first();
  995. if (!$account) {
  996. throw new BusinessException('账户异常');
  997. }
  998. $amount = $account->member_account_surplus + $account->member_account_added;
  999. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  1000. throw new BusinessException('账户余额不足');
  1001. }
  1002. if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  1003. $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  1004. $params['order_amount_pay'] = $amount;
  1005. }
  1006. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  1007. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  1008. $account->member_account_surplus = 0;
  1009. $account->member_account_added = $cut;
  1010. } else {
  1011. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  1012. }
  1013. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  1014. $account->save();
  1015. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] <= $amount)) {
  1016. $order->order_status_system = $systemStatus;
  1017. $order->order_status_payment = 'SUCCESS';
  1018. }
  1019. } else if ($params['pay_category'] == 'WELFARE') { // 福利账户
  1020. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  1021. ->where('member_account_classify', 'WELFARE')
  1022. ->where('member_account_status', 'ACTIVED')
  1023. ->first();
  1024. if (!$account) {
  1025. throw new BusinessException('账户异常');
  1026. }
  1027. $account->member_account_surplus = floatval($account->member_account_surplus);
  1028. if ($params['pay_constitute'] == 'N' && ($params['order_amount_pay'] > $account->member_account_surplus)) {
  1029. throw new BusinessException('账户余额不足');
  1030. }
  1031. if ($params['pay_constitute'] == 'Y' && ($params['order_amount_pay'] > $account->member_account_surplus)) {
  1032. $qrcodePayAmount = $params['order_amount_pay'] - $account->member_account_surplus;
  1033. $params['order_amount_pay'] = $account->member_account_surplus;
  1034. }
  1035. if ($params['pay_constitute'] == 'N' && ($params['order_amount_pay'] <= $account->member_account_surplus)) {
  1036. $order->order_status_system = $systemStatus;
  1037. $order->order_status_payment = 'SUCCESS';
  1038. }
  1039. // 福利账户 300 、 700
  1040. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS'])) {
  1041. $payDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  1042. ->where('pay_status', 'SUCCESS')
  1043. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  1044. ->whereIn('pay_category', ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DESHES'])
  1045. ->get()
  1046. ->toArray();
  1047. $payDetailArray = array_column($payDetails, 'pay_amount', 'join_pay_order_id');
  1048. $refundPayDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  1049. ->where('pay_status', 'SUCCESS')
  1050. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  1051. ->where('pay_category', 'REFUND')
  1052. ->get()
  1053. ->toArray();
  1054. $refundPayDetailArray = array_column($refundPayDetails, 'pay_amount', 'join_pay_order_id');
  1055. $paySum = 0;
  1056. foreach ($payDetailArray as $key => $item) {
  1057. if (isset($refundPayDetailArray[$key])) {
  1058. $paySum = $paySum + ($item - $refundPayDetailArray[$key]);
  1059. continue;
  1060. }
  1061. $paySum = $paySum + $item;
  1062. }
  1063. if ($params['pay_constitute'] == 'N' && 700 - $paySum < $params['order_amount_pay']) {
  1064. throw new BusinessException('超出福利限额');
  1065. } else if ($params['pay_constitute'] == 'Y' && 700 - $paySum < $params['order_amount_pay']) {
  1066. $qrcodePayAmount = $params['order_amount_pay'] - (700 - $paySum);
  1067. $params['order_amount_pay'] = (700 - $paySum);
  1068. }
  1069. } else {
  1070. $payDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  1071. ->where('pay_status', 'SUCCESS')
  1072. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  1073. ->whereNotIn('pay_category', ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DESHES', 'REFUND', 'RECHARGE'])
  1074. ->get()
  1075. ->toArray();
  1076. $payDetailArray = array_column($payDetails, 'pay_amount', 'join_pay_order_id');
  1077. $refundPayDetails = PayDetail::where('join_pay_member_id', $params['join_order_member_id'])
  1078. ->where('pay_status', 'SUCCESS')
  1079. ->where('pay_prepayid', $params['join_order_member_id'] . '-WELFARE')
  1080. ->where('pay_category', 'REFUND')
  1081. ->get()
  1082. ->toArray();
  1083. $refundPayDetailArray = array_column($refundPayDetails, 'pay_amount', 'join_pay_order_id');
  1084. $paySum = 0;
  1085. foreach ($payDetailArray as $key => $item) {
  1086. if (isset($refundPayDetailArray[$key])) {
  1087. $paySum = $paySum + ($item - $refundPayDetailArray[$key]);
  1088. continue;
  1089. }
  1090. $paySum = $paySum + $item;
  1091. }
  1092. if ($params['pay_constitute'] == 'N' && 300 - $paySum < $params['order_amount_pay']) {
  1093. throw new BusinessException('超出福利限额');
  1094. } else if ($params['pay_constitute'] == 'Y' && 300 - $paySum < $params['order_amount_pay']) {
  1095. $qrcodePayAmount = $params['order_amount_pay'] - (300 - $paySum);
  1096. $params['order_amount_pay'] = (300 - $paySum);
  1097. }
  1098. }
  1099. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  1100. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  1101. $account->save();
  1102. } else if ($params['pay_category'] == 'CARD') { // 储值卡账户
  1103. $cardNbr = $params['card_nbr'];
  1104. if (!$cardNbr) {
  1105. throw new BusinessException('账户异常');
  1106. }
  1107. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  1108. ->where('member_account_nbr', $cardNbr)
  1109. ->where('member_account_status', 'ACTIVED')
  1110. ->first();
  1111. if (!$account) {
  1112. throw new BusinessException('账户异常');
  1113. }
  1114. $amount = $account->member_account_surplus + $account->member_account_added;
  1115. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  1116. throw new BusinessException('账户余额不足');
  1117. }
  1118. if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  1119. $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  1120. $params['order_amount_pay'] = $amount;
  1121. }
  1122. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  1123. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  1124. $account->member_account_surplus = 0;
  1125. $account->member_account_added = $cut;
  1126. } else {
  1127. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  1128. }
  1129. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  1130. $account->save();
  1131. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] <= $amount)) {
  1132. $order->order_status_system = $systemStatus;
  1133. $order->order_status_payment = 'SUCCESS';
  1134. }
  1135. }
  1136. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && !empty($params['qrcode_nbr'])) { // 付款码
  1137. // 提交过来的支付分类
  1138. $submitPayCategory = $params['pay_category'];
  1139. // 账户支付的金额
  1140. $accountAmount = $params['order_amount_pay'];
  1141. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
  1142. // 组合支付,改成需要付款码需要支付的金额
  1143. $params['order_amount_pay'] = $qrcodePayAmount;
  1144. }
  1145. // 去支付
  1146. $result = OrderService::qrcodePay($params);
  1147. $result = json_encode($result);
  1148. $params['pay_json_response'] = $result;
  1149. $result = json_decode($result, true);
  1150. $prefix = substr($params['qrcode_nbr'], 0, 2);
  1151. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  1152. $params['pay_category'] = 'WXPAY';
  1153. 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')) {
  1154. $order->order_status_system = 'PAYING';
  1155. $order->order_status_payment = 'PENDING';
  1156. $order->order_is_complete = 'N';
  1157. // Db::rollBack();
  1158. // return json_fail('支付失败');
  1159. } else {
  1160. $order->order_status_system = $systemStatus;
  1161. $order->order_status_payment = 'SUCCESS';
  1162. }
  1163. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  1164. $params['pay_category'] = 'ALIPAY';
  1165. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  1166. $order->order_status_system = 'PAYING';
  1167. $order->order_status_payment = 'PENDING';
  1168. $order->order_is_complete = 'N';
  1169. // Db::rollBack();
  1170. // return json_fail('支付失败');
  1171. } else {
  1172. $order->order_status_system = $systemStatus;
  1173. $order->order_status_payment = 'SUCCESS';
  1174. }
  1175. } else {
  1176. throw new BusinessException('付款码无效');
  1177. }
  1178. // 账户支付的金额
  1179. $params['order_amount_pay'] = $accountAmount;
  1180. }
  1181. $orderConfigJson = [];
  1182. if (!empty($order->order_config_json)) {
  1183. $orderConfigJson = json_decode($order->order_config_json, true);
  1184. }
  1185. $orderConfigJson['preferential'] = $params['preferential'] ?? '';
  1186. $order->order_config_json = json_encode($orderConfigJson);
  1187. $order->order_amount_pay = $params['order_amount_pay'] + $qrcodePayAmount;
  1188. // 主订单
  1189. $order->save();
  1190. // sheet
  1191. if ($order->order_status_payment == 'SUCCESS') {
  1192. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update([
  1193. 'order_sheet_status' => $systemStatus,
  1194. ]);
  1195. }
  1196. // payDetail
  1197. // 不组合支付,那就只有一条支付记录 先删,再加
  1198. PayDetail::where('join_pay_order_id', $order->order_groupby)
  1199. ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  1200. ->delete();
  1201. $payData = [
  1202. 'pay_amount' => $params['order_amount_pay']
  1203. ];
  1204. if ($order->order_status_payment == 'SUCCESS') {
  1205. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  1206. $payData['pay_status'] = 'SUCCESS';
  1207. }
  1208. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1209. $payData['pay_prepayid'] = $params['pay_category'];
  1210. $payData['pay_json_response'] = $params['pay_json_response'];
  1211. } else if ($params['pay_category'] == 'CASH') {
  1212. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-CASH';
  1213. } else if ($params['pay_category'] == 'WELFARE') {
  1214. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-WELFARE';
  1215. } else if ($params['pay_category'] == 'VIP') {
  1216. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-VIP';
  1217. } else if ($params['pay_category'] == 'CARD') {
  1218. $payData['pay_prepayid'] = $params['card_nbr'];
  1219. } else if ($params['pay_category'] == 'OFFLINE') {
  1220. $payData['pay_prepayid'] = 'OFFLINE';
  1221. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1222. $payData['pay_prepayid'] = 'OFFLINE_ALIPAY';
  1223. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1224. $payData['pay_prepayid'] = 'OFFLINE_WXPAY';
  1225. } else if ($params['pay_category'] == 'MONEY') {
  1226. $payData['pay_prepayid'] = 'MONEY';
  1227. }
  1228. $payData['join_pay_member_id'] = $params['join_order_member_id'];
  1229. $payData['join_pay_order_id'] = $order->order_groupby;
  1230. $payData['pay_status'] = !empty($payData['pay_status']) && $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING';
  1231. $payData['pay_category'] = $params['goods_classify'] ?? '';
  1232. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  1233. $payData['pay_json_request'] = json_encode($params); // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"}
  1234. $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')])) : '[]';
  1235. $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
  1236. $payData['pay_addtimes'] = time();
  1237. PayDetail::insert($payData);
  1238. $writeOffDate = [];
  1239. $applyData = [];
  1240. Db::commit();
  1241. // 触发事件
  1242. if ($order->order_is_complete == 'Y' && $order->order_status_payment == 'SUCCESS') {
  1243. // 完成订单
  1244. Event::dispatch('order.complete', $params);
  1245. // 会员升级
  1246. // Event::dispatch('order_pay.member_level.up', $params['join_order_member_id']);
  1247. }
  1248. if ($order->order_status_payment == 'SUCCESS') {
  1249. // 上级提成
  1250. Event::dispatch('commission.order', $params);
  1251. }
  1252. // 打小票
  1253. if ($order->order_status_payment == 'SUCCESS') {
  1254. if (!empty($premises) && !empty($premises->dept_id)) {
  1255. $voteData = [
  1256. 'func' => 'procActionToPrinter',
  1257. 'sign' => '',
  1258. 'data' => [
  1259. 'printer_premises' => $premises->dept_id,
  1260. 'printer_device' => ["收银"],
  1261. 'printer_action' => 'ExecPrintOrder',
  1262. 'printer_data' => [
  1263. 'order_id' => $params['orderId'],
  1264. 'order_batch' => ''
  1265. ]
  1266. ]
  1267. ];
  1268. // dump("小票参数", $voteData);
  1269. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  1270. }
  1271. }
  1272. if ($order->order_status_payment != 'SUCCESS') {
  1273. _syslog("订单", "支付异常,检查是否有轮询");
  1274. // 恢复优惠券到已占用
  1275. if (!is_array($couponUseJson)) {
  1276. $couponUseJson = json_decode($couponUseJson, true);
  1277. }
  1278. // 如果下单时就填了,不用恢复
  1279. if (empty($discountJson)) {
  1280. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  1281. }
  1282. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  1283. }
  1284. _syslog("订单", "订单支付成功");
  1285. return json_success('支付成功');
  1286. } catch (BusinessException $e) {
  1287. dump($e->getMessage());
  1288. Db::rollBack();
  1289. _syslog("订单", "订单支付失败:" . $e->getMessage());
  1290. return json_fail("支付失败:" . $e->getMessage());
  1291. } catch (\Exception $e) {
  1292. dump($e->getMessage());
  1293. Db::rollBack();
  1294. _syslog("订单", "订单支付失败");
  1295. return json_fail('支付失败');
  1296. }
  1297. }
  1298. /**
  1299. * 组合支付
  1300. */
  1301. public function payConstitute(Request $request)
  1302. {
  1303. $params = $request->post();
  1304. // 余额、福利、储值卡 验证短信
  1305. if (!empty($params['pay_category_constitute']) && !in_array('OFFLINE', $params['pay_category_constitute']) && !in_array('MONEY', $params['pay_category_constitute']) && !in_array('QRCODE', $params['pay_category_constitute']) && (in_array('CASH', $params['pay_category_constitute']) || in_array('CARD', $params['pay_category_constitute']) || in_array('WELFARE', $params['pay_category_constitute']))) {
  1306. $code = $params['sms_code'];
  1307. if (!$code) {
  1308. return json_fail("验证码错误,请重新输入");
  1309. }
  1310. $member = Member::find($params['join_order_member_id']);
  1311. $mobile = $member->member_mobile;
  1312. $key = "SMS:CODE:ORDER_PAY:" . $mobile;
  1313. $redisCode = Redis::get($key);
  1314. if ($redisCode != $code && $code != '123456') {
  1315. return json_fail("验证码错误,请重新输入");
  1316. }
  1317. Redis::del($key);
  1318. }
  1319. // 验证线下付款密码
  1320. if (!empty($params['pay_category_constitute']) && in_array('OFFLINE', $params['pay_category_constitute']) && in_array('MONEY', $params['pay_category_constitute'])) {
  1321. $password = $params['offline_password'];
  1322. if ($password != '666888') {
  1323. return json_fail("密码错误,请重新输入");
  1324. }
  1325. }
  1326. $order = Order::where('order_id', $params['order_id'])->first();
  1327. if (!$order) {
  1328. return json_fail('订单异常');
  1329. }
  1330. if ($order->order_status_system != 'PAYING') {
  1331. return json_fail('订单不是可支付状态');
  1332. }
  1333. if (!empty($order->order_config_json)) {
  1334. $orderConfigJson = json_decode($order->order_config_json, true);
  1335. if (isset($orderConfigJson['premises'])) {
  1336. $premises = SysDept::where('dept_name', $orderConfigJson['premises'])->first();
  1337. if (!empty($premises)) {
  1338. $params['submit_premises_id'] = $premises->dept_id;
  1339. }
  1340. }
  1341. }
  1342. $params['orderId'] = $params['order_id'];
  1343. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  1344. $order->order_groupby = $params['orderGroupId'];
  1345. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
  1346. ->select('goods_id', 'goods_name', 'goods_classify')
  1347. ->first();
  1348. if (!$goods) {
  1349. return json_fail('产品数据异常');
  1350. }
  1351. $goods = $goods->toArray();
  1352. $params['goods_classify'] = $goods['goods_classify'];
  1353. $systemStatus = 'SENDING'; // 待发货
  1354. // 立即结算
  1355. if (in_array($params['goods_classify'], ['MEALS', 'VIP'])) {
  1356. $order->order_is_complete = 'Y';
  1357. $systemStatus = 'DONE';
  1358. }
  1359. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $params['delivery'] == 'ARRIVAL') {
  1360. $params['order_is_complete'] = 'N';
  1361. $systemStatus = "WAITING";
  1362. }
  1363. $payDetail = PayDetail::where('join_pay_order_id', $order->order_groupby)->first();
  1364. Db::beginTransaction();
  1365. try {// 使用优惠券
  1366. $couponUseJson = [];
  1367. $discountJson = [];
  1368. if (!empty($order->order_discount_json)) {
  1369. $discountJson = json_decode($order->order_discount_json, true);
  1370. foreach ($discountJson as $item) {
  1371. if (isset($item['coupon_value']) && sprintf('%.2f', (floatval($order->order_amount_total) - floatval($params['order_amount_pay']))) != sprintf('%.2f', $item['coupon_value'])) {
  1372. throw new BusinessException("计算优惠后,实付金额错误!");
  1373. }
  1374. }
  1375. $couponUseJson = $discountJson;
  1376. $this->changeOrderCouponStatus($couponUseJson, 'USED');
  1377. // 释放下单时选的优惠券
  1378. // $order->order_discount_json = $this->releaseCoupon($discountJson);
  1379. }
  1380. if (empty($discountJson) && !empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  1381. $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  1382. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  1383. throw new BusinessException("计算优惠后,实付金额错误!");
  1384. }
  1385. // 组装优惠券使用数据,存主表优惠里
  1386. if (!empty($couponResult['use_coupon_json'])) {
  1387. $couponUseJson = $couponResult['use_coupon_json'];
  1388. }
  1389. }
  1390. // 存储优惠信息
  1391. if (empty($discountJson)) {
  1392. $order->order_discount_json = json_encode($this->discountRecord($couponUseJson, $params));
  1393. }
  1394. $orderAmountPay = $params['order_amount_pay'];
  1395. $constituteList = array_column($params['pay_category_constitute_list'], 'amount', 'category');// 验证金额
  1396. $constituteAmount = 0;
  1397. foreach ($params['pay_category_constitute_list'] as $item) {
  1398. $constituteAmount = sprintf("%.2f", $constituteAmount) + sprintf("%.2f", $item['amount']);
  1399. }
  1400. if (sprintf("%.2f", $params['order_amount_pay']) != sprintf("%.2f", $constituteAmount)) {
  1401. throw new BusinessException('组合支付金额与应付金额不一致');
  1402. }
  1403. $wxAndAliPayStatus = 'Y';
  1404. $waitToPayAccount = [];
  1405. // 清除订单的支付记录,重建
  1406. PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])->delete();
  1407. if (!empty($params['pay_category_constitute']) && in_array('OFFLINE', $params['pay_category_constitute'])) { //线下付款
  1408. $order->order_status_system = $systemStatus;
  1409. $order->order_status_payment = 'SUCCESS';
  1410. $params['pay_category'] = $params['pay_category_sub'] ?? 'OFFLINE';
  1411. // 线下支付金额
  1412. if (isset($constituteList['OFFLINE'])) {
  1413. $params['order_amount_pay'] = $constituteList['OFFLINE'];
  1414. // 生成支付记录
  1415. $params['order_status_payment'] = 'PENDING';
  1416. OrderService::createProductPayConstituteDetail($params);
  1417. $params['order_status_payment'] = 'SUCCESS';
  1418. }
  1419. $params['order_amount_pay'] = $orderAmountPay;
  1420. }
  1421. if (!empty($params['pay_category_constitute']) && in_array('MONEY', $params['pay_category_constitute'])) { //现金付款
  1422. $order->order_status_system = $systemStatus;
  1423. $order->order_status_payment = 'SUCCESS';
  1424. $params['pay_category'] = 'MONEY';
  1425. // 线下支付金额
  1426. if (isset($constituteList['MONEY'])) {
  1427. $params['order_amount_pay'] = $constituteList['MONEY'];
  1428. // 生成支付记录
  1429. $params['order_status_payment'] = 'PENDING';
  1430. OrderService::createProductPayConstituteDetail($params);
  1431. $params['order_status_payment'] = 'SUCCESS';
  1432. }
  1433. $params['order_amount_pay'] = $orderAmountPay;
  1434. }
  1435. if (!empty($params['pay_category_constitute']) && in_array('CASH', $params['pay_category_constitute'])) { // 余额支付
  1436. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  1437. ->where('member_account_classify', 'CASH')
  1438. ->where('member_account_status', 'ACTIVED')
  1439. ->first();
  1440. if (!$account) {
  1441. throw new BusinessException('账户异常');
  1442. }
  1443. if (isset($constituteList['CASH'])) {
  1444. $params['order_amount_pay'] = $constituteList['CASH'];
  1445. $params['pay_category'] = $params['join_order_member_id'] . '-CASH';
  1446. }
  1447. $amount = $account->member_account_surplus + $account->member_account_added;
  1448. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  1449. throw new BusinessException('账户余额不足');
  1450. }
  1451. // 余额账户扣款数据
  1452. $waitToPayAccount['cash'] = [
  1453. 'group_id' => $params['orderGroupId'],
  1454. 'member_id' => $params['join_order_member_id'],
  1455. 'amount' => $constituteList['CASH'],
  1456. 'nbr' => $params['join_order_member_id'] . '-CASH'
  1457. ];
  1458. // 生成支付记录
  1459. $params['order_status_payment'] = 'PENDING';
  1460. OrderService::createProductPayConstituteDetail($params);
  1461. $params['order_status_payment'] = 'SUCCESS';
  1462. $params['order_amount_pay'] = $orderAmountPay;
  1463. $order->order_status_system = $systemStatus;
  1464. $order->order_status_payment = 'SUCCESS';
  1465. }
  1466. if (!empty($params['pay_category_constitute']) && in_array('CARD', $params['pay_category_constitute'])) { // 储值卡账户
  1467. $cardNbr = $params['card_nbr'];
  1468. if (!$cardNbr) {
  1469. throw new BusinessException('账户异常');
  1470. }
  1471. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  1472. ->where('member_account_nbr', $cardNbr)
  1473. ->where('member_account_status', 'ACTIVED')
  1474. ->first();
  1475. if (!$account) {
  1476. Db::rollBack();
  1477. return json_fail('账户异常');
  1478. }
  1479. // 储值卡账户支付金额
  1480. if (isset($constituteList['CARD'])) {
  1481. $params['order_amount_pay'] = $constituteList['CARD'];
  1482. $params['pay_category'] = $cardNbr;
  1483. }
  1484. $amount = $account->member_account_surplus + $account->member_account_added;
  1485. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  1486. throw new BusinessException('账户余额不足');
  1487. }
  1488. // 储值卡账户扣款数据
  1489. $waitToPayAccount['card'] = [
  1490. 'group_id' => $params['orderGroupId'],
  1491. 'member_id' => $params['join_order_member_id'],
  1492. 'amount' => $constituteList['CARD'],
  1493. 'nbr' => $params['join_order_member_id'] . '-CARD'
  1494. ];
  1495. // 生成支付记录
  1496. $params['order_status_payment'] = 'PENDING';
  1497. OrderService::createProductPayConstituteDetail($params);
  1498. $params['order_status_payment'] = 'SUCCESS';
  1499. $params['order_amount_pay'] = $orderAmountPay;
  1500. $order->order_status_system = $systemStatus;
  1501. $order->order_status_payment = 'SUCCESS';
  1502. }
  1503. if (!empty($params['pay_category_constitute']) && in_array('QRCODE', $params['pay_category_constitute']) && !empty($params['qrcode_nbr'])) { // 付款码
  1504. if (isset($constituteList['QRCODE']) && $constituteList['QRCODE'] > 0) {
  1505. // 付款码支付金额
  1506. $params['order_amount_pay'] = $constituteList['QRCODE'];
  1507. // 调支付
  1508. $result = OrderService::qrcodePay($params);
  1509. $result = json_encode($result);
  1510. $params['pay_json_response'] = $result;
  1511. $result = json_decode($result, true);
  1512. $prefix = substr($params['qrcode_nbr'], 0, 2);
  1513. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  1514. $params['pay_category'] = 'WXPAY';
  1515. 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')) {
  1516. // 标记支付状态
  1517. $wxAndAliPayStatus = 'N';
  1518. $order->order_status_system = 'PAYING';
  1519. $order->order_status_payment = 'PENDING';
  1520. $order->order_is_complete = 'N';
  1521. $params['order_status_payment'] = 'PAYING';
  1522. } else {
  1523. $order->order_status_system = $systemStatus;
  1524. $order->order_status_payment = 'SUCCESS';
  1525. $params['order_status_payment'] = 'SUCCESS';
  1526. }
  1527. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  1528. $params['pay_category'] = 'ALIPAY';
  1529. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  1530. // 标记支付状态
  1531. $wxAndAliPayStatus = 'N';
  1532. $order->order_status_system = 'PAYING';
  1533. $order->order_status_payment = 'PENDING';
  1534. $order->order_is_complete = 'N';
  1535. $params['order_status_payment'] = 'PAYING';
  1536. } else {
  1537. $order->order_status_system = $systemStatus;
  1538. $order->order_status_payment = 'SUCCESS';
  1539. $params['order_status_payment'] = 'SUCCESS';
  1540. }
  1541. } else {
  1542. throw new BusinessException('付款码无效');
  1543. }
  1544. }
  1545. // 生成支付记录
  1546. OrderService::createProductPayConstituteDetail($params);
  1547. // 账户支付的金额
  1548. $params['order_amount_pay'] = $orderAmountPay;
  1549. }
  1550. $orderConfigJson = [];
  1551. if (!empty($order->order_config_json)) {
  1552. $orderConfigJson = json_decode($order->order_config_json, true);
  1553. }
  1554. $orderConfigJson['preferential'] = $params['preferential'] ?? '';
  1555. $order->order_config_json = json_encode($orderConfigJson);
  1556. $order->order_amount_pay = $params['order_amount_pay'];
  1557. // 主订单
  1558. $order->save();
  1559. // sheet
  1560. if ($order->order_status_payment == 'SUCCESS') {
  1561. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update([
  1562. 'order_sheet_status' => $systemStatus,
  1563. ]);
  1564. }
  1565. if ($wxAndAliPayStatus == 'Y') {
  1566. PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])->update([
  1567. 'pay_status' => 'SUCCESS',
  1568. 'pay_paytimes' => date('Y-m-d H:i:s')
  1569. ]);
  1570. // 扣款
  1571. foreach ($waitToPayAccount as $item) {
  1572. $accountNbr = $item['nbr'];
  1573. $account = MemberAccount::where('member_account_nbr', $accountNbr)->first();
  1574. $account->member_account_expend = $account->member_account_expend + $item['amount'];
  1575. if ($account->member_account_surplus < $item['amount'] && strpos($accountNbr, 'CASH') !== false) {
  1576. $account->member_account_added = $account->member_account_added - ($item['amount'] - $account->member_account_surplus);
  1577. $account->member_account_surplus = 0;
  1578. } else {
  1579. $account->member_account_surplus = $account->member_account_surplus - $item['amount'];
  1580. }
  1581. $account->save();
  1582. }
  1583. }
  1584. Db::commit();
  1585. // 触发事件
  1586. if ($order->order_is_complete == 'Y' && $order->order_status_payment == 'SUCCESS') {
  1587. // 完成订单
  1588. Event::dispatch('order.complete', $params);
  1589. // 会员升级
  1590. // Event::dispatch('order_pay.member_level.up', $params['join_order_member_id']);
  1591. }
  1592. if ($order->order_status_payment == 'SUCCESS') {
  1593. // 上级提成
  1594. Event::dispatch('commission.order', $params);
  1595. }
  1596. // 打小票
  1597. if ($order->order_status_payment == 'SUCCESS') {
  1598. if (!empty($premises) && !empty($premises->dept_id)) {
  1599. $voteData = [
  1600. 'func' => 'procActionToPrinter',
  1601. 'sign' => '',
  1602. 'data' => [
  1603. 'printer_premises' => $premises->dept_id,
  1604. 'printer_device' => ["收银"],
  1605. 'printer_action' => 'ExecPrintOrder',
  1606. 'printer_data' => [
  1607. 'order_id' => $params['orderId'],
  1608. 'order_batch' => ''
  1609. ]
  1610. ]
  1611. ];
  1612. // dump("小票参数", $voteData);
  1613. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  1614. }
  1615. }
  1616. if ($order->order_status_payment != 'SUCCESS') {
  1617. _syslog("订单", "支付异常,检查是否有轮询");
  1618. // 恢复优惠券到已占用
  1619. if (!is_array($couponUseJson)) {
  1620. $couponUseJson = json_decode($couponUseJson, true);
  1621. }
  1622. // 如果下单时就填了,不用恢复
  1623. if (empty($discountJson)) {
  1624. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  1625. }
  1626. // 清除支付记录,恢复账户金额
  1627. // $this->restoreAccount($params['pay_detail_item']);
  1628. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  1629. }
  1630. _syslog("订单", "订单支付成功");
  1631. return json_success('支付成功');
  1632. } catch (\Exception $e) {
  1633. dump($e->getMessage());
  1634. Db::rollBack();
  1635. _syslog("订单", "订单支付失败");
  1636. return json_fail('支付失败');
  1637. }
  1638. }
  1639. /**
  1640. * @Desc
  1641. * @Author Gorden
  1642. * @Date 2024/6/7 10:30
  1643. *
  1644. * @param $params
  1645. * @return void
  1646. * @throws BusinessException
  1647. */
  1648. public function insertMain($params, $wxAndAliPayStatus = 'Y')
  1649. {
  1650. try {
  1651. if (empty($params['order_extend_json'])) {
  1652. $params['order_extend_json'] = [];
  1653. } else {
  1654. if (is_json($params['order_extend_json'])) {
  1655. $params['order_extend_json'] = json_decode($params['order_extend_json'], true);
  1656. }
  1657. }
  1658. // 账户扣款
  1659. if ($wxAndAliPayStatus == 'Y' && !empty($params['waitToPayAccount'])) {
  1660. foreach ($params['waitToPayAccount'] as $account) {
  1661. $this->deductAmount($account['nbr'], $account['amount']);
  1662. }
  1663. }
  1664. // 推荐人
  1665. $params['order_extend_json']['referee'] = $params['referee'] ?? '';
  1666. foreach ($params['goodsContentList'] as $goods) {
  1667. $discountJson = [];
  1668. if (!empty($params['order_discount_json'])) {
  1669. $discountJson = json_decode($params['order_discount_json'], true);
  1670. foreach ($discountJson as &$item) {
  1671. if (!empty($item['coupon_value'])) {
  1672. $item['coupon_value'] = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $params['order_amount_total']) * $item['coupon_value'], 2);
  1673. }
  1674. }
  1675. }
  1676. $orderId = 'OD' . date('ymdHi') . random_string(4, 'up');
  1677. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $params['order_amount_total']) * $params['order_amount_pay'], 2);
  1678. // $amountPay = round(floatval($goods['goods_sales_price']) * $goods['nbr'] * $params['order_ratio'], 2);
  1679. $data = [
  1680. 'order_id' => $orderId,
  1681. 'order_groupby' => $params['orderGroupId'],
  1682. 'join_order_member_id' => $params['join_order_member_id'],
  1683. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  1684. 'order_amount_total' => floatval($goods['goods_sales_price']) * $goods['nbr'],
  1685. 'order_amount_pay' => $amountPay,
  1686. 'order_category' => $goods['goods_classify'],
  1687. 'order_classify' => $goods['goods_classify'],
  1688. 'order_is_complete' => $params['order_is_complete'] ?? 'N',
  1689. 'order_status_system' => $params['order_status_system'],
  1690. 'order_status_payment' => $params['order_status_payment'],
  1691. 'order_status_storage' => $params['order_status_storage'],
  1692. 'order_platform' => $params['order_platform'],
  1693. 'order_remark' => $params['order_remark'] ?? '',
  1694. 'order_discount_json' => json_encode($discountJson),
  1695. 'order_config_json' => $params['order_config_json'] ?? '[]',
  1696. 'order_express_json' => $params['order_express_json'] ?? '[]',
  1697. 'order_extend_json' => $params['order_extend_json'] ? json_encode($params['order_extend_json']) : '[]',
  1698. 'order_addtimes' => time()
  1699. ];
  1700. Order::insert($data);
  1701. $sheetIds = $this->insertSheetOne($params, $goods, $orderId, $amountPay);
  1702. $this->insertPayDetailOne($params, $orderId, $amountPay, $goods);
  1703. $params['order_express_goods'] = json_encode(['sheet' => $sheetIds]);
  1704. $this->insertExpressOne($params, $orderId);
  1705. }
  1706. } catch (\Exception $e) {
  1707. dump($e->getMessage());
  1708. throw new BusinessException('订单创建信息失败');
  1709. }
  1710. }
  1711. /**
  1712. * @Desc 扣除账户金额
  1713. * @Author Gorden
  1714. * @Date 2024/9/26 14:16
  1715. *
  1716. * @param $accountNbr
  1717. * @param $amount
  1718. * @return void
  1719. */
  1720. public function deductAmount($accountNbr, $amount)
  1721. {
  1722. try {
  1723. $account = MemberAccount::where('member_account_nbr', $accountNbr)->first();
  1724. $account->member_account_expend = $account->member_account_expend + $amount;
  1725. if ($account->member_account_surplus < $amount && strpos($accountNbr, 'CASH') !== false) {
  1726. $account->member_account_added = $account->member_account_added - ($amount - $account->member_account_surplus);
  1727. $account->member_account_surplus = 0;
  1728. } else {
  1729. $account->member_account_surplus = $account->member_account_surplus - $amount;
  1730. }
  1731. $account->save();
  1732. } catch (\Exception $e) {
  1733. _syslog("下单扣款", "扣款失败");
  1734. throw new BusinessException("账户扣款失败");
  1735. }
  1736. }
  1737. /**
  1738. * @Desc 打小票
  1739. * @Author Gorden
  1740. * @Date 2024/9/24 9:35
  1741. *
  1742. * @param $params
  1743. * @param $premises
  1744. * @param $orderId
  1745. * @return void
  1746. */
  1747. public function printerTicket($params, $premises, $orderId)
  1748. {
  1749. // 打小票
  1750. if ($params['order_status_payment'] == 'SUCCESS') {
  1751. if (!empty($premises) && !empty($premises->dept_id)) {
  1752. $voteData = [
  1753. 'func' => 'procActionToPrinter',
  1754. 'sign' => '',
  1755. 'data' => [
  1756. 'printer_premises' => $premises->dept_id,
  1757. 'printer_device' => ["收银"],
  1758. 'printer_action' => 'ExecPrintOrder',
  1759. 'printer_data' => [
  1760. 'order_id' => $orderId,
  1761. 'order_batch' => ''
  1762. ]
  1763. ]
  1764. ];
  1765. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  1766. }
  1767. }
  1768. }
  1769. /**
  1770. * @Desc
  1771. * @Author Gorden
  1772. * @Date 2024/6/7 10:25
  1773. *
  1774. * @param $params
  1775. * @return void
  1776. * @throws BusinessException
  1777. */
  1778. public function insertSheet($params)
  1779. {
  1780. try {
  1781. $orderSheetIds = [];
  1782. foreach ($params['goodsContentList'] as $goods) {
  1783. //{"unit": "份", "table": null, "premises": "15"}
  1784. $price = floatval($goods['goods_sales_price']);
  1785. $extendJson['unit'] = $goods['sku_name'];
  1786. if (isset($params['submit_premises_id'])) {
  1787. $extendJson['premises'] = $params['submit_premises_id'];
  1788. }
  1789. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  1790. $extendJson['table'] = null;
  1791. }
  1792. $data = [
  1793. 'join_sheet_member_id' => $params['join_order_member_id'],
  1794. 'join_sheet_order_id' => $params['orderId'],
  1795. 'join_sheet_goods_id' => $goods['goods_id'],
  1796. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  1797. 'order_sheet_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  1798. 'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL',
  1799. 'order_sheet_num' => $goods['nbr'],
  1800. 'order_sheet_price' => $goods['goods_sales_price'],
  1801. 'order_sheet_amount' => $price * $goods['nbr'],
  1802. 'order_sheet_pay' => $price * $goods['nbr'],
  1803. 'order_sheet_task_status' => 'NONE',
  1804. 'order_sheet_remark' => $params['order_remark'] ?? '',
  1805. 'order_sheet_addtimes' => time(),
  1806. 'order_sheet_extend_json' => json_encode($extendJson)
  1807. ];
  1808. $orderSheetId = OrderSheet::insertGetId($data);
  1809. $orderSheetIds[] = $orderSheetId;
  1810. // 减库存,规格和总库存
  1811. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
  1812. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  1813. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  1814. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  1815. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  1816. }
  1817. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  1818. throw new BusinessException('库存不足');
  1819. }
  1820. $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson);
  1821. $goodsSku->save();
  1822. }
  1823. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  1824. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  1825. if ($goodsRunning->goods_running_storage < 0) {
  1826. throw new BusinessException('库存不足');
  1827. }
  1828. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  1829. $goodsRunning->save();
  1830. }
  1831. // return $orderSheetIds;
  1832. } catch (\support\exception\BusinessException $e) {
  1833. dump($e->getMessage() . '||' . $e->getLine());
  1834. throw new BusinessException($e->getMessage());
  1835. } catch (\Exception $e) {
  1836. dump($e->getMessage() . '||' . $e->getLine());
  1837. throw new BusinessException('订单创建失败');
  1838. }
  1839. }
  1840. /**
  1841. * @Desc 单条入库
  1842. * @Author Gorden
  1843. * @Date 2024/9/23 10:19
  1844. *
  1845. * @param $params
  1846. * @return array
  1847. * @throws BusinessException
  1848. */
  1849. public function insertSheetOne($params, $goods, $orderId, $amountPay)
  1850. {
  1851. try {
  1852. $orderSheetIds = [];
  1853. // foreach ($params['goodsContentList'] as $goods) {
  1854. //{"unit": "份", "table": null, "premises": "15"}
  1855. $price = floatval($goods['goods_sales_price']);
  1856. $extendJson['unit'] = $goods['sku_name'];
  1857. if (isset($params['submit_premises_id'])) {
  1858. $extendJson['premises'] = $params['submit_premises_id'];
  1859. }
  1860. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  1861. $extendJson['table'] = null;
  1862. }
  1863. $data = [
  1864. 'join_sheet_member_id' => $params['join_order_member_id'],
  1865. 'join_sheet_order_id' => $orderId,
  1866. 'join_sheet_goods_id' => $goods['goods_id'],
  1867. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  1868. 'order_sheet_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  1869. 'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL',
  1870. 'order_sheet_num' => $goods['nbr'],
  1871. 'order_sheet_price' => $goods['goods_sales_price'],
  1872. 'order_sheet_price_pay' => round($amountPay / $goods['nbr'], 2),
  1873. 'order_sheet_amount' => $price * $goods['nbr'],
  1874. 'order_sheet_pay' => $amountPay,
  1875. 'order_sheet_task_status' => 'NONE',
  1876. 'order_sheet_remark' => $params['order_remark'] ?? '',
  1877. 'order_sheet_addtimes' => time(),
  1878. 'order_sheet_extend_json' => json_encode($extendJson)
  1879. ];
  1880. $orderSheetId = OrderSheet::insertGetId($data);
  1881. $orderSheetIds[] = $orderSheetId;
  1882. // 减库存,规格和总库存
  1883. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
  1884. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  1885. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  1886. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  1887. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  1888. }
  1889. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  1890. throw new BusinessException('库存不足');
  1891. }
  1892. $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson);
  1893. $goodsSku->save();
  1894. }
  1895. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  1896. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  1897. if ($goodsRunning->goods_running_storage < 0) {
  1898. throw new BusinessException('库存不足');
  1899. }
  1900. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  1901. $goodsRunning->save();
  1902. // }
  1903. return $orderSheetIds;
  1904. } catch (\support\exception\BusinessException $e) {
  1905. dump($e->getMessage() . '||' . $e->getLine());
  1906. throw new BusinessException($e->getMessage());
  1907. } catch (\Exception $e) {
  1908. dump($e->getMessage() . '||' . $e->getLine());
  1909. throw new BusinessException('订单创建失败');
  1910. }
  1911. }
  1912. /**
  1913. * @Desc
  1914. * @Author Gorden
  1915. * @Date 2024/6/7 10:35
  1916. *
  1917. * @param $params
  1918. * @return void
  1919. * @throws BusinessException
  1920. */
  1921. public function insertPayDetail($params)
  1922. {
  1923. try {
  1924. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1925. $payPrepayid = $params['pay_category'];
  1926. } else if ($params['pay_category'] == 'OFFLINE') {
  1927. $payPrepayid = 'OFFLINE';
  1928. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1929. $payPrepayid = 'OFFLINE_ALIPAY';
  1930. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1931. $payPrepayid = 'OFFLINE_WXPAY';
  1932. } else if ($params['pay_category'] == 'MONEY') {
  1933. $payPrepayid = 'MONEY';
  1934. } else {
  1935. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  1936. }
  1937. $data = [
  1938. 'join_pay_member_id' => $params['join_order_member_id'],
  1939. 'join_pay_order_id' => $params['orderGroupId'],
  1940. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  1941. 'pay_category' => $params['goods_classify'],
  1942. 'pay_amount' => $params['order_amount_pay'],
  1943. 'pay_prepayid' => $payPrepayid,
  1944. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1945. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
  1946. 'pay_json_request' => json_encode($params),
  1947. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  1948. 'pay_remark' => $params['order_remark'] ?? '',
  1949. 'pay_addtimes' => time(),
  1950. ];
  1951. PayDetail::insert($data);
  1952. } catch (\Exception $e) {
  1953. dump($e->getMessage());
  1954. throw new BusinessException('创建支付记录失败');
  1955. }
  1956. }
  1957. /**
  1958. * @Desc 单条入库
  1959. * @Author Gorden
  1960. * @Date 2024/6/7 10:35
  1961. *
  1962. * @param $params
  1963. * @return void
  1964. * @throws BusinessException
  1965. */
  1966. public function insertPayDetailOne($params, $orderId, $amountPay, $goods)
  1967. {
  1968. try {
  1969. if (!empty($params['pay_detail_item'])) {
  1970. foreach ($params['pay_detail_item'] as $item) {
  1971. $data = [
  1972. 'join_pay_member_id' => $item['join_order_member_id'],
  1973. 'join_pay_order_id' => $item['orderGroupId'],
  1974. 'pay_status' => $item['settlement_now'] == 'Y' && $item['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  1975. 'pay_category' => $goods['goods_classify'],
  1976. 'pay_amount' => round(($amountPay / $params['order_amount_pay']) * $item['order_amount_pay'], 2),
  1977. 'pay_prepayid' => $item['pay_category'],
  1978. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1979. 'join_pay_object_json' => !empty($item['orderId']) ? json_encode(['order_id' => $orderId]) : '[]',
  1980. 'pay_json_request' => json_encode($item),
  1981. 'pay_json_response' => $item['pay_json_response'] ?? '[]',
  1982. 'pay_remark' => $item['order_remark'] ?? '',
  1983. 'pay_addtimes' => time(),
  1984. ];
  1985. PayDetail::insert($data);
  1986. }
  1987. } else {
  1988. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1989. $payPrepayid = $params['pay_category'];
  1990. } else if ($params['pay_category'] == 'OFFLINE') {
  1991. $payPrepayid = 'OFFLINE';
  1992. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1993. $payPrepayid = 'OFFLINE_ALIPAY';
  1994. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1995. $payPrepayid = 'OFFLINE_WXPAY';
  1996. } else if ($params['pay_category'] == 'MONEY') {
  1997. $payPrepayid = 'MONEY';
  1998. } else {
  1999. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  2000. }
  2001. $data = [
  2002. 'join_pay_member_id' => $params['join_order_member_id'],
  2003. 'join_pay_order_id' => $params['orderGroupId'],
  2004. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  2005. 'pay_category' => $goods['goods_classify'],
  2006. 'pay_amount' => $amountPay,
  2007. 'pay_prepayid' => $payPrepayid,
  2008. 'pay_paytimes' => date('Y-m-d H:i:s'),
  2009. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $orderId]) : '[]',
  2010. 'pay_json_request' => json_encode($params),
  2011. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  2012. 'pay_remark' => $params['order_remark'] ?? '',
  2013. 'pay_addtimes' => time(),
  2014. ];
  2015. PayDetail::insert($data);
  2016. }
  2017. } catch (\Exception $e) {
  2018. dump($e->getMessage());
  2019. throw new BusinessException('创建支付记录失败');
  2020. }
  2021. }
  2022. /**
  2023. * @Desc
  2024. * @Author Gorden
  2025. * @Date 2024/6/7 10:50
  2026. *
  2027. * @param $params
  2028. * @return void
  2029. * @throws BusinessException
  2030. */
  2031. public function insertAppointment($params, $writeOffDate, $applyData = [])
  2032. {
  2033. try {
  2034. $data = [
  2035. 'appointment_id' => $params['appointmentId'],
  2036. 'join_appointment_member_id' => $params['join_order_member_id'],
  2037. 'join_appointment_goods_id' => $params['join_sheet_goods_id'],
  2038. 'join_appointment_goods_sku_id' => $params['join_sheet_goods_sku_id'],
  2039. 'join_appointment_order_id' => $params['orderId'],
  2040. 'join_appointment_member_benefit_id' => $params['benefitId'],
  2041. 'appointment_classify' => $params['goods_classify'],
  2042. 'appointment_status' => 'INIT',
  2043. 'appointment_category' => 'NORMAL',
  2044. 'appointment_platform' => 'SYSTEM',
  2045. 'appointment_addtimes' => time(),
  2046. 'appointment_datetime' => '',
  2047. 'appointment_apply_datetime' => '',
  2048. 'appointment_apply_json' => '[]',
  2049. 'appointment_done_datetime' => '',
  2050. 'appointment_remark' => $params['order_remark'] ?? '',
  2051. 'appointment_done_json' => '[]'
  2052. ];
  2053. Appointment::insert($data);
  2054. } catch (\Exception $e) {
  2055. dump($e->getMessage());
  2056. throw new BusinessException("创建权益记录失败");
  2057. }
  2058. }
  2059. /**
  2060. * @Desc
  2061. * @Author Gorden
  2062. * @Date 2024/6/7 11:05
  2063. *
  2064. * @param $params
  2065. * @param $goods
  2066. * @return void
  2067. * @throws BusinessException
  2068. */
  2069. public function insertMemberBenefit($params, $goods)
  2070. {
  2071. try {
  2072. $data = [
  2073. 'member_benefit_id' => $params['benefitId'],
  2074. 'join_benefit_member_id' => $params['join_order_member_id'],
  2075. 'join_benefit_package_id' => $params['packageId'] ?? '',
  2076. 'join_benefit_goods_id' => $goods['goods_id'],
  2077. 'join_benefit_goods_sku_id' => $goods['skuId'] ?? '',
  2078. 'join_benefit_order_id' => $params['orderId'],
  2079. 'member_benefit_status' => 'ACTIVED',
  2080. 'member_benefit_category' => $goods['category'],
  2081. 'member_benefit_name' => $goods['goods_name'],
  2082. 'member_benefit_limit_count' => $params['order_sheet_num'],
  2083. 'member_benefit_used_count' => 0,
  2084. 'member_benefit_remark' => $params['order_remark'] ?? '',
  2085. 'member_benefit_addtimes' => time()
  2086. ];
  2087. MemberBenefit::insert($data);
  2088. } catch (\Exception $e) {
  2089. dump($e->getMessage());
  2090. throw new BusinessException('创建会员权益失败');
  2091. }
  2092. }
  2093. public function saveExpress($params, $orderId)
  2094. {
  2095. try {
  2096. $express = new OrderExpress();
  2097. $express->order_express_type = $params['order_express_type'];
  2098. $express->join_express_order_id = $orderId;
  2099. $express->join_express_dept_id = $params['submit_premises_id'] ?? 0;
  2100. $express->order_express_goods = $params['order_express_goods'];
  2101. $express->order_express_city = $params['order_express_city'];
  2102. $express->order_express_address = $params['order_express_address'];
  2103. $express->order_express_mobile = $params['order_express_mobile'] ?? '';
  2104. $express->order_express_telephone = $params['order_express_telephone'] ?? '';
  2105. $express->order_express_person = $params['order_express_person'] ?? '';
  2106. $express->order_express_company = $params['dept_premises_id'] ?? '';
  2107. $express->order_express_extend_json = $params['order_express_extend_json'] ?? '[]';
  2108. $express->order_express_addtimes = time();
  2109. $express->save();
  2110. } catch (\Exception $e) {
  2111. dump($e->getMessage());
  2112. throw new BusinessException('物流信息保存失败');
  2113. }
  2114. }
  2115. /**
  2116. * @Desc 修改优惠券状态
  2117. * @Author Gorden
  2118. * @Date 2024/9/19 9:03
  2119. *
  2120. * @param $coupon
  2121. * @param $status
  2122. * @return void
  2123. */
  2124. private function changeOrderCouponStatus($coupon, $status)
  2125. {
  2126. if (!empty($coupon) && is_array($coupon)) {
  2127. $updateData['coupon_detail_status'] = $status;
  2128. if ($status == 'ACTIVED' || $status == 'WAITING') {
  2129. $updateData['coupon_detail_used_datetime'] = '';
  2130. } elseif ($status == 'USED') {
  2131. $updateData['coupon_detail_used_datetime'] = date('Y-m-d H:i:s');
  2132. }
  2133. foreach ($coupon as $item) {
  2134. if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
  2135. foreach ($item['coupon_detail_id'] as $detailId) {
  2136. CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
  2137. ->where('coupon_detail_id', $detailId)->update($updateData);
  2138. }
  2139. }
  2140. }
  2141. }
  2142. }
  2143. private function discountRecord($orderDiscountJson, $params)
  2144. {
  2145. $json = [];
  2146. if (!empty($orderDiscountJson)) {
  2147. $json = json_decode($orderDiscountJson, true);
  2148. }
  2149. try {
  2150. if (!empty($params['preferential']) && in_array('wipe', $params['preferential'])) {
  2151. if (intval($params['order_amount_total'] / 10) * 10 != $params['order_amount_pay']) {
  2152. throw new BusinessException("抹零后实际支付金额错误");
  2153. }
  2154. $params['order_discount_amount'] = $params['order_amount_total'] - $params['order_amount_pay'];
  2155. $couponClassifyDesc = $couponClassify = '抹零';
  2156. } elseif (!empty($params['preferential']) && in_array('custom', $params['preferential'])) {
  2157. if (sprintf("%.2f", $params['order_amount_total'] - $params['order_discount_amount']) != sprintf("%.2f", $params['order_amount_pay'])) {
  2158. throw new BusinessException("餐厅前台优惠后实际支付金额错误");
  2159. }
  2160. $couponClassifyDesc = $couponClassify = '餐厅前台优惠';
  2161. } else if (!empty($params['preferential']) && intval($params['preferential'][0]) < 100 && intval($params['preferential'][0]) >= 50) {
  2162. if (($params['order_amount_total'] * intval($params['preferential'][0])) / 100 != $params['order_amount_pay']) {
  2163. throw new BusinessException("折扣后实际支付金额错误");
  2164. }
  2165. $couponClassify = '折扣';
  2166. $couponClassifyDesc = intval($params['preferential'][0]) / 10 . '折';
  2167. $params['order_discount_amount'] = $params['order_amount_total'] - $params['order_amount_pay'];
  2168. } else {
  2169. return $json;
  2170. }
  2171. $json[date('Y-m-d H:i:s')] = [
  2172. 'coupon_id' => $params['coupon_id'] ?? null,
  2173. 'coupon_value' => $params['order_discount_amount'] ?? '',
  2174. 'coupon_classify' => $couponClassify,
  2175. 'coupon_detail_id' => [$couponClassifyDesc],
  2176. 'coupon_classify_en' => $params['preferential']
  2177. ];
  2178. return $json;
  2179. } catch (BusinessException $e) {
  2180. throw new BusinessException($e->getMessage());
  2181. } catch (\Exception $e) {
  2182. dump($e->getMessage());
  2183. throw new BusinessException("优惠数据错误");
  2184. }
  2185. }
  2186. /**
  2187. * @Desc 入配送记录
  2188. * @Author Gorden
  2189. * @Date 2024/9/23 10:45
  2190. *
  2191. * @param $params
  2192. * @return void
  2193. * @throws BusinessException
  2194. */
  2195. public function insertExpressOne($params, $orderId)
  2196. {
  2197. if (isset($params['delivery']) && $params['delivery'] == 'LOGISTICS') {
  2198. $params['order_express_type'] = '配送';
  2199. // 入配送
  2200. $this->saveExpress($params, $orderId);
  2201. } else if (isset($params['delivery']) && $params['delivery'] == 'PICKUP') {
  2202. $params['order_express_type'] = '自提';
  2203. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  2204. if (!$premises) {
  2205. throw new BusinessException('自提门店不存在');
  2206. }
  2207. $params['order_express_city'] = $premises->dept_city;
  2208. $params['order_express_address'] = $premises->dept_address;
  2209. $params['order_express_telephone'] = $premises->dept_telephone;
  2210. $params['order_express_extend_json'] = json_encode(['pick_code' => random_string(4, 'number')]);
  2211. $this->saveExpress($params, $orderId);
  2212. } else if (isset($params['delivery']) && $params['delivery'] == 'ARRIVAL') {
  2213. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  2214. $params['order_express_type'] = '到店';
  2215. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  2216. $params['order_express_type'] = '堂食';
  2217. }
  2218. if (!$premises) {
  2219. throw new BusinessException('门店不存在');
  2220. }
  2221. $params['order_express_city'] = $premises->dept_city;
  2222. $params['order_express_address'] = $premises->dept_address;
  2223. $params['order_express_telephone'] = $premises->dept_telephone;
  2224. $this->saveExpress($params, $orderId);
  2225. }
  2226. }
  2227. /**
  2228. * @Desc 释放下单时选的优惠券
  2229. * @Author Gorden
  2230. * @Date 2024/9/19 9:56
  2231. *
  2232. * @param $coupon
  2233. * @return false|string
  2234. */
  2235. private function releaseCoupon($coupon)
  2236. {
  2237. if (!empty($coupon) && is_array($coupon)) {
  2238. $updateData = [
  2239. 'coupon_detail_status' => 'ACTIVED',
  2240. 'coupon_detail_used_datetime' => ''
  2241. ];
  2242. foreach ($coupon as $key => $item) {
  2243. if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
  2244. foreach ($item['coupon_detail_id'] as $detailId) {
  2245. if (substr($detailId, 0, 4) == 'CUDT') {
  2246. CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
  2247. ->where('coupon_detail_id', $detailId)
  2248. ->where('coupon_detail_status', 'WAITING')
  2249. ->update($updateData);
  2250. }
  2251. }
  2252. unset($coupon[$key]);
  2253. }
  2254. }
  2255. }
  2256. return json_encode($coupon);
  2257. }
  2258. }