GoodsController.php 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  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 != '123456') {
  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 != '123456') {
  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 $constituteList) {
  741. if ($constituteList['category'] != 'CARD') {
  742. continue;
  743. }
  744. $cardNbr = $constituteList['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($constituteList['amount'])) {
  755. $params['order_amount_pay'] = $constituteList['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' => $constituteList['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 != '123456') {
  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['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] > $amount)) {
  1128. throw new BusinessException('账户余额不足');
  1129. }
  1130. if ($params['pay_constitute'] == 'Y' && (!$account || $params['order_amount_pay'] > $amount)) {
  1131. $qrcodePayAmount = $params['order_amount_pay'] - $amount;
  1132. $params['order_amount_pay'] = $amount;
  1133. }
  1134. if ($params['order_amount_pay'] > $account->member_account_surplus) {
  1135. $cut = $account->member_account_added - ($params['order_amount_pay'] - $account->member_account_surplus);
  1136. $account->member_account_surplus = 0;
  1137. $account->member_account_added = $cut;
  1138. } else {
  1139. $account->member_account_surplus = $account->member_account_surplus - $params['order_amount_pay'];
  1140. }
  1141. $account->member_account_expend = $account->member_account_expend + $params['order_amount_pay'];
  1142. $account->member_account_update_user_id = JwtToken::getCurrentId();
  1143. $account->member_account_updatetimes = time();
  1144. $account->save();
  1145. if ($params['pay_constitute'] == 'N' && (!$account || $params['order_amount_pay'] <= $amount)) {
  1146. $order->order_status_system = $systemStatus;
  1147. $order->order_status_payment = 'SUCCESS';
  1148. }
  1149. }
  1150. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && !empty($params['qrcode_nbr'])) { // 付款码
  1151. // 提交过来的支付分类
  1152. $submitPayCategory = $params['pay_category'];
  1153. // 账户支付的金额
  1154. $accountAmount = $params['order_amount_pay'];
  1155. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
  1156. // 组合支付,改成需要付款码需要支付的金额
  1157. $params['order_amount_pay'] = $qrcodePayAmount;
  1158. }
  1159. // 去支付
  1160. $result = OrderService::qrcodePay($params);
  1161. $result = json_encode($result);
  1162. $params['pay_json_response'] = $result;
  1163. $result = json_decode($result, true);
  1164. $prefix = substr($params['qrcode_nbr'], 0, 2);
  1165. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  1166. $params['pay_category'] = 'WXPAY';
  1167. 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')) {
  1168. $order->order_status_system = $oldOrderStatus;
  1169. $order->order_status_payment = 'PENDING';
  1170. $order->order_is_complete = 'N';
  1171. // Db::rollBack();
  1172. // return json_fail('支付失败');
  1173. } else {
  1174. $order->order_status_system = $systemStatus;
  1175. $order->order_status_payment = 'SUCCESS';
  1176. }
  1177. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  1178. $params['pay_category'] = 'ALIPAY';
  1179. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  1180. $order->order_status_system = $oldOrderStatus;
  1181. $order->order_status_payment = 'PENDING';
  1182. $order->order_is_complete = 'N';
  1183. // Db::rollBack();
  1184. // return json_fail('支付失败');
  1185. } else {
  1186. $order->order_status_system = $systemStatus;
  1187. $order->order_status_payment = 'SUCCESS';
  1188. }
  1189. } else {
  1190. throw new BusinessException('付款码无效');
  1191. }
  1192. // 账户支付的金额
  1193. $params['order_amount_pay'] = $accountAmount;
  1194. }
  1195. $orderConfigJson = [];
  1196. if (!empty($order->order_config_json)) {
  1197. $orderConfigJson = json_decode($order->order_config_json, true);
  1198. }
  1199. $orderConfigJson['preferential'] = $params['preferential'] ?? '';
  1200. $order->order_config_json = json_encode($orderConfigJson);
  1201. $order->order_amount_pay = $params['order_amount_pay'] + $qrcodePayAmount;
  1202. // 主订单
  1203. $order->save();
  1204. // sheet
  1205. if ($order->order_status_payment == 'SUCCESS') {
  1206. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update([
  1207. 'order_sheet_status' => $systemStatus,
  1208. ]);
  1209. }
  1210. // payDetail
  1211. // 不组合支付,那就只有一条支付记录 先删,再加
  1212. PayDetail::where('join_pay_order_id', $order->order_groupby)
  1213. ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  1214. ->delete();
  1215. $payData = [
  1216. 'pay_amount' => $params['order_amount_pay']
  1217. ];
  1218. if ($order->order_status_payment == 'SUCCESS') {
  1219. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  1220. $payData['pay_status'] = 'SUCCESS';
  1221. }
  1222. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1223. $payData['pay_prepayid'] = $params['pay_category'];
  1224. $payData['pay_json_response'] = $params['pay_json_response'];
  1225. } else if ($params['pay_category'] == 'CASH') {
  1226. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-CASH';
  1227. } else if ($params['pay_category'] == 'WELFARE') {
  1228. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-WELFARE';
  1229. } else if ($params['pay_category'] == 'VIP') {
  1230. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-VIP';
  1231. } else if ($params['pay_category'] == 'CARD') {
  1232. $payData['pay_prepayid'] = $params['card_id'];
  1233. } else if ($params['pay_category'] == 'OFFLINE') {
  1234. $payData['pay_prepayid'] = 'OFFLINE';
  1235. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1236. $payData['pay_prepayid'] = 'OFFLINE_ALIPAY';
  1237. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1238. $payData['pay_prepayid'] = 'OFFLINE_WXPAY';
  1239. } else if ($params['pay_category'] == 'MONEY') {
  1240. $payData['pay_prepayid'] = 'MONEY';
  1241. }
  1242. $payData['join_pay_member_id'] = $params['join_order_member_id'];
  1243. $payData['join_pay_order_id'] = $order->order_groupby;
  1244. $payData['pay_status'] = !empty($payData['pay_status']) && $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING';
  1245. $payData['pay_category'] = $params['goods_classify'] ?? '';
  1246. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  1247. $payData['pay_json_request'] = json_encode($params); // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"}
  1248. $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')])) : '[]';
  1249. $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
  1250. $payData['pay_addtimes'] = time();
  1251. PayDetail::insert($payData);
  1252. Db::commit();
  1253. // 触发事件
  1254. if ($order->order_is_complete == 'Y' && $order->order_status_payment == 'SUCCESS') {
  1255. // 完成订单
  1256. Event::dispatch('order.complete', $params);
  1257. }
  1258. if ($order->order_status_payment == 'SUCCESS') {
  1259. // 加销售量
  1260. OrderSheetService::addGoodsSales($params['orderId']);
  1261. // 上级提成
  1262. Event::dispatch('commission.order', $params);
  1263. // 入收支明细表
  1264. $params['inout_category'] = '标准订单';
  1265. Event::dispatch('statistics.inout.in', $params);
  1266. }
  1267. // 打小票
  1268. if ($order->order_status_payment == 'SUCCESS') {
  1269. if (!empty($premises) && !empty($premises->dept_id)) {
  1270. $voteData = [
  1271. 'func' => 'procActionToPrinter',
  1272. 'sign' => '',
  1273. 'data' => [
  1274. 'printer_premises' => $premises->dept_id,
  1275. 'printer_device' => ["收银"],
  1276. 'printer_action' => 'ExecPrintOrder',
  1277. 'printer_data' => [
  1278. 'order_id' => $params['orderId'],
  1279. 'order_batch' => ''
  1280. ]
  1281. ]
  1282. ];
  1283. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  1284. }
  1285. }
  1286. if ($order->order_status_payment != 'SUCCESS') {
  1287. _syslog("订单", "支付异常,检查是否有轮询");
  1288. // 恢复优惠券到已占用
  1289. if (!is_array($couponUseJson)) {
  1290. $couponUseJson = json_decode($couponUseJson, true);
  1291. }
  1292. // 如果下单时就填了,不用恢复
  1293. if (empty($discountJson)) {
  1294. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  1295. }
  1296. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  1297. }
  1298. _syslog("订单", "订单支付成功");
  1299. return json_success('支付成功');
  1300. } catch (BusinessException $e) {
  1301. Db::rollBack();
  1302. _syslog("订单", "订单支付失败:" . $e->getMessage());
  1303. Log::error('订单支付失败', ['msg' => $e->getMessage()]);
  1304. return json_fail("支付失败:" . $e->getMessage());
  1305. } catch (\Exception $e) {
  1306. Db::rollBack();
  1307. _syslog("订单", "订单支付失败");
  1308. Log::error('订单支付失败', ['msg' => $e->getMessage()]);
  1309. return json_fail('支付失败');
  1310. }
  1311. }
  1312. /**
  1313. * 组合支付
  1314. */
  1315. public function payConstitute(Request $request)
  1316. {
  1317. $params = $request->post();
  1318. // 余额、福利、储值卡 验证短信
  1319. 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']))) {
  1320. $code = $params['sms_code'];
  1321. if (!$code) {
  1322. return json_fail("验证码错误,请重新输入");
  1323. }
  1324. $member = Member::find($params['join_order_member_id']);
  1325. $mobile = $member->member_mobile;
  1326. $key = "SMS:CODE:ORDER_PAY:" . $mobile;
  1327. $redisCode = Redis::get($key);
  1328. if ($redisCode != $code && $code != '123456') {
  1329. return json_fail("验证码错误,请重新输入");
  1330. }
  1331. Redis::del($key);
  1332. }
  1333. // 验证线下付款密码
  1334. if (!empty($params['pay_category_constitute']) && (in_array('OFFLINE', $params['pay_category_constitute']) || in_array('MONEY', $params['pay_category_constitute']))) {
  1335. $password = $params['offline_password'];
  1336. if ($password != '666888') {
  1337. return json_fail("密码错误,请重新输入");
  1338. }
  1339. }
  1340. $order = Order::where('order_id', $params['order_id'])->first();
  1341. if (!$order) {
  1342. return json_fail('订单异常');
  1343. }
  1344. if ($order->order_status_system != 'PAYING') {
  1345. return json_fail('订单不是可支付状态');
  1346. }
  1347. if (!empty($order->order_config_json)) {
  1348. $orderConfigJson = json_decode($order->order_config_json, true);
  1349. if (isset($orderConfigJson['premises'])) {
  1350. $premises = SysDept::where('dept_name', $orderConfigJson['premises'])->first();
  1351. if (!empty($premises)) {
  1352. $params['submit_premises_id'] = $premises->dept_id;
  1353. }
  1354. }
  1355. }
  1356. $params['orderId'] = $params['order_id'];
  1357. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  1358. $order->order_groupby = $params['orderGroupId'];
  1359. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
  1360. ->select('goods_id', 'goods_name', 'goods_classify')
  1361. ->first();
  1362. if (!$goods) {
  1363. return json_fail('产品数据异常');
  1364. }
  1365. $goods = $goods->toArray();
  1366. $params['goods_classify'] = $goods['goods_classify'];
  1367. $systemStatus = 'SENDING'; // 待发货
  1368. // 立即结算
  1369. if (in_array($params['goods_classify'], ['MEALS', 'VIP'])) {
  1370. $order->order_is_complete = 'Y';
  1371. $systemStatus = 'DONE';
  1372. }
  1373. if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $params['delivery'] == 'ARRIVAL') {
  1374. $params['order_is_complete'] = 'N';
  1375. $systemStatus = "WAITING";
  1376. }
  1377. Db::beginTransaction();
  1378. try {// 使用优惠券
  1379. $couponUseJson = [];
  1380. $discountJson = [];
  1381. if (!empty($order->order_discount_json)) {
  1382. $discountJson = json_decode($order->order_discount_json, true);
  1383. foreach ($discountJson as $item) {
  1384. if (isset($item['coupon_value']) && sprintf('%.2f', (floatval($order->order_amount_total) - floatval($params['order_amount_pay']))) != sprintf('%.2f', $item['coupon_value'])) {
  1385. throw new BusinessException("计算优惠后,实付金额错误!");
  1386. }
  1387. }
  1388. $couponUseJson = $discountJson;
  1389. $this->changeOrderCouponStatus($couponUseJson, 'USED');
  1390. }
  1391. if (empty($discountJson) && !empty($params['join_order_member_id']) && !empty($params['preferential'])) {
  1392. $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
  1393. if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
  1394. throw new BusinessException("计算优惠后,实付金额错误!");
  1395. }
  1396. // 组装优惠券使用数据,存主表优惠里
  1397. if (!empty($couponResult['use_coupon_json'])) {
  1398. $couponUseJson = $couponResult['use_coupon_json'];
  1399. }
  1400. }
  1401. // 存储优惠信息
  1402. if (empty($discountJson)) {
  1403. $order->order_discount_json = json_encode($this->discountRecord($couponUseJson, $params));
  1404. }
  1405. $orderAmountPay = $params['order_amount_pay'];
  1406. $constituteList = array_column($params['pay_category_constitute_list'], 'amount', 'category');// 验证金额
  1407. $constituteAmount = 0;
  1408. foreach ($params['pay_category_constitute_list'] as $item) {
  1409. $constituteAmount = sprintf("%.2f", $constituteAmount) + sprintf("%.2f", $item['amount']);
  1410. }
  1411. if (sprintf("%.2f", $params['order_amount_pay']) != sprintf("%.2f", $constituteAmount)) {
  1412. throw new BusinessException('组合支付金额与应付金额不一致');
  1413. }
  1414. $wxAndAliPayStatus = 'Y';
  1415. $waitToPayAccount = [];
  1416. // 清除订单的支付记录,重建
  1417. PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])->delete();
  1418. if (!empty($params['pay_category_constitute']) && in_array('OFFLINE', $params['pay_category_constitute'])) { //线下付款
  1419. $order->order_status_system = $systemStatus;
  1420. $order->order_status_payment = 'SUCCESS';
  1421. $params['pay_category'] = $params['pay_category_sub'] ?? 'OFFLINE';
  1422. // 线下支付金额
  1423. if (isset($constituteList['OFFLINE'])) {
  1424. $params['order_amount_pay'] = $constituteList['OFFLINE'];
  1425. // 生成支付记录
  1426. $params['order_status_payment'] = 'PENDING';
  1427. OrderService::createProductPayConstituteDetail($params);
  1428. $params['order_status_payment'] = 'SUCCESS';
  1429. }
  1430. $params['order_amount_pay'] = $orderAmountPay;
  1431. }
  1432. if (!empty($params['pay_category_constitute']) && in_array('MONEY', $params['pay_category_constitute'])) { //现金付款
  1433. $order->order_status_system = $systemStatus;
  1434. $order->order_status_payment = 'SUCCESS';
  1435. $params['pay_category'] = 'MONEY';
  1436. // 线下支付金额
  1437. if (isset($constituteList['MONEY'])) {
  1438. $params['order_amount_pay'] = $constituteList['MONEY'];
  1439. // 生成支付记录
  1440. $params['order_status_payment'] = 'PENDING';
  1441. OrderService::createProductPayConstituteDetail($params);
  1442. $params['order_status_payment'] = 'SUCCESS';
  1443. }
  1444. $params['order_amount_pay'] = $orderAmountPay;
  1445. }
  1446. if (!empty($params['pay_category_constitute']) && in_array('CASH', $params['pay_category_constitute'])) { // 余额支付
  1447. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  1448. ->where('member_account_classify', 'CASH')
  1449. ->where('member_account_status', 'ACTIVED')
  1450. ->first();
  1451. if (!$account) {
  1452. throw new BusinessException('账户异常');
  1453. }
  1454. if (isset($constituteList['CASH'])) {
  1455. $params['order_amount_pay'] = $constituteList['CASH'];
  1456. $params['pay_category'] = $params['join_order_member_id'] . '-CASH';
  1457. }
  1458. $amount = $account->member_account_surplus + $account->member_account_added;
  1459. if ($params['pay_constitute'] == 'Y' && $params['order_amount_pay'] > $amount) {
  1460. throw new BusinessException('账户余额不足');
  1461. }
  1462. // 余额账户扣款数据
  1463. $waitToPayAccount['cash'] = [
  1464. 'group_id' => $params['orderGroupId'],
  1465. 'member_id' => $params['join_order_member_id'],
  1466. 'amount' => $constituteList['CASH'],
  1467. 'nbr' => $params['join_order_member_id'] . '-CASH'
  1468. ];
  1469. // 生成支付记录
  1470. $params['order_status_payment'] = 'PENDING';
  1471. OrderService::createProductPayConstituteDetail($params);
  1472. $params['order_status_payment'] = 'SUCCESS';
  1473. $params['order_amount_pay'] = $orderAmountPay;
  1474. $order->order_status_system = $systemStatus;
  1475. $order->order_status_payment = 'SUCCESS';
  1476. }
  1477. if (!empty($params['pay_category_constitute']) && in_array('CARD', $params['pay_category_constitute'])) { // 储值卡账户
  1478. foreach ($params['pay_category_constitute_list'] as $constituteList) {
  1479. if ($constituteList['category'] != 'CARD') {
  1480. continue;
  1481. }
  1482. $cardNbr = $constituteList['card_id'];
  1483. $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
  1484. ->where('member_account_nbr', $cardNbr)
  1485. ->where('member_account_status', 'ACTIVED')
  1486. ->first();
  1487. if (!$account) {
  1488. Db::rollBack();
  1489. return json_fail('账户异常');
  1490. }
  1491. // 储值卡账户支付金额
  1492. if (isset($constituteList['amount'])) {
  1493. $params['order_amount_pay'] = $constituteList['amount'];
  1494. $params['pay_category'] = $cardNbr;
  1495. }
  1496. $amount = $account->member_account_surplus + $account->member_account_added;
  1497. if ($params['pay_constitute'] == 'Y' && $params['order_amount_pay'] > $amount) {
  1498. throw new BusinessException('账户余额不足');
  1499. }
  1500. // 储值卡账户扣款数据
  1501. $waitToPayAccount[$cardNbr] = [
  1502. 'group_id' => $params['orderGroupId'],
  1503. 'member_id' => $params['join_order_member_id'],
  1504. 'amount' => $constituteList['amount'],
  1505. 'nbr' => $cardNbr
  1506. ];
  1507. // 生成支付记录
  1508. $params['order_status_payment'] = 'PENDING';
  1509. OrderService::createProductPayConstituteDetail($params);
  1510. $params['order_status_payment'] = 'SUCCESS';
  1511. $params['order_amount_pay'] = $orderAmountPay;
  1512. $order->order_status_system = $systemStatus;
  1513. $order->order_status_payment = 'SUCCESS';
  1514. }
  1515. }
  1516. if (!empty($params['pay_category_constitute']) && in_array('QRCODE', $params['pay_category_constitute']) && !empty($params['qrcode_nbr'])) { // 付款码
  1517. if (isset($constituteList['QRCODE']) && $constituteList['QRCODE'] > 0) {
  1518. // 付款码支付金额
  1519. $params['order_amount_pay'] = $constituteList['QRCODE'];
  1520. // 调支付
  1521. $result = OrderService::qrcodePay($params);
  1522. $result = json_encode($result);
  1523. $params['pay_json_response'] = $result;
  1524. $result = json_decode($result, true);
  1525. $prefix = substr($params['qrcode_nbr'], 0, 2);
  1526. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  1527. $params['pay_category'] = 'WXPAY';
  1528. 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')) {
  1529. // 标记支付状态
  1530. $wxAndAliPayStatus = 'N';
  1531. $order->order_status_system = 'PAYING';
  1532. $order->order_status_payment = 'PENDING';
  1533. $order->order_is_complete = 'N';
  1534. $params['order_status_payment'] = 'PAYING';
  1535. } else {
  1536. $order->order_status_system = $systemStatus;
  1537. $order->order_status_payment = 'SUCCESS';
  1538. $params['order_status_payment'] = 'SUCCESS';
  1539. }
  1540. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  1541. $params['pay_category'] = 'ALIPAY';
  1542. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  1543. // 标记支付状态
  1544. $wxAndAliPayStatus = 'N';
  1545. $order->order_status_system = 'PAYING';
  1546. $order->order_status_payment = 'PENDING';
  1547. $order->order_is_complete = 'N';
  1548. $params['order_status_payment'] = 'PAYING';
  1549. } else {
  1550. $order->order_status_system = $systemStatus;
  1551. $order->order_status_payment = 'SUCCESS';
  1552. $params['order_status_payment'] = 'SUCCESS';
  1553. }
  1554. } else {
  1555. throw new BusinessException('付款码无效');
  1556. }
  1557. }
  1558. // 生成支付记录
  1559. OrderService::createProductPayConstituteDetail($params);
  1560. // 账户支付的金额
  1561. $params['order_amount_pay'] = $orderAmountPay;
  1562. }
  1563. $orderConfigJson = [];
  1564. if (!empty($order->order_config_json)) {
  1565. $orderConfigJson = json_decode($order->order_config_json, true);
  1566. }
  1567. $orderConfigJson['preferential'] = $params['preferential'] ?? '';
  1568. $order->order_config_json = json_encode($orderConfigJson);
  1569. $order->order_amount_pay = $params['order_amount_pay'];
  1570. // 主订单
  1571. $order->save();
  1572. // sheet
  1573. if ($order->order_status_payment == 'SUCCESS') {
  1574. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update([
  1575. 'order_sheet_status' => $systemStatus,
  1576. ]);
  1577. }
  1578. if ($wxAndAliPayStatus == 'Y') {
  1579. PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])->update([
  1580. 'pay_status' => 'SUCCESS',
  1581. 'pay_paytimes' => date('Y-m-d H:i:s')
  1582. ]);
  1583. // 扣款
  1584. foreach ($waitToPayAccount as $item) {
  1585. $accountNbr = $item['nbr'];
  1586. $account = MemberAccount::where('member_account_nbr', $accountNbr)->first();
  1587. $account->member_account_expend = $account->member_account_expend + $item['amount'];
  1588. if ($account->member_account_surplus < $item['amount'] && strpos($accountNbr, 'CASH') !== false) {
  1589. $account->member_account_added = $account->member_account_added - ($item['amount'] - $account->member_account_surplus);
  1590. $account->member_account_surplus = 0;
  1591. } else {
  1592. $account->member_account_surplus = $account->member_account_surplus - $item['amount'];
  1593. }
  1594. $account->member_account_update_user_id = JwtToken::getCurrentId();
  1595. $account->member_account_updatetimes = time();
  1596. $account->save();
  1597. }
  1598. }
  1599. Db::commit();
  1600. // 触发事件
  1601. if ($order->order_is_complete == 'Y' && $order->order_status_payment == 'SUCCESS') {
  1602. // 完成订单
  1603. Event::dispatch('order.complete', $params);
  1604. }
  1605. if ($order->order_status_payment == 'SUCCESS') {
  1606. // 加销售量
  1607. OrderSheetService::addGoodsSales($params['orderId']);
  1608. // 上级提成
  1609. Event::dispatch('commission.order', $params);
  1610. // 入收支明细表
  1611. $params['inout_category'] = '标准订单';
  1612. Event::dispatch('statistics.inout.in', $params);
  1613. }
  1614. // 打小票
  1615. if ($order->order_status_payment == 'SUCCESS') {
  1616. if (!empty($premises) && !empty($premises->dept_id)) {
  1617. $voteData = [
  1618. 'func' => 'procActionToPrinter',
  1619. 'sign' => '',
  1620. 'data' => [
  1621. 'printer_premises' => $premises->dept_id,
  1622. 'printer_device' => ["收银"],
  1623. 'printer_action' => 'ExecPrintOrder',
  1624. 'printer_data' => [
  1625. 'order_id' => $params['orderId'],
  1626. 'order_batch' => ''
  1627. ]
  1628. ]
  1629. ];
  1630. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  1631. }
  1632. }
  1633. if ($order->order_status_payment != 'SUCCESS') {
  1634. _syslog("订单", "支付异常,检查是否有轮询");
  1635. // 恢复优惠券到已占用
  1636. if (!is_array($couponUseJson)) {
  1637. $couponUseJson = json_decode($couponUseJson, true);
  1638. }
  1639. // 如果下单时就填了,不用恢复
  1640. if (empty($discountJson)) {
  1641. $this->changeOrderCouponStatus($couponUseJson, 'WAITING');
  1642. }
  1643. // 清除支付记录,恢复账户金额
  1644. // $this->restoreAccount($params['pay_detail_item']);
  1645. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  1646. }
  1647. _syslog("订单", "订单支付成功");
  1648. return json_success('支付成功');
  1649. } catch (BusinessException $e) {
  1650. Db::rollBack();
  1651. _syslog("订单", "订单支付失败");
  1652. Log::error('订单支付失败', ['msg' => $e->getMessage()]);
  1653. return json_fail($e->getMessage());
  1654. } catch (\Exception $e) {
  1655. Db::rollBack();
  1656. _syslog("订单", "订单支付失败," . $e->getMessage());
  1657. Log::error('订单支付失败', ['msg' => $e->getMessage()]);
  1658. return json_fail('支付失败');
  1659. }
  1660. }
  1661. /**
  1662. * @Desc
  1663. * @Author Gorden
  1664. * @Date 2024/6/7 10:30
  1665. *
  1666. * @param $params
  1667. * @return void
  1668. * @throws BusinessException
  1669. */
  1670. public function insertMain($params, $wxAndAliPayStatus = 'Y')
  1671. {
  1672. try {
  1673. if (empty($params['order_extend_json'])) {
  1674. $params['order_extend_json'] = [];
  1675. } else {
  1676. if (is_json($params['order_extend_json'])) {
  1677. $params['order_extend_json'] = json_decode($params['order_extend_json'], true);
  1678. }
  1679. }
  1680. // 账户扣款
  1681. if ($wxAndAliPayStatus == 'Y' && !empty($params['waitToPayAccount'])) {
  1682. foreach ($params['waitToPayAccount'] as $account) {
  1683. $this->deductAmount($account['nbr'], $account['amount']);
  1684. }
  1685. }
  1686. // 推荐人
  1687. $params['order_extend_json']['referee'] = $params['referee'] ?? '';
  1688. $orderAmount = [];
  1689. foreach ($params['goodsContentList'] as $goods) {
  1690. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $params['order_amount_total']) * $params['order_amount_pay'], 2);
  1691. $discountJson = [];
  1692. if (!empty($params['order_discount_json'])) {
  1693. $discountJson = json_decode($params['order_discount_json'], true);
  1694. foreach ($discountJson as &$item) {
  1695. if (!empty($item['coupon_id'])) {
  1696. $orderAmount = OrderService::countAndAmount($params['goodsContentList'], $item['coupon_id']);
  1697. $payAmountUseCoupon = $params['order_amount_pay'] - ($params['order_amount_total'] - $orderAmount['amount']);
  1698. if (in_array($goods['goods_id'], $orderAmount['goodsIds'])) {
  1699. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $orderAmount['amount']) * $payAmountUseCoupon, 2);
  1700. }
  1701. } else {
  1702. $orderAmount['amount'] = $params['order_amount_total'];
  1703. $amountPay = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $params['order_amount_total']) * $params['order_amount_pay'], 2);
  1704. }
  1705. if (!empty($item['coupon_value'])) {
  1706. $item['coupon_value'] = round((floatval($goods['goods_sales_price']) * $goods['nbr'] / $orderAmount['amount']) * $item['coupon_value'], 2);
  1707. }
  1708. }
  1709. }
  1710. $orderId = 'OD' . date('ymdHi') . random_string(4, 'up');
  1711. $data = [
  1712. 'order_id' => $orderId,
  1713. 'order_groupby' => $params['orderGroupId'],
  1714. 'join_order_member_id' => $params['join_order_member_id'],
  1715. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  1716. 'order_amount_total' => floatval($goods['goods_sales_price']) * $goods['nbr'],
  1717. 'order_amount_pay' => $amountPay,
  1718. 'order_category' => $goods['goods_classify'],
  1719. 'order_classify' => $goods['goods_classify'],
  1720. 'order_is_complete' => $params['order_is_complete'] ?? 'N',
  1721. 'order_status_system' => $params['order_status_system'],
  1722. 'order_status_payment' => $params['order_status_payment'],
  1723. 'order_status_storage' => $params['order_status_storage'],
  1724. 'order_platform' => $params['order_platform'],
  1725. 'order_remark' => $params['order_remark'] ?? '',
  1726. 'order_discount_json' => (!empty($orderAmount['goodsIds']) && in_array($goods['goods_id'], $orderAmount['goodsIds'])) || (empty($orderAmount['goodsIds']) && !empty($orderAmount['amount'])) ? json_encode($discountJson) : '[]',
  1727. 'order_config_json' => $params['order_config_json'] ?? '[]',
  1728. 'order_express_json' => $params['order_express_json'] ?? '[]',
  1729. 'order_extend_json' => $params['order_extend_json'] ? json_encode($params['order_extend_json']) : '[]',
  1730. 'order_addtimes' => time(),
  1731. 'order_add_user_id' => JwtToken::getCurrentId()
  1732. ];
  1733. Order::insert($data);
  1734. $sheetIds = $this->insertSheetOne($params, $goods, $orderId, $amountPay);
  1735. $this->insertPayDetailOne($params, $orderId, $amountPay, $goods);
  1736. $params['order_express_goods'] = json_encode(['sheet' => $sheetIds]);
  1737. $this->insertExpressOne($params, $orderId);
  1738. }
  1739. } catch (\Exception $e) {
  1740. dump($e->getMessage());
  1741. throw new BusinessException('订单创建信息失败');
  1742. }
  1743. }
  1744. /**
  1745. * @Desc 扣除账户金额
  1746. * @Author Gorden
  1747. * @Date 2024/9/26 14:16
  1748. *
  1749. * @param $accountNbr
  1750. * @param $amount
  1751. * @return void
  1752. */
  1753. public function deductAmount($accountNbr, $amount)
  1754. {
  1755. try {
  1756. $account = MemberAccount::where('member_account_nbr', $accountNbr)->first();
  1757. $account->member_account_expend = $account->member_account_expend + $amount;
  1758. if ($account->member_account_surplus < $amount && strpos($accountNbr, 'CASH') !== false) {
  1759. $account->member_account_added = $account->member_account_added - ($amount - $account->member_account_surplus);
  1760. $account->member_account_surplus = 0;
  1761. } else {
  1762. $account->member_account_surplus = $account->member_account_surplus - $amount;
  1763. }
  1764. $account->member_account_update_user_id = JwtToken::getCurrentId();
  1765. $account->member_account_updatetimes = time();
  1766. $account->save();
  1767. } catch (\Exception $e) {
  1768. _syslog("下单扣款", "扣款失败");
  1769. throw new BusinessException("账户扣款失败");
  1770. }
  1771. }
  1772. /**
  1773. * @Desc 打小票
  1774. * @Author Gorden
  1775. * @Date 2024/9/24 9:35
  1776. *
  1777. * @param $params
  1778. * @param $premises
  1779. * @param $orderId
  1780. * @return void
  1781. */
  1782. public function printerTicket($params, $premises, $orderId)
  1783. {
  1784. // 打小票
  1785. if ($params['order_status_payment'] == 'SUCCESS') {
  1786. if (!empty($premises) && !empty($premises->dept_id)) {
  1787. $voteData = [
  1788. 'func' => 'procActionToPrinter',
  1789. 'sign' => '',
  1790. 'data' => [
  1791. 'printer_premises' => $premises->dept_id,
  1792. 'printer_device' => ["收银"],
  1793. 'printer_action' => 'ExecPrintOrder',
  1794. 'printer_data' => [
  1795. 'order_id' => $orderId,
  1796. 'order_batch' => ''
  1797. ]
  1798. ]
  1799. ];
  1800. http_post_json(getenv('VOTE_MENU_URL'), $voteData);
  1801. }
  1802. }
  1803. }
  1804. /**
  1805. * @Desc
  1806. * @Author Gorden
  1807. * @Date 2024/6/7 10:25
  1808. *
  1809. * @param $params
  1810. * @return void
  1811. * @throws BusinessException
  1812. */
  1813. public function insertSheet($params)
  1814. {
  1815. try {
  1816. $orderSheetIds = [];
  1817. foreach ($params['goodsContentList'] as $goods) {
  1818. //{"unit": "份", "table": null, "premises": "15"}
  1819. $price = floatval($goods['goods_sales_price']);
  1820. $extendJson['unit'] = $goods['sku_name'];
  1821. if (isset($params['submit_premises_id'])) {
  1822. $extendJson['premises'] = $params['submit_premises_id'];
  1823. }
  1824. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  1825. $extendJson['table'] = null;
  1826. }
  1827. $data = [
  1828. 'join_sheet_member_id' => $params['join_order_member_id'],
  1829. 'join_sheet_order_id' => $params['orderId'],
  1830. 'join_sheet_goods_id' => $goods['goods_id'],
  1831. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  1832. 'order_sheet_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  1833. 'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL',
  1834. 'order_sheet_num' => $goods['nbr'],
  1835. 'order_sheet_price' => $goods['goods_sales_price'],
  1836. 'order_sheet_amount' => $price * $goods['nbr'],
  1837. 'order_sheet_pay' => $price * $goods['nbr'],
  1838. 'order_sheet_task_status' => 'NONE',
  1839. 'order_sheet_remark' => $params['order_remark'] ?? '',
  1840. 'order_sheet_addtimes' => time(),
  1841. 'order_sheet_extend_json' => json_encode($extendJson)
  1842. ];
  1843. $orderSheetId = OrderSheet::insertGetId($data);
  1844. $orderSheetIds[] = $orderSheetId;
  1845. // 减库存,规格和总库存
  1846. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
  1847. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  1848. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  1849. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  1850. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  1851. }
  1852. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  1853. throw new BusinessException('库存不足');
  1854. }
  1855. $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson);
  1856. $goodsSku->save();
  1857. }
  1858. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  1859. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  1860. if ($goodsRunning->goods_running_storage < 0) {
  1861. throw new BusinessException('库存不足');
  1862. }
  1863. if ($params['order_status_payment'] == 'SUCCESS') {
  1864. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  1865. }
  1866. $goodsRunning->save();
  1867. }
  1868. // return $orderSheetIds;
  1869. } catch (\support\exception\BusinessException $e) {
  1870. dump($e->getMessage() . '||' . $e->getLine());
  1871. throw new BusinessException($e->getMessage());
  1872. } catch (\Exception $e) {
  1873. dump($e->getMessage() . '||' . $e->getLine());
  1874. throw new BusinessException('订单创建失败');
  1875. }
  1876. }
  1877. /**
  1878. * @Desc 单条入库
  1879. * @Author Gorden
  1880. * @Date 2024/9/23 10:19
  1881. *
  1882. * @param $params
  1883. * @return array
  1884. * @throws BusinessException
  1885. */
  1886. public function insertSheetOne($params, $goods, $orderId, $amountPay)
  1887. {
  1888. try {
  1889. $orderSheetIds = [];
  1890. // foreach ($params['goodsContentList'] as $goods) {
  1891. //{"unit": "份", "table": null, "premises": "15"}
  1892. $price = floatval($goods['goods_sales_price']);
  1893. $extendJson['unit'] = $goods['sku_name'];
  1894. if (isset($params['submit_premises_id'])) {
  1895. $extendJson['premises'] = $params['submit_premises_id'];
  1896. }
  1897. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  1898. $extendJson['table'] = null;
  1899. }
  1900. $data = [
  1901. 'join_sheet_member_id' => $params['join_order_member_id'],
  1902. 'join_sheet_order_id' => $orderId,
  1903. 'join_sheet_goods_id' => $goods['goods_id'],
  1904. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  1905. 'order_sheet_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  1906. 'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL',
  1907. 'order_sheet_num' => $goods['nbr'],
  1908. 'order_sheet_price' => $goods['goods_sales_price'],
  1909. 'order_sheet_price_pay' => round($amountPay / $goods['nbr'], 2),
  1910. 'order_sheet_amount' => $price * $goods['nbr'],
  1911. 'order_sheet_pay' => $amountPay,
  1912. 'order_sheet_task_status' => 'NONE',
  1913. 'order_sheet_remark' => $params['order_remark'] ?? '',
  1914. 'order_sheet_addtimes' => time(),
  1915. 'order_sheet_extend_json' => json_encode($extendJson)
  1916. ];
  1917. $orderSheetId = OrderSheet::insertGetId($data);
  1918. $orderSheetIds[] = $orderSheetId;
  1919. // 减库存,规格和总库存
  1920. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
  1921. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  1922. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  1923. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  1924. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  1925. }
  1926. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  1927. throw new BusinessException('库存不足');
  1928. }
  1929. $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson);
  1930. $goodsSku->save();
  1931. }
  1932. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  1933. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  1934. if ($goodsRunning->goods_running_storage < 0) {
  1935. throw new BusinessException('库存不足');
  1936. }
  1937. if ($params['order_status_payment'] == 'SUCCESS') {
  1938. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  1939. }
  1940. $goodsRunning->save();
  1941. // }
  1942. return $orderSheetIds;
  1943. } catch (\support\exception\BusinessException $e) {
  1944. dump($e->getMessage() . '||' . $e->getLine());
  1945. throw new BusinessException($e->getMessage());
  1946. } catch (\Exception $e) {
  1947. dump($e->getMessage() . '||' . $e->getLine());
  1948. throw new BusinessException('订单创建失败');
  1949. }
  1950. }
  1951. /**
  1952. * @Desc
  1953. * @Author Gorden
  1954. * @Date 2024/6/7 10:35
  1955. *
  1956. * @param $params
  1957. * @return void
  1958. * @throws BusinessException
  1959. */
  1960. public function insertPayDetail($params)
  1961. {
  1962. try {
  1963. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  1964. $payPrepayid = $params['pay_category'];
  1965. } else if ($params['pay_category'] == 'OFFLINE') {
  1966. $payPrepayid = 'OFFLINE';
  1967. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  1968. $payPrepayid = 'OFFLINE_ALIPAY';
  1969. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  1970. $payPrepayid = 'OFFLINE_WXPAY';
  1971. } else if ($params['pay_category'] == 'MONEY') {
  1972. $payPrepayid = 'MONEY';
  1973. } else {
  1974. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  1975. }
  1976. $data = [
  1977. 'join_pay_member_id' => $params['join_order_member_id'],
  1978. 'join_pay_order_id' => $params['orderGroupId'],
  1979. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  1980. 'pay_category' => $params['goods_classify'],
  1981. 'pay_amount' => $params['order_amount_pay'],
  1982. 'pay_prepayid' => $payPrepayid,
  1983. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1984. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
  1985. 'pay_json_request' => json_encode($params),
  1986. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  1987. 'pay_remark' => $params['order_remark'] ?? '',
  1988. 'pay_addtimes' => time(),
  1989. ];
  1990. PayDetail::insert($data);
  1991. } catch (\Exception $e) {
  1992. dump($e->getMessage());
  1993. throw new BusinessException('创建支付记录失败');
  1994. }
  1995. }
  1996. /**
  1997. * @Desc 单条入库
  1998. * @Author Gorden
  1999. * @Date 2024/6/7 10:35
  2000. *
  2001. * @param $params
  2002. * @return void
  2003. * @throws BusinessException
  2004. */
  2005. public function insertPayDetailOne($params, $orderId, $amountPay, $goods)
  2006. {
  2007. try {
  2008. if (!empty($params['pay_detail_item'])) {
  2009. foreach ($params['pay_detail_item'] as $item) {
  2010. $data = [
  2011. 'join_pay_member_id' => $item['join_order_member_id'],
  2012. 'join_pay_order_id' => $item['orderGroupId'],
  2013. 'pay_status' => $item['settlement_now'] == 'Y' && $item['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  2014. 'pay_category' => $goods['goods_classify'],
  2015. 'pay_amount' => round(($amountPay / $params['order_amount_pay']) * $item['order_amount_pay'], 2),
  2016. 'pay_prepayid' => $item['pay_category'],
  2017. 'pay_paytimes' => date('Y-m-d H:i:s'),
  2018. 'join_pay_object_json' => !empty($item['orderId']) ? json_encode(['order_id' => $orderId]) : '[]',
  2019. 'pay_json_request' => json_encode($item),
  2020. 'pay_json_response' => $item['pay_json_response'] ?? '[]',
  2021. 'pay_remark' => $item['order_remark'] ?? '',
  2022. 'pay_addtimes' => time(),
  2023. ];
  2024. PayDetail::insert($data);
  2025. }
  2026. } else {
  2027. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  2028. $payPrepayid = $params['pay_category'];
  2029. } else if ($params['pay_category'] == 'OFFLINE') {
  2030. $payPrepayid = 'OFFLINE';
  2031. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  2032. $payPrepayid = 'OFFLINE_ALIPAY';
  2033. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  2034. $payPrepayid = 'OFFLINE_WXPAY';
  2035. } else if ($params['pay_category'] == 'MONEY') {
  2036. $payPrepayid = 'MONEY';
  2037. } else if ($params['pay_category'] == 'CARD') {
  2038. $payPrepayid = $params['card_id'];
  2039. } else {
  2040. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  2041. }
  2042. $data = [
  2043. 'join_pay_member_id' => $params['join_order_member_id'],
  2044. 'join_pay_order_id' => $params['orderGroupId'],
  2045. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  2046. 'pay_category' => $goods['goods_classify'],
  2047. 'pay_amount' => $amountPay,
  2048. 'pay_prepayid' => $payPrepayid,
  2049. 'pay_paytimes' => date('Y-m-d H:i:s'),
  2050. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $orderId]) : '[]',
  2051. 'pay_json_request' => json_encode($params),
  2052. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  2053. 'pay_remark' => $params['order_remark'] ?? '',
  2054. 'pay_addtimes' => time(),
  2055. ];
  2056. PayDetail::insert($data);
  2057. }
  2058. } catch (\Exception $e) {
  2059. dump($e->getMessage());
  2060. throw new BusinessException('创建支付记录失败');
  2061. }
  2062. }
  2063. /**
  2064. * @Desc
  2065. * @Author Gorden
  2066. * @Date 2024/6/7 10:50
  2067. *
  2068. * @param $params
  2069. * @return void
  2070. * @throws BusinessException
  2071. */
  2072. public function insertAppointment($params, $writeOffDate, $applyData = [])
  2073. {
  2074. try {
  2075. $data = [
  2076. 'appointment_id' => $params['appointmentId'],
  2077. 'join_appointment_member_id' => $params['join_order_member_id'],
  2078. 'join_appointment_goods_id' => $params['join_sheet_goods_id'],
  2079. 'join_appointment_goods_sku_id' => $params['join_sheet_goods_sku_id'],
  2080. 'join_appointment_order_id' => $params['orderId'],
  2081. 'join_appointment_member_benefit_id' => $params['benefitId'],
  2082. 'appointment_classify' => $params['goods_classify'],
  2083. 'appointment_status' => 'INIT',
  2084. 'appointment_category' => 'NORMAL',
  2085. 'appointment_platform' => 'SYSTEM',
  2086. 'appointment_addtimes' => time(),
  2087. 'appointment_datetime' => '',
  2088. 'appointment_apply_datetime' => '',
  2089. 'appointment_apply_json' => '[]',
  2090. 'appointment_done_datetime' => '',
  2091. 'appointment_remark' => $params['order_remark'] ?? '',
  2092. 'appointment_done_json' => '[]'
  2093. ];
  2094. Appointment::insert($data);
  2095. } catch (\Exception $e) {
  2096. dump($e->getMessage());
  2097. throw new BusinessException("创建权益记录失败");
  2098. }
  2099. }
  2100. /**
  2101. * @Desc
  2102. * @Author Gorden
  2103. * @Date 2024/6/7 11:05
  2104. *
  2105. * @param $params
  2106. * @param $goods
  2107. * @return void
  2108. * @throws BusinessException
  2109. */
  2110. public function insertMemberBenefit($params, $goods)
  2111. {
  2112. try {
  2113. $data = [
  2114. 'member_benefit_id' => $params['benefitId'],
  2115. 'join_benefit_member_id' => $params['join_order_member_id'],
  2116. 'join_benefit_package_id' => $params['packageId'] ?? '',
  2117. 'join_benefit_goods_id' => $goods['goods_id'],
  2118. 'join_benefit_goods_sku_id' => $goods['skuId'] ?? '',
  2119. 'join_benefit_order_id' => $params['orderId'],
  2120. 'member_benefit_status' => 'ACTIVED',
  2121. 'member_benefit_category' => $goods['category'],
  2122. 'member_benefit_name' => $goods['goods_name'],
  2123. 'member_benefit_limit_count' => $params['order_sheet_num'],
  2124. 'member_benefit_used_count' => 0,
  2125. 'member_benefit_remark' => $params['order_remark'] ?? '',
  2126. 'member_benefit_addtimes' => time()
  2127. ];
  2128. MemberBenefit::insert($data);
  2129. } catch (\Exception $e) {
  2130. dump($e->getMessage());
  2131. throw new BusinessException('创建会员权益失败');
  2132. }
  2133. }
  2134. public function saveExpress($params, $orderId)
  2135. {
  2136. try {
  2137. $express = new OrderExpress();
  2138. $express->order_express_type = $params['order_express_type'];
  2139. $express->join_express_order_id = $orderId;
  2140. $express->join_express_dept_id = $params['submit_premises_id'] ?? 0;
  2141. $express->order_express_goods = $params['order_express_goods'];
  2142. $express->order_express_city = $params['order_express_city'];
  2143. $express->order_express_address = $params['order_express_address'];
  2144. $express->order_express_mobile = $params['order_express_mobile'] ?? '';
  2145. $express->order_express_telephone = $params['order_express_telephone'] ?? '';
  2146. $express->order_express_person = $params['order_express_person'] ?? '';
  2147. $express->order_express_company = $params['dept_premises_id'] ?? '';
  2148. $express->order_express_extend_json = $params['order_express_extend_json'] ?? '[]';
  2149. $express->order_express_addtimes = time();
  2150. $express->save();
  2151. } catch (\Exception $e) {
  2152. dump($e->getMessage());
  2153. throw new BusinessException('物流信息保存失败');
  2154. }
  2155. }
  2156. /**
  2157. * @Desc 修改优惠券状态
  2158. * @Author Gorden
  2159. * @Date 2024/9/19 9:03
  2160. *
  2161. * @param $coupon
  2162. * @param $status
  2163. * @return void
  2164. */
  2165. private function changeOrderCouponStatus($coupon, $status)
  2166. {
  2167. if (!empty($coupon) && is_array($coupon)) {
  2168. $updateData['coupon_detail_status'] = $status;
  2169. if ($status == 'ACTIVED' || $status == 'WAITING') {
  2170. $updateData['coupon_detail_used_datetime'] = '';
  2171. } elseif ($status == 'USED') {
  2172. $updateData['coupon_detail_used_datetime'] = date('Y-m-d H:i:s');
  2173. }
  2174. foreach ($coupon as $item) {
  2175. if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
  2176. foreach ($item['coupon_detail_id'] as $detailId) {
  2177. CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
  2178. ->where('coupon_detail_id', $detailId)->update($updateData);
  2179. }
  2180. }
  2181. }
  2182. }
  2183. }
  2184. private function discountRecord($orderDiscountJson, $params)
  2185. {
  2186. $json = [];
  2187. if (!empty($orderDiscountJson)) {
  2188. $json = json_decode($orderDiscountJson, true);
  2189. }
  2190. try {
  2191. if (!empty($params['preferential']) && in_array('wipe', $params['preferential'])) {
  2192. if (intval($params['order_amount_total'] / 10) * 10 != $params['order_amount_pay']) {
  2193. throw new BusinessException("抹零后实际支付金额错误");
  2194. }
  2195. $params['order_discount_amount'] = $params['order_amount_total'] - $params['order_amount_pay'];
  2196. $couponClassifyDesc = $couponClassify = '抹零';
  2197. } elseif (!empty($params['preferential']) && in_array('custom', $params['preferential'])) {
  2198. if (sprintf("%.2f", $params['order_amount_total'] - $params['order_discount_amount']) != sprintf("%.2f", $params['order_amount_pay'])) {
  2199. throw new BusinessException("餐厅前台优惠后实际支付金额错误");
  2200. }
  2201. $couponClassifyDesc = $couponClassify = '餐厅前台优惠';
  2202. } else if (!empty($params['preferential']) && intval($params['preferential'][0]) < 100 && intval($params['preferential'][0]) >= 50) {
  2203. if (($params['order_amount_total'] * intval($params['preferential'][0])) / 100 != $params['order_amount_pay']) {
  2204. throw new BusinessException("折扣后实际支付金额错误");
  2205. }
  2206. $couponClassify = '折扣';
  2207. $couponClassifyDesc = intval($params['preferential'][0]) / 10 . '折';
  2208. $params['order_discount_amount'] = $params['order_amount_total'] - $params['order_amount_pay'];
  2209. } else {
  2210. return $json;
  2211. }
  2212. $json[date('Y-m-d H:i:s')] = [
  2213. 'coupon_id' => $params['coupon_id'] ?? null,
  2214. 'coupon_value' => $params['order_discount_amount'] ?? '',
  2215. 'coupon_classify' => $couponClassify,
  2216. 'coupon_detail_id' => [$couponClassifyDesc],
  2217. 'coupon_classify_en' => $params['preferential']
  2218. ];
  2219. return $json;
  2220. } catch (BusinessException $e) {
  2221. throw new BusinessException($e->getMessage());
  2222. } catch (\Exception $e) {
  2223. dump($e->getMessage());
  2224. throw new BusinessException("优惠数据错误");
  2225. }
  2226. }
  2227. /**
  2228. * @Desc 入配送记录
  2229. * @Author Gorden
  2230. * @Date 2024/9/23 10:45
  2231. *
  2232. * @param $params
  2233. * @return void
  2234. * @throws BusinessException
  2235. */
  2236. public function insertExpressOne($params, $orderId)
  2237. {
  2238. if (isset($params['delivery']) && $params['delivery'] == 'LOGISTICS') {
  2239. $params['order_express_type'] = '配送';
  2240. // 入配送
  2241. $this->saveExpress($params, $orderId);
  2242. } else if (isset($params['delivery']) && $params['delivery'] == 'PICKUP') {
  2243. $params['order_express_type'] = '自提';
  2244. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  2245. if (!$premises) {
  2246. throw new BusinessException('自提门店不存在');
  2247. }
  2248. $params['order_express_city'] = $premises->dept_city;
  2249. $params['order_express_address'] = $premises->dept_address;
  2250. $params['order_express_telephone'] = $premises->dept_telephone;
  2251. $params['order_express_extend_json'] = json_encode(['pick_code' => random_string(4, 'number')]);
  2252. $this->saveExpress($params, $orderId);
  2253. } else if (isset($params['delivery']) && $params['delivery'] == 'ARRIVAL') {
  2254. $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first();
  2255. $params['order_express_type'] = '到店';
  2256. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  2257. $params['order_express_type'] = '堂食';
  2258. }
  2259. if (!$premises) {
  2260. throw new BusinessException('门店不存在');
  2261. }
  2262. $params['order_express_city'] = $premises->dept_city;
  2263. $params['order_express_address'] = $premises->dept_address;
  2264. $params['order_express_telephone'] = $premises->dept_telephone;
  2265. $this->saveExpress($params, $orderId);
  2266. }
  2267. }
  2268. /**
  2269. * @Desc 释放下单时选的优惠券
  2270. * @Author Gorden
  2271. * @Date 2024/9/19 9:56
  2272. *
  2273. * @param $coupon
  2274. * @return false|string
  2275. */
  2276. private function releaseCoupon($coupon)
  2277. {
  2278. if (!empty($coupon) && is_array($coupon)) {
  2279. $updateData = [
  2280. 'coupon_detail_status' => 'ACTIVED',
  2281. 'coupon_detail_used_datetime' => ''
  2282. ];
  2283. foreach ($coupon as $key => $item) {
  2284. if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
  2285. foreach ($item['coupon_detail_id'] as $detailId) {
  2286. if (substr($detailId, 0, 4) == 'CUDT') {
  2287. CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
  2288. ->where('coupon_detail_id', $detailId)
  2289. ->where('coupon_detail_status', 'WAITING')
  2290. ->update($updateData);
  2291. }
  2292. }
  2293. unset($coupon[$key]);
  2294. }
  2295. }
  2296. }
  2297. return json_encode($coupon);
  2298. }
  2299. }