DeliveryAppointmentController.php 26 KB

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