StorageController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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\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. $fromLocation = app("MaterialBoxService")->getBoxLocation($ide);
  36. if (!$fromLocation)$this->error("WAS无此库位信息");
  37. $box = MaterialBox::query()->where("code",$ide)->first();
  38. if (!$box)$this->error("WAS无此料箱");
  39. //库存相关信息
  40. /** @var Station|\stdClass $station */
  41. $station = Station::query()->firstOrCreate(["code"=>$fromLocation],[
  42. 'name' => $fromLocation, 'code' => $fromLocation, 'station_type_id' => 5
  43. ]);
  44. $item = StoreItem::query()->whereHas("store",function (Builder $query){
  45. $query->where("asn_code",request("asn"));
  46. })->whereHas("commodity",function (Builder $query)use($barCode){
  47. $query->whereHas("barcodes",function (Builder $query)use($barCode){
  48. $query->where("code",$barCode);
  49. });
  50. })->first();
  51. //get flux
  52. $tasks = app("StorageService")->getFluxTask($asn,$barCode,$amount);
  53. if (!$tasks)$this->error("该单无上架任务或不存在");
  54. //此处嵌套三层事务 以最高层级为准
  55. DB::beginTransaction(); //总体事务 回滚WAS错误操作
  56. try{
  57. //库存记录
  58. if (!app("StorageService")->enterWarehouse($station->id, $box->id, $item->commodity_id ?? null, $amount, $box->material_box_model_id))$this->error("库存异常");
  59. DB::connection("oracle")->transaction(function ()use($tasks,$ide){ //单体嵌套事务 回滚FLUX失败任务
  60. foreach ($tasks as $task)if (!app("StorageService")->fluxPA($task,$ide)){
  61. DB::connection("oracle")->rollBack();
  62. $this->error("FLUX上架失败");
  63. };
  64. });
  65. DB::commit();
  66. }catch (\Exception $e){
  67. DB::rollBack();
  68. $this->error($e->getMessage());
  69. }
  70. //亮灯
  71. app("CacheShelfService")->_stationCacheLightOn($fromLocation,$ide);
  72. $maximum = app("CommodityMaterialBoxModelService")->getMaximum($box->material_box_model_id,$item->commodity_id ?? null);
  73. if ($maximum && $maximum<$amount)app("CommodityMaterialBoxModelService")->setMaximum($box->material_box_model_id,$item->commodity_id ?? null,$amount);
  74. $this->success(["model" => $box->material_box_model_id,"commodity" => $item->commodity_id ?? null, "maximum" => $maximum]);
  75. }
  76. public function setMaximum()
  77. {
  78. app("CommodityMaterialBoxModelService")->setMaximum(request("model"),request("commodity"),request("maximum"));
  79. $this->success();
  80. }
  81. /**
  82. * 检查最大限值并返回
  83. *
  84. */
  85. public function checkMaximum()
  86. {
  87. $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode"));
  88. if (!$item)$this->error("无此单据记录");
  89. $models = CommodityMaterialBoxModel::query()->where("commodity_id",$item->commodity_id)->get();
  90. if ($models->count()==0)$this->error("商品首入,请使用缓存架空箱入库");
  91. foreach ($models as $model){
  92. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"));
  93. if ($result){
  94. $result->maximum = $model->maximum-$result->amount;
  95. $this->success($result);
  96. }
  97. }
  98. $this->success(["need"=>$models[0]->maximum,"material_box_model_id"=>$models[0]->material_box_model_id,"commodity_id"=>$item->commodity_id]);
  99. }
  100. /**
  101. * 检查ASN可上架总数
  102. */
  103. public function checkAsnAmount()
  104. {
  105. $sql = <<<sql
  106. 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
  107. LEFT JOIN TSK_TASKLISTS ON DOC_ASN_DETAILS.ASNNO = TSK_TASKLISTS.DOCNO AND DOC_ASN_DETAILS.ASNLINENO = TSK_TASKLISTS.DOCLINENO
  108. WHERE ASNNO = ? AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?)
  109. AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
  110. sql;
  111. $task = DB::connection("oracle")->selectOne(DB::raw($sql),[request("asn"),request("barCode"),request("barCode"),request("barCode")]);
  112. $this->success($task->amount ?? 0);
  113. }
  114. /**
  115. * 重置缓存架指定格口
  116. */
  117. public function resetCacheShelf()
  118. {
  119. $this->gate("入库管理-入库-缓存架入库");
  120. $boxes = request("boxes");
  121. //清理任务
  122. $data = '';
  123. $occupy = Storage::query()->with("station:id,code")->whereIn("station_id",Station::query()->select("id")->whereIn("code",$boxes))->where("status",1)->get();
  124. foreach ($occupy as $item){
  125. unset($boxes[array_search($item->station->code,$boxes)]);
  126. $data .= '“'.$item->station->code.'”,';
  127. }
  128. if ($occupy->count()>0){
  129. $data .= "存在任务待处理,无法调取";
  130. $boxes = array_values($boxes);
  131. }
  132. //重新调取料箱
  133. $result = app("ForeignHaiRoboticsService")->paddingCacheShelf(Station::query()->whereIn("code",$boxes)->get());
  134. if ($result===null)$this->error("任务下发错误,检查日志");
  135. if ($result===false)$this->error("已无可用料箱,部分库位填充失败");
  136. $this->success(["data"=>$data,"boxes"=>$boxes]);
  137. }
  138. /**
  139. * 取得料箱
  140. */
  141. public function acquireBox()
  142. {
  143. $this->gate("入库管理-入库-半箱补货入库");
  144. $boxId = request("material_box_id");
  145. $modelId = request("material_box_model_id");
  146. //获取目标库位
  147. $station = app("StationService")->getMirrorMappingLocation(request("station"));
  148. if (!$station)$this->error("未知库位");
  149. $occupyTask = StationTaskMaterialBox::query()->where("station_id",$station->id)->whereNotIn("status",["完成","取消"])->first();
  150. if ($occupyTask)$this->error("库位存在任务未处理完成");
  151. $amount = app("StorageService")->checkPutAmount(request("asn"),request("barCode"));
  152. if ($amount<request("amount"))$this->error("待上架数量不足,最大可上数量为{$amount}");
  153. //获取料箱
  154. if ($boxId && !app("MaterialBoxService")->checkUsableBox($boxId)){
  155. $blacklist = [$boxId];
  156. $boxId = null;
  157. //料箱存在且不可用
  158. $models = CommodityMaterialBoxModel::query()->where("commodity_id",request("commodity_id"))->get();
  159. $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode"));
  160. if (!$item)$this->error("无此单据记录");
  161. foreach ($models as $model){
  162. //料箱不可用寻找新料箱
  163. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"),$blacklist);
  164. while ($result){
  165. //料箱可用并且数量符合本次半箱数量 跳出
  166. if (app("MaterialBoxService")->checkUsableBox($result->material_box_id)
  167. && $model->maximum-$result->amount>=request("amount")){$boxId = $result->material_box_id;break;}
  168. else $blacklist[] = $result->material_box_id;
  169. //否则黑名单此料箱继续查找 直至料箱为空
  170. $result = app("StorageService")->getHalfBoxLocation($model,$item,request("asn"),$blacklist);
  171. }
  172. }
  173. }
  174. //料箱不存在且该商品有过入库记录 拿取空箱
  175. if (!$boxId){
  176. if (!$modelId){
  177. $box = null;
  178. $models = CommodityMaterialBoxModel::query()->where("commodity_id",request("commodity_id"))->get();
  179. foreach ($models as $model){
  180. $box = app("MaterialBoxService")->getAnEmptyBox([],$model->material_box_model_id);
  181. if($box)break;
  182. }
  183. }else $box = app("MaterialBoxService")->getAnEmptyBox([],$modelId);
  184. if (!$box)$this->error("无可用料箱");
  185. $boxId = $box->id;
  186. }
  187. //发起取箱任务
  188. DB::beginTransaction();
  189. $collection = new Collection();
  190. $task = StationTask::query()->create([
  191. 'status' => "待处理",
  192. 'station_id' => $station->id,
  193. ]);
  194. $collection->add(StationTaskMaterialBox::query()->create([
  195. 'station_id' => $station->id,
  196. 'material_box_id'=>$boxId,
  197. 'status'=>"待处理",
  198. 'type' => '取',
  199. 'station_task_id' => $task->id,
  200. ]));
  201. if (!app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','立架出至缓存架'))$this->error("呼叫机器人失败");
  202. //生成临时任务事务
  203. TaskTransaction::query()->create([
  204. "doc_code" => request("asn"),
  205. "bar_code" => request("barCode"),
  206. "fm_station_id" => $station->id,
  207. "material_box_id" => $boxId,
  208. "commodity_id" => request("commodity_id"),
  209. "amount" => request("amount"),
  210. "type" => "入库",
  211. "user_id" => Auth::id(),
  212. "mark" => 1
  213. ]);
  214. DB::commit();
  215. //亮灯
  216. app("CacheShelfService")->stationLightUp($station->code,null,'2');
  217. $this->success();
  218. }
  219. /**
  220. * 溢出校正
  221. */
  222. public function overflowRevision()
  223. {
  224. $this->gate("入库管理-入库-半箱补货入库");
  225. $station = request("station");
  226. $amount = request("amount");
  227. //获取目标库位
  228. $station = app("StationService")->getMirrorMappingLocation($station);
  229. if (!$station)$this->error("未知库位");
  230. $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)->where("amount",">",$amount)
  231. ->where("type","入库")->where("mark",1)->where("status",0)->first();
  232. if (!$task)$this->error("无任务存在");
  233. $task->update(["amount" => DB::raw("amount - {$amount}")]);
  234. $storage = Storage::query()->where("material_box_id",$task->material_box_id)->first();
  235. $maximum = (($storage->amount ?? 0)+$task->amount)-$amount;
  236. CommodityMaterialBoxModel::query()->where("material_box_model_id",$task->materialBox->material_box_model_id)
  237. ->where("commodity_id",$task->commodity_id)->update(["maximum"=>$maximum]);
  238. $this->success("校正成功");
  239. }
  240. public function syncStorage()
  241. {
  242. ini_set('max_execution_time', 0);
  243. $model = MaterialBoxModel::query()->create([
  244. "code" => "common"
  245. ]);
  246. $sql = <<<sql
  247. select * from INV_LOT_LOC_ID where traceid = '*' and locationid like 'IDE%'
  248. sql;
  249. DB::beginTransaction();
  250. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $inv){
  251. $materialBox = MaterialBox::query()->firstOrCreate(["code"=>$inv->locationid],[
  252. "code" => $inv->locationid,
  253. "material_box_model_id"=>$model
  254. ]);
  255. $owner = Owner::query()->firstOrCreate([
  256. "code" => $inv->customerid
  257. ],[
  258. "code" => $inv->customerid,
  259. "name" => $inv->customerid,
  260. ]);
  261. $commodity = Commodity::query()->where("owner_id",$owner->id)->where("sku",$inv->sku)->first();
  262. $s = \App\Storage::query()->where("material_box_id",$materialBox->id)
  263. ->where("commodity_id",$commodity->id)->first();
  264. if (!$s)\App\Storage::query()->create([
  265. "material_box_id" => $materialBox->id,
  266. "commodity_id" => $commodity->id,
  267. "amount" => $inv->qty,
  268. ]);else $s->update(["amount" => $inv->qty]);
  269. }
  270. DB::commit();
  271. $this->success();
  272. }
  273. /**
  274. * 在安卓端的个体登录鉴权
  275. *
  276. */
  277. public function androidLogin()
  278. {
  279. $errors=Validator::make(request()->input(),[
  280. "name" => 'required|string',
  281. 'password' => 'required|string'])->errors();
  282. if($errors->count()>0){return ['success'=>false,'errors'=>$errors];}
  283. request()->offsetSet("remember",true);
  284. if (!$this->attemptLogin(request()))return ['success'=>false,'errors'=>['name'=>['登录信息验证失败']]];
  285. if (!Gate::allows("入库管理-入库-缓存架入库") && !Gate::allows("入库管理-入库-半箱补货入库"))return ['success'=>false,'errors'=>['name'=>['用户无权操作入库']]];
  286. return ['success'=>true,'url'=>url("store/inStorage/android.index")];
  287. }
  288. public function username(): string
  289. {
  290. return 'name';
  291. }
  292. }