Tuesday, March 8, 2016

Job to import the excel data to ax


public static void Excelupload()
{

    SysExcelApplication             application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                   cells;
    COMVariantType                  type;
    FilenameOpen                    filename;
    dialogField                     dialogFilename;
    Dialog                          dialog;
    int                             row;

    PayrollEarningStatement         payrollEarningStatement,payrollEarningStatement1;
    PayrollEarningStatementLine     payrollEarningStatementLine,payrollEarningStatementLine1;

    HcmPersonnelNumberId            PersonnelNumber;
    PayrollPayCycleId               Paycycle;
    PeriodEnd                       PeriodEnd;
    TransDate                       EarningDate;
    PayrollEarningCodeId            Earningcode;
    PayrollEarningQuantity          Quantity;
    PayrollEarningRate              EarningRate;
    HcmWorkerRecId                  Worker;
    PayrollEarningCodeRecId         EarningcodeRecid;
    PayrollPayPeriodRecId           payperiod;
    PayrollPayCycleRecId            PaycycleRecid;
    NumberSeq                       num;

    #PayrollConstants
    #Excel

    ;

    dialog                  =   new Dialog("@SYS55058");
    dialogFilename          =   dialog.addField(extendedTypeStr(FilenameOpen),"@SYS80053");
    dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS28576",#XLS]);
    dialog.filenameLookupTitle("@BUR7359");
    dialog.caption('Position- Upload');
    dialogFilename.value(filename);
    if(!dialog.run())
    return;

    filename            =   dialogFilename.value();
    application         =   SysExcelApplication::construct();
    workbooks           =   application.workbooks();

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("@SYS19358");
    }

    workbook            =   workbooks.item(1);
    worksheets          =   workbook.worksheets();
    worksheet           =   worksheets.itemFromNum(1);
    cells               =   worksheet.cells();
    row = 1;
    try
    {
        ttsBegin;

        do
        {
            row++;
            PersonnelNumber   = cells.item(row, 1).value().bStr();
            Paycycle          = cells.item(row, 2).value().bStr();
            PeriodEnd         = cells.item(row, 3).value().date();
            EarningDate       = cells.item(row, 4).value().date();
            Earningcode       = cells.item(row, 5).value().bStr();
            Quantity          = cells.item(row, 6).value().double();
            EarningRate       = cells.item(row, 7).value().double();


            Worker                  = HcmWorker::findByPersonnelNumber(PersonnelNumber).RecId;
            EarningcodeRecid        = PayrollEarningCode::findByEarningCode(Earningcode).RecId;
            PaycycleRecid           = PayrollPayCycle::findByPayCycle(Paycycle).RecId;
            payperiod               = CEMPayrollearningExcelupload::findByPayCycleAndEndDate(PaycycleRecid,PeriodEnd).RecId;
            if(!Worker)
            {
                info(strFmt("Personnel number %1 does not exist in %2 row",PersonnelNumber,row));
            }
            if(!EarningcodeRecid)
            {
                info(strFmt("Earningcode %1 does not exist in %2 row",Earningcode,row));
            }
            if(!PaycycleRecid)
            {
                info(strFmt("Paycycle %1 does not exist in %2 row",Paycycle,row));
            }
            if(!payperiod)
            {
                info(strFmt("PeriodEnd %1 does not exist in %2 row",PeriodEnd,row));
            }
            if(!EarningDate && PeriodEnd )
            {
                info(strFmt("Check the input format for Earningdate %1 and Period Enddate%2 in %3 row",EarningDate,PeriodEnd,row));
            }


            select * from payrollEarningStatement where payrollEarningStatement.Worker == Worker &&
                payrollEarningStatement.PayPeriod == payperiod;

            if(!payrollEarningStatement)
            {
                payrollEarningStatement.initValue();
                num = NumberSeq::newgetnum(PRLPayrollParameters::numRefEarningStatDocumentNumber(),true);
                payrollEarningStatement.DocumentNumber = num.num();
                payrollEarningStatement.Worker = Worker;
                payrollEarningStatement.PayPeriod = payperiod;
                payrollEarningStatement.insert();
                num.used();
            }

            payrollEarningStatement.clear();
            select RecId from payrollEarningStatement where payrollEarningStatement.Worker == Worker &&
                payrollEarningStatement.PayPeriod == payperiod;
            if(payrollEarningStatement.RecId)
            {
                payrollEarningStatementLine.EarningStatement            = payrollEarningStatement.RecId;
                payrollEarningStatementLine.EarningDate                 = EarningDate;
                payrollEarningStatementLine.EarningRate                 = EarningRate;
                payrollEarningStatementLine.Quantity                    = Quantity;
                payrollEarningStatementLine.AccountingCurrencyAmount    = decRound(Quantity * EarningRate, #DECIMALPRECISION);
                payrollEarningStatementLine.EarningCode                 = EarningcodeRecid;
                payrollEarningStatementLine.Position                    = HcmWorker::getPrimaryPosition(Worker,DateTimeUtil::newDateTime(EarningDate,0));
                if(!payrollEarningStatementLine.Position)
                {
                   info(strFmt("Primary position doest not exist for the worker %1",PersonnelNumber));
                }
                payrollEarningStatementLine.PaymentStatus               = PayrollPaymentProcessingStatus::None;
                payrollEarningStatementLine.GenerationSource            = PayrollGenerationSource::UserEntry;
                payrollEarningStatementLine.WorkerTaxRegion             = PayrollPositionWorkerDefaultTaxRgn::findByWorkerPosition(
                                        HcmWorker::getPrimaryPosition(Worker,DateTimeUtil::newDateTime(EarningDate,0)),Worker).WorkerTaxRegion;
                payrollEarningStatementLine.DefaultDimension            = HcmEmployment::findByWorkerLegalEntity(Worker,CompanyInfo::findByCompany_IN(curext()).RecId).DefaultDimension;
                payrollEarningStatementLine.setDefaultDimension();
                payrollEarningStatementLine.setAccountingDistributionTemplate();
                payrollEarningStatementLine.insert();
            }

            type = cells.item(row + 1, 1).value().variantType();

        } while (type != COMVariantType::VT_EMPTY);
        application.quit();

        info("Successfull inserted");

       ttsCommit;
    }
    catch
    {

        Error("Not inserted");
    }


}

No comments:

Post a Comment