PartnerController.php 53 KB

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