KangyangCityController.php 57 KB

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