How to using to code import to GL journal[AX2012]

static void THK_importLedgerJournalTrans(Args _args)
{
    Filename                          fileName = "C:\\Users\\ksiu3880\\Desktop\\GL Journal test.xlsx";

    LedgerJournalId                   ledgerJournalId = "000425_010";
    Name                              nameDimensionAttribute1 = "Department";
    Name                              nameDimensionAttribute2 = "CostCenter";
    Name                              nameDimensionAttribute3 = "ExpensePurpose";

    LedgerJournalTrans_Asset          ledgerJournalTrans_Asset;

    AxLedgerJournalTable              axLedgerJournalTable;
    AxLedgerJournalTrans              axLedgerJournalTrans;

    DimensionAttributeValueSetStorage dimensionAttributeValueSetStorage;

    SysExcelApplication               application = SysExcelApplication::construct();
    SysExcelWorkbooks                 workbooks = application.workbooks();
    SysExcelWorkbook                  workbook;
    SysExcelWorksheets                worksheets;
    SysExcelWorksheet                 worksheet;
    SysExcelCells                     cells;

    COMVariantType                    type;

    TransDate                         transDate;
    LedgerJournalACType               ledgerJournalACType, ledgerJournalACTypeOffset;
    MainAccountNum                    mainAccountNum, mainAccountNumOffset;
    DimensionValue                    dimensionValue1, dimensionValue2, dimensionValue3;
    LedgerJournalTransTxt             ledgerJournalTransTxt;
    CurrencyCode                      currencyCode;
    AmountCur                         amountCur;
    ExchRate                          exchRate, exchRateSecond;
    InvoiceId                         invoiceId;
    DocumentDate                      documentDate;
    DueDate                           dueDate;
    PaymTermId                        paymTermId;
    AssetTransTypeJournal             assetTransTypeJournal;
    AssetBookIdPurchSalesInvent       assetBookIdPurchSalesInvent;

    container                         con, conOffset;
    int                               numOfDimension;

    int                               row = 2;

    setPrefix("THK_importLedgerJournalTrans");

    try
    {
        if (!LedgerJournalTable::checkExist(ledgerJournalId))
            throw Exception::Error;

        workbooks.open(fileName);

        workbook   = workbooks.item(1);
        worksheets = workbook.worksheets();
        worksheet  = worksheets.itemFromNum(1);
        cells      = worksheet.cells();

        ttsBegin;

        do
        {
            row++;

            print row;

            setPrefix(strFmt("Row %1", row));

            transDate                   = cells.item(row, 2).value().date();
            mainAccountNum              = strLRTrim(cells.item(row, 4).value().bStr());
            dimensionValue1             = strLRTrim(cells.item(row, 5).value().bStr());
            dimensionValue2             = strLRTrim(cells.item(row, 6).value().bStr());
            dimensionValue3             = strLRTrim(cells.item(row, 7).value().bStr());
            ledgerJournalTransTxt       = strLRTrim(cells.item(row, 8).value().bStr());
            currencyCode                = strLRTrim(cells.item(row, 9).value().bStr());
            amountCur                   = cells.item(row, 10).value().double();
            exchRate                    = cells.item(row, 13).value().double();
            exchRateSecond              = cells.item(row, 14).value().double();
            invoiceId                   = strLRTrim(cells.item(row, 15).value().bStr());
            documentDate                = cells.item(row, 16).value().date();
            dueDate                     = cells.item(row, 17).value().date();
            paymTermId                  = strLRTrim(cells.item(row, 18).value().bStr());
            assetBookIdPurchSalesInvent = strLRTrim(cells.item(row, 20).value().bStr());
            mainAccountNumOffset        = strLRTrim(cells.item(row, 22).value().bStr());

            switch (strLRTrim(cells.item(row, 3).value().bStr()))
            {
                case "Bank":         ledgerJournalACType = ledgerJournalACType::Bank;        break;
                case "Customer":     ledgerJournalACType = ledgerJournalACType::Cust;        break;
                case "Fixed Assets": ledgerJournalACType = ledgerJournalACType::FixedAssets; break;
                case "Ledger":       ledgerJournalACType = ledgerJournalACType::Ledger;      break;
                case "Project":      ledgerJournalACType = ledgerJournalACType::Project;     break;
                case "Vendor":       ledgerJournalACType = ledgerJournalACType::Vend;        break;
                default:             ledgerJournalACType = ledgerJournalACType::Ledger;      break;
            }

            switch (strLRTrim(cells.item(row, 19).value().bStr()))
            {
                case "Acquisition":            assetTransTypeJournal = AssetTransTypeJournal::Acquisition;    break;
                case "Acquisition Adjustment": assetTransTypeJournal = AssetTransTypeJournal::AcquisitionAdj; break;
                default:                       assetTransTypeJournal = AssetTransTypeJournal::Acquisition;    break;
            }

            switch (strLRTrim(cells.item(row, 21).value().bStr()))
            {
                case "Bank":         ledgerJournalACTypeOffset = ledgerJournalACType::Bank;        break;
                case "Customer":     ledgerJournalACTypeOffset = ledgerJournalACType::Cust;        break;
                case "Fixed Assets": ledgerJournalACTypeOffset = ledgerJournalACType::FixedAssets; break;
                case "Ledger":       ledgerJournalACTypeOffset = ledgerJournalACType::Ledger;      break;
                case "Project":      ledgerJournalACTypeOffset = ledgerJournalACType::Project;     break;
                case "Vendor":       ledgerJournalACTypeOffset = ledgerJournalACType::Vend;        break;
                default:             ledgerJournalACTypeOffset = ledgerJournalACType::Ledger;      break;
            }

            axLedgerJournalTrans              = new AxLedgerJournalTrans();
            con                               = [mainAccountNum, mainAccountNum];
            conOffset                         = [mainAccountNumOffset, mainAccountNumOffset];
            dimensionAttributeValueSetStorage = new DimensionAttributeValueSetStorage();
            numOfDimension                    = 0;

            if (dimensionValue1)
            {
                con            += [nameDimensionAttribute1, dimensionValue1];
                conOffset      += [nameDimensionAttribute1, dimensionValue1];
                numOfDimension ++;

                dimensionAttributeValueSetStorage.addItem(DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::findByName(nameDimensionAttribute1), dimensionValue1));
            }

            if (dimensionValue2)
            {
                con            += [nameDimensionAttribute2, dimensionValue2];
                conOffset      += [nameDimensionAttribute2, dimensionValue2];
                numOfDimension ++;

                dimensionAttributeValueSetStorage.addItem(DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::findByName(nameDimensionAttribute2), dimensionValue2));
            }

            if (dimensionValue3)
            {
                con            += [nameDimensionAttribute3, dimensionValue3];
                conOffset      += [nameDimensionAttribute3, dimensionValue3];
                numOfDimension ++;

                dimensionAttributeValueSetStorage.addItem(DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::findByName(nameDimensionAttribute3), dimensionValue3));
            }

            con       = conIns(con, 3, numOfDimension);
            conOffset = conIns(conOffset, 3, numOfDimension);

            axLedgerJournalTrans.parmJournalNum(ledgerJournalId);
            axLedgerJournalTrans.parmTransDate(transDate);
            axLedgerJournalTrans.parmAccountType(ledgerJournalACType);
            axLedgerJournalTrans.parmLedgerDimension(AxdDimensionUtil::getMultiTypeAccountId(enumNum(LedgerJournalACType), ledgerJournalACType, con));
            axLedgerJournalTrans.parmTxt(ledgerJournalTransTxt);
            axLedgerJournalTrans.parmCurrencyCode(currencyCode);
            axLedgerJournalTrans.parmExchRate(exchRate * 100);
            axLedgerJournalTrans.parmPaymId(num2str(exchRateSecond, 0, 12, 1, 0));
            axLedgerJournalTrans.parmInvoice(invoiceId);
            axLedgerJournalTrans.parmDocumentDate(documentDate);
            axLedgerJournalTrans.parmDue(dueDate);
            axLedgerJournalTrans.parmPayment(paymTermId);

            if (ledgerJournalACType != LedgerJournalACType::Ledger)
                axLedgerJournalTrans.parmDefaultDimension(dimensionAttributeValueSetStorage.save());

            if (amountCur >= 0)
                axLedgerJournalTrans.parmAmountCurDebit(amountCur);
            else
                axLedgerJournalTrans.parmAmountCurCredit(-amountCur);

            if (mainAccountNumOffset)
            {
                axLedgerJournalTrans.parmOffsetAccountType(ledgerJournalACTypeOffset);
                axLedgerJournalTrans.parmOffsetLedgerDimension(AxdDimensionUtil::getMultiTypeAccountId(enumNum(LedgerJournalACType), ledgerJournalACTypeOffset, conOffset));

                if (ledgerJournalACTypeOffset != LedgerJournalACType::Ledger)
                    axLedgerJournalTrans.parmOffsetDefaultDimension(dimensionAttributeValueSetStorage.save());
            }

            axLedgerJournalTrans.save();

            if (axLedgerJournalTrans.ledgerJournalTrans().isFixedAssetsTransaction())
            {
                ledgerJournalTrans_Asset.clear();
                ledgerJournalTrans_Asset.initValue();

                ledgerJournalTrans_Asset.RefRecId  = axLedgerJournalTrans.ledgerJournalTrans().RecId;
                ledgerJournalTrans_Asset.AssetId   = axLedgerJournalTrans.ledgerJournalTrans().getAssetId();
                ledgerJournalTrans_Asset.Company   = axLedgerJournalTrans.ledgerJournalTrans().getAssetCompany();
                ledgerJournalTrans_Asset.TransType = assetTransTypeJournal;
                ledgerJournalTrans_Asset.BookId    = assetBookIdPurchSalesInvent;

                if (!ledgerJournalTrans_Asset.validateWrite())
                    throw Exception::Error;

                ledgerJournalTrans_Asset.insert();
            }

            type = cells.item(row + 1, 1).value().variantType();
        }
        while (type != COMVariantType::VT_EMPTY);

        ttsCommit;
    }
    catch (Exception::Error)
    {
    }

    application.quit();
}

 

posted @ 2013-09-27 22:09  Fandy Xie  Views(2073)  Comments(0Edit  收藏  举报