StorageController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. //重新调取料箱
  134. $result = app("ForeignHaiRoboticsService")->paddingCacheShelf(Station::query()->whereIn("code",$boxes)->get());
  135. if ($result===null)$this->error("任务下发错误,检查日志");
  136. if ($result===false)$this->error("已无可用料箱,部分库位填充失败");
  137. $this->success(["data"=>$data,"boxes"=>$boxes]);
  138. }
  139. /**
  140. * 取得料箱
  141. */
  142. public function acquireBox()
  143. {
  144. $this->gate("入库管理-入库-半箱补货入库");
  145. $boxId = request("material_box_id");
  146. $modelId = request("material_box_model_id");
  147. //获取目标库位
  148. $station = app("StationService")->getMirrorMappingLocation(request("station"));
  149. if (!$station)$this->error("未知库位");
  150. $occupyTask = StationTaskMaterialBox::query()->where("station_id",$station->id)->whereNotIn("status",["完成","取消"])->first();
  151. if ($occupyTask)$this->error("库位存在任务未处理完成");
  152. $amount = app("StorageService")->checkPutAmount(request("asn"),request("barCode"));
  153. if ($amount<request("amount"))$this->error("待上架数量不足,最大可上数量为{$amount}");
  154. //获取料箱
  155. if ($boxId && !app("MaterialBoxService")->checkUsableBox($boxId)){
  156. $blacklist = [$boxId];
  157. $boxId = null;
  158. //料箱存在且不可用
  159. $models = CommodityMaterialBoxModel::query()->where("commodity_id",request("commodity_id"))->get();
  160. $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode"));
  161. if (!$item)$this->error("无此单据记录");
  162. foreach ($models as $model){
  163. //料箱不可用寻找新料箱
  164. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"),$blacklist);
  165. while ($result){
  166. //料箱可用并且数量符合本次半箱数量 跳出
  167. if (app("MaterialBoxService")->checkUsableBox($result->material_box_id)
  168. && $model->maximum-$result->amount>=request("amount")){$boxId = $result->material_box_id;break;}
  169. else $blacklist[] = $result->material_box_id;
  170. //否则黑名单此料箱继续查找 直至料箱为空
  171. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"),$blacklist);
  172. }
  173. }
  174. }
  175. //料箱不存在且该商品有过入库记录 拿取空箱
  176. if (!$boxId){
  177. if (!$modelId){
  178. $box = null;
  179. $models = CommodityMaterialBoxModel::query()->with("materialBoxModel")->where("commodity_id",request("commodity_id"))->get();
  180. foreach ($models as $model){
  181. if (!$model->materialBoxModel)continue;
  182. $box = app("MaterialBoxService")->getAnEmptyBox($model->materialBoxModel);
  183. if($box)break;
  184. }
  185. }else $box = app("MaterialBoxService")->getAnEmptyBox(MaterialBoxModel::query()->find($modelId));
  186. if (!$box)$this->error("无可用料箱");
  187. $boxId = $box->id;
  188. }
  189. //发起取箱任务
  190. DB::beginTransaction();
  191. $collection = new Collection();
  192. $task = StationTask::query()->create([
  193. 'status' => "待处理",
  194. 'station_id' => $station->id,
  195. ]);
  196. $collection->add(StationTaskMaterialBox::query()->create([
  197. 'station_id' => $station->id,
  198. 'material_box_id'=>$boxId,
  199. 'status'=>"待处理",
  200. 'type' => '取',
  201. 'station_task_id' => $task->id,
  202. ]));
  203. if (!app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','立架出至缓存架'))$this->error("呼叫机器人失败");
  204. //生成临时任务事务
  205. TaskTransaction::query()->create([
  206. "doc_code" => request("asn"),
  207. "bar_code" => request("barCode"),
  208. "fm_station_id" => $station->id,
  209. "material_box_id" => $boxId,
  210. "commodity_id" => request("commodity_id"),
  211. "amount" => request("amount"),
  212. "type" => "入库",
  213. "user_id" => Auth::id(),
  214. "mark" => 1
  215. ]);
  216. DB::commit();
  217. //亮灯
  218. app("CacheShelfService")->stationLightUp($station->code,null,'2');
  219. $this->success();
  220. }
  221. /**
  222. * 溢出校正
  223. */
  224. public function overflowRevision()
  225. {
  226. $this->gate("入库管理-入库-半箱补货入库");
  227. $station = request("station");
  228. $amount = request("amount");
  229. //获取目标库位
  230. $station = app("StationService")->getMirrorMappingLocation($station);
  231. if (!$station)$this->error("未知库位");
  232. $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)->where("amount",">",$amount)
  233. ->where("type","入库")->where("mark",1)->where("status",0)->first();
  234. if (!$task)$this->error("无任务存在");
  235. $task->update(["amount" => DB::raw("amount - {$amount}")]);
  236. $storage = Storage::query()->where("material_box_id",$task->material_box_id)->first();
  237. $maximum = (($storage->amount ?? 0)+$task->amount)-$amount;
  238. CommodityMaterialBoxModel::query()->where("material_box_model_id",$task->materialBox->material_box_model_id)
  239. ->where("commodity_id",$task->commodity_id)->update(["maximum"=>$maximum]);
  240. $this->success("校正成功");
  241. }
  242. public function syncStorage()
  243. {
  244. ini_set('max_execution_time', 0);
  245. $model = MaterialBoxModel::query()->create([
  246. "code" => "common"
  247. ]);
  248. $sql = <<<sql
  249. select * from INV_LOT_LOC_ID where traceid = '*' and locationid like 'IDE%'
  250. sql;
  251. DB::beginTransaction();
  252. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $inv){
  253. $materialBox = MaterialBox::query()->firstOrCreate(["code"=>$inv->locationid],[
  254. "code" => $inv->locationid,
  255. "material_box_model_id"=>$model
  256. ]);
  257. $owner = Owner::query()->firstOrCreate([
  258. "code" => $inv->customerid
  259. ],[
  260. "code" => $inv->customerid,
  261. "name" => $inv->customerid,
  262. ]);
  263. $commodity = Commodity::query()->where("owner_id",$owner->id)->where("sku",$inv->sku)->first();
  264. $s = \App\Storage::query()->where("material_box_id",$materialBox->id)
  265. ->where("commodity_id",$commodity->id)->first();
  266. if (!$s)\App\Storage::query()->create([
  267. "material_box_id" => $materialBox->id,
  268. "commodity_id" => $commodity->id,
  269. "amount" => $inv->qty,
  270. ]);else $s->update(["amount" => $inv->qty]);
  271. }
  272. DB::commit();
  273. $this->success();
  274. }
  275. /**
  276. * 在安卓端的个体登录鉴权
  277. *
  278. */
  279. public function androidLogin()
  280. {
  281. $errors=Validator::make(request()->input(),[
  282. "name" => 'required|string',
  283. 'password' => 'required|string'])->errors();
  284. if($errors->count()>0){return ['success'=>false,'errors'=>$errors];}
  285. request()->offsetSet("remember",true);
  286. if (!$this->attemptLogin(request()))return ['success'=>false,'errors'=>['name'=>['登录信息验证失败']]];
  287. if (!Gate::allows("入库管理-入库-缓存架入库") && !Gate::allows("入库管理-入库-半箱补货入库"))return ['success'=>false,'errors'=>['name'=>['用户无权操作入库']]];
  288. return ['success'=>true,'url'=>url("store/inStorage/android.index")];
  289. }
  290. public function username(): string
  291. {
  292. return 'name';
  293. }
  294. /**
  295. * 库外箱绑定至库位
  296. */
  297. public function bindBox()
  298. {
  299. $location = request("location");
  300. $box = request("ide");
  301. if (!$location || !$box)$this->error("参数传递错误");
  302. $task = StationTaskMaterialBox::query()->select("id")->where(function ($query)use($location,$box){
  303. /** @var Builder $query */
  304. $query->whereHas("station",function ($query)use($location){
  305. /** @var Builder $query */
  306. $query->where("code",$location);
  307. })->orWhereHas("materialBox",function ($query)use($box){
  308. /** @var Builder $query */
  309. $query->where("code",$box);
  310. });
  311. })->whereNotIn("status",["完成","取消"])->first();
  312. if ($task)$this->error("库位或料箱存在任务待执行,无法放置料箱");
  313. $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select(DB::raw("1"))
  314. ->where("ks_bin_code",$box)->where("status",4)->first();
  315. if (!$ks)$this->error("海柔料箱状态异常");
  316. DB::beginTransaction();
  317. try {
  318. $storages = Storage::query()->whereHas("station",function ($query)use($location){
  319. /** @var Builder $query */
  320. $query->where("code",$location);
  321. })->orWhereHas("materialBox",function ($query)use($box){
  322. /** @var Builder $query */
  323. $query->where("code",$box);
  324. })->lockForUpdate()->get();
  325. $station = Station::query()->where("code",$location)->first();
  326. $box = MaterialBox::query()->where("code",$box)->first();
  327. if (!$station || !$box)$this->error("库位或料箱未在WAS记录");
  328. switch ($storages->count()){
  329. case 0:
  330. Storage::query()->create([
  331. "station_id" => $station->id,
  332. "material_box_id" => $box->id,
  333. ]);
  334. break;
  335. case 1:
  336. $storage = $storages->first();
  337. if ($storage->status==1)$this->error("库存占用中");
  338. //已经映射库位 跳出
  339. if ($storage->station_id==$station->id && $storage->material_box_id==$box->id)break;
  340. //库存中 仅有库位信息
  341. if ($storage->station_id==$station->id){
  342. //料箱信息存在且有库存-》新建库位映射,将此料箱库位清空
  343. if ($storage->material_box_id && $storage->amount){
  344. Storage::query()->create([
  345. "station_id" => $station->id,
  346. "material_box_id" => $box->id,
  347. ]);
  348. $storage->update(["station_id"=>null]);
  349. //料箱信息不存在或没有库存信息 映射到当前库位
  350. }else $storage->update(["material_box_id"=>$box->id,"commodity_id"=>null,"amount"=>0]);
  351. }else $storage->update(["station_id"=>$station->id]);
  352. break;
  353. case 2:
  354. //ls:库位库存 bs:料箱库存
  355. $ls = null;
  356. $bs = null;
  357. foreach ($storages as $storage){
  358. if ($storage->station_id==$station->id)$ls = $storage;
  359. else $bs = $storage;
  360. }
  361. //其一不存在说明库存中重复记录 需要人工筛查
  362. if (!$ls || !$bs)$this->error("库存中重复库位记录或料箱记录");
  363. //库位库存存在有效库存 库位置空 否则删除库存记录
  364. if ($ls->material_box_id && $ls->amount)$ls->update(["station_id"=>null]);
  365. else $ls->delete();
  366. //料箱映射库位改至当前库位
  367. $bs->update(["station_id"=>$station->id]);
  368. break;
  369. default:
  370. $this->error("库存信息异常,料箱或库位未完全释放");
  371. }
  372. DB::commit();
  373. $this->success();
  374. }catch (\Exception $e){
  375. DB::rollBack();
  376. $this->error($e->getMessage());
  377. }
  378. }
  379. public function bindModelIndex(){
  380. $models = MaterialBoxModel::query()->get();
  381. return view("store.inStorage.boxBindModel",compact("models"));
  382. }
  383. public function searchIde()
  384. {
  385. $ide = request("ide");
  386. $box = MaterialBox::query()->where("code",$ide)->first();
  387. if (!$box)$this->error("料箱不存在");
  388. $this->success($box->material_box_model_id);
  389. }
  390. public function boxBindModel()
  391. {
  392. $this->success(MaterialBox::query()->where("code",request("ide"))
  393. ->update(["material_box_model_id"=>request("material_box_model_id")]));
  394. }
  395. }