KangyangCityController.php 58 KB

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