HandInStorageService.php 42 KB

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