GoodsController.php 121 KB

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