Monday 17 April 2017

Create, Confirm/ Invoice Purchase order

static void CreatePOInvoice(Args _args)
{
    PurchTable purchTable;
    PurchLine purchLine;
    VendTable vendTable = VendTable::find("000001"); //Specify Vendor Account which is nothing but selecting a Purchase Order
    AxPurchTable axPurchTable;
    AxPurchLine axPurchLine;
    PurchFormLetter purchFormLetter;
    ;

    //Create Purchase order
    purchTable.initFromVendTable(vendTable);

    axPurchTable = axPurchTable::newPurchTable(purchTable);
    axPurchTable.parmPurchaseType(PurchaseType::Purch);
    axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder);


    axPurchTable.parmDeliveryDate(str2date("08/18/2014",213)); // Important because system check the transaction date.
    axPurchTable.parmAccountingDate(str2date("08/18/2014",213));
    axPurchTable.parmPurchStatus(PurchStatus::Backorder);

    axPurchTable.doSave();


    purchLine.initFromPurchTable(purchTable);

    axPurchLine = AxPurchLine::newPurchLine(purchLine);
    axpurchLine.parmItemId("450");
    //axpurchLine.parmInventDimId('000458');//important because Wherhouse, dimension location, dimension inventory status must be specified.
    axPurchLine.parmPurchQty(10);
    axPurchLine.parmPurchPrice(100);
    axPurchLine.doSave();

    //PO confirmation
    purchTable = axPurchTable.purchTable();
    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
    purchFormLetter.update(purchTable, strFmt("Inv_%1", purchTable.PurchId));

    // PO invoicing
    purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
    purchFormLetter.update(purchTable, strFmt("Inv_%1", purchTable.PurchId));
    info(strFmt("purchase order %1 invoiced",purchTable.PurchId));

}

