|
|
@@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Model;
|
|
|
use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
class StorageService
|
|
|
{
|
|
|
@@ -237,7 +238,7 @@ class StorageService
|
|
|
*
|
|
|
* @return array|null
|
|
|
*/
|
|
|
- public function getFluxTask($asn,$barCode,$amount):?array
|
|
|
+ public function getFluxTask(string $asn,string $barCode,int $amount):array
|
|
|
{
|
|
|
$sql = <<<sql
|
|
|
SELECT * FROM DOC_ASN_DETAILS LEFT JOIN BAS_SKU ON DOC_ASN_DETAILS.CUSTOMERID = BAS_SKU.CUSTOMERID AND DOC_ASN_DETAILS.SKU = BAS_SKU.SKU
|
|
|
@@ -246,15 +247,123 @@ WHERE ASNNO = ? AND (ALTERNATE_SKU1 = ? OR ALTERNATE_SKU2 = ? OR ALTERNATE_SKU3
|
|
|
AND TASKPROCESS = '00' AND TASKTYPE = 'PA'
|
|
|
sql;
|
|
|
$asns = DB::connection("oracle")->select(DB::raw($sql),[$asn,$barCode,$barCode,$barCode,$amount]);
|
|
|
- if (!$asns)return null;
|
|
|
+ if (!$asns)return [];
|
|
|
$nums = [];
|
|
|
- foreach ($asns as $asn){
|
|
|
+ $sum = 0;
|
|
|
+ $maxIndex = null;
|
|
|
+ foreach ($asns as $i => $asn){
|
|
|
if ((int)$asn->fmqty == $amount)return [$asn];
|
|
|
$nums[] = (int)$asn->fmqty;
|
|
|
+ $sum += (int)$asn->fmqty;
|
|
|
+ if ((int)$asn->fmqty>$amount)$maxIndex = $i;
|
|
|
}
|
|
|
- $result = $this->twoSum($nums,$amount);
|
|
|
- if ($result)return [$asns[$result[0]],$asns[$result[1]]];
|
|
|
- return null;
|
|
|
+ if ($sum<$amount)return []; //上架数大于入库数
|
|
|
+ $result = $this->getMatch($nums,$amount);
|
|
|
+ if (!$result)return $this->splitTask($asns,$maxIndex,$amount);
|
|
|
+ $arr = [];
|
|
|
+ foreach ($result as $index)$arr[] = $asns[$index];
|
|
|
+ return $arr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拆分任务
|
|
|
+ * @param array $asns
|
|
|
+ * @param int|null $maxIndex
|
|
|
+ * @param int $amount
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function splitTask($asns,$maxIndex,$amount):array
|
|
|
+ {
|
|
|
+ $result = [];
|
|
|
+ if ($maxIndex===null){
|
|
|
+ foreach ($asns as $asn){
|
|
|
+ if ($amount>(int)$asn->fmqty){
|
|
|
+ $result[] = $asn;
|
|
|
+ $amount-=(int)$asn->fmqty;
|
|
|
+ }else $splitTarget = $asn;
|
|
|
+ }
|
|
|
+ }else $splitTarget = $asns[$maxIndex];
|
|
|
+ $result[] = $this->copyTask($splitTarget,$amount);
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param \stdClass $task
|
|
|
+ * @param int $amount
|
|
|
+ *
|
|
|
+ * @return \stdClass
|
|
|
+ *
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ private function copyTask($task,$amount)
|
|
|
+ {
|
|
|
+ DB::connection("oracle")->beginTransaction();
|
|
|
+ try {
|
|
|
+ $columns = '';
|
|
|
+ $values = '';
|
|
|
+ $seq = 0;
|
|
|
+ foreach ($task as $key=>$val){
|
|
|
+ if (Str::upper($key)=='TASKID_SEQUENCE') {
|
|
|
+ $taskMax = DB::connection("oracle")->selectOne(DB::raw("select MAX(TASKID_SEQUENCE) maxseq from TSK_TASKLISTS where taskid = ?"),[$task->taskid]);
|
|
|
+ $val = $taskMax->maxseq + 1;
|
|
|
+ $seq = $val;
|
|
|
+ }
|
|
|
+ if (Str::upper($key)=='FMQTY' || Str::upper($key)=='FMQTY_EACH')$val = $amount;
|
|
|
+ if (Str::upper($key)=='FMID')$val = "WAS".$val;
|
|
|
+ $columns .= "'".$key."',";
|
|
|
+ $values .= "'".$val."',";
|
|
|
+ }
|
|
|
+ $columns = mb_substr($columns,0,-1);
|
|
|
+ $values = mb_substr($values,0,-1);
|
|
|
+ $sql = <<<sql
|
|
|
+ INSERT INTO TSK_TASKLISTS({$columns}) VALUES({$values})
|
|
|
+sql;
|
|
|
+ DB::connection("oracle")->insert(DB::raw($sql));
|
|
|
+ DB::connection("oracle")->update(DB::raw("UPDATE TSK_TASKLISTS SET FMQTY = FMQTY-?,FMQTY_EACH = FMQTY_EACH-? WHERE TASKID = ? AND TASKID_SEQUENCE = ?"),[
|
|
|
+ $amount,$amount,$task->taskid,$task->taskid_sequence
|
|
|
+ ]);
|
|
|
+ $invs = DB::connection("oracle")->select(DB::raw("SELECT * FROM INV_LOT_LOC_ID WHERE LOTNUM = ? AND LOCATIONID IN (?,?) AND TRACEID = ?"),[
|
|
|
+ $task->fmlotnum,$task->fmlocation,$task->plantolocation,$task->fmid
|
|
|
+ ]);
|
|
|
+ foreach ($invs as $inv){
|
|
|
+ $columns = '';
|
|
|
+ $values = '';
|
|
|
+ if ($inv->locationid==$task->fmlocation){
|
|
|
+ DB::connection("oracle")->update(DB::raw("UPDATE inv_lot_loc_id SET qty = qty-? WHERE lotnum = ? AND locationid = ? AND traceid = ?"),[
|
|
|
+ $amount,$inv->lotnum,$inv->locationid,$inv->traceid
|
|
|
+ ]);
|
|
|
+ foreach ($inv as $key=>$val){
|
|
|
+ if (Str::upper($key)=='TRACEID') $val = "WAS".$task->fmid;
|
|
|
+ if (Str::upper($key)=='QTY') $val = $amount;
|
|
|
+ $columns .= "'".$key."',";
|
|
|
+ $values .= "'".$val."',";
|
|
|
+ }
|
|
|
+ $columns = mb_substr($columns,0,-1);
|
|
|
+ $values = mb_substr($values,0,-1);
|
|
|
+ DB::connection("oracle")->insert(DB::raw("INSERT INTO inv_lot_loc_id({$columns}) VALUES({$values})"));
|
|
|
+ }
|
|
|
+ if ($inv->locationid==$task->plantolocation){
|
|
|
+ DB::connection("oracle")->update(DB::raw("UPDATE inv_lot_loc_id SET QTYPA = QTYPA-? WHERE lotnum = ? AND locationid = ? AND traceid = ?"),[
|
|
|
+ $amount,$inv->lotnum,$inv->locationid,$inv->traceid
|
|
|
+ ]);
|
|
|
+ foreach ($inv as $key=>$val){
|
|
|
+ if (Str::upper($key)=='TRACEID') $val = "WAS".$task->fmid;
|
|
|
+ if (Str::upper($key)=='QTYPA') $val = $amount;
|
|
|
+ $columns .= "'".$key."',";
|
|
|
+ $values .= "'".$val."',";
|
|
|
+ }
|
|
|
+ $columns = mb_substr($columns,0,-1);
|
|
|
+ $values = mb_substr($values,0,-1);
|
|
|
+ DB::connection("oracle")->insert(DB::raw("INSERT INTO inv_lot_loc_id({$columns}) VALUES({$values})"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DB::connection("oracle")->commit();
|
|
|
+ }catch(\Exception $e) {
|
|
|
+ DB::connection("oracle")->rollBack();
|
|
|
+ throw new \Exception("拆分任务失败:".$e->getMessage());
|
|
|
+ }
|
|
|
+ return DB::connection("oracle")->selectOne(DB::raw("SELECT * FROM TSK_TASKLISTS WHERE TASKID = ? AND TASKID_SEQUENCE = ?"),[$task->taskid,$seq]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -264,14 +373,23 @@ sql;
|
|
|
* @param Integer $target
|
|
|
* @return Integer[]|null
|
|
|
*/
|
|
|
- protected function twoSum($nums, $target) {
|
|
|
+ protected function getMatch(array $nums,int $target) :?array
|
|
|
+ {
|
|
|
$map=[];
|
|
|
- for($i=0;$i<count($nums);$i++){
|
|
|
- $complement=$target-$nums[$i];
|
|
|
- if(array_key_exists($complement,$map)){
|
|
|
- return [$map[$complement],$i];
|
|
|
+ foreach ($nums as $index=>$val){
|
|
|
+ $complement=$target-$val;
|
|
|
+ if(array_key_exists($complement,$map))return [$map[$complement],$index];
|
|
|
+ if ($val==$target)return [$index];
|
|
|
+ $map[$val]=$index;
|
|
|
+ if ($val<$target){
|
|
|
+ $temp = $nums;
|
|
|
+ unset($temp[$index]);
|
|
|
+ $arr = $this->getMatch($temp,$target-$val);
|
|
|
+ if ($arr) {
|
|
|
+ $arr[] = $index;
|
|
|
+ return $arr;
|
|
|
+ }
|
|
|
}
|
|
|
- $map[$nums[$i]]=$i;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -296,22 +414,6 @@ sql;
|
|
|
if (!$inv)return false;//余量与入库不符
|
|
|
DB::connection("oracle")->transaction(function ()use($inv,$amount,$ide,$asn,&$who){
|
|
|
$db = DB::connection("oracle");
|
|
|
- /*$qty = $amount;
|
|
|
- foreach ($inv as $in){
|
|
|
- if ($qty==0)break;
|
|
|
- if ($in->qty > $qty){
|
|
|
- $db->update(DB::raw("update inv_lot_loc_id set qty = qty-?,qtymvout = qty-? where lotnum = ? and locationid = ? and traceid = ?"),[
|
|
|
- $qty,$qty,$in->lotnum,$in->locationid,$in->traceid
|
|
|
- ]);
|
|
|
- $in->qty = $in->qty-$qty;
|
|
|
- $qty = 0;
|
|
|
- }else{
|
|
|
- $db->delete(DB::raw("DELETE FROM inv_lot_loc_id WHERE lotnum = ? and locationid = ? and traceid = ?"),[
|
|
|
- $in->lotnum,$in->locationid,$in->traceid
|
|
|
- ]);
|
|
|
- $qty = $qty-$in->qty;
|
|
|
- }
|
|
|
- }*/
|
|
|
$db->delete(DB::raw("DELETE FROM inv_lot_loc_id WHERE lotnum = ? AND traceid = ? AND traceid != '*' AND qty = 0"),[
|
|
|
$inv->lotnum,$inv->traceid
|
|
|
]);
|
|
|
@@ -341,13 +443,13 @@ sql;
|
|
|
]);
|
|
|
$this->setTrNumber($max);
|
|
|
$sql = <<<sql
|
|
|
-update TSK_TASKLISTS set TASKPROCESS = '99',REASONCODE = 'OK',PLANTOLOCATION = ?,PLANLOGICALTOSEQUENCE = ?,
|
|
|
-CREATE_TRANSACTIONID = ?,OPENWHO = ?,OPENTIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),
|
|
|
+update TSK_TASKLISTS set TASKPROCESS = '99',REASONCODE = 'OK',PLANTOLOCATION = ?,PLANLOGICALTOSEQUENCE = ?,FMID = '*'
|
|
|
+COMPLETED_TRANSACTIONID = ?,OPENWHO = ?,OPENTIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),
|
|
|
CLOSEWHO = ?,CLOSETIME = ?,EDITTIME = ?,EDITWHO = ?
|
|
|
where taskid = ? AND TASKID_SEQUENCE = ?
|
|
|
sql;
|
|
|
$db->update(DB::raw($sql),[
|
|
|
- $ide,$ide,$trid,$who,date("Y-m-d H:i:s"),$who,date("Y-m-d H:i:s"),date("Y-m-d H:i:s"),$who,$asn->taskid,$asn->taskid_sequence
|
|
|
+ $ide,'0',$trid,$who,date("Y-m-d H:i:s"),$who,date("Y-m-d H:i:s"),date("Y-m-d H:i:s"),$who,$asn->taskid,$asn->taskid_sequence
|
|
|
]);
|
|
|
});
|
|
|
//成功后应去修改ASN状态及数量 暂时不知上架后ASN应为状态
|