Thursday, April 14, 2011

create slaes order programmatically using x++ code


void CreateSalesOrder(container con,CustAccount custAccount,Description custDescription)
{
NumberSeq num;
str customerRef;
str deliveryName;
str deliveryStreet;
ExternalItemId externalItemId;

;

//create sales table
salesTable.clear();
salesTable.initValue();

num = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
newSalesId = num.num();

if (salesTable::exist(newSalesId))
{
num.abort();
checkFailed("@LIQ1977");
checkFailed(strfmt("@LIQ1978", newSalesId));
throw error("@SYS23020");
}

salesTable.SalesId = newSalesId;
num.used();

if(custDescription != "")
{
salesTable.SalesName = custDescription;
}

//fetch the customer details from cust table using account no selected from drop-down list
select * from custTable where custTable.AccountNum == custAccount;

salesTable.CustAccount = custTable.AccountNum;
salesTable.InvoiceAccount = custTable.InvoiceAccount;
salesTable.SalesType = SalesType::Sales;
salesTable.SalesStatus = SalesStatus::Backorder;
salesTable.CurrencyCode = custTable.Currency;

salesTable.CustGroup = custTable.CustGroup;
salesTable.DeliveryDateControlType = SalesDeliveryDateControlType::SalesLeadTime;

//Set dates as systemDateTime
salesTable.ReceiptDateRequested = systemdateget();
salesTable.ShippingDateRequested = systemdateget();

salesTable.PurchOrderFormNum = conpeek(con,4);
customerRef = substr(conpeek(con,7),7,2);
salesTable.CustomerRef = customerRef;
salesTable.LanguageId = #Language;
salesTable.DlvMode = #DlvMode;

//Init from cust table
salesTable.initFromCustTable();

//Overwrite customer address with the address from user file.
//concatenate string
deliveryName = conpeek(con,8) + ' ' + conpeek(con,9) + ' ' + conpeek(con,10);
salesTable.DeliveryName = deliveryName;
//concatenate string
deliveryStreet = conpeek(con,11) + ' \ ' + conpeek(con,12) + ' ' + conpeek(con,13);
salesTable.DeliveryStreet = deliveryStreet;

salesTable.DeliveryCity = strrtrim(conpeek(con,14));
salesTable.DeliveryState = strrtrim(conpeek(con,15));
salesTable.DeliveryZipCode = strrtrim(conpeek(con,16));
salesTable.ShipCarrierDeliveryContact = conpeek(con,17);

//Craete Sales Order
salesTable.insert();

// Create Sales Order Line
salesLine.SalesId = salesTable.SalesId;
salesLine.initFromSalesTable(salesTable);

externalItemId = conpeek(con,3);
//Get ItemId from custVendExternalItem table using externalItemId from file
select ItemId,RecId from custVendExternalItem where custVendExternalItem.ExternalItemId == externalItemId;
if(custVendExternalItem.RecId == 0)
{
throw error('@LIQ2648');
}
//Set sales item id from custVendExternalItem table
salesLine.ItemId = custVendExternalItem.ItemId;
salesLine.SalesUnit = InventTable::find(salesLine.ItemId).salesUnitId();
select * from inventTable where inventTable.ItemId == salesLine.ItemId;
salesLine.initFromInventTable(inventTable);

select * from inventDim where inventDim.InventLocationId == custTable.InventLocation && inventDim.InventSiteId == custTable.InventSiteId;

if(inventDim.RecId != 0)
{
salesLine.InventDimId = inventDim.inventDimId;
}
else
{
throw error("@LIQ1979");
}

//Set Qty as 1 by default as per business - Bo
salesLine.SalesQty = 1;
salesLine.ConfirmedDlv = salesTable.ShippingDateConfirmed;
salesLine.lineNum = SalesLine::lastLineNum(salesLine.salesId) + 1.0;
salesLine.LinePercent = 10;
salesLine.RemainInventPhysical = 1;
salesLine.RemainSalesPhysical = 1;
salesLine.DlvMode = #DlvMode;
salesLine.SalesStatus = SalesStatus::Backorder;
//salesLine.SalesPrice;
salesLine.setPriceDisc(inventDim);

//Insert sales line items
salesLine.insert();

}

No comments:

Post a Comment

Leave your comments and solutins