KangyangCityController.php 54 KB

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