Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
374 lines (310 sloc) 17.3 KB
<?xml version="1.0" encoding="utf-8"?>
<AxClass xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Name>DEVTutorialCreateLedgerJournal</Name>
<SourceCode>
<Declaration><![CDATA[
class DEVTutorialCreateLedgerJournal
{
}
]]></Declaration>
<Methods>
<Method>
<Name>main</Name>
<Source><![CDATA[
public static void main(Args _args)
{
Dialog dialog = new Dialog("Copy ledger journal");
DialogField dfSourceJournal = dialog.addField(extendedTypeStr(LedgerJournalId), "Source journal");
dialog.addText("Copy type(0-LedgerJournalEngine, 1-DataEntity, 2-Table defaultRow, 10-createByCombination)");
DialogField dfCopyType = dialog.addField(extendedTypeStr(Integer));
LedgerJournalTable ledgerJournalTableOrig;
LedgerJournalTrans ledgerJournalTransOrig;
if (dialog.run())
{
int numLines;
Integer copyType = dfCopyType.value();
if (copyType != 10)
{
ledgerJournalTableOrig = LedgerJournalTable::find(dfSourceJournal.value());
if (! ledgerJournalTableOrig)
{
throw error(strFmt("Journal not found"));
}
}
else
{
numLines = str2Int(dfSourceJournal.value());
if (! numLines)
{
throw error(strFmt("Specify number of lines"));
}
}
DEVTutorialCreateLedgerJournal tutorialCreateLedgerJournal = new DEVTutorialCreateLedgerJournal();
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
real timeMs;
stopwatch.Start();
ttsbegin;
switch (copyType)
{
case 0:
numLines = tutorialCreateLedgerJournal.createJournalUsingLedgerJournalEngine(ledgerJournalTableOrig);
break;
case 1:
numLines = tutorialCreateLedgerJournal.createJournalUsingEntity(ledgerJournalTableOrig);
break;
case 2:
numLines = tutorialCreateLedgerJournal.createJournalUsingTableDefaultRow(ledgerJournalTableOrig);
break;
case 10:
numLines = tutorialCreateLedgerJournal.createByCombination(numLines);
break;
}
ttscommit;
stopwatch.Stop();
timeMs = stopwatch.get_ElapsedMilliseconds();
info(strFmt("Time for %1 lines(CopyType=%2): %3 sec", numLines, copyType, timeMs / 1000));
}
}
]]></Source>
</Method>
<Method>
<Name>createJournalUsingTableDefaultRow</Name>
<Source><![CDATA[
public Integer createJournalUsingTableDefaultRow(LedgerJournalTable _ledgerJournalTableOrig)
{
//MCRLedgerJournal https://www.linkedin.com/pulse/simplest-way-create-general-journal-d365-using-code-udoye-samuel/
LedgerJournalTable ledgerJournalTable;
LedgerJournalTrans ledgerJournalTrans, ledgerJournalTransOrig;
int numLines;
while select ledgerJournalTransOrig
order by RecId
where ledgerJournalTransOrig.JournalNum == _ledgerJournalTableOrig.JournalNum
{
numLines++;
if (!ledgerJournalTable.RecId)
{
ledgerJournalTable.clear();
ledgerJournalTable.initValue();
ledgerJournalTable.JournalName = _ledgerJournalTableOrig.JournalName;
ledgerJournalTable.initFromLedgerJournalName();
ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
ledgerJournalTable.Name = strFmt("Copy of %1, Date %2", _ledgerJournalTableOrig.JournalNum, DEV::systemdateget());
ledgerJournalTable.insert();
info(strFmt("Journal %1 created", ledgerJournalTable.JournalNum));
}
ledgerJournalTrans.clear();
ledgerJournalTrans.initValue();
ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
ledgerJournalTrans.TransDate = DEV::systemdateget();
ledgerJournalTrans.AccountType = ledgerJournalTransOrig.AccountType;
ledgerJournalTrans.LedgerDimension = ledgerJournalTransOrig.LedgerDimension;
ledgerJournalTrans.DefaultDimension = ledgerJournalTransOrig.DefaultDimension;
ledgerJournalTrans.OffsetAccountType = ledgerJournalTransOrig.OffsetAccountType;
ledgerJournalTrans.OffsetLedgerDimension = ledgerJournalTransOrig.OffsetLedgerDimension;
ledgerJournalTrans.OffsetDefaultDimension = ledgerJournalTransOrig.OffsetDefaultDimension;
ledgerJournalTrans.CurrencyCode = ledgerJournalTransOrig.CurrencyCode;
ledgerJournalTrans.AmountCurCredit = ledgerJournalTransOrig.AmountCurCredit;
ledgerJournalTrans.AmountCurDebit = ledgerJournalTransOrig.AmountCurDebit;
//addition fields
ledgerJournalTrans.Approver = HcmWorker::userId2Worker(curuserid());
ledgerJournalTrans.Approved = NoYes::Yes;
ledgerJournalTrans.Txt = ledgerJournalTransOrig.Txt;
ledgerJournalTrans.SkipBlockedForManualEntryCheck = true;
ledgerJournalTrans.defaultRow();
DEV::validateWriteRecordCheck(ledgerJournalTrans);
ledgerJournalTrans.insert();
}
return numLines;
}
]]></Source>
</Method>
<Method>
<Name>createJournalUsingEntity</Name>
<Source><![CDATA[
public Integer createJournalUsingEntity(LedgerJournalTable _ledgerJournalTableOrig)
{
LedgerJournalTrans ledgerJournalTransOrig;
int numLines;
LedgerJournalEntity ledgerJournalEntity;
Num journalNum;
DimensionCombinationEntity dimensionCombinationEntity, dimensionCombinationEntityOffset;
while select ledgerJournalTransOrig
order by RecId
where ledgerJournalTransOrig.JournalNum == _ledgerJournalTableOrig.JournalNum
{
numLines++;
select dimensionCombinationEntity
where dimensionCombinationEntity.RecId == ledgerJournalTransOrig.LedgerDimension;
select dimensionCombinationEntityOffset
where dimensionCombinationEntityOffset.RecId == ledgerJournalTransOrig.OffsetLedgerDimension;
ledgerJournalEntity.initValue();
ledgerJournalEntity.JournalBatchNumber = journalNum;
ledgerJournalEntity.Description = strFmt("Copy of %1, Date %2", _ledgerJournalTableOrig.JournalNum, DEV::systemdateget());
ledgerJournalEntity.JournalName = _ledgerJournalTableOrig.JournalName;
ledgerJournalEntity.LineNumber++;
//if you have string and want to convert to ID - DimensionDefaultResolver::newResolver(_dimensionDisplayValue).resolve();
//AX > General ledge > Chart of accounts > Dimensions > Financial dimension configuration for integrating applications
ledgerJournalEntity.AccountType = ledgerJournalTransOrig.AccountType;
ledgerJournalEntity.AccountDisplayValue = dimensionCombinationEntity.DisplayValue;
ledgerJournalEntity.OffsetAccountType = ledgerJournalTransOrig.OffsetAccountType;
ledgerJournalEntity.OffsetAccountDisplayValue = dimensionCombinationEntityOffset.DisplayValue;
ledgerJournalEntity.CreditAmount = ledgerJournalTransOrig.AmountCurCredit;
ledgerJournalEntity.DebitAmount = ledgerJournalTransOrig.AmountCurDebit;
ledgerJournalEntity.CurrencyCode = ledgerJournalTransOrig.CurrencyCode;
ledgerJournalEntity.TEXT = ledgerJournalTransOrig.Txt;
ledgerJournalEntity.TRANSDATE = ledgerJournalTransOrig.TransDate;
ledgerJournalEntity.defaultRow();
DEV::validateWriteRecordCheck(ledgerJournalEntity);
ledgerJournalEntity.insert();
journalNum = ledgerJournalEntity.JournalBatchNumber;
}
return numLines;
}
]]></Source>
</Method>
<Method>
<Name>createJournalUsingLedgerJournalEngine</Name>
<Source><![CDATA[
public Integer createJournalUsingLedgerJournalEngine(LedgerJournalTable _ledgerJournalTableOrig)
{
LedgerJournalTable ledgerJournalTable;
LedgerJournalTrans ledgerJournalTrans, ledgerJournalTransOrig;
LedgerJournalEngine ledgerJournalEngine;
LedgerJournalName ledgerJournalName;
int numLines;
ledgerJournalName = _ledgerJournalTableOrig.ledgerJournalName();
while select ledgerJournalTransOrig
order by RecId
where ledgerJournalTransOrig.JournalNum == _ledgerJournalTableOrig.JournalNum
{
numLines++;
if (!ledgerJournalTable.RecId)
{
//Often journal name from parameters is specified here
DEV::validateCursorField(ledgerJournalName, fieldNum(LedgerJournalName, JournalName));
ledgerJournalTable.clear();
ledgerJournalTable.initValue();
ledgerJournalTable.JournalName = ledgerJournalName.JournalName;
ledgerJournalTable.initFromLedgerJournalName();
ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
ledgerJournalTable.Name = strFmt("Copy of %1, Date %2", _ledgerJournalTableOrig.JournalNum, DEV::systemdateget());
ledgerJournalTable.insert();
info(strFmt("Journal %1 created", ledgerJournalTable.JournalNum));
ledgerJournalEngine = LedgerJournalEngine::construct(ledgerJournalTable.JournalType);
ledgerJournalEngine.newJournalActive(ledgerJournalTable);
}
ledgerJournalTrans.clear();
ledgerJournalTrans.initValue();
ledgerJournalEngine.initValue(ledgerJournalTrans);
ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
ledgerJournalTrans.TransDate = DEV::systemdateget();
ledgerJournalTrans.AccountType = ledgerJournalTransOrig.AccountType;
ledgerJournalTrans.modifiedField(fieldNum(LedgerJournalTrans, AccountType));
ledgerJournalTrans.LedgerDimension = ledgerJournalTransOrig.LedgerDimension;
if (!ledgerJournalTrans.LedgerDimension)
{
throw error("Missing or invalid ledger dimension for journal process");
}
ledgerJournalTrans.modifiedField(fieldNum(LedgerJournalTrans, LedgerDimension));
ledgerJournalEngine.accountModified(LedgerJournalTrans);
ledgerJournalTrans.OffsetAccountType = ledgerJournalTransOrig.OffsetAccountType;
ledgerJournalTrans.modifiedField(fieldNum(LedgerJournalTrans, OffsetAccountType));
ledgerJournalTrans.OffsetLedgerDimension = ledgerJournalTransOrig.OffsetLedgerDimension;
ledgerJournalTrans.modifiedField(fieldNum(LedgerJournalTrans, OffsetLedgerDimension));
ledgerJournalEngine.offsetAccountModified(ledgerJournalTrans);
//amounts
LedgerJournalTrans.CurrencyCode = ledgerJournalTransOrig.CurrencyCode;
ledgerJournalEngine.currencyModified(LedgerJournalTrans);
LedgerJournalTrans.AmountCurCredit = ledgerJournalTransOrig.AmountCurCredit;
LedgerJournalTrans.AmountCurDebit = ledgerJournalTransOrig.AmountCurDebit;
//additional fields
LedgerJournalTrans.Approver = HcmWorker::userId2Worker(curuserid());
LedgerJournalTrans.Approved = NoYes::Yes;
ledgerJournalTrans.Txt = ledgerJournalTransOrig.Txt;
LedgerJournalTrans.SkipBlockedForManualEntryCheck = true;
DEV::validateWriteRecordCheck(ledgerJournalTrans);
ledgerJournalTrans.insert();
ledgerJournalEngine.write(ledgerJournalTrans);
}
return numLines;
}
]]></Source>
</Method>
<Method>
<Name>createByCombination</Name>
<Source><![CDATA[
public int createByCombination(int _numCount)
{
DEVTutorialCombinationGenerator combinationAccount = new DEVTutorialCombinationGenerator();
DEVTutorialCombinationGenerator combinationAccountOffset = new DEVTutorialCombinationGenerator();
combinationAccount.setMaxElements(_numCount);
combinationAccountOffset.setMaxElements(_numCount);
combinationAccount.addLevel('Account', ['600150', '600120', '600170','600155', '600180', '600181']);
combinationAccount.addLevel('BusinessUnit', ['001', '002', '003', '004', '005', '006', '069', '078', '081', '082']);
combinationAccount.addLevel('CostCenter', ['007', '008', '009', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', '021', '086']);
combinationAccountOffset.addLevel('Account', ['200190']);
combinationAccountOffset.addLevel('BusinessUnit', ['001', '002', '003', '004', '005', '006', '069', '078', '081', '082']);
combinationAccountOffset.addLevel('CostCenter', ['007', '008', '009', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', '021', '086']);
LedgerJournalTable ledgerJournalTable;
LedgerJournalTrans ledgerJournalTrans;
ttsbegin;
while (combinationAccount.moveToNext())
{
combinationAccountOffset.moveToNext();
if (!ledgerJournalTable.RecId)
{
ledgerJournalTable.clear();
ledgerJournalTable.initValue();
ledgerJournalTable.JournalName = "GenJrn";
ledgerJournalTable.initFromLedgerJournalName();
ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
ledgerJournalTable.Name = strFmt("Combination %1, Date %2", _numCount, DEV::systemdateget());
ledgerJournalTable.insert();
info(strFmt("Journal %1 created", ledgerJournalTable.JournalNum));
}
ledgerJournalTrans.clear();
ledgerJournalTrans.initValue();
ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
ledgerJournalTrans.TransDate = DEV::systemdateget();
ledgerJournalTrans.AccountType = LedgerJournalACType::Ledger;
DimensionDefault dim;
dim = DEVDimensionHelper::setValueToDefaultDimension(dim, 'BusinessUnit', combinationAccount.getLevelValue('BusinessUnit'));
dim = DEVDimensionHelper::setValueToDefaultDimension(dim, 'CostCenter', combinationAccount.getLevelValue('CostCenter'));
ledgerJournalTrans.LedgerDimension = LedgerDimensionFacade::serviceCreateLedgerDimension(
LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId(combinationAccount.getLevelValue('Account')), dim);
ledgerJournalTrans.OffsetAccountType = LedgerJournalACType::Ledger;
dim = DEVDimensionHelper::setValueToDefaultDimension(0, 'BusinessUnit', combinationAccountOffset.getLevelValue('BusinessUnit'));
dim = DEVDimensionHelper::setValueToDefaultDimension(dim, 'CostCenter', combinationAccountOffset.getLevelValue('CostCenter'));
ledgerJournalTrans.OffsetLedgerDimension = LedgerDimensionFacade::serviceCreateLedgerDimension(
LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId(combinationAccountOffset.getLevelValue('Account')), dim);
ledgerJournalTrans.CurrencyCode = "USD";
ledgerJournalTrans.AmountCurCredit = combinationAccount.getCurElement();
ledgerJournalTrans.Approver = HcmWorker::userId2Worker(curuserid());
ledgerJournalTrans.Approved = NoYes::Yes;
ledgerJournalTrans.SkipBlockedForManualEntryCheck = true;
ledgerJournalTrans.defaultRow();
DEV::validateWriteRecordCheck(ledgerJournalTrans);
ledgerJournalTrans.insert();
}
ttscommit;
return combinationAccount.getCurElement();
}
]]></Source>
</Method>
<Method>
<Name>postLedgerJournal</Name>
<Source><![CDATA[
private void postLedgerJournal(LedgerJournalTable _ledgerJournalTable)
{
LedgerJournalCheckPost ledgerJournalCheckPost;
if (_ledgerJournalTable.RecId)
{
ledgerJournalCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(_ledgerJournalTable, NoYes::Yes);
ledgerJournalCheckPost.run();
}
}
]]></Source>
</Method>
</Methods>
</SourceCode>
</AxClass>
You can’t perform that action at this time.