Create PO Confirm and invoice credit note orders

 class BPContractCreateCreditNote

{

    VendInvoiceTrans vendInvoiceTrans;

    BPInvoiceProposalTable proposalTable;

    BPInvoiceProposalLine proposalLine;

    PurchCopying purchCopying;

    TmpFrmVirtual tmpFrmVirtualHeader;

    TmpFrmVirtual tmpFrmVirtualLines;

    SysInfoAction_FormRun SysInfoAction;

    VendInvoiceJour  invoiceJour;

    BPInvoiceProposalD proposalId;

        

    


    public static void createCreditNoteFromInvProposal(container _callerparameters)

    {

        BPContractCreateCreditNote creditNoteCreate =  new  BPContractCreateCreditNote();

        creditNoteCreate.runProcess(_callerparameters);

        

       

    }


    public Void runProcess(container _callerparameters)

    {

        proposalTable =conPeek(_callerparameters, 1);

        proposalId =  proposalTable.BPInvoiceProposalD;

       

       /* while select proposalLine where proposalLine.BPInvoiceProposald == proposalTable.BPInvoiceProposalD

        {

            this.loadTmpVirtualTableAndCreateCN(VendInvoiceTrans::findRecId(proposalLine.CreditNoteRefRecId));

        }

        this.createCN();  */


        this.createAndPostCreditPO();

        

        //sysInfoAction = SysInfoAction_FormRun::newFormnameDesc(formstr(SalesTable), “show credit note”);

        //sysInfoAction.parmCallerBuffer(SalesTable::find(salesId));

        //info(strfmt("Credit notes created succesfully for the SalesOrder %1 and for lotId %2", salesId, dfInventTransId.value()),”,sysInfoAction);

    }


    void loadTmpVirtualTableAndCreateCN(vendInvoiceTrans _vendInvoiceTrans)

    {


        tmpFrmVirtualLines.TableNum = _vendInvoiceTrans.TableId;

        tmpFrmVirtualLines.RecordNo = _vendInvoiceTrans.RecId;

        tmpFrmVirtualLines.Id = _vendInvoiceTrans.InvoiceId;

        tmpFrmVirtualLines.LineNum = _vendInvoiceTrans.LineNum;

        tmpFrmVirtualLines.TransDate = _vendInvoiceTrans.InvoiceDate;

        tmpFrmVirtualLines.Qty = _vendInvoiceTrans.Qty;


        tmpFrmVirtualLines.write();

    }


    void createCN()

    {



        //PurchCopyingForm purchCopyingForm    = PurchCopyingForm::newPurchCopyingForm(SalesPurchCopy::CreditNoteLines, purchTable,fr );

        //purchCopyingForm.constructAndInitCopying(tmpFrmVirtualLines,tmpFrmVirtualHeader,1,NoYes::Yes);

        //purchCopyingForm.copy();



     

    }


    public void createAndPostCreditPO()

