StorageService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. namespace App\Services;
  3. use App\CommodityMaterialBoxModel;
  4. use App\Log;
  5. use App\Station;
  6. use App\StationTask;
  7. use App\StationTaskMaterialBox;
  8. use App\TaskTransaction;
  9. use App\Traits\ServiceAppAop;
  10. use App\Storage;
  11. use App\ValueStore;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Str;
  17. class StorageService
  18. {
  19. use ServiceAppAop;
  20. protected $modelClass=Storage::class;
  21. /**
  22. * 填充缓存架
  23. *
  24. * @param \Illuminate\Database\Eloquent\Collection $stations
  25. */
  26. public function paddingCacheShelf($stations)
  27. {
  28. $collection = new Collection();
  29. $stationCollection = new Collection();
  30. $blacklist = [];
  31. foreach ($stations as $station){
  32. $box = app("MaterialBoxService")->getAnEmptyBox($blacklist);
  33. if (!$box)continue;
  34. $task = StationTask::query()->create([
  35. 'status' => "待处理",
  36. 'station_id' => $station->id,
  37. ]);
  38. $collection->add(StationTaskMaterialBox::query()->create([
  39. 'station_id' => $station->id,
  40. 'material_box_id'=>$box->id,
  41. 'status'=>"待处理",
  42. 'type' => '取',
  43. 'station_task_id' => $task->id,
  44. ]));
  45. $stationCollection->add($station->code);
  46. $blacklist[] = $box->id;
  47. }
  48. app("ForeignHaiRoboticsService")->fetchGroup_multiLocation($stationCollection,$collection,'','立架出至缓存架');
  49. }
  50. /**
  51. * 缓存架放置记录
  52. *
  53. * @param StationTaskMaterialBox|\stdClass $stationTaskMaterialBox
  54. */
  55. public function putCacheShelf($stationTaskMaterialBox)
  56. {
  57. DB::beginTransaction();
  58. try{
  59. $storage = Storage::query()->where("material_box_id",$stationTaskMaterialBox->material_box_id)->lockForUpdate()->first();
  60. if ($storage)$storage->update(["station_id"=>$stationTaskMaterialBox->station_id,'status'=>0]);
  61. else Storage::query()->create([
  62. "station_id" => $stationTaskMaterialBox->station_id,
  63. "material_box_id" => $stationTaskMaterialBox->material_box_id,
  64. ]);
  65. $stationTaskMaterialBox->loadMissing("station");
  66. //清理原有任务
  67. app("StorageService")->clearTask([$stationTaskMaterialBox->station->code]);
  68. DB::commit();
  69. }catch (\Exception $e){
  70. DB::rollBack();
  71. }
  72. }
  73. /**
  74. * 释放库位占用
  75. *
  76. * @param StationTaskMaterialBox|\stdClass $stationTaskMaterialBox
  77. */
  78. public function releaseOccupation($stationTaskMaterialBox)
  79. {
  80. if ($stationTaskMaterialBox->station->station_type_id != 5)return;
  81. $storage = Storage::query()->where("material_box_id",$stationTaskMaterialBox->material_box_id)->first();
  82. if (!$storage)return;
  83. $update = [];
  84. if ($storage->status == 1)$update["status"] = 0;
  85. if ($storage->station_id)$update["station_id"] = null;
  86. if ($update)$storage->update($update);
  87. }
  88. /**
  89. * 检查临时事务标记处理一些特殊情况
  90. *
  91. * @param StationTaskMaterialBox|\stdClass $stationTaskMaterialBox
  92. */
  93. public function checkMark($stationTaskMaterialBox)
  94. {
  95. $task = TaskTransaction::query()->where("material_box_id",$stationTaskMaterialBox->material_box_id)
  96. ->where("status",0)->first();
  97. if (!$task)return;
  98. //蓝灯闪烁
  99. if ($task->type == '入库' && $task->mark == 1)app("CacheShelfService")->stationLightUp($stationTaskMaterialBox->station->code,null,'2','2');
  100. }
  101. /**
  102. * 检查存储 根据事务表做处理
  103. *
  104. * @param Station|\stdClass $station
  105. *
  106. * @return bool
  107. *
  108. * @throws
  109. */
  110. public function checkStorage($station)
  111. {
  112. $task = TaskTransaction::query()->with("materialBox")->where("fm_station_id",$station->id)
  113. ->where("status",0)->first();
  114. if (!$task)return true;
  115. //建立入库任务,通知入库,完善库存
  116. if ($task->type == '入库' && $task->mark == 1){
  117. DB::beginTransaction();
  118. try{
  119. //get flux
  120. $asns = $this->getFluxTask($task->doc_code,$task->bar_code,$task->amount);
  121. if (!$asns)return false;
  122. $ide = $task->materialBox->code;
  123. DB::connection("oracle")->beginTransaction();
  124. try{
  125. foreach ($asns as $asn)if (!$this->fluxPA($asn,$ide,(int)$asn->fmqty)){
  126. DB::connection("oracle")->rollBack();
  127. return false;
  128. };
  129. }catch(\Exception $e){
  130. DB::connection("oracle")->rollBack();
  131. return false;
  132. }
  133. $taskMaterialBox = $this->createWarehousingTask($station->id,$task->material_box_id);//建立入库任务
  134. if (!$this->enterWarehouse($station->id,$task->material_box_id,$task->commodity_id,$task->amount))throw new \Exception("库存异常"); //处理库存
  135. $task->update([
  136. "task_id" => $taskMaterialBox->id,
  137. "status" => 1,
  138. "user_id" => Auth::id(),
  139. ]);//标记事务完成
  140. $collection = new Collection([$taskMaterialBox]);
  141. app("ForeignHaiRoboticsService")->fetchGroup($station->code,$collection,'','缓存架入立架'); //呼叫机器人入库
  142. DB::commit();
  143. DB::connection("oracle")->commit();
  144. return true;
  145. }catch(\Exception $e){
  146. DB::rollBack();
  147. DB::connection("oracle")->rollBack();
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. /**
  154. * 建立入库任务
  155. *
  156. * @param $stationId
  157. * @param $boxId
  158. *
  159. * @return Model|\stdClass
  160. */
  161. public function createWarehousingTask($stationId,$boxId)
  162. {
  163. /** @var StationTask|\stdClass $task */
  164. $task = StationTask::query()->create([
  165. 'status' => "待处理",
  166. 'station_id' => $stationId,
  167. ]);
  168. return StationTaskMaterialBox::query()->create([
  169. 'station_id' => $stationId,
  170. 'material_box_id'=>$boxId,
  171. 'status'=>"待处理",
  172. 'type' => '放',
  173. 'station_task_id' => $task->id,
  174. ]);
  175. }
  176. /**
  177. * 库存入库
  178. *
  179. * @param integer $stationId
  180. * @param integer $boxId
  181. * @param integer $commodityId
  182. * @param integer $amount
  183. * @param integer $modelId
  184. *
  185. * @return bool
  186. */
  187. public function enterWarehouse($stationId, $boxId, $commodityId, $amount, $modelId = null)
  188. {
  189. DB::beginTransaction();
  190. try{
  191. $storage = Storage::query()->where("material_box_id",$boxId)->lockForUpdate()->first();
  192. $obj = [
  193. "station_id" => $stationId,
  194. "material_box_id" => $boxId,
  195. "commodity_id" => $commodityId,
  196. "amount" => $amount,
  197. "status" => 1,
  198. ];
  199. if ($storage){
  200. $amountTemp = (int)$storage->amount + (int)$amount;
  201. $obj["amount"] = DB::raw("amount+{$amount}");
  202. $storage->update($obj);
  203. $amount = $amountTemp;
  204. } else Storage::query()->create($obj);
  205. if ($commodityId && $modelId){
  206. //维护料箱最大上限 用于半箱补货
  207. $model = CommodityMaterialBoxModel::query()->select("maximum")->where("commodity_id",$commodityId)
  208. ->where("material_box_model_id",$modelId)->first();
  209. if (!$model)CommodityMaterialBoxModel::query()->create(["commodity_id"=>$commodityId,"material_box_model_id"=>$modelId,"maximum"=>$amount]);
  210. if ($model && $model->maximum < $amount)$model->update(["maximum"=>$amount]);
  211. }
  212. DB::commit();
  213. LogService::log(__CLASS__,"库存增加",$storage->toJson()." | ".json_encode([$stationId, $boxId, $commodityId, $amount, $modelId]));
  214. return true;
  215. }catch(\Exception $e){
  216. DB::rollBack();
  217. return false;
  218. }
  219. }
  220. /**
  221. * 获取FLUX上架任务列表
  222. *
  223. * @param string $asn
  224. * @param string $barCode
  225. * @param int $amount
  226. *
  227. * @return array|null
  228. */
  229. public function getFluxTask(string $asn,string $barCode,int $amount):array
  230. {
  231. $sql = <<<sql
  232. SELECT * FROM DOC_ASN_DETAILS LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  233. LEFT JOIN TSK_TASKLISTS ON DOC_ASN_DETAILS.ASNNO = TSK_TASKLISTS.DOCNO AND DOC_ASN_DETAILS.ASNLINENO = TSK_TASKLISTS.DOCLINENO
  234. WHERE ASNNO = ? AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?) AND RECEIVEDQTY >= ?
  235. AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
  236. sql;
  237. $asns = DB::connection("oracle")->select(DB::raw($sql),[$asn,$barCode,$barCode,$barCode,$amount]);
  238. if (!$asns)return [];
  239. $nums = [];
  240. $sum = 0;
  241. $maxIndex = null;
  242. foreach ($asns as $i => $asn){
  243. if ((int)$asn->fmqty == $amount)return [$asn];
  244. $nums[] = (int)$asn->fmqty;
  245. $sum += (int)$asn->fmqty;
  246. if ((int)$asn->fmqty>$amount)$maxIndex = $i;
  247. }
  248. if ($sum<$amount)return []; //上架数大于入库数
  249. $result = $this->getMatch($nums,$amount);
  250. if (!$result)return $this->splitTask($asns,$maxIndex,$amount);
  251. $arr = [];
  252. foreach ($result as $index)$arr[] = $asns[$index];
  253. return $arr;
  254. }
  255. /**
  256. * 拆分任务
  257. * @param array $asns
  258. * @param int|null $maxIndex
  259. * @param int $amount
  260. *
  261. * @return array
  262. */
  263. private function splitTask($asns,$maxIndex,$amount):array
  264. {
  265. $result = [];
  266. if ($maxIndex===null){
  267. foreach ($asns as $asn){
  268. if ($amount>(int)$asn->fmqty){
  269. $result[] = $asn;
  270. $amount-=(int)$asn->fmqty;
  271. }else $splitTarget = $asn;
  272. }
  273. }else $splitTarget = $asns[$maxIndex];
  274. $result[] = $this->copyTask($splitTarget,$amount);
  275. return $result;
  276. }
  277. /**
  278. * @param \stdClass $task
  279. * @param int $amount
  280. *
  281. * @return \stdClass
  282. *
  283. * @throws
  284. */
  285. private function copyTask($task,$amount)
  286. {
  287. DB::connection("oracle")->beginTransaction();
  288. try {
  289. $columns = '';
  290. $values = '';
  291. $seq = 0;
  292. foreach ($task as $key=>$val){
  293. if (Str::upper($key)=='TASKID_SEQUENCE') {
  294. $taskMax = DB::connection("oracle")->selectOne(DB::raw("select MAX(TASKID_SEQUENCE) maxseq from TSK_TASKLISTS where taskid = ?"),[$task->taskid]);
  295. $val = $taskMax->maxseq + 1;
  296. $seq = $val;
  297. }
  298. if (Str::upper($key)=='FMQTY' || Str::upper($key)=='FMQTY_EACH')$val = $amount;
  299. if (Str::upper($key)=='FMID')$val = "WAS".$val;
  300. $columns .= "'".$key."',";
  301. $values .= "'".$val."',";
  302. }
  303. $columns = mb_substr($columns,0,-1);
  304. $values = mb_substr($values,0,-1);
  305. $sql = <<<sql
  306. INSERT INTO TSK_TASKLISTS({$columns}) VALUES({$values})
  307. sql;
  308. DB::connection("oracle")->insert(DB::raw($sql));
  309. DB::connection("oracle")->update(DB::raw("UPDATE TSK_TASKLISTS SET FMQTY = FMQTY-?,FMQTY_EACH = FMQTY_EACH-? WHERE TASKID = ? AND TASKID_SEQUENCE = ?"),[
  310. $amount,$amount,$task->taskid,$task->taskid_sequence
  311. ]);
  312. $invs = DB::connection("oracle")->select(DB::raw("SELECT * FROM INV_LOT_LOC_ID WHERE LOTNUM = ? AND LOCATIONID IN (?,?) AND TRACEID = ?"),[
  313. $task->fmlotnum,$task->fmlocation,$task->plantolocation,$task->fmid
  314. ]);
  315. foreach ($invs as $inv){
  316. $columns = '';
  317. $values = '';
  318. if ($inv->locationid==$task->fmlocation){
  319. DB::connection("oracle")->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty-? WHERE lotnum = ? AND locationid = ? AND traceid = ?"),[
  320. $amount,$inv->lotnum,$inv->locationid,$inv->traceid
  321. ]);
  322. foreach ($inv as $key=>$val){
  323. if (Str::upper($key)=='TRACEID') $val = "WAS".$task->fmid;
  324. if (Str::upper($key)=='QTY') $val = $amount;
  325. $columns .= "'".$key."',";
  326. $values .= "'".$val."',";
  327. }
  328. $columns = mb_substr($columns,0,-1);
  329. $values = mb_substr($values,0,-1);
  330. DB::connection("oracle")->insert(DB::raw("INSERT INTO inv_lot_loc_id({$columns}) VALUES({$values})"));
  331. }
  332. if ($inv->locationid==$task->plantolocation){
  333. DB::connection("oracle")->update(DB::raw("UPDATE inv_lot_loc_id SET QTYPA = QTYPA-? WHERE lotnum = ? AND locationid = ? AND traceid = ?"),[
  334. $amount,$inv->lotnum,$inv->locationid,$inv->traceid
  335. ]);
  336. foreach ($inv as $key=>$val){
  337. if (Str::upper($key)=='TRACEID') $val = "WAS".$task->fmid;
  338. if (Str::upper($key)=='QTYPA') $val = $amount;
  339. $columns .= "'".$key."',";
  340. $values .= "'".$val."',";
  341. }
  342. $columns = mb_substr($columns,0,-1);
  343. $values = mb_substr($values,0,-1);
  344. DB::connection("oracle")->insert(DB::raw("INSERT INTO inv_lot_loc_id({$columns}) VALUES({$values})"));
  345. }
  346. }
  347. DB::connection("oracle")->commit();
  348. }catch(\Exception $e) {
  349. DB::connection("oracle")->rollBack();
  350. throw new \Exception("拆分任务失败:".$e->getMessage());
  351. }
  352. return DB::connection("oracle")->selectOne(DB::raw("SELECT * FROM TSK_TASKLISTS WHERE TASKID = ? AND TASKID_SEQUENCE = ?"),[$task->taskid,$seq]);
  353. }
  354. /**
  355. * 获取匹配数字
  356. *
  357. * @param Integer[] $nums
  358. * @param Integer $target
  359. * @return Integer[]|null
  360. */
  361. protected function getMatch(array $nums,int $target) :?array
  362. {
  363. $map=[];
  364. foreach ($nums as $index=>$val){
  365. $complement=$target-$val;
  366. if(array_key_exists($complement,$map))return [$map[$complement],$index];
  367. if ($val==$target)return [$index];
  368. $map[$val]=$index;
  369. if ($val<$target){
  370. $temp = $nums;
  371. unset($temp[$index]);
  372. $arr = $this->getMatch($temp,$target-$val);
  373. if ($arr) {
  374. $arr[] = $index;
  375. return $arr;
  376. }
  377. }
  378. }
  379. return null;
  380. }
  381. /**
  382. * 将任务在flux上架
  383. *
  384. * @param $asn
  385. * @param $ide
  386. * @param $amount
  387. * @return bool
  388. * @throws \Throwable
  389. */
  390. public function fluxPA($asn,$ide,$amount)
  391. {
  392. if (!$asn->taskid)return false;//ASN单无此入库信息,禁止上架
  393. $sql = <<<sql
  394. SELECT * FROM inv_lot_loc_id WHERE lotnum = ? AND traceid = ? AND customerid= ? and sku = ? and qty = {$amount}
  395. sql;
  396. $inv = DB::connection("oracle")->selectOne(DB::raw($sql),[$asn->fmlotnum,$asn->plantoid,$asn->customerid,$asn->sku]);
  397. if (!$inv)return false;//余量与入库不符
  398. DB::connection("oracle")->transaction(function ()use($inv,$amount,$ide,$asn,&$who){
  399. $db = DB::connection("oracle");
  400. $db->delete(DB::raw("DELETE FROM inv_lot_loc_id WHERE lotnum = ? AND traceid = ? AND traceid != '*' AND qty = 0"),[
  401. $inv->lotnum,$inv->traceid
  402. ]);
  403. $invHistory = $db->selectOne(DB::raw("SELECT * FROM inv_lot_loc_id WHERE lotnum = ? AND locationid = ? AND customerid = ? AND sku = ? AND traceid = '*' FOR UPDATE"),[
  404. $inv->lotnum,$ide,$inv->customerid,$inv->sku
  405. ]);
  406. $who = 'WAS'.(Auth::user() ? '-'.Auth::user()["name"] : '');
  407. if ($invHistory)$db->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty+? WHERE lotnum = ? AND locationid = ? AND traceid = '*'"),[
  408. (int)$amount,$inv->lotnum,$ide
  409. ]);
  410. else $db->insert(DB::raw("INSERT INTO inv_lot_loc_id VALUES(?,?,'*',?,?,?,0,0,0,0,0,0,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,0,0,0,0,0,'*',0,null)"),[
  411. $inv->lotnum,$ide,$inv->customerid,$inv->sku,$amount,date("Y-m-d H:i:s"),$who,
  412. date("Y-m-d H:i:s"),$who
  413. ]);
  414. $sql = <<<sql
  415. INSERT INTO ACT_TRANSACTION_LOG VALUES(?,'PA',?,?,?,?,'ASN',?,?,?,?,?,?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,
  416. TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,0,0,0,0,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,?,null,null,null,'*',?,?,?,?,?,?,?,
  417. ?,?,?,?,?,'N',null,?,?,?,?,?,?,?,null,null)
  418. sql;
  419. list($trid,$max) = $this->getTrNumber();
  420. $db->insert(DB::raw($sql),[
  421. $trid,$asn->customerid,$asn->sku,
  422. $asn->asnno,$asn->asnlineno,$inv->lotnum,$asn->fmlocation,$asn->plantoid,$asn->packid,$asn->uom,$amount,$amount,'99',date("Y-m-d H:i:s"),$who,
  423. date("Y-m-d H:i:s"),$who,date("Y-m-d H:i:s"),$asn->customerid,$asn->sku,$ide,$who,$asn->packid,$asn->uom,$amount,$amount,$inv->lotnum,
  424. '*','0','N','*',$asn->taskid_sequence,$asn->warehouseid,$asn->userdefine1,$asn->userdefine2,
  425. $asn->userdefine3,$asn->userdefine4,$asn->userdefine5,'O'
  426. ]);
  427. $this->setTrNumber($max);
  428. $sql = <<<sql
  429. update TSK_TASKLISTS set TASKPROCESS = '99',REASONCODE = 'OK',PLANTOLOCATION = ?,PLANLOGICALTOSEQUENCE = ?,FMID = '*'
  430. COMPLETED_TRANSACTIONID = ?,OPENWHO = ?,OPENTIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),
  431. CLOSEWHO = ?,CLOSETIME = ?,EDITTIME = ?,EDITWHO = ?
  432. where taskid = ? AND TASKID_SEQUENCE = ?
  433. sql;
  434. $db->update(DB::raw($sql),[
  435. $ide,'0',$trid,$who,date("Y-m-d H:i:s"),$who,date("Y-m-d H:i:s"),date("Y-m-d H:i:s"),$who,$asn->taskid,$asn->taskid_sequence
  436. ]);
  437. });
  438. //成功后应去修改ASN状态及数量 暂时不知上架后ASN应为状态
  439. /* $sql = <<<sql
  440. UPDATE doc_asn_details SET linestatus = ?,edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ? AND asnlineno = ?
  441. sql;*/
  442. //if ($asn->expectedqty>$asn->receivedqty && $asn->linestatus!='30')DB::connection("oracle")->update(DB::raw($sql),['30',date("Y-m-d H:i:s"),$who,$asn->asnno,$asn->asnlineno]);
  443. if ($asn->expectedqty==$asn->receivedqty && $asn->linestatus=='40'){
  444. //DB::connection("oracle")->update(DB::raw($sql),['40',date("Y-m-d H:i:s"),$who,$asn->asnno,$asn->asnlineno]);
  445. $check = DB::connection("oracle")->selectOne(DB::raw("SELECT 1 FROM DOC_ASN_DETAILS WHERE ASNNO = ? AND LINESTATUS != '40'"),[$asn->asnno]);
  446. if (!$check){
  447. $logs = DB::connection("oracle")->select(DB::raw("SELECT SUM(FMQTY) qty,TRANSACTIONTYPE FROM ACT_TRANSACTION_LOG WHERE DOCNO = ? AND TRANSACTIONTYPE IN ('PA','IN') GROUP BY TRANSACTIONTYPE"),[$asn->asnno]);
  448. $paQty = 0;
  449. $inQty = 0;
  450. foreach ($logs as $log){
  451. if ($log->transactiontype == 'IN')$inQty = $log->qty;
  452. else $paQty = $log->qty;
  453. }
  454. if ($paQty == $inQty){
  455. DB::connection("oracle")->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '99',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  456. [date("Y-m-d H:i:s"),$who,$asn->asnno]);
  457. DB::connection("oracle")->update(DB::raw("UPDATE DOC_ASN_DETAILS SET linestatus = '99',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  458. [date("Y-m-d H:i:s"),$who,$asn->asnno]);
  459. }
  460. }
  461. }
  462. return true;
  463. }
  464. /**
  465. * put cache rack box to warehousing(将缓存架料箱入库)
  466. *
  467. * @param string $fromLocation
  468. * @param integer $boxId
  469. *
  470. * @return int
  471. */
  472. public function putWareHousing(string $fromLocation, $boxId):?int
  473. {
  474. $station = Station::query()->select("id")
  475. ->where("station_type_id",5)->where("code",$fromLocation)->first();
  476. if (!$station)return null;
  477. if (StationTask::query()->select("id")->where("status","!=",'完成')->where("station_id",$station->id)->first())return null;
  478. /** @var StationTaskMaterialBox|\stdClass $stmb */
  479. $stmb = $this->createWarehousingTask($station->id,$boxId);
  480. return $stmb->id;
  481. }
  482. /**
  483. * 获取事务现号
  484. *
  485. * @return array
  486. */
  487. private function getTrNumber()
  488. {
  489. $val = ValueStore::query()->select("value")->where("name","flux_tr_number")->first();
  490. if (!$val)$val = ValueStore::query()->create(["name"=>"flux_tr_number","value"=>'0']);
  491. $max = $val->value+1;
  492. $number = sprintf("%09d", $max);
  493. return array('W'.$number,$max);
  494. }
  495. /**
  496. * 设置事务现号
  497. *
  498. * @param integer $max
  499. */
  500. private function setTrNumber($max)
  501. {
  502. ValueStore::query()->select("value")->where("name","flux_tr_number")->update(["value"=>(string)((int)$max+1)]);
  503. }
  504. /**
  505. * 入库
  506. *
  507. * @param integer $boxId
  508. * @param integer $amount
  509. * @param integer $commodityId
  510. *
  511. * @return bool
  512. */
  513. public function warehousing($boxId, $amount, $commodityId = null):bool
  514. {
  515. DB::beginTransaction();
  516. try{
  517. $storage = Storage::query()->where("material_box_id",$boxId)->lockForUpdate()->first();
  518. if (!$storage && !$commodityId)return false;
  519. if (!$storage){
  520. Storage::query()->create([
  521. "station_id" => null,
  522. "material_box_id" => $boxId,
  523. "commodity_id" => $commodityId,
  524. "amount" => $amount,
  525. ]);
  526. return true;
  527. }
  528. if ($commodityId && $storage->commodity_id && $storage->commodity_id!=$commodityId)return false;
  529. $obj = [
  530. "station_id" => null,
  531. "amount" => DB::raw("amount + {$amount}"),
  532. ];
  533. if (!$storage->commodity_id && $commodityId)$obj["commodity_id"] = $commodityId;
  534. $storage->update($obj);
  535. DB::commit();
  536. return true;
  537. }catch (\Exception $e){
  538. DB::rollBack();
  539. return false;
  540. }
  541. }
  542. /**
  543. * 出库
  544. *
  545. * @param integer $boxId
  546. * @param integer $amount
  547. * @param integer $commodityId
  548. *
  549. * @return bool
  550. */
  551. public function outWarehousing($boxId, $amount, $commodityId = null):bool
  552. {
  553. DB::beginTransaction();
  554. try{
  555. $storage = Storage::query()->where("material_box_id",$boxId)->lockForUpdate()->first();
  556. if (!$storage)return false;
  557. if ($commodityId && $storage->commodity_id && $storage->commodity_id!=$commodityId)return false;
  558. $obj = [
  559. "station_id" => null,
  560. "amount" => DB::raw("amount - {$amount}"),
  561. ];
  562. if (!$storage->commodity_id && $commodityId)$obj["commodity_id"] = $commodityId;
  563. $storage->update($obj);
  564. DB::commit();
  565. return true;
  566. }catch (\Exception $e){
  567. DB::rollBack();
  568. return false;
  569. }
  570. }
  571. /**
  572. * 清除任务
  573. *
  574. * @param array $stationCodes
  575. */
  576. public function clearTask(array $stationCodes)
  577. {
  578. //清除海柔信息,标记料箱为出库
  579. DB::connection("mysql_haiRobotics")->table("ks_bin")->whereIn("ks_bin_space_code",$stationCodes)
  580. ->where("status",1)->update([
  581. "ks_bin_space_code" => null,"ks_bin_space_id"=>null,"orig_ks_bin_space_code"=>null,"orig_ks_bin_space_id"=>null,
  582. "status"=>4,
  583. ]);
  584. //WAS任务清除
  585. $station = Station::query()->select("id")->whereIn("code",$stationCodes);
  586. $task = StationTask::query()->select("id")->where("status","!=",'完成')->whereIn("station_id",$station);
  587. StationTaskMaterialBox::query()->where("status","!=",'完成')->whereIn("station_task_id",$task)->update([
  588. "status" => "完成"
  589. ]);
  590. StationTask::query()->where("status","!=",'完成')->whereIn("station_id",$station)->update([
  591. "status" => "完成"
  592. ]);
  593. Storage::query()->whereIn("station_id",$station)->update([
  594. "status" => 1,
  595. "station_id" => null,
  596. ]);
  597. return true;
  598. }
  599. }