HandInStorageService.php 44 KB

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