Monday, July 27, 2020

Create Purchase Order using X++

PurchTable                        purchTable;
PurchLine                         purchLine;
PurchId                             purchId;
LogisticsPostalAddress    logisticsPostalAddress;

//Create purchase order
public void createPO()
{
    ttsbegin;
    
    this.createHeader();
    this.createLine();
    if(purchId)
    {
            this.postConfirmation();
     }
    ttscommit;
}

//Create purchase order header
public void createHeader()
{
        purchId = NumberSeq::newGetNum(PurchParameters::numRefPurchId()).num();
        purchTable.clear();
        purchTable.PurchId = purchId;
        purchTable.initValue();
        purchTable.initFromVendTable(VendTable::find("VendAccount"));
        purchTable.PurchStatus = PurchStatus::Backorder;
        purchTable.DocumentState = VersioningDocumentState::Approved;
        purchTable.InventSiteId = InventLocation::find("ToWarehouse").InventSiteId;
        purchTable.InventLocationId = "ToWarehouse";
        purchTable.ChangeRequestRequired = NoYes::no;
        purchTable.MCRDropShipment = NoYes::Yes;
        logisticsPostalAddress = InventLocation::find("ToWarehouse").logisticsPostalAddress();
        purchTable.DeliveryName = Logisticslocation::find(logisticsPostalAddress.location).description;
        purchTable.DeliveryPostalAddress = logisticsPostalAddress.RecId;
        purchTable.VendorRef  = "Vendor reference"; //Optional
        purchTable.Requester = "HCMWorkerRecId";
        purchTable.WorkerPurchPlacer = this.getWorkerPurchPlacer();
        purchTable.insert();
}

//Create purchase order line
public void createLine()
{
        purchLine.clear();
        purchLine.PurchId = purchId;
        purchLine.ItemId = "ItemId";
        purchLine.itemIdChanged();
        purchLine.PurchQty = "Quantity";
        purchLine.DeliveryType = TradeLineDlvType::DropShip;
        inventDimfind.InventSiteId = purchTable.InventSiteId;
        inventDimfind.InventLocationId = purchTable.InventLocationId;
        inventDimfind.wMSLocationId = InventLocation::find(purchTable.InventLocationId).WMSLocationIdDefaultReceipt;
        inventDimLines = InventDim::findOrCreate(inventDimfind);
        purchLine.createLine(true, true, true, false, true, true);
        purchLine.selectForUpdate(true);
        purchLine.InventDimId = inventDimLines.inventDimId;
        purchLine.doUpdate();
}

//Post purchase order confirmation
public void postConfirmation()
{
        PurchFormLetter     purchFormLetter;
        purchformletter = PurchFormLetter::construct(documentstatus::PurchaseOrder);
        purchformletter.update(purchtable,
                                strfmt("inv_%1", purchId), systemDateGet(), PurchUpdate::All, 
                                AccountOrder::None,
                                NoYes::No,
                                true,
                                true);
}

public RefRecId getWorkerPurchPlacer ()
{
        UserInfo userInfo;
        RefRecId refRecId;
        select name from userInfo where userInfo.id == curUserId();

        if(userInfo.id != '')
        {        
            refRecId = HcmWorker::findByPerson(DirPerson::find(DirPartyTable::findByName(                                                userInfo.name).RecId).RecId).RecId;
        }
        return refRecId;
}

No comments:

Post a Comment