Sunday, November 16, 2025

Fixed asset split

 

The following code is developed to automatically create fixed asset records using the purchase order number and its corresponding quantity split into fixed asset.


class FixedAssetCreationService extends SysOperationServiceBase 

      #define.CurrentVersion(1) 

    #localMacro.CurrentList 

        assetBook, 

        assetId, 

        bookId, 

        transDate, 

        percent, 

        journalName 

    #endMacro 

    AssetBook splitAssetBook; 

    AssetBookId                 bookId; 

    AssetId                     assetId; 

    AssetTable                  assetTable,assetTableLoc; 

    AssetBook                   assetBook; 

    AssetDepbook                assetDepBook; 

    AssetGroup                  assetGroup; 

    AssetDepreciationProfile    assetDepProfile; 

    NumberSeq                   assetIdNumberSeq; 

    NumberSequenceTable         numberSequenceTable; 

    NumberSequenceReference     numberSequenceReference; 

    PurchTable                  purchtable; 

    PurchLine                   purchline; 

    int                         originalPurchQty,remainingQty,remainingQtyFAJournal; 

    AssetSumCalc                assetSumCalc; 

    AssetSumCalc                assetSumCalcReporting; 

    LedgerJournalTable          ledgerJournalTable; 

    LedgerJournalTrans          ledgerJournalTrans; 

    LedgerJournalTrans_Asset    ledgerJournalTrans_Asset; 

    NumberSeq                   numberseq; 

    NumberSequenceTable         numSeqTable; 

    Voucher                     voucherNum; 

    LedgerJournalName           ledgerJournalName; 

    RefRecId                    recId; 

    AssetLedgerAccounts         assetLedgerAccounts; 

    str                         mainaccount; 

    LedgerJournalNameId         journalName; 

    TransDate                   fromdate,toDate; 

    int                         i=1; 

    AssetTransType              assetTransType; 

    AssetTransTypeJournal       assetTransTypeJournal; 

    VendTable                   vendTable; 

    VendInvoiceJour             vendInvoiceJour; 

    VendInvoiceTrans            vendInvoiceTrans; 

    int                         lineNum = 1; 

  

    public void ProcessReport( _FixedAssetCreationContract _Contract) 

    { 

        journalName = _Contract.parmjournalName(); 

        fromdate    = _Contract.parmFromDate(); 

        toDate      = _Contract.parmToDate(); 

        this.createFixedAsset(); 

  

    } 

  

    protected AssetId createFixedAsset() 

    {       

        while select vendInvoiceTrans 

            join vendInvoiceJour 

                where vendInvoiceJour.PurchId == vendInvoiceTrans.PurchId 

            join purchtable 

            where purchtable.PurchId == vendInvoiceTrans.PurchId 

            join PurchLine 

            where purchtable.PurchId == purchline.PurchId 

            //&& purchline.PurchId == vendInvoiceTrans.PurchId 

            join assetTable 

            where purchline.RecId == assetTable.PurchLineRecId 

                && vendInvoiceJour.InvoiceId == vendInvoiceTrans.InvoiceId 

                && vendInvoiceJour.InvoiceDate >= fromdate 

                && vendInvoiceJour.InvoiceDate <= toDate 

                && purchline.InventTransId == vendInvoiceTrans.InventTransId 

                && vendInvoiceTrans. _IsSplitFixedAsset == NoYes::No 

                //&& PurchLine.LineNumber == lineNum 

        {           

            { 

                assetGroup = AssetGroup::find(assetTable.AssetGroup); // Asset group to which Asset will belong 

  

                select Continuous from numberSequenceTable 

                join AutoNumberSequenceTable from assetGroup where 

                assetGroup.GroupId == assetTable.AssetGroup && 

                numberSequenceTable.RecId == assetGroup.AutoNumberSequenceTable; 

  

                if (numberSequenceTable.Continuous == NoYes::No) 

                { 

                    if (assetGroup.AutoNumberSequenceTable) 

                    { 

                        throw error("@SYS116327"); 

                    } 

                    else 

                    { 

                        if (AssetParameters::find().AutoNumber) 

                        { 

                            numberSequenceReference = AssetParameters::numRefAssetId(); 

                            select Continuous from numberSequenceTable  

                                where numberSequenceTable.RecId == numberSequenceReference.NumberSequenceId; 

                            if (numberSequenceTable.Continuous == NoYes::No) 

                            { 

                                throw error("@SYS116327"); 

                            } 

                        } 

                        else 

                        { 

                            throw error("@SYS122736"); 

                        } 

                    } 

                }               

  

                //assetTable.selectForUpdate(true);   

          

                remainingQty = purchline. _NumberofFASplit == 0 ? vendInvoiceTrans.Qty - 1 : vendInvoiceTrans.Qty; 

  

                if(remainingQty <= 0) 

                { 

                    continue; 

                } 

  

                ledgerJournalTable.clear(); 

                ledgerJournalTable.initValue(); 

                ledgerJournalTable.JournalNum   = JournalTableData::newTable(ledgerJournalTable).nextJournalId(); 

                ledgerJournalTable.Posted       = NoYes::No; 

                ledgerJournalTable.JournalName  = journalName; 

               // ledgerJournalName = LedgerJournalName::find(ledgerJournalTable.JournalName); 

  

                ledgerJournalTable.Name = "@SYS67430"; 

                ledgerJournalTable.initFromLedgerJournalName(ledgerJournalTable.JournalName); 

                ledgerJournalTable.JournalType  = LedgerJournalType::Assets; 

                ledgerJournalTable.insert(); 

                 

                AssetTable          assetTableCreate; 

                 

                while (remainingQty  > 0) 

                { 

                    ledgerJournalTrans.clear(); 

  

                    ttsbegin; 

                    select firstOnly numSeqTable 

                        where numSeqTable.RecId == LedgerJournalName::find(ledgerJournalTable.JournalName).NumberSequenceTable; 

                    if (numSeqTable )//&& !voucherNum) 

                    { 

                        numberseq = numberseq::newGetVoucherFromCode(numSeqTable.NumberSequence); 

                        voucherNum = numberseq.voucher(); 

                    } 

                    ttscommit; 

         

                    ledgerJournalTrans.Voucher                              = voucherNum; 

                    //ledgerJournalTrans.DefaultDimension                     = purchline.DefaultDimension; 

                    //numberseq.used(); 

                    for(i = 1;i<=2;i++) 

                    { 

                        assetTableCreate.clear(); 

                        assetTableCreate.Name                 = assetTable.Name; 

                        assetTableCreate.AssetGroup           = assetGroup.GroupId; 

                        assetTableCreate.AssetType            = assetTable.AssetType;//AssetType::Tangible; 

                        assetTableCreate.Quantity             = 1; 

                        assetTableCreate.PropertyType         = AssetPropertyType::FixedAsset; 

                        assetTableCreate.UnitCost             = purchline.PurchPrice; 

                        assetTableCreate.UnitOfMeasure        = purchline.PurchUnit;  

                        // start ---27-09-2023,Ratna.P 

                        AssetBook   AssetBookloc; 

                        select * from AssetBookloc 

                            where AssetBookloc.PurchId == purchtable.PurchId; 

  

                        if (!AssetBookloc) 

                        { 

                            AssetBookloc.PurchId               = purchtable.PurchId; 

                            AssetBookloc.VendAccount           = purchtable.OrderAccount; 

                            AssetBookloc.DefaultDimension      = purchline.DefaultDimension; 

                            AssetBookloc.insert(); 

                        } 

                         

                        // End--- 27-09-2023 ,Ratna.P 

                        assetBook = AssetBookloc; 

  

                        ledgerJournalTrans.parmAccount(assetTable.AssetId, LedgerJournalACType::FixedAssets); 

                         

  

                        //assetBook.insert();                        

                        //ttsbegin; 

                        //splitAssetBook = AssetBook::find(assetBook.AssetId, assetBook.BookId, true); 

                        //splitAssetBook.Depreciation                  = assetBook.Depreciation; 

                        //splitAssetBook.DepreciationStartDate         = assetBook.DepreciationStartDate; 

                        //splitAssetBook.ServiceLife                   = assetBook.ServiceLife; 

                        //splitAssetBook.LifeTime                      = assetBook.LifeTime; 

                        //splitAssetBook.LifeTimeRest                  = assetBook.LifeTimeRest; 

                        //splitAssetBook.UsedFromDate                  = assetBook.LastDepreciationDate ? assetBook.UsedFromDate : assetBook.UsedFromDate; 

                        //splitAssetBook.DepreciationConvention        = assetBook.DepreciationConvention; 

  

                        //splitAssetBook.AcquisitionDate               = assetBook.AcquisitionDate; 

                        //splitAssetBook.LastDepreciationDateExtraOrd  = assetBook.LastDepreciationDateExtraOrd; 

                        //splitAssetBook.ExceedingNetBookValue         = assetBook.ExceedingNetBookValue; 

                        //splitAssetBook.NegativeNetBookValue          = assetBook.NegativeNetBookValue; 

                        //splitAssetBook.RevaluationGroupId            = assetBook.RevaluationGroupId; 

                        //splitAssetBook.ReserveTypeId                 = assetBook.ReserveTypeId; 

  

                        //splitAssetBook.ConsumptionFactorId           = assetBook.ConsumptionFactorId; 

                        //splitAssetBook.ConsumptionUnitId             = assetBook.ConsumptionUnitId; 

                        //splitAssetBook.DepreciationAmountUnit        = assetBook.DepreciationAmountUnit; 

                        //splitAssetBook.ConsumptionQtyEstimated       = assetBook.ConsumptionQtyEstimated; 

  

                        //splitAssetBook.VendAccount                   = assetBook.VendAccount; 

                        //splitAssetBook.PurchId                       = assetBook.PurchId; 

                        //splitAssetBook.VendInvoiceId                 = assetBook.VendInvoiceId; 

  

                        //// <GEECZ> 

                        //splitAssetBook.DepreciationGroupId_W = assetBook.DepreciationGroupId_W; 

                        //// </GEECZ> 

  

                        //splitAssetBook.update(); 

                        //ttscommit; 

                        //info(strFmt("%1", purchline.PurchId)); 

                        assetIdNumberSeq = assetTableCreate.initAssetNumberSeq(); 

                        if(i == 2) 

                        { 

                         //   ttsbegin; 

                            if (assetIdNumberSeq) 

                            { 

                                assetTableCreate.AssetId = assetIdNumberSeq.num(); 

                                assetIdNumberSeq.used(); 

                            } 

                           // ttscommit; 

                            assetTableCreate.insert(); // Create Fixed Asset 

                            if (assetTableCreate.AssetId) 

                            { 

                                assetBook PrevAssetbook = AssetBook::find(assetBook.AssetId, assetBook.BookId); 

                                this.createAssetBook(PrevAssetbook, assetTableCreate); 

                            } 

                        } 

                          

                        if(i == 1) 

                        { 

                            assetTransType = AssetTransType::Acquisition; 

                            ledgerJournalTrans.LedgerDimension                      = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(purchline.AssetId,LedgerJournalACType::FixedAssets); 

                        } 

                        else 

                        { 

                            assetTransType = AssetTransType::Depreciation; 

                            ledgerJournalTrans.LedgerDimension                      = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(assetTableCreate.AssetId,LedgerJournalACType::FixedAssets); 

                        } 

            

                        ledgerjournalTrans.initValue(); 

                        

                        ledgerJournalTrans.JournalNum                           = ledgerJournalTable.Journalnum; 

                        ledgerJournalTrans.CurrencyCode                         = Ledger::accountingCurrency(CompanyInfo::find().RecId); 

                        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(assetTableCreate.AssetId,LedgerJournalACType::FixedAssets); 

                        //select ledgerJournalName 

                        //where ledgerJournalTable.JournalName == ledgerJournalName.JournalName; 

                        ledgerJournalTrans.DefaultDimension                     = purchline.DefaultDimension; 

                        ledgerJournalTrans.OffsetAccountType                    = LedgerJournalACType::Ledger; 

  

                        select firstonly LedgerDimension from assetLedgerAccounts 

                            where assetLedgerAccounts.PostingProfile == 'FAPr'//ledgerJournalTrans.PostingProfile 

                                && assetLedgerAccounts.BookId == purchline.AssetBookId 

                                && assetLedgerAccounts.AccountCode    == TableGroupAll::GroupId 

                                && assetLedgerAccounts.AccountRelation  == assetTable.AssetGroup//assetTable.AssetId//"CDM0002" 

                                && assetLedgerAccounts.TransType == AssetTransType::AcquisitionAdj;//str2enum(AssetTransType,enum2str(ledgerJournalTrans_Asset.TransType)); 

  

                        //ledgerJournalTrans.OffsetLedgerDimension                    = LedgerDimensionFacade::ServiceCreateLedgerDimension(assetLedgerAccounts.LedgerDimension,purchline.DefaultDimension); 

                         

                        if(i == 1) 

                        { 

                            ledgerJournalTrans.AmountCurCredit = purchline.PurchPrice; 

                            ledgerJournalTrans.Txt = 'Acquisition adjustment'; 

                        } 

                        else if( i == 2) 

                        { 

                            ledgerJournalTrans.AmountCurCredit= 0; 

                            ledgerJournalTrans.AmountCurDebit = purchline.PurchPrice; 

                            ledgerJournalTrans.Txt = 'Acquisition'; 

                        }                         

                        if (ledgerJournalTrans.validateWrite()) 

                        { 

                            ledgerJournalTrans.insert(); 

                        } 

  

                        ledgerJournalTrans_Asset.initValue(); 

                        ledgerJournalTrans_Asset.RefRecId                       = ledgerJournalTrans.RecId; 

                        ledgerJournalTrans_Asset.AssetId                        = assetTable.assetId; 

                        //ledgerJournalTrans_Asset.TransType                      = AssetTransTypeJournal::AcquisitionAdj; 

                        ledgerJournalTrans_Asset.BookId                         = AssetTable.assetBookCurrent().BookId; 

                        if(i == 1) 

                        { 

                            assetTransTypeJournal = AssetTransTypeJournal::Acquisition; 

                            ledgerJournalTrans_Asset.TransType  = AssetTransTypeJournal::AcquisitionAdj; 

                        } 

                        else 

                        { 

                            assetTransTypeJournal = AssetTransTypeJournal::Depreciation; 

                            ledgerJournalTrans_Asset.TransType  = AssetTransTypeJournal::Acquisition; 

                        } 

  

                        ledgerJournalTrans_asset.insert();  

  

                        ttsbegin; 

                        LedgerJournalTable.selectForUpdate(true); 

                        LedgerJournalTable.numOfLines = LedgerJournalTable.numOfLines(); 

                        LedgerJournalTable.update(); 

                        ttscommit; 

                         

                        info(strFmt("%1", assetTablecreate.AssetId)); 

                    } 

                    remainingQty --; 

                } 

              

                purchline.reread(); 

                ttsbegin; 

  

                vendInvoiceTrans.selectForUpdate(true); 

                purchline.selectForUpdate(true); 

                if( purchline. _NumberofFASplit == 0) 

                purchline. _NumberofFASplit += vendInvoiceTrans.Qty-1; 

                else 

                    purchline. _NumberofFASplit += vendInvoiceTrans.Qty; 

                vendInvoiceTrans. _IsSplitFixedAsset = NoYes::Yes; 

                vendInvoiceTrans.update(); 

                purchline.update(); 

                lineNum ++; 

                ttscommit; 

            } 

        } 

        return assetTable.AssetId; 

    } 

  

    AssetId parmAssetId(AssetId _assetId = assetId) 

    { 

        assetId = _assetId; 

  

        return assetId; 

    } 

  

    AssetBookId parmBookId(AssetBookId _bookId = bookId) 

    { 

        bookId = _bookId; 

  

        return bookId; 

    } 

  

    AssetBook assetBook() 

    { 

        return assetBook; 

    } 

  

    public void createAssetBook(AssetBook _PrevAssetbook, AssetTable _assetTable) 

    { 

        ttsbegin; 

        assetBook assetbookloc = _PrevAssetbook; 

        splitAssetBook = AssetBook::find(_assetTable.AssetId, _PrevAssetbook.BookId, true); 

        //splitAssetBook.selectForUpdate(true); 

        //splitAssetBook.AssetId                      =  _assetTable.AssetId; 

        //splitAssetBook.BookId                       = assetbookloc.BookId; 

        splitAssetBook.Depreciation                  = assetbookloc.Depreciation; 

        splitAssetBook.DepreciationStartDate         = assetbookloc.DepreciationStartDate; 

        splitAssetBook.ServiceLife                   = assetbookloc.ServiceLife; 

        splitAssetBook.LifeTime                      = assetbookloc.LifeTime; 

        splitAssetBook.LifeTimeRest                  = assetbookloc.LifeTimeRest; 

        splitAssetBook.UsedFromDate                  = assetbookloc.LastDepreciationDate ? assetBook.UsedFromDate : assetBook.UsedFromDate; 

        splitAssetBook.DepreciationConvention        = assetbookloc.DepreciationConvention; 

  

        splitAssetBook.AcquisitionDate               = assetbookloc.AcquisitionDate; 

        splitAssetBook.LastDepreciationDateExtraOrd  = assetbookloc.LastDepreciationDateExtraOrd; 

        splitAssetBook.ExceedingNetBookValue         = assetbookloc.ExceedingNetBookValue; 

        splitAssetBook.NegativeNetBookValue          = assetbookloc.NegativeNetBookValue; 

        splitAssetBook.RevaluationGroupId            = assetbookloc.RevaluationGroupId; 

        splitAssetBook.ReserveTypeId                 = assetbookloc.ReserveTypeId; 

  

        splitAssetBook.ConsumptionFactorId           = assetbookloc.ConsumptionFactorId; 

        splitAssetBook.ConsumptionUnitId             = assetbookloc.ConsumptionUnitId; 

        splitAssetBook.DepreciationAmountUnit        = assetbookloc.DepreciationAmountUnit; 

        splitAssetBook.ConsumptionQtyEstimated       = assetbookloc.ConsumptionQtyEstimated; 

  

        splitAssetBook.VendAccount                   = assetbookloc.VendAccount; 

        splitAssetBook.PurchId                       = assetbookloc.PurchId; 

        splitAssetBook.VendInvoiceId                 = assetbookloc.VendInvoiceId; 

        splitAssetBook.DepreciationProfile            =  assetbookloc.DepreciationProfile; 

        // <GEECZ> 

        splitAssetBook.DepreciationGroupId_W = assetbookloc.DepreciationGroupId_W; 

        // </GEECZ> 

  

        splitAssetBook.update(); 

        ttscommit; 

    } 

  

 


No comments:

Post a Comment