| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace App\Http\Controllers;
- use App\CarType;
- use App\CommodityBarcode;
- use App\Components\AsyncResponse;
- use App\DeliveryAppointment;
- use App\DeliveryAppointmentCar;
- use App\DeliveryAppointmentDetail;
- use App\Imports\AppointmentDetail;
- use App\Services\common\ExportService;
- use App\Services\common\QueryService;
- 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;
- class DeliveryAppointmentController extends Controller
- {
- use AsyncResponse;
- public function list()
- {
- if(!Gate::allows('入库管理-客户预约-预约管理')){ return view("exception.authority"); }
- $list = app("DeliveryAppointmentService")->query(request()->input())
- ->paginate(request("paginate") ?? 50);
- $warehouses = Warehouse::query()->select("id","name")->get();
- $owners = app("OwnerService")->getIntersectPermitting();
- return view("store.deliveryAppointment.list",compact("list","warehouses","owners"));
- }
- public function appointment()
- {
- if(!Gate::allows('入库管理-客户预约-预约')){ return view("exception.authority"); }
- $owners = app("OwnerService")->getIntersectPermitting();
- $cars = CarType::query()->get();
- $warehouses = Warehouse::query()->select("id","name")->get();
- return view("store.deliveryAppointment.appointment",compact("owners","cars","warehouses"));
- }
- 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::tomorrow();
- $end = Carbon::today()->addDays(7);
- $map = [];
- DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
- ->whereBetween("appointment_date",[$start->toDateString(),$end->toDateString()])
- ->where("status",0)
- ->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 = [];
- foreach (DeliveryAppointment::PERIOD as $key=>$period){
- $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
- $used = $map[$date."-".$key] ?? 0; //已使用产能
- $available = $total-$used; //可用产能
- if ($available < $need)$periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>false];
- else $periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>true];
- }
- $list[] = ["date"=>$date,"period"=>$periods];
- }
- $this->success($list);
- }
- /**
- * 确定预约
- */
- 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]);
- $errors = Validator::make($selectDate,[
- "date" => ["required","date","after:today"],
- "time" => ["required","integer"],
- ])->errors();
- if (count($errors)>0)$this->error("未选定预约日期");
- DB::transaction(function ()use($model,$selectDate,$details,&$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",$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" => $model["asn_number"] ?? 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"],
- ]);
- 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);
- //唯一码 随机数+十位+当前下标+个位+日期
- $number = $rand.$ten.$index.$one.date("d");
- $insert[] = [
- "delivery_appointment_id" => $appointment->id,
- "license_plate_number" => $car["license_plate_number"],
- "car_id" => $car["car_id"],
- "driver_name" => $car["driver_name"],
- "driver_phone" => $car["driver_phone"],
- "appointment_number" => $number,
- ];
- }
- DeliveryAppointmentCar::query()->insert($insert);
- });
- //md5加密在密文第五位后插入
- $md5 = substr_replace(md5(date("m-d")),$appointment->id,5,0);
- $this->success(["key"=>$md5]);
- }
- 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" => ["required","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("id")]);
- /** @var Builder $query */
- $list = $query->with(["owner","warehouse"])->get();
- $row = ["状态","货主","预约时间","仓库","预约号","车牌号","车型",
- "司机姓名","司机电话","吨","立方","箱数","采购单号","ASN单号","商品名称","条码","数量","创建时间"];
- foreach ($list as &$data){
- $appointment = "";
- $number = "";
- $carType = "";
- $driverName = "";
- $driverPhone = "";
- $commodityName = "";
- $commodityCode = "";
- $amount = "";
- foreach ($data->cars as $car){
- $appointment .= $car->appointment_number."\r\n";
- $number .= $car->license_plate_number."\r\n";
- $carType .= ($car->car->name ?? '')."\r\n";
- $driverName .= ($car->driver_name ?? '')."\r\n";
- $driverPhone .= ($car->driver_phone ?? '')."\r\n";
- }
- foreach ($data->details as $detail){
- $commodityName .= ($detail->commodity->name ?? $detail->name)."\r\n";
- $commodityCode .= ($detail->commodity->barcodes->code ?? $detail->bar_code)."\r\n";
- $amount .= $detail->amount."\r\n";
- }
- $data = [
- 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
- ];
- }
- return app(ExportService::class)->json($row,$list,"预约记录");
- }
- }
|