HandInStorageService.php 33 KB

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