HandInStorageService.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <?php
  2. namespace App\Services;
  3. use App\CommodityBarcode;
  4. use App\OracleBasCode;
  5. use App\OracleBasCustomer;
  6. use App\OracleBasForwardingLoc;
  7. use App\OracleBasLocation;
  8. use App\OracleBasLotId;
  9. use App\OracleBasSKU;
  10. use App\OracleDOCASNDetail;
  11. use App\OracleDOCASNHeader;
  12. use App\OracleInvLotAtt;
  13. use App\OracleInvLotLocId;
  14. use App\Traits\ServiceAppAop;
  15. use App\ValueStore;
  16. use Carbon\Carbon;
  17. use Doctrine\DBAL\Schema\AbstractAsset;
  18. use Illuminate\Database\Eloquent\Builder;
  19. use Illuminate\Database\Eloquent\Collection;
  20. use Illuminate\Database\Eloquent\Model;
  21. use Illuminate\Support\Facades\Auth;
  22. use Illuminate\Support\Facades\Cache;
  23. use Illuminate\Support\Facades\DB;
  24. use Monolog\Handler\IFTTTHandler;
  25. use phpDocumentor\Reflection\Types\Object_;
  26. class HandInStorageService
  27. {
  28. use ServiceAppAop;
  29. /**
  30. * @param $asnno
  31. * @return object
  32. * 根据asn单号获取 总预期数量 总已收数量
  33. */
  34. public function getAsnQty($asnno)
  35. {
  36. $asnQty=OracleDOCASNDetail::query()
  37. ->select('expectedqty','receivedqty')
  38. ->where('asnno',$asnno)
  39. ->get();
  40. $expectedqty=0;
  41. $receivedqty=0;
  42. foreach ($asnQty as $qty){
  43. $expectedqty+=$qty->expectedqty;
  44. $receivedqty+=$qty->receivedqty;
  45. }
  46. return (object)array('expectedqty'=>$expectedqty,'receivedqty'=>$receivedqty);
  47. }
  48. /**
  49. * @param array $info
  50. * @return bool|int
  51. * 校验货主拣货位
  52. */
  53. public function checkForwardingLoc(array $info)
  54. {
  55. $res=OracleBasCustomer::query()
  56. ->where('customerid',$info['customerid'])
  57. ->where('customer_type','OW')
  58. ->where('udf1','Y')->count(); //查询此货主是否必须有拣货位
  59. if ($res>0) {
  60. $amount=OracleBasForwardingLoc::query()
  61. ->where('customerid',$info['customerid'])
  62. ->where('sku',$info['sku'])
  63. ->count();
  64. if ($amount==0)return 1;//请维护拣货位!
  65. }
  66. return true;
  67. }
  68. /**
  69. * @param array $info
  70. * @return bool|int
  71. * 校验产品档案长宽高
  72. */
  73. public function checkWidthHeight(array $info)
  74. {
  75. $basSku=OracleBasSKU::query()->where('customerid',$info['customerid'])->where('sku',$info['sku'])->first();
  76. if (!$basSku)return 1;//需要维护产品档案
  77. if ($basSku->skulength<=0||$basSku->skuwidth<=0||$basSku->skuhigh<=0)return 2;//需要维护该产品档案中的长宽高
  78. return true;
  79. }
  80. /**
  81. * @param array $info
  82. * @return bool|int
  83. * 校验产品档案重量体积
  84. */
  85. public function checkCubicWeight(array $info)
  86. {
  87. $basSku=OracleBasSKU::query()->where('customerid',$info['customerid'])->where('sku',$info['sku'])->first();
  88. if (!$basSku)return 1;//需要维护产品档案
  89. if ($basSku->grossweight<=0||$basSku->cube<=0)return 2;//需要维护该产品档案中的重量体积
  90. return true;
  91. }
  92. /**
  93. * @param array $info
  94. * @param array $param
  95. * @return bool|int
  96. * 校验库位
  97. */
  98. public function checkLocation(array $info, array $param)
  99. {
  100. $location = OracleBasLocation::query()
  101. ->where('locationid', $info['location'])
  102. ->where('status', 'OK')
  103. ->first();
  104. if (!$location) return 1;//库位不存在
  105. if ($location['mix_flag'] == 'N' && $location['mix_lotflag'] == 'N') { // 库位:产品和批次都不可混放
  106. $inv = OracleInvLotLocId::query()->where('locationid', $info['location'])->first();
  107. if (!$inv) return true; //当前库位无库存余量 可直接入库
  108. if ($inv['customerid'] == $param['customerid'] && $inv['sku'] == $param['sku']
  109. && $inv['lotnum'] == $param['plantolotnum'])
  110. return true;
  111. else return 2; //库位:产品和批次不可混放
  112. }
  113. if ($location['mix_flag'] == 'Y' && $location['mix_lotflag'] == 'N') {//库位:产品可混放,批次不可
  114. $invs = OracleInvLotLocId::query()->where('locationid', $info['location'])->get();
  115. if ($invs->count() == 0) return true; //当前库位无库存余量 可直接入库
  116. $skuInvs = []; // 库位没有该商品
  117. foreach ($invs as $inv) {
  118. if ($inv['customerid'] != $param['customerid'] || $inv['sku'] != $param['sku']) { // 库位没有该商品
  119. $skuInvs[] = $inv;
  120. continue;
  121. }
  122. if ($inv['lotnum'] == $param['plantolotnum']) return true; // 批次相同
  123. return 3; //库位:产品相同,不能混放批次
  124. }
  125. if (count($skuInvs) == count($invs)) return true;
  126. }
  127. if ($location['mix_flag'] == 'N' && $location['mix_lotflag'] == 'Y') { //库位:产品不可混放,批次可混放
  128. $inv = OracleInvLotLocId::query()->where('locationid', $info['location'])->first();
  129. if (!$inv) return true; //当前库位无库存余量 可直接入库
  130. if ($inv['customerid'] == $param['customerid'] || $inv['sku'] == $param['sku']) return true;
  131. else return 4; //库位:产品不能混放
  132. }
  133. // 库位
  134. return true;
  135. }
  136. /**
  137. * @param array $info
  138. * @return int|mixed
  139. * 校验asn单号是否能收货
  140. */
  141. public function checkAsnOperation(array $info)
  142. {
  143. if (!$info['customerid'] || !$info['asntype']) {
  144. $asn = OracleDOCASNHeader::query()
  145. ->select(['asnno', 'asnreference1', 'asnstatus', 'addtime', 'customerid', 'asntype'])
  146. ->where('asnno', $info['asnno'])
  147. ->whereIn('asnstatus', ['00', '30'])
  148. ->first();
  149. if (!$asn) return 1; //无效asn单号
  150. return $this->whetherDeliver($asn);
  151. }
  152. return $this->whetherDeliver($info);
  153. }
  154. private function whetherDeliver($asn)
  155. {
  156. if ($asn['asntype'] != 'XNRK' && $asn['asntype'] != 'THRK' && $asn['asntype'] != 'F31') {
  157. $res = app(DeliveryAppointmentService::class)->checkOperableAsn($asn['asnno'], $asn['customerid']);
  158. if ($res) return $asn;
  159. else return 2; //当前asn单号无预约记录
  160. }
  161. return $asn;
  162. }
  163. /**
  164. * @param $asn
  165. * @return Builder[]|Collection
  166. * 获取富勒asn_header 根据货主,asn,或者条码
  167. *
  168. */
  169. public function selectAsn($asn)
  170. {
  171. if (!$asn) return OracleDOCASNHeader::query() //空扫
  172. ->select(['asnno', 'asnreference1', 'asnstatus', 'addtime', 'customerid', 'asntype'])
  173. ->where('asnstatus', '00')
  174. ->orderByDesc('addtime')
  175. ->limit(50)
  176. ->get();
  177. if (strpos(strtoupper($asn), 'ASN') !== false) { //asn 单号
  178. return OracleDOCASNHeader::query()
  179. ->select(['asnno', 'asnreference1', 'asnstatus', 'addtime', 'customerid', 'asntype'])
  180. ->where('asnno', $asn)
  181. ->whereIn('asnstatus', ['00', '30'])
  182. ->get();
  183. } else {
  184. $asns = OracleDOCASNHeader::query() //货主
  185. ->select(['asnno', 'asnreference1', 'asnstatus', 'addtime', 'customerid', 'asntype'])
  186. ->where('customerid', strtoupper($asn))
  187. ->whereIn('asnstatus', ['00', '30'])
  188. ->get();
  189. if ($asns->count() > 0) {
  190. return $asns;
  191. } else { //商品条码
  192. $sql = <<<SQL
  193. SELECT DOC_ASN_HEADER.ASNNO,DOC_ASN_HEADER.addtime,DOC_ASN_HEADER.asnreference1,DOC_ASN_HEADER.customerid,DOC_ASN_HEADER.asnstatus,DOC_ASN_HEADER.asntype FROM DOC_ASN_HEADER
  194. LEFT JOIN DOC_ASN_DETAILS ON DOC_ASN_HEADER.ASNNO = DOC_ASN_DETAILS.ASNNO
  195. LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  196. WHERE DOC_ASN_HEADER.ASNSTATUS in ('00','30') and (BAS_SKU.ALTERNATE_SKU1 = ? OR BAS_SKU.ALTERNATE_SKU2 = ? OR BAS_SKU.ALTERNATE_SKU3 = ?)
  197. group by DOC_ASN_HEADER.ASNNO,DOC_ASN_HEADER.addtime,DOC_ASN_HEADER.asnreference1,DOC_ASN_HEADER.customerid,DOC_ASN_HEADER.asnstatus,DOC_ASN_HEADER.asntype
  198. SQL;
  199. return DB::connection("oracle")->select(DB::raw($sql), [$asn, $asn, $asn]);
  200. }
  201. }
  202. }
  203. /**
  204. * @param $asnno
  205. * @return Builder[]|Collection
  206. * 获取富勒asn_detail (集合)
  207. */
  208. public function selectAsnDetails($asnno)
  209. {
  210. $sql = <<<sql
  211. SELECT DOC_ASN_DETAILS.sku,DOC_ASN_DETAILS.expectedqty,DOC_ASN_DETAILS.skudescrc,DOC_ASN_DETAILS.asnlineno,DOC_ASN_DETAILS.asnno,
  212. DOC_ASN_DETAILS.receivedqty,BAS_SKU.alternate_sku1
  213. FROM DOC_ASN_DETAILS LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  214. WHERE asnno = ? AND linestatus IN ('00','30')
  215. sql;
  216. $asn_details = DB::connection("oracle")->select(DB::raw($sql), [$asnno]);
  217. if (count($asn_details) > 0) return $asn_details;
  218. else return array();
  219. }
  220. /**
  221. * @param $asnno
  222. * @param $skuOrBarcode
  223. * @return Builder|Model|object|null
  224. *根据sku 或者条码获取asn_detail
  225. */
  226. public function getAsnDetail($asnno, $skuOrBarcode)
  227. {
  228. $sql = <<<sql
  229. SELECT DOC_ASN_DETAILS.sku,DOC_ASN_DETAILS.expectedqty,DOC_ASN_DETAILS.skudescrc,
  230. DOC_ASN_DETAILS.lotatt01, DOC_ASN_DETAILS.lotatt02, DOC_ASN_DETAILS.lotatt03, DOC_ASN_DETAILS.lotatt04,
  231. DOC_ASN_DETAILS.lotatt05, DOC_ASN_DETAILS.lotatt06, DOC_ASN_DETAILS.lotatt07, DOC_ASN_DETAILS.lotatt08,
  232. DOC_ASN_DETAILS.asnlineno,DOC_ASN_DETAILS.asnno,DOC_ASN_DETAILS.receivedqty FROM DOC_ASN_DETAILS
  233. LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  234. WHERE ASNNO = ? AND LINESTATUS IN ('00','30') AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?)
  235. sql;
  236. $asn_detail = DB::connection("oracle")->selectOne(DB::raw($sql), [$asnno, $skuOrBarcode, $skuOrBarcode, $skuOrBarcode]);
  237. if ($asn_detail) return $asn_detail;
  238. else return OracleDOCASNDetail::query()
  239. ->select(['sku', 'expectedqty', 'skudescrc', 'asnlineno', 'asnno', 'receivedqty',
  240. 'lotatt01','lotatt02','lotatt03','lotatt04','lotatt05','lotatt06','lotatt07','lotatt08'])
  241. ->where('asnno', $asnno)
  242. ->where('sku', $skuOrBarcode)
  243. ->whereIn('linestatus', ['00', '30'])
  244. ->first();
  245. }
  246. /**
  247. * @return mixed
  248. * 获取质量状态
  249. */
  250. public function getQualityStatus()
  251. {
  252. return Cache::remember('BAS_CODE_QLT_STS', 600, function () {
  253. return OracleBasCode::query()->select(['codeid', 'code', 'codename_c'])
  254. ->where('codeid', 'QLT_STS')
  255. ->get();
  256. });
  257. }
  258. /**
  259. * @return mixed
  260. * 获取属性仓
  261. */
  262. public function getAttributeLocation()
  263. {
  264. return Cache::remember('BAS_CODE_CUS_UDFPC', 600, function () {
  265. return OracleBasCode::query()->select(['codeid', 'code', 'codename_c'])
  266. ->where('codeid', 'CUS_UDFPC')
  267. ->get();
  268. });
  269. }
  270. /**
  271. * @param $customerid
  272. * @param $sku
  273. * @return mixed
  274. * 根据customerid和sku 查询商品关联的批次属性规则
  275. */
  276. public function getBasSkuLotId($customerid, $sku)
  277. {
  278. return Cache::remember('bas_sku_lot_' . $customerid . '_' . $sku, 600, function () use ($customerid, $sku) {
  279. return OracleBasSKU::query()->select(['customerid', 'sku', 'lotid'])
  280. ->where('customerid', $customerid)
  281. ->where('sku', $sku)
  282. ->with('lotId')
  283. ->first();
  284. });
  285. }
  286. /**
  287. * @param $barcode
  288. * @return array
  289. * 根据条码获取 库存
  290. */
  291. public function getInvotlocid($barcode): array
  292. {
  293. $sql=<<<sql
  294. select INV_LOT_LOC_ID.CUSTOMERID,BAS_SKU.ALTERNATE_SKU1,INV_LOT_LOC_ID.LOCATIONID,INV_LOT_ATT.LOTATT05,INV_LOT_ATT.LOTATT08,
  295. sum(INV_LOT_LOC_ID.QTY) AS QTY from INV_LOT_LOC_ID
  296. left join BAS_SKU on INV_LOT_LOC_ID.CUSTOMERID=BAS_SKU.CUSTOMERID and INV_LOT_LOC_ID.SKU =BAS_SKU.SKU
  297. left join INV_LOT_ATT on INV_LOT_ATT.LOTNUM=INV_LOT_LOC_ID.LOTNUM
  298. where (BAS_SKU.ALTERNATE_SKU1 = ? OR BAS_SKU.ALTERNATE_SKU2 = ? OR BAS_SKU.ALTERNATE_SKU3 = ?)
  299. group by INV_LOT_LOC_ID.CUSTOMERID,BAS_SKU.ALTERNATE_SKU1,INV_LOT_LOC_ID.LOCATIONID,INV_LOT_ATT.LOTATT05,INV_LOT_ATT.LOTATT08
  300. sql;
  301. $invLots = DB::connection("oracle")->select(DB::raw($sql), [$barcode, $barcode, $barcode]);
  302. if (!$invLots)return [];
  303. else return $invLots;
  304. }
  305. /**
  306. * @param string $barCode
  307. * @return array|int
  308. * 根据商品条码 获取完全收货状态 部分收货状态的 PA任务
  309. */
  310. public function getTsk($trackNumber, string $barCode): array
  311. {
  312. $sql = <<<sql
  313. SELECT TSK_TASKLISTS.CustomerID,TSK_TASKLISTS.DOCNO,TSK_TASKLISTS.Sku,TSK_TASKLISTS.PlanToLotNum,TSK_TASKLISTS.PlanToID,DOC_ASN_DETAILS.SKUDESCRC,sum(TSK_TASKLISTS.PlanToQty) AS QTY
  314. FROM DOC_ASN_DETAILS
  315. LEFT JOIN TSK_TASKLISTS ON DOC_ASN_DETAILS.ASNNO = TSK_TASKLISTS.DOCNO AND
  316. DOC_ASN_DETAILS.ASNLINENO = TSK_TASKLISTS.DOCLINENO
  317. WHERE
  318. ASNNO in (SELECT asnno FROM DOC_ASN_DETAILS
  319. LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  320. WHERE LINESTATUS in ('30','40') and (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?))
  321. AND TSK_TASKLISTS.TASKPROCESS = '00'
  322. AND TSK_TASKLISTS.TASKTYPE = 'PA'
  323. sql;
  324. if (!$trackNumber){ //没有输入条件 空扫
  325. $owner_codes = app('OwnerService')->getIntersectPermitting(['code']);
  326. if (count($owner_codes)>0){
  327. $sql.=' AND TSK_TASKLISTS.CustomerID IN (';
  328. foreach ($owner_codes as $index => $no){
  329. if ($index==0){
  330. $sql.="'".$no->code."'";
  331. continue;
  332. }
  333. $sql.=",'".$no->code."'";
  334. }
  335. $sql.=')';
  336. }else{
  337. $sql .= 'AND TSK_TASKLISTS.CustomerID IS NULL ';
  338. }
  339. }else{
  340. if (strpos(strtoupper($trackNumber), 'ASN') !== false) $sql.='AND TSK_TASKLISTS.DOCNO= ?'; //输入条件为asn单号
  341. if ($this->checkUserOwnerAuth($trackNumber)){ //输入条件为货主
  342. $sql.='AND TSK_TASKLISTS.CustomerID= ?';
  343. }else{
  344. $sql.='AND TSK_TASKLISTS.PlanToID= ?'; //不是货主 判断是否为跟踪号
  345. }
  346. }
  347. $sql.=' group by TSK_TASKLISTS.CustomerID,TSK_TASKLISTS.DOCNO,TSK_TASKLISTS.Sku,TSK_TASKLISTS.PlanToLotNum,TSK_TASKLISTS.PlanToID,DOC_ASN_DETAILS.SKUDESCRC';
  348. if ($trackNumber){
  349. if ($this->checkUserOwnerAuth($trackNumber))$tasks = DB::connection("oracle")->select(DB::raw($sql), [$barCode, $barCode, $barCode, strtoupper($trackNumber)]);
  350. else $tasks = DB::connection("oracle")->select(DB::raw($sql), [$barCode, $barCode, $barCode, $trackNumber]);
  351. }else {$tasks = DB::connection("oracle")->select(DB::raw($sql), [$barCode, $barCode, $barCode]);}
  352. if (!$tasks) return [];
  353. else return $tasks;
  354. }
  355. /**
  356. * @param $owner_code
  357. * @return bool
  358. * 判断当前用户货主权限
  359. */
  360. public function checkUserOwnerAuth($owner_code): bool
  361. {
  362. $owner_codes = app('OwnerService')->getIntersectPermitting(['code']);
  363. $owner=$owner_codes->where('code','=',strtoupper($owner_code));
  364. if ($owner->count()>0)return true;
  365. else return false;
  366. }
  367. /**
  368. * @throws \Throwable
  369. * flux手持端 上架
  370. */
  371. public function fluxHandPa(array $info, array $taskParam): bool
  372. {
  373. $tasks = $this->selectFluxTask($taskParam, $info['amount']);
  374. if (!$tasks) return false; //获取任务失败
  375. return DB::connection("oracle")->transaction(function () use ($tasks, $info) { //单体嵌套事务 回滚FLUX失败任务
  376. foreach ($tasks as $task) {
  377. $res=$this->checkExpiryPa($task,$info['location']);
  378. if ($res!==true)return $res;
  379. if (!app("StorageService")->fluxPA($task, $info['location'])) {
  380. DB::connection("oracle")->rollBack();
  381. return false; //上架失败
  382. }
  383. }
  384. return true; //上架成功
  385. });
  386. }
  387. /**
  388. * @param $task
  389. * @param $location
  390. * @return bool|int
  391. * 上架校验效期
  392. */
  393. public function checkExpiryPa($task,$location)
  394. {
  395. if (!$task->taskid)return 0;//任务id不存在
  396. if (strpos($task->taskid,'MIX') !== false)return true;//合并拣货,不校验
  397. $sql = <<<sql
  398. select instr(DESCR,'拣货') as var_IsPickingArea from BAS_Zone where ZONE=(select PutawayZone from BAS_Location where LocationID = ?)
  399. sql;
  400. $basZone = DB::connection("oracle")->selectOne(DB::raw($sql), [$location]);
  401. if ($basZone&&$basZone->var_ispickingarea>0) return true; //不是存储区,不校验
  402. $sql1 = <<<sql
  403. select SKU,LotAtt02,CustomerID from INV_LOT_ATT WHERE LOTNUM=?
  404. sql;
  405. $invLotAtt = DB::connection("oracle")->selectOne(DB::raw($sql1), [$task->fmlotnum]);
  406. $sql2 = <<<sql
  407. select count(*) as var_amountOfDecaying from INV_LOT_LOC_ID store
  408. left join BAS_Location location on store.LocationID=location.locationId
  409. left join BAS_Zone zone on zone.zone=location.PickZone
  410. left join INV_LOT_ATT attres on store.LOTNUM=attres.LOTNUM
  411. where store.SKU=?
  412. and store.CustomerID=?
  413. and store.LOTNUM!=?
  414. and attres.LotAtt02 > ?
  415. and zone.DESCR like '%拣货%'
  416. sql;
  417. $invLotLocId=DB::connection("oracle")->selectOne(DB::raw($sql2), [$invLotAtt->sku,$invLotAtt->customerid,$task->fmlotnum,$invLotAtt->lotatt02]);
  418. if ($invLotLocId&&$invLotLocId->var_amountofdecaying>0)return 1;//拣货区找到效期更新的同样货品,不能上架至存储区
  419. return true;
  420. }
  421. /**
  422. * @param $taskParam
  423. * @param $amount
  424. * @return array
  425. * 根据跟踪号,货主,sku,批次 获取任务列表 再通过数量 进行任务的重组(拆分或选定)
  426. */
  427. public function selectFluxTask($taskParam, $amount): array
  428. {
  429. /** @var StorageService $storageService */
  430. $storageService = app('StorageService');
  431. $sql = <<<sql
  432. select * from TSK_TASKLISTS where customerid = ? AND sku = ? AND plantoid = ? AND plantolotnum = ? AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
  433. sql;
  434. $tasks = DB::connection("oracle")->select(DB::raw($sql), [$taskParam['customerid'], $taskParam['sku'], $taskParam['plantoid'], $taskParam['plantolotnum']]);
  435. if (!$tasks) return [];
  436. $nums = [];
  437. $sum = 0;
  438. $maxIndex = null;
  439. foreach ($tasks as $i => $task) {
  440. if ((int)$task->fmqty == $amount) return [$task];
  441. $nums[] = (int)$task->fmqty;
  442. $sum += (int)$task->fmqty;
  443. if ((int)$task->fmqty > $amount) $maxIndex = $i;
  444. }
  445. if ($sum < $amount) return []; //上架数大于入库数
  446. $result = $storageService->getMatch($nums, $amount);
  447. if (!$result) return $storageService->splitTask($tasks, $maxIndex, $amount);
  448. $arr = [];
  449. foreach ($result as $index) $arr[] = $tasks[$index];
  450. return $arr;
  451. }
  452. /**
  453. * @throws \Throwable
  454. * fulx 手持收货
  455. */
  456. public function fluxHandIn(array $info)
  457. {
  458. $lotatt = array_filter($info, function ($key) {
  459. return strpos($key, 'lotatt') === 0;
  460. }, ARRAY_FILTER_USE_KEY);
  461. $invlotatt = [];
  462. for ($i = 1; $i <= 8; $i++) {
  463. $invlotatt["lotatt0{$i}"] = null;
  464. }
  465. foreach ($invlotatt as $key => &$item) {
  466. foreach ($lotatt as $key1 => $item1) {
  467. if ($key === $key1) $item = $item1;
  468. }
  469. }
  470. $who = 'WAS' . (Auth::user() ? '-' . Auth::user()["name"] : '');
  471. $time = Carbon::now()->toDateTimeString();
  472. return DB::connection("oracle")->transaction(function () use ($info, $invlotatt, $who, $time) {
  473. //flux 批次号
  474. $lotNum = $this->getOrCreateLotNum($info, $invlotatt, $who, $time);
  475. if (!$lotNum) {
  476. DB::connection("oracle")->rollBack();
  477. return false;
  478. }
  479. //flux 创建入库事务
  480. $actTransactionLog = $this->setFluxActTransactionLog($info, $lotNum, $who, $time);
  481. if (!$actTransactionLog) {
  482. DB::connection("oracle")->rollBack();
  483. return false;
  484. }
  485. //flux 创建上架任务
  486. $this->setFluxTskTaskListPA($info, $invlotatt, $actTransactionLog, $who, $time);
  487. //flux 完善库存余量
  488. $this->updateFluxInv($info, $lotNum, $who, $time, $actTransactionLog);
  489. //flux 更新asn_detail 和 asn_header 状态
  490. return $this->updateFluxAsn($info, $invlotatt, $time, $who);
  491. });
  492. }
  493. /**
  494. * @throws \Throwable
  495. */
  496. public function updateFluxAsn(array $info, array $invlotatt, $time, $who): bool
  497. {
  498. $db = DB::connection("oracle");
  499. $asn = OracleDOCASNHeader::query()
  500. ->withCount('asnDetails')
  501. ->with('asnDetails')
  502. ->where('asnno', $info['asnno'])
  503. ->first();
  504. if (!$asn || !$asn->asnDetails || !$asn->asn_details_count) return false;
  505. $asnDetails = $asn->asnDetails;
  506. $receiveAsn = null;
  507. foreach ($asnDetails as $asnDetail) {
  508. if ($asnDetail['asnno'] == $info['asnno'] &&
  509. $asnDetail['asnlineno'] == $info['asnlineno'] &&
  510. $asnDetail['customerid'] == $info['customerid'] &&
  511. $asnDetail['sku'] == $info['sku']) $receiveAsn = $asnDetail;
  512. }
  513. return $db->transaction(function () use ($db, $info, $receiveAsn, $invlotatt, $time, $who, $asn) {
  514. if ($receiveAsn && (int)$receiveAsn['receivedqty'] + (int)$info['amount'] < (int)$receiveAsn['expectedqty']) {
  515. //asn_detail 收货数量+已收数量<预期数量
  516. $db->update(DB::raw("UPDATE DOC_ASN_DETAILS SET receivedqty = receivedqty + ?,receivedqty_each = receivedqty_each + ?,linestatus = '30',holdrejectcode ='OK',
  517. reserve_flag ='Y',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),receivedtime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ?,
  518. lotatt01 =?,lotatt02 =?,lotatt03 =?,lotatt04 =?,lotatt05 =?,lotatt06 =?,lotatt07 =?,lotatt08=? WHERE asnno = ? and asnlineno = ?"),
  519. [(int)$info['amount'], (int)$info['amount'], $time, $time, $who, $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  520. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], $info['asnno'], $info['asnlineno']]);
  521. //asn_header 部分收货状态
  522. $db->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '30',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  523. [$time, $who, $info['asnno']]);
  524. } elseif ($receiveAsn && (int)$receiveAsn['receivedqty'] + (int)$info['amount'] == (int)$receiveAsn['expectedqty']) {
  525. //asn_detail 收货数量+已收数量=预期数量
  526. $db->update(DB::raw("UPDATE DOC_ASN_DETAILS SET receivedqty=receivedqty+?,receivedqty_each=receivedqty_each+?,linestatus = '40',
  527. edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),receivedtime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ?,holdrejectcode='OK',
  528. reserve_flag='Y',lotatt01=?,lotatt02=?,lotatt03=?,lotatt04=?,lotatt05=?,lotatt06=?,lotatt07=?,lotatt08=? WHERE asnno = ? and asnlineno = ?"),
  529. [(int)$info['amount'], (int)$info['amount'], $time, $time, $who, $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  530. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], $info['asnno'], $info['asnlineno']]);
  531. //当asn_detail 所有状态都为完全收货是 asn_header 状态修改为 完全收货(asnstatus=40)
  532. if (OracleDOCASNDetail::query()->where('asnno', $info['asnno'])->where('linestatus', 40)->count() == $asn->asn_details_count) {
  533. $db->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '40',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  534. [$time, $who, $info['asnno']]);
  535. } else {
  536. //asn_header 部分收货状态
  537. $db->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '30',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  538. [$time, $who, $info['asnno']]);
  539. }
  540. }
  541. return true;
  542. });
  543. }
  544. /**
  545. * @throws \Throwable
  546. */
  547. public function updateFluxInv(array $info, $lotNum, $who, $time, array $actTransactionLog)
  548. {
  549. $db = DB::connection("oracle");
  550. $db->transaction(function () use ($db, $info, $lotNum, $actTransactionLog, $who, $time) {
  551. //更新 inv_lot 批次 库存表
  552. $invLot = $db->selectOne(DB::raw("SELECT * FROM INV_LOT WHERE lotnum = ? AND customerid = ? AND sku = ? "), [
  553. $lotNum, $info['customerid'], $info['sku']
  554. ]);
  555. if ($invLot) $db->update(DB::raw("UPDATE INV_LOT SET qty = qty+?,edittime=?,editwho=? WHERE lotnum = ? AND customerid = ? AND sku = ?"), [
  556. (int)$info['amount'], $time, $who, $lotNum, $info['customerid'], $info['sku'],
  557. ]);
  558. else $db->insert(DB::raw("INSERT INTO INV_LOT VALUES(?,?,?,?,0,0,0,0,0,0,0,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?)"), [
  559. $lotNum, $info['customerid'], $info['sku'], $info['amount'], $time, $who, $time, $who
  560. ]);
  561. //更新 inv_lot_loc_id 批次/库位/跟踪号 库存表
  562. $invLotId = $db->selectOne(DB::raw("SELECT * FROM inv_lot_loc_id WHERE lotnum = ? AND locationid = ? AND customerid = ? AND sku = ? AND traceid = ? FOR UPDATE"), [
  563. $lotNum, $actTransactionLog['location'], $actTransactionLog['customerid'], $actTransactionLog['sku'], $actTransactionLog['trackid']
  564. ]);
  565. // if ($info['location']) { //存在目标库位
  566. // $invLotIdHasPreLocation = $db->selectOne(DB::raw("SELECT * FROM inv_lot_loc_id WHERE lotnum = ? AND locationid = ? AND customerid = ? AND sku = ? AND traceid = ? FOR UPDATE"), [
  567. // $lotNum, $info['location'], $actTransactionLog['customerid'], $actTransactionLog['sku'], $actTransactionLog['trackid']
  568. // ]);
  569. //
  570. // if ($invLotIdHasPreLocation) $db->update(DB::raw("UPDATE inv_lot_loc_id SET qtypa = qtypa+?,edittime=?,editwho=? WHERE lotnum = ? AND locationid = ? AND traceid = ?"), [
  571. // (int)$info['amount'], $time, $who, $lotNum, $info['location'], $actTransactionLog['trackid']
  572. // ]);
  573. // 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,'*',?,null)"), [
  574. // $lotNum, $info['location'], $actTransactionLog['trackid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], 0, $time, $who, $time, $who, (int)$info['amount']
  575. // ]);
  576. //
  577. // if ($invLotId){
  578. // $db->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty+?,qtymvout = qtymvout+?,edittime=?,editwho=? WHERE lotnum = ? AND locationid = ? AND traceid = ?"), [
  579. // (int)$info['amount'], (int)$info['amount'], $time, $who, $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid']
  580. // ]);
  581. // } else {
  582. // $db->insert(DB::raw("INSERT INTO inv_lot_loc_id VALUES(?,?,?,?,?,?,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)"), [
  583. // $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], (int)$info['amount'], (int)$info['amount'],
  584. // $time, $who, $time, $who,
  585. // ]);
  586. // }
  587. //
  588. // }
  589. if ($invLotId) $db->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty+?,qtymvout = qtymvout+?,edittime=?,editwho=? WHERE lotnum = ? AND locationid = ? AND traceid = ?"), [
  590. (int)$info['amount'],(int)$info['amount'], $time, $who, $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid']
  591. ]);
  592. else $db->insert(DB::raw("INSERT INTO inv_lot_loc_id VALUES(?,?,?,?,?,?,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)"), [
  593. $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], (int)$info['amount'], (int)$info['amount'], $time, $who, $time, $who,
  594. ]);
  595. });
  596. }
  597. /**
  598. * @throws \Throwable
  599. */
  600. public function setFluxTskTaskListPA(array $info, array $invlotatt, $actTransactionLog, $who, $time)
  601. {
  602. $db = DB::connection("oracle");
  603. $db->transaction(function () use ($db, $info, $invlotatt, $actTransactionLog, $who, $time) {
  604. $sql = <<<sql
  605. INSERT INTO TSK_TASKLISTS VALUES(?,'1','PA',?,?,'ASN',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,null,null,?,?,?,?,?,?,?,?,null,null,null,null,
  606. 0,0,0,0,null,?,null,null,null,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),null,null,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),'N',null,null,
  607. ?,?,?,'N',null,?,'*',null,null,null,'N',null,null)
  608. sql;
  609. $db->insert(DB::raw($sql), [
  610. $actTransactionLog['tsid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], $actTransactionLog['docno'], $actTransactionLog['doclineno'],
  611. $actTransactionLog['lotNum'], $actTransactionLog['packid'], 'EA', $info['amount'], $info['amount'], $actTransactionLog['location'], $actTransactionLog['location'],
  612. $actTransactionLog['trackid'], $actTransactionLog['lotNum'], $actTransactionLog['packid'], 'EA', $info['amount'], $info['amount'],
  613. $info['location'], $info['location'], $actTransactionLog['trackid'], '00', 'Putaway Task', '3', $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  614. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], $actTransactionLog['trid'], $who, $time, null, null, null, null,
  615. $actTransactionLog['userdefine1'], $actTransactionLog['userdefine2'], $actTransactionLog['userdefine3'], $actTransactionLog['warehouseid']
  616. ]);
  617. });
  618. }
  619. /**
  620. * @param array $info
  621. * @param $lotNum
  622. * @param $who
  623. * @param $time
  624. * @return mixed
  625. * @throws \Throwable
  626. * 创建入库事务
  627. */
  628. public function setFluxActTransactionLog(array $info, $lotNum, $who, $time)
  629. {
  630. $db = DB::connection("oracle");
  631. return $db->transaction(function () use ($db, $info, $lotNum, $time, $who) {
  632. if ($info['trackNumber']) $trackNumber = $info['trackNumber'];
  633. else $trackNumber = substr(md5($time), 0, 30);
  634. $asnHeader = OracleDOCASNHeader::query()->where('asnno', $info['asnno'])->first();
  635. $asnDetail = OracleDOCASNDetail::query()->where('asnno', $info['asnno'])->where('sku', $info['sku'])->first();
  636. $sql = <<<sql
  637. INSERT INTO ACT_TRANSACTION_LOG VALUES(?,'IN',?,?,?,?,'ASN',?,?,?,?,?,?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,
  638. TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,0,0,0,0,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,?,null,null,null,?,?,?,?,?,?,?,?,
  639. ?,?,?,?,'1','Y',null,?,?,?,?,null,null,?,null,null)
  640. sql;
  641. list($trid, $max) = app('StorageService')->getTrNumber();
  642. list($tsid, $max) = $this->getTsNum();
  643. $db->insert(DB::raw($sql), [
  644. $trid, $asnDetail->customerid, $asnDetail->sku,
  645. $asnDetail->asnno, $asnDetail->asnlineno, $lotNum, $asnDetail->receivinglocation, '*', $asnDetail->packid, 'EA', $info['amount'], $info['amount'], '99', $time, $who,
  646. $time, $who, $time, $asnDetail->customerid, $asnDetail->sku, $trackNumber, $asnDetail->receivinglocation, $who, $asnDetail->packid, 'EA', $info['amount'], $info['amount'], $lotNum,
  647. '*', '0', 'N', $tsid, substr($asnDetail->receivinglocation, -4), $asnHeader->userdefine1, $asnHeader->userdefine2,
  648. $asnHeader->userdefine3, 'O'
  649. ]);
  650. app('StorageService')->setTrNumber();
  651. $this->setTsNum();
  652. $actTransactionLog = [
  653. 'trid' => $trid, 'docno' => $asnDetail->asnno, 'customerid' => $asnDetail->customerid, 'sku' => $asnDetail->sku, 'doclineno' => $asnDetail->asnlineno, 'lotNum' => $lotNum, 'location' => $asnDetail->receivinglocation,
  654. 'packid' => $asnDetail->packid, 'tsid' => $tsid, 'warehouseid' => substr($asnDetail->receivinglocation, -4), 'userdefine1' => $asnHeader->userdefine1, 'userdefine2' => $asnHeader->userdefine2,
  655. 'userdefine3' => $asnHeader->userdefine3, 'trackid' => $trackNumber
  656. ];
  657. return $actTransactionLog;
  658. });
  659. }
  660. /**
  661. * @param array $info
  662. * @return mixed
  663. * @throws \Throwable
  664. * 或去flux 批次号
  665. */
  666. public function getOrCreateLotNum(array $info, array $invlotatt, $who, $time)
  667. {
  668. $invlotatt['customerid'] = $info['customerid'];
  669. $invlotatt['sku'] = $info['sku'];
  670. //根据批次规则查询或新建批次
  671. $lotnum = OracleInvLotAtt::query()->where($invlotatt)->value('lotnum');
  672. if ($lotnum) return $lotnum;
  673. $db = DB::connection("oracle");
  674. list($num, $max) = $this->getLtNum();
  675. return $db->transaction(function () use ($db, $info, $invlotatt, $num, $who, $time) {
  676. $sql = <<<sql
  677. INSERT INTO INV_LOT_ATT VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,
  678. TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?)
  679. sql;
  680. $db->insert(DB::raw($sql), [
  681. $num, $info['customerid'], $info['sku'], $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  682. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], null, null, null, null, $time, $who, $time, $who, $time, null
  683. ]);
  684. $this->setLtNum();
  685. return $num;
  686. });
  687. }
  688. /**
  689. * 获取批次号
  690. * @return array
  691. */
  692. private function getLtNum(): array
  693. {
  694. $val = ValueStore::query()->select("value")->where("name", "flux_lt_number")->lockForUpdate()->first();
  695. if (!$val) $val = ValueStore::query()->create(["name" => "flux_lt_number", "value" => '0']);
  696. $max = $val->value + 1;
  697. $number = sprintf("%07d", $max);
  698. return array('WLT' . $number, $max);
  699. }
  700. /**
  701. * 设置批次号
  702. */
  703. private function setLtNum()
  704. {
  705. ValueStore::query()
  706. ->select("value")
  707. ->where("name", "flux_lt_number")
  708. ->update(["value" => DB::raw("value+1")]);
  709. }
  710. /**
  711. * 获取批次号
  712. * @return array
  713. */
  714. private function getTsNum(): array
  715. {
  716. $val = ValueStore::query()->select("value")->where("name", "flux_ts_number")->lockForUpdate()->first();
  717. if (!$val) $val = ValueStore::query()->create(["name" => "flux_ts_number", "value" => '0']);
  718. $max = $val->value + 1;
  719. $number = sprintf("%07d", $max);
  720. return array('WTS' . $number, $max);
  721. }
  722. /**
  723. * 设置批次号
  724. */
  725. private function setTsNum()
  726. {
  727. ValueStore::query()
  728. ->select("value")
  729. ->where("name", "flux_ts_number")
  730. ->update(["value" => DB::raw("value+1")]);
  731. }
  732. }