Thursday 28 November 2019

Create Invoice proposal through code AX 2012

Create Invoice proposal through code AX 2012

class : ProjInvoiceProposalCreateLines

public void new()
{
}

.........................

ProjInvoiceProposalInsertLines
String30                        psaInvoiceId,invId;
/// <summary>
/// Contains the code that performs the actual job of the class.
/// </summary>
public void run()
{
    ProjFundingSourceRefId  fundingSourceRefIdPrev;
    CurrencyCode            currencyCodePrev;
    ProjInvoiceProjId       projInvoiceProjIdPrev;
    RefRecId                taxInformationRecid;
    boolean                 isInvoiceSplit_IN       = isCountryRegion_IN && ProjParameters::find().InvoiceSplit_IN;

    boolean                 ret;
    ProjProposalJour        proposalJour;
    ProjTable               projTable;
    Set                     fundingSourceRefIdPrevSet = new Set(Types::Int64);

    ALE_ProposalInvoiceTable proposalInvoiceTable;
    #OCCRetryCount

    startLengthyOperation();

    try
    {

        ttsbegin;

        this.progressInit("@SYS54552", progressTotal, #AviFormLetter);

        progress.setText("@SYS26577");

        projInvoiceProposalCreateLines.updateContractLineNum();

        tmpProjProposalTrans = projInvoiceProposalCreateLines.prepareForInvoicing();

        if (isValidateProjTable)
        {
            this.removeBlockedProjIds();
        }

        while select tmpProjProposalTrans order by ProjInvoiceProjId, FundingSourceRefId, CurrencyCode, TaxInformation_IN
        {
            if (!ProjFundingSource::isExternalFunding(tmpProjProposalTrans.FundingSourceRefId))
            {
                continue;
            }

            // <GBR>
            if (!BrazilParameters::isEnabled() || (projInvoiceProposalCreateLines.parmProposalCreateLinesParams().parmReverseMarking_BR()))
            {
                if (tmpProjProposalTrans.ProjInvoiceProjId  != projInvoiceProjIdPrev  ||
                    !fundingSourceRefIdPrevSet.in(tmpProjProposalTrans.FundingSourceRefId) ||
                    tmpProjProposalTrans.CurrencyCode       != currencyCodePrev       ||
                    ((tmpProjProposalTrans.TaxInformation_IN != taxInformationRecid) && isInvoiceSplit_IN))
                {
                    ret = this.setProjProposalJour(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.ProjId, tmpProjProposalTrans.FundingSourceRefId, tmpProjProposalTrans.CurrencyCode, tmpProjProposalTrans.TaxInformation_IN, tmpProjProposalTrans.FixedExchRate);
                    fundingSourceRefIdPrevSet.add(tmpProjProposalTrans.FundingSourceRefId);
                }

                projInvoiceProjIdPrev  = tmpProjProposalTrans.ProjInvoiceProjId;
                fundingSourceRefIdPrev = tmpProjProposalTrans.FundingSourceRefId;
                currencyCodePrev       = tmpProjProposalTrans.CurrencyCode;
                taxInformationRecid    = tmpProjProposalTrans.TaxInformation_IN;
            }
            // </GBR>
            else
            {
                this.setProjProposalJour_BR(tmpProjProposalTrans);
                ret = true;
            }

            if (!ret)
            {
                delete_from tmpProjProposalTrans where tmpProjProposalTrans.ProjInvoiceProjId == projInvoiceProjIdPrev && tmpProjProposalTrans.FundingSourceRefId == fundingSourceRefIdPrev && tmpProjProposalTrans.CurrencyCode == currencyCodePrev;
                continue;
            }

            select RecId from projTable
                where projTable.ProjId == tmpProjProposalTrans.ProjId &&
                      projTable.ProjInvoiceProjId  == tmpProjProposalTrans.ProjInvoiceProjId
                exists join proposalJour
                    where proposalJour.ProjInvoiceProjId == projTable.ProjInvoiceProjId &&
                          proposalJour.ProposalId == projProposalJour.ProposalId;

            if (projTable.RecId)
            {
            switch (tmpProjProposalTrans.RefTableId)
            {
                case tableNum(ProjCostTransSale) :      this.doCost(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.RefRecId);
                                                        break;

                case tableNum(ProjEmplTransSale) :      this.doEmpl(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.RefRecId);
                                                        break;

                case tableNum(ProjItemTransSale) :      this.doItem(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.RefRecId);
                                                        break;

                case tableNum(ProjOnAccTransSale)  :    this.doOnAccount(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.RefRecId);
                                                        break;

                case tableNum(ProjRevenueTransSale) :   this.doRevenue(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.RefRecId);
                                                        break;

                case tableNum(SalesLine) :              this.doSalesLine(tmpProjProposalTrans.ProjInvoiceProjId, tmpProjProposalTrans.RefRecId);
                                                        break;
            }

            if (isCreditNoteTrans && invoiceQty > 0)
            {
                PSAContractLineItemsServer::updateUnitsOrPercentProposal(tmpProjProposalTrans.ContractLineNum, NoYes::Yes, invoiceQty, isCreditNoteTrans);
                isCreditNoteTrans = false;
                invoiceQty = 0;
            }
            else
            {
                PSAContractLineItemsServer::updateUnitsOrPercentProposal(tmpProjProposalTrans.ContractLineNum, NoYes::Yes);
            }
                if(tmpProjProposalTrans.Selected == NoYes::No)

                {
                    select forUpdate firstOnly proposalInvoiceTable where proposalInvoiceTable.PSAInvoiceId != '';
                    proposalInvoiceTable.ProposalId = projProposalJour.ProposalId;
                    proposalInvoiceTable.update();
                }
        }
        }


        this.updateInvoiceTotal();

        this.updateEInvoiceData_NO();

        // When an invoice proposal is generated, the system will
        // automatically create retainage withholding records and/or retainage billing records at the project level.
        if (isConfigurationkeyEnabled(configurationKeyNum(PSAARRetainage)))
        {
            this.calcRetention();
        }

        ttscommit;
    }

    catch (Exception::Deadlock)
    {
        retry;
    }

    catch (Exception::UpdateConflict)
    {
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::UpdateConflictNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::UpdateConflict;
        }
    }

    catch (Exception::Error)
    {
        // Update has been canceled.
        throw error("@SYS18447");
    }

    endLengthyOperation();
}
.........................

