AppointmentService.php 38 KB

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