Wednesday 3 February 2016

How to Create Purchase order with Multiple Lines through Code in Ax 2012

static void Raj_Po(Args _args)
{
    NumberSeq numberSeq;
    Purchtable Purchtable;
    PurchLine PurchLine;
    PurchFormLetter purchFormLetter;
    
    ;
    ttsbegin;
    numberSeq =NumberSeq::newGetNum(purchparameters::numRefPurchId(),true);

    // Initialize Purchase order values
    Purchtable.initValue();
    Purchtable.PurchId = numberSeq.num();
    Purchtable.OrderAccount = 'US-101';
    Purchtable.CurrencyCode = 'USD';
    Purchtable.initFromVendTable();
    if (!Purchtable.validateWrite())
    {
        throw Exception::Error;
    }
    Purchtable.insert();

    // Initialize Purchase Line items
    PurchLine.PurchId = Purchtable.PurchId;
    PurchLine.ItemId = 'C0001';
    PurchLine.PurchQty = 2;
    PurchLine.createLine(true, true, true, true, true, false);
    PurchLine.clear();


    PurchLine.PurchId = Purchtable.PurchId;
    PurchLine.ItemId = 'C0002';
    PurchLine.PurchQty = 1;
    PurchLine.createLine(true, true, true, true, true, false);
    ttscommit;

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

        //PO Product Receipt
    ttsBegin;
    purchFormLetter = purchFormLetter::construct(DocumentStatus::PackingSlip);
    purchFormLetter.update(purchtable, // Purchase record Buffer
    "PO_"+purchTable.PurchId, // Invoice Number
    systemdateget()); // Transaction date
    ttsCommit;
    

    //PO Invoice
    ttsBegin;
    purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);
    //purchFormLetter.update(purchtable,"Inv_123",systemdateget()); // Transaction date
    //purchFormLetter.update(purchTable, strFmt("PO_%1", purchTable.PurchId));
    purchFormLetter.update(purchtable, // Purchase record Buffer
    "INV-"+purchTable.PurchId, // Invoice Number
    systemdateget()); // Transaction date
    
    ttscommit;
    if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)
    {
        info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));
    }
}

Update purchase order lines

static void updatePO(Args _args)
{
    SysExcelApplication     application;
    SysExcelWorkbooks       workbooks;
    SysExcelWorkbook        workbook;
    SysExcelWorksheets      worksheets;
    SysExcelWorksheet       worksheet;
    SysExcelCells           cells;
    COMVariantType          type;
    Name                    name;
    FileName                filename;
    Purchtable              purchtable;
    PurchLine               purchLine;
    InventDim               inventDim;
    inventDimId             inventDimId;
    int row;

    boolean first = true;
    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    filename = @"C:\Users\rajendra.c\Desktop\test.xlsx";
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    row =1;
    do
    {
        row++;
        select  purchtable where purchtable.PurchId == cells.item(row, 6).value().bStr()
            join purchLine where purchLine.PurchId == purchtable.PurchId;
        if(purchLine)
        {
            purchLine.clear();
            purchLine.PurchId   = cells.item(row, 6).value().bStr();
            purchLine.ItemId    = cells.item(row, 1).value().bStr();
            purchLine.PurchQty  = 1;
            //purchLine.initFromPurchTable(purchtable);
           
            //InventDim
            inventDim.clear();
            inventDim.InventSizeId   = cells.item(row, 4).value().bStr();
            //inventDim.InventSiteId   ="2";
            inventDim.wMSLocationId  = cells.item(row, 3).value().bStr();
            inventDim.inventSerialId = cells.item(row, 5).value().bStr();

            inventDimId              = InventDim::findOrCreate(inventDim).InventDimId;
            purchLine.InventDimId    = inventDimId;
            purchLine.createLine(true, true, true, true, true, false);

        }

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

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

    info("Updated successfully");

}

No comments:

Post a Comment