ProjInvoiceProposalCreateLinesBase
String30                                psaInvoiceId;
/// <summary>
/// Prepares the selected records for invoicing.
/// </summary>
/// <returns>
/// A buffer of type <c>PSATmpProjProposalTrans</c>
/// </returns>
/// <remarks>
/// Deduction transactions and regular transactions are merged.
/// </remarks>
public PSATmpProjProposalTrans prepareForInvoicing()
{
    // Start added by kiran PSA AX Integration 13 Aug 2018
    PSATmpProjProposalTrans     tmpProposalTrans;
    ALE_STG_HourJournal         hourJournal;
    ALE_STG_Milestone           milestone;
    ALE_STG_ExpenseJournal      expenseJournal;
    ALE_STG_FPReveueAllocation  allocation;
    ProjEmplTrans               projEmplTrans;
    ProjEmplTransSale           projEmplTransSale;
    ProjEmplTransCost           projEmplTransCost;
    ProjOnAccTrans              projOnAccTrans;
    ProjTable                   projTable;
    ProjOnAccTransSale          projOnAccTransSale;
    ProjCostTrans               projCostTrans;
    ProjCostTransCost           projCostTransCost;
    ProjCostTransSale           projCostTransSale;
   ProjRevenueTrans             projRevenueTrans;
    ProjRevenueTransSale        projRevenueTransSale;
    ProjCostTransTaxExtensionIN projCostTransTaxExtensionIN;
    boolean                     hourBatch;

    ALE_ProposalInvoiceTable    proposalInvoiceTable;

    select proposalInvoiceTable;

    hourBatch = true;
    // End added by kiran PSA AX Integration 13 Aug 2018

    while select proposalTransCreateDeduction where proposalTransCreateDeduction.Selected == true
    {
        tmpProposalTrans.data(proposalTransCreateDeduction);
        tmpProposalTrans.insert();
        hourBatch = false;
    }

    while select proposalTransCreate where proposalTransCreate.Selected == true
    {
        tmpProposalTrans.data(proposalTransCreate);
        tmpProposalTrans.insert();
        hourBatch = false;
    }
    if(hourBatch == true)
    {
        // insert hour journal lines
        /*while select allocation
            join projRevenueTrans order by TransId asc
                join projRevenueTransSale
                where allocation.Status == 2
                 &&   projRevenueTrans.TransId == allocation.TransId
            && projRevenueTransSale.TransId == projRevenueTrans.TransId
            && allocation.PSAInvoiceId == proposalInvoiceTable.PSAInvoiceId

        {
            projTable = ProjTable::find(allocation.ProjId);

            //tmpProposalTrans.ActivityNumber     = projRevenueTrans.ActivityNumber;
            tmpProposalTrans.CategoryId         = projRevenueTrans.CategoryId;
            tmpProposalTrans.CustAccount        = projTable.CustAccount;
            tmpProposalTrans.CurrencyCode       = projRevenueTrans.CurrencyId;
            tmpProposalTrans.DefaultDimension   = projRevenueTrans.DefaultDimension;
            tmpProposalTrans.FundingType        = ProjFundingSource::find(projRevenueTransSale.FundingSource).FundingType;
            tmpProposalTrans.FundingSourceRefId = projRevenueTransSale.FundingSource;
            tmpProposalTrans.LinePropertyId     = projRevenueTrans.LinePropertyId;
            tmpProposalTrans.LineAmount         = projRevenueTransSale.LineAmount;
            tmpProposalTrans.ProjId             = projRevenueTrans.ProjId;
            tmpProposalTrans.ProjInvoiceProjId  = projTable.ProjInvoiceProjId;
            tmpProposalTrans.Qty                = projRevenueTrans.Qty;
            tmpProposalTrans.RefRecId           = projRevenueTrans.RecId;
            tmpProposalTrans.RefTableId         = tableNum(projRevenueTransSale);
            tmpProposalTrans.RefTransId         = projRevenueTrans.TransId;
            tmpProposalTrans.RefRecIdTrans      = projRevenueTrans.RecId;
            //tmpProposalTrans.Task = projEmplTrans.tas;
            tmpProposalTrans.TransactionOrigin  = projRevenueTrans.TransactionOrigin;
            tmpProposalTrans.TransDate          = projRevenueTrans.TransDate;
            //tmpProposalTrans.TransType = projEmplTrans.transty.tra;
            tmpProposalTrans.SalesPrice         = projRevenueTransSale.SalesPrice;

            tmpProposalTrans.Worker = projRevenueTrans.Worker;
            tmpProposalTrans.insert();
        }*/
        //fee
        while select allocation
            join projRevenueTrans order by TransId asc
                join projRevenueTransSale
                where allocation.Status == 3
                 &&   projRevenueTrans.TransId == allocation.TransId
            && projRevenueTransSale.TransId == projRevenueTrans.TransId
            && allocation.PSAInvoiceId == proposalInvoiceTable.PSAInvoiceId

        {
            projTable = ProjTable::find(allocation.ProjId);

            //tmpProposalTrans.ActivityNumber     = projRevenueTrans.ActivityNumber;
            tmpProposalTrans.CategoryId         = projRevenueTrans.CategoryId;
            tmpProposalTrans.CustAccount        = projTable.CustAccount;
            tmpProposalTrans.CurrencyCode       = projRevenueTrans.CurrencyId;
            tmpProposalTrans.DefaultDimension   = projRevenueTrans.DefaultDimension;
            tmpProposalTrans.FundingType        = ProjFundingSource::find(projRevenueTransSale.FundingSource).FundingType;
            tmpProposalTrans.FundingSourceRefId = projRevenueTransSale.FundingSource;
            tmpProposalTrans.LinePropertyId     = projRevenueTrans.LinePropertyId;
            tmpProposalTrans.LineAmount         = projRevenueTransSale.LineAmount;
            tmpProposalTrans.ProjId             = projRevenueTrans.ProjId;
            tmpProposalTrans.ProjInvoiceProjId  = projTable.ProjInvoiceProjId;
            tmpProposalTrans.Qty                = projRevenueTrans.Qty;
            tmpProposalTrans.RefRecId           = projRevenueTrans.RecId;
            tmpProposalTrans.RefTableId         = tableNum(projRevenueTransSale);
            tmpProposalTrans.RefTransId         = projRevenueTrans.TransId;
            tmpProposalTrans.RefRecIdTrans      = projRevenueTrans.RecId;
            //tmpProposalTrans.Task = projEmplTrans.tas;
            tmpProposalTrans.TransactionOrigin  = projRevenueTrans.TransactionOrigin;
            tmpProposalTrans.TransDate          = projRevenueTrans.TransDate;
            //tmpProposalTrans.TransType = projEmplTrans.transty.tra;
            tmpProposalTrans.SalesPrice         = projRevenueTransSale.SalesPrice;

            tmpProposalTrans.Worker = projRevenueTrans.Worker;
            tmpProposalTrans.insert();
        }
    // cost
        while select hourJournal
        join projEmplTrans order by TransId asc
            join ProjEmplTransSale
            where hourJournal.Status == 2
            &&   projEmplTrans.TransId == hourJournal.TransId
            && ProjEmplTransSale.TransId == projEmplTrans.TransId
            && hourJournal.InvoiceId == proposalInvoiceTable.PSAInvoiceId
            && hourJournal.AXInvoiceId == ''
        {
            projTable = ProjTable::find(hourJournal.ProjId);

            tmpProposalTrans.ActivityNumber     = projEmplTrans.ActivityNumber;
            tmpProposalTrans.CategoryId         = projEmplTrans.CategoryId;
            tmpProposalTrans.CustAccount        = projTable.CustAccount;
            tmpProposalTrans.CurrencyCode       = projEmplTrans.CurrencyId;
            tmpProposalTrans.DefaultDimension   = projEmplTrans.DefaultDimension;
            tmpProposalTrans.FundingType        = ProjFundingSource::find(ProjEmplTransSale.FundingSource).FundingType;
            tmpProposalTrans.FundingSourceRefId = ProjEmplTransSale.FundingSource;
            tmpProposalTrans.LinePropertyId     = projEmplTrans.LinePropertyId;
            tmpProposalTrans.LineAmount         = ProjEmplTransSale.LineAmount;
            tmpProposalTrans.ProjId             = projEmplTrans.ProjId;
            tmpProposalTrans.ProjInvoiceProjId  = ProjTable::find(projEmplTrans.ProjId).ProjInvoiceProjId;
            tmpProposalTrans.Qty                = projEmplTrans.Qty;
            tmpProposalTrans.RefRecId           = projEmplTrans.RecId;
            tmpProposalTrans.RefTransId         = projEmplTrans.TransId;
            tmpProposalTrans.RefTableId         = tableNum(ProjEmplTransSale);
            tmpProposalTrans.RefRecIdTrans      = projEmplTrans.RecId;
            //tmpProposalTrans.Task = projEmplTrans.tas;
            tmpProposalTrans.TransactionOrigin  = projEmplTrans.TransactionOrigin;
            tmpProposalTrans.TransDate          = projEmplTrans.TransDate;
            //tmpProposalTrans.TransType = projEmplTrans.transty.tra;
            tmpProposalTrans.SalesPrice         = ProjEmplTransSale.SalesPrice;

            tmpProposalTrans.Worker             = projEmplTrans.Worker;
            tmpProposalTrans.insert();
        }
        // Expense
       /* while select hourJournal
        join projCostTrans order by TransId asc
            join projCostTransCost
            where hourJournal.Status == 2
            &&   projCostTrans.TransId == hourJournal.TransId
            && projCostTransCost.TransId == projCostTrans.TransId
            && hourJournal.InvoiceId == proposalInvoiceTable.PSAInvoiceId
            && hourJournal.AXInvoiceId == ''
        {
            projTable = ProjTable::find(hourJournal.ProjId);

            tmpProposalTrans.ActivityNumber         = projCostTrans.ActivityNumber;
            tmpProposalTrans.CategoryId             = projCostTrans.CategoryId;
            tmpProposalTrans.CustAccount            = projTable.CustAccount;
            tmpProposalTrans.CurrencyCode           = projCostTrans.CurrencyId;
            tmpProposalTrans.DefaultDimension       = projCostTrans.DefaultDimension;
            tmpProposalTrans.FundingType            = ProjFundingSource::find(projCostTransCost.FundingSource).FundingType;
            tmpProposalTrans.FundingSourceRefId     = projCostTransCost.FundingSource;
            tmpProposalTrans.LinePropertyId         = projCostTrans.LinePropertyId;
            tmpProposalTrans.LineAmount             = projCostTransCost.LineAmount;
            tmpProposalTrans.ProjId                 = projCostTrans.ProjId;
            tmpProposalTrans.ProjInvoiceProjId      = projTable.ProjInvoiceProjId;
            tmpProposalTrans.Qty                    = projCostTrans.Qty;
            tmpProposalTrans.RefRecId               = projCostTrans.RecId;
            tmpProposalTrans.RefTransId             = projCostTrans.TransId;
            tmpProposalTrans.RefTableId             = tableNum(projCostTransCost);
            tmpProposalTrans.RefRecIdTrans          = projCostTrans.RecId;
            //tmpProposalTrans.Task = projEmplTrans.tas;
            tmpProposalTrans.TransactionOrigin      = projCostTrans.TransactionOrigin;
            tmpProposalTrans.TransDate              = projCostTrans.TransDate;
            //tmpProposalTrans.TransType = projEmplTrans.transty.tra;
            tmpProposalTrans.SalesPrice             = projCostTransCost.CostPrice;

            tmpProposalTrans.Worker                 = projEmplTrans.Worker;
            tmpProposalTrans.insert();
        }*/

        while select expenseJournal
        join projCostTrans order by TransId asc
            join projCostTransSale
            where expenseJournal.Status == 2
            &&   projCostTrans.TransId == expenseJournal.TransId
            && projCostTransSale.TransId == projCostTrans.TransId
            && expenseJournal.PSAInvoiceId == proposalInvoiceTable.PSAInvoiceId
            && expenseJournal.AXInvoiceId == ''
        {
            projTable = ProjTable::find(expenseJournal.ProjId);

            tmpProposalTrans.ActivityNumber     = projCostTrans.ActivityNumber;
            tmpProposalTrans.CategoryId         = projCostTrans.CategoryId;
            tmpProposalTrans.CustAccount        = projTable.CustAccount;
            tmpProposalTrans.CurrencyCode       = projCostTrans.CurrencyId;
            tmpProposalTrans.DefaultDimension   = projCostTrans.DefaultDimension;
            tmpProposalTrans.FundingType        = ProjFundingSource::find(projCostTransSale.FundingSource).FundingType;
            tmpProposalTrans.FundingSourceRefId = projCostTransSale.FundingSource;
            tmpProposalTrans.LinePropertyId     = projCostTrans.LinePropertyId;
            tmpProposalTrans.LineAmount         = projCostTransSale.LineAmount;
            tmpProposalTrans.ProjId             = projCostTrans.ProjId;
            tmpProposalTrans.ProjInvoiceProjId  = projTable.ProjInvoiceProjId;
            tmpProposalTrans.Qty                = projCostTrans.Qty;
            tmpProposalTrans.RefRecId           = projCostTrans.RecId;
            tmpProposalTrans.RefTransId             = projCostTrans.TransId;
            tmpProposalTrans.RefTableId         = tableNum(projCostTransSale);
            tmpProposalTrans.RefRecIdTrans      = projCostTrans.RecId;
            //tmpProposalTrans.Task = projEmplTrans.tas;
            tmpProposalTrans.TransactionOrigin  = projCostTrans.TransactionOrigin;
            tmpProposalTrans.TransDate          = projCostTrans.TransDate;
            //tmpProposalTrans.TransType = projEmplTrans.transty.tra;
            tmpProposalTrans.SalesPrice         = projCostTrans.TotalSalesAmountCur;

            tmpProposalTrans.Worker = projEmplTrans.Worker;
            tmpProposalTrans.insert();
        }
        // Enpense End
        // Onaccount - Milestone

        while select milestone
        join projOnAccTrans order by TransId asc
            join projOnAccTransSale
            where milestone.Status == 2
            &&   projOnAccTrans.TransId == milestone.TransId
            && projOnAccTransSale.TransId == projOnAccTrans.TransId
            && milestone.InvoiceId == proposalInvoiceTable.PSAInvoiceId
            && milestone.AXInvoiceId == ''
        {
            projTable = ProjTable::find(milestone.ProjId);

            tmpProposalTrans.ActivityNumber = projOnAccTrans.ActivityNumber;

            tmpProposalTrans.CustAccount        = projTable.CustAccount;
            tmpProposalTrans.CurrencyCode       = projOnAccTrans.CurrencyId;
            tmpProposalTrans.DefaultDimension   = projOnAccTrans.DefaultDimension;
            tmpProposalTrans.FundingSourceRefId = projOnAccTransSale.FundingSource;
            tmpProposalTrans.FundingType        = ProjFundingSource::find(projOnAccTransSale.FundingSource).FundingType;
            tmpProposalTrans.LineAmount         = projOnAccTransSale.Amount;

            tmpProposalTrans.ProjId             = projOnAccTrans.ProjId;
            tmpProposalTrans.ProjInvoiceProjId  = projTable.ProjInvoiceProjId;
            tmpProposalTrans.Qty                = projOnAccTrans.Qty;
            tmpProposalTrans.RefRecId           = projOnAccTransSale.RecId;
            tmpProposalTrans.RefTableId         = tableNum(projOnAccTransSale);
            tmpProposalTrans.RefRecIdTrans      = projOnAccTrans.RecId;
            tmpProposalTrans.RefTransId         = projOnAccTrans.TransId;
            //tmpProposalTrans.Task = projOnAccTrans.LinePropertyId;
            tmpProposalTrans.TransactionOrigin  = projOnAccTrans.TransactionOrigin;
            tmpProposalTrans.TransDate          = projOnAccTrans.TransDate;
            tmpProposalTrans.TransType          = ProjTransType::OnAccount;
            tmpProposalTrans.SalesPrice         = projOnAccTransSale.Amount;

            tmpProposalTrans.insert();
        }
    }

    return tmpProposalTrans;
}
...................................................

