GoodsController.php 121 KB

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