GoodsController.php 123 KB

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