KangyangCityController.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 = '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('ymdHi') . random_string(4, '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. if (!empty($item['member_info'])){
  913. if (empty($item['member_info']['member_info_headimg']) || substr($item['member_info']['member_info_headimg'],0,1) == '.'){
  914. $item['member_info']['member_info_headimg'] = "https://img.wanyuewellness.com.cn/images/avatar_default.png";
  915. }
  916. }
  917. }
  918. $order->sheet_amount = number_format($sheetAmount, 2);
  919. $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $order->order_id)
  920. ->where('pay_category', 'VIP')
  921. ->where('pay_status', 'SUCCESS')
  922. ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json')
  923. ->orderBy('pay_addtimes', 'DESC')
  924. ->get();
  925. $order->pay_amount_total = 0;
  926. foreach ($payDetails as &$payDetail) {
  927. $categoryArray = explode('-', $payDetail->pay_prepayid);
  928. if (isset($categoryArray[1])) {
  929. $payDetail->pay_category = $categoryArray[1];
  930. } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) {
  931. $payDetail->pay_category = $categoryArray[0];
  932. }
  933. if (!empty($payDetail->pay_extend_json)) {
  934. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  935. $order->cancel_times = $payExtendJson['cancel_times'] ?? '';
  936. if (isset($payExtendJson['ticket'])) {
  937. $payDetail->ticket = str_replace('/thumb', '', getenv("STORAGE_DOMAIN") . $payExtendJson['ticket']);
  938. }
  939. }
  940. $order->pay_amount_total += $payDetail->pay_amount;
  941. }
  942. $refund = OrderReturn::where('join_return_order_id', $orderId)
  943. ->select('order_return_status', 'order_return_apply_datetime', 'order_return_apply_json', 'order_return_accept_datetime', 'order_return_refund_json', 'order_return_extend_json')
  944. ->first();
  945. if (!empty($refund->order_return_refund_json)) {
  946. $returnRefundJson = json_decode($refund->order_return_refund_json, true);
  947. if (isset($returnRefundJson['user_id'])) {
  948. $userName = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name');
  949. $returnRefundJson['person'] = $userName;
  950. unset($returnRefundJson['user_id']);
  951. }
  952. $refund->order_return_refund_json = json_encode($returnRefundJson);
  953. }
  954. if (!empty($order->order_config_json)) {
  955. $orderConfigJson = json_decode($order->order_config_json, true);
  956. if (isset($orderConfigJson['reach'])) {
  957. $order->reach = $orderConfigJson['reach'];
  958. }
  959. if (isset($orderConfigJson['table'])) {
  960. $order->table = $orderConfigJson['table'];
  961. }
  962. if (isset($orderConfigJson['eat'])) {
  963. $order->eat = $orderConfigJson['eat'];
  964. }
  965. if (isset($orderConfigJson['express'])) {
  966. $order->express = $orderConfigJson['express'];
  967. }
  968. if (isset($orderConfigJson['premises'])) {
  969. $order->premises = $orderConfigJson['premises'];
  970. }
  971. if (isset($orderConfigJson['tableid'])) {
  972. $order->dept_table_id = $orderConfigJson['tableid'];
  973. }
  974. }
  975. if (!empty($order->order_extend_json)) {
  976. $orderExtendJson = json_decode($order->order_extend_json, true);
  977. $order->referee = $orderExtendJson['referee'] ?? '';
  978. if (isset($orderExtendJson['cancel_times'])) {
  979. $order->cancel_times = $orderExtendJson['cancel_times'];
  980. }
  981. if (isset($orderExtendJson['free_remark'])) {
  982. $order->free_remark = $orderExtendJson['free_remark'];
  983. }
  984. }
  985. $discount = ['coupon_name' => '', 'classify' => '', 'value' => 0];
  986. if (!empty($order->order_discount_json)) {
  987. $orderDiscountJson = json_decode($order->order_discount_json, true);
  988. foreach ($orderDiscountJson as $discountItem) {
  989. if (!empty($discountItem['coupon_id'])) {
  990. $coupon = Coupon::where('coupon_id', $discountItem['coupon_id'])
  991. ->select('coupon_name', 'coupon_classify', 'coupon_category', 'coupon_value', 'coupon_minimum_limit')
  992. ->first();
  993. if (!$coupon) {
  994. continue;
  995. }
  996. $classify = CouponService::couponClassifyInfo($coupon->coupon_classify, $coupon->coupon_category, $coupon->coupon_value, $coupon->coupon_minimum_limit);
  997. $discount['coupon_name'] .= $coupon->coupon_classify . ':' . $coupon->coupon_name . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  998. }
  999. if ($discountItem['coupon_classify'] == '退款') {
  1000. continue;
  1001. }
  1002. if (empty($discountItem['coupon_id']) && !empty($discountItem['coupon_classify'])) {
  1003. if (!empty($discountItem['coupon_detail_id'])) {
  1004. $discount['classify'] .= $discountItem['coupon_detail_id'][0] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  1005. } else {
  1006. $discount['classify'] .= $discountItem['coupon_classify'] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), ';
  1007. }
  1008. }
  1009. if (!empty($discountItem['coupon_value'])) {
  1010. $discount['value'] += $discountItem['coupon_value'];
  1011. }
  1012. }
  1013. if (!empty($discount['coupon_name'])) {
  1014. $discount['coupon_name'] = rtrim($discount['coupon_name'], ', ');
  1015. }
  1016. if (!empty($discount['classify'])) {
  1017. $discount['classify'] = rtrim($discount['classify'], ', ');
  1018. }
  1019. }
  1020. $discount['value'] = sprintf("%.2f", $discount['value']);
  1021. $order->discount = $discount;
  1022. $order->premises = $order->premises ?? $premises;
  1023. $data = [
  1024. 'order' => $order,
  1025. 'refund' => json_decode(json_encode($refund)),
  1026. 'sheet' => json_decode(json_encode($orderSheet)),
  1027. 'express' => json_decode(json_encode($express)),
  1028. 'payDetails' => json_decode(json_encode($payDetails))
  1029. ];
  1030. return json_success('', $data);
  1031. } catch (\Exception $e) {
  1032. dump($e->getMessage());
  1033. }
  1034. }
  1035. /**
  1036. * @Desc 获取分期的订单
  1037. * @Author Gorden
  1038. * @Date 2024/9/10 15:14
  1039. *
  1040. * @param Request $request
  1041. * @return Response
  1042. */
  1043. public function getPaidOrder(Request $request)
  1044. {
  1045. $memberId = $request->get('member_id');
  1046. if (!$memberId) {
  1047. return json_fail('参数异常');
  1048. }
  1049. $order = Order::where('join_order_member_id', $memberId)
  1050. ->where('order_category', 'VIP')
  1051. ->where('order_status_system', 'PAYING')
  1052. ->select('order_id', 'order_amount_total', 'order_amount_paid', 'order_amount_pay')
  1053. ->first();
  1054. return json_success('', $order);
  1055. }
  1056. /**
  1057. * @Desc 上传支付凭证
  1058. * @Author Gorden
  1059. * @Date 2024/9/12 10:14
  1060. *
  1061. * @param Request $request
  1062. * @return Response
  1063. */
  1064. public function uploadTicket(Request $request)
  1065. {
  1066. $payId = $request->post('pay_id');
  1067. $url = $request->post('url');
  1068. if (!$payId || !$url) {
  1069. return json_fail("参数异常");
  1070. }
  1071. $url = str_replace(getenv("STORAGE_DOMAIN"), '', $url);
  1072. try {
  1073. $payDetail = PayDetail::where('pay_id', $payId)->first();
  1074. if (!$payDetail) {
  1075. return json_fail('数据异常');
  1076. }
  1077. $extendJson = [];
  1078. if (!empty($payDetail->pay_extend_json)) {
  1079. $extendJson = json_decode($payDetail->pay_extend_json, true);
  1080. }
  1081. $extendJson['ticket'] = $url;
  1082. $payDetail->pay_extend_json = json_encode($extendJson);
  1083. dump($payDetail->save());
  1084. return json_success('success');
  1085. } catch (\Exception $e) {
  1086. return json_fail("上传附件失败");
  1087. }
  1088. }
  1089. }