AppointmentService.php 46 KB

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