Monday, 20 January 2014

how to split purchase order through company wise in ax 2012

void clicked()
{
    Dialog      dialog;
    DialogField SelectedOrder, SelectedCompany, SelectedQuantity;
    PurchTable purchTable,purchTablelocal,purchTableMain;
    PurchLine  LocPurchLine,purchLine,purchlinelocal;
    VendTable       vendTable1 ;
    AxPurchTable    axPurchTable;
    AxPurchLine     axPurchLine;
    PurchFormLetter purchFormLetter;
    NumberSeq      numberSeq;
    str         PurchId, compny, p_order,ordr_act,currency,vend_group,language,name,item,item_name,unit;
    real qty,price,net_amt,p=0,q=0;

    container  othercompany;
    ;
    super();
    purchTable = PurchaseTable_ds.getFirst(1);
    PurchId    = purchTable.PurchId;
    ordr_act    = purchTable.OrderAccount;
    currency  = purchTable.CurrencyCode;
    vend_group = purchTable.VendGroup;
    language = purchTable.LanguageId;
    name = purchTable.PurchName;
    select ItemId,PurchPrice,PurchUnit,LineAmount from purchLine join * from purchTable where purchTable.PurchId==purchLine.PurchId;
    item = purchLine.ItemId;
    price = purchLine.PurchPrice;
    unit = purchLine.PurchUnit;
    //net_amt = purchLine.LineAmount;
    //item_name = purchLine.itemName();

    dialog = new Dialog("Split Purchase Order");
    selectedOrder = dialog.addFieldValue(extendedTypeStr(PurchId),PurchId, "Selected Purchase Order ");
    dialog.addText("Select New Company ");
    SelectedCompany= dialog.addField(extendedTypeStr(SelectableDataArea));
    dialog.addText("Enter new quantity");
    SelectedQuantity = dialog.addField(extendedTypestr(PurchOrderedQty));

    dialog.run();
    if (dialog.closedOk())
    {
        info(selectedOrder.value());
        info(SelectedCompany.value());
        info(SelectedQuantity.value());

        qty = any2real(SelectedQuantity.value());
        compny = any2str(SelectedCompany.value());
        p_order = any2str(selectedOrder.value());

        ttsbegin;
        changeCompany(compny)
        {
            purchTable   = null;
            purchLine   = null;
            //purchTablelocal = null;
            // numberSeq = NumberSeq::newGetNumFromCode(purchParameters::numRefPurchaseOrderId().NumberSequence,true);
             select forUpdate purchTablelocal  where purchTablelocal.PurchId ==selectedOrder.value()&& purchTablelocal.dataAreaId==SelectedCompany.value();
            if(!purchTablelocal.RecId)
            {
                purchTablelocal.initValue();
                purchTablelocal.PurchId = p_order;                     //numberSeq.num();
                purchTablelocal.OrderAccount = ordr_act;
                purchTablelocal.InvoiceAccount=ordr_act;
                purchTablelocal.CurrencyCode=currency;
                purchTablelocal.VendGroup= vend_group;
                purchTablelocal.PurchName=name;
                purchTablelocal.LanguageId=language;
                purchTablelocal.initFromVendTableMandatoryFields();
                purchTablelocal.insert();
                info(strFmt("Created a purchase order %1 in %2 Company",purchTablelocal.PurchId,SelectedCompany.value()));
            }
            select forUpdate LocPurchLine  where LocPurchLine.PurchId ==selectedOrder.value()&& LocPurchLine.dataAreaId==SelectedCompany.value();
            if(!LocPurchLine.RecId)
             {
                LocPurchLine.PurchId = purchTablelocal.PurchId;
                LocPurchLine.ItemId = item;
                LocPurchLine.itemName();
                LocPurchLine.PurchQty = qty;
                LocPurchLine.PurchUnit = unit;
                LocPurchLine.VendGroup =  vend_group;
                LocPurchLine.PurchPrice = price;
                LocPurchLine.LineAmount = qty*price;
                //LocPurchLine.createLine(true, true, true, true, true, false);
                LocPurchLine.insert();

            }

        }
        ttscommit;

        changeCompany('USMF')
        {

            ttsBegin;
            select forUpdate purchlinelocal  where purchlinelocal.PurchId == selectedOrder.value();

            {

                   if(purchlinelocal.RecId)
                {
                   p = purchlinelocal.PurchQty ;
                   q = p - qty ;
                   purchlinelocal.PurchQty = q ;
                   purchlinelocal.LineAmount = q*price;
                   purchlinelocal.update();
                }
            ttsCommit;
            }

        }

    }
 }

No comments:

Post a Comment