GoodsController.php 124 KB

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