AppointmentService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. namespace app\admin\service\order;
  3. use app\admin\controller\member\BenefitController;
  4. use app\admin\controller\member\QuotaController;
  5. use app\model\Appointment;
  6. use app\model\Goods;
  7. use app\model\GoodsSku;
  8. use app\model\Member;
  9. use app\model\MemberAccount;
  10. use app\model\MemberBenefit;
  11. use app\model\MemberCert;
  12. use app\model\MemberInfo;
  13. use app\model\MemberQuota;
  14. use app\model\Order;
  15. use app\model\OrderSheet;
  16. use app\model\PayDetail;
  17. use app\model\SysDept;
  18. use app\model\SysSerial;
  19. use support\Db;
  20. use support\exception\BusinessException;
  21. use support\Request;
  22. use function _PHPStan_cc8d35ffb\RingCentral\Psr7\str;
  23. class AppointmentService
  24. {
  25. public static function select(Request $request)
  26. {
  27. $page = $request->get('page');
  28. $pageSize = $request->get('pageSize');
  29. $id = $request->get('id', '');
  30. $mobile = $request->get('mobile', '');
  31. $status = $request->get('status', '');
  32. $rows = Db::table('appointment')
  33. ->leftJoin('member', 'member.member_id', '=', 'appointment.join_appointment_member_id')
  34. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'appointment.join_appointment_member_id')
  35. ->leftJoin('member_info', 'member_info.join_info_member_id', '=', 'appointment.join_appointment_member_id')
  36. ->leftJoin('goods', 'goods.goods_id', '=', 'appointment.join_appointment_goods_id')
  37. ->leftJoin('order', 'order.order_id', '=', 'appointment.join_appointment_order_id')
  38. ->leftJoin('member_benefit', 'member_benefit.member_benefit_id', '=', 'appointment.join_appointment_member_benefit_id')
  39. ->leftJoin('goods_sku', 'goods_sku.goods_sku_id', '=', 'appointment.join_appointment_goods_sku_id')
  40. ->whereIn('appointment_status',['PAYING','WAITING','PENDING','DOING','DONE','CANCEL','REVOKE'])
  41. ->when($id != '', function ($query) use ($id) {
  42. $query->where('appointment.appointment_id', 'like', '%' . $id . '%');
  43. })->when($mobile != '', function ($query) use ($mobile) {
  44. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  45. })->when($status != '', function ($query) use ($status) {
  46. $query->where('appointment.appointment_status', $status);
  47. });
  48. $total = $rows->count('member.member_id');
  49. $rows = $rows->select('appointment.*',
  50. 'member.member_mobile',
  51. 'member_cert.member_cert_name',
  52. 'member_info.member_info_nickname',
  53. 'goods.goods_name', 'goods.goods_cover', 'goods.goods_sales_price',
  54. 'order.order_name', 'goods_sku.goods_sku_specs_json', 'order.order_amount_total', 'order.order_amount_pay',
  55. 'order.order_category', 'order.order_status_system', 'order.order_status_payment', 'order.order_status_storage',
  56. 'order.order_addtimes',
  57. 'member_benefit.member_benefit_name','member_benefit.member_benefit_id as benefit_id'
  58. )
  59. ->orderBy('appointment.appointment_addtimes', 'desc')
  60. ->forPage($page, $pageSize)
  61. ->get();
  62. foreach ($rows as &$row) {
  63. $row->goods_cover = getenv('STORAGE_DOMAIN') . $row->goods_cover;
  64. $row->goods_sku_specs_json = !empty($row->goods_sku_specs_json) ? json_decode($row->goods_sku_specs_json, true) : [];
  65. $row->order_addtimes = date('Y-m-d H:i:s', $row->order_addtimes);
  66. $row->appointment_addtimes = date('Y-m-d H:i:s', $row->appointment_addtimes);
  67. $row->appointment_apply_json = !empty($row->appointment_apply_json) ? json_decode($row->appointment_apply_json,true) : [];
  68. if ($row->appointment_category == 'NOFEE') {
  69. $row->member_benefit_name = "后付费预约";
  70. }
  71. if (!empty($row->appointment_apply_json)) {
  72. $applyJson = $row->appointment_apply_json;
  73. $row->person = $applyJson['person'] ?? '';
  74. $row->appointment_times = $applyJson['times'] ?? '';
  75. }
  76. $skus = GoodsSku::where('join_sku_goods_id',$row->join_appointment_goods_id)
  77. ->select('goods_sku_id','goods_sku_specs_json','goods_sku_sales_price')
  78. ->get();
  79. $skuJson = [];
  80. foreach ($skus as $key => $sku){
  81. if (!empty($sku->goods_sku_specs_json)) {
  82. $specsJson = json_decode($sku->goods_sku_specs_json, true);
  83. $skuTitle = '';
  84. foreach ($specsJson as $item3) {
  85. if (is_array($item3)) {
  86. $item3 = implode(',', $item3);
  87. }
  88. $skuTitle .= $item3 . '-';
  89. }
  90. $skuJson[$key] = [
  91. 'goods_sku_id' => $sku->goods_sku_id,
  92. 'goods_sku_sales_price' => $sku->goods_sku_sales_price,
  93. 'sku_name' => rtrim($skuTitle, '-')
  94. ];
  95. }
  96. }
  97. $row->sku = $skuJson;
  98. }
  99. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  100. }
  101. public static function confirm($id)
  102. {
  103. $appointment = Appointment::where('appointment_id', $id)->where('appointment_status', 'WAITING')->first();
  104. if (!$appointment) {
  105. return json_fail('数据异常');
  106. }
  107. try {
  108. $appointment->appointment_status = 'PENDING';
  109. $appointment->save();
  110. return json_success('操作成功');
  111. } catch (\Exception $e) {
  112. dump($e->getMessage());
  113. return json_fail("操作失败");
  114. }
  115. }
  116. public static function add(Request $request)
  117. {
  118. $params = $request->post();
  119. Db::beginTransaction();
  120. try {
  121. // 先检查选择的时段是否可预约
  122. $goods = Goods::where('goods_id', $params['join_appointment_goods_id'])->first();
  123. $appointments = Appointment::where('appointment_datetime', $params['appointment_datetime'])->get()->toArray();
  124. if ($goods->is_support_appointment == 'Y' && !empty($goods->goods_attribute_json)) {
  125. $extendJson = json_decode($goods->goods_attribute_json, true);
  126. foreach ($extendJson['times'] as $key => $time) {
  127. if ($time['duration'] == $params['appointment_times']) {
  128. if (!empty($appointments)) {
  129. foreach ($appointments as $appointment) {
  130. $applyJson = json_decode($appointment['appointment_apply_json'], true);
  131. $applyJsonTimes = $applyJson['times'] ?? '';
  132. if ($time['duration'] == $applyJsonTimes) {
  133. $extendJson['times'][$key]['person'] -= $applyJson['person'];
  134. if ($extendJson['times'][$key]['person'] < $params['person']) {
  135. return json_fail('当前时段名额不足,请选择其他时段');
  136. }
  137. }
  138. }
  139. } else {
  140. if ($extendJson['times'][$key]['person'] < $params['person']) {
  141. return json_fail('当前时段名额不足,请选择其他时段');
  142. }
  143. }
  144. }
  145. }
  146. }
  147. // 如果没有,新建
  148. if (empty($params['join_appointment_member_id']) && !empty($params['username']) && !empty($params['mobile'])) {
  149. $memberId = "ME" . str_pad(SysSerial::getSerial(), 16, "0", STR_PAD_LEFT) . random_string(6, 'up');
  150. $params['join_appointment_member_id'] = $memberId;
  151. // 主表
  152. $memberData = [
  153. 'member_id' => $memberId,
  154. 'member_is_owner' => 'N',
  155. 'member_classify' => 'MEMBER',
  156. 'member_status' => 'ACTIVED',
  157. 'member_mobile' => $params['mobile'],
  158. 'member_from' => 'backstage',
  159. 'member_addtimes' => time()
  160. ];
  161. Member::insert($memberData);
  162. // 账户表
  163. $accountData = [
  164. 'join_account_member_id' => $memberId,
  165. 'member_account_classify' => 'POINTS',
  166. 'member_account_status' => 'ACTIVED',
  167. 'member_account_category' => 'NORMAL',
  168. 'member_account_nbr' => $memberId . '-POINTS',
  169. 'member_account_name' => '积分账户',
  170. 'member_account_addtimes' => time()
  171. ];
  172. // 积分账户
  173. MemberAccount::insert($accountData);
  174. $accountData['member_account_classify'] = 'CASH';
  175. $accountData['member_account_nbr'] = $memberId . '-CASH';
  176. $accountData['member_account_name'] = '余额账户';
  177. // 现金/余额账户
  178. MemberAccount::insert($accountData);
  179. // cert
  180. $certData = [
  181. 'join_cert_member_id' => $memberId,
  182. 'member_cert_name' => $params['username']
  183. ];
  184. MemberCert::insert($certData);
  185. // info
  186. $infoData = [
  187. 'join_info_member_id' => $memberId,
  188. 'member_info_nickname' => $params['username']
  189. ];
  190. MemberInfo::insert($infoData);
  191. }
  192. $appointmentId = "AP" . date('YmdHis') . random_string(6, 'up');
  193. $data = [
  194. 'appointment_id' => $appointmentId,
  195. 'join_appointment_member_id' => $params['join_appointment_member_id'] ?? '',
  196. 'join_appointment_goods_id' => $params['join_appointment_goods_id'] ?? '',
  197. 'join_appointment_goods_sku_id' => $params['join_appointment_goods_sku_id'] ?? '',
  198. 'appointment_status' => $params['appointment_status'] ?? '',
  199. 'appointment_datetime' => isset($params['appointment_datetime']) ? date('Y-m-d', strtotime($params['appointment_datetime'])) : '',
  200. 'appointment_apply_datetime' => isset($params['appointment_apply_datetime']) ? date('Y-m-d H:i:s', strtotime($params['appointment_apply_datetime'])) : '',
  201. 'appointment_doing_datetime' => isset($params['appointment_doing_datetime']) ? date('Y-m-d H:i:s', strtotime($params['appointment_doing_datetime'])) : '',
  202. 'appointment_done_datetime' => isset($params['appointment_done_datetime']) ? date('Y-m-d H:i:s', strtotime($params['appointment_done_datetime'])) : '',
  203. 'appointment_cancel_datetime' => isset($params['appointment_cancel_datetime']) ? date('Y-m-d H:i:s', strtotime($params['appointment_cancel_datetime'])) : '',
  204. 'appointment_remark' => $params['appointment_remark'] ?? '',
  205. 'appointment_category' => 'NOFEE',
  206. 'appointment_addtimes' => time(),
  207. 'appointment_mode' => $params['appointment_mode'] ?? '',
  208. 'settlement_mode' => $params['settlement_mode'] ?? '',
  209. ];
  210. if (!empty($params['join_appointment_member_id'])) {
  211. $member = Member::with([
  212. 'cert' => function ($query) {
  213. $query->select('join_cert_member_id', 'member_cert_name');
  214. }
  215. ])->where('member_id', $params['join_appointment_member_id'])
  216. ->first()
  217. ->toArray();
  218. $data['appointment_apply_json'] = json_encode([
  219. 'name' => !empty($member['cert']['member_cert_name']) ? $member['cert']['member_cert_name'] : $member['member_mobile'],
  220. 'mobile' => $member['member_mobile'],
  221. 'person' => $params['person'],
  222. 'remark' => $data['appointment_remark'],
  223. 'premises' => $params['premises'],
  224. 'times' => $params['appointment_times']
  225. ]);
  226. }
  227. Appointment::insert($data);
  228. // 如果预约已完成,生成订单
  229. if ($params['appointment_status'] == 'DONE') {
  230. $sku = GoodsSku::find($params['join_appointment_goods_sku_id']);
  231. $orderId = 'OD' . date('YmdHis') . random_string(6, 'up');
  232. $groupBy = 'OD' . date('YmdHis') . random_string(6, 'up');
  233. $order = [
  234. 'order_id' => $orderId,
  235. 'order_groupby' => $groupBy,
  236. 'join_order_member_id' => $params['join_appointment_member_id'],
  237. 'order_name' => "预约-" . $goods->goods_name,
  238. 'order_amount_total' => $sku->goods_sku_sales_price * $params['person'],
  239. 'order_amount_pay' => $sku->goods_sku_sales_price * $params['person'],
  240. 'order_is_complete' => 'N',
  241. 'order_category' => 'OTHER',
  242. 'order_status_system' => 'PAYING',
  243. 'order_status_payment' => 'PENDING',
  244. 'order_status_storage' => 'PENDING',
  245. 'order_addtimes' => time()
  246. ];
  247. Order::insert($order);
  248. // 订单详情
  249. $sheet = [
  250. 'join_sheet_member_id' => $params['join_appointment_member_id'],
  251. 'join_sheet_order_id' => $orderId,
  252. 'join_sheet_goods_id' => $params['join_appointment_goods_id'],
  253. 'order_sheet_status' => 'PAYING',
  254. 'order_sheet_category' => 'APPOINTMENT',
  255. 'order_sheet_unit' => '套',
  256. 'order_sheet_num' => $params['person'],
  257. 'order_sheet_price' => $sku->goods_sku_sales_price,
  258. 'order_sheet_amount' => $sku->goods_sku_sales_price * $params['person'],
  259. 'order_sheet_pay' => $sku->goods_sku_sales_price * $params['person'],
  260. 'order_sheet_task_status' => 'NONE',
  261. 'order_sheet_addtimes' => time()
  262. ];
  263. OrderSheet::insert($sheet);
  264. // 订单号更新到预约
  265. Appointment::where('appointment_id', $data['appointment_id'])->update(['join_appointment_order_id' => $orderId]);
  266. // 支付数据
  267. $pay = [
  268. 'join_pay_member_id' => $params['join_appointment_member_id'],
  269. 'join_pay_order_id' => $groupBy,
  270. 'pay_amount' => $sheet['order_sheet_pay'],
  271. 'pay_remark' => '预约单',
  272. 'pay_addtimes' => time(),
  273. 'pay_prepayid' => $params['join_appointment_member_id'] . '-' . $params['settlement_mode'],
  274. 'pay_paytimes' => date('Y-m-d H:i:s'),
  275. 'join_pay_object_json' => json_encode(['appointment_id' => $appointmentId]),
  276. 'pay_category' => "APPOINTMENT"
  277. ];
  278. // 现金余额结算
  279. if ($params['settlement_mode'] == 'CASH') {
  280. $account = MemberAccount::where('join_account_member_id', $params['join_appointment_member_id'])
  281. ->where('member_account_classify', 'CASH')
  282. ->where('member_account_status', 'ACTIVED')
  283. ->first();
  284. if (!$account) {
  285. Db::rollBack();
  286. return json_fail('当前账户不存在或已过期');
  287. }
  288. if ($account->member_account_surplus < $sheet['order_sheet_pay']) {
  289. throw new BusinessException("账户余额不足");
  290. // $pay['pay_status'] = 'PENDING';
  291. } else {
  292. MemberAccount::where('member_account_id', $account->member_account_id)->update([
  293. 'member_account_expend' => $account->member_account_expend + $sheet['order_sheet_pay'],
  294. 'member_account_surplus' => $account->member_account_surplus - $sheet['order_sheet_pay']
  295. ]);
  296. $pay['pay_status'] = 'SUCCESS';
  297. // 更新订单状态
  298. Order::where('order_id', $orderId)->update(['order_status_system' => 'CONFIRM', 'order_status_payment' => 'SUCCESS']);
  299. OrderSheet::where('join_sheet_order_id', $orderId)->update(['order_sheet_status' => 'CONFIRM']);
  300. }
  301. PayDetail::insert($pay);
  302. } elseif ($params['settlement_mode'] == 'QRCODE') {
  303. $pay['pay_status'] = 'SUCCESS';
  304. PayDetail::insert($pay);
  305. // 更新订单状态
  306. Order::where('order_id', $orderId)->update(['order_status_system' => 'CONFIRM', 'order_status_payment' => 'SUCCESS']);
  307. OrderSheet::where('join_sheet_order_id', $orderId)->update(['order_sheet_status' => 'CONFIRM']);
  308. }
  309. }
  310. Db::commit();
  311. return json_success("添加预约成功");
  312. }catch (BusinessException $e){
  313. Db::rollBack();
  314. return json_fail($e->getMessage());
  315. } catch (\Exception $e) {
  316. Db::rollBack();
  317. dump($e->getMessage());
  318. return json_fail("添加预约失败");
  319. }
  320. }
  321. public static function update(Request $request)
  322. {
  323. $params = $request->post();
  324. $appointmentId = $params['appointment_id'];
  325. $appointment = Appointment::where('appointment_id', $appointmentId)->first();
  326. if (empty($appointment)) {
  327. return json_fail("数据不存在");
  328. }
  329. Db::beginTransaction();
  330. try {
  331. // 先检查选择的时段是否可预约
  332. $goods = Goods::where('goods_id', $params['join_appointment_goods_id'])->first();
  333. $appointments = Appointment::where('appointment_datetime', $params['appointment_datetime'])
  334. ->where('appointment_id', '<>', $params['appointment_id'])
  335. ->get()
  336. ->toArray();
  337. if ($goods->is_support_appointment == 'Y' && !empty($goods->goods_attribute_json)) {
  338. $extendJson = json_decode($goods->goods_attribute_json, true);
  339. foreach ($extendJson['times'] as $key => $time) {
  340. if ($time['duration'] == $params['appointment_times']) {
  341. if (!empty($appointments)){
  342. foreach ($appointments as $appointmentItem) {
  343. $applyJson = json_decode($appointmentItem['appointment_apply_json'], true);
  344. $applyJsonTimes = $applyJson['times'] ?? '';
  345. if ($time['duration'] == $applyJsonTimes) {
  346. $extendJson['times'][$key]['person'] -= $applyJson['person'];
  347. if ($extendJson['times'][$key]['person'] < $params['person']) {
  348. return json_fail('当前时段名额不足,请选择其他时段');
  349. }
  350. }
  351. }
  352. }else{
  353. if ($extendJson['times'][$key]['person'] < $params['person']) {
  354. return json_fail('当前时段名额不足,请选择其他时段');
  355. }
  356. }
  357. }
  358. }
  359. }
  360. $data = [
  361. 'appointment_status' => $params['appointment_status'],
  362. // 'join_appointment_goods_sku_id' => $params['join_appointment_goods_sku_id'] ?? '',
  363. 'appointment_datetime' => $params['appointment_datetime'] ? date('Y-m-d', strtotime($params['appointment_datetime'])) : null,
  364. 'appointment_apply_datetime' => $params['appointment_apply_datetime'] ? date('Y-m-d H:i:s', strtotime($params['appointment_apply_datetime'])) : null,
  365. 'appointment_doing_datetime' => $params['appointment_doing_datetime'] ? date('Y-m-d H:i:s', strtotime($params['appointment_doing_datetime'])) : null,
  366. 'appointment_done_datetime' => $params['appointment_done_datetime'] ? date('Y-m-d H:i:s', strtotime($params['appointment_done_datetime'])) : null,
  367. 'appointment_cancel_datetime' => $params['appointment_cancel_datetime'] ? date('Y-m-d H:i:s', strtotime($params['appointment_cancel_datetime'])) : null,
  368. 'appointment_remark' => $params['appointment_remark'] ?? '',
  369. 'appointment_mode' => $params['appointment_mode'] ?? '',
  370. 'settlement_mode' => $params['settlement_mode'] ?? '',
  371. ];
  372. // 如果是完成状态
  373. // if ($params['appointment_status'] == 'DONE' && $appointment->appointment_status != 'DONE') {
  374. // if (empty($params['appointment_done_datetime'])) {
  375. // $data['appointment_done_datetime'] = date('Y-m-d H:i:s');
  376. // }
  377. // MemberBenefit::where('member_benefit_id', $appointment->join_appointment_member_benefit_id)->increment('member_benefit_used_count');
  378. // }
  379. if (!empty($params['join_appointment_member_id'])) {
  380. $member = Member::with([
  381. 'cert' => function ($query) {
  382. $query->select('join_cert_member_id', 'member_cert_name');
  383. }
  384. ])->where('member_id', $params['join_appointment_member_id'])
  385. ->first();
  386. if(!$member){
  387. throw new BusinessException("核销人员不存在");
  388. }
  389. $member = $member->toArray();
  390. $data['appointment_apply_json'] = json_encode([
  391. 'name' => !empty($member['cert']['member_cert_name']) ? $member['cert']['member_cert_name'] : $member['member_mobile'],
  392. 'mobile' => $member['member_mobile'],
  393. 'person' => $params['person'],
  394. 'remark' => $data['appointment_remark'],
  395. 'premises' => $params['premises'],
  396. 'times' => $params['appointment_times']
  397. ]);
  398. }
  399. Appointment::where('appointment_id', $appointmentId)->update($data);
  400. // 如果服务结束,没有生成过订单,生成订单
  401. if ($params['appointment_status'] == 'DONE' && empty($appointment->join_appointment_order_id) && $params['settlement_mode'] != 'QUOTA') {
  402. $sku = GoodsSku::find($params['join_appointment_goods_sku_id']);
  403. $orderId = 'OD' . date('YmdHis') . random_string(6, 'up');
  404. $groupBy = 'OD' . date('YmdHis') . random_string(6, 'up');
  405. $order = [
  406. 'order_id' => $orderId,
  407. 'order_groupby' => $groupBy,
  408. 'join_order_member_id' => $params['join_appointment_member_id'],
  409. 'order_name' => "预约-" . $goods->goods_name,
  410. 'order_amount_total' => $sku->goods_sku_sales_price * $params['person'],
  411. 'order_amount_pay' => $sku->goods_sku_sales_price * $params['person'],
  412. 'order_is_complete' => 'N',
  413. 'order_category' => 'OTHER',
  414. 'order_status_system' => 'PAYING',
  415. 'order_status_payment' => 'PENDING',
  416. 'order_status_storage' => 'PENDING',
  417. 'order_addtimes' => time()
  418. ];
  419. Order::insert($order);
  420. // 订单详情
  421. $sheet = [
  422. 'join_sheet_member_id' => $params['join_appointment_member_id'],
  423. 'join_sheet_order_id' => $orderId,
  424. 'join_sheet_goods_id' => $params['join_appointment_goods_id'],
  425. 'order_sheet_status' => 'PAYING',
  426. 'order_sheet_category' => 'APPOINTMENT',
  427. 'order_sheet_unit' => '套',
  428. 'order_sheet_num' => $params['person'],
  429. 'order_sheet_price' => $sku->goods_sku_sales_price,
  430. 'order_sheet_amount' => $sku->goods_sku_sales_price * $params['person'],
  431. 'order_sheet_pay' => $sku->goods_sku_sales_price * $params['person'],
  432. 'order_sheet_task_status' => 'NONE',
  433. 'order_sheet_addtimes' => time()
  434. ];
  435. OrderSheet::insert($sheet);
  436. // 订单号更新到预约
  437. Appointment::where('appointment_id', $appointmentId)->update(['join_appointment_order_id' => $orderId]);
  438. // 支付数据
  439. $pay = [
  440. 'join_pay_member_id' => $params['join_appointment_member_id'],
  441. 'join_pay_order_id' => $groupBy,
  442. 'pay_amount' => $sheet['order_sheet_pay'],
  443. 'pay_remark' => '预约单',
  444. 'pay_addtimes' => time(),
  445. 'pay_prepayid' => $params['join_appointment_member_id'].'-'.$params['settlement_mode'],
  446. 'pay_paytimes' => date('Y-m-d H:i:s'),
  447. 'pay_category'=>$goods->goods_classify
  448. ];
  449. // 现金余额结算
  450. if ($params['settlement_mode'] == 'CASH') {
  451. $account = MemberAccount::where('join_account_member_id', $params['join_appointment_member_id'])
  452. ->where('member_account_classify', 'CASH')
  453. ->where('member_account_status', 'ACTIVED')
  454. ->first();
  455. if (!$account) {
  456. Db::rollBack();
  457. return json_fail('当前账户不存在或已过期');
  458. }
  459. if ($account->member_account_surplus < $sheet['order_sheet_pay']) {
  460. throw new BusinessException("账户余额不足");
  461. } else {
  462. MemberAccount::where('member_account_id', $account->member_account_id)->update([
  463. 'member_account_expend' => $account->member_account_expend + $sheet['order_sheet_pay'],
  464. 'member_account_surplus' => $account->member_account_surplus - $sheet['order_sheet_pay']
  465. ]);
  466. $pay['pay_status'] = 'SUCCESS';
  467. $pay['join_pay_object_json'] = json_encode(['payment' => ['member_account_id' => $account->member_account_id]]);
  468. // 更新订单状态
  469. Order::where('order_id', $orderId)->update(['order_status_system' => 'CONFIRM', 'order_status_payment' => 'SUCCESS']);
  470. OrderSheet::where('join_sheet_order_id', $orderId)->update(['order_sheet_status' => 'CONFIRM']);
  471. }
  472. // $pay['pay_category'] = 'CASH';
  473. PayDetail::insert($pay);
  474. } elseif ($params['settlement_mode'] == 'QRCODE' && empty($appointment->join_appointment_order_id)) {
  475. // $pay['pay_category'] = 'QRCODE';
  476. $pay['pay_status'] = 'SUCCESS';
  477. PayDetail::insert($pay);
  478. // 更新订单状态
  479. Order::where('order_id', $orderId)->update(['order_status_system' => 'CONFIRM', 'order_status_payment' => 'SUCCESS']);
  480. OrderSheet::where('join_sheet_order_id', $orderId)->update(['order_sheet_status' => 'CONFIRM']);
  481. }
  482. // 回写权益表
  483. $benefitId = 'BF'.date('YmdHis').random_string(6,'up');
  484. $benefitData = [
  485. 'member_benefit_id'=>$benefitId,
  486. 'join_benefit_member_id'=>$params['join_appointment_member_id'],
  487. 'join_benefit_goods_id'=>$params['join_appointment_goods_id'],
  488. 'join_benefit_goods_sku_id'=>$params['join_appointment_goods_sku_id'] ?? '',
  489. 'join_benefit_order_id'=>$orderId,
  490. 'member_benefit_status'=>'DONE',
  491. 'member_benefit_name'=>$goods->goods_name,
  492. 'member_benefit_limit_count'=>1,
  493. 'member_benefit_used_count'=>1,
  494. 'member_benefit_addtimes'=>time()
  495. ];
  496. MemberBenefit::insert($benefitData);
  497. // $premisesName = SysDept::where('dept_id',$params['premises'])->value('dept_name');
  498. // 记录核销
  499. $paramsData = [
  500. 'dept_premises_id' => $params['premises'],
  501. 'order_remark'=>$params['appointment_remark'],
  502. 'write_off_member_id' => $params['write_off_member_id'],
  503. 'join_order_member_id' => $params['join_appointment_member_id'],
  504. ];
  505. $writeOffData = OrderService::generateWriteOffData($paramsData);
  506. Appointment::where('appointment_id', $appointmentId)->update([
  507. 'join_appointment_member_benefit_id' => $benefitId,
  508. 'appointment_status' => 'DONE',
  509. 'appointment_done_datetime' => $params['appointment_done_datetime'] ? date('Y-m-d H:i:s', strtotime($params['appointment_done_datetime'])) : null,
  510. 'appointment_done_json' => json_encode($writeOffData)
  511. ]);
  512. }
  513. // 已有服务核销
  514. if($params['appointment_status'] == 'DONE' && $params['settlement_mode'] == 'QUOTA' && !empty($params['quota_id'])){
  515. // $premisesName = SysDept::where('dept_id',$params['premises'])->value('dept_name');
  516. $paramsData = [
  517. 'member_id'=>$params['join_appointment_member_id'],
  518. 'nbr'=>$params['person'],
  519. 'times'=>date('Y-m-d H:i:s'),
  520. 'write_off_member_id' => $params['write_off_member_id'],
  521. 'remark'=>$params['appointment_remark'],
  522. 'dept_premises_id'=>$params['premises']
  523. ];
  524. $quotaType = substr($params['quota_id'],0,2);
  525. if($quotaType == 'MQ'){
  526. $quota = MemberQuota::where('member_quota_id',$params['quota_id'])
  527. ->select('member_quota_id','join_member_rule_added_component_id')
  528. ->first()
  529. ->toArray();
  530. $paramsData['component_id'] = $quota['join_member_rule_added_component_id'];
  531. QuotaController::doWriteOff($paramsData);
  532. }else if($quotaType == 'BF'){
  533. $paramsData['benefit_id'] = $params['quota_id'];
  534. BenefitController::doWriteOff($paramsData);
  535. }
  536. // 回写权益ID
  537. if(empty($appointment->join_appointment_member_benefit_id)){
  538. Appointment::where('appointment_id', $appointmentId)->update(['join_appointment_member_benefit_id' => $params['quota_id']]);
  539. }
  540. }
  541. Db::commit();
  542. return json_success("数据更新成功");
  543. }catch(\support\exception\BusinessException $e){
  544. Db::rollBack();
  545. dump($e->getFile().'--'.$e->getLine());
  546. return json_fail($e->getMessage());
  547. } catch (\Exception $e) {
  548. Db::rollBack();
  549. dump($e->getFile().'--'.$e->getLine());
  550. return json_fail("数据更新失败");
  551. }
  552. }
  553. public static function getTimes(Request $request)
  554. {
  555. $goodsId = $request->get('goods_id', '');
  556. $date = $request->get('date', '');
  557. // $memberId = $request->get('member_id', '');
  558. $appointmentId = $request->get('appointment_id', '');
  559. if (!$goodsId || !$date) {
  560. return json_fail("查询时间段失败!");
  561. }
  562. $goods = Goods::where('goods_id', $goodsId)->first();
  563. if (!$goods) {
  564. return json_fail('数据不存在');
  565. }
  566. // 当前服务已预约出去的
  567. $appointments = Appointment::where('join_appointment_goods_id', $goodsId)
  568. ->where('appointment_datetime', $date)
  569. ->when($appointmentId != '', function ($query) use ($appointmentId) {
  570. $query->where('appointment_id', '<>', $appointmentId);
  571. })
  572. ->get()
  573. ->toArray();
  574. if ($goods->is_support_appointment == 'Y' && !empty($goods->goods_attribute_json)) {
  575. $extendJson = json_decode($goods->goods_attribute_json, true);
  576. $times = $extendJson['times'];
  577. foreach ($times as $key => $time) {
  578. // $timesStr = $time['appointmentTimeStart'] . '~' . $time['appointmentTimeEnd'];
  579. $times[$key]['timesStr'] = $time['duration'];
  580. foreach ($appointments as $appointment) {
  581. if (!empty($appointment['appointment_apply_json'])) {
  582. $applyJson = json_decode($appointment['appointment_apply_json'], true);
  583. $applyJsonTimes = $applyJson['times'] ?? '';
  584. if ($time['duration'] == $applyJsonTimes) {
  585. $times[$key]['person'] -= $applyJson['person'];
  586. if ($times[$key]['person'] < 1) {
  587. $times[$key]['disabled'] = true;
  588. }
  589. }
  590. }
  591. }
  592. }
  593. return json_success('', $times);
  594. }
  595. }
  596. }