AppointmentService.php 39 KB

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