StorageController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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\Services\LogService;
  11. use App\Station;
  12. use App\StationTask;
  13. use App\StationTaskMaterialBox;
  14. use App\Store;
  15. use App\StoreItem;
  16. use App\TaskTransaction;
  17. use Illuminate\Database\Eloquent\Builder;
  18. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  19. use Illuminate\Support\Collection;
  20. use Illuminate\Support\Facades\Auth;
  21. use Illuminate\Support\Facades\Cache;
  22. use Illuminate\Support\Facades\DB;
  23. use Illuminate\Support\Facades\Gate;
  24. use Illuminate\Support\Facades\Validator;
  25. class StorageController extends Controller
  26. {
  27. use AsyncResponse;
  28. use AuthenticatesUsers;
  29. public function putShelf()
  30. {
  31. $this->error("停用缓存架上架,联系管理员开启");
  32. $this->gate("入库管理-入库-缓存架入库");
  33. $asn = \request("asn");
  34. $ide = \request("ide");
  35. $barCode = \request("barCode");
  36. $amount = (int)\request("amount");
  37. //check info
  38. if (!$asn || !$ide || !$barCode || !$amount)$this->error("信息不完整");
  39. $box = MaterialBox::query()->with("station")->where("code",$ide)->first();
  40. if (!$box)$this->error("WAS无此料箱");
  41. if (!$box->station)$this->error("WAS无此库位信息");
  42. if (!app("StationService")->isCacheShelfLocation($box->station))$this->error("非缓存架库位");
  43. $fromLocation = $box->station->code; //源库位
  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. if (!$item)$this->error("WAS无此ASN单信息");
  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( $box->id, $item->commodity_id, $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")->lightUp($fromLocation,'1','0',["detail01"=>$ide,"qty01"=>$amount]);
  73. $maximum = app("CommodityMaterialBoxModelService")->getMaximum($box->material_box_model_id,$item->commodity_id);
  74. $this->success(["model" => $box->material_box_model_id,"commodity" => $item->commodity_id, "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. $track = request("track");
  88. $barCode = request("barCode");
  89. $lotNum = request("lotNum");
  90. $sql = <<<SQL
  91. SELECT * FROM INV_LOT_ATT WHERE LOTNUM = '{$lotNum}'
  92. SQL;
  93. $lot = DB::connection("oracle")->selectOne(DB::raw($sql));
  94. if (!$lot)$this->error("批次丢失");
  95. $commodity = Commodity::query()->whereHas("owner",function ($query)use($lot){
  96. $query->where("code",$lot->customerid);
  97. })->where("sku",$lot->sku)->first();
  98. if (!$commodity)$this->error("WAS无此商品信息");
  99. $models = CommodityMaterialBoxModel::query()->where("commodity_id",$commodity->id)->get();
  100. if ($models->count()==0)$this->success(["material_box_model_id"=>null]);
  101. $map = [];
  102. foreach ($models as $model)$map[$model->material_box_model_id] = $model;
  103. $models = app("MaterialBoxModelService")->getModelSortedByOwner($commodity->owner_id);
  104. $m = null;
  105. foreach ($models as $model){
  106. if (!isset($map[$model->id]))continue;
  107. $model->maximum = $map[$model->id]->maximum;
  108. $boxes = app("MaterialBoxService")->getModelAvailableBox($model->id);
  109. if ($boxes->count()==0)continue;
  110. if (!$m)$m = $model;
  111. $boxCodes = "";
  112. foreach ($boxes as $box){
  113. $boxCodes .= "'".$box->code."',";
  114. }
  115. $boxCodes = rtrim($boxCodes,",");
  116. $sql = <<<SQL
  117. SELECT LOCATIONID,({$model->maximum}-QTY) AS QTY FROM INV_LOT_LOC_ID WHERE LOTNUM = '{$lotNum}'
  118. AND LOCATIONID IN ({$boxCodes}) AND TRACEID = '*' AND {$model->maximum}-QTY > 0 ORDER BY (CASE QTY WHEN 0 THEN 1 ELSE 0 END),{$model->maximum}-QTY
  119. SQL;
  120. $res = DB::connection("oracle")->selectOne(DB::raw($sql));
  121. if ($res){
  122. $m = $model;
  123. $m->maximum = $res->qty;
  124. break;
  125. }
  126. }
  127. if (!$m)$this->error("没有可用料箱");
  128. $this->success(["need"=>$m->maximum,"material_box_model_id"=>$m->id,"commodity_id"=>$commodity->id]);
  129. }
  130. /**
  131. * 检查ASN可上架总数
  132. */
  133. public function checkAsnAmount()
  134. {
  135. $sql = <<<sql
  136. 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
  137. LEFT JOIN TSK_TASKLISTS ON DOC_ASN_DETAILS.ASNNO = TSK_TASKLISTS.DOCNO AND DOC_ASN_DETAILS.ASNLINENO = TSK_TASKLISTS.DOCLINENO
  138. WHERE ASNNO = ? AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?)
  139. AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
  140. sql;
  141. $task = DB::connection("oracle")->selectOne(DB::raw($sql),[request("asn"),request("barCode"),request("barCode"),request("barCode")]);
  142. $this->success($task->amount ?? 0);
  143. }
  144. /**
  145. * 重置缓存架指定格口
  146. */
  147. public function resetCacheShelf()
  148. {
  149. $this->gate("入库管理-入库-缓存架入库");
  150. $boxes = request("boxes");
  151. //清理任务
  152. $data = '';
  153. //剔除准备执行任务但没有真正开始执行的待定库位
  154. $occupy = Station::query()->select("id")->whereIn("code",$boxes)->where("status",1)->get();
  155. foreach ($occupy as $item){
  156. unset($boxes[array_search($item->code,$boxes)]);
  157. $data .= '“'.$item->code.'”,';
  158. }
  159. if ($occupy->count()>0){
  160. $data .= "存在任务待处理,无法调取 ";
  161. $boxes = array_values($boxes);
  162. }
  163. //剔除存在任务的库位
  164. $tasks = StationTaskMaterialBox::query()->with("station:id,code")
  165. ->whereIn("station_id",Station::query()->select("id")->whereIn("code",$boxes))
  166. ->whereNotIn("status",["完成","取消"])->get();
  167. foreach ($tasks as $task){
  168. unset($boxes[array_search($task->station->code,$boxes)]);
  169. $data .= '“'.$task->station->code.'”,';
  170. };
  171. if ($tasks->count()>0){
  172. $data .= "任务排队中,无法调取";
  173. $boxes = array_values($boxes);
  174. }
  175. //重新调取料箱
  176. $result = app("ForeignHaiRoboticsService")->paddingCacheShelf(Station::query()->whereIn("code",$boxes)->get());
  177. if ($result===null)$this->error("任务下发错误,检查日志");
  178. if ($result===false)$this->error("已无可用料箱,部分库位填充失败");
  179. $this->success(["data"=>$data,"boxes"=>$boxes]);
  180. }
  181. /**
  182. * 取得料箱
  183. */
  184. public function acquireBox()
  185. {
  186. $this->gate("入库管理-入库-半箱补货入库");
  187. $boxId = request("material_box_id");
  188. $modelId = request("material_box_model_id");
  189. if (!$modelId)$this->error("商品未设定库存上限");
  190. //获取目标库位
  191. $station = app("StationService")->getMirrorMappingLocation(request("station"));
  192. if (!$station)$this->error("未知库位");
  193. $isAvailable = app("StationService")->isAvailable($station);
  194. if (!$isAvailable)$this->error("库存被占用或存在任务未处理");
  195. $amount = app("StorageService")->checkPutAmount(request("asn"),request("barCode"));
  196. if ($amount<request("amount"))$this->error("待上架数量不足,最大可上数量为{$amount}");
  197. //发起取箱任务
  198. $exe = function ($boxId)use($station){
  199. DB::beginTransaction();
  200. $collection = new Collection();
  201. $task = StationTask::query()->create([
  202. 'status' => "待处理",
  203. 'station_id' => $station->id,
  204. ]);
  205. $collection->add(StationTaskMaterialBox::query()->create([
  206. 'station_id' => $station->id,
  207. 'material_box_id'=>$boxId,
  208. 'status'=>"待处理",
  209. 'type' => '取',
  210. 'station_task_id' => $task->id,
  211. ]));
  212. if (!app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','立架出至缓存架'))$this->error("呼叫机器人失败");
  213. //生成临时任务事务
  214. TaskTransaction::query()->create([
  215. "doc_code" => request("asn"),
  216. "bar_code" => request("barCode"),
  217. "fm_station_id" => $station->id,
  218. "material_box_id" => $boxId,
  219. "commodity_id" => request("commodity_id"),
  220. "amount" => request("amount"),
  221. "type" => "入库",
  222. "user_id" => Auth::id(),
  223. "mark" => 1
  224. ]);
  225. DB::commit();
  226. //亮灯
  227. app("CacheShelfService")->lightUp($station->code,'3','2');
  228. app("StationService")->locationOccupy($station->code);//占用库位
  229. Cache::forever("CACHE_SHELF_OCCUPANCY_{$station->id}",true);
  230. };
  231. if ($boxId && app("MaterialBoxService")->checkUsableBox($boxId))$this->success($exe($boxId));
  232. $item = app("StoreItemService")->getMaxAvailableDetail(request("asn"),request("barCode"));
  233. if (!$item)$this->error("无此单据记录");
  234. $models = app("MaterialBoxModelService")->getModelSortedByOwner($item->store->owner_id);
  235. $models->load(["commodity"=>function($query)use($item){
  236. $query->where("commodity_id",$item->commodity_id);
  237. }]);
  238. //获取料箱
  239. if ($boxId){
  240. $blacklist = [$boxId];
  241. $boxId = null;
  242. //料箱存在且不可用
  243. /** @var \Illuminate\Database\Eloquent\Collection $models */
  244. foreach ($models as $model){
  245. if (!$model->commodity)continue;
  246. //料箱不可用寻找新料箱
  247. $result = app("StorageService")->getHalfBoxLocation($model->commodity,$item,request("asn"),$blacklist);
  248. while ($result){
  249. //料箱可用并且数量符合本次半箱数量 跳出
  250. if (app("MaterialBoxService")->checkUsableBox($result->material_box_id)
  251. && $model->maximum-$result->amount>=request("amount"))$this->success($exe($result->material_box_id));
  252. else $blacklist[] = $result->material_box_id;
  253. //否则黑名单此料箱继续查找 直至料箱为空
  254. $result = app("StorageService")->getHalfBoxLocation($model->commodity,$item,request("asn"),$blacklist);
  255. }
  256. }
  257. }
  258. //料箱不存在且该商品有过入库记录 拿取空箱
  259. if (!$modelId){
  260. $box = null;
  261. foreach ($models as $model){
  262. if (!$model->commodity)continue;
  263. $box = app("MaterialBoxService")->getAnEmptyBox($model);
  264. if($box)break;
  265. }
  266. }else $box = app("MaterialBoxService")->getAnEmptyBox(MaterialBoxModel::query()->find($modelId));
  267. if (!$box)$this->error("无可用料箱");
  268. $this->success($exe($box->id));
  269. }
  270. /**
  271. * 溢出校正
  272. */
  273. public function overflowRevision()
  274. {
  275. $this->gate("入库管理-入库-半箱补货入库");
  276. $station = request("station");
  277. $amount = request("amount");
  278. //获取目标库位
  279. $station = app("StationService")->getMirrorMappingLocation($station);
  280. if (!$station)$this->error("未知库位");
  281. $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)->where("amount",">",$amount)
  282. ->where("type","入库")->where("mark",1)->where("status",0)->first();
  283. if (!$task)$this->error("无任务存在");
  284. $task->update(["amount" => DB::raw("amount - {$amount}")]);
  285. $storage = MaterialBoxCommodity::query()->where("material_box_id",$task->material_box_id)
  286. ->where("commodity_id",$task->commodity_id)->first();
  287. $maximum = (($storage->amount ?? 0)+$task->amount)-$amount;
  288. CommodityMaterialBoxModel::query()->where("material_box_model_id",$task->materialBox->material_box_model_id)
  289. ->where("commodity_id",$task->commodity_id)->update(["maximum"=>$maximum]);
  290. $this->success("校正成功");
  291. }
  292. public function syncStorage()
  293. {
  294. ini_set('max_execution_time', 0);
  295. $model = MaterialBoxModel::query()->create([
  296. "code" => "common"
  297. ]);
  298. $sql = <<<sql
  299. select * from INV_LOT_LOC_ID where traceid = '*' and locationid like 'IDE%'
  300. sql;
  301. DB::beginTransaction();
  302. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $inv){
  303. $materialBox = MaterialBox::query()->firstOrCreate(["code"=>$inv->locationid],[
  304. "code" => $inv->locationid,
  305. "material_box_model_id"=>$model
  306. ]);
  307. $owner = Owner::query()->firstOrCreate([
  308. "code" => $inv->customerid
  309. ],[
  310. "code" => $inv->customerid,
  311. "name" => $inv->customerid,
  312. ]);
  313. $commodity = Commodity::query()->where("owner_id",$owner->id)->where("sku",$inv->sku)->first();
  314. $s = MaterialBoxCommodity::query()->where("material_box_id",$materialBox->id)
  315. ->where("commodity_id",$commodity->id)->first();
  316. if (!$s)MaterialBoxCommodity::query()->create([
  317. "material_box_id" => $materialBox->id,
  318. "commodity_id" => $commodity->id,
  319. "amount" => $inv->qty,
  320. ]);else $s->update(["amount" => $inv->qty]);
  321. }
  322. DB::commit();
  323. $this->success();
  324. }
  325. /**
  326. * 在安卓端的个体登录鉴权
  327. *
  328. */
  329. public function androidLogin()
  330. {
  331. $errors=Validator::make(request()->input(),[
  332. "name" => 'required|string',
  333. 'password' => 'required|string'])->errors();
  334. if($errors->count()>0){return ['success'=>false,'errors'=>$errors];}
  335. request()->offsetSet("remember",true);
  336. if (!$this->attemptLogin(request()))return ['success'=>false,'errors'=>['name'=>['登录信息验证失败']]];
  337. if (!Gate::allows("入库管理-入库-缓存架入库") && !Gate::allows("入库管理-入库-半箱补货入库"))return ['success'=>false,'errors'=>['name'=>['用户无权操作入库']]];
  338. return ['success'=>true,'url'=>url("store/inStorage/android.index")];
  339. }
  340. public function username(): string
  341. {
  342. return 'name';
  343. }
  344. /**
  345. * 库外箱绑定至库位
  346. */
  347. public function bindBox()
  348. {
  349. $location = request("location");
  350. $box = request("ide");
  351. if (!$location || !$box)$this->error("参数传递错误");
  352. $task = StationTaskMaterialBox::query()->select("id")->where(function ($query)use($location,$box){
  353. /** @var Builder $query */
  354. $query->whereHas("station",function ($query)use($location){
  355. /** @var Builder $query */
  356. $query->where("code",$location);
  357. })->orWhereHas("materialBox",function ($query)use($box){
  358. /** @var Builder $query */
  359. $query->where("code",$box);
  360. });
  361. })->whereNotIn("status",["完成","取消"])->first();
  362. if ($task)$this->error("库位或料箱存在任务待执行,无法放置料箱");
  363. $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select(DB::raw("1"))
  364. ->where("ks_bin_code",$box)->where("status",4)->first();
  365. if (!$ks)$this->error("海柔料箱状态异常");
  366. DB::beginTransaction();
  367. try {
  368. $station = Station::query()->where("code",$location)->where("status",0)->first();
  369. $box = MaterialBox::query()->where("code",$box)->first();
  370. if (!$station || !$box)$this->error("库位或料箱未在WAS记录");
  371. $station->update(["material_box_id"=>$box->id]);
  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. public function commodityBindModel()
  396. {
  397. $models = MaterialBoxModel::query()->get();
  398. return view("store.inStorage.commodityBindModel",compact("models"));
  399. }
  400. public function getModels()
  401. {
  402. $this->success(MaterialBoxModel::query()->select("id","code","description","maximum_kind")->get());
  403. }
  404. /**
  405. * 设定料箱最大
  406. */
  407. public function settingModelMaximum()
  408. {
  409. $insert = function ($commodityId){
  410. $in = [];
  411. $map = [];
  412. $ids = [];
  413. foreach (request("models") as $model){
  414. $in[] = [
  415. "commodity_id" => $commodityId,
  416. "material_box_model_id" => $model["id"],
  417. "maximum" => $model["maximum"]
  418. ];
  419. $map[$model["id"]] = $model;
  420. $ids[] = $model["id"];
  421. }
  422. return array($in,$map,$ids);
  423. };
  424. if (request("commodityId")){
  425. list($in,$map,$ids) = $insert(request("commodityId"));
  426. if ($ids){
  427. $map = array_flip($ids);
  428. foreach (CommodityMaterialBoxModel::query()->where("commodity_id",request("commodityId"))
  429. ->lockForUpdate()->get() as $model){
  430. $index = $map[$model->material_box_model_id];
  431. if ($model->maximum != $in[$index]["maximum"]){
  432. CommodityMaterialBoxModel::query()->where("commodity_id",request("commodityId"))
  433. ->where("material_box_model_id",$model->material_box_model_id)
  434. ->update(["maximum"=>$in[$index]["maximum"]]);
  435. }
  436. unset($in[$index]);
  437. }
  438. $in = array_values($in);
  439. }
  440. if ($in){
  441. LogService::log(__CLASS__,__METHOD__,json_encode($in));
  442. CommodityMaterialBoxModel::query()->insert($in);
  443. }
  444. $this->success();
  445. }
  446. $item = StoreItem::query()->whereHas("store",function (Builder $query){
  447. $query->where("asn_code",request("asn"));
  448. })->whereHas("commodity",function (Builder $query){
  449. $query->whereHas("barcodes",function (Builder $query){
  450. $query->where("code",request("barCode"));
  451. });
  452. })->first();
  453. if (!$item)$this->error("商品不存在");
  454. list($in,$map,$ids) = $insert($item->store->commodity_id);
  455. LogService::log(__CLASS__,__METHOD__,json_encode($in));
  456. CommodityMaterialBoxModel::query()->insert($in);
  457. $this->getMaterBoxModel($item,$map);
  458. }
  459. private function getMaterBoxModel($commodity,$map)
  460. {
  461. $models = app("MaterialBoxModelService")->getModelSortedByOwner($commodity->owner_id);
  462. foreach ($models as $model){
  463. if (!isset($map[$model->id]))continue;
  464. //$result = app("StorageService")->getHalfBoxLocation($map[$model->id],$item,$item->store->asn_code);
  465. $result = app("StorageService")->getMaxAvailableHalfBoxLocation($map[$model->id],$item,$item->store->asn_code);
  466. if (!$result)continue;
  467. $result->maximum = $model->maximum-$result->amount;
  468. $this->success($result);
  469. }
  470. $this->success(["need"=>$models[0]->maximum,"material_box_model_id"=>$models[0]->material_box_model_id,"commodity_id"=>$commodity->id]);
  471. }
  472. /**
  473. * 根据条码检索商品
  474. */
  475. public function searchBarCode()
  476. {
  477. $commodities = Commodity::query()->with("owner")->whereHas("barcodes",function (Builder $query){
  478. $query->where("code",request("barCode"));
  479. })->get();
  480. if (!$commodities->count())$this->error("库内无此商品信息");
  481. if ($commodities->count()>1)$commodities->load("owner");
  482. $this->success($commodities);
  483. }
  484. /**
  485. * 根据商品ID检索已存在的上限设定
  486. */
  487. public function searchModel()
  488. {
  489. $mapping = [];
  490. foreach (CommodityMaterialBoxModel::query()->where("commodity_id",request("id"))->get() as $model){
  491. $mapping[$model->material_box_model_id] = $model->maximum;
  492. }
  493. $this->success($mapping);
  494. }
  495. /**
  496. * 根据ASN号检索未设定商品型号上限的信息
  497. */
  498. public function searchAsn()
  499. {
  500. $sql = <<<SQL
  501. SELECT CUSTOMERID,SKU FROM TSK_TASKLISTS WHERE DOCNO = ? AND TASKPROCESS = '00' AND TASKTYPE = 'PA' GROUP BY CUSTOMERID,SKU
  502. SQL;
  503. $store = Store::query()->select("owner_id")->where("asn_code",request("asn"))->first();
  504. if (!$store || !$store->owner_id)$this->error("WAS内无此ASN单信息");
  505. $tasks = DB::connection("oracle")->select(DB::raw($sql),[request("asn")]);
  506. $codes = array_column($tasks,"sku");
  507. $model = MaterialBoxModel::query()->where("maximum_kind",1)->first();
  508. if (!$model)$this->error("单品型号缺失");
  509. $commodities = Commodity::query()->with("model")->where("owner_id",$store->owner_id)
  510. ->whereIn("sku",$codes)->get()->toArray();
  511. if (count($codes)!=count($commodities))$this->error("WAS商品信息不全");
  512. foreach ($commodities as $index=>$commodity){
  513. if ($commodity->model)unset($commodities[$index]);
  514. }
  515. $this->success(["commodities"=>array_values($commodities),"model"=>$model]);
  516. }
  517. /**
  518. * 设置商品上限
  519. */
  520. public function settingCommodityMaximum()
  521. {
  522. $modelId = request("modelId");
  523. $insert = [];
  524. $date = date("Y-m-d H:i:s");
  525. foreach (request("commodities") as $commodity){
  526. $insert[] = [
  527. "commodity_id" => $commodity["id"],
  528. "material_box_model_id" => $modelId,
  529. "maximum" => $commodity->maximum,
  530. "created_at" => $date,
  531. "updated_at" => $date,
  532. ];
  533. }
  534. $this->success(CommodityMaterialBoxModel::query()->insert($insert));
  535. }
  536. /**
  537. * 检查任务获取多批次
  538. */
  539. public function checkTask()
  540. {
  541. $track = request("track");
  542. $barCode = request("barCode");
  543. $sql = <<<SQL
  544. SELECT TSK_TASKLISTS.FMLOTNUM FROM TSK_TASKLISTS LEFT JOIN BAS_SKU ON TSK_TASKLISTS.CUSTOMERID = BAS_SKU.CUSTOMERID
  545. AND TSK_TASKLISTS.SKU = BAS_SKU.SKU
  546. WHERE FMID = ? AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?)
  547. AND TASKPROCESS = '00' AND TASKTYPE = 'PA' GROUP BY FMID,FMLOTNUM
  548. SQL;
  549. $tasks = DB::connection("oracle")->select(DB::raw($sql),[$track,$barCode,$barCode,$barCode]);
  550. $result = ["count"=>count($tasks)];
  551. if (count($tasks)>1){
  552. $lotNums = "";
  553. foreach ($tasks as $task)$lotNums .= "'".$task->fmlotnum."',";
  554. $lotNums = rtrim($lotNums,",");
  555. $sql = <<<SQL
  556. SELECT * FROM INV_LOT_ATT WHERE LOTNUM IN ({$lotNums});
  557. SQL;
  558. $result["lots"] = DB::connection("oracle")->select(DB::raw($sql));
  559. }
  560. if (count($tasks)==1)$result["lots"] = $tasks[0]->fmlotnum;
  561. $this->success($result);
  562. }
  563. }