error("停用缓存架上架,联系管理员开启"); $this->gate("入库管理-入库-缓存架入库"); $asn = \request("asn"); $ide = \request("ide"); $barCode = \request("barCode"); $amount = (int)\request("amount"); //check info if (!$asn || !$ide || !$barCode || !$amount)$this->error("信息不完整"); $box = MaterialBox::query()->with("station")->where("code",$ide)->first(); if (!$box)$this->error("WAS无此料箱"); if (!$box->station)$this->error("WAS无此库位信息"); if (!app("StationService")->isCacheShelfLocation($box->station))$this->error("非缓存架库位"); $fromLocation = $box->station->code; //源库位 $item = StoreItem::query()->whereHas("store",function (Builder $query){ $query->where("asn_code",request("asn")); })->whereHas("commodity",function (Builder $query)use($barCode){ $query->whereHas("barcodes",function (Builder $query)use($barCode){ $query->where("code",$barCode); }); })->first(); if (!$item)$this->error("WAS无此ASN单信息"); //get flux $tasks = app("StorageService")->getFluxTask($asn,$barCode,$amount); if (!$tasks)$this->error("该单无上架任务或不存在"); //此处嵌套三层事务 以最高层级为准 DB::beginTransaction(); //总体事务 回滚WAS错误操作 try{ //库存记录 if (!app("StorageService")->enterWarehouse( $box->id, $item->commodity_id, $amount, $box->material_box_model_id))$this->error("库存异常"); DB::connection("oracle")->transaction(function ()use($tasks,$ide){ //单体嵌套事务 回滚FLUX失败任务 foreach ($tasks as $task)if (!app("StorageService")->fluxPA($task,$ide)){ DB::connection("oracle")->rollBack(); $this->error("FLUX上架失败"); }; }); DB::commit(); }catch (\Exception $e){ DB::rollBack(); $this->error($e->getMessage()); } //亮灯 app("CacheShelfService")->lightUp($fromLocation,'1','0',["detail01"=>$ide,"qty01"=>$amount]); $maximum = app("CommodityMaterialBoxModelService")->getMaximum($box->material_box_model_id,$item->commodity_id); $this->success(["model" => $box->material_box_model_id,"commodity" => $item->commodity_id, "maximum" => $maximum]); } public function setMaximum() { app("CommodityMaterialBoxModelService")->setMaximum(request("model"),request("commodity"),request("maximum")); $this->success(); } /** * 检查最大限值并返回 * */ public function checkMaximum() { $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode")); if (!$item)$this->error("无此单据记录"); $models = CommodityMaterialBoxModel::query()->where("commodity_id",$item->commodity_id)->get(); if ($models->count()==0)$this->success(["material_box_model_id"=>null]); $map = []; foreach ($models as $model)$map[$model->material_box_model_id] = $model; $this->getMaterBoxModel($item,$map); } /** * 检查ASN可上架总数 */ public function checkAsnAmount() { $sql = <<selectOne(DB::raw($sql),[request("asn"),request("barCode"),request("barCode"),request("barCode")]); $this->success($task->amount ?? 0); } /** * 重置缓存架指定格口 */ public function resetCacheShelf() { $this->gate("入库管理-入库-缓存架入库"); $boxes = request("boxes"); //清理任务 $data = ''; //剔除准备执行任务但没有真正开始执行的待定库位 $occupy = Station::query()->select("id")->whereIn("code",$boxes)->where("status",1)->get(); foreach ($occupy as $item){ unset($boxes[array_search($item->code,$boxes)]); $data .= '“'.$item->code.'”,'; } if ($occupy->count()>0){ $data .= "存在任务待处理,无法调取 "; $boxes = array_values($boxes); } //剔除存在任务的库位 $tasks = StationTaskMaterialBox::query()->with("station:id,code") ->whereIn("station_id",Station::query()->select("id")->whereIn("code",$boxes)) ->whereNotIn("status",["完成","取消"])->get(); foreach ($tasks as $task){ unset($boxes[array_search($task->station->code,$boxes)]); $data .= '“'.$task->station->code.'”,'; }; if ($tasks->count()>0){ $data .= "任务排队中,无法调取"; $boxes = array_values($boxes); } //重新调取料箱 $result = app("ForeignHaiRoboticsService")->paddingCacheShelf(Station::query()->whereIn("code",$boxes)->get()); if ($result===null)$this->error("任务下发错误,检查日志"); if ($result===false)$this->error("已无可用料箱,部分库位填充失败"); $this->success(["data"=>$data,"boxes"=>$boxes]); } /** * 取得料箱 */ public function acquireBox() { $this->gate("入库管理-入库-半箱补货入库"); $boxId = request("material_box_id"); $modelId = request("material_box_model_id"); if (!$modelId)$this->error("商品未设定库存上限"); //获取目标库位 $station = app("StationService")->getMirrorMappingLocation(request("station")); if (!$station)$this->error("未知库位"); $isAvailable = app("StationService")->isAvailable($station); if (!$isAvailable)$this->error("库存被占用或存在任务未处理"); $amount = app("StorageService")->checkPutAmount(request("asn"),request("barCode")); if ($amounterror("待上架数量不足,最大可上数量为{$amount}"); //发起取箱任务 $exe = function ($boxId)use($station){ DB::beginTransaction(); $collection = new Collection(); $task = StationTask::query()->create([ 'status' => "待处理", 'station_id' => $station->id, ]); $collection->add(StationTaskMaterialBox::query()->create([ 'station_id' => $station->id, 'material_box_id'=>$boxId, 'status'=>"待处理", 'type' => '取', 'station_task_id' => $task->id, ])); if (!app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','立架出至缓存架'))$this->error("呼叫机器人失败"); //生成临时任务事务 TaskTransaction::query()->create([ "doc_code" => request("asn"), "bar_code" => request("barCode"), "fm_station_id" => $station->id, "material_box_id" => $boxId, "commodity_id" => request("commodity_id"), "amount" => request("amount"), "type" => "入库", "user_id" => Auth::id(), "mark" => 1 ]); DB::commit(); //亮灯 app("CacheShelfService")->lightUp($station->code,'2','1'); app("StationService")->locationOccupy($station->code);//占用库位 }; if ($boxId && app("MaterialBoxService")->checkUsableBox($boxId))$this->success($exe($boxId)); $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode")); if (!$item)$this->error("无此单据记录"); $models = app("MaterialBoxModelService")->getModelSortedByOwner($item->store->owner_id); $models->load(["commodity"=>function($query)use($item){ $query->where("commodity_id",$item->commodity_id); }]); //获取料箱 if ($boxId){ $blacklist = [$boxId]; $boxId = null; //料箱存在且不可用 /** @var \Illuminate\Database\Eloquent\Collection $models */ foreach ($models as $model){ if (!$model->commodity)continue; //料箱不可用寻找新料箱 $result = app("StorageService")->getHalfBoxLocation($model->commodity,$item,request("asn"),$blacklist); while ($result){ //料箱可用并且数量符合本次半箱数量 跳出 if (app("MaterialBoxService")->checkUsableBox($result->material_box_id) && $model->maximum-$result->amount>=request("amount"))$this->success($exe($result->material_box_id)); else $blacklist[] = $result->material_box_id; //否则黑名单此料箱继续查找 直至料箱为空 $result = app("StorageService")->getHalfBoxLocation($model->commodity,$item,request("asn"),$blacklist); } } } //料箱不存在且该商品有过入库记录 拿取空箱 if (!$modelId){ $box = null; foreach ($models as $model){ if (!$model->commodity)continue; $box = app("MaterialBoxService")->getAnEmptyBox($model); if($box)break; } }else $box = app("MaterialBoxService")->getAnEmptyBox(MaterialBoxModel::query()->find($modelId)); if (!$box)$this->error("无可用料箱"); $this->success($exe($box->id)); } /** * 溢出校正 */ public function overflowRevision() { $this->gate("入库管理-入库-半箱补货入库"); $station = request("station"); $amount = request("amount"); //获取目标库位 $station = app("StationService")->getMirrorMappingLocation($station); if (!$station)$this->error("未知库位"); $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)->where("amount",">",$amount) ->where("type","入库")->where("mark",1)->where("status",0)->first(); if (!$task)$this->error("无任务存在"); $task->update(["amount" => DB::raw("amount - {$amount}")]); $storage = MaterialBoxCommodity::query()->where("material_box_id",$task->material_box_id) ->where("commodity_id",$task->commodity_id)->first(); $maximum = (($storage->amount ?? 0)+$task->amount)-$amount; CommodityMaterialBoxModel::query()->where("material_box_model_id",$task->materialBox->material_box_model_id) ->where("commodity_id",$task->commodity_id)->update(["maximum"=>$maximum]); $this->success("校正成功"); } public function syncStorage() { ini_set('max_execution_time', 0); $model = MaterialBoxModel::query()->create([ "code" => "common" ]); $sql = <<select(DB::raw($sql)) as $inv){ $materialBox = MaterialBox::query()->firstOrCreate(["code"=>$inv->locationid],[ "code" => $inv->locationid, "material_box_model_id"=>$model ]); $owner = Owner::query()->firstOrCreate([ "code" => $inv->customerid ],[ "code" => $inv->customerid, "name" => $inv->customerid, ]); $commodity = Commodity::query()->where("owner_id",$owner->id)->where("sku",$inv->sku)->first(); $s = MaterialBoxCommodity::query()->where("material_box_id",$materialBox->id) ->where("commodity_id",$commodity->id)->first(); if (!$s)MaterialBoxCommodity::query()->create([ "material_box_id" => $materialBox->id, "commodity_id" => $commodity->id, "amount" => $inv->qty, ]);else $s->update(["amount" => $inv->qty]); } DB::commit(); $this->success(); } /** * 在安卓端的个体登录鉴权 * */ public function androidLogin() { $errors=Validator::make(request()->input(),[ "name" => 'required|string', 'password' => 'required|string'])->errors(); if($errors->count()>0){return ['success'=>false,'errors'=>$errors];} request()->offsetSet("remember",true); if (!$this->attemptLogin(request()))return ['success'=>false,'errors'=>['name'=>['登录信息验证失败']]]; if (!Gate::allows("入库管理-入库-缓存架入库") && !Gate::allows("入库管理-入库-半箱补货入库"))return ['success'=>false,'errors'=>['name'=>['用户无权操作入库']]]; return ['success'=>true,'url'=>url("store/inStorage/android.index")]; } public function username(): string { return 'name'; } /** * 库外箱绑定至库位 */ public function bindBox() { $location = request("location"); $box = request("ide"); if (!$location || !$box)$this->error("参数传递错误"); $task = StationTaskMaterialBox::query()->select("id")->where(function ($query)use($location,$box){ /** @var Builder $query */ $query->whereHas("station",function ($query)use($location){ /** @var Builder $query */ $query->where("code",$location); })->orWhereHas("materialBox",function ($query)use($box){ /** @var Builder $query */ $query->where("code",$box); }); })->whereNotIn("status",["完成","取消"])->first(); if ($task)$this->error("库位或料箱存在任务待执行,无法放置料箱"); $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select(DB::raw("1")) ->where("ks_bin_code",$box)->where("status",4)->first(); if (!$ks)$this->error("海柔料箱状态异常"); DB::beginTransaction(); try { $station = Station::query()->where("code",$location)->where("status",0)->first(); $box = MaterialBox::query()->where("code",$box)->first(); if (!$station || !$box)$this->error("库位或料箱未在WAS记录"); $station->update(["material_box_id"=>$box->id]); DB::commit(); $this->success(); }catch (\Exception $e){ DB::rollBack(); $this->error($e->getMessage()); } } public function bindModelIndex(){ $models = MaterialBoxModel::query()->get(); return view("store.inStorage.boxBindModel",compact("models")); } public function searchIde() { $ide = request("ide"); $box = MaterialBox::query()->where("code",$ide)->first(); if (!$box)$this->error("料箱不存在"); $this->success($box->material_box_model_id); } public function boxBindModel() { $this->success(MaterialBox::query()->where("code",request("ide")) ->update(["material_box_model_id"=>request("material_box_model_id")])); } public function commodityBindModel() { $models = MaterialBoxModel::query()->get(); return view("store.inStorage.commodityBindModel",compact("models")); } public function getModels() { $this->success(MaterialBoxModel::query()->select("id","code","description","maximum_kind")->get()); } /** * 设定料箱最大 */ public function settingModelMaximum() { $insert = function ($commodityId){ $in = []; $map = []; $ids = []; foreach (request("models") as $model){ $insert[] = [ "commodity_id" => $commodityId, "material_box_model_id" => $model["id"], "maximum" => $model["maximum"] ]; $map[$model["id"]] = $model; $ids[] = $model["id"]; } return array($in,$map,$ids); }; if (request("commodityId")){ list($in,$map,$ids) = $insert(request("commodityId")); if ($ids){ $map = array_flip($ids); foreach (CommodityMaterialBoxModel::query()->whereIn("id",$ids)->lockForUpdate()->get() as $model){ $index = $map[$model->id]; if ($model->maximum != $in[$index]["maximum"])$model->update(["maximum"=>$in[$index]["maximum"]]); unset($in[$index]); } $in = array_values($in); } if ($in){ LogService::log(__CLASS__,__METHOD__,json_encode($in)); CommodityMaterialBoxModel::query()->insert($in); } $this->success(); } $item = StoreItem::query()->whereHas("store",function (Builder $query){ $query->where("asn_code",request("asn")); })->whereHas("commodity",function (Builder $query){ $query->whereHas("barcodes",function (Builder $query){ $query->where("code",request("barCode")); }); })->first(); if (!$item)$this->error("商品不存在"); list($in,$map,$ids) = $insert($item->store->commodity_id); LogService::log(__CLASS__,__METHOD__,json_encode($in)); CommodityMaterialBoxModel::query()->insert($in); $this->getMaterBoxModel($item,$map); } private function getMaterBoxModel($item,$map) { $models = app("MaterialBoxModelService")->getModelSortedByOwner($item->store->owner_id); foreach ($models as $model){ if (!isset($map[$model->id]))continue; $result = app("StorageService")->getHalfBoxLocation($map[$model->id],$item,$item->store->asn_code); if (!$result)continue; $result->maximum = $model->maximum-$result->amount; $this->success($result); } $this->success(["need"=>$models[0]->maximum,"material_box_model_id"=>$models[0]->material_box_model_id,"commodity_id"=>$item->commodity_id]); } /** * 根据条码检索商品 */ public function searchBarCode() { $commodities = Commodity::query()->whereHas("barcodes",function (Builder $query){ $query->where("code",request("barCode")); })->get(); if (!$commodities->count())$this->error("库内无此商品信息"); if ($commodities->count()>1)$commodities->load("owner"); $this->success($commodities); } }