DeliveryAppointmentController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. //唯一码 随机数+十位+当前下标+个位+日期
  168. $number = $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");
  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. $list[] = [
  363. "id" => $car->id,
  364. "license_plate_number" => $car->license_plate_number,
  365. "driver_name" => $car->driver_name,
  366. "driver_phone" => $car->driver_phone,
  367. "status" => $car->status,
  368. "cubic_meter" => isset($car->deliveryAppointment->cubic_meter) && $car->deliveryAppointment->cubic_meter>0 ? ($count>1 ? $car->deliveryAppointment->cubic_meter."/".$count : $car->deliveryAppointment->cubic_meter) : "",
  369. "tonne" => isset($car->deliveryAppointment->tonne) && $car->deliveryAppointment->tonne>0 ? ($count>1 ? $car->deliveryAppointment->tonne."/".$count : $car->deliveryAppointment->tonne) : "",
  370. //"diff" => $diff>0 ? $diff*1000 : 0,
  371. ];
  372. });
  373. return $list;
  374. }
  375. /**
  376. * 获取展览数据
  377. */
  378. public function getExhibitionList()
  379. {
  380. $this->gate("入库管理-入库预约-入库区终端");
  381. $hour = date("H");
  382. $warehouse = request("warehouse");
  383. $index = null;
  384. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  385. $arr = explode("-",$period);
  386. if (count($arr)!=2)continue;
  387. if ($hour<$arr[1]){
  388. $index = $key;
  389. break;
  390. }
  391. }
  392. if ($index===null)$this->success();
  393. $list = $this->carList($index,date("Y-m-d"),$warehouse);
  394. $counts = DeliveryAppointmentCar::query()->whereHas("deliveryAppointment",function (Builder $query)use($index,$warehouse){
  395. $query->where("appointment_date",date("Y-m-d"))
  396. ->where("warehouse_id",$warehouse)->whereIn("status",[0,2]);
  397. })->selectRaw("status, COUNT(1) AS c")->groupByRaw("status")->get();
  398. $success = 0;
  399. $work = 0;
  400. $notReached = 0;
  401. if ($counts)foreach ($counts as $c){
  402. switch ($c->status){
  403. case 0:
  404. $notReached = $c->c;
  405. break;
  406. case 1:
  407. $work = $c->c;
  408. break;
  409. case 2:
  410. $success = $c->c;
  411. break;
  412. }
  413. }
  414. $result = ["list"=>$list,"success"=>$success,"work"=>$work,"notReached"=>$notReached,"nextDay"=>$this->carList(0,date("Y-m-d",strtotime("+1 day")),$warehouse)];
  415. $nextTime = DeliveryAppointment::PERIOD[$index+1] ?? null;
  416. if ($nextTime){
  417. $nextTime = explode("-",$nextTime)[0];
  418. $timestamp = strtotime(date("Y-m-d")." ".$nextTime.":00:00");
  419. $result["refresh"] = (($timestamp-time())*1000) ?? 1000;
  420. }
  421. $this->success($result);
  422. }
  423. public function getKey()
  424. {
  425. $this->success(app("DeliveryAppointmentService")->getKey());
  426. }
  427. /**
  428. * 错误信息
  429. *
  430. * @return string
  431. */
  432. public function errMsg()
  433. {
  434. return <<<html
  435. <div style='font-weight: bold;color: red;margin: 500px auto;text-align: center;'>
  436. <span style='font-size: 200px;'>&times;</span><br>
  437. <span style='font-size: 50px'>二维码已过期,请重新扫描!</span><br><h1 style="color: #1b1e21;">如果多次扫描失败,请联系管理人员检查!</h1>
  438. </div>"
  439. html;
  440. }
  441. /**
  442. * 检查key有效性
  443. * 21-4-2 切换永久二维码 取消时段校验
  444. *
  445. * @param string $key
  446. * @param int $offset
  447. *
  448. * @return bool
  449. */
  450. private function check($key,$offset):bool
  451. {
  452. if (!$key)return false;
  453. $key = base64_decode($key);
  454. $ch = app("DeliveryAppointmentService")->getKey();
  455. $len = strlen($ch);
  456. if (substr($key,0,$len)!=$ch)return false;
  457. /*$timeLen = strlen($key)-$len;
  458. $time = substr($key,$len)+$offset;
  459. $thisTime = (integer)substr(time(),$timeLen*-1);
  460. if ($thisTime>$time)return false;*/
  461. return true;
  462. }
  463. /**
  464. * 进入预约界面填写预约码
  465. */
  466. public function delivery()
  467. {
  468. if (!$this->check(request("k"),65))return $this->errMsg();
  469. return view("store.deliveryAppointment.delivery",["k"=>request("k")]);
  470. }
  471. /**
  472. * 验证预约码
  473. *
  474. */
  475. public function checkAppointment()
  476. {
  477. if (!$this->check(request("k"),180))return ["status"=>406];
  478. $number = request("number");
  479. if (!$number)return ["status"=>417];
  480. $period = app("DeliveryAppointmentService")->getPeriod();
  481. if ($period===false)return ["status"=>416]; //非法时段扫码
  482. $car = DeliveryAppointmentCar::query()->whereNull("delivery_time")->where("status",0)
  483. ->where("appointment_number",$number)->whereHas("deliveryAppointment",function (Builder $query)use($period){
  484. $query->where("appointment_date",date("Y-m-d"))
  485. ->where("date_period",$period)->where("status",0);
  486. })->first();
  487. if (!$car)return ["status"=>417];
  488. $car->update(["delivery_time"=>date("Y-m-d H:i:s"),"status"=>1]);
  489. /** @var DeliveryAppointmentCar $car */
  490. event(new DeliveryAppointmentEvent($car));
  491. return ["status"=>200,"k"=>$car->delivery_appointment_id];
  492. }
  493. public function successMsg()
  494. {
  495. if (!request("k"))return view("exception.404");
  496. /** @var \stdClass $appointment */
  497. $appointment = DeliveryAppointment::query()->with(["cars"=>function($query){
  498. /** @var Builder $query */
  499. $query->whereNull("delivery_time");
  500. }])->find(request("k"));
  501. return view("store.deliveryAppointment.deliverySuccess",["cars"=>$appointment->cars]);
  502. }
  503. /**
  504. * 卸货完成
  505. */
  506. public function unloading()
  507. {
  508. if (!request("id"))$this->error("非法参数");
  509. /** @var DeliveryAppointmentCar|\stdClass $car */
  510. $car = DeliveryAppointmentCar::query()->find(request("id"));
  511. if (!$car || !$car->deliveryAppointment)$this->error("单据不存在");
  512. $car->update(["status"=>2]);
  513. app("DeliveryAppointmentService")->checkFull($car->delivery_appointment_id);
  514. event(new DeliveryAppointmentEvent($car));
  515. $this->success();
  516. }
  517. /**
  518. * 产能维护
  519. * */
  520. public function capacityMaintenance()
  521. {
  522. if(!Gate::allows('入库管理-入库预约-产能维护')){ return view("exception.authority"); }
  523. $warehouses = Warehouse::query()->select("name","id","production_capacity","reduced_production_capacity_coefficient")->get();
  524. return view("store.deliveryAppointment.capacityMaintenance",compact("warehouses"));
  525. }
  526. /**
  527. * 修改产能
  528. */
  529. public function updateCapacity()
  530. {
  531. $this->gate("入库管理-入库预约-产能维护");
  532. $id = request("id");
  533. $capacity = request("production_capacity");
  534. Warehouse::query()->where("id",$id)->update(["production_capacity"=>$capacity]);
  535. $this->success();
  536. }
  537. /**
  538. * 入库区终端界面
  539. */
  540. public function exhibition()
  541. {
  542. if(!Gate::allows('入库管理-入库预约-入库区终端')){ return view("exception.authority"); }
  543. if (!request("warehouse"))$warehouses = Warehouse::query()->select("name","id")->get();
  544. return view("store.deliveryAppointment.exhibition",["warehouses"=>$warehouses??[],"id"=>request("warehouse")]);
  545. }
  546. /**
  547. * 匹配入库单
  548. */
  549. public function verifyASN()
  550. {
  551. $this->gate("入库管理-入库预约-预约");
  552. $asn = request("asn");
  553. $owner = request("owner_id");
  554. if (!$asn || strlen($asn)<13)$this->error("非法ASN单号");
  555. $query = Store::query()->where("asn_code",$asn);
  556. if ($owner)$query->where("owner_id",$owner);
  557. $store = $query->with("storeItems")->first();
  558. if (!$store)$this->error("无此ASN单号");
  559. $this->success($store->storeItems);
  560. }
  561. /**
  562. * 标记质检单
  563. */
  564. public function qualityInspectionMark()
  565. {
  566. $this->gate("入库管理-入库预约-质检");
  567. $ids = request("ids");
  568. if (!$ids)$this->error("未选择任何记录");
  569. DeliveryAppointment::query()->whereIn("id",$ids)->update(["type_mark"=>0]);
  570. $this->success();
  571. }
  572. }