    {

        boolean hasModified;

        NumberSeq numberSeq;

        Purchtable Purchtable;

        PurchLine PurchLine;

        PurchFormLetter purchFormLetter;

        PurchLineForeignTradeCategory purchLineForeignTradeCategory;


        if(!(proposalTable.CreditNotePurchId && PurchTable::find( proposalTable.CreditNotePurchId)) )

        {

            ttsbegin;

            //numberSeq = //NumberSeq::newGetNumFromCode(purchParameters::numRefPurchaseOrderId().NumberSequenc//e,true);


            // Initialize Purchase order values

            Purchtable.initValue();

            Purchtable.PurchId = NumberSeq::newGetNum(PurchParameters::numRefPurchId()).num();//numberSeq.num();

            Purchtable.OrderAccount = proposalTable.bpProjContractTable().InvoiceAccount;

            Purchtable.initFromVendTable();

            if (!Purchtable.validateWrite())

            {

                throw Exception::Error;

            }

            Purchtable.insert();


            ttscommit;


            hasModified = true;

            ttsbegin;

            BPInvoiceProposalTable  proposalUpdate = BPInvoiceProposalTable::find(proposalId,true);

            proposalUpdate.CreditNotePurchId =  Purchtable.PurchId;

            proposalUpdate.doUpdate();

            ttscommit;

        }

        else

        {

            purchTable =     PurchTable::find( proposalTable.CreditNotePurchId);

        }


        if(Purchtable)

        {


            while select proposalLine where proposalLine.BPInvoiceProposald == proposalTable.BPInvoiceProposalD

            {

                select firstonly forupdate  purchLine where purchLine.BPCreditInvProposalLineRecId == proposalLine.RecId;

  

                if(!purchLine.RecId )

                {

                    PurchLine purchLineLoc =   VendInvoiceTrans::findRecId(proposalLine.CreditNoteRefRecId).purchLine();

                    hasModified = true;

            

                    ttsbegin;

                    purchLine.clear();

                    purchLine.initValue();


                    purchLine.PurchId = purchTable.PurchId;

                    purchLine.ProcurementCategory = purchLineLoc.ProcurementCategory;

                    purchLine.initFromItemOrCategory("" ,purchLineLoc.ProcurementCategory,purchLineLoc.Name,false,false);

                    purchLine.InventDimId=purchLineLoc.inventDimId;

                    purchLine.ProjId =  purchLineLoc.ProjId;

                    purchLine.ProjCategoryId = purchLineLoc.ProjCategoryId;

                    purchLine.ProjLinePropertyId = purchLineLoc.ProjLinePropertyId ;

                    purchLine.ActivityNumber =  purchLineLoc.ActivityNumber;


                    purchLine.createLine(true, true, true, true, true, true);

                    if (purchLine.RecId && purchLine.isCategoryBased())

                    {

                        purchLineForeignTradeCategory.initFromPurchLine(purchLine);

                        purchLineForeignTradeCategory.insert();

                    }

                    purchLine.PurchQty = proposalLine.PurchQty;

                    purchLine.RemainPurchPhysical =  purchLine.PurchQty;

                

                    purchLine.PurchUnit = purchLineLoc.PurchUnit;

                    purchLine.LineAmount = proposalLine.InvoiceNowAmount;

                    purchLine.BPCreditInvProposalLineRecId = proposalLine.RecId ;

                    purchLine.update();


                    ttsCommit;

                }

                else

                {

                    if(purchLine.LineAmount != proposalLine.InvoiceNowAmount)

                    {

                        hasModified = true;

                        ttsbegin;

                        purchLine.selectForUpdate(true);

                        purchLine.LineAmount =  proposalLine.InvoiceNowAmount;

                        purchLine.update();

                        ttscommit;

                    }

                }

            }


            if(this.canConfirm())

            {

                if(hasModified ||  (!hasModified && PurchTable::find(purchTable.PurchId).DocumentState != VersioningDocumentState::Confirmed ))

                {


                    purchFormLetter = PurchFormLetter_Confirmation::newConfirmation();

                    purchFormLetter.update(purchTable,

                                strFmt("ConNum_%1", purchTable.PurchId),

                                systemDateGet(),

                                PurchUpdate::All,

                                AccountOrder::None,

                                NoYes::No,

                                NoYes::no);

                }


                if(PurchTable::find(purchTable.PurchId).DocumentState == VersioningDocumentState::Confirmed)

                {


                    purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);

                    purchFormLetter.update(purchtable, // Purchase record Buffer

                proposalTable.BPInvoiceProposalD, // Invoice Number

                systemdateget()); // Transaction date

                    if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)

                    {

               

                        invoiceJour = purchFormletter.getOutputContract().parmJournal();

                        this.endPostingUpdate();

                        info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));

                    }

                    else

                    {

                        throw error(strfmt("unable to post invoice %1",purchTable.PurchId) );

                    }

                }

                else

                {

                    throw error(strfmt("unable to confirm %1",purchTable.PurchId) );

                }

            }

            else

            {

                throw error(strfmt("unable to create credit note order lines for %1",purchTable.PurchId) );

            }


        }

    }


    public void endPostingUpdate()

    {

       BPInvoiceProposalTable proposalLoc = BPInvoiceProposalTable::find(proposalId,true);


        ttsbegin;

        proposalLoc.selectForUpdate(true);

        proposalLoc.InvoiceAmount= invoiceJour.InvoiceAmount;

        proposalLoc.BPDocumentStatus = BPDocumentStatus::Invoiced;

        proposalLoc.doUpdate();

        ttscommit;


        ttsbegin;

        invoiceJour.selectForUpdate(true);

        invoiceJour.BPInvoiceProposald = proposalLoc.BPInvoiceProposalD;

        invoiceJour.BPContractNumber = proposalLoc.BPContractNumber;

        invoiceJour.doUpdate();

        ttscommit;

    }


    public boolean canConfirm ()

    {

        BPInvoiceProposalLine proposalLineCount;


        purchLine         purchLineLoc  ;


        select firstonly RecId from  proposalLineCount

            where proposalLineCount.BPInvoiceProposald == proposalId

            notexists join purchLineLoc

            where purchLineLoc.BPCreditInvProposalLineRecId  == proposalLineCount.RecId ;


        if(proposalLineCount.RecId)

        {

            return false;

        }

        return true;

    }


}

Comments

Popular posts from this blog

Logistics address, postal address, address phone fax contact information

Ledger dimensions and financial Dimensions X++

Prepay posting vendor invoice with amounts