Create inventory transfer journal from production picking list

Pievienoja Džordžs Bārens Pr, 23.08.2021 - 15:04

To create inventory transfer journal from production picking list, add a button to ProdJournalTable form and put this code under Override Method - Clicked

void clicked()
{
ProdJournalBOM prodJournalBOM;
InventDim inventDim;
InventJournalTable inventJournalTable;
InventJournalTrans inventJournalTrans;
NumberSeq numSeq;
InventDim fromInventDim, toInventDim;
Args args;
;
// check if journal is empty
if(ProdJournalTable.NumOfLines > 0)
{
ttsbegin;
// create new transfer journal
inventJournalTable.clear();
numSeq = NumberSeq::newGetNum(InventParameters::numRefTransferId());
inventJournalTable.initFromInventJournalName(InventJournalName::find(InventParameters::find().TransferJournalNameId));
inventJournalTable.Description = strfmt("Production %1, journal %2", ProdJournalTable.ProdId, ProdJournalTable.JournalId);
inventJournalTable.SystemBlocked = true;
inventJournalTable.insert();
while select * from prodJournalBOM
where (prodjournalBOM.JournalId == ProdjournalTable.JournalId && prodjournalBOM.BOMConsump > 0)
{
// find dimensions
inventDim = inventDim::find(prodJournalBOM.InventDimId);
fromInventDim.InventLocationId = "Location 1";
fromInventDim.wMSLocationId = "DEFAULT";
fromInventDim.inventBatchId = inventDim.inventBatchId;
fromInventDim.inventSerialId = inventDim.inventSerialId;
fromInventDim = InventDim::findOrCreate(fromInventDim);
toInventDim.InventLocationId = "Location 2";
toInventDim.wMSLocationId = "DEFAULT";
toInventDim.inventBatchId = inventDim.inventBatchId;
toInventDim.inventSerialId = inventDim.inventSerialId;
toInventDim = InventDim::findOrCreate(toInventDim);
// insert new line
inventJournalTrans.clear();
inventJournalTrans.initFromInventJournalTable(inventJournalTable);
inventJournalTrans.TransDate = ProdTable::find(ProdJournalTable.ProdId).StUpDate; // transfer date = production start date
inventJournalTrans.ItemId = prodJournalBOM.ItemId;
inventJournalTrans.InventDimId = fromInventDim.inventDimId;
inventJournalTrans.initFromInventTable(InventTable::find(prodJournalBOM.ItemId));
inventJournalTrans.ToInventDimId = toInventDim.inventDimId;
inventJournalTrans.Qty = prodJournalBOM.BOMConsump * (-1); // negative
inventJournalTrans.insert();
}
inventJournalTable.SystemBlocked = false;
inventJournalTable::initTotal(inventJournalTable);
inventJournalTable.update();
ttscommit;
// show transfer journal
args = new Args();
args.record(InventJournalTable::find(inventJournalTable.JournalId));
new MenuFunction(MenuItemDisplayStr(InventJournalTable), MenuItemType::Display).run(args);
}
else
{
info(strfmt("Not created, journal %1 is empty", ProdJournalTable.JournalId));
}
super();
}