ProjInvoiceProposalCreateLinesParams

public void new()
{
}

..................
ProjInvoiceJournalCreate

/// <summary>
/// Initializes the journal header record.
/// </summary>
protected void initJournalHeader()
{
    ALE_ProposalInvoiceTable proposalInvoiceTable;

    if(projProposalJour.LineProperty == ProjLinePropertyCode::Invoiced)
    {
        throw error("@SYS23025");
    }

    projInvoiceTable = projProposalJour.projInvoiceTable();

    this.checkBeforePostingEinvoice();

    creditWarning = ProjParameters::find().CreditLineError;

    projInvoiceJour.clear();

    projInvoiceJour.initFromProjProposal(projProposalJour);

    if (projInvoiceJour.DeliveryPostalAddress &&
        !LogisticsPostalAddress::findRecId(projInvoiceJour.DeliveryPostalAddress,
                                           false,
                                           DateTimeUtil::minValue(),
                                           DateTimeUtil::newDateTime(projProposalJour.InvoiceDate, timeMax())).Address)
    {
        projInvoiceJour.DeliveryPostalAddress  = LogisticsLocationEntity::location2PostalAddress(
                                                 ProjFundingSource::find(projProposalJour.FundingSource).InvoiceLocation).RecId;
    }

    projInvoiceJour.ProjInvoiceId        = this.getJournalNumber();
    projInvoiceJour.LedgerVoucher        = this.getVoucher();
    projInvoiceJour.ParmId               = projInvoiceParmTable.ParmId;
    projInvoiceJour.InvoiceDate          = projInvoiceParmTable.InvoiceDate;
    // todo
    // Start - Added on 14 Aug 2019
    select forUpdate firstOnly proposalInvoiceTable where proposalInvoiceTable.PSAInvoiceId != '';
    proposalInvoiceTable.InvoiceId = projInvoiceJour.ProjInvoiceId;
    proposalInvoiceTable.update();
    // End - Added on 14 Aug 2019

    if (!projInvoiceJour.isProforma())
    {
        if (EInvoiceParameters_MX::isElectronicInvoiceEnabled())
        {
            EInvoicePost_MX::validateInvoiceIDForEInvoice_MX(projInvoiceJour.ProjInvoiceId, numberSeq.parmNumberSequenceId());
        }

        if (EInvoiceCFDIParameters_MX::isElectronicInvoiceEnabled())
        {
            EInvoicePost_MX::validateInvoiceDateForCFDI_MX(projInvoiceJour.InvoiceDate);
        }
    }


    projInvoiceJour.DocumentDate_W = projInvoiceParmTable.DocumentDate_W;
    projInvoiceJour.SalesDate_CZ = projInvoiceParmTable.SalesDate_CZ;

    if(SysCountryRegionCode::isLegalEntityInCountryRegion([#isoLT, #isoLV]))
    {
        if (projInvoiceParmTable.AutoNumbering_LT)
        {
            if (! LtInvoiceAutoNumberingTable::checkLastDate(ltDocNumberingCode, projInvoiceParmTable.DocumentDate_W, true))
            {
                throw error("@SYS18447");
            }

            projInvoiceJour.InvoiceNumberingCode_LT   = LtInvoiceAutoNumberingTable::find(ltDocNumberingCode).NumberSequenceCode;

            LtInvoiceAutoNumberingTable::updateLastDate(ltDocNumberingCode, projInvoiceParmTable.DocumentDate_W);
        }
        projInvoiceJour.InvoiceRegister_LT = LtInvoiceAutoNumberingGroups::registeringForProjInvoice(CustTable::find(projInvoiceParmTable.InvoiceAccount).CustGroup);
    }

    this.exchRateSet();
}


Tuesday 20 August 2019

Create and post Invoice proposal through code in ax 2012 X++

Create and post Invoice proposal through code in ax 2012 X++

public class CreateInvoiceProposal extends RunBaseBatch
{

}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public boolean canGoBatch()
{
    return true;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

protected boolean canGoBatchJournal()
{
    return true;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public boolean runsImpersonated()
{
    return true;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

static ClassDescription description()
{
    return "Invocie proposal creation";
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public static void main(Args args)
{
    CreateInvoiceProposal createInvoiceProposal = new  CreateInvoiceProposal();

    if (createInvoiceProposal.prompt())
    {
        createInvoiceProposal.run();
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public void run()
{
    ProjParameters          projParameters;
    ProjEmplTrans           projEmplTrans;
    ProjOnAccTrans          projOnAccTrans;

    ProjProposalJour        projProposalJour;           //Table which will store journal records after creating Invoice Proposal
    ProjFormLetter          projFormLetter;

    ProjOnAccTrans          objProjOnAccTrans_1;
    ALE_STG_Milestone       milestone,milestoneLines;
    ALE_STG_HourJournal     hourJournal,hourJournalLines;
    ALE_STG_ExpenseJournal  expenseJournal,expenseJournalLines;
    TransactionID           transId;

    ALE_ProposalInvoiceTable proposalInvoiceTable;

    CategoryId              categoryId;
    ProjTable               projTable;
    container               transIdCon;
    int                     i,j;

    ProjInvoiceProposalInsertLines  projInvoiceProposalInsertLines;
    ProjInvoiceProposalCreateLinesBase  projInvoiceProposalCreateLinesBase;
    ProjInvoiceProposalCreateLines      proposalCreateLines;
    ProjInvoiceProposalCreateLinesParams projInvoiceProposalCreateLinesParams;

    proposalCreateLines = new ProjInvoiceProposalCreateLines();
    projInvoiceProposalCreateLinesParams = new ProjInvoiceProposalCreateLinesParams();
    proposalCreateLines.parmProposalCreateLinesParams(projInvoiceProposalCreateLinesParams).parmInvoiceDate(today());

    projInvoiceProposalInsertLines = new ProjInvoiceProposalInsertLines(proposalCreateLines,false);

    select projParameters;
    try
    {
        while select hourJournal group by InvoiceId,CompanyId order by ProjectDate asc //ProjectDate,ProjId
            join expenseJournal
            where hourJournal.Status == 2 && hourJournal.InterCompany == NoYes::No && hourJournal.InvoiceId != '' && hourJournal.AXInvoiceId == '' && expenseJournal.PSAInvoiceId == hourJournal.InvoiceId
        {
            ttsBegin;
            changeCompany(hourJournal.CompanyId)

        // Start added by kiran PSA AX Integration 13 Aug 2019
            delete_from proposalInvoiceTable;
        // End added by kiran PSA AX Integration 13 Aug 2019
            proposalInvoiceTable.PSAInvoiceId = hourJournal.InvoiceId;
            proposalInvoiceTable.insert();

            projInvoiceProposalInsertLines.run();

            select proposalInvoiceTable;
            select forUpdate projProposalJour where projProposalJour.ProposalId == proposalInvoiceTable.ProposalId  &&
                projProposalJour.InvoiceDate == today()
                && (projProposalJour.LineProperty!=ProjLinePropertyCode::Invoiced && projProposalJour.LineProperty!=ProjLinePropertyCode::Canceled);
                {

                    //For approving Invoice proposals

                    projProposalJour.LineProperty = ProjLinePropertyCode::Approved;

                    projProposalJour.update();

                    //For Posting Invoice Proposals

                    projFormLetter = ProjFormLetter::construct(DocumentStatus::ProjectInvoice);

                    projFormLetter.createParmLine(projProposalJour);

                    projFormLetter.run();

                    //To get latest record

                    projProposalJour = projProposalJour::find(projProposalJour.ProposalId);

                    info(strFmt("Invoice ProposalId: %1 \t InvoiceId: %2",projProposalJour.ProposalId,projProposalJour.ProjInvoiceId));

                }
               select proposalInvoiceTable;
            while select forUpdate hourJournalLines order by RecId asc where hourJournalLines.InvoiceId == hourJournal.InvoiceId
                && hourJournalLines.Status == 2
                && hourJournalLines.InterCompany == NoYes::No
            {
               hourJournalLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                hourJournalLines.Status = 3;
                hourJournalLines.update();
            }

            while select forUpdate expenseJournalLines order by RecId asc where expenseJournalLines.PSAInvoiceId == hourJournal.InvoiceId
                && expenseJournalLines.Status == 2
                && expenseJournalLines.InterCompany == NoYes::No
            {
               expenseJournalLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                expenseJournalLines.Status = 3;
                expenseJournalLines.update();
            }

            ttsCommit;
        }
        // Onaccount - expense
        while select milestone group by InvoiceId,CompanyId order by ProjDate asc //ProjectDate,ProjId
            join expenseJournal
            where milestone.Status == 2 && milestone.InterCompany == NoYes::No && milestone.InvoiceId != '' && milestone.AXInvoiceId == '' && expenseJournal.PSAInvoiceId == milestone.InvoiceId
        {
            ttsBegin;
            changeCompany(milestone.CompanyId)

        // Start added by kiran PSA AX Integration 13 Aug 2019
            delete_from proposalInvoiceTable;
        // End added by kiran PSA AX Integration 13 Aug 2019
            proposalInvoiceTable.PSAInvoiceId = milestone.InvoiceId;
            proposalInvoiceTable.insert();

            projInvoiceProposalInsertLines.run();

            select proposalInvoiceTable;
            select forUpdate projProposalJour where projProposalJour.ProposalId == proposalInvoiceTable.ProposalId  &&
                projProposalJour.InvoiceDate == today()
                && (projProposalJour.LineProperty!=ProjLinePropertyCode::Invoiced && projProposalJour.LineProperty!=ProjLinePropertyCode::Canceled);
                {

                    //For approving Invoice proposals

                    projProposalJour.LineProperty = ProjLinePropertyCode::Approved;

                    projProposalJour.update();

                    //For Posting Invoice Proposals

                    projFormLetter = ProjFormLetter::construct(DocumentStatus::ProjectInvoice);

                    projFormLetter.createParmLine(projProposalJour);

                    projFormLetter.run();

                    //To get latest record

                    projProposalJour = projProposalJour::find(projProposalJour.ProposalId);

                    info(strFmt("Invoice ProposalId: %1 \t InvoiceId: %2",projProposalJour.ProposalId,projProposalJour.ProjInvoiceId));

                }
               select proposalInvoiceTable;
            while select forUpdate milestoneLines order by RecId asc where milestoneLines.InvoiceId == milestone.InvoiceId
                && milestoneLines.Status == 2
                && milestoneLines.InterCompany == NoYes::No
            {
               milestoneLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                milestoneLines.Status = 3;
                milestoneLines.update();
            }

            while select forUpdate expenseJournalLines order by RecId asc where expenseJournalLines.PSAInvoiceId == milestone.InvoiceId
                && expenseJournalLines.Status == 2
                && expenseJournalLines.InterCompany == NoYes::No
            {
               expenseJournalLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                expenseJournalLines.Status = 3;
                expenseJournalLines.update();
            }

            ttsCommit;
        }
        // On Account - expense end
        // On account alone - start

        while select milestone group by InvoiceId,CompanyId order by ProjDate asc //ProjectDate,ProjId
            where milestone.Status == 2 && milestone.InterCompany == NoYes::No && milestone.InvoiceId != '' && milestone.AXInvoiceId == ''
        {
            ttsBegin;
            changeCompany(milestone.CompanyId)

        // Start added by kiran PSA AX Integration 13 Aug 2019
            delete_from proposalInvoiceTable;
        // End added by kiran PSA AX Integration 13 Aug 2019
            proposalInvoiceTable.PSAInvoiceId = milestone.InvoiceId;
            proposalInvoiceTable.insert();

            projInvoiceProposalInsertLines.run();

            select proposalInvoiceTable;
            select forUpdate projProposalJour where projProposalJour.ProposalId == proposalInvoiceTable.ProposalId  &&
                projProposalJour.InvoiceDate == today()
                && (projProposalJour.LineProperty!=ProjLinePropertyCode::Invoiced && projProposalJour.LineProperty!=ProjLinePropertyCode::Canceled);
                {

                    //For approving Invoice proposals

                    projProposalJour.LineProperty = ProjLinePropertyCode::Approved;

                    projProposalJour.update();

                    //For Posting Invoice Proposals

                    projFormLetter = ProjFormLetter::construct(DocumentStatus::ProjectInvoice);

                    projFormLetter.createParmLine(projProposalJour);

                    projFormLetter.run();

                    //To get latest record

                    projProposalJour = projProposalJour::find(projProposalJour.ProposalId);

                    info(strFmt("Invoice ProposalId: %1 \t InvoiceId: %2",projProposalJour.ProposalId,projProposalJour.ProjInvoiceId));

                }
               select proposalInvoiceTable;
            while select forUpdate milestoneLines order by RecId asc where milestoneLines.InvoiceId == milestone.InvoiceId
                && milestoneLines.Status == 2
                && milestoneLines.InterCompany == NoYes::No
            {
               milestoneLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                milestoneLines.Status = 3;
                milestoneLines.update();
            }
        ttsCommit;
        }

        // On account alone - end

        // Expense/Cost Invoice proposal

        while select expenseJournal group by PSAInvoiceId,CompanyId order by ProjectDate asc //ProjectDate,ProjId
            where expenseJournal.Status == 2 && expenseJournal.InterCompany == NoYes::No && expenseJournal.PSAInvoiceId != '' && expenseJournal.AXInvoiceId == ''
        {
            ttsBegin;
            changeCompany(expenseJournal.CompanyId)

        // Start added by kiran PSA AX Integration 13 Aug 2019
            delete_from proposalInvoiceTable;
        // End added by kiran PSA AX Integration 13 Aug 2019
            proposalInvoiceTable.PSAInvoiceId = expenseJournal.PSAInvoiceId;
            proposalInvoiceTable.insert();

            projInvoiceProposalInsertLines.run();

            select proposalInvoiceTable;
            select forUpdate projProposalJour where projProposalJour.ProposalId == proposalInvoiceTable.ProposalId  &&
                projProposalJour.InvoiceDate == today()
                && (projProposalJour.LineProperty!=ProjLinePropertyCode::Invoiced && projProposalJour.LineProperty!=ProjLinePropertyCode::Canceled);
                {

                    //For approving Invoice proposals

                    projProposalJour.LineProperty = ProjLinePropertyCode::Approved;

                    projProposalJour.update();

                    //For Posting Invoice Proposals

                    projFormLetter = ProjFormLetter::construct(DocumentStatus::ProjectInvoice);

                    projFormLetter.createParmLine(projProposalJour);

                    projFormLetter.run();

                    //To get latest record

                    projProposalJour = projProposalJour::find(projProposalJour.ProposalId);

                    info(strFmt("Invoice ProposalId: %1 \t InvoiceId: %2",projProposalJour.ProposalId,projProposalJour.ProjInvoiceId));

                }
            select proposalInvoiceTable;

            while select forUpdate expenseJournalLines order by RecId asc where expenseJournalLines.PSAInvoiceId == expenseJournal.PSAInvoiceId
                && expenseJournalLines.Status == 2
                && expenseJournalLines.InterCompany == NoYes::No
            {
                expenseJournalLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                expenseJournalLines.Status = 3;
                expenseJournalLines.update();
            }

            ttsCommit;
        }
        //
        //

        while select hourJournal group by InvoiceId,CompanyId order by ProjectDate asc //ProjectDate,ProjId
            where hourJournal.Status == 2 && hourJournal.InterCompany == NoYes::No && hourJournal.InvoiceId != '' && hourJournal.AXInvoiceId == ''
        {
            ttsBegin;
            changeCompany(hourJournal.CompanyId)

        // Start added by kiran PSA AX Integration 13 Aug 2019
            delete_from proposalInvoiceTable;
        // End added by kiran PSA AX Integration 13 Aug 2019
            proposalInvoiceTable.PSAInvoiceId = hourJournal.InvoiceId;
            proposalInvoiceTable.insert();

            projInvoiceProposalInsertLines.run();

            select proposalInvoiceTable;
            select forUpdate projProposalJour where projProposalJour.ProposalId == proposalInvoiceTable.ProposalId  &&
                projProposalJour.InvoiceDate == today()
                && (projProposalJour.LineProperty!=ProjLinePropertyCode::Invoiced && projProposalJour.LineProperty!=ProjLinePropertyCode::Canceled);
                {

                    //For approving Invoice proposals

                    projProposalJour.LineProperty = ProjLinePropertyCode::Approved;

                    projProposalJour.update();

                    //For Posting Invoice Proposals

                    projFormLetter = ProjFormLetter::construct(DocumentStatus::ProjectInvoice);

                    projFormLetter.createParmLine(projProposalJour);

                    projFormLetter.run();

                    //To get latest record

                    projProposalJour = projProposalJour::find(projProposalJour.ProposalId);

                    info(strFmt("Invoice ProposalId: %1 \t InvoiceId: %2",projProposalJour.ProposalId,projProposalJour.ProjInvoiceId));

                }
            select proposalInvoiceTable;
            while select forUpdate hourJournalLines order by RecId asc where hourJournalLines.InvoiceId == hourJournal.InvoiceId
                && hourJournalLines.Status == 2
                && hourJournalLines.InterCompany == NoYes::No
            {
               hourJournalLines.AXInvoiceId = proposalInvoiceTable.InvoiceId;
                hourJournalLines.Status = 3;
                hourJournalLines.update();
            }

            ttsCommit;
        }
        //


    }
    catch(Exception::Error)
    {
        error('An Exception has occurred');
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Get data from db in ax 2012 X++

Get data from db in ax 2012 X++

class  PSA_InvoiceProposalInboundProcess extends RunBaseBatch
{
    System.Data.OleDb.OleDbConnection objConn;
    System.Data.OleDb.OleDbCommand cmdSelect,cmdUpdate;
    System.Data.OleDb.OleDbDataReader reader,readerUpdate;
    container resultCon;
    str connectStr;
    ProjParameters projParameters;
    Map dimAttributeMap;
    ProjId  projID;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public boolean canGoBatch()
{
    return true;

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

protected boolean canGoBatchJournal()
{
    return true;

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public void createMilestone(container _c)
{
    ALE_STG_Milestone milestone;
    ProjLineProperty    projLineProperty;
    ProjId          projIDLoc,psaProjId;
    TransDate       ProjDateLoc;
    DimensionValue  WorkerLoc;
    Qty             HoursLoc;
    AmountMST       SalesPriceLoc,CostPriceLoc;
    str             LinePropertyLoc;
    CurrencyCode    CurrencyCodeLoc;
    Description     GuidLoc,ContractingUnitLoc,resourcingUnitLoc;
    str             CompanyIdLoc,msg;
    String30        invoiceStatus,psaInvoiceId;

    ;
    GuidLoc         = '';
    CompanyIdLoc    = '';
    /*ContractingUnitLoc = '';
    resourcingUnitLoc = '';*/


    GuidLoc         = conpeek(_c, 1);
    CompanyIdLoc    = conpeek(_c, 2);
    invoiceStatus    = conpeek(_c, 3);
    psaInvoiceId    = conpeek(_c, 4);
    /*resourcingUnitLoc  = conpeek(_c, 11);
    ContractingUnitLoc = conpeek(_c, 12);*/

        select forupdate milestone where milestone.GUID == GuidLoc;
    milestone.BillingStatus = invoiceStatus;
    milestone.InvoiceId = psaInvoiceId;
    milestone.Status = 2 ;
    milestone.update();

}



>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public Str1260 formatString(Str1260 _msg)
{
    str replace(str _source, str _what, str _with)
    {
        int found = 0;
        str target = _source;
        do
        {
            found = strscan(target, _what, found, strlen(target));
            if (found != 0)
            {
                target = strdel(target, found, strlen(_what));
                target = strins(target, _with, found);
                found += strlen(_with);
            }
        } while (found != 0);
        return target;
    }
    ;
    _msg = replace(_msg, '\n', '');
    _msg = replace(_msg, '\r', '');
    _msg = replace(_msg, '\t', '');
    _msg = replace(_msg, '\'', '');
    return _msg;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public container pack()
{
    return conNull();
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public container processMilestone(System.Data.OleDb.OleDbDataReader readerProject)
{
    int startLine = infologline(), currentLine, axStatus;
    int             i;
    Name            name,psaContractingUnit,psaResourcingUnit;
    ProjId          projIDLoc,psaProjId;
    TransDate       psaProjDate;
    DimensionValue  psaWorker;
    Qty             psaHours;
    AmountMST       psaSalesPrice,psaCostPrice;
    str             psaLineProperty;
    String30        psaInvoiceStatus,psaInvoiceId;
    Description     psaGuid;
    str             psaCompanyId,msg;

    psaGuid         = '';
    psaCompanyId    = '';
    psaInvoiceStatus = '';
    psaInvoiceId = '';
    /*psaResourcingUnit = '';
    psaContractingUnit = '';*/

    try
    {


        if (!readerProject.IsDBNull(0))
            psaInvoiceStatus = readerProject.GetString(0);
        else
            psaInvoiceStatus = "";

        if (!readerProject.IsDBNull(1))
            psaGuid = readerProject.GetString(1);
        else
            psaGuid = "";

        if (!readerProject.IsDBNull(2))
            psaCompanyId = readerProject.GetString(2);
        else
            psaCompanyId = "";
        if (!readerProject.IsDBNull(3))
            psaInvoiceId = readerProject.GetString(3);
        else
            psaInvoiceId = "";

        ttsbegin;

        // Invoice Status
        if (readerProject.IsDBNull(0))
        {
            msg = 'InvoiceStatus cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        this.createMilestone([psaGuid,psaCompanyId,psaInvoiceStatus,psaInvoiceId]);

        axStatus = 2;
        msg = strFmt('Milestone successfully ');
        ttscommit;
    }
    catch
    {
        ttsAbort;
        axStatus = -1;
        for (currentLine = startLine + 1; currentLine <= infologline(); currentLine++)
        {
            msg += infolog.text(currentLine);
        }
        msg = this.formatString(msg);

        infolog.clear();
    }
    return [axStatus, msg];
}


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public void run()
{
    str datestr;
    ALE_STG_HourJournal   hourJournalUpdate;
    ALE_STG_ExpenseJournal  expenseJournal;
    ALE_STG_Milestone       milestone;
    Ale_ProcessingStatus status;
    DataAreaId          psaCompanyId;
    Description         pGuid;
    string255 err;
    projParameters = ProjParameters::find();

    connectStr = 'Provider=SQLNCLI11;Integrated Security=SSPI;' +'Persist Security Info=False;Initial Catalog=' +
                    projParameters.ALE_PSA_StagingDBName + ';Data Source=' + projParameters.ALE_PSA_StagingDBServer + ';'+ 'User id=axadmin;Password=gavs_2012';
    try
    {
        objConn = new System.Data.OleDb.OleDbConnection(connectStr);
        objConn.Open();

        cmdSelect = objConn.CreateCommand();
        cmdSelect.set_CommandText('SELECT HJ.BillingStatus,HJ.GUID,HJ.CompanyId,HJ.PSAInvoiceId '+
                                    'FROM HourJournal AS HJ '+ 'WHERE HJ.STATUS = 2');
        reader = cmdSelect.ExecuteReader();

        while (reader.Read())
        {
            psaCompanyId = reader.GetString(2);

            if (psaCompanyId == '' || !CompanyInfo::findDataArea(psaCompanyId))
            {
                resultCon = [-1,"Company code error"];
                pGuid       = reader.GetString(1);
                cmdUpdate   = objConn.CreateCommand();
            }
            else
            {
                changeCompany(psaCompanyId)
                resultCon = this.processHourJournalTable(reader);
                pGuid       = reader.GetString(1);
                cmdUpdate   = objConn.CreateCommand();
            }

            cmdUpdate = objConn.CreateCommand();
            cmdUpdate.set_CommandText(strFmt('UPDATE HourJournal SET STATUS = %1,' + ' ERRORMESSAGE = ' + "'%2'" + 'WHERE GUID = ' + "'%3'",
                                                conPeek(resultCon, 1), conPeek(resultCon, 2), pGuid));
            readerUpdate = cmdUpdate.ExecuteReader();

            if (conPeek(resultCon, 1) == 1)
                status = Ale_ProcessingStatus::Processed;
            else if (conPeek(resultCon, 1) == -1)
                status = Ale_ProcessingStatus::Error;
            else
                status = Ale_ProcessingStatus::Unprocessed;

            err = conPeek(resultCon, 2);

            //Update AX staging hourJournal-start
            update_recordSet hourJournalUpdate
                setting processingstatus = status,ErrorMsg = err
            where hourJournalUpdate.GUID == pGuid
                && hourJournalUpdate.ProcessingStatus == Ale_ProcessingStatus::Unprocessed;
            //Update AX staging hourJournal-end
        }
        // update expense journal for Invoice proposal
        cmdSelect = objConn.CreateCommand();
        cmdSelect.set_CommandText('SELECT EJ.BillingStatus,EJ.GUID,EJ.CompanyId,EJ.PSAInvoiceId '+
                                    'FROM ExpenseJournal AS EJ '+ 'WHERE EJ.STATUS = 2');
        reader = cmdSelect.ExecuteReader();

        while (reader.Read())
        {
            psaCompanyId = reader.GetString(2);

            if (psaCompanyId == '' || !CompanyInfo::findDataArea(psaCompanyId))
            {
                resultCon = [-1,"Company code error"];
                pGuid       = reader.GetString(1);
                cmdUpdate   = objConn.CreateCommand();
            }
            else
            {
                changeCompany(psaCompanyId)
                resultCon = this.processExpenseJournalTable(reader);
                pGuid       = reader.GetString(1);
                cmdUpdate   = objConn.CreateCommand();
            }

            cmdUpdate = objConn.CreateCommand();
            cmdUpdate.set_CommandText(strFmt('UPDATE ExpenseJournal SET STATUS = %1,' + ' ERRORMESSAGE = ' + "'%2'" + 'WHERE GUID = ' + "'%3'",
                                                conPeek(resultCon, 1), conPeek(resultCon, 2), pGuid));
            readerUpdate = cmdUpdate.ExecuteReader();

            if (conPeek(resultCon, 1) == 1)
                status = Ale_ProcessingStatus::Processed;
            else if (conPeek(resultCon, 1) == -1)
                status = Ale_ProcessingStatus::Error;
            else
                status = Ale_ProcessingStatus::Unprocessed;

            err = conPeek(resultCon, 2);

            //Update AX staging hourJournal-start
            update_recordSet expenseJournal
                setting processingstatus = status,ErrorMsg = err
            where expenseJournal.GUID == pGuid
                && expenseJournal.ProcessingStatus == Ale_ProcessingStatus::Unprocessed;
            //Update AX staging hourJournal-end
        }
        //
        // on Account
        cmdSelect = objConn.CreateCommand();
        cmdSelect.set_CommandText('SELECT MS.BillingStatus,MS.GUID,MS.CompanyId,MS.PSAInvoiceId '+
                                    'FROM Milestone AS MS '+ 'WHERE MS.STATUS = 2');
        reader = cmdSelect.ExecuteReader();

        while (reader.Read())
        {
            psaCompanyId = reader.GetString(2);

            if (psaCompanyId == '' || !CompanyInfo::findDataArea(psaCompanyId))
            {
                resultCon = [-1,"Company code error"];
                pGuid       = reader.GetString(1);
                cmdUpdate   = objConn.CreateCommand();
            }
            else
            {
                changeCompany(psaCompanyId)
                resultCon = this.processMilestone(reader);
                pGuid       = reader.GetString(1);
                cmdUpdate   = objConn.CreateCommand();
            }

            cmdUpdate = objConn.CreateCommand();
            cmdUpdate.set_CommandText(strFmt('UPDATE Milestone SET STATUS = %1,' + ' ERRORMESSAGE = ' + "'%2'" + 'WHERE GUID = ' + "'%3'",
                                                conPeek(resultCon, 1), conPeek(resultCon, 2), pGuid));
            readerUpdate = cmdUpdate.ExecuteReader();

            if (conPeek(resultCon, 1) == 1)
                status = Ale_ProcessingStatus::Processed;
            else if (conPeek(resultCon, 1) == -1)
                status = Ale_ProcessingStatus::Error;
            else
                status = Ale_ProcessingStatus::Unprocessed;

            err = conPeek(resultCon, 2);

            //Update AX staging hourJournal-start
            update_recordSet milestone
                setting processingstatus = status,ErrorMsg = err
            where milestone.GUID == pGuid
                && milestone.ProcessingStatus == Ale_ProcessingStatus::Unprocessed;
            //Update AX staging hourJournal-end
        }
    }
    catch(Exception::Error)
    {
        error('An Exception has occurred');
    }

    if(objConn)
    {
        objConn.Close();
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public boolean runsImpersonated()
{
    return true;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public boolean unpack(container _packedClass)
{
    return true;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

static ClassDescription description()
{
    return " Project invoice proposal inbound process";
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public static void main(Args args)
{
    ALE_PSA_InvoiceProposalInboundProcess invoiceProposalInboundProcess = new ALE_PSA_InvoiceProposalInboundProcess();;

    if (invoiceProposalInboundProcess.prompt())
    {
        invoiceProposalInboundProcess.run();
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>