/// <summary>
/// Service class for move inventory
/// </summary>
class MovementTransferService extends sysoperationservicebase
{
QueryRun queryRun;
/// <summary>
/// method to call Initquery
/// </summary>
public void new()
{
this.initQuery();
}
/// <summary>
/// descrption of the service class
/// </summary>
/// <returns>test</returns>
static ClassDescription description()
{
return 'Movement transfer journals';
}
/// <summary>
/// initilaize query for the process
/// </summary>
public void initQuery()
{
Query query = new Query();
query.addDataSource(tablenum(TransactionBase));
queryRun = new QueryRun(query);
}
/// <summary>
/// Returns query run for the query
/// </summary>
/// <returns>QR</returns>
public QueryRun queryRun()
{
return queryRun;
}
/// <summary>
/// to call the process method
/// </summary>
public void run()
{
//2. Query Processing
this.initQuery();
QueryBuildDataSource qBDS = queryRun.query().dataSourceTable(tablenum(TransactionBase));
qBDS.addRange(Fieldnum(TransactionBase , TransactionType)).value('OTransferMaterial');
qBDS.addRange(Fieldnum(TransactionBase , Task)).value('ProcessOTransferMaterial');
qBDS.addRange(fieldNum(TransactionBase, IsProcessed)).value(SysQuery::value(Isprocessed::NotProcessed));
while (queryRun.next())
{
TransactionBase TransactionBase = queryRun.get(tablenum(MV2TransactionBase));
this.processRecord(TransactionBase);
}
}
/// <summary>
/// Proceesed records
/// </summary>
/// <param name = "_TransactionBase">TransactionBase</param>
public void processRecord(TransactionBase _TransactionBase)
{
TransactionBase TransactionBaseUPD;
InventTable inventTable;
InventJournalTrans inventJournalTrans;
InventJournalTable inventJournalTable;
InventDim frominventDim,ToinventDim, inventdimfnd;
InventDimCombination inventdimcombo;
WMSLocation fromwMSLocation, toWmslocation;
InventoryTransactions inventoryTrans;
tolocation mv2tolocation;
int lineNum;
;
try
{
ttsbegin;
InventParameters inventParameters = InventParameters::find();
inventoryTrans = InventoryTransactions::findbyId(_TransactionBase.ID);
if(!inventoryTrans)
{
throw Global::error('Inventory transactions are empty');
}
mv2tolocation = mv2tolocation::findbyId(_MV2TransactionBase.ID);
if(!mv2tolocation)
{
throw Global::error('ToLocations are does not exists');
}
inventJournalTable.clear();
inventJournalTable.initValue();
inventJournalTable.initFromInventJournalName(InventJournalName::find(inventParameters.TransferJournalNameId));
if (inventJournalTable.validateField(fieldNum(InventJournalTable, JournalNameId)))
{
inventJournalTable.modifiedField(fieldNum(InventJournalTable, JournalNameId));
inventJournalTable.JournalType = InventJournalType::Transfer;
inventJournalTable.Description = 'Inventory Transfer Journal';
inventJournalTable.insert();
}
inventJournalTrans.clear();
inventJournalTrans.initFromInventJournalTable(inventJournalTable);
inventJournalTrans.TransDate = DateTimeUtil::date(_MV2TransactionBase.TransactionDate);
//get item details
inventdimcombo = InventDimCombination::findVariantId(inventoryTrans.ItemNumber);
if(inventdimcombo)
{
inventTable = inventdimcombo.inventTable();
}
else
{
inventTable = InventTable::find(inventoryTrans.ItemNumber);
}
inventJournalTrans.initFromInventTable(inventTable);
inventJournalTrans.LineNum = lineNum;
inventJournalTrans.modifiedField(fieldNum(InventJournalTrans, ItemId));
frominventDim.clear();
frominventDim.initValue();
frominventDim.InventSiteId = InventSite::findRecId(str2Int64(inventoryTrans.SiteKey)).SiteId;
frominventDim.InventLocationId = InventLocation::findRecId(str2Int64(inventoryTrans.WarehouseKey)).InventLocationId;
select firstonly fromwMSLocation
where fromwMSLocation.RecId == str2Int64(inventoryTrans.InventoryLocationKey);
frominventDim.wMSLocationId = fromwMSLocation.wMSLocationId;
//get product dimensions from Varaint
inventdimfnd = InventDim::findInLegalEntity(inventdimcombo.InventDimId, curExt());
//from Inventory details
frominventDim.InventColorId = inventdimfnd.InventColorId;
frominventDim.InventSizeId = inventdimfnd.InventSizeId;
frominventDim.configId = inventdimfnd.configId;
frominventDim.InventStyleId = inventdimfnd.InventStyleId;
frominventDim.InventVersionId = inventdimfnd.InventVersionId;
//batch lot number
if(EcoResTrackingDimensionGroup::find(inventTable.trackingDimensionGroup()).Name=='Batch-Phy')
{
InventBatch inventbatch = InventBatch::findOrCreateWithProdDate(inventoryTrans.BatchLotNumber,
inventTable.ItemId,
DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()));
if(inventbatch)
{
frominventDim.inventBatchId = inventbatch.inventBatchId;
}
}
frominventDim = InventDim::findOrCreate(frominventDim);
//to inventory details
ToinventDim.clear();
ToinventDim.initValue();
ToinventDim.InventSiteId = InventSite::findRecId(str2Int64(mv2tolocation.ToSiteKey)).SiteId;
ToinventDim.InventLocationId = InventLocation::findRecId(str2Int64(mv2tolocation.ToWarehouseKey)).InventLocationId;
select firstonly toWmslocation
where toWmslocation.RecId == str2Int64(mv2tolocation.ToInventoryLocationKey);
ToinventDim.wMSLocationId = toWmslocation.wMSLocationId;
ToinventDim.InventColorId = inventdimfnd.InventColorId;
ToinventDim.InventSizeId = inventdimfnd.InventSizeId;
ToinventDim.configId = inventdimfnd.configId;
ToinventDim.InventStyleId = inventdimfnd.InventStyleId;
ToinventDim.InventVersionId = inventdimfnd.InventVersionId;
if(frominventDim.inventBatchId)
{
ToinventDim.inventBatchId = frominventDim.inventBatchId;
}
ToinventDim = InventDim::findOrCreate(ToinventDim);
inventJournalTrans.InventDimId = frominventDim.inventDimId;
inventJournalTrans.ToInventDimId = ToinventDim.inventDimId;
inventJournalTrans.modifiedField(fieldNum(InventJournalTrans, inventDimId));
inventJournalTrans.Qty = inventoryTrans.Quantity;
inventJournalTrans.modifiedField(fieldNum(inventJournalTrans, Qty));
inventJournalTrans.JournalType = InventJournalType::Transfer;
inventJournalTrans.insert();
if(MV2Parameters::find().PostMoveInventory == Noyes::Yes)
{
this.postInventTransferJouranl(inventJournalTrans.JournalId);
}
// update in log form
mV2TransactionBaseUPD = mV2TransactionBase::findbyId(_MV2TransactionBase.ID, true);
mV2TransactionBaseUPD.IsProcessed = MV2Isprocessed::Processed;
mV2TransactionBaseUPD.MV2JournalId = inventJournalTable.JournalId;
mV2TransactionBaseUPD.ErrorDescription = ' ';
mV2TransactionBaseUPD.update();
ttscommit;
}
catch (Exception::Error)
{
str errormessage = infolog.text(infolog.line());
ttsbegin;
mV2TransactionBaseUPD = mV2TransactionBase::findbyId(_MV2TransactionBase.ID, true);
mV2TransactionBaseUPD.IsProcessed = MV2Isprocessed::Error;
mV2TransactionBaseUPD.MV2JournalId = nullValueFromType(Types::String);
mV2TransactionBaseUPD.ErrorDescription = errormessage;
mV2TransactionBaseUPD.update();
ttscommit;
}
}
/// <summary>
/// method is used for posting the journal
/// </summary>
/// <param name = "_journalId">journalnum</param>
public void postInventTransferJouranl(JournalId _journalId)
{
InventJournalTable inventJournalTable;
InventJournalTrans inventJournalTrans;
JournalCheckPost journalCheckPost;
boolean throwserror=true;
boolean showinforesult=true;
inventJournalTable = InventJournalTable::find(_journalId,true);
try
{
ttsbegin;
inventJournalTable.SystemBlocked = NoYes::Yes;
inventJournalTable.update();
journalCheckPost =
InventJournalCheckPost::newJournalCheckPost(JournalCheckpostType::Check,inventJournalTable);
journalCheckPost.parmThrowCheckFailed(throwserror);
journalCheckPost.parmShowInfoResult(showinforesult);
journalCheckPost.run();
inventJournalTable.SystemBlocked = NoYes::No;
inventJournalTable.Posted = NoYes::Yes;
select count(RecId) from inventJournalTrans where inventJournalTrans.JournalId == inventJournalTable.JournalId;
inventJournalTable.NumOfLines = inventJournalTrans.recid;
inventJournalTable.update();
ttscommit;
}
catch
{
throw exception::UpdateConflict;
}
}
}