Create Fixed asset Journal using X++
My requirement is to create fixed assests journal for a fixed asset where I need to create journal lines for each asset transaction type from the asset value books, here am considering only current asset value book details to create the journal lines.
public class PRO_FAJournalCreationHelper
{
AssetSumCalc assetSumCalc;
AssetSumCalc assetSumCalcReporting;
LedgerJournalId journalNum;
public static boolean CreateFixedAssetJournal(Assettable _assettable)
{
LedgerJournalTable ledgerJournalTable;
LedgerJournalTrans ledgerJournalTrans;
LedgerJournalName ledgerJournalName;
NumberSeq numberseq;
NumberSequenceTable numberSequenceTable;
Voucher voucherNum;
boolean ret = false;
PRO_FAJournalCreationHelper journalCreationHelper = PRO_FAJournalCreationHelper::Construct();
LedgerJournalTrans_Asset ledgerJournalTrans_Asset;
Assettable assetTable = Assettable::find(_assettable.AssetId);
ledgerJournalTable.initValue();
ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
ledgerJournalTable.Posted = NoYes::No;
ledgerJournalName = LedgerJournalName::find(AssetParameters::find().PRO_MissingJournalName);
ledgerJournalTable.JournalName = ledgerJournalName.JournalName;
ledgerJournalTable.JournalType = LedgerJournalType::Assets;
ledgerJournalTable.initFromLedgerJournalName(ledgerJournalTable.JournalName);
ledgerJournalTable.insert();
if (ledgerJournalTable.RecId)
{
DictEnum dictEnum = new SysDictEnum(enumName2Id("AssetTransTypeJournal"));
int i;
for (i=0;i<=dictEnum.values();
i++ )
{
if (dictEnum.index2Value(i) == AssetTransTypeJournal::Acquisition
|| dictEnum.index2Value(i)
== AssetTransTypeJournal::AcquisitionAdj
||dictEnum.index2Value(i)
== AssetTransTypeJournal::Depreciation
||dictEnum.index2Value(i)
== AssetTransTypeJournal::DepreciationAdj
||dictEnum.index2Value(i)
== AssetTransTypeJournal::Revaluation
||dictEnum.index2Value(i)
== AssetTransTypeJournal::WriteUpAdj
|| dictEnum.index2Value(i)
== AssetTransTypeJournal::WriteDownAdj)
{
ttsbegin;
ledgerjournalTrans.initValue();
ledgerjournalTrans.JournalNum = ledgerJournalTable.JournalNum;
ledgerJournalTrans.CurrencyCode = Ledger::accountingCurrency(CompanyInfo::find().RecId);
numberseq =
NumberSeq::newGetVoucherFromCode(NumberSequenceTable::find(ledgerJournalName.NumberSequenceTable).NumberSequence);
voucherNum =
numberseq.voucher();
ledgerJournalTrans.Voucher = voucherNum;
numberseq.used();
ledgerJournalTrans.AccountType = LedgerJournalACType::FixedAssets;
ledgerJournalTrans.TransactionType = LedgerTransType::FixedAssets;
ledgerJournalTrans.Approved = NoYes::Yes;
ledgerJournalTrans.Approver = HcmWorker::userId2Worker(curuserid());
ledgerJournalTrans.LineNum = LedgerJournalTrans::lastLineNum(ledgerJournalTrans.JournalNum)
+ 1;
ledgerJournalTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
ledgerJournalTrans.LedgerDimension = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(assetTable.AssetId,LedgerJournalACType::FixedAssets);
ledgerJournalTrans.accountName();
journalCreationHelper.updateTotals(assetTable.assetBookCurrent());
AmountCur amountvalue =
journalCreationHelper.getAmmountfromBooks(dictEnum.index2Value(i));
ledgerJournalTrans.AmountCurCredit = amountvalue >0 ? amountvalue : 0;
ledgerJournalTrans.AmountCurDebit = amountvalue <0 ? amountvalue :0 ;
ledgerJournalTrans.OffsetAccountType = LedgerJournalACType::Ledger;
//
ledgerJournalTrans.OffsetLedgerDimension =
ledgerJournalTrans.getOffsetLedgerDimensionForLedgerType(AssetLedgerAccounts::assetOffsetLedgerDimension(assetTable.AssetId,
assetTable.assetBookCurrent().BookId, dictEnum.index2Value(i)),curExt());
ledgerJournalTrans.OffsetLedgerDimension = AssetParameters::find().DimensionAttributeValueCombination;
if
(ledgerJournalTrans.validateWrite())
{
ledgerJournalTrans.insert();
}
if
(ledgerJournalTrans.isFixedAssetsTransaction())
{
ledgerJournalTrans.initValue();
ledgerJournalTrans_Asset.RefRecId = ledgerJournalTrans.RecId;
ledgerJournalTrans_Asset.AssetId = assetTable.assetId;
ledgerJournalTrans_Asset.TransType = dictEnum.index2Value(i) ;
ledgerJournalTrans_Asset.BookId =
assetTable.assetBookCurrent().BookId;
ledgerJournalTrans_asset.insert();
ret = true;
}
ttscommit;
}
}
}
ttsbegin;
LedgerJournalTable.selectForUpdate(true);
LedgerJournalTable.numOfLines =
LedgerJournalTable.numOfLines();
LedgerJournalTable.update();
ttscommit;
if (ret)
{
Info(strFmt("Journal - %1 has been created",ledgerJournalTable.JournalNum ));
}
return ret;
}
public static PRO_FAJournalCreationHelper Construct()
{
PRO_FAJournalCreationHelper FAJournalCreationHelper = new PRO_FAJournalCreationHelper();
return FAJournalCreationHelper;
//
PRO_FAJournalCreationHelper.CreateFixedAssetJournal();
}
private void updateTotals(AssetBook _assetBook)
{
assetSumCalc = AssetSumCalc_Trans::newAssetYear(_assetBook.AssetId,
_assetBook.BookId, DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()), assetYear::All);
if (Ledger::reportingCurrency())
{
assetSumCalcReporting = AssetSumCalc_Trans::newAssetYearByCurrency(_assetBook.AssetId,
_assetBook.BookId, DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()), assetYear::All, AssetSumCalcType::Reporting);
}
}
public AmountCur getAmmountfromBooks(AssetTransTypeJournal _assetTransTypeJournal)
{
switch(_assetTransTypeJournal)
{
case AssetTransTypeJournal::Acquisition:
if (assetSumCalc)
{
return assetSumCalc.acquisitionValue();
}
else
{
return 0;
}
break;
case AssetTransTypeJournal::AcquisitionAdj:
if (assetSumCalc)
{
return assetSumCalc.acquisitionAdjValue();
}
else
{
return 0;
}
break;
case AssetTransTypeJournal::Revaluation:
if (assetSumCalc)
{
return assetSumCalc.revaluationValue();
}
else
{
return 0;
}
break;
case AssetTransTypeJournal::WriteUpAdj:
if (assetSumCalc)
{
return assetSumCalc.writeUpAdjValue();
}
else
{
return 0;
}
break;
case AssetTransTypeJournal::WriteDownAdj:
if (assetSumCalc)
{
return assetSumCalc.writeDownAdjValue();
}
else
{
return 0;
}
break;
case AssetTransTypeJournal::Depreciation:
if (assetSumCalc)
{
return assetSumCalc.depreciation();
}
else
{
return 0;
}
break;
}
return 0;
}
}