StorageController.php 17 KB

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