HandInStorageService.php 41 KB

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