KangyangCityController.php 56 KB

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