HandInStorageService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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','notes'])
  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','notes'])
  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','notes'])
  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,DOC_ASN_HEADER.notes 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.SKU in (select SKU from BAS_SKU where ALTERNATE_SKU1=? union
  197. select SKU from BAS_SKU where ALTERNATE_SKU2=? union
  198. select SKU from BAS_SKU where ALTERNATE_SKU3=? )
  199. 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,DOC_ASN_HEADER.notes
  200. SQL;
  201. return DB::connection("oracle")->select(DB::raw($sql), [$asn, $asn, $asn]);
  202. }
  203. }
  204. }
  205. /**
  206. * @param $asnno
  207. * @return Builder[]|Collection
  208. * 获取富勒asn_detail (集合)
  209. */
  210. public function selectAsnDetails($asnno)
  211. {
  212. $sql = <<<sql
  213. SELECT DOC_ASN_DETAILS.sku,DOC_ASN_DETAILS.expectedqty,DOC_ASN_DETAILS.skudescrc,DOC_ASN_DETAILS.asnlineno,DOC_ASN_DETAILS.asnno,
  214. DOC_ASN_DETAILS.receivedqty,BAS_SKU.alternate_sku1
  215. FROM DOC_ASN_DETAILS LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  216. WHERE asnno = ? AND linestatus IN ('00','30')
  217. sql;
  218. $asn_details = DB::connection("oracle")->select(DB::raw($sql), [$asnno]);
  219. if (count($asn_details) > 0) return $asn_details;
  220. else return array();
  221. }
  222. /**
  223. * @param $asnno
  224. * @param $skuOrBarcode
  225. * @return Builder|Model|object|null
  226. *根据sku 或者条码获取asn_detail
  227. */
  228. public function getAsnDetail($asnno, $skuOrBarcode)
  229. {
  230. $sql = <<<sql
  231. SELECT DOC_ASN_DETAILS.sku,DOC_ASN_DETAILS.expectedqty,DOC_ASN_DETAILS.skudescrc,
  232. DOC_ASN_DETAILS.lotatt01, DOC_ASN_DETAILS.lotatt02, DOC_ASN_DETAILS.lotatt03, DOC_ASN_DETAILS.lotatt04,
  233. DOC_ASN_DETAILS.lotatt05, DOC_ASN_DETAILS.lotatt06, DOC_ASN_DETAILS.lotatt07, DOC_ASN_DETAILS.lotatt08,
  234. DOC_ASN_DETAILS.asnlineno,DOC_ASN_DETAILS.asnno,DOC_ASN_DETAILS.receivedqty FROM DOC_ASN_DETAILS
  235. LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
  236. WHERE ASNNO = ? AND LINESTATUS IN ('00','30') AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3 = ?)
  237. sql;
  238. $asn_detail = DB::connection("oracle")->selectOne(DB::raw($sql), [$asnno, $skuOrBarcode, $skuOrBarcode, $skuOrBarcode]);
  239. if ($asn_detail) return $asn_detail;
  240. else return OracleDOCASNDetail::query()
  241. ->select(['sku', 'expectedqty', 'skudescrc', 'asnlineno', 'asnno', 'receivedqty',
  242. 'lotatt01','lotatt02','lotatt03','lotatt04','lotatt05','lotatt06','lotatt07','lotatt08'])
  243. ->where('asnno', $asnno)
  244. ->where('sku', $skuOrBarcode)
  245. ->whereIn('linestatus', ['00', '30'])
  246. ->first();
  247. }
  248. /**
  249. * @return mixed
  250. * 获取质量状态
  251. */
  252. public function getQualityStatus()
  253. {
  254. return Cache::remember('BAS_CODE_QLT_STS', 600, function () {
  255. return OracleBasCode::query()->select(['codeid', 'code', 'codename_c'])
  256. ->where('codeid', 'QLT_STS')
  257. ->get();
  258. });
  259. }
  260. /**
  261. * @return mixed
  262. * 获取属性仓
  263. */
  264. public function getAttributeLocation()
  265. {
  266. return Cache::remember('BAS_CODE_CUS_UDFPC', 600, function () {
  267. return OracleBasCode::query()->select(['codeid', 'code', 'codename_c'])
  268. ->where('codeid', 'CUS_UDFPC')
  269. ->get();
  270. });
  271. }
  272. /**
  273. * @param $customerid
  274. * @param $sku
  275. * @return mixed
  276. * 根据customerid和sku 查询商品关联的批次属性规则
  277. */
  278. public function getBasSkuLotId($customerid, $sku)
  279. {
  280. return Cache::remember('bas_sku_lot_' . $customerid . '_' . $sku, 600, function () use ($customerid, $sku) {
  281. return OracleBasSKU::query()->select(['customerid', 'sku', 'lotid'])
  282. ->where('customerid', $customerid)
  283. ->where('sku', $sku)
  284. ->with('lotId')
  285. ->first();
  286. });
  287. }
  288. /**
  289. * @param $barcode
  290. * @return array
  291. * 根据条码获取 库存
  292. */
  293. public function getInvotlocid($barcode): array
  294. {
  295. $sql=<<<sql
  296. select INV_LOT_LOC_ID.CUSTOMERID,BAS_SKU.ALTERNATE_SKU1,INV_LOT_LOC_ID.LOCATIONID,INV_LOT_ATT.LOTATT05,INV_LOT_ATT.LOTATT08,
  297. sum(INV_LOT_LOC_ID.QTY) AS QTY from INV_LOT_LOC_ID
  298. left join BAS_SKU on INV_LOT_LOC_ID.CUSTOMERID=BAS_SKU.CUSTOMERID and INV_LOT_LOC_ID.SKU =BAS_SKU.SKU
  299. left join INV_LOT_ATT on INV_LOT_ATT.LOTNUM=INV_LOT_LOC_ID.LOTNUM
  300. where BAS_SKU.SKU in (select SKU from BAS_SKU where ALTERNATE_SKU1=? union
  301. select SKU from BAS_SKU where ALTERNATE_SKU2=? union
  302. select SKU from BAS_SKU where ALTERNATE_SKU3=? )
  303. group by INV_LOT_LOC_ID.CUSTOMERID,BAS_SKU.ALTERNATE_SKU1,INV_LOT_LOC_ID.LOCATIONID,INV_LOT_ATT.LOTATT05,INV_LOT_ATT.LOTATT08
  304. sql;
  305. $invLots = DB::connection("oracle")->select(DB::raw($sql), [$barcode, $barcode, $barcode]);
  306. if (!$invLots)return [];
  307. else return $invLots;
  308. }
  309. /**
  310. * @param string $barCode
  311. * @return array|int
  312. * 根据商品条码 获取完全收货状态 部分收货状态的 PA任务
  313. */
  314. public function getTsk($trackNumber,$barCode): array
  315. {
  316. $sql = <<<sql
  317. select t.*, DOC_ASN_DETAILS.RECEIVEDQTY
  318. from (select TSK_TASKLISTS.CustomerID,
  319. TSK_TASKLISTS.DOCNO,
  320. TSK_TASKLISTS.Sku,
  321. TSK_TASKLISTS.PlanToLotNum,
  322. TSK_TASKLISTS.PlanToID,
  323. DOC_ASN_DETAILS.SKUDESCRC,
  324. TSK_TASKLISTS.DOCLINENO,
  325. sum(TSK_TASKLISTS.PlanToQty) as qty
  326. from TSK_TASKLISTS
  327. LEFT JOIN DOC_ASN_DETAILS ON DOC_ASN_DETAILS.ASNNO = TSK_TASKLISTS.DOCNO AND
  328. DOC_ASN_DETAILS.ASNLINENO = TSK_TASKLISTS.DOCLINENO
  329. where TSK_TASKLISTS.TASKTYPE = 'PA'
  330. AND TSK_TASKLISTS.TASKPROCESS = '00'
  331. sql;
  332. if (!$trackNumber){ //没有输入条件 空扫
  333. $owner_codes = app('OwnerService')->getIntersectPermitting(['code']);
  334. if (count($owner_codes)>0){
  335. $sql.=' AND TSK_TASKLISTS.CustomerID IN (';
  336. foreach ($owner_codes as $index => $no){
  337. if ($index==0){
  338. $sql.="'".$no->code."'";
  339. continue;
  340. }
  341. $sql.=",'".$no->code."'";
  342. }
  343. $sql.=')';
  344. }else{
  345. $sql .= ' AND TSK_TASKLISTS.CustomerID IS NULL ';
  346. }
  347. }else{
  348. if (strpos(strtoupper($trackNumber), 'ASN') !== false){
  349. $sql.='AND TSK_TASKLISTS.DOCNO= ?'; //输入条件为asn单号
  350. } else{ //不为asn号时 判断是否为货主
  351. if ($this->checkUserOwnerAuth($trackNumber)){ //输入条件为货主
  352. $sql.=' AND TSK_TASKLISTS.CustomerID= ?';
  353. }else{
  354. $sql.=' AND TSK_TASKLISTS.PlanToID= ?'; //不是货主 判断是否为跟踪号
  355. }
  356. }
  357. }
  358. if ($barCode)$sql.=" AND TSK_TASKLISTS.sku in (select SKU from BAS_SKU where ALTERNATE_SKU1='".$barCode."' union
  359. select SKU from BAS_SKU where ALTERNATE_SKU2='".$barCode."' union
  360. select SKU from BAS_SKU where ALTERNATE_SKU3='".$barCode."' )";
  361. $sql.=' group by TSK_TASKLISTS.CustomerID, TSK_TASKLISTS.DOCNO, TSK_TASKLISTS.Sku, TSK_TASKLISTS.PlanToLotNum,
  362. TSK_TASKLISTS.PlanToID, DOC_ASN_DETAILS.SKUDESCRC, TSK_TASKLISTS.DOCLINENO) t
  363. left join DOC_ASN_DETAILS on t.DOCLINENO = DOC_ASN_DETAILS.ASNLINENO and t.DOCNO = DOC_ASN_DETAILS.ASNNO';
  364. if ($trackNumber){
  365. if ($this->checkUserOwnerAuth($trackNumber))$tasks = DB::connection("oracle")->select(DB::raw($sql), [strtoupper($trackNumber)]);
  366. else $tasks = DB::connection("oracle")->select(DB::raw($sql), [$trackNumber]);
  367. }else {$tasks = DB::connection("oracle")->select(DB::raw($sql));}
  368. if (!$tasks) return [];
  369. else return $tasks;
  370. }
  371. /**
  372. * @param $owner_code
  373. * @return bool
  374. * 判断当前用户货主权限
  375. */
  376. public function checkUserOwnerAuth($owner_code): bool
  377. {
  378. $owner_codes = app('OwnerService')->getIntersectPermitting(['code']);
  379. $owner=$owner_codes->where('code','=',strtoupper($owner_code));
  380. if ($owner->count()>0)return true;
  381. else return false;
  382. }
  383. /**
  384. * @throws \Throwable
  385. * flux手持端 上架
  386. */
  387. public function fluxHandPa(array $info, array $taskParam): bool
  388. {
  389. $tasks = $this->selectFluxTask($taskParam, $info['amount']);
  390. if (!$tasks) return false; //获取任务失败
  391. return DB::connection("oracle")->transaction(function () use ($tasks, $info) { //单体嵌套事务 回滚FLUX失败任务
  392. foreach ($tasks as $task) {
  393. $res=$this->checkExpiryPa($task,$info['location']);
  394. if ($res!==true)return $res;
  395. if (!app("StorageService")->fluxPA($task, $info['location'])) {
  396. DB::connection("oracle")->rollBack();
  397. return false; //上架失败
  398. }
  399. }
  400. return true; //上架成功
  401. });
  402. }
  403. /**
  404. * @param $task
  405. * @param $location
  406. * @return bool|int
  407. * 上架校验效期
  408. */
  409. public function checkExpiryPa($task,$location)
  410. {
  411. if (!$task->taskid)return 0;//任务id不存在
  412. if (strpos($task->taskid,'MIX') !== false)return true;//合并拣货,不校验
  413. $sql = <<<sql
  414. select instr(DESCR,'拣货') as var_IsPickingArea from BAS_Zone where ZONE=(select PutawayZone from BAS_Location where LocationID = ?)
  415. sql;
  416. $basZone = DB::connection("oracle")->selectOne(DB::raw($sql), [$location]);
  417. if ($basZone&&$basZone->var_ispickingarea>0) return true; //不是存储区,不校验
  418. $sql1 = <<<sql
  419. select SKU,LotAtt02,CustomerID from INV_LOT_ATT WHERE LOTNUM=?
  420. sql;
  421. $invLotAtt = DB::connection("oracle")->selectOne(DB::raw($sql1), [$task->fmlotnum]);
  422. $sql2 = <<<sql
  423. select count(*) as var_amountOfDecaying from INV_LOT_LOC_ID store
  424. left join BAS_Location location on store.LocationID=location.locationId
  425. left join BAS_Zone zone on zone.zone=location.PickZone
  426. left join INV_LOT_ATT attres on store.LOTNUM=attres.LOTNUM
  427. where store.SKU=?
  428. and store.CustomerID=?
  429. and store.LOTNUM!=?
  430. and attres.LotAtt02 > ?
  431. and zone.DESCR like '%拣货%'
  432. sql;
  433. $invLotLocId=DB::connection("oracle")->selectOne(DB::raw($sql2), [$invLotAtt->sku,$invLotAtt->customerid,$task->fmlotnum,$invLotAtt->lotatt02]);
  434. if ($invLotLocId&&$invLotLocId->var_amountofdecaying>0)return 1;//拣货区找到效期更新的同样货品,不能上架至存储区
  435. return true;
  436. }
  437. /**
  438. * @param $taskParam
  439. * @param $amount
  440. * @return array
  441. * 根据跟踪号,货主,sku,批次 获取任务列表 再通过数量 进行任务的重组(拆分或选定)
  442. */
  443. public function selectFluxTask($taskParam, $amount): array
  444. {
  445. /** @var StorageService $storageService */
  446. $storageService = app('StorageService');
  447. $sql = <<<sql
  448. select * from TSK_TASKLISTS where customerid = ? AND sku = ? AND plantoid = ? AND plantolotnum = ? AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
  449. sql;
  450. $tasks = DB::connection("oracle")->select(DB::raw($sql), [$taskParam['customerid'], $taskParam['sku'], $taskParam['plantoid'], $taskParam['plantolotnum']]);
  451. if (!$tasks) return [];
  452. $nums = [];
  453. $sum = 0;
  454. $maxIndex = null;
  455. foreach ($tasks as $i => $task) {
  456. if ((int)$task->fmqty == $amount) return [$task];
  457. $nums[] = (int)$task->fmqty;
  458. $sum += (int)$task->fmqty;
  459. if ((int)$task->fmqty > $amount) $maxIndex = $i;
  460. }
  461. if ($sum < $amount) return []; //上架数大于入库数
  462. $result = $storageService->getMatch($nums, $amount);
  463. if (!$result) return $storageService->splitTask($tasks, $maxIndex, $amount);
  464. $arr = [];
  465. foreach ($result as $index) $arr[] = $tasks[$index];
  466. return $arr;
  467. }
  468. /**
  469. * @throws \Throwable
  470. * fulx 手持收货
  471. */
  472. public function fluxHandIn(array $info)
  473. {
  474. $lotatt = array_filter($info, function ($key) {
  475. return strpos($key, 'lotatt') === 0;
  476. }, ARRAY_FILTER_USE_KEY);
  477. $invlotatt = [];
  478. for ($i = 1; $i <= 8; $i++) {
  479. $invlotatt["lotatt0{$i}"] = null;
  480. }
  481. foreach ($invlotatt as $key => &$item) {
  482. foreach ($lotatt as $key1 => $item1) {
  483. if ($key === $key1) $item = $item1;
  484. }
  485. }
  486. $who = 'WAS' . (Auth::user() ? '-' . Auth::user()["name"] : '');
  487. $time = Carbon::now()->toDateTimeString();
  488. return DB::connection("oracle")->transaction(function () use ($info, $invlotatt, $who, $time) {
  489. //flux 批次号
  490. $lotNum = $this->getOrCreateLotNum($info, $invlotatt, $who, $time);
  491. if (!$lotNum) {
  492. DB::connection("oracle")->rollBack();
  493. return false;
  494. }
  495. //flux 创建入库事务
  496. $actTransactionLog = $this->setFluxActTransactionLog($info, $lotNum, $who, $time);
  497. if (!$actTransactionLog) {
  498. DB::connection("oracle")->rollBack();
  499. return false;
  500. }
  501. //flux 创建上架任务
  502. $this->setFluxTskTaskListPA($info, $invlotatt, $actTransactionLog, $who, $time);
  503. //flux 完善库存余量
  504. $this->updateFluxInv($info, $lotNum, $who, $time, $actTransactionLog);
  505. //flux 更新asn_detail 和 asn_header 状态
  506. return $this->updateFluxAsn($info, $invlotatt, $time, $who);
  507. });
  508. }
  509. /**
  510. * @throws \Throwable
  511. */
  512. public function updateFluxAsn(array $info, array $invlotatt, $time, $who): bool
  513. {
  514. $db = DB::connection("oracle");
  515. $asn = OracleDOCASNHeader::query()
  516. ->withCount('asnDetails')
  517. ->with('asnDetails')
  518. ->where('asnno', $info['asnno'])
  519. ->first();
  520. if (!$asn || !$asn->asnDetails || !$asn->asn_details_count) return false;
  521. $asnDetails = $asn->asnDetails;
  522. $receiveAsn = null;
  523. foreach ($asnDetails as $asnDetail) {
  524. if ($asnDetail['asnno'] == $info['asnno'] &&
  525. $asnDetail['asnlineno'] == $info['asnlineno'] &&
  526. $asnDetail['customerid'] == $info['customerid'] &&
  527. $asnDetail['sku'] == $info['sku']) $receiveAsn = $asnDetail;
  528. }
  529. return $db->transaction(function () use ($db, $info, $receiveAsn, $invlotatt, $time, $who, $asn) {
  530. if ($receiveAsn && (int)$receiveAsn['receivedqty'] + (int)$info['amount'] < (int)$receiveAsn['expectedqty']) {
  531. //asn_detail 收货数量+已收数量<预期数量
  532. $db->update(DB::raw("UPDATE DOC_ASN_DETAILS SET receivedqty = receivedqty + ?,receivedqty_each = receivedqty_each + ?,linestatus = '30',holdrejectcode ='OK',
  533. reserve_flag ='Y',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),receivedtime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ?,
  534. lotatt01 =?,lotatt02 =?,lotatt03 =?,lotatt04 =?,lotatt05 =?,lotatt06 =?,lotatt07 =?,lotatt08=? WHERE asnno = ? and asnlineno = ?"),
  535. [(int)$info['amount'], (int)$info['amount'], $time, $time, $who, $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  536. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], $info['asnno'], $info['asnlineno']]);
  537. //asn_header 部分收货状态
  538. $db->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '30',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  539. [$time, $who, $info['asnno']]);
  540. } elseif ($receiveAsn && (int)$receiveAsn['receivedqty'] + (int)$info['amount'] == (int)$receiveAsn['expectedqty']) {
  541. //asn_detail 收货数量+已收数量=预期数量
  542. $db->update(DB::raw("UPDATE DOC_ASN_DETAILS SET receivedqty=receivedqty+?,receivedqty_each=receivedqty_each+?,linestatus = '40',
  543. edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),receivedtime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ?,holdrejectcode='OK',
  544. reserve_flag='Y',lotatt01=?,lotatt02=?,lotatt03=?,lotatt04=?,lotatt05=?,lotatt06=?,lotatt07=?,lotatt08=? WHERE asnno = ? and asnlineno = ?"),
  545. [(int)$info['amount'], (int)$info['amount'], $time, $time, $who, $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  546. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], $info['asnno'], $info['asnlineno']]);
  547. //当asn_detail 所有状态都为完全收货是 asn_header 状态修改为 完全收货(asnstatus=40)
  548. if (OracleDOCASNDetail::query()->where('asnno', $info['asnno'])->where('linestatus', 40)->count() == $asn->asn_details_count) {
  549. $db->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '40',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  550. [$time, $who, $info['asnno']]);
  551. } else {
  552. //asn_header 部分收货状态
  553. $db->update(DB::raw("UPDATE DOC_ASN_HEADER SET asnstatus = '30',edittime = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),editwho = ? WHERE asnno = ?"),
  554. [$time, $who, $info['asnno']]);
  555. }
  556. }
  557. return true;
  558. });
  559. }
  560. /**
  561. * @throws \Throwable
  562. */
  563. public function updateFluxInv(array $info, $lotNum, $who, $time, array $actTransactionLog)
  564. {
  565. $db = DB::connection("oracle");
  566. $db->transaction(function () use ($db, $info, $lotNum, $actTransactionLog, $who, $time) {
  567. //更新 inv_lot 批次 库存表
  568. $invLot = $db->selectOne(DB::raw("SELECT * FROM INV_LOT WHERE lotnum = ? AND customerid = ? AND sku = ? "), [
  569. $lotNum, $info['customerid'], $info['sku']
  570. ]);
  571. if ($invLot) $db->update(DB::raw("UPDATE INV_LOT SET qty = qty+?,edittime=?,editwho=? WHERE lotnum = ? AND customerid = ? AND sku = ?"), [
  572. (int)$info['amount'], $time, $who, $lotNum, $info['customerid'], $info['sku'],
  573. ]);
  574. 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'),?)"), [
  575. $lotNum, $info['customerid'], $info['sku'], $info['amount'], $time, $who, $time, $who
  576. ]);
  577. //更新 inv_lot_loc_id 批次/库位/跟踪号 库存表
  578. $invLotId = $db->selectOne(DB::raw("SELECT * FROM inv_lot_loc_id WHERE lotnum = ? AND locationid = ? AND customerid = ? AND sku = ? AND traceid = ? FOR UPDATE"), [
  579. $lotNum, $actTransactionLog['location'], $actTransactionLog['customerid'], $actTransactionLog['sku'], $actTransactionLog['trackid']
  580. ]);
  581. // if ($info['location']) { //存在目标库位
  582. // $invLotIdHasPreLocation = $db->selectOne(DB::raw("SELECT * FROM inv_lot_loc_id WHERE lotnum = ? AND locationid = ? AND customerid = ? AND sku = ? AND traceid = ? FOR UPDATE"), [
  583. // $lotNum, $info['location'], $actTransactionLog['customerid'], $actTransactionLog['sku'], $actTransactionLog['trackid']
  584. // ]);
  585. //
  586. // if ($invLotIdHasPreLocation) $db->update(DB::raw("UPDATE inv_lot_loc_id SET qtypa = qtypa+?,edittime=?,editwho=? WHERE lotnum = ? AND locationid = ? AND traceid = ?"), [
  587. // (int)$info['amount'], $time, $who, $lotNum, $info['location'], $actTransactionLog['trackid']
  588. // ]);
  589. // 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)"), [
  590. // $lotNum, $info['location'], $actTransactionLog['trackid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], 0, $time, $who, $time, $who, (int)$info['amount']
  591. // ]);
  592. //
  593. // if ($invLotId){
  594. // $db->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty+?,qtymvout = qtymvout+?,edittime=?,editwho=? WHERE lotnum = ? AND locationid = ? AND traceid = ?"), [
  595. // (int)$info['amount'], (int)$info['amount'], $time, $who, $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid']
  596. // ]);
  597. // } else {
  598. // $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)"), [
  599. // $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], (int)$info['amount'], (int)$info['amount'],
  600. // $time, $who, $time, $who,
  601. // ]);
  602. // }
  603. //
  604. // }
  605. if ($invLotId) $db->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty+?,qtymvout = qtymvout+?,edittime=?,editwho=? WHERE lotnum = ? AND locationid = ? AND traceid = ?"), [
  606. (int)$info['amount'],(int)$info['amount'], $time, $who, $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid']
  607. ]);
  608. 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)"), [
  609. $lotNum, $actTransactionLog['location'], $actTransactionLog['trackid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], (int)$info['amount'], (int)$info['amount'], $time, $who, $time, $who,
  610. ]);
  611. });
  612. }
  613. /**
  614. * @throws \Throwable
  615. */
  616. public function setFluxTskTaskListPA(array $info, array $invlotatt, $actTransactionLog, $who, $time)
  617. {
  618. $db = DB::connection("oracle");
  619. $db->transaction(function () use ($db, $info, $invlotatt, $actTransactionLog, $who, $time) {
  620. $sql = <<<sql
  621. INSERT INTO TSK_TASKLISTS VALUES(?,'1','PA',?,?,'ASN',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,null,null,?,?,?,?,?,?,?,?,null,null,null,null,
  622. 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,
  623. ?,?,?,'N',null,?,'*',null,null,null,'N',null,null)
  624. sql;
  625. $db->insert(DB::raw($sql), [
  626. $actTransactionLog['tsid'], $actTransactionLog['customerid'], $actTransactionLog['sku'], $actTransactionLog['docno'], $actTransactionLog['doclineno'],
  627. $actTransactionLog['lotNum'], $actTransactionLog['packid'], 'EA', $info['amount'], $info['amount'], $actTransactionLog['location'], $actTransactionLog['location'],
  628. $actTransactionLog['trackid'], $actTransactionLog['lotNum'], $actTransactionLog['packid'], 'EA', $info['amount'], $info['amount'],
  629. $info['location'], $info['location'], $actTransactionLog['trackid'], '00', 'Putaway Task', '3', $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  630. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], $actTransactionLog['trid'], $who, $time, null, null, null, null,
  631. $actTransactionLog['userdefine1'], $actTransactionLog['userdefine2'], $actTransactionLog['userdefine3'], $actTransactionLog['warehouseid']
  632. ]);
  633. });
  634. }
  635. /**
  636. * @param array $info
  637. * @param $lotNum
  638. * @param $who
  639. * @param $time
  640. * @return mixed
  641. * @throws \Throwable
  642. * 创建入库事务
  643. */
  644. public function setFluxActTransactionLog(array $info, $lotNum, $who, $time)
  645. {
  646. $db = DB::connection("oracle");
  647. return $db->transaction(function () use ($db, $info, $lotNum, $time, $who) {
  648. if ($info['trackNumber']) $trackNumber = $info['trackNumber'];
  649. else $trackNumber = substr(md5($time), 0, 30);
  650. $asnHeader = OracleDOCASNHeader::query()->where('asnno', $info['asnno'])->first();
  651. $asnDetail = OracleDOCASNDetail::query()->where('asnno', $info['asnno'])->where('sku', $info['sku'])->first();
  652. $sql = <<<sql
  653. INSERT INTO ACT_TRANSACTION_LOG VALUES(?,'IN',?,?,?,?,'ASN',?,?,?,?,?,?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,
  654. TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,0,0,0,0,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,?,null,null,null,?,?,?,?,?,?,?,?,
  655. ?,?,?,?,'1','Y',null,?,?,?,?,null,null,?,null,null)
  656. sql;
  657. list($trid, $max) = app('StorageService')->getTrNumber();
  658. list($tsid, $max) = $this->getTsNum();
  659. $db->insert(DB::raw($sql), [
  660. $trid, $asnDetail->customerid, $asnDetail->sku,
  661. $asnDetail->asnno, $asnDetail->asnlineno, $lotNum, $asnDetail->receivinglocation, '*', $asnDetail->packid, 'EA', $info['amount'], $info['amount'], '99', $time, $who,
  662. $time, $who, $time, $asnDetail->customerid, $asnDetail->sku, $trackNumber, $asnDetail->receivinglocation, $who, $asnDetail->packid, 'EA', $info['amount'], $info['amount'], $lotNum,
  663. '*', '0', 'N', $tsid, substr($asnDetail->receivinglocation, -4), $asnHeader->userdefine1, $asnHeader->userdefine2,
  664. $asnHeader->userdefine3, 'O'
  665. ]);
  666. app('StorageService')->setTrNumber();
  667. $this->setTsNum();
  668. $actTransactionLog = [
  669. 'trid' => $trid, 'docno' => $asnDetail->asnno, 'customerid' => $asnDetail->customerid, 'sku' => $asnDetail->sku, 'doclineno' => $asnDetail->asnlineno, 'lotNum' => $lotNum, 'location' => $asnDetail->receivinglocation,
  670. 'packid' => $asnDetail->packid, 'tsid' => $tsid, 'warehouseid' => substr($asnDetail->receivinglocation, -4), 'userdefine1' => $asnHeader->userdefine1, 'userdefine2' => $asnHeader->userdefine2,
  671. 'userdefine3' => $asnHeader->userdefine3, 'trackid' => $trackNumber
  672. ];
  673. return $actTransactionLog;
  674. });
  675. }
  676. /**
  677. * @param array $info
  678. * @return mixed
  679. * @throws \Throwable
  680. * 或去flux 批次号
  681. */
  682. public function getOrCreateLotNum(array $info, array $invlotatt, $who, $time)
  683. {
  684. $invlotatt['customerid'] = $info['customerid'];
  685. $invlotatt['sku'] = $info['sku'];
  686. //根据批次规则查询或新建批次
  687. $lotnum = OracleInvLotAtt::query()->where($invlotatt)->value('lotnum');
  688. if ($lotnum) return $lotnum;
  689. $db = DB::connection("oracle");
  690. list($num, $max) = $this->getLtNum();
  691. return $db->transaction(function () use ($db, $info, $invlotatt, $num, $who, $time) {
  692. $sql = <<<sql
  693. INSERT INTO INV_LOT_ATT VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,
  694. TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?)
  695. sql;
  696. $db->insert(DB::raw($sql), [
  697. $num, $info['customerid'], $info['sku'], $invlotatt['lotatt01'], $invlotatt['lotatt02'], $invlotatt['lotatt03'], $invlotatt['lotatt04'],
  698. $invlotatt['lotatt05'], $invlotatt['lotatt06'], $invlotatt['lotatt07'], $invlotatt['lotatt08'], null, null, null, null, $time, $who, $time, $who, $time, null
  699. ]);
  700. $this->setLtNum();
  701. return $num;
  702. });
  703. }
  704. /**
  705. * 获取批次号
  706. * @return array
  707. */
  708. private function getLtNum(): array
  709. {
  710. $val = ValueStore::query()->select("value")->where("name", "flux_lt_number")->lockForUpdate()->first();
  711. if (!$val) $val = ValueStore::query()->create(["name" => "flux_lt_number", "value" => '0']);
  712. $max = $val->value + 1;
  713. $number = sprintf("%07d", $max);
  714. return array('WLT' . $number, $max);
  715. }
  716. /**
  717. * 设置批次号
  718. */
  719. private function setLtNum()
  720. {
  721. ValueStore::query()
  722. ->select("value")
  723. ->where("name", "flux_lt_number")
  724. ->update(["value" => DB::raw("value+1")]);
  725. }
  726. /**
  727. * 获取批次号
  728. * @return array
  729. */
  730. private function getTsNum(): array
  731. {
  732. $val = ValueStore::query()->select("value")->where("name", "flux_ts_number")->lockForUpdate()->first();
  733. if (!$val) $val = ValueStore::query()->create(["name" => "flux_ts_number", "value" => '0']);
  734. $max = $val->value + 1;
  735. $number = sprintf("%07d", $max);
  736. return array('WTS' . $number, $max);
  737. }
  738. /**
  739. * 设置批次号
  740. */
  741. private function setTsNum()
  742. {
  743. ValueStore::query()
  744. ->select("value")
  745. ->where("name", "flux_ts_number")
  746. ->update(["value" => DB::raw("value+1")]);
  747. }
  748. }