AppointmentService.php 38 KB

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