StorageController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CommodityMaterialBoxModel;
  4. use App\Components\AsyncResponse;
  5. use App\MaterialBox;
  6. use App\Station;
  7. use App\StationTask;
  8. use App\StationTaskMaterialBox;
  9. use App\Storage;
  10. use App\StoreItem;
  11. use App\TaskTransaction;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\DB;
  16. class StorageController extends Controller
  17. {
  18. use AsyncResponse;
  19. public function putShelf()
  20. {
  21. $asn = \request("asn");
  22. $ide = \request("ide");
  23. $barCode = \request("barCode");
  24. $amount = (int)\request("amount");
  25. //check info
  26. if (!$asn || !$ide || !$barCode || !$amount)$this->error("信息不完整");
  27. $fromLocation = app("MaterialBoxService")->getBoxLocation($ide);
  28. if (!$fromLocation)$this->error("海柔无此库位信息");
  29. $box = MaterialBox::query()->select("id")->where("code",$ide)->first();
  30. if (!$box)$this->error("WAS无此料箱");
  31. //库存相关信息
  32. /** @var Station|\stdClass $station */
  33. $station = Station::query()->firstOrCreate(["code"=>$fromLocation],[
  34. 'name' => $fromLocation, 'code' => $fromLocation, 'station_type_id' => 5
  35. ]);
  36. $item = StoreItem::query()->whereHas("store",function (Builder $query){
  37. $query->where("asn_code",request("asn"));
  38. })->whereHas("commodity",function (Builder $query)use($barCode){
  39. $query->whereHas("barcodes",function (Builder $query)use($barCode){
  40. $query->where("code",$barCode);
  41. });
  42. })->first();
  43. //get flux
  44. $asns = app("StorageService")->getFluxTask($asn,$barCode,$amount);
  45. if (!$asns)$this->error("ASN不存在或上架数量与入库数量不符");
  46. //此处嵌套三层事务 以最高层级为准
  47. DB::beginTransaction(); //总体事务 回滚WAS错误操作
  48. try{
  49. //if (!app("StorageService")->putWareHousing($fromLocation,$box->id))$this->error("错误库位或库位已被下达任务");
  50. //库存记录
  51. if (!app("StorageService")->enterWarehouse($station->id, $box->id, $item->commodity_id ?? null, $amount, $box->material_box_model_id))$this->error("库存异常");
  52. DB::connection("oracle")->transaction(function ()use($asns,$ide){ //单体嵌套事务 回滚FLUX失败任务
  53. foreach ($asns as $asn)if (!app("StorageService")->fluxPA($asn,$ide,(int)$asn->fmqty)){
  54. DB::connection("oracle")->rollBack();
  55. $this->error("FLUX上架失败");
  56. };
  57. });
  58. DB::commit();
  59. }catch (\Exception $e){
  60. DB::rollBack();
  61. $this->error($e->getMessage());
  62. }
  63. //亮灯
  64. app("CacheShelfService")->_stationCacheLightOn($fromLocation,$ide);
  65. $maximum = app("CommodityMaterialBoxModelService")->getMaximum($box->material_box_model_id,$item->commodity_id ?? null);
  66. if ($maximum && $maximum<$amount)app("CommodityMaterialBoxModelService")->setMaximum($box->material_box_model_id,$item->commodity_id ?? null,$amount);
  67. $this->success(["model" => $box->material_box_model_id,"commodity" => $item->commodity_id ?? null, "maximum" => $maximum]);
  68. }
  69. public function setMaximum()
  70. {
  71. app("CommodityMaterialBoxModelService")->setMaximum(request("model"),request("commodity"),request("maximum"));
  72. $this->success();
  73. }
  74. /**
  75. * 检查最大限值并返回
  76. *
  77. */
  78. public function checkMaximum()
  79. {
  80. $item = StoreItem::query()->whereHas("store",function (Builder $query){
  81. $query->where("asn_code",request("asn"));
  82. })->whereHas("commodity",function (Builder $query){
  83. $query->whereHas("barcodes",function (Builder $query){
  84. $query->where("code",request("barCode"));
  85. });
  86. })->first();
  87. if (!$item)$this->error("WAS无此单据记录");
  88. $models = CommodityMaterialBoxModel::query()->where("commodity_id",$item->commodity_id)->get();
  89. if ($models->count()==0)$this->error("商品首入,请使用缓存架空箱入库");
  90. foreach ($models as $model){
  91. $stationCodes = '';//拼接库位编码
  92. $map = [];//库位与库存映射
  93. //查询填充
  94. Storage::query()->with("station")->whereHas("materialBox",function (Builder $query)use($model){
  95. $query->where("material_box_model_id",$model->material_box_model_id);
  96. })->where("commodity_id",$model->commodity_id)->where("amount","<",$model->maximum)
  97. ->where("status",0)->where(DB::raw("{$model->maximum}-amount"),">",0)->get()
  98. ->each(function ($storage)use(&$stationCodes,&$map){
  99. $stationCodes .= "'".$storage->station->code."',";
  100. $map[$storage->station->code] = $storage;
  101. });
  102. //不存在跳出
  103. if (!$stationCodes)continue;
  104. $stationCodes = mb_substr($stationCodes,0,-1);
  105. //查询对应asn detail
  106. $detail = DB::connection("oracle")->selectOne(DB::raw("SELECT * FROM DOC_ASN_DETAILS WHERE ASNNO = ? AND ASNLINENO = ?"),[
  107. request("asn"),$item->asn_line_code
  108. ]);
  109. if($detail)continue;
  110. $detail = get_object_vars($detail);
  111. //查询对应批次属性
  112. $lot = DB::connection("oracle")->selectOne(DB::raw("SELECT * FROM BAS_LOTID WHERE LOTID = (SELECT LOTID FROM BAS_SKU WHERE CUSTOMERID = ? AND SKU = ?)"),[
  113. $detail["customerid"],$detail["sku"]
  114. ]);
  115. if($lot)continue;
  116. //通过符合条件的批次号来查询 库存
  117. $lot = get_object_vars($lot);
  118. $sql = <<<sql
  119. SELECT * FROM INV_LOT_LOC_ID WHERE LOTNUM IN
  120. (SELECT LOTNUM FROM INV_LOT_ATT WHERE INV_LOT_ATT.CUSTOMERID = ? AND SKU = ?
  121. sql;
  122. //拼接可以合并的批次属性要求
  123. for ($i=1;$i<=8;$i++){
  124. if ($lot["lotkey0{$i}"]=='Y'){
  125. $val = $detail["lotatt0{$i}"] ? "'{$detail["lotatt0{$i}"]}'" : null;
  126. $sql .= " AND LOTATT0{$i} = $val";
  127. }
  128. }
  129. $sql .= ") AND LOCATIONID IN ({$stationCodes}) AND TRACEID = '*' AND {$model->maximum}-QTY > 0 ORDER BY {$model->maximum}-QTY";
  130. $res = DB::connection("oracle")->selectOne(DB::raw($sql),[
  131. $detail["customerid"],$detail["sku"]
  132. ]);
  133. if ($res)$this->success($map[$res->locationid]);
  134. }
  135. $this->success(["need"=>$models[0]->maximum,"material_box_model_id"=>$models[0]->material_box_model_id,"commodity_id"=>$item->commodity_id]);
  136. }
  137. /**
  138. * 重置缓存架指定格口
  139. */
  140. public function resetCacheShelf()
  141. {
  142. $boxes = request("boxes");
  143. //清理任务
  144. $data = '';
  145. $occupy = Storage::query()->with("station:id,code")->whereIn("station_id",Station::query()->select("id")->whereIn("code",$boxes))->where("status",1)->get();
  146. foreach ($occupy as $item){
  147. unset($boxes[array_search($item->station->code,$boxes)]);
  148. $data .= '“'.$item->station->code.'”,';
  149. }
  150. if ($occupy->count()>0){
  151. $data .= "存在任务待处理,无法调取";
  152. $boxes = array_values($boxes);
  153. }
  154. //重新调取料箱
  155. $result = app("ForeignHaiRoboticsService")->paddingCacheShelf(Station::query()->whereIn("code",$boxes)->get());
  156. if ($result===null)$this->error("任务下发错误,检查日志");
  157. if ($result===false)$this->error("已无可用料箱,部分库位填充失败");
  158. $this->success(["data"=>$data,"boxes"=>$boxes]);
  159. }
  160. /**
  161. * 取得料箱
  162. */
  163. public function acquireBox()
  164. {
  165. $boxId = request("material_box_id");
  166. $modelId = request("material_box_model_id");
  167. //获取目标库位
  168. $station = app("StationService")->getMirrorMappingLocation(request("station"));
  169. if (!$station)$this->error("未知库位");
  170. //获取料箱
  171. if ($boxId && !app("MaterialBoxService")->checkUsableBox($boxId)){
  172. $boxId = null;
  173. //料箱存在且不可用
  174. $models = CommodityMaterialBoxModel::query()->where("commodity_id",request("commodity_id"))->get();
  175. foreach ($models as $model){
  176. Storage::query()->select("material_box_id")
  177. ->whereHas("materialBox",function (Builder $query)use($model){
  178. $query->where("material_box_model_id",$model->material_box_model_id);
  179. })->where("commodity_id",$model->commodity_id)->where("amount","<",$model->maximum)
  180. ->where("status",0)->where(DB::raw("{$model->maximum}-amount"),">=",request("amount"))->get()->each(function ($box)use(&$boxId){
  181. if (app("MaterialBoxService")->checkUsableBox($box->id)){
  182. $boxId = $box->id;
  183. return false;
  184. }
  185. return true;
  186. });
  187. if ($boxId)break;
  188. }
  189. }
  190. if (!$boxId){
  191. if (!$modelId){
  192. $box = null;
  193. $models = CommodityMaterialBoxModel::query()->where("commodity_id",request("commodity_id"))->get();
  194. foreach ($models as $model){
  195. $box = app("MaterialBoxService")->getAnEmptyBox([],$model->material_box_model_id);
  196. if($box)break;
  197. }
  198. }else $box = app("MaterialBoxService")->getAnEmptyBox([],$modelId);
  199. if (!$box)$this->error("无可用料箱");
  200. $boxId = $box->id;
  201. }
  202. if (!$boxId || !app("MaterialBoxService")->checkUsableBox($boxId))$this->error("无可用料箱");
  203. //发起取箱任务
  204. DB::beginTransaction();
  205. $collection = new Collection();
  206. $task = StationTask::query()->create([
  207. 'status' => "待处理",
  208. 'station_id' => $station->id,
  209. ]);
  210. $collection->add(StationTaskMaterialBox::query()->create([
  211. 'station_id' => $station->id,
  212. 'material_box_id'=>$boxId,
  213. 'status'=>"待处理",
  214. 'type' => '取',
  215. 'station_task_id' => $task->id,
  216. ]));
  217. if (!app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','立架出至缓存架'))$this->error("呼叫机器人失败");
  218. //生成临时任务事务
  219. TaskTransaction::query()->create([
  220. "doc_code" => request("asn"),
  221. "bar_code" => request("barCode"),
  222. "fm_station_id" => $station->id,
  223. "material_box_id" => $boxId,
  224. "commodity_id" => request("commodity_id"),
  225. "amount" => request("amount"),
  226. "type" => "入库",
  227. "user_id" => Auth::id(),
  228. "mark" => 1
  229. ]);
  230. DB::commit();
  231. //亮灯
  232. app("CacheShelfService")->stationLightUp($station->code,null,'2');
  233. $this->success();
  234. }
  235. /**
  236. * 溢出校正
  237. */
  238. public function overflowRevision()
  239. {
  240. $station = request("station");
  241. $amount = request("amount");
  242. //获取目标库位
  243. $station = app("StationService")->getMirrorMappingLocation($station);
  244. if (!$station)$this->error("未知库位");
  245. $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)->where("amount",">",$amount)
  246. ->where("type","入库")->where("mark",1)->where("status",0)->first();
  247. if (!$task)$this->error("无任务存在");
  248. $task->update(["amount" => DB::raw("amount - {$amount}")]);
  249. $storage = Storage::query()->where("material_box_id",$task->material_box_id)->first();
  250. $maximum = (($storage->amount ?? 0)+$task->amount)-$amount;
  251. CommodityMaterialBoxModel::query()->where("material_box_model_id",$task->materialBox->material_box_model_id)
  252. ->where("commodity_id",$task->commodity_id)->update(["maximum"=>$maximum]);
  253. $this->success("校正成功");
  254. }
  255. }