GoodsController.php 122 KB

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