class CreatePostSalesOrder
{
    ///
<summary>
    ///
Create sales order and posting sales order
    ///
</summary>
   
public static void main(Args _args)
   
{        
       
SalesTable salesTable;
       
SalesLine salesLine;
       
InventTable inventTable;
       
InventDim inventDim;
       
CustTable custTable;
       
CustAccount custAccount;
       
NumberSeq numberSeq;
       
SalesId salesID;
       
str warehouse;
salesFormLetter      salesFormLetter ;
       
try
       
{
           
ttsbegin;
           
salesTable.clear();
           
//Number sequence automatically gets the next number as per system's
configuration
           
//SalesId is mandatory to create sales order
           
numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
           
numberSeq.used();
           
salesTable.SalesId = numberSeq.num();
           
salesID = salesTable.SalesId;
           
salesTable.initValue();
           
           
//Set the warehouse as per your scenario
           
//It will be used to set the Site & Warehouse and create inventory
dimension id
           
warehouse = "11";
           
//Give CustAccount as per your scenario
           
//It is mandatory to create sales order
           
custAccount = "US-001";
           
if(CustTable::find(custAccount))
           
{
               
salesTable.CustAccount = custAccount;
           
}
           
else
           
{
               
info(strFmt("Customer account %1 doesn't exist.",
custAccount));
           
}
           
//Initializing the sales order from customer
           
salesTable.initFromCustTable();
           
if(InventLocation::find(warehouse).InventLocationId != "")
           
{
               
salesTable.InventSiteId = InventLocation::find(warehouse).InventSiteId;
               
salesTable.InventLocationId =
inventlocation::find(warehouse).InventLocationId;
           
}
           
salesTable.insert();
           
try
           
{
               
inventTable.clear();
               
inventDim.clear();
        
       salesLine.clear();
               
//Give ItemId as per your scenario
               
//It is mandatory to create sales line
               
select * from inventTable
                    where inventTable.itemId ==
"A0001";
                    
                salesLine.clear();
               
salesLine.SalesId = salesID;
               
salesLine.ItemId = inventTable.ItemId;
               
salesLine.itemIdChanged();
               
//Initializing the sales line from inventory
               
salesLine.initFromInventTable(InventTable::find(salesLine.ItemId));
               
//Setting and creating inventory dimensions
               
//I have given the warehouse in 
               
if(Inventlocation::find(warehouse).InventLocationId != "")
                {
                    inventdim.InventSiteId =
InventLocation::find(warehouse).InventSiteId;
                    inventdim.InventLocationId
= Inventlocation::find(warehouse).InventLocationId;
               
}
               
salesLine.InventDimId = InventdIm::findOrCreate(inventDim).inventDimId;
               
salesLine.createLine(NoYes::Yes, // Validate
               
NoYes::Yes, // initFromSalesTable
               
NoYes::No, // initFromInventTable
               
NoYes::Yes, // calcInventQty
               
NoYes::Yes, // searchMarkup
               
NoYes::Yes); //
               
//Set the values as per your scenario
               
salesLine.SalesPrice = 250;
               
salesLine.SalesQty = 3;
               
salesLine.LineDisc = 10;
               
salesLine.LineAmount= salesLine.calcLineAmount();
               
salesLine.update();
               
ttscommit;
           
}
           
catch(Exception::Error)
           
{
               
ttsabort;
           
}
                //
                //
Confirmation
                salesFormLetter =
SalesFormLetter::construct(DocumentStatus::Confirmation);
                salesFormLetter.update(SalesTable::find(salesID));
                info(strFmt("Sales %1
created and confirmed.", salesTable.SalesId));
              //Packing slip
               salesFormLetter=SalesFormLetter::construct(DocumentStatus::PackingSlip);
               salesFormLetter.update(SalesTable::find(salesID));                                salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::All,AccountOrder::Non
e, NoYes::No,NoYes::Yes);
                info("Sales Order
Status is Delivered");
           
    //Invoicing the sales
order
                 SalesFormLetter
formLetterObj;
                 formLetterObj =
SalesFormLetter::construct(DocumentStatus::Invoice);
                 formLetterObj.update(SalesTable::find(salesID));
        
   
               info(strFmt("Sales
order created with Sales ID: %1",salesID));
       
}
       
catch(Exception::Error)
       
{
           
ttsabort;
       
}
    }
}
 
Comments
Post a Comment