AppointmentService.php 38 KB

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