StorageController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commodity;
  4. use App\CommodityMaterialBoxModel;
  5. use App\Components\AsyncResponse;
  6. use App\MaterialBox;
  7. use App\MaterialBoxModel;
  8. use App\Owner;
  9. use App\Station;
  10. use App\StationTask;
  11. use App\StationTaskMaterialBox;
  12. use App\Storage;
  13. use App\StoreItem;
  14. use App\TaskTransaction;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  18. use Illuminate\Support\Collection;
  19. use Illuminate\Support\Facades\Auth;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Gate;
  22. use Illuminate\Support\Facades\Validator;
  23. class StorageController extends Controller
  24. {
  25. use AsyncResponse;
  26. use AuthenticatesUsers;
  27. public function putShelf()
  28. {
  29. $this->gate("入库管理-入库-缓存架入库");
  30. $asn = \request("asn");
  31. $ide = \request("ide");
  32. $barCode = \request("barCode");
  33. $amount = (int)\request("amount");
  34. //check info
  35. if (!$asn || !$ide || !$barCode || !$amount)$this->error("信息不完整");
  36. $fromLocation = app("MaterialBoxService")->getBoxLocation($ide);
  37. if (!$fromLocation)$this->error("WAS无此库位信息");
  38. $box = MaterialBox::query()->where("code",$ide)->first();
  39. if (!$box)$this->error("WAS无此料箱");
  40. //库存相关信息
  41. /** @var Station|\stdClass $station */
  42. $station = Station::query()->firstOrCreate(["code"=>$fromLocation],[
  43. 'name' => $fromLocation, 'code' => $fromLocation, 'station_type_id' => 5
  44. ]);
  45. $item = StoreItem::query()->whereHas("store",function (Builder $query){
  46. $query->where("asn_code",request("asn"));
  47. })->whereHas("commodity",function (Builder $query)use($barCode){
  48. $query->whereHas("barcodes",function (Builder $query)use($barCode){
  49. $query->where("code",$barCode);
  50. });
  51. })->first();
  52. //get flux
  53. $tasks = app("StorageService")->getFluxTask($asn,$barCode,$amount);
  54. if (!$tasks)$this->error("该单无上架任务或不存在");
  55. //此处嵌套三层事务 以最高层级为准
  56. DB::beginTransaction(); //总体事务 回滚WAS错误操作
  57. try{
  58. //库存记录
  59. if (!app("StorageService")->enterWarehouse($station->id, $box->id, $item->commodity_id ?? null, $amount, $box->material_box_model_id))$this->error("库存异常");
  60. DB::connection("oracle")->transaction(function ()use($tasks,$ide){ //单体嵌套事务 回滚FLUX失败任务
  61. foreach ($tasks as $task)if (!app("StorageService")->fluxPA($task,$ide)){
  62. DB::connection("oracle")->rollBack();
  63. $this->error("FLUX上架失败");
  64. };
  65. });
  66. DB::commit();
  67. }catch (\Exception $e){
  68. DB::rollBack();
  69. $this->error($e->getMessage());
  70. }
  71. //亮灯
  72. app("CacheShelfService")->_stationCacheLightOn($fromLocation,$ide);
  73. $maximum = app("CommodityMaterialBoxModelService")->getMaximum($box->material_box_model_id,$item->commodity_id ?? null);
  74. if ($maximum && $maximum<$amount)app("CommodityMaterialBoxModelService")->setMaximum($box->material_box_model_id,$item->commodity_id ?? null,$amount);
  75. $this->success(["model" => $box->material_box_model_id,"commodity" => $item->commodity_id ?? null, "maximum" => $maximum]);
  76. }
  77. public function setMaximum()
  78. {
  79. app("CommodityMaterialBoxModelService")->setMaximum(request("model"),request("commodity"),request("maximum"));
  80. $this->success();
  81. }
  82. /**
  83. * 检查最大限值并返回
  84. *
  85. */
  86. public function checkMaximum()
  87. {
  88. $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode"));
  89. if (!$item)$this->error("无此单据记录");
  90. $models = CommodityMaterialBoxModel::query()->where("commodity_id",$item->commodity_id)->get();
  91. if ($models->count()==0)$this->error("商品首入,请使用缓存架空箱入库");
  92. foreach ($models as $model){
  93. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"));
  94. if ($result){
  95. $result->maximum = $model->maximum-$result->amount;
  96. $this->success($result);
  97. }
  98. }
  99. $this->success(["need"=>$models[0]->maximum,"material_box_model_id"=>$models[0]->material_box_model_id,"commodity_id"=>$item->commodity_id]);
  100. }
  101. /**
  102. * 检查ASN可上架总数
  103. */
  104. public function checkAsnAmount()
  105. {
  106. $sql = <<<sql
  107. SELECT sum(fmqty) amount FROM DOC_ASN_DETAILS LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  108. LEFT JOIN TSK_TASKLISTS ON DOC_ASN_DETAILS.ASNNO = TSK_TASKLISTS.DOCNO AND DOC_ASN_DETAILS.ASNLINENO = TSK_TASKLISTS.DOCLINENO
  109. WHERE ASNNO = ? AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?)
  110. AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
  111. sql;
  112. $task = DB::connection("oracle")->selectOne(DB::raw($sql),[request("asn"),request("barCode"),request("barCode"),request("barCode")]);
  113. $this->success($task->amount ?? 0);
  114. }
  115. /**
  116. * 重置缓存架指定格口
  117. */
  118. public function resetCacheShelf()
  119. {
  120. $this->gate("入库管理-入库-缓存架入库");
  121. $boxes = request("boxes");
  122. //清理任务
  123. $data = '';
  124. $occupy = Storage::query()->with("station:id,code")->whereIn("station_id",Station::query()->select("id")->whereIn("code",$boxes))->where("status",1)->get();
  125. foreach ($occupy as $item){
  126. unset($boxes[array_search($item->station->code,$boxes)]);
  127. $data .= '“'.$item->station->code.'”,';
  128. }
  129. if ($occupy->count()>0){
  130. $data .= "存在任务待处理,无法调取 ";
  131. $boxes = array_values($boxes);
  132. }
  133. $tasks = StationTaskMaterialBox::query()->with("station:id,code")
  134. ->whereIn("station_id",Station::query()->select("id")->whereIn("code",$boxes))
  135. ->whereNotIn("status",["完成","取消"])->get();
  136. foreach ($tasks as $task){
  137. unset($boxes[array_search($task->station->code,$boxes)]);
  138. $data .= '“'.$task->station->code.'”,';
  139. };
  140. if ($tasks->count()>0){
  141. $data .= "任务排队中,无法调取";
  142. $boxes = array_values($boxes);
  143. }
  144. //重新调取料箱
  145. $result = app("ForeignHaiRoboticsService")->paddingCacheShelf(Station::query()->whereIn("code",$boxes)->get());
  146. if ($result===null)$this->error("任务下发错误,检查日志");
  147. if ($result===false)$this->error("已无可用料箱,部分库位填充失败");
  148. $this->success(["data"=>$data,"boxes"=>$boxes]);
  149. }
  150. /**
  151. * 取得料箱
  152. */
  153. public function acquireBox()
  154. {
  155. $this->gate("入库管理-入库-半箱补货入库");
  156. $boxId = request("material_box_id");
  157. $modelId = request("material_box_model_id");
  158. //获取目标库位
  159. $station = app("StationService")->getMirrorMappingLocation(request("station"));
  160. if (!$station)$this->error("未知库位");
  161. $occupyTask = StationTaskMaterialBox::query()->where("station_id",$station->id)->whereNotIn("status",["完成","取消"])->first();
  162. if ($occupyTask)$this->error("库位存在任务未处理完成");
  163. $amount = app("StorageService")->checkPutAmount(request("asn"),request("barCode"));
  164. if ($amount<request("amount"))$this->error("待上架数量不足,最大可上数量为{$amount}");
  165. //获取料箱
  166. if ($boxId && !app("MaterialBoxService")->checkUsableBox($boxId)){
  167. $blacklist = [$boxId];
  168. $boxId = null;
  169. //料箱存在且不可用
  170. $models = CommodityMaterialBoxModel::query()->where("commodity_id",request("commodity_id"))->get();
  171. $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode"));
  172. if (!$item)$this->error("无此单据记录");
  173. foreach ($models as $model){
  174. //料箱不可用寻找新料箱
  175. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"),$blacklist);
  176. while ($result){
  177. //料箱可用并且数量符合本次半箱数量 跳出
  178. if (app("MaterialBoxService")->checkUsableBox($result->material_box_id)
  179. && $model->maximum-$result->amount>=request("amount")){$boxId = $result->material_box_id;break;}
  180. else $blacklist[] = $result->material_box_id;
  181. //否则黑名单此料箱继续查找 直至料箱为空
  182. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"),$blacklist);
  183. }
  184. }
  185. }
  186. //料箱不存在且该商品有过入库记录 拿取空箱
  187. if (!$boxId){
  188. if (!$modelId){
  189. $box = null;
  190. $models = CommodityMaterialBoxModel::query()->with("materialBoxModel")->where("commodity_id",request("commodity_id"))->get();
  191. foreach ($models as $model){
  192. if (!$model->materialBoxModel)continue;
  193. $box = app("MaterialBoxService")->getAnEmptyBox($model->materialBoxModel);
  194. if($box)break;
  195. }
  196. }else $box = app("MaterialBoxService")->getAnEmptyBox(MaterialBoxModel::query()->find($modelId));
  197. if (!$box)$this->error("无可用料箱");
  198. $boxId = $box->id;
  199. }
  200. //发起取箱任务
  201. DB::beginTransaction();
  202. $collection = new Collection();
  203. $task = StationTask::query()->create([
  204. 'status' => "待处理",
  205. 'station_id' => $station->id,
  206. ]);
  207. $collection->add(StationTaskMaterialBox::query()->create([
  208. 'station_id' => $station->id,
  209. 'material_box_id'=>$boxId,
  210. 'status'=>"待处理",
  211. 'type' => '取',
  212. 'station_task_id' => $task->id,
  213. ]));
  214. if (!app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','立架出至缓存架'))$this->error("呼叫机器人失败");
  215. //生成临时任务事务
  216. TaskTransaction::query()->create([
  217. "doc_code" => request("asn"),
  218. "bar_code" => request("barCode"),
  219. "fm_station_id" => $station->id,
  220. "material_box_id" => $boxId,
  221. "commodity_id" => request("commodity_id"),
  222. "amount" => request("amount"),
  223. "type" => "入库",
  224. "user_id" => Auth::id(),
  225. "mark" => 1
  226. ]);
  227. DB::commit();
  228. //亮灯
  229. app("CacheShelfService")->stationLightUp($station->code,null,'2');
  230. $this->success();
  231. }
  232. /**
  233. * 溢出校正
  234. */
  235. public function overflowRevision()
  236. {
  237. $this->gate("入库管理-入库-半箱补货入库");
  238. $station = request("station");
  239. $amount = request("amount");
  240. //获取目标库位
  241. $station = app("StationService")->getMirrorMappingLocation($station);
  242. if (!$station)$this->error("未知库位");
  243. $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)->where("amount",">",$amount)
  244. ->where("type","入库")->where("mark",1)->where("status",0)->first();
  245. if (!$task)$this->error("无任务存在");
  246. $task->update(["amount" => DB::raw("amount - {$amount}")]);
  247. $storage = Storage::query()->where("material_box_id",$task->material_box_id)->first();
  248. $maximum = (($storage->amount ?? 0)+$task->amount)-$amount;
  249. CommodityMaterialBoxModel::query()->where("material_box_model_id",$task->materialBox->material_box_model_id)
  250. ->where("commodity_id",$task->commodity_id)->update(["maximum"=>$maximum]);
  251. $this->success("校正成功");
  252. }
  253. public function syncStorage()
  254. {
  255. ini_set('max_execution_time', 0);
  256. $model = MaterialBoxModel::query()->create([
  257. "code" => "common"
  258. ]);
  259. $sql = <<<sql
  260. select * from INV_LOT_LOC_ID where traceid = '*' and locationid like 'IDE%'
  261. sql;
  262. DB::beginTransaction();
  263. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $inv){
  264. $materialBox = MaterialBox::query()->firstOrCreate(["code"=>$inv->locationid],[
  265. "code" => $inv->locationid,
  266. "material_box_model_id"=>$model
  267. ]);
  268. $owner = Owner::query()->firstOrCreate([
  269. "code" => $inv->customerid
  270. ],[
  271. "code" => $inv->customerid,
  272. "name" => $inv->customerid,
  273. ]);
  274. $commodity = Commodity::query()->where("owner_id",$owner->id)->where("sku",$inv->sku)->first();
  275. $s = \App\Storage::query()->where("material_box_id",$materialBox->id)
  276. ->where("commodity_id",$commodity->id)->first();
  277. if (!$s)\App\Storage::query()->create([
  278. "material_box_id" => $materialBox->id,
  279. "commodity_id" => $commodity->id,
  280. "amount" => $inv->qty,
  281. ]);else $s->update(["amount" => $inv->qty]);
  282. }
  283. DB::commit();
  284. $this->success();
  285. }
  286. /**
  287. * 在安卓端的个体登录鉴权
  288. *
  289. */
  290. public function androidLogin()
  291. {
  292. $errors=Validator::make(request()->input(),[
  293. "name" => 'required|string',
  294. 'password' => 'required|string'])->errors();
  295. if($errors->count()>0){return ['success'=>false,'errors'=>$errors];}
  296. request()->offsetSet("remember",true);
  297. if (!$this->attemptLogin(request()))return ['success'=>false,'errors'=>['name'=>['登录信息验证失败']]];
  298. if (!Gate::allows("入库管理-入库-缓存架入库") && !Gate::allows("入库管理-入库-半箱补货入库"))return ['success'=>false,'errors'=>['name'=>['用户无权操作入库']]];
  299. return ['success'=>true,'url'=>url("store/inStorage/android.index")];
  300. }
  301. public function username(): string
  302. {
  303. return 'name';
  304. }
  305. /**
  306. * 库外箱绑定至库位
  307. */
  308. public function bindBox()
  309. {
  310. $location = request("location");
  311. $box = request("ide");
  312. if (!$location || !$box)$this->error("参数传递错误");
  313. $task = StationTaskMaterialBox::query()->select("id")->where(function ($query)use($location,$box){
  314. /** @var Builder $query */
  315. $query->whereHas("station",function ($query)use($location){
  316. /** @var Builder $query */
  317. $query->where("code",$location);
  318. })->orWhereHas("materialBox",function ($query)use($box){
  319. /** @var Builder $query */
  320. $query->where("code",$box);
  321. });
  322. })->whereNotIn("status",["完成","取消"])->first();
  323. if ($task)$this->error("库位或料箱存在任务待执行,无法放置料箱");
  324. $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select(DB::raw("1"))
  325. ->where("ks_bin_code",$box)->where("status",4)->first();
  326. if (!$ks)$this->error("海柔料箱状态异常");
  327. DB::beginTransaction();
  328. try {
  329. $storages = Storage::query()->whereHas("station",function ($query)use($location){
  330. /** @var Builder $query */
  331. $query->where("code",$location);
  332. })->orWhereHas("materialBox",function ($query)use($box){
  333. /** @var Builder $query */
  334. $query->where("code",$box);
  335. })->lockForUpdate()->first();
  336. $station = Station::query()->where("code",$location)->first();
  337. $box = MaterialBox::query()->where("code",$box)->first();
  338. if (!$station || !$box)$this->error("库位或料箱未在WAS记录");
  339. switch ($storages->count()){
  340. case 0:
  341. Storage::query()->create([
  342. "station_id" => $station->id,
  343. "material_box_id" => $box->id,
  344. ]);
  345. break;
  346. case 1:
  347. $storage = $storages->first();
  348. if ($storage->status==1)$this->error("库存占用中");
  349. //已经映射库位 跳出
  350. if ($storage->station_id==$station->id && $storage->material_box_id==$box->id)break;
  351. //库存中 仅有库位信息
  352. if ($storage->station_id==$station->id){
  353. //料箱信息存在且有库存-》新建库位映射,将此料箱库位清空
  354. if ($storage->material_box_id && $storage->amount){
  355. Storage::query()->create([
  356. "station_id" => $station->id,
  357. "material_box_id" => $box->id,
  358. ]);
  359. $storage->update(["station_id"=>null]);
  360. //料箱信息不存在或没有库存信息 映射到当前库位
  361. }else $storage->update(["material_box_id"=>$box->id,"commodity_id"=>null,"amount"=>0]);
  362. }else $storage->update(["station_id"=>$station->id]);
  363. break;
  364. case 2:
  365. //ls:库位库存 bs:料箱库存
  366. $ls = null;
  367. $bs = null;
  368. foreach ($storages as $storage){
  369. if ($storage->station_id==$station->id)$ls = $storage;
  370. else $bs = $storage;
  371. }
  372. //其一不存在说明库存中重复记录 需要人工筛查
  373. if (!$ls || !$bs)$this->error("库存中重复库位记录或料箱记录");
  374. //库位库存存在有效库存 库位置空 否则删除库存记录
  375. if ($ls->material_box_id && $ls->amount)$ls->update(["station_id"=>null]);
  376. else $ls->delete();
  377. //料箱映射库位改至当前库位
  378. $bs->update(["station_id"=>$station->id]);
  379. break;
  380. default:
  381. $this->error("库存信息异常,料箱或库位未完全释放");
  382. }
  383. DB::commit();
  384. $this->success();
  385. }catch (\Exception $e){
  386. DB::rollBack();
  387. $this->error($e->getMessage());
  388. }
  389. }
  390. public function bindModelIndex(){
  391. $models = MaterialBoxModel::query()->get();
  392. return view("store.inStorage.boxBindModel",compact("models"));
  393. }
  394. public function searchIde()
  395. {
  396. $ide = request("ide");
  397. $box = MaterialBox::query()->where("code",$ide)->first();
  398. if (!$box)$this->error("料箱不存在");
  399. $this->success($box->material_box_model_id);
  400. }
  401. public function boxBindModel()
  402. {
  403. $this->success(MaterialBox::query()->where("code",request("ide"))
  404. ->update(["material_box_model_id"=>request("material_box_model_id")]));
  405. }
  406. }