DeliveryAppointmentController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CarType;
  4. use App\Components\AsyncResponse;
  5. use App\DeliveryAppointment;
  6. use App\DeliveryAppointmentCar;
  7. use App\Events\DeliveryAppointmentEvent;
  8. use App\Imports\AppointmentDetail;
  9. use App\Jobs\DeliveryAppointmentCheck;
  10. use App\Logistic;
  11. use App\Services\common\ExportService;
  12. use App\Store;
  13. use App\Warehouse;
  14. use Carbon\Carbon;
  15. use Carbon\CarbonPeriod;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Gate;
  20. use Illuminate\Support\Facades\Validator;
  21. use Oursdreams\Export\Export;
  22. class DeliveryAppointmentController extends Controller
  23. {
  24. use AsyncResponse;
  25. public function list()
  26. {
  27. if(!Gate::allows('入库管理-入库预约-预约管理')){ return view("exception.authority"); }
  28. $list = app("DeliveryAppointmentService")->query(request()->input())
  29. ->with(["logistic",'warehouse','details.commodity.barcodes','cars.car'])
  30. ->paginate(request("paginate") ?? 50);
  31. $warehouses = Warehouse::query()->select("id","name")->get();
  32. $owners = app("OwnerService")->getIntersectPermitting();
  33. return view("store.deliveryAppointment.list",compact("list","warehouses","owners"));
  34. }
  35. public function appointment()
  36. {
  37. if(!Gate::allows('入库管理-入库预约-预约')){ return view("store.deliveryAppointment.index"); }
  38. $owners = app("OwnerService")->getIntersectPermitting();
  39. $cars = CarType::query()->get();
  40. $logistics = Logistic::query()->get();
  41. $warehouses = Warehouse::query()->select("id","name")->get();
  42. return view("store.deliveryAppointment.appointment",compact("owners","cars","warehouses","logistics"));
  43. }
  44. public function import()
  45. {
  46. $this->importExcel(new AppointmentDetail());
  47. }
  48. /**
  49. * 获取产能
  50. *
  51. */
  52. public function getCapacity()
  53. {
  54. $this->gate("入库管理-入库预约-预约");
  55. $model = request("model");
  56. $errors = $this->appointmentValidator($model)->errors();
  57. if (count($errors)>0)$this->success(["errors"=>$errors]);
  58. /** @var \stdClass $warehouse */
  59. $warehouse = Warehouse::query()->find($model["warehouse_id"]);
  60. $tonne = $model["tonne"] ?? 0;
  61. $cubicMeter = $model["cubic_meter"] ?? 0;
  62. $amount = request("detail_amount");
  63. $need = app("DeliveryAppointmentService")->calculateCapacity($tonne,$cubicMeter,$amount,$warehouse->reduced_production_capacity_coefficient);//所需产能
  64. $start = Carbon::today();
  65. $end = Carbon::today()->addDays(6);
  66. $map = [];
  67. DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  68. ->whereBetween("appointment_date",[$start->toDateString(),$end->toDateString()])
  69. ->whereIn("status",[0,2])
  70. ->where("warehouse_id",$warehouse->id)
  71. ->groupBy(["appointment_date","date_period"])->get()
  72. ->each(function ($appointment)use(&$map){
  73. $map[$appointment->appointment_date."-".$appointment->date_period] = $appointment->capacity;
  74. });
  75. $list = [];
  76. $capacity = $warehouse->production_capacity;
  77. foreach (CarbonPeriod::create($start,$end) as $date){
  78. /** @var $date Carbon */
  79. $date = $date->format("Y-m-d");
  80. $periods = [];
  81. if ($date==date("Y-m-d")){
  82. $hour = date("H");
  83. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  84. $period = explode("-",$period);
  85. $periodArr = ["time"=>$period[0].":00 - ".$period[1].":00","index"=>$key,"isAvailable"=>false];
  86. if ($hour<$period[1]-1){
  87. $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
  88. $used = $map[$date."-".$key] ?? 0; //已使用产能
  89. $available = $total-$used; //可用产能
  90. if ($available > $need)$periodArr["isAvailable"] = true;
  91. }
  92. $periods[] = $periodArr;
  93. }
  94. }else{
  95. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  96. $period = explode("-",$period);
  97. $period = $period[0].":00 - ".$period[1].":00";
  98. $periodArr = ["time"=>$period,"index"=>$key,"isAvailable"=>false];
  99. $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
  100. $used = $map[$date."-".$key] ?? 0; //已使用产能
  101. $available = $total-$used; //可用产能
  102. if ($available > $need)$periodArr["isAvailable"] = true;
  103. $periods[] = $periodArr;
  104. }
  105. }
  106. $list[] = ["date"=>$date,"period"=>$periods];
  107. }
  108. $this->success($list);
  109. }
  110. /**
  111. * 确定预约
  112. */
  113. public function submitAppointment()
  114. {
  115. $this->gate("入库管理-入库预约-预约");
  116. $model = request("model");
  117. $selectDate = request("date");
  118. $details = request("details");
  119. $errors = $this->appointmentValidator($model)->errors();
  120. if (count($errors)>0)$this->success(["errors"=>$errors]);
  121. $errors = Validator::make($selectDate,[
  122. "date" => ["required","date","after_or_equal:today"],
  123. "time" => ["required","integer"],
  124. ])->errors();
  125. if (count($errors)>0)$this->error("未选定预约日期");
  126. DB::transaction(function ()use($model,$selectDate,$details,&$appointment){
  127. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  128. ->where("appointment_date",$selectDate["date"])
  129. ->where("date_period",$selectDate["time"])
  130. ->where("warehouse_id",$model["warehouse_id"])
  131. ->where("status",0)
  132. ->groupBy(["appointment_date","date_period"])
  133. ->lockForUpdate()->first();
  134. /** @var \stdClass $warehouse */
  135. $warehouse = Warehouse::query()->find($model["warehouse_id"]);
  136. $need = app("DeliveryAppointmentService")->
  137. calculateCapacity($model["tonne"] ?? 0,$model["cubic_meter"] ?? 0,count($details),
  138. $warehouse->reduced_production_capacity_coefficient);
  139. if ($result){
  140. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
  141. $available = $total-$result->capacity;
  142. if ($available < $need)$this->success(["isFail"=>true]);
  143. }
  144. /** @var \stdClass $appointment */
  145. $appointment = DeliveryAppointment::query()->create([
  146. "user_id" => Auth::id(),
  147. "owner_id" => $model["owner_id"],
  148. "procurement_number" => $model["procurement_number"] ?? null,
  149. "asn_number" => $model["asn_number"] ?? null,
  150. "logistic_number" => $model["logistic_number"] ?? null,
  151. "logistic_id" => $model["logistic_id"] ?? null,
  152. "warehouse_id" => $model["warehouse_id"],
  153. "tonne" => $model["tonne"] ?? 0,
  154. "cubic_meter" => $model["cubic_meter"] ?? 0,
  155. "box_amount" => $model["box_amount"] ?? 0,
  156. "capacity" => $need,
  157. "appointment_date" => $selectDate["date"],
  158. "date_period" => $selectDate["time"],
  159. "remark" => $model["remark"] ?? null,
  160. ]);
  161. if ($details)app("DeliveryAppointmentService")->insertDetails($appointment,$details);
  162. $insert = [];
  163. foreach ($model["cars"] as $index=>$car){
  164. $rand = mt_rand(0,9);
  165. $len = strlen($appointment->id);
  166. $ten = $len < 2 ? "0" : substr($appointment->id,$len-2,1);
  167. $one = substr($appointment->id,$len-1,1);
  168. //唯一码 仓库CODE+随机数+十位+当前下标+个位+日期
  169. $number = $warehouse->code.$rand.$ten.$index.$one.date("d");
  170. $insert[] = [
  171. "delivery_appointment_id" => $appointment->id,
  172. "license_plate_number" => $car["license_plate_number"],
  173. "car_id" => $car["car_id"] ?? null,
  174. "driver_name" => $car["driver_name"] ?? null,
  175. "driver_phone" => $car["driver_phone"] ?? null,
  176. "appointment_number" => $number,
  177. ];
  178. }
  179. DeliveryAppointmentCar::query()->insert($insert);
  180. });
  181. dispatch(new DeliveryAppointmentCheck($appointment->id))->delay(Carbon::parse($appointment->appointment_date." ".(explode("-",DeliveryAppointment::PERIOD[$appointment->date_period])[1]).":00:01"));
  182. //当日或次日预约单广播
  183. if (strtotime(date('Y-m-d',strtotime("+2 day")))>strtotime($appointment->appointment_date." 00:00:00")){
  184. $appointment->load("cars");
  185. event(new DeliveryAppointmentEvent($appointment->cars[0]));
  186. }
  187. //md5加密在密文第五位后插入
  188. $md5 = substr_replace(md5(date("m-d")),$appointment->id,5,0);
  189. $this->success(["key"=>$md5]);
  190. }
  191. /**
  192. * 修改预约
  193. */
  194. public function updateAppointment()
  195. {
  196. $this->gate("入库管理-入库预约-预约");
  197. $id = request("id");
  198. if (!$id)$this->error("非法参数");
  199. $selectDate = request("date");
  200. $errors = Validator::make($selectDate,[
  201. "date" => ["required","date","after_or_equal:today"],
  202. "time" => ["required","integer"],
  203. ])->errors();
  204. if (count($errors)>0)$this->error("未选定预约日期");
  205. /** @var DeliveryAppointment|\stdClass $appointment */
  206. $appointment = DeliveryAppointment::query()->with("cars")->find($id);
  207. if (!$appointment)$this->error("预约单不存在");
  208. foreach ($appointment->cars as $car){
  209. if ($car->status!=0)$this->error("车辆已达,无法修改预约");
  210. }
  211. DB::transaction(function ()use($id,$selectDate,&$appointment){
  212. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  213. ->where("appointment_date",$selectDate["date"])
  214. ->where("date_period",$selectDate["time"])
  215. ->where("warehouse_id",$appointment->warehouse_id)
  216. ->where("status",0)
  217. ->groupBy(["appointment_date","date_period"])
  218. ->lockForUpdate()->first();
  219. /** @var \stdClass $warehouse */
  220. $warehouse = Warehouse::query()->find($appointment->warehouse_id);
  221. if ($result){
  222. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
  223. $available = $total-$result->capacity;
  224. if ($available < $appointment->capacity)$this->success(["isFail"=>true]);
  225. }
  226. $appointment->update([
  227. "appointment_date" => $selectDate["date"],
  228. "date_period" => $selectDate["time"],
  229. ]);
  230. });
  231. dispatch(new DeliveryAppointmentCheck($appointment->id))->delay(Carbon::parse($selectDate["date"]." ".(explode("-",DeliveryAppointment::PERIOD[$selectDate["time"]])[1]).":00:01"));
  232. //当日或次日预约单广播
  233. $old = $appointment->appointment_date == date('Y-m-d') ? 0 : ($appointment->appointment_date == date('Y-m-d',strtotime('+1 day') ? 1 : 2));
  234. $new = $selectDate["date"] == date('Y-m-d') ? 0 : ($selectDate["date"] == date('Y-m-d',strtotime('+1 day') ? 1 : 2));
  235. if ($old==2 && $new==2)$this->success(); //超过广播区间不推送
  236. if (($old-$new)!=0 || $appointment->date_period!=$selectDate["time"]){
  237. $appointment->cars[0]->change = true;
  238. $appointment->cars[0]->old = $old;
  239. $appointment->cars[0]->new = $new;
  240. event(new DeliveryAppointmentEvent($appointment->cars[0]));
  241. }
  242. $this->success();
  243. }
  244. private function appointmentValidator(array $model)
  245. {
  246. return Validator::make($model,[
  247. "owner_id" => ["required","integer"],
  248. "warehouse_id" => ["required","integer"],
  249. "tonne" => ["required_without:cubic_meter","numeric"],
  250. "cubic_meter" => ["required_without:tonne","numeric"],
  251. "box_amount" => ["nullable","integer"],
  252. "cars.*.license_plate_number" => ["nullable","size:7"],
  253. "cars.*.car_id" => ["nullable","integer"],
  254. "cars.*.driver_phone" => ["nullable"],
  255. "cars.*.driver_name" => ["nullable"],
  256. ],[
  257. 'required'=>':attribute 不应为空',
  258. 'integer'=>':attribute 应为数值',
  259. 'required_without'=>':attribute 不应为空',
  260. 'numeric'=>':attribute 必须为数字',
  261. 'size'=>':attribute 非法',
  262. ],[
  263. 'owner_id'=>'货主',
  264. 'warehouse_id'=>'仓库',
  265. 'tonne'=>'吨',
  266. 'cubic_meter'=>'立方',
  267. 'cars.*.license_plate_number'=>'车牌号',
  268. 'cars.*.car_id'=>'车型',
  269. 'cars.*.driver_phone'=>'司机电话',
  270. 'cars.*.driver_name'=>'司机姓名',
  271. ]);
  272. }
  273. /**
  274. * 根据key取id 鉴权数据
  275. */
  276. public function showAppointmentInfo()
  277. {
  278. if(!Gate::allows('入库管理-入库预约-预约')){ return view("exception.authority"); }
  279. $key = request("k");
  280. $len = strlen($key);
  281. $id = substr($key,5,$len-32);
  282. $md5 = substr($key,0,5).substr($key,5+$len-32);
  283. if ($md5!==md5(date("m-d")))return view("exception.404");
  284. /** @var \stdClass $appointment */
  285. $appointment = DeliveryAppointment::query()->with("cars")->find($id);
  286. if (!$appointment || $appointment->user_id != Auth::id())return view("exception.404");
  287. return view("store.deliveryAppointment.success",compact("appointment"));
  288. }
  289. /**
  290. * 取消预约
  291. */
  292. public function cancel()
  293. {
  294. $this->gate("入库管理-入库预约-预约");
  295. $id = request("id");
  296. if (!$id)$this->error("非法参数");
  297. DeliveryAppointment::query()->where("status",0)->where("id",$id)->update(["status"=>1]);
  298. $this->success(1);
  299. }
  300. /**
  301. * 导出
  302. */
  303. public function export()
  304. {
  305. if(!Gate::allows('入库管理-入库预约-预约管理')){ return view("exception.authority"); }
  306. if (request("checkAllSign")){
  307. $params = request()->input();
  308. unset($params["checkAllSign"]);
  309. $query = app("DeliveryAppointmentService")->query($params);
  310. }else $query = app("DeliveryAppointmentService")->query(["id"=>request("id")]);
  311. /** @var Builder $query */
  312. $list = $query->with(["owner","warehouse"])->get();
  313. $row = ["状态","货主","预约时间","仓库","预约号","车牌号","车型",
  314. "司机姓名","司机电话","吨","立方","箱数","采购单号","ASN单号","商品名称","条码","数量","创建时间"];
  315. foreach ($list as &$data){
  316. $appointment = "";
  317. $number = "";
  318. $carType = "";
  319. $driverName = "";
  320. $driverPhone = "";
  321. $commodityName = "";
  322. $commodityCode = "";
  323. $amount = "";
  324. foreach ($data->cars as $car){
  325. $appointment .= $car->appointment_number."\r\n";
  326. $number .= $car->license_plate_number."\r\n";
  327. $carType .= ($car->car->name ?? '')."\r\n";
  328. $driverName .= ($car->driver_name ?? '')."\r\n";
  329. $driverPhone .= ($car->driver_phone ?? '')."\r\n";
  330. }
  331. foreach ($data->details as $detail){
  332. $commodityName .= ($detail->commodity->name ?? $detail->name)."\r\n";
  333. $commodityCode .= ($detail->commodity->barcodes->code ?? $detail->bar_code)."\r\n";
  334. $amount .= $detail->amount."\r\n";
  335. }
  336. $data = [
  337. DeliveryAppointment::STATUS[$data->status],
  338. $data->owner->name ?? '',
  339. $data->appointment_date,
  340. $data->warehouse->name ?? '',
  341. $appointment,
  342. $number,
  343. $carType,
  344. $driverName,
  345. $driverPhone,
  346. $data->tonne,
  347. $data->cubic_meter,
  348. $data->box_amount,
  349. $data->procurement_number,
  350. $data->asn_number,
  351. $commodityName,
  352. $commodityCode,
  353. $amount,
  354. $data->created_at
  355. ];
  356. }
  357. return Export::make($row,$list,"预约记录");
  358. }
  359. private function carList($period,$date,$warehouse)
  360. {
  361. $list = [];
  362. DeliveryAppointmentCar::query()->with(["deliveryAppointment"=>function($query){
  363. /** @var Builder $query */
  364. $query->withCount("cars")->with("owner");
  365. }])->whereHas("deliveryAppointment",function ($query)use($period,$warehouse,$date){
  366. /** @var Builder $query */
  367. $query->where("appointment_date",$date)
  368. ->where("warehouse_id",$warehouse)->whereIn("status",[0,2]);
  369. })->where(function ($query)use($period){
  370. /** @var Builder $query */
  371. $query->where("status",1)->orWhereHas("deliveryAppointment",function (Builder $query)use($period){
  372. $query->where("date_period",">=",$period);
  373. });
  374. })->orderByRaw("(CASE WHEN status=0 THEN 2 WHEN status=2 THEN 3 END),IF(ISNULL(delivery_time),1,0),delivery_time")
  375. ->limit(10)->get()->each(function ($car)use(&$list){
  376. //$diff = $car->delivery_time ? (strtotime($car->delivery_time)+1799)-time() : 0;
  377. $count = $car->deliveryAppointment->cars_count ?? 0;
  378. $owner = $car->deliveryAppointment->owner->name ?? "";
  379. $len = mb_strlen($owner);
  380. $ownerName = "";
  381. for($i=0;$i<$len-1;$i++)$ownerName .= "*";
  382. $ownerName .= mb_substr($owner,$len-1,1);
  383. $list[] = [
  384. "id" => $car->id,
  385. "license_plate_number" => $car->license_plate_number ? $car->license_plate_number : substr($car->appointment_number,0,5)."****".substr($car->appointment_number,9,1),
  386. "driver_name" => $car->driver_name,
  387. "driver_phone" => $car->driver_phone,
  388. "status" => DeliveryAppointmentCar::STATUS[$car->status],
  389. "cubic_meter" => isset($car->deliveryAppointment->cubic_meter) && $car->deliveryAppointment->cubic_meter>0 ? ($count>1 ? $car->deliveryAppointment->cubic_meter."/".$count : $car->deliveryAppointment->cubic_meter) : "",
  390. "tonne" => isset($car->deliveryAppointment->tonne) && $car->deliveryAppointment->tonne>0 ? ($count>1 ? $car->deliveryAppointment->tonne."/".$count : $car->deliveryAppointment->tonne) : "",
  391. //"diff" => $diff>0 ? $diff*1000 : 0,
  392. "owner_name" => $ownerName,
  393. "type" => DeliveryAppointment::TYPE[$car->deliveryAppointment->type_mark] ?? '',
  394. "period"=>isset($car->deliveryAppointment->date_period) ? ($car->deliveryAppointment->date_period==0 ? '上午' : '下午') : '',
  395. "delivery_time" => $car->delivery_time ? substr($car->delivery_time,11,5) : '',
  396. ];
  397. });
  398. return $list;
  399. }
  400. /**
  401. * 获取展览数据
  402. */
  403. public function getExhibitionList()
  404. {
  405. $this->gate("入库管理-入库预约-入库区终端");
  406. $hour = date("H");
  407. $warehouse = request("warehouse");
  408. $index = null;
  409. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  410. $arr = explode("-",$period);
  411. if (count($arr)!=2)continue;
  412. if ($hour<$arr[1]){
  413. $index = $key;
  414. break;
  415. }
  416. }
  417. if ($index===null)$this->success();
  418. $list = $this->carList($index,date("Y-m-d"),$warehouse);
  419. $counts = DeliveryAppointmentCar::query()->whereHas("deliveryAppointment",function (Builder $query)use($index,$warehouse){
  420. $query->where("appointment_date",date("Y-m-d"))
  421. ->where("warehouse_id",$warehouse)->whereIn("status",[0,2]);
  422. })->selectRaw("status, COUNT(1) AS c")->groupByRaw("status")->get();
  423. $success = 0;
  424. $work = 0;
  425. $notReached = 0;
  426. if ($counts)foreach ($counts as $c){
  427. switch ($c->status){
  428. case 0:
  429. $notReached = $c->c;
  430. break;
  431. case 1:
  432. $work = $c->c;
  433. break;
  434. case 2:
  435. $success = $c->c;
  436. break;
  437. }
  438. }
  439. $result = ["list"=>$list,"success"=>$success,"work"=>$work,"notReached"=>$notReached,"nextDay"=>$this->carList(0,date("Y-m-d",strtotime("+1 day")),$warehouse)];
  440. $nextTime = DeliveryAppointment::PERIOD[$index+1] ?? null;
  441. if ($nextTime){
  442. $nextTime = explode("-",$nextTime)[0];
  443. $timestamp = strtotime(date("Y-m-d")." ".$nextTime.":00:00");
  444. $result["refresh"] = (($timestamp-time())*1000) ?? 1000;
  445. }
  446. $this->success($result);
  447. }
  448. public function getKey()
  449. {
  450. $this->success(app("DeliveryAppointmentService")->getKey());
  451. }
  452. /**
  453. * 错误信息
  454. *
  455. * @return string
  456. */
  457. public function errMsg()
  458. {
  459. return <<<html
  460. <div style='font-weight: bold;color: red;margin: 500px auto;text-align: center;'>
  461. <span style='font-size: 200px;'>&times;</span><br>
  462. <span style='font-size: 50px'>二维码已过期,请重新扫描!</span><br><h1 style="color: #1b1e21;">如果多次扫描失败,请联系管理人员检查!</h1>
  463. </div>"
  464. html;
  465. }
  466. /**
  467. * 检查key有效性
  468. * 21-4-2 切换永久二维码 取消时段校验
  469. *
  470. * @param string $key
  471. * @param int $offset
  472. *
  473. * @return bool
  474. */
  475. private function check($key,$offset):bool
  476. {
  477. if (!$key)return false;
  478. $key = base64_decode($key);
  479. $ch = app("DeliveryAppointmentService")->getKey();
  480. $len = strlen($ch);
  481. if (substr($key,0,$len)!=$ch)return false;
  482. /*$timeLen = strlen($key)-$len;
  483. $time = substr($key,$len)+$offset;
  484. $thisTime = (integer)substr(time(),$timeLen*-1);
  485. if ($thisTime>$time)return false;*/
  486. return true;
  487. }
  488. /**
  489. * 进入预约界面填写预约码
  490. */
  491. public function delivery()
  492. {
  493. if (!$this->check(request("k"),65))return $this->errMsg();
  494. return view("store.deliveryAppointment.delivery",["k"=>request("k")]);
  495. }
  496. /**
  497. * 验证预约码 允许当日产能满足后的逾期
  498. *
  499. */
  500. public function checkAppointment()
  501. {
  502. if (!$this->check(request("k"),180))return ["status"=>406];
  503. $number = request("number");
  504. if (!$number)return ["status"=>417];
  505. $period = app("DeliveryAppointmentService")->getPeriod();
  506. if ($period===false)return ["status"=>416]; //非法时段扫码
  507. $mark = false;
  508. mark:
  509. $car = DeliveryAppointmentCar::query()->whereNull("delivery_time")->where("status",0)
  510. ->where("appointment_number",$number)->whereHas("deliveryAppointment",function (Builder $query)use($period){
  511. $query->where("appointment_date",date("Y-m-d"))
  512. ->where("date_period",$period)->whereIn("status",[0,3]);
  513. })->first();
  514. if (!$car && $period===1){$period = 0;$mark = true;goto mark;}
  515. if ($mark && $car){
  516. $car->load("deliveryAppointment");
  517. $available = app("DeliveryAppointmentService")->getAvailableCapacity($car->deliveryAppointment->appointment_date,$car->deliveryAppointment->date_period,$car->deliveryAppointment->warehouse_id);
  518. if ($available<$car->deliveryAppointment->capacity)$car = null;
  519. }
  520. if (!$car)return ["status"=>417];
  521. $car->update(["delivery_time"=>date("Y-m-d H:i:s"),"status"=>1]);
  522. /** @var DeliveryAppointmentCar $car */
  523. if ($mark)DeliveryAppointment::query()->where("id",$car->delivery_appointment_id)->update(["status" => 0]);
  524. else event(new DeliveryAppointmentEvent($car));
  525. return ["status"=>200,"k"=>$car->delivery_appointment_id];
  526. }
  527. public function successMsg()
  528. {
  529. if (!request("k"))return view("exception.404");
  530. /** @var \stdClass $appointment */
  531. $appointment = DeliveryAppointment::query()->with(["cars"=>function($query){
  532. /** @var Builder $query */
  533. $query->whereNull("delivery_time");
  534. }])->find(request("k"));
  535. return view("store.deliveryAppointment.deliverySuccess",["cars"=>$appointment->cars]);
  536. }
  537. /**
  538. * 卸货完成
  539. */
  540. public function unloading()
  541. {
  542. $this->gate("入库管理-入库预约-预约管理-卸货完成");
  543. if (!request("id"))$this->error("非法参数");
  544. /** @var DeliveryAppointmentCar|\stdClass $car */
  545. $car = DeliveryAppointmentCar::query()->find(request("id"));
  546. if (!$car || !$car->deliveryAppointment)$this->error("单据不存在");
  547. $car->update(["status"=>2]);
  548. app("DeliveryAppointmentService")->checkFull($car->delivery_appointment_id);
  549. event(new DeliveryAppointmentEvent($car));
  550. $this->success();
  551. }
  552. /**
  553. * 管理员代替签到
  554. */
  555. public function signIn()
  556. {
  557. $this->gate("入库管理-入库预约-预约管理-签到");
  558. if (!request("id"))$this->error("非法参数");
  559. /** @var DeliveryAppointmentCar|\stdClass $car */
  560. $car = DeliveryAppointmentCar::query()->find(request("id"));
  561. if (!$car || !$car->deliveryAppointment)$this->error("单据不存在");
  562. $car->update(["status"=>1]);
  563. event(new DeliveryAppointmentEvent($car));
  564. $this->success();
  565. }
  566. /**
  567. * 产能维护
  568. * */
  569. public function capacityMaintenance()
  570. {
  571. if(!Gate::allows('入库管理-入库预约-产能维护')){ return view("exception.authority"); }
  572. $warehouses = Warehouse::query()->select("name","id","production_capacity","reduced_production_capacity_coefficient")->get();
  573. return view("store.deliveryAppointment.capacityMaintenance",compact("warehouses"));
  574. }
  575. /**
  576. * 修改产能
  577. */
  578. public function updateCapacity()
  579. {
  580. $this->gate("入库管理-入库预约-产能维护");
  581. $id = request("id");
  582. $capacity = request("production_capacity");
  583. Warehouse::query()->where("id",$id)->update(["production_capacity"=>$capacity]);
  584. $this->success();
  585. }
  586. /**
  587. * 入库区终端界面
  588. */
  589. public function exhibition()
  590. {
  591. if(!Gate::allows('入库管理-入库预约-入库区终端')){ return view("exception.authority"); }
  592. if (!request("warehouse"))$warehouses = Warehouse::query()->select("name","id")->get();
  593. return view("store.deliveryAppointment.exhibition",["warehouses"=>$warehouses??[],"id"=>request("warehouse")]);
  594. }
  595. /**
  596. * 匹配入库单
  597. */
  598. public function verifyASN()
  599. {
  600. $this->gate("入库管理-入库预约-预约");
  601. $asn = preg_split('/[,, ]+/is', request("asn"));
  602. $owner = request("owner_id");
  603. if (!$asn || strlen(request("asn"))<13)$this->error("非法ASN单号");
  604. $query = Store::query()->whereIn("asn_code",$asn);
  605. if ($owner)$query->where("owner_id",$owner);
  606. $stores = $query->with("storeItems")->get();
  607. if (!$stores)$this->error("无此ASN单号");
  608. $items = [];
  609. $stores->each(function ($store)use(&$items){
  610. if ($store->storeItems)$items = array_merge($items,$store->storeItems->toArray());
  611. });
  612. $this->success($items);
  613. }
  614. /**
  615. * 标记质检单
  616. */
  617. public function qualityInspectionMark()
  618. {
  619. $this->gate("入库管理-入库预约-质检");
  620. $ids = request("ids");
  621. if (!$ids)$this->error("未选择任何记录");
  622. DeliveryAppointment::query()->whereIn("id",$ids)->update(["type_mark"=>0]);
  623. $this->success();
  624. }
  625. }