GoodsController.php 122 KB

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