NewCustomerController.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\service\coupon\CouponService;
  4. use app\admin\service\member\MemberService;
  5. use app\admin\service\order\OrderService;
  6. use app\admin\validate\order\OrderValidate;
  7. use app\controller\Curd;
  8. use app\model\Appointment;
  9. use app\model\ClientConfig;
  10. use app\model\Coupon;
  11. use app\model\Goods;
  12. use app\model\GoodsComponent;
  13. use app\model\GoodsRunning;
  14. use app\model\GoodsSku;
  15. use app\model\Member;
  16. use app\model\MemberAccount;
  17. use app\model\MemberBenefit;
  18. use app\model\MemberRole;
  19. use app\model\Order;
  20. use app\model\OrderExpress;
  21. use app\model\OrderReturn;
  22. use app\model\OrderSheet;
  23. use app\model\PayDetail;
  24. use app\model\Supplier;
  25. use app\model\SysDept;
  26. use app\model\SysUser;
  27. use support\Db;
  28. use support\exception\BusinessException;
  29. use support\Redis;
  30. use support\Request;
  31. use support\Response;
  32. use Webman\Event\Event;
  33. use Yansongda\Pay\Pay;
  34. class NewCustomerController extends Curd
  35. {
  36. public function __construct()
  37. {
  38. $this->model = new Order();
  39. $this->validate = true;
  40. $this->validateClass = new OrderValidate();
  41. }
  42. /**
  43. * @Desc 列表
  44. * @Author Gorden
  45. * @Date 2024/3/28 15:01
  46. *
  47. * @param Request $request
  48. * @return Response
  49. * @throws \support\exception\BusinessException
  50. */
  51. public function select(Request $request): Response
  52. {
  53. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  54. $where['order_classify'] = 'COMBINE';
  55. if (!empty($where['order_addtimes'])) {
  56. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  57. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  58. }
  59. $order = $request->get('order', 'desc');
  60. $field = $field ?? 'order_addtimes';
  61. $orderIds = [];
  62. if (!empty($orderId)) {
  63. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  64. ->where('order_classify', 'COMBINE')
  65. ->pluck('order_id')
  66. ->toArray();
  67. }
  68. $goodsName = trim($request->get('goods_name', ''));
  69. if (!empty($goodsName)) {
  70. $goodsIds = Goods::where('goods_classify', 'COMBINE')
  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. $query = $this->doSelect($where, $field, $order);
  85. return $this->doFormat($query, $format, $limit);
  86. }
  87. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  88. {
  89. $model = $this->model->with([
  90. 'sheets' => function ($query) {
  91. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  92. },
  93. 'member' => function ($query) {
  94. $query->select('member_id', 'member_mobile');
  95. },
  96. 'cert' => function ($query) {
  97. $query->select('join_cert_member_id', 'member_cert_name');
  98. },
  99. // 'return' => function ($query) use ($where){
  100. // if (isset($where['return'])){
  101. // dump($where['return']);
  102. // $query = $query->where('order_return_status',$where['return']);
  103. // }
  104. // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
  105. // },
  106. // 'express' => function ($query) {
  107. // $query->select('join_express_order_id', 'order_express_type');
  108. // }
  109. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
  110. ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
  111. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  112. foreach ($where as $column => $value) {
  113. if (is_array($value)) {
  114. if ($value[0] === 'like' || $value[0] === 'not like') {
  115. $model = $model->where($column, $value[0], "%$value[1]%");
  116. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  117. $model = $model->where($column, $value[0], $value[1]);
  118. } elseif ($value[0] == 'in' && !empty($value[1])) {
  119. $valArr = $value[1];
  120. if (is_string($value[1])) {
  121. $valArr = explode(",", trim($value[1]));
  122. }
  123. $model = $model->whereIn($column, $valArr);
  124. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  125. $valArr = $value[1];
  126. if (is_string($value[1])) {
  127. $valArr = explode(",", trim($value[1]));
  128. }
  129. $model = $model->whereNotIn($column, $valArr);
  130. } elseif ($value[0] == 'null') {
  131. $model = $model->whereNull($column);
  132. } elseif ($value[0] == 'not null') {
  133. $model = $model->whereNotNull($column);
  134. } elseif ($value[0] !== '' || $value[1] !== '') {
  135. $model = $model->whereBetween($column, $value);
  136. }
  137. } else {
  138. $model = $model->where($column, $value);
  139. }
  140. }
  141. if ($field) {
  142. $model = $model->orderBy($field, $order);
  143. }
  144. $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_remark');
  145. return $model;
  146. }
  147. public function afterQuery($items)
  148. {
  149. foreach ($items as &$item) {
  150. $sheetDeng = '';
  151. $item['sheet'] = $item['sheets'][0] ?? [];
  152. if (!empty($item['sheet'])) {
  153. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  154. if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
  155. $sheetDeng = ' 等餐品';
  156. }
  157. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  158. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  159. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  160. }
  161. unset($item['sheets']);
  162. if (isset($item['orders_return_id'])) {
  163. $item['return'] = [
  164. 'orders_return_id' => $item['orders_return_id'],
  165. 'join_return_order_id' => $item['join_return_order_id'],
  166. 'order_return_status' => $item['order_return_status'],
  167. 'order_return_apply_json' => $item['order_return_apply_json'],
  168. 'order_return_remark' => $item['order_return_remark']
  169. ];
  170. }
  171. if (isset($item['join_express_order_id'])) {
  172. $item['express'] = [
  173. 'join_express_order_id' => $item['join_express_order_id'],
  174. 'order_express_type' => $item['order_express_type']
  175. ];
  176. unset($item['join_express_order_id'], $item['order_express_type']);
  177. }
  178. // if (!empty($item['order_extend_json'])){
  179. // $orderExtendJson = json_decode($item['order_extend_json'],true);
  180. // $item['referee'] = $orderExtendJson['referee'] ?? '';
  181. // }
  182. }
  183. return $items;
  184. }
  185. /**
  186. * @Desc 下单+支付
  187. * @Author Gorden
  188. * @Date 2024/9/5 13:13
  189. *
  190. * @param Request $request
  191. * @return Response
  192. */
  193. public function insert(Request $request): Response
  194. {
  195. $params = $request->post();
  196. $goodsClassifys = array_unique(array_column($params['goodsContentList'], 'goods_classify'));
  197. $params['goods_classify'] = $goodsClassifys[0];
  198. $params['submit_goods_classify'] = $goodsClassifys[0];
  199. Db::beginTransaction();
  200. try {
  201. // 验证线下付款密码
  202. if ($params['settlement_now'] == 'Y' && $params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  203. $password = $params['offline_password'];
  204. if ($password != '666888') {
  205. throw new BusinessException('密码错误,请重新输入');
  206. }
  207. }
  208. // 下单账户
  209. if (empty($params['join_order_member_id']) && !empty($params['mobile'])) {
  210. if (Member::where('member_mobile', $params['mobile'])->exists()) {
  211. throw new BusinessException('会员已存在');
  212. }
  213. $params['join_order_member_id'] = $params['member_id'] = 'MR' . date('ymdHi') . random_string(4, 'up');
  214. // 创建会员
  215. MemberService::createMember($params);
  216. } else if (empty($params['join_order_member_id']) && empty($params['mobile'])) {
  217. $params['join_order_member_id'] = Member::where('member_mobile', '0000')->value('member_id');
  218. }
  219. if (empty($params['join_order_member_id'])) {
  220. throw new BusinessException('检查下单账户');
  221. }
  222. // 检查库存
  223. OrderService::checkGoodsStorage($params);
  224. $params['orderId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  225. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  226. // 查询是否有未完全支付订单
  227. $paidOrder = Order::where('join_order_member_id', $params['join_order_member_id'])
  228. ->where('order_category', 'COMBINE')
  229. ->where('order_status_system', 'PAYING')
  230. ->first();
  231. if (!empty($paidOrder)) {
  232. $paidOrder->order_groupby = $params['orderGroupId'];
  233. $params['orderId'] = $paidOrder->order_id;
  234. } else {
  235. // 检查产品配置
  236. $this->checkGoodsConfig($params);
  237. }
  238. $systemStatus = 'PAYING';
  239. // 立即结算
  240. if ($params['settlement_now'] == 'Y') {
  241. $params['order_is_complete'] = 'Y';
  242. $systemStatus = 'DONE';
  243. }
  244. if ($params['settlement_now'] == 'Y' && ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY')) {
  245. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  246. $params['pay_category'] = $params['pay_category_sub'];
  247. }
  248. $params['order_status_system'] = $systemStatus;
  249. $params['order_status_payment'] = 'SUCCESS';
  250. }
  251. if ($params['pay_category'] == 'QRCODE' && $params['settlement_now'] == 'Y' && !empty($params['qrcode_nbr'])) { // 付款码
  252. $result = OrderService::qrcodePay($params);
  253. $result = json_encode($result);
  254. $params['pay_json_response'] = $result;
  255. $result = json_decode($result, true);
  256. $prefix = substr($params['qrcode_nbr'], 0, 2);
  257. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  258. $params['pay_category'] = 'WXPAY';
  259. 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')) {
  260. $params['order_status_system'] = 'PAYING';
  261. $params['order_status_payment'] = 'PENDING';
  262. $params['order_is_complete'] = 'N';
  263. } else {
  264. $params['order_status_system'] = $systemStatus;
  265. $params['order_status_payment'] = 'SUCCESS';
  266. }
  267. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  268. $params['pay_category'] = 'ALIPAY';
  269. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  270. $params['order_status_system'] = 'PAYING';
  271. $params['order_status_payment'] = 'PENDING';
  272. $params['order_is_complete'] = 'N';
  273. } else {
  274. $params['order_status_system'] = $systemStatus;
  275. $params['order_status_payment'] = 'SUCCESS';
  276. }
  277. } else {
  278. throw new BusinessException('付款码无效');
  279. }
  280. }
  281. if (empty($paidOrder)) {
  282. // 写入主订单
  283. $this->insertMain($params);
  284. // 订单详情
  285. $this->insertSheet($params);
  286. } else {
  287. $params['orderGroupId'] = $paidOrder->order_groupby;
  288. if ($params['order_status_payment'] == 'SUCCESS') {
  289. $params['order_amount_paid'] = $paidOrder->order_amount_paid;
  290. $paidOrder->order_amount_paid = $paidOrder->order_amount_paid + $params['order_amount_pay'];
  291. $paidOrder->order_amount_pay = $paidOrder->order_amount_paid;
  292. if (floatval($paidOrder->order_amount_paid) >= $paidOrder->order_amount_total) {
  293. $paidOrder->order_is_complete = 'Y';
  294. $paidOrder->order_status_system = 'DONE';
  295. $paidOrder->order_status_payment = 'SUCCESS';
  296. // 更新sheet
  297. OrderSheet::where('join_sheet_order_id', $paidOrder->order_id)->update(['order_sheet_status' => 'DONE']);
  298. }
  299. }
  300. $paidOrder->save();
  301. // 清除此订单的未支付记录
  302. PayDetail::whereJsonContains('join_pay_object_json->order_id', $paidOrder->order_id)->where('pay_status', 'WAITING')->delete();
  303. }
  304. // 支付记录
  305. $this->insertPayDetail($params);
  306. Db::commit();
  307. // 分期付完
  308. $full = '';
  309. if ($params['goods_classify'] == 'COMBINE' && $params['order_status_payment'] == 'SUCCESS' && (!empty($paidOrder) && floatval($paidOrder->order_amount_paid) >= $paidOrder->order_amount_total || floatval($params['order_amount_pay']) >= $params['order_amount_total'])) {
  310. $full = '-满额';
  311. $params['member_id'] = $params['join_order_member_id'];
  312. // 完成订单
  313. Event::dispatch('order.complete', $params);
  314. Event::dispatch('order.new_custom.grant', $params);
  315. }
  316. if ($params['order_status_payment'] == 'SUCCESS') {
  317. // 入收支明细表
  318. $params['inout_category'] = '购买组合包收入' . $full;
  319. Event::dispatch('statistics.inout.in', $params);
  320. }
  321. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
  322. _syslog("订单", "支付异常,检查是否有轮询");
  323. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  324. }
  325. _syslog("订单", "创建订单成功");
  326. return json_success('创建订单成功');
  327. } catch (BusinessException $e) {
  328. Db::rollBack();
  329. dump($e->getMessage());
  330. dump($e->getTrace());
  331. _syslog("订单", $e->getMessage());
  332. return json_fail($e->getMessage());
  333. } catch (\Exception $e) {
  334. Db::rollBack();
  335. dump($e->getMessage());
  336. dump($e->getTrace());
  337. _syslog("订单", "创建订单失败");
  338. return json_fail('创建订单失败');
  339. }
  340. }
  341. /**
  342. * @Desc 支付
  343. * @Author Gorden
  344. * @Date 2024/9/5 13:12
  345. *
  346. * @param Request $request
  347. * @return Response
  348. */
  349. public function pay(Request $request)
  350. {
  351. $params = $request->post();
  352. // 验证线下付款密码
  353. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  354. $password = $params['offline_password'];
  355. if ($password != '666888') {
  356. return json_fail("密码错误,请重新输入");
  357. }
  358. }
  359. $order = Order::where('order_id', $params['order_id'])->first();
  360. if (!$order) {
  361. return json_fail('订单异常');
  362. }
  363. if ($order->order_status_system != 'PAYING') {
  364. return json_fail('订单不是可支付状态');
  365. }
  366. $params['orderId'] = $params['order_id'];
  367. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  368. $order->order_groupby = $params['orderGroupId'];
  369. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
  370. ->select('goods_id', 'goods_name', 'goods_classify')
  371. ->first();
  372. if (!$goods) {
  373. return json_fail('产品数据异常');
  374. }
  375. $goods = $goods->toArray();
  376. $params['goods_classify'] = $goods['goods_classify'] ?? '';
  377. // 立即结算
  378. if ($params['goods_classify'] == 'COMBINE') {
  379. $order->order_is_complete = 'Y';
  380. $systemStatus = 'DONE';
  381. }
  382. $paymentStatus = 'PENDING';
  383. Db::beginTransaction();
  384. try {
  385. // 组合支付时,付款码应收金额
  386. $qrcodePayAmount = 0;
  387. if ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY') {
  388. $order->order_status_system = $systemStatus;
  389. $order->order_status_payment = 'SUCCESS';
  390. $paymentStatus = 'SUCCESS';
  391. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  392. $params['pay_category'] = $params['pay_category_sub'];
  393. }
  394. }
  395. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && !empty($params['qrcode_nbr'])) { // 付款码
  396. // 提交过来的支付分类
  397. $submitPayCategory = $params['pay_category'];
  398. // 账户支付的金额
  399. $accountAmount = $params['order_amount_pay'];
  400. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
  401. // 组合支付,改成需要付款码需要支付的金额
  402. $params['order_amount_pay'] = $qrcodePayAmount;
  403. }
  404. // 去支付
  405. $result = OrderService::qrcodePay($params);
  406. $result = json_encode($result);
  407. $params['pay_json_response'] = $result;
  408. $result = json_decode($result, true);
  409. $prefix = substr($params['qrcode_nbr'], 0, 2);
  410. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  411. $params['pay_category'] = 'WXPAY';
  412. 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')) {
  413. $order->order_status_system = 'PAYING';
  414. $order->order_status_payment = 'PENDING';
  415. $order->order_is_complete = 'N';
  416. // Db::rollBack();
  417. // return json_fail('支付失败');
  418. } else {
  419. $order->order_status_system = $systemStatus;
  420. $order->order_status_payment = 'SUCCESS';
  421. $paymentStatus = 'SUCCESS';
  422. }
  423. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  424. $params['pay_category'] = 'ALIPAY';
  425. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  426. $order->order_status_system = 'PAYING';
  427. $order->order_status_payment = 'PENDING';
  428. $order->order_is_complete = 'N';
  429. // Db::rollBack();
  430. // return json_fail('支付失败');
  431. } else {
  432. $order->order_status_system = $systemStatus;
  433. $order->order_status_payment = 'SUCCESS';
  434. $paymentStatus = 'SUCCESS';
  435. }
  436. } else {
  437. throw new BusinessException('付款码无效');
  438. }
  439. // 账户支付的金额
  440. $params['order_amount_pay'] = $accountAmount;
  441. }
  442. if ($order->order_status_payment == 'SUCCESS') {
  443. $order->order_amount_paid = $order->order_amount_paid + $params['order_amount_pay'];
  444. $order->order_amount_pay = $order->order_amount_paid;
  445. }
  446. // 分期,支付完就结束了
  447. if ($order->order_status_payment == 'SUCCESS' && $params['goods_classify'] == 'COMBINE' && floatval($order->order_amount_paid) >= $order->order_amount_total) {
  448. $order->order_is_complete = 'Y';
  449. }
  450. if (floatval($order->order_amount_paid) < $order->order_amount_total) {
  451. $order->order_status_system = 'PAYING';
  452. $order->order_status_payment = 'PENDING';
  453. $order->order_is_complete = 'N';
  454. }
  455. // 主订单
  456. $order->save();
  457. // sheet
  458. if ($order->order_status_payment == 'SUCCESS') {
  459. $data = [
  460. 'order_sheet_status' => $systemStatus
  461. ];
  462. if (floatval($order->order_amount_paid) < $order->order_amount_total) {
  463. $data['order_sheet_status'] = 'BEING';
  464. }
  465. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update($data);
  466. }
  467. // payDetail
  468. $payData = [
  469. 'pay_amount' => $params['order_amount_pay']
  470. ];
  471. if ($paymentStatus == 'SUCCESS') {
  472. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  473. $payData['pay_status'] = 'SUCCESS';
  474. }
  475. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  476. $payData['pay_prepayid'] = $params['pay_category'];
  477. $payData['pay_json_response'] = $params['pay_json_response'];
  478. } else if ($params['pay_category'] == 'CASH') {
  479. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-CASH';
  480. } else if ($params['pay_category'] == 'WELFARE') {
  481. $payData['pay_prepayid'] = $params['join_order_member_id'] . '-WELFARE';
  482. } else if ($params['pay_category'] == 'CARD') {
  483. $payData['pay_prepayid'] = $params['card_nbr'];
  484. } else if ($params['pay_category'] == 'OFFLINE') {
  485. $payData['pay_prepayid'] = 'OFFLINE';
  486. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  487. $payData['pay_prepayid'] = 'OFFLINE_ALIPAY';
  488. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  489. $payData['pay_prepayid'] = 'OFFLINE_WXPAY';
  490. } else if ($params['pay_category'] == 'MONEY') {
  491. $payData['pay_prepayid'] = 'MONEY';
  492. }
  493. // 清除此订单的未支付记录
  494. PayDetail::whereJsonContains('join_pay_object_json->order_id', $order->order_id)->where('pay_status', 'WAITING')->delete();
  495. $payData['join_pay_member_id'] = $params['join_order_member_id'];
  496. $payData['join_pay_order_id'] = $order->order_groupby;
  497. $payData['pay_status'] = !empty($payData['pay_status']) && $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING';
  498. $payData['pay_category'] = $params['goods_classify'] ?? '';
  499. $payData['pay_paytimes'] = date('Y-m-d H:i:s');
  500. $payData['pay_json_request'] = json_encode($params); // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"}
  501. $payData['pay_json_response'] = !empty($payData['pay_status']) && $payData['pay_status'] == 'SUCCESS' ? json_encode([
  502. 'pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s')
  503. ]) : '[]';
  504. $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
  505. $payData['pay_addtimes'] = time();
  506. PayDetail::insert($payData);
  507. Db::commit();
  508. // 分期付款完成
  509. $full = '';
  510. if ($params['goods_classify'] == 'COMBINE' && $order->order_status_payment == 'SUCCESS' && floatval($order->order_amount_paid) >= $order->order_amount_total) {
  511. $full = '-满额';
  512. $params['member_id'] = $params['join_order_member_id'];
  513. // 完成订单
  514. Event::dispatch('order.complete', $params);
  515. // 处理分期完成后的操作
  516. Event::dispatch('order.new_custom.grant', $params);
  517. }
  518. if ($order->order_status_payment == 'SUCCESS') {
  519. // 入收支明细表
  520. $params['inout_category'] = '购买组合包收入' . $full;
  521. Event::dispatch('statistics.inout.in', $params);
  522. }
  523. if ($order->order_status_payment != 'SUCCESS' && $paymentStatus != 'SUCCESS') {
  524. _syslog("订单", "支付异常,检查是否有轮询");
  525. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  526. }
  527. _syslog("订单", "订单支付成功");
  528. return json_success('支付成功');
  529. } catch (BusinessException $e) {
  530. dump($e->getMessage());
  531. Db::rollBack();
  532. _syslog("订单", "订单支付失败:" . $e->getMessage());
  533. return json_fail("支付失败:" . $e->getMessage());
  534. } catch (\Exception $e) {
  535. dump($e->getMessage());
  536. Db::rollBack();
  537. _syslog("订单", "订单支付失败");
  538. return json_fail('支付失败');
  539. }
  540. }
  541. /**
  542. * @Desc
  543. * @Author Gorden
  544. * @Date 2024/6/7 10:30
  545. *
  546. * @param $params
  547. * @return void
  548. * @throws BusinessException
  549. */
  550. public function insertMain($params)
  551. {
  552. try {
  553. $orderCategory = 'NORMAL';
  554. if (!empty($params['order_category'])) {
  555. $orderCategory = $params['order_category'];
  556. } else if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  557. $orderCategory = 'DISHES';
  558. } else if (isset($params['goods_classify'])) {
  559. $orderCategory = $params['goods_classify'];
  560. }
  561. if (empty($params['order_extend_json'])) {
  562. $params['order_extend_json'] = [];
  563. } else {
  564. if (is_json($params['order_extend_json'])) {
  565. $params['order_extend_json'] = json_decode($params['order_extend_json'], true);
  566. }
  567. }
  568. // 推荐人
  569. $params['order_extend_json']['referee'] = $params['referee'] ?? '';
  570. $data = [
  571. 'order_id' => $params['orderId'],
  572. 'order_groupby' => $params['orderGroupId'],
  573. 'join_order_member_id' => $params['join_order_member_id'],
  574. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  575. 'order_amount_total' => $params['order_amount_total'],
  576. 'order_amount_pay' => $params['order_status_payment'] == 'SUCCESS' ? $params['order_amount_pay'] : 0,
  577. 'order_amount_paid' => $params['order_status_payment'] == 'SUCCESS' ? $params['order_amount_pay'] : 0,
  578. 'order_category' => $orderCategory,
  579. 'order_classify' => $orderCategory,
  580. 'order_is_complete' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) && $params['order_status_payment'] == 'SUCCESS' ? 'Y' : 'N',
  581. 'order_status_system' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  582. 'order_status_payment' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'PENDING',
  583. 'order_status_storage' => $params['order_status_storage'],
  584. 'order_platform' => $params['order_platform'],
  585. 'order_remark' => $params['order_remark'] ?? '',
  586. 'order_discount_json' => $params['order_discount_json'] ?? '[]',
  587. 'order_config_json' => $params['order_config_json'] ?? '[]',
  588. 'order_express_json' => $params['order_express_json'] ?? '[]',
  589. 'order_extend_json' => $params['order_extend_json'] ? json_encode($params['order_extend_json']) : '[]',
  590. 'order_addtimes' => time()
  591. ];
  592. Order::insert($data);
  593. } catch (\Exception $e) {
  594. dump($e->getMessage());
  595. throw new BusinessException('订单创建信息失败');
  596. }
  597. }
  598. /**
  599. * @Desc
  600. * @Author Gorden
  601. * @Date 2024/6/7 10:25
  602. *
  603. * @param $params
  604. * @return void
  605. * @throws BusinessException
  606. */
  607. public function insertSheet($params)
  608. {
  609. try {
  610. $orderSheetIds = [];
  611. foreach ($params['goodsContentList'] as $goods) {
  612. //{"unit": "份", "table": null, "premises": "15"}
  613. $price = floatval($goods['goods_sales_price']);
  614. $extendJson['unit'] = $goods['sku_name'];
  615. if (isset($params['submit_premises_id'])) {
  616. $extendJson['premises'] = $params['submit_premises_id'];
  617. }
  618. $orderSheetStatus = 'PAYING';
  619. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
  620. if (floatval($params['order_amount_pay']) >= floatval($params['order_amount_total'])) {
  621. $orderSheetStatus = 'DONE';
  622. } else {
  623. $orderSheetStatus = 'BEING';
  624. }
  625. }
  626. // 减库存
  627. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  628. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  629. if ($goodsRunning->goods_running_storage < 0) {
  630. throw new BusinessException('库存不足');
  631. }
  632. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  633. $goodsRunning->save();
  634. $data = [
  635. 'join_sheet_member_id' => $params['join_order_member_id'],
  636. 'join_sheet_order_id' => $params['orderId'],
  637. 'join_sheet_goods_id' => $goods['goods_id'],
  638. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  639. 'order_sheet_status' => $orderSheetStatus,
  640. 'order_sheet_category' => 'COMBINE',
  641. 'order_sheet_num' => $goods['nbr'],
  642. 'order_sheet_price' => $goods['goods_sales_price'],
  643. 'order_sheet_amount' => $price * $goods['nbr'],
  644. 'order_sheet_pay' => $price * $goods['nbr'],
  645. 'order_sheet_task_status' => 'NONE',
  646. 'order_sheet_remark' => $params['order_remark'] ?? '',
  647. 'order_sheet_addtimes' => time(),
  648. 'order_sheet_extend_json' => json_encode($extendJson)
  649. ];
  650. $orderSheetId = OrderSheet::insertGetId($data);
  651. $orderSheetIds[] = $orderSheetId;
  652. }
  653. return $orderSheetIds;
  654. } catch (\support\exception\BusinessException $e) {
  655. dump($e->getMessage() . '||' . $e->getLine());
  656. throw new BusinessException($e->getMessage());
  657. } catch (\Exception $e) {
  658. dump($e->getMessage() . '||' . $e->getLine());
  659. throw new BusinessException('订单创建失败');
  660. }
  661. }
  662. /**
  663. * @Desc
  664. * @Author Gorden
  665. * @Date 2024/6/7 10:35
  666. *
  667. * @param $params
  668. * @return void
  669. * @throws BusinessException
  670. */
  671. public function insertPayDetail($params)
  672. {
  673. try {
  674. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  675. $payPrepayid = $params['pay_category'];
  676. } else if ($params['pay_category'] == 'OFFLINE') {
  677. $payPrepayid = 'OFFLINE';
  678. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  679. $payPrepayid = 'OFFLINE_ALIPAY';
  680. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  681. $payPrepayid = 'OFFLINE_WXPAY';
  682. } else if ($params['pay_category'] == 'MONEY') {
  683. $payPrepayid = 'MONEY';
  684. } else {
  685. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  686. }
  687. $data = [
  688. 'join_pay_member_id' => $params['join_order_member_id'],
  689. 'join_pay_order_id' => $params['orderGroupId'],
  690. 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  691. 'pay_category' => $params['goods_classify'],
  692. 'pay_amount' => $params['order_amount_pay'],
  693. 'pay_prepayid' => $payPrepayid,
  694. 'pay_paytimes' => date('Y-m-d H:i:s'),
  695. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
  696. 'pay_json_request' => json_encode($params),
  697. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  698. 'pay_remark' => $params['order_remark'] ?? '',
  699. 'pay_addtimes' => time(),
  700. ];
  701. PayDetail::insert($data);
  702. } catch (\Exception $e) {
  703. dump($e->getMessage());
  704. throw new BusinessException('创建支付记录失败');
  705. }
  706. }
  707. /**
  708. * @Desc 订单商品详情
  709. * @Author Gorden
  710. * @Date 2024/3/29 8:50
  711. *
  712. * @param Request $request
  713. * @return Response
  714. */
  715. public function sheet(Request $request)
  716. {
  717. try {
  718. $orderId = $request->get('order_id');
  719. $orderSheet = OrderSheet::with([
  720. 'member' => function ($query) {
  721. $query->select('member_id', 'member_mobile', 'member_is_owner', 'join_member_role_id', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
  722. },
  723. 'goods' => function ($query) {
  724. $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify', 'goods_if_express', 'goods_attribute_json');
  725. },
  726. 'memberInfo',
  727. 'cert',
  728. 'sku' => function ($query) {
  729. $query->where('goods_sku_status', 'ON')
  730. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price');
  731. },
  732. 'skus',
  733. 'refund' => function ($query) {
  734. $query->select('join_return_order_id', 'order_return_status');
  735. }
  736. ])->where('join_sheet_order_id', $orderId)
  737. ->orderBy('order_sheet_id', 'DESC')
  738. ->get()
  739. ->toArray();
  740. $order = Order::where('order_id', $orderId)->first();
  741. // $express = OrderExpress::where('join_express_order_id', $orderId)->first();
  742. // $premises = '';
  743. // if ($express && $express->order_express_type == '自提') {
  744. // $premises = $express->order_express_company;
  745. // }
  746. $sheetAmount = 0;
  747. foreach ($orderSheet as &$item) {
  748. $sheetAmount += $item['order_sheet_amount'];
  749. $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
  750. if (!empty($item['goods']) && $item['goods']['goods_classify'] == 'PACKAGE') {
  751. $components = GoodsComponent::with('goods')
  752. ->where('join_component_master_goods_id', $item['goods']['goods_id'])
  753. ->select('join_component_master_goods_id', 'join_component_goods_id', 'goods_component_price',
  754. 'goods_component_price', 'goods_component_json')
  755. ->get()
  756. ->toArray();
  757. $goodsArr = [];
  758. foreach ($components as $component) {
  759. $configJson = !empty($component['goods_component_json']) ? json_decode($component['goods_component_json'], true) : [];
  760. if (!empty($component['goods'])) {
  761. $supplierName = Supplier::where('supplier_id', $component['goods']['join_goods_supplier_id'])->value('supplier_name');
  762. $benefit = MemberBenefit::where('join_benefit_member_id', $item['join_sheet_member_id'])
  763. ->where('join_benefit_goods_id', $component['goods']['goods_id'])
  764. ->where('join_benefit_order_id', $orderId)
  765. ->first();
  766. $goodsArr[] = [
  767. 'goods_name' => $component['goods']['goods_name'],
  768. 'goods_cover' => getenv('STORAGE_DOMAIN') . $component['goods']['goods_cover'],
  769. 'supplier_name' => $supplierName,
  770. 'nbr' => $configJson['nbr'] ?? 0,
  771. 'used' => !empty($benefit->member_benefit_used_count) ? intval($benefit->member_benefit_used_count) : ''
  772. ];
  773. }
  774. }
  775. $item['goods']['components'] = $goodsArr;
  776. }
  777. if (!empty($item['sku'])) {
  778. if (!empty($item['sku']['goods_sku_specs_json'])) {
  779. $specsJson = json_decode($item['sku']['goods_sku_specs_json'], true);
  780. $skuName = '';
  781. foreach ($specsJson as $specsKey => $skuSpecs) {
  782. if (is_array($skuSpecs)) {
  783. $skuName = $skuName . ' ' . implode(' ', $skuSpecs) . ';';
  784. } else {
  785. $skuName = $skuName . ' ' . $skuSpecs . ';';
  786. }
  787. }
  788. $item['sku']['goods_sku_title'] = rtrim($skuName, ';');
  789. }
  790. }
  791. if (!empty($item['skus'])) {
  792. foreach ($item['skus'] as $key => $skus) {
  793. if (!empty($skus['goods_sku_specs_json'])) {
  794. $item['skus'][$key]['goods_sku_specs_json'] = json_decode($skus['goods_sku_specs_json']);
  795. $skuName = '';
  796. foreach ($item['skus'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  797. $keyStr = ($specsKey == '规格') ? '' : ($specsKey . ':');
  798. if (is_array($skuSpecs)) {
  799. $skuName = $skuName . $keyStr . ' ' . implode(' ', $skuSpecs) . '; ';
  800. } else {
  801. $skuName = $skuName . $keyStr . ' ' . $skuSpecs . '; ';
  802. }
  803. }
  804. $item['skus'][$key]['sku_name'] = rtrim($skuName, '; ');
  805. }
  806. }
  807. } else {
  808. $item['skus'] = [];
  809. }
  810. $coupons = [];
  811. // 产品携带的优惠券
  812. if (!empty($item['goods']['goods_attribute_json'])) {
  813. $goodsAttributeJson = json_decode($item['goods']['goods_attribute_json'], true);
  814. if (isset($goodsAttributeJson['coupon'])) {
  815. $couponIds = array_keys($goodsAttributeJson['coupon']);
  816. $coupons = Coupon::whereIn('coupon_id', $couponIds)
  817. ->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_is_period', 'coupon_period_json', 'coupon_addtimes')
  818. ->get()
  819. ->toArray();
  820. foreach ($coupons as &$coupon) {
  821. if (isset($goodsAttributeJson['coupon'][$coupon['coupon_id']])) {
  822. $coupon['num'] = $goodsAttributeJson['coupon'][$coupon['coupon_id']]['num'];
  823. }
  824. }
  825. }
  826. }
  827. $item['member']['level'] = '普通会员';
  828. if (!empty($item['member']['join_member_role_id'])) {
  829. $item['member']['level'] = MemberRole::where('member_role_id', $item['member']['join_member_role_id'])->value('member_role_name');
  830. }
  831. if (!empty($item['member_info'])) {
  832. if (empty($item['member_info']['member_info_headimg']) || substr($item['member_info']['member_info_headimg'], 0, 1) == '.') {
  833. $item['member_info']['member_info_headimg'] = "https://img.wanyuewellness.com.cn/images/avatar_default.png";
  834. }
  835. }
  836. }
  837. $order->sheet_amount = number_format($sheetAmount, 2);
  838. $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $order->order_id)
  839. ->where('pay_category', 'COMBINE')
  840. ->where('pay_status', 'SUCCESS')
  841. ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json')
  842. ->orderBy('pay_addtimes', 'DESC')
  843. ->get();
  844. $order->pay_amount_total = 0;
  845. foreach ($payDetails as &$payDetail) {
  846. $categoryArray = explode('-', $payDetail->pay_prepayid);
  847. if (isset($categoryArray[1])) {
  848. $payDetail->pay_category = $categoryArray[1];
  849. } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) {
  850. $payDetail->pay_category = $categoryArray[0];
  851. }
  852. if (!empty($payDetail->pay_extend_json)) {
  853. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  854. $order->cancel_times = $payExtendJson['cancel_times'] ?? '';
  855. if (isset($payExtendJson['ticket'])) {
  856. $payDetail->ticket = str_replace('/thumb', '', getenv("STORAGE_DOMAIN") . $payExtendJson['ticket']);
  857. }
  858. }
  859. $order->pay_amount_total += $payDetail->pay_amount;
  860. }
  861. $refund = OrderReturn::where('join_return_order_id', $orderId)
  862. ->select('order_return_status', 'order_return_apply_datetime', 'order_return_apply_json', 'order_return_accept_datetime', 'order_return_refund_json', 'order_return_extend_json')
  863. ->first();
  864. if (!empty($refund->order_return_refund_json)) {
  865. $returnRefundJson = json_decode($refund->order_return_refund_json, true);
  866. if (isset($returnRefundJson['user_id'])) {
  867. $userName = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name');
  868. $returnRefundJson['person'] = $userName;
  869. unset($returnRefundJson['user_id']);
  870. }
  871. $refund->order_return_refund_json = json_encode($returnRefundJson);
  872. }
  873. if (!empty($order->order_config_json)) {
  874. $orderConfigJson = json_decode($order->order_config_json, true);
  875. if (isset($orderConfigJson['reach'])) {
  876. $order->reach = $orderConfigJson['reach'];
  877. }
  878. if (isset($orderConfigJson['table'])) {
  879. $order->table = $orderConfigJson['table'];
  880. }
  881. if (isset($orderConfigJson['eat'])) {
  882. $order->eat = $orderConfigJson['eat'];
  883. }
  884. if (isset($orderConfigJson['express'])) {
  885. $order->express = $orderConfigJson['express'];
  886. }
  887. if (isset($orderConfigJson['premises'])) {
  888. $order->premises = $orderConfigJson['premises'];
  889. }
  890. if (isset($orderConfigJson['tableid'])) {
  891. $order->dept_table_id = $orderConfigJson['tableid'];
  892. }
  893. }
  894. if (!empty($order->order_extend_json)) {
  895. $orderExtendJson = json_decode($order->order_extend_json, true);
  896. $order->referee = $orderExtendJson['referee'] ?? '';
  897. if (isset($orderExtendJson['cancel_times'])) {
  898. $order->cancel_times = $orderExtendJson['cancel_times'];
  899. }
  900. if (isset($orderExtendJson['free_remark'])) {
  901. $order->free_remark = $orderExtendJson['free_remark'];
  902. }
  903. }
  904. $discount = ['coupon_name' => '', 'classify' => '', 'value' => 0];
  905. if (!empty($order->order_discount_json)) {
  906. $orderDiscountJson = json_decode($order->order_discount_json, true);
  907. foreach ($orderDiscountJson as $discountItem) {
  908. if (!empty($discountItem['coupon_id'])) {
  909. $coupon = Coupon::where('coupon_id', $discountItem['coupon_id'])
  910. ->select('coupon_name', 'coupon_classify', 'coupon_category', 'coupon_value', 'coupon_minimum_limit')
  911. ->first();
  912. if (!$coupon) {
  913. continue;
  914. }
  915. $classify = CouponService::couponClassifyInfo($coupon->coupon_classify, $coupon->coupon_category, $coupon->coupon_value, $coupon->coupon_minimum_limit);
  916. $discount['coupon_name'] .= $coupon->coupon_classify . ':' . $coupon->coupon_name . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  917. }
  918. if ($discountItem['coupon_classify'] == '退款') {
  919. continue;
  920. }
  921. if (empty($discountItem['coupon_id']) && !empty($discountItem['coupon_classify'])) {
  922. if (!empty($discountItem['coupon_detail_id'])) {
  923. $discount['classify'] .= $discountItem['coupon_detail_id'][0] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  924. } else {
  925. $discount['classify'] .= $discountItem['coupon_classify'] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  926. }
  927. }
  928. if (!empty($discountItem['coupon_value'])) {
  929. $discount['value'] += $discountItem['coupon_value'];
  930. }
  931. }
  932. if (!empty($discount['coupon_name'])) {
  933. $discount['coupon_name'] = rtrim($discount['coupon_name'], ', ');
  934. }
  935. if (!empty($discount['classify'])) {
  936. $discount['classify'] = rtrim($discount['classify'], ', ');
  937. }
  938. }
  939. $discount['value'] = sprintf("%.2f", $discount['value']);
  940. $order->discount = $discount;
  941. // $order->premises = $order->premises ?? $premises;
  942. $data = [
  943. 'order' => $order,
  944. 'refund' => json_decode(json_encode($refund)),
  945. 'sheet' => json_decode(json_encode($orderSheet)),
  946. 'coupons' => $coupons,
  947. // 'express' => json_decode(json_encode($express)),
  948. 'payDetails' => json_decode(json_encode($payDetails))
  949. ];
  950. return json_success('', $data);
  951. } catch (\Exception $e) {
  952. dump($e->getMessage());
  953. }
  954. }
  955. /**
  956. * @Desc 获取分期的订单
  957. * @Author Gorden
  958. * @Date 2024/9/10 15:14
  959. *
  960. * @param Request $request
  961. * @return Response
  962. */
  963. public function getPaidOrder(Request $request)
  964. {
  965. $memberId = $request->get('member_id');
  966. if (!$memberId) {
  967. return json_fail('参数异常');
  968. }
  969. $order = Order::where('join_order_member_id', $memberId)
  970. ->where('order_category', 'COMBINE')
  971. ->where('order_status_system', 'PAYING')
  972. ->select('order_id', 'order_amount_total', 'order_amount_paid', 'order_amount_pay')
  973. ->first();
  974. return json_success('', $order);
  975. }
  976. /**
  977. * @Desc 上传支付凭证
  978. * @Author Gorden
  979. * @Date 2024/9/12 10:14
  980. *
  981. * @param Request $request
  982. * @return Response
  983. */
  984. public function uploadTicket(Request $request)
  985. {
  986. $payId = $request->post('pay_id');
  987. $url = $request->post('url');
  988. if (!$payId || !$url) {
  989. return json_fail("参数异常");
  990. }
  991. $url = str_replace(getenv("STORAGE_DOMAIN"), '', $url);
  992. try {
  993. $payDetail = PayDetail::where('pay_id', $payId)->first();
  994. if (!$payDetail) {
  995. return json_fail('数据异常');
  996. }
  997. $extendJson = [];
  998. if (!empty($payDetail->pay_extend_json)) {
  999. $extendJson = json_decode($payDetail->pay_extend_json, true);
  1000. }
  1001. $extendJson['ticket'] = $url;
  1002. $payDetail->pay_extend_json = json_encode($extendJson);
  1003. dump($payDetail->save());
  1004. return json_success('success');
  1005. } catch (\Exception $e) {
  1006. return json_fail("上传附件失败");
  1007. }
  1008. }
  1009. /**
  1010. * @Desc 查询订单状态
  1011. * @Author Gorden
  1012. * @Date 2024/8/19 11:55
  1013. *
  1014. * @param Request $request
  1015. * @return Response|void
  1016. */
  1017. public function getOrderPayStatus(Request $request)
  1018. {
  1019. $groupId = $request->get('group_id');
  1020. if (!$groupId) {
  1021. return json_fail('参数异常');
  1022. }
  1023. $order = Order::where('order_groupby', $groupId)->first();
  1024. if (empty($order)) {
  1025. return json_fail('订单不存在');
  1026. }
  1027. $order = $order->toArray();
  1028. $sheet = OrderSheet::where('join_sheet_order_id', $order['order_id'])
  1029. ->select('order_sheet_id', 'join_sheet_goods_id', 'join_sheet_goods_sku_id')
  1030. ->first();
  1031. $payDetailType = PayDetail::where('join_pay_order_id', $groupId)->pluck('pay_prepayid')->toArray();
  1032. try {
  1033. Db::beginTransaction();
  1034. $payStatus = 'N';
  1035. $payAmount = 0;
  1036. if (in_array('WXPAY', $payDetailType)) {
  1037. $result = Pay::wechat(config('payment.wxpay'))->find($groupId, 'pos');
  1038. $result = json_decode(json_encode($result), true);
  1039. // $result = '{"return_code":"SUCCESS","return_msg":"OK","result_code":"SUCCESS","mch_id":"1680393367","appid":"wxc6274da7198e3eb4","openid":"o3JAn6Ii_bAlxS-jbNEC4WnPhdwM","is_subscribe":"N","trade_type":"MICROPAY","trade_state":"SUCCESS","bank_type":"OTHERS","total_fee":"2","fee_type":"CNY","cash_fee":"1000","cash_fee_type":"CNY","transaction_id":"4200067718202409250802875650","out_trade_no":"OD24092518408RV7","attach":[],"time_end":"20240925184009","trade_state_desc":"支付成功","nonce_str":"OeGOkjch4eaV5qIt","sign":"6DCB3BFC594EBC018A2BEE2C3DFEA4E3"}';
  1040. // $result = json_decode($result, true);
  1041. if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
  1042. $payStatus = 'Y';
  1043. $payAmount = ($result['total_fee'] / 100);
  1044. $orderUpdateData['order_status_payment'] = 'PENDING';
  1045. $orderUpdateData['order_amount_paid'] = $order['order_amount_paid'] + $payAmount;
  1046. $orderUpdateData['order_amount_pay'] = $order['order_amount_pay'] + $payAmount;
  1047. if ($order['order_amount_paid'] + $payAmount >= $order['order_amount_total']) {
  1048. $orderUpdateData['order_status_payment'] = 'SUCCESS';
  1049. $orderUpdateData['order_status_system'] = 'DONE';
  1050. $orderUpdateData['order_is_complete'] = 'Y';
  1051. }
  1052. // 主订单
  1053. Order::where('order_groupby', $groupId)->update($orderUpdateData);
  1054. // Sheet
  1055. OrderSheet::where('join_sheet_order_id', $order['order_id'])->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
  1056. // 支付记录
  1057. PayDetail::where('join_pay_order_id', $order['order_groupby'])->where('pay_prepayid', 'WXPAY')->update([
  1058. 'pay_status' => 'SUCCESS',
  1059. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1060. 'pay_json_response' => json_encode($result)
  1061. ]);
  1062. }
  1063. } else if (in_array('ALIPAY', $payDetailType)) {
  1064. $result = Pay::alipay(config('payment.alipay'))->find($groupId);
  1065. $result = json_decode(json_encode($result), true);
  1066. // $result = '{"code":"10000","msg":"Success","buyer_logon_id":"138******93","buyer_pay_amount":"5.87","fund_bill_list":[{"amount":"5.69","fund_channel":"ALIPAYACCOUNT"},{"amount":"0.18","fund_channel":"COUPON"},{"amount":"0.13","fund_channel":"DISCOUNT"}],"invoice_amount":"5.69","out_trade_no":"OD2409251550Q07A","point_amount":"0.00","receipt_amount":"0.01","send_pay_date":"2024-09-25 15:50:22","total_amount":"0.01","trade_no":"2024092523001439431419750998","trade_status":"TRADE_SUCCESS","buyer_open_id":"04309N2aVhSZz4cKwN_DN2DQa7ekM3z5n8kscQHsmIZOJsf"}';
  1067. // $result = json_decode($result, true);
  1068. if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
  1069. $payStatus = 'Y';
  1070. $payAmount = $result['total_amount'];
  1071. $orderUpdateData['order_status_payment'] = 'PENDING';
  1072. $orderUpdateData['order_amount_paid'] = $order['order_amount_paid'] + $payAmount;
  1073. $orderUpdateData['order_amount_pay'] = $order['order_amount_pay'] + $payAmount;
  1074. if ($order['order_amount_paid'] + $payAmount >= $order['order_amount_total']) {
  1075. $orderUpdateData['order_status_payment'] = 'SUCCESS';
  1076. $orderUpdateData['order_status_system'] = 'DONE';
  1077. $orderUpdateData['order_is_complete'] = 'Y';
  1078. }
  1079. // 主订单
  1080. Order::where('order_groupby', $groupId)->update($orderUpdateData);
  1081. // Sheet
  1082. OrderSheet::where('join_sheet_order_id', $order['order_id'])->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
  1083. // 支付记录
  1084. PayDetail::where('join_pay_order_id', $order['order_groupby'])->where('pay_prepayid', 'ALIPAY')->update([
  1085. 'pay_status' => 'SUCCESS',
  1086. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1087. 'pay_json_response' => json_encode($result)
  1088. ]);
  1089. }
  1090. }
  1091. $full = '';
  1092. if ($payStatus == 'Y') {
  1093. Db::commit();
  1094. if ($order['order_amount_paid'] + $payAmount >= $order['order_amount_total']) {
  1095. $full = '-满额';
  1096. $params['member_id'] = $order['join_order_member_id'];
  1097. $params['join_sheet_goods_id'] = $sheet->join_sheet_goods_id;
  1098. Event::dispatch('order.new_custom.grant', $params);
  1099. }
  1100. // 入收支明细表
  1101. $params['orderId'] = $order['order_id'];
  1102. $params['inout_category'] = '购买组合包收入' . $full;
  1103. Event::dispatch('statistics.inout.in', $params);
  1104. return json_success('success');
  1105. }
  1106. Db::rollBack();
  1107. return json_fail('没有查询到记录');
  1108. } catch (BusinessException $e) {
  1109. Db::rollBack();
  1110. _syslog("支付轮询", '订单支付异常:' . $e->getMessage());
  1111. return json_fail($e->getMessage());
  1112. } catch (\Exception $e) {
  1113. dump($e->getMessage());
  1114. Db::rollBack();
  1115. return json_fail('查询失败');
  1116. }
  1117. }
  1118. /**
  1119. * @Desc 检查产品配置
  1120. * @Author Gorden
  1121. * @Date 2024/9/28 15:42
  1122. *
  1123. * @param $params
  1124. * @return void
  1125. * @throws BusinessException
  1126. */
  1127. public function checkGoodsConfig($params)
  1128. {
  1129. foreach ($params['goodsContentList'] as $goods) {
  1130. $goods = Goods::where('goods_id', $goods['goods_id'])->select('goods_attribute_json')->first();
  1131. if (!empty($goods)) {
  1132. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  1133. // 不允许分期
  1134. if (isset($goodsAttributeJson['control']) && $goodsAttributeJson['control']['if_installment'] == 'N') {
  1135. if ($params['order_amount_total'] > $params['order_amount_pay']) {
  1136. throw new BusinessException('不支持分期支付');
  1137. }
  1138. }
  1139. // 不允许重复购买
  1140. if (isset($goodsAttributeJson['control']) && $goodsAttributeJson['control']['if_repeat_buy'] == 'N') {
  1141. if (Order::where('join_order_member_id', $params['join_order_member_id'])->where('order_classify', 'COMBINE')->exists()) {
  1142. throw new BusinessException('仅可购买一次');
  1143. }
  1144. }
  1145. }
  1146. }
  1147. }
  1148. }