Wednesday, 3 February 2016

GeneralLedger Journal Import

class GeneralLedgerJournalImport extends RunBase
{
    FilePath                path;
    Dialog                  dlg;
    ledgerJournalTable      ledgerJournalTable;
    LedgerJournalNameId     journalName;
    DialogField             dialogFieldFile;
    DialogField             dialogJournalName;
    JournalId               journalNum;
    MainAccountNum          account;
}

public boolean canGoBatch()
{
    return true;
}

public LedgerJournalTable createJournalTable(JournalNameId _nameId, Str _name = '')
{
    ;
    journalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();


    ledgerJournalTable.JournalNum = journalNum;
    ledgerJournalTable.JournalName = _nameId;
    ledgerJournalTable.initFromLedgerJournalName();
    if(_name)
    {
        ledgerJournalTable.Name = _name;
    }

    ledgerJournalTable.insert();

    return ledgerJournalTable;
}

public LedgerJournalTrans createJournalTrans(ledgerJournalTable        _ledgerJournalTable,
                                       MainAccountNum           _accountNum,
                                       //SelectableDataArea       _legalEntity,
                                       Name                     _accountingUnit,
                                       Name                     _lineOfBusiness,
                                       AmountCurDebit           _debit,
                                       TransDate                _postingDate,
                                       LedgerJournalACType       _accountType = LedgerJournalACType::Ledger

                                       )
{
    #Define.Branches('Branches')
    LedgerJournalTrans                  ledgerJournalTrans;

    //DimensionAttribute                  dimAttribAccunit = DimensionAttribute::findByName('BusinessUnit');
    //DimensionAttribute                  dimAttribLineof = DimensionAttribute::findByName('Department');

    DimensionAttribute                  dimAttribAccunit = DimensionAttribute::findByName('AccountingUnit');
    DimensionAttribute                  dimAttribLineof = DimensionAttribute::findByName('LineofBusiness');
    NumberSeq                           numberSeq;
    NumberSequenceTable                 numberSequenceTable;
    LedgerJournalName                   ledgerJournalName;

    DimensionAttributeValue             dimAttribValue;
    DimensionAttributeValueSetStorage   store = new DimensionAttributeValueSetStorage();

    //_lineOfBusiness = _accountingUnit;

    ledgerJournalTrans.clear();
    ledgerJournalTrans.initValue();
    ledgerJournalTrans.LineNum              = LedgerJournalTrans::lastLineNum(_ledgerJournalTable.JournalNum) + 1;
    ledgerJournalTrans.JournalNum           = _ledgerJournalTable.JournalNum;
    ledgerJournalTrans.AccountType          = _accountType;
    ledgerJournalTrans.TransDate            = _postingDate;

    ledgerJournalName   = LedgerJournalName::find(_LedgerJournalTable.JournalName);
    numberSequenceTable = NumberSequenceTable::find(ledgerJournalName.NumberSequenceTable);

    numberSeq = NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence);
    ledgerJournalTrans.Voucher = numberSeq.voucher();


    ledgerJournalTrans.TransactionType = LedgerTransType::GeneralJournal;

    if (_accountingUnit)
    {
        dimAttribValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribAccunit,_accountingUnit,false,false);
        if(!dimAttribValue)
        {
            error(strFmt("@CIT127", _accountingUnit));
            return ledgerJournalTrans;
        }
        store.addItem(dimAttribValue);
        //ledgerJournalTrans.DefaultDimension =  store.save();
    }

    if (_lineOfBusiness)
    {
        dimAttribValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribLineof,_lineOfBusiness,false,false);
        if(!dimAttribValue)
        {
            error(strFmt("@CIT127", _lineOfBusiness));
            return ledgerJournalTrans;
        }
        store.addItem(dimAttribValue);
        ledgerJournalTrans.DefaultDimension =  store.save();
    }

    if (ledgerJournalTrans.AccountType == LedgerJournalACType::Ledger)
    {
        ledgerJournalTrans.LedgerDimension      = DimensionStorage::getDefaultAccountForMainAccountNum(_accountNum);
        if(!ledgerJournalTrans.LedgerDimension)
        {
            error(strFmt("@CIT127", _accountNum));
            return ledgerJournalTrans;
        }
        ledgerJournalTrans.LedgerDimension = DimensionDefaultingService::serviceCreateLedgerDimension(ledgerJournalTrans.LedgerDimension, store.save());
    }

    if (_debit > 0)
    {
        ledgerJournalTrans.AmountCurDebit       = Currency::amount(_debit);
    }
    else
    {
        ledgerJournalTrans.AmountCurCredit       = -Currency::amount(_debit);
    }

    ledgerJournalTrans.insert();
    return ledgerJournalTrans;
}
protected Object dialog()
{
    dlg =super();
    dialogFieldFile = dlg.addFieldValue(this.pathType(),path);
    dialogJournalName = dlg.addFieldValue(extendedTypeStr(LedgerJournalNameId), journalName, "@SYS35283");
    return dlg;
}
public boolean getFromDialog()
{
    path = dialogFieldFile.value();
    journalName = dialogJournalName.value();

    return super();
}
Public identifierName pathType()
{
    return extendedtypestr(FileNameOpen);
}
public void readFile (str                             _path,
                      LedgerJournalNameId             _journalName)
                    //  NoYes                           _isFile)
{

    #Define.JournalDesc('ADP GL Import')
    #Define.MainAccountColumn(2)
    #Define.LegalEntityColumn(1)
    #Define.AmountColumn(5)
    #Define.PostingDateColumn(10)

    CommaIo                         io;
    container                       record;

    int                             row, legalEntityLen;
    FileName                        filename;
    str                             mainAccountNumStr, legalEntityStr, accountingUnitStr, amount;
    MainAccountNum                  accountNum;

    TransDate                       postingDate;
    AmountCurDebit                  debit;

    //SysOperationProgress            prog;
    //int                             startTime = timeNow();
    Map                             balanceMap = new Map(Types::String, Types::Real);
    MapEnumerator                   mapEnum;
    LedgerParameters                ledgerParameters;
    MainAccount                     companyRoundingAccout;
    ;

    filename  = _path;
    setPrefix(strFmt("@SYS76498", curext(), filename));
    //prog = SysOperationProgress::newGeneral('',filename,100000);
    try
    {
        io = new CommaIo(filename, 'r');
        row = 1;

        ttsBegin;
        ledgerJournalTable  = this.createJournalTable(_journalName, #JournalDesc);
        ledgerParameters    = ledgerParameters::find();
        record = io.read();
        do
        {
            row++;
            record = io.read();
            if(conLen(record)>2)
            {
                legalEntityStr          = conPeek(record, #LegalEntityColumn);
                mainAccountNumStr       = conPeek(record, #MainAccountColumn);
                legalEntityLen          = strLen(legalEntityStr);

                accountingUnitStr       = subStr(legalEntityStr, legalEntityLen-3,4);
                //accountingUnitStr       = subStr(legalEntityStr, legalEntityLen-2,3);
                legalEntityStr          = subStr(legalEntityStr,1,2);

                accountNum      = subStr(mainAccountNumStr,1,4);
                //accountNum      = subStr(mainAccountNumStr,1,6);
                amount          = conPeek(record, #AmountColumn);
                amount          = StrRem(amount,'$');
                amount          = StrRem(amount,'(');
                amount          = StrRem(amount,')');

                debit           = str2num(amount);
                postingDate     = conPeek(record, #PostingDateColumn);

                //this.createJournalTrans(ledgerJournalTable, accountNum, legalEntityStr, accountingUnitStr, accountingUnitStr, debit,postingDate, LedgerJournalACType::Ledger);
                this.createJournalTrans(ledgerJournalTable, accountNum, accountingUnitStr, accountingUnitStr, debit,postingDate, LedgerJournalACType::Ledger);

                if (balanceMap.exists(accountingUnitStr))
                {
                    balanceMap.insert( accountingUnitStr, balanceMap.lookup(accountingUnitStr) + debit);
                }
                else
                {
                    balanceMap.insert( accountingUnitStr, debit);
                }
            }
        }while(conLen(record)>0);

        mapEnum = balanceMap.getEnumerator();
        while(mapEnum.moveNext())
        {
            if (mapEnum.currentValue() !=0 && (mapEnum.currentValue() >= -0.15 && mapEnum.currentValue() <= 0.15))
            {
                companyRoundingAccout = MainAccount::find(ledgerParameters.CompanyRoundingWrite);
                //this.createJournalTrans(ledgerJournalTable, companyRoundingAccout.MainAccountId, legalEntityStr, mapEnum.currentKey(), mapEnum.currentKey(), mapEnum.currentValue(),today(), LedgerJournalACType::Ledger);
                this.createJournalTrans(ledgerJournalTable, companyRoundingAccout.MainAccountId, mapEnum.currentKey(), mapEnum.currentKey(), mapEnum.currentValue(),today(), LedgerJournalACType::Ledger);
            }
            else if (mapEnum.currentValue() !=0)
            {
                warning(strFmt("@CIT127",accountingUnitStr ));
            }
        }

        ttsCommit;

        io = null;
    }
    catch (Exception::Error)
    {
        io = null;
        throw error("@SYS19358");
    }
}
public void run()
{
    if(dlg.run())
    {
        //this.readFile(path,journalName,NoYes::Yes);
        this.readFile(path,journalName);
    }
}
public static ClassDescription description()
{
    return "@CIT189";
}
public static void main(Args args)
{
    GeneralLedgerJournalImport    generalLedgerJournalImport = new generalLedgerJournalImport();

    generalLedgerJournalImport.getLast();
    if (generalLedgerJournalImport.prompt())
    {
       generalLedgerJournalImport.run();
    }
}

No comments:

Post a Comment