================================================================
public void createPOInvoice(DropShipInvoiceTmp dropShipInvoiceTmp,
                            ImportInvoices importInvoices,
                            transdate   shipDate = systemDateGet())
{
    PurchFormLetter purchFormLetter;
    VendInvoiceInfoTable    vendInvoiceInfoTable;
    VendInvoiceInfoTable    vendInvInfoTable;
    PurchTable  purchTable = PurchTable::find(dropShipInvoiceTmp.PurchId, true);
    VendInvoiceInfoLine     vendInvoiceinfoLine;
    TmpFrmVirtual tmpFrmVirtual;
    List selectedList = new List(Types::Record);
    vendPackingSlipJour vendPackingSlipJour;
    Set             origPurchSubTableSet;
    Set             origPurchLineSet;
    Set             newPurchLineSet;
    container purchSubTableCont;
    container purchLineCont;
    PurchFormLetter_Invoice _purchFormLetter_invoice;
    VendAccount  invoiceAccount;
    CurrencyCode    currencyCode;

    origPurchSubTableSet = new Set(typeName2Type(extendedTypeStr(PurchId)));
    origPurchLineSet = new Set(typeName2Type(extendedTypeStr(RecId)));
    newPurchLineSet = new Set(typeName2Type(extendedTypeStr(RecId)));

    ttsBegin;
    //create entries in vendinvoiceinfo tables
    purchFormLetter = PurchFormLetter::construct(7, 'FormLetterService', 'postPurchaseOrderInvoice', SysOperationExecutionMode::Synchronous);
    purchFormLetter.resetParmListCommonCS();

    //purchFormLetter.parmCallerFormName('purchTable');
    purchFormLetter.parmDocumentOrigin(DocumentOrigin::Manual);
    purchFormLetter.parmCallerMenuItem('PurchFormLetter_Invoice');

    //purchFormLetter.parmCallerTable(purchTable);
    //purchFormLetter.purchTable(purchTable);
    //ttsbegin;
    tmpFrmVirtual.setTmp();
    // Add the packing slips into tmpFrmVirtual
        vendpackingslipjour.clear();
    while select vendpackingslipjour
    where vendpackingslipjour.packingslipid == "PO-0001287" || vendpackingslipjour.packingslipid == "PO-0001288"

    {
    tmpFrmVirtual.clear();
    tmpFrmVirtual.TableNum = vendpackingslipjour.TableId;
    tmpFrmVirtual.RecordNo = vendpackingslipjour.RecId;
    tmpFrmVirtual.NoYes = NoYes::Yes;
    tmpFrmVirtual.Id = vendpackingslipjour.PurchId;
    tmpFrmVirtual.insert();

    }
    while select tmpFrmVirtual
    {
    selectedList.addEnd(tmpFrmVirtual);
    }
    Purchformletter.selectFromJournal(selectedList.pack());
    purchFormLetter.specQty(PurchUpdate::PackingSlip);
    //purchFormLetter.parmCallerMenuItem('PurchFormLetter_Invoice');
    //purchFormLetter.allowEmptyTable(purchFormLetter.initAllowEmptyTable(true));

    purchFormLetter.multiForm(true);
    purchFormLetter.proforma(false);
    //purchFormLetter.enableUpdateNowField(true);
    purchFormLetter.parmInvoiceType(PurchInvoiceType::Standard);
    //purchFormLetter.prePromptInit();
    //purchFormLetter.forceSaveLast();
    vendInvoiceInfoTable.clear();
    /*ttsBegin;
    if (!purchTable.DropShipInvoice)
    {
        purchTable.DropShipInvoice = NoYes::Yes;
        purchTable.update();
    }
    ttsCommit;*/

    //updating the doc date and invoice id in vendinvoiceinfotable before posting
    ttsBegin;
    select firstonly vendInvoiceInfoTable
    order by RecId Desc
        where
            vendInvoiceInfoTable.ParmId  != "";
    if (vendInvoiceInfoTable)
    {
        vendInvInfoTable = vendInvoiceInfoTable::findRecId(vendInvoiceInfoTable.RecId, true);
        vendInvInfoTable.Num            = 'poi3ufgyt';
        vendInvInfoTable.DocumentDate   = shipDate;
        importInvoices = importInvoices::Create;
        if (importInvoices == ImportInvoices::Create)
            vendInvInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;
        vendInvInfoTable.update();
    }
    ttsCommit;
    _purchFormLetter_invoice = purchFormLetter;
    /*purchSubTableCont += 'PO-0001283';
    purchSubTableCont += 'PO-0001284';
    purchLineCont += purchTable::find('PO-0001283').RecId;
    purchLineCont += purchTable::find('PO-0001284').RecId;*/
    [invoiceAccount, currencyCode, purchSubTableCont, purchLineCont] = PurchSelectLinesManager::getPurchSetsData(vendInvInfoTable.TableRefId);
    origPurchSubTableSet = Set::create(purchSubTableCont);
    origPurchSubTableSet.add('PO-0001288');
    origPurchLineSet = Set::create(purchLineCont);
    newPurchLineSet = Set::create(purchLineCont);
    newPurchLineSet.add(purchTable::find('PO-0001288').RecId);
    info(con2Str(purchSubTableCont));
    _purchFormLetter_invoice.purchSelectLines(origPurchSubTableSet, newPurchLineSet, vendInvInfoTable);
    if (importInvoices == ImportInvoices::Create)
    {
        VendInvoiceInfoTable::moveFromActiveToSaved(vendInvInfoTable);
        while select  forUpdate vendInvoiceInfoTable
            where vendInvoiceInfoTable.PurchId == purchTable.PurchId
                && vendInvoiceInfoTable.ParmId  != ""
                && vendInvoiceInfoTable.RecId != vendInvInfoTable.RecId
                && vendInvoiceInfoTable.VendInvoiceSaveStatus == VendInvoiceSaveStatus::Pending
        {
            vendInvoiceInfoTable.resetParmId('');
        }
        //purchFormLetter.prePromptModifyData();
    }
    //Create the pending invoice
    purchFormLetter.editLinesChanged(true);
    purchFormLetter.reArrangeNow(false);
    purchFormLetter.parmSaveChanges(true);

    //apply charges
    //if (dropShipInvoiceTmp.ShippingCharge)
      //  this.createShippingCharges(dropShipInvoiceTmp, vendInvInfoTable, purchTable.OrderAccount);
    if (importInvoices != ImportInvoices::Create)
        purchFormLetter.run();
    ttsCommit;

}