| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- <?php
- namespace App\Http\Controllers;
- use App\CarType;
- use App\Components\AsyncResponse;
- use App\DeliveryAppointment;
- use App\DeliveryAppointmentCar;
- use App\Events\DeliveryAppointmentEvent;
- use App\Imports\AppointmentDetail;
- use App\Jobs\DeliveryAppointmentCheck;
- use App\Logistic;
- use App\Store;
- use App\Warehouse;
- use Carbon\Carbon;
- use Carbon\CarbonPeriod;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Support\Facades\Validator;
- use Oursdreams\Export\Export;
- class DeliveryAppointmentController extends Controller
- {
- use AsyncResponse;
- public function list()
- {
- if(!Gate::allows('入库管理-入库预约-预约管理')){ return view("exception.authority"); }
- $list = app("DeliveryAppointmentService")->query(request()->input())
- ->with(["logistic",'warehouse'])
- ->orderByRaw("if(status=0,0,1),created_at DESC")
- ->paginate(request("paginate") ?? 50);
- $warehouses = Warehouse::query()->select("id","name")->get();
- $owners = app("OwnerService")->getIntersectPermitting();
- $params = request()->input();
- return view("store.deliveryAppointment.list",compact("list","warehouses","owners","params"));
- }
- public function appointment()
- {
- if(!Gate::allows('入库管理-入库预约-预约')){ return view("store.deliveryAppointment.index"); }
- $owners = app("OwnerService")->getIntersectPermitting();
- $cars = CarType::query()->get();
- $logistics = Logistic::query()->get();
- $warehouses = Warehouse::query()->select("id","name")->get();
- return view("store.deliveryAppointment.appointment",compact("owners","cars","warehouses","logistics"));
- }
- public function import()
- {
- $this->importExcel(new AppointmentDetail());
- }
- /**
- * 获取产能
- *
- */
- public function getCapacity()
- {
- $this->gate("入库管理-入库预约-预约");
- $model = request("model");
- $errors = $this->appointmentValidator($model)->errors();
- if (count($errors)>0)$this->success(["errors"=>$errors]);
- /** @var \stdClass $warehouse */
- $warehouse = Warehouse::query()->find($model["warehouse_id"]);
- $tonne = $model["tonne"] ?? 0;
- $cubicMeter = $model["cubic_meter"] ?? 0;
- $amount = request("detail_amount");
- $need = app("DeliveryAppointmentService")->calculateCapacity($tonne,$cubicMeter,$amount,$warehouse->reduced_production_capacity_coefficient);//所需产能
- $start = Carbon::today();
- if (request("postpone"))$start = $start->addDays(request("postpone"));
- $end = Carbon::today()->addDays(6);
- $map = [];
- DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
- ->whereBetween("appointment_date",[$start->toDateString(),$end->toDateString()])
- ->whereIn("status",[0,2])
- ->where("warehouse_id",$warehouse->id)
- ->groupBy(["appointment_date","date_period"])->get()
- ->each(function ($appointment)use(&$map){
- $map[$appointment->appointment_date."-".$appointment->date_period] = $appointment->capacity;
- });
- $list = [];
- $capacity = $warehouse->production_capacity;
- foreach (CarbonPeriod::create($start,$end) as $date){
- /** @var $date Carbon */
- $date = $date->format("Y-m-d");
- $periods = [];
- if ($date==date("Y-m-d")){
- $hour = date("H");
- foreach (DeliveryAppointment::PERIOD as $key=>$period){
- $period = explode("-",$period);
- $periodArr = ["time"=>$period[0].":00 - ".$period[1].":00","index"=>$key,"isAvailable"=>false];
- if ($hour<$period[1]-1){
- $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
- $used = $map[$date."-".$key] ?? 0; //已使用产能
- $available = $total-$used; //可用产能
- if ($available > $need)$periodArr["isAvailable"] = true;
- }
- $periods[] = $periodArr;
- }
- }else{
- foreach (DeliveryAppointment::PERIOD as $key=>$period){
- $period = explode("-",$period);
- $period = $period[0].":00 - ".$period[1].":00";
- $periodArr = ["time"=>$period,"index"=>$key,"isAvailable"=>false];
- $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
- $used = $map[$date."-".$key] ?? 0; //已使用产能
- $available = $total-$used; //可用产能
- if ($available > $need)$periodArr["isAvailable"] = true;
- $periods[] = $periodArr;
- }
- }
- $list[] = ["date"=>$date,"period"=>$periods];
- }
- $this->success($list);
- }
- private function checkAndGetAsn($codes){
- if ($codes){
- $codes = array_filter(array_unique(preg_split('/[,, ]+/u', $codes)));
- if (count($codes)>0){
- $asnCount = Store::query()->where("asn_code",$codes)->count();
- $codes = count($codes)===$asnCount ? implode(",",$codes) : null;
- }else{
- $codes = null;
- }
- }
- return $codes;
- }
- /**
- * 确定预约
- */
- public function submitAppointment()
- {
- $this->gate("入库管理-入库预约-预约");
- $model = request("model");
- $selectDate = request("date");
- $details = request("details");
- $errors = $this->appointmentValidator($model)->errors();
- if (count($errors)>0)$this->success(["errors"=>$errors]);
- $asnCodes = $this->checkAndGetAsn($model["asn_number"] ?? null);
- if (!$asnCodes)$this->error("ASN单据错误");
- $errors = Validator::make($selectDate,[
- "date" => ["required","date","after_or_equal:today"],
- "time" => ["required","integer"],
- ])->errors();
- if (count($errors)>0)$this->error("未选定预约日期");
- DB::transaction(function ()use($model,$selectDate,$details,&$appointment,$asnCodes){
- $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
- ->where("appointment_date",$selectDate["date"])
- ->where("date_period",$selectDate["time"])
- ->where("warehouse_id",$model["warehouse_id"])
- ->where("status",0)
- ->groupBy(["appointment_date","date_period"])
- ->lockForUpdate()->first();
- /** @var \stdClass $warehouse */
- $warehouse = Warehouse::query()->find($model["warehouse_id"]);
- $need = app("DeliveryAppointmentService")->
- calculateCapacity($model["tonne"] ?? 0,$model["cubic_meter"] ?? 0,count($details),
- $warehouse->reduced_production_capacity_coefficient);
- if ($result){
- $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
- $available = $total-$result->capacity;
- if ($available < $need)$this->success(["isFail"=>true]);
- }
- /** @var \stdClass $appointment */
- $appointment = DeliveryAppointment::query()->create([
- "user_id" => Auth::id(),
- "owner_id" => $model["owner_id"],
- "procurement_number" => $model["procurement_number"] ?? null,
- "asn_number" => $asnCodes,
- "logistic_number" => $model["logistic_number"] ?? null,
- "logistic_id" => $model["logistic_id"] ?? null,
- "warehouse_id" => $model["warehouse_id"],
- "tonne" => $model["tonne"] ?? 0,
- "cubic_meter" => $model["cubic_meter"] ?? 0,
- "box_amount" => $model["box_amount"] ?? 0,
- "capacity" => $need,
- "appointment_date" => $selectDate["date"],
- "date_period" => $selectDate["time"],
- "remark" => $model["remark"] ?? null,
- ]);
- if ($details)app("DeliveryAppointmentService")->insertDetails($appointment,$details);
- $insert = [];
- foreach ($model["cars"] as $index=>$car){
- $rand = mt_rand(0,9);
- $len = strlen($appointment->id);
- $ten = $len < 2 ? "0" : substr($appointment->id,$len-2,1);
- $one = substr($appointment->id,$len-1,1);
- //唯一码 仓库CODE+随机数+十位+当前下标+个位+日期
- $number = $warehouse->code.$rand.$ten.$index.$one.date("d");
- $insert[] = [
- "delivery_appointment_id" => $appointment->id,
- "license_plate_number" => $car["license_plate_number"],
- "car_id" => $car["car_id"] ?? null,
- "driver_name" => $car["driver_name"] ?? null,
- "driver_phone" => $car["driver_phone"] ?? null,
- "appointment_number" => $number,
- ];
- }
- DeliveryAppointmentCar::query()->insert($insert);
- });
- dispatch(new DeliveryAppointmentCheck($appointment->id))->delay(Carbon::parse($appointment->appointment_date." ".(explode("-",DeliveryAppointment::PERIOD[$appointment->date_period])[1]).":00:01"));
- // x当日或次日预约单广播 √大于当前时间的都广播
- if (strtotime($appointment->appointment_date." 00:00:00")>strtotime(date('Y-m-d'))){
- $appointment->load("cars");
- event(new DeliveryAppointmentEvent($appointment->cars[0]));
- }
- //md5加密在密文第五位后插入
- $md5 = substr_replace(md5(date("m-d")),$appointment->id,5,0);
- $this->success(["key"=>$md5]);
- }
- /**
- * 修改预约
- */
- public function updateAppointment()
- {
- $this->gate("入库管理-入库预约-预约");
- $id = request("id");
- if (!$id)$this->error("非法参数");
- $selectDate = request("date");
- $errors = Validator::make($selectDate,[
- "date" => ["required","date","after_or_equal:today"],
- "time" => ["required","integer"],
- ])->errors();
- if (count($errors)>0)$this->error("未选定预约日期");
- /** @var DeliveryAppointment|\stdClass $appointment */
- $appointment = DeliveryAppointment::query()->whereIn("status",[0,3])->with("cars")->find($id);
- if (!$appointment)$this->error("预约单不存在");
- foreach ($appointment->cars as $car){
- if ($car->status!=0)$this->error("车辆已达,无法修改预约");
- }
- DB::transaction(function ()use($id,$selectDate,&$appointment){
- $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
- ->where("appointment_date",$selectDate["date"])
- ->where("date_period",$selectDate["time"])
- ->where("warehouse_id",$appointment->warehouse_id)
- ->where("status",0)
- ->groupBy(["appointment_date","date_period"])
- ->lockForUpdate()->first();
- /** @var \stdClass $warehouse */
- $warehouse = Warehouse::query()->find($appointment->warehouse_id);
- if ($result){
- $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
- $available = $total-$result->capacity;
- if ($available < $appointment->capacity)$this->success(["isFail"=>true]);
- }
- $update = [
- "appointment_date" => $selectDate["date"],
- "date_period" => $selectDate["time"],
- ];
- if ($appointment->status==3)$update["status"] = 0;
- $appointment->update($update);
- });
- dispatch(new DeliveryAppointmentCheck($appointment->id))->delay(Carbon::parse($selectDate["date"]." ".(explode("-",DeliveryAppointment::PERIOD[$selectDate["time"]])[1]).":00:01"));
- //当日或次日预约单广播
- $old = $appointment->appointment_date == date('Y-m-d') ? 0 : ($appointment->appointment_date == date('Y-m-d',strtotime('+1 day') ? 1 : 2));
- $new = $selectDate["date"] == date('Y-m-d') ? 0 : ($selectDate["date"] == date('Y-m-d',strtotime('+1 day') ? 1 : 2));
- //if ($old==2 && $new==2)$this->success(); //超过广播区间不推送
- if (($old-$new)!=0 || $appointment->date_period!=$selectDate["time"]){
- $appointment->cars[0]->change = true;
- $appointment->cars[0]->old = $old;
- $appointment->cars[0]->new = $new;
- event(new DeliveryAppointmentEvent($appointment->cars[0]));
- }
- $this->success();
- }
- private function appointmentValidator(array $model)
- {
- return Validator::make($model,[
- "owner_id" => ["required","integer"],
- "warehouse_id" => ["required","integer"],
- "tonne" => ["required_without:cubic_meter","numeric"],
- "cubic_meter" => ["required_without:tonne","numeric"],
- "box_amount" => ["nullable","integer"],
- "cars.*.license_plate_number" => ["nullable","size:7"],
- "cars.*.car_id" => ["nullable","integer"],
- "cars.*.driver_phone" => ["nullable"],
- "cars.*.driver_name" => ["nullable"],
- ],[
- 'required'=>':attribute 不应为空',
- 'integer'=>':attribute 应为数值',
- 'required_without'=>':attribute 不应为空',
- 'numeric'=>':attribute 必须为数字',
- 'size'=>':attribute 非法',
- ],[
- 'owner_id'=>'货主',
- 'warehouse_id'=>'仓库',
- 'tonne'=>'吨',
- 'cubic_meter'=>'立方',
- 'cars.*.license_plate_number'=>'车牌号',
- 'cars.*.car_id'=>'车型',
- 'cars.*.driver_phone'=>'司机电话',
- 'cars.*.driver_name'=>'司机姓名',
- ]);
- }
- /**
- * 根据key取id 鉴权数据
- */
- public function showAppointmentInfo()
- {
- if(!Gate::allows('入库管理-入库预约-预约')){ return view("exception.authority"); }
- $key = request("k");
- $len = strlen($key);
- $id = substr($key,5,$len-32);
- $md5 = substr($key,0,5).substr($key,5+$len-32);
- if ($md5!==md5(date("m-d")))return view("exception.404");
- /** @var \stdClass $appointment */
- $appointment = DeliveryAppointment::query()->with("cars")->find($id);
- if (!$appointment || $appointment->user_id != Auth::id())return view("exception.404");
- return view("store.deliveryAppointment.success",compact("appointment"));
- }
- /**
- * 取消预约
- */
- public function cancel()
- {
- $this->gate("入库管理-入库预约-预约");
- $id = request("id");
- if (!$id)$this->error("非法参数");
- DeliveryAppointment::query()->where("status",0)->where("id",$id)->update(["status"=>1]);
- $this->success(1);
- }
- /**
- * 导出
- */
- public function export()
- {
- if(!Gate::allows('入库管理-入库预约-预约管理')){ return view("exception.authority"); }
- if (request("checkAllSign")){
- $params = request()->input();
- unset($params["checkAllSign"]);
- $query = app("DeliveryAppointmentService")->query($params);
- }else $query = app("DeliveryAppointmentService")->query(["id"=>request("data")]);
- /** @var Builder $query */
- $deliveries = $query->with(["owner","warehouse"])->get();
- $row = ["状态","货主","预约时间","仓库","预约号","车牌号","车型",
- "司机姓名","司机电话","吨","立方","箱数","采购单号","ASN单号","商品名称","条码","数量","创建时间"];
- $list = [];
- foreach ($deliveries as $data){
- $appointment = "";
- $number = "";
- $carType = "";
- $driverName = "";
- $driverPhone = "";
- $commodityName = "";
- $commodityCode = "";
- $amount = "";
- foreach ($data->cars as $car){
- if ($car->appointment_number)$appointment .= $car->appointment_number."\r\n";
- if ($car->license_plate_number)$number .= $car->license_plate_number."\r\n";
- if ($car->car)$carType .= ($car->car->name ?? '')."\r\n";
- if ($car->driver_name)$driverName .= ($car->driver_name ?? '')."\r\n";
- if ($car->driver_phone)$driverPhone .= ($car->driver_phone ?? '')."\r\n";
- }
- foreach ($data->details as $detail){
- $commodityName .= ($detail->commodity->name ?? $detail->name)."\r\n";
- $commodityCode .= ($detail->commodity->barcodes[0]->code ?? $detail->bar_code)."\r\n";
- $amount .= $detail->amount."\r\n";
- }
- $list[] = [
- DeliveryAppointment::STATUS[$data->status],
- $data->owner->name ?? '',
- $data->appointment_date,
- $data->warehouse->name ?? '',
- $appointment,
- $number,
- $carType,
- $driverName,
- $driverPhone,
- $data->tonne,
- $data->cubic_meter,
- $data->box_amount,
- $data->procurement_number,
- $data->asn_number,
- $commodityName,
- $commodityCode,
- $amount,
- $data->created_at->toDateTimeString()
- ];
- }
- return Export::make($row,$list,"预约记录",true);
- }
- private function carList($period,$date,$warehouse)
- {
- $list = [];
- DeliveryAppointmentCar::query()->with(["deliveryAppointment"=>function($query){
- /** @var Builder $query */
- $query->withCount("cars")->with("owner");
- }])->whereHas("deliveryAppointment",function ($query)use($period,$warehouse,$date){
- /** @var Builder $query */
- if ($date){
- $query->where("appointment_date",$date);
- }else{
- $query->where("appointment_date",">",date("Y-m-d"));
- }
- $query->where("warehouse_id",$warehouse)->whereIn("status",[0,2]);
- })->where(function ($query)use($period){
- /** @var Builder $query */
- $query->where("status",1)->orWhereHas("deliveryAppointment",function (Builder $query)use($period){
- $query->where("date_period",">=",$period);
- });
- })->orderByRaw("(CASE WHEN status=0 THEN 2 WHEN status=2 THEN 3 END),IF(ISNULL(delivery_time),1,0),delivery_time")
- ->limit(10)->get()->each(function ($car)use(&$list){
- //$diff = $car->delivery_time ? (strtotime($car->delivery_time)+1799)-time() : 0;
- $count = $car->deliveryAppointment->cars_count ?? 0;
- $owner = $car->deliveryAppointment->owner->name ?? "";
- $len = mb_strlen($owner);
- $ownerName = "";
- for($i=0;$i<$len-1;$i++)$ownerName .= "*";
- $ownerName .= mb_substr($owner,$len-1,1);
- $list[] = [
- "id" => $car->id,
- "license_plate_number" => $car->license_plate_number ? $car->license_plate_number : substr($car->appointment_number,0,5)."****".substr($car->appointment_number,9,1),
- "driver_name" => $car->driver_name,
- "driver_phone" => $car->driver_phone,
- "status" => DeliveryAppointmentCar::STATUS[$car->status],
- "cubic_meter" => isset($car->deliveryAppointment->cubic_meter) && $car->deliveryAppointment->cubic_meter>0 ? ($count>1 ? $car->deliveryAppointment->cubic_meter."/".$count : $car->deliveryAppointment->cubic_meter) : "",
- "tonne" => isset($car->deliveryAppointment->tonne) && $car->deliveryAppointment->tonne>0 ? ($count>1 ? $car->deliveryAppointment->tonne."/".$count : $car->deliveryAppointment->tonne) : "",
- //"diff" => $diff>0 ? $diff*1000 : 0,
- "owner_name" => $ownerName,
- "type" => DeliveryAppointment::TYPE[$car->deliveryAppointment->type_mark] ?? '',
- "period"=>isset($car->deliveryAppointment->date_period) ? ($car->deliveryAppointment->date_period==0 ? '上午' : '下午') : '',
- "delivery_time" => $car->delivery_time ? substr($car->delivery_time,11,5) : '',
- ];
- });
- return $list;
- }
- /**
- * 获取展览数据
- */
- public function getExhibitionList()
- {
- $this->gate("入库管理-入库预约-入库区终端");
- $hour = date("H");
- $warehouse = request("warehouse");
- $index = null;
- foreach (DeliveryAppointment::PERIOD as $key=>$period){
- $arr = explode("-",$period);
- if (count($arr)!=2)continue;
- if ($hour<$arr[1]){
- $index = $key;
- break;
- }
- }
- if ($index===null)$this->success();
- $list = $this->carList($index,date("Y-m-d"),$warehouse);
- $counts = DeliveryAppointmentCar::query()->whereHas("deliveryAppointment",function (Builder $query)use($index,$warehouse){
- $query->where("appointment_date",date("Y-m-d"))
- ->where("warehouse_id",$warehouse)->whereIn("status",[0,2]);
- })->selectRaw("status, COUNT(1) AS c")->groupByRaw("status")->get();
- $success = 0;
- $work = 0;
- $notReached = 0;
- if ($counts)foreach ($counts as $c){
- switch ($c->status){
- case 0:
- $notReached = $c->c;
- break;
- case 1:
- $work = $c->c;
- break;
- case 2:
- $success = $c->c;
- break;
- }
- }
- $result = ["list"=>$list,"success"=>$success,"work"=>$work,"notReached"=>$notReached,"nextDay"=>$this->carList(0,/*date("Y-m-d",strtotime("+1 day"))*/null,$warehouse)];
- $nextTime = DeliveryAppointment::PERIOD[$index+1] ?? null;
- if ($nextTime){
- $nextTime = explode("-",$nextTime)[0];
- $timestamp = strtotime(date("Y-m-d")." ".$nextTime.":00:00");
- $result["refresh"] = (($timestamp-time())*1000) ?? 1000;
- }
- $this->success($result);
- }
- public function getKey()
- {
- $this->success(app("DeliveryAppointmentService")->getKey());
- }
- /**
- * 错误信息
- *
- * @return string
- */
- public function errMsg()
- {
- return <<<html
- <div style='font-weight: bold;color: red;margin: 500px auto;text-align: center;'>
- <span style='font-size: 200px;'>×</span><br>
- <span style='font-size: 50px'>二维码已过期,请重新扫描!</span><br><h1 style="color: #1b1e21;">如果多次扫描失败,请联系管理人员检查!</h1>
- </div>"
- html;
- }
- /**
- * 检查key有效性
- * 21-4-2 切换永久二维码 取消时段校验
- *
- * @param string $key
- * @param int $offset
- *
- * @return bool
- */
- private function check($key,$offset):bool
- {
- if (!$key)return false;
- $key = base64_decode($key);
- $ch = app("DeliveryAppointmentService")->getKey();
- $len = strlen($ch);
- if (substr($key,0,$len)!=$ch)return false;
- /*$timeLen = strlen($key)-$len;
- $time = substr($key,$len)+$offset;
- $thisTime = (integer)substr(time(),$timeLen*-1);
- if ($thisTime>$time)return false;*/
- return true;
- }
- /**
- * 进入预约界面填写预约码
- */
- public function delivery()
- {
- if (!$this->check(request("k"),65))return $this->errMsg();
- return view("store.deliveryAppointment.delivery",["k"=>request("k")]);
- }
- /**
- * 验证预约码 允许当日产能满足后的逾期
- *
- */
- public function checkAppointment()
- {
- if (!$this->check(request("k"),180))return ["status"=>406];
- $number = request("number");
- if (!$number)return ["status"=>417];
- $period = app("DeliveryAppointmentService")->getPeriod();
- if ($period===false)return ["status"=>416]; //非法时段扫码
- $car = DeliveryAppointmentCar::query()->with("deliveryAppointment")->whereNull("delivery_time")->where("status",0)
- ->where("appointment_number",$number)->whereHas("deliveryAppointment",function (Builder $query)use($period){
- $query->whereIn("status",[0,3]);
- })->first();
- if (!$car || !$car->deliveryAppointment)return ["status"=>417];
- $car->update(["delivery_time"=>date("Y-m-d H:i:s"),"status"=>1]);
- if (Carbon::now()->gt(Carbon::parse($car->deliveryAppointment->appointment_date." ".explode("-",DeliveryAppointment::PERIOD[$car->deliveryAppointment->date_period])[1].":00:00"))){
- $available = app("DeliveryAppointmentService")->getAvailableCapacity(date("Y-m-d"),$period,$car->deliveryAppointment->warehouse_id);
- if ($available<$car->deliveryAppointment->capacity)return ["status"=>417];
- DeliveryAppointment::query()->where("id",$car->delivery_appointment_id)->update(["status" => 0]);
- }
- event(new DeliveryAppointmentEvent($car));
- return ["status"=>200,"k"=>$car->delivery_appointment_id];
- }
- public function successMsg()
- {
- if (!request("k"))return view("exception.404");
- /** @var \stdClass $appointment */
- $appointment = DeliveryAppointment::query()->with(["cars"=>function($query){
- /** @var Builder $query */
- $query->whereNull("delivery_time");
- }])->find(request("k"));
- return view("store.deliveryAppointment.deliverySuccess",["cars"=>$appointment->cars]);
- }
- /**
- * 卸货完成
- */
- public function unloading()
- {
- $this->gate("入库管理-入库预约-预约管理-卸货完成");
- if (!request("id"))$this->error("非法参数");
- /** @var DeliveryAppointmentCar|\stdClass $car */
- $car = DeliveryAppointmentCar::query()->with("deliveryAppointment")->where("status",1)->find(request("id"));
- if (!$car || !$car->deliveryAppointment)$this->error("单据不存在");
- if (!$car->delivery_time)$this->error("尚未签到");
- if (mb_substr($car->delivery_time,0,10)!=date('Y-m-d'))$this->error("禁止越天逾期操作");
- $car->update(["status"=>2,"unloaded_at"=>date("Y-m-d H:i:s")]);
- $status = app("DeliveryAppointmentService")->checkFull($car->delivery_appointment_id);
- event(new DeliveryAppointmentEvent($car));
- $this->success($status);
- }
- /**
- * 管理员代替签到
- */
- public function signIn()
- {
- $this->gate("入库管理-入库预约-预约管理-签到");
- if (!request("id"))$this->error("非法参数");
- $period = app("DeliveryAppointmentService")->getPeriod();
- if ($period===false)$this->error("当前非工作时间,请在指定时间内送货");
- /** @var DeliveryAppointmentCar|\stdClass $car */
- $car = DeliveryAppointmentCar::query()->whereHas("deliveryAppointment",function (Builder $query){
- $query->whereIn("status",[0,3]);
- })->find(request("id"));
- if (!$car || !$car->deliveryAppointment)$this->error("单据不存在");
- $car->update(["status"=>1,"delivery_time"=>date("Y-m-d H:i:s")]);
- if (Carbon::now()->gt(Carbon::parse($car->deliveryAppointment->appointment_date." ".explode("-",DeliveryAppointment::PERIOD[$car->deliveryAppointment->date_period])[1].":00:00"))){
- $available = app("DeliveryAppointmentService")->getAvailableCapacity(date("Y-m-d"),$period,$car->deliveryAppointment->warehouse_id);
- if ($available<$car->deliveryAppointment->capacity)$this->error("仓库产能不足,请另择日期送货");
- DeliveryAppointment::query()->where("id",$car->delivery_appointment_id)->update(["status" => 0]);
- }
- event(new DeliveryAppointmentEvent($car));
- $this->success();
- }
- /**
- * 产能维护
- * */
- public function capacityMaintenance()
- {
- if(!Gate::allows('入库管理-入库预约-产能维护')){ return view("exception.authority"); }
- $warehouses = Warehouse::query()->select("name","id","production_capacity","reduced_production_capacity_coefficient")->get();
- return view("store.deliveryAppointment.capacityMaintenance",compact("warehouses"));
- }
- /**
- * 修改产能
- */
- public function updateCapacity()
- {
- $this->gate("入库管理-入库预约-产能维护");
- $id = request("id");
- $capacity = request("production_capacity");
- Warehouse::query()->where("id",$id)->update(["production_capacity"=>$capacity]);
- $this->success();
- }
- /**
- * 入库区终端界面
- */
- public function exhibition()
- {
- if(!Gate::allows('入库管理-入库预约-入库区终端')){ return view("exception.authority"); }
- if (!request("warehouse"))$warehouses = Warehouse::query()->select("name","id")->get();
- return view("store.deliveryAppointment.exhibition",["warehouses"=>$warehouses??[],"id"=>request("warehouse")]);
- }
- /**
- * 匹配入库单
- */
- public function verifyASN()
- {
- $this->gate("入库管理-入库预约-预约");
- $asn = preg_split('/[,, ]+/is', request("asn"));
- $owner = request("owner_id");
- if (!$asn || strlen(request("asn"))<13)$this->error("非法ASN单号");
- $query = Store::query()->whereIn("asn_code",$asn);
- if ($owner)$query->where("owner_id",$owner);
- $stores = $query->with("storeItems")->get();
- if (!$stores)$this->error("无此ASN单号");
- $items = [];
- $stores->each(function ($store)use(&$items){
- if ($store->storeItems)$items = array_merge($items,$store->storeItems->toArray());
- });
- $this->success($items);
- }
- /**
- * 标记质检单
- */
- public function qualityInspectionMark()
- {
- $this->gate("入库管理-入库预约-质检");
- $ids = request("ids");
- if (!$ids)$this->error("未选择任何记录");
- DeliveryAppointment::query()->whereIn("id",$ids)->update(["type_mark"=>0]);
- $this->success();
- }
- }
|