Showing posts with label Create SALE ORDER from code in dynamics ax. Show all posts
Showing posts with label Create SALE ORDER from code in dynamics ax. Show all posts

Saturday, 7 July 2012

SALE ORDER from X++


Create SALE ORDER from code in dynamics ax



static void createSalesTable(CustAccount _custAccount) {
SalesTable salesTable;
NumberSeq NumberSeq;
;
NumberSeq = NumberSeq::newGetNumFromCode(SalesParameters::numRefSalesId().numberSequence);
salesTable.SalesId = NumberSeq.num();
salesTable.initValue();
salesTable.CustAccount = _custAccount;
salesTable.initFromCustTable();
salesTable.insert();
}

static void createSalesLine(SalesId _salesId, ItemId _itemId) {
SalesLine salesLine;
;
salesLine.clear();
salesLine.SalesId = _salesId;
salesLine.ItemId = _itemId;
salesLine.createLine(NoYes::Yes, // Validate
NoYes::Yes, // initFromSalesTable
NoYes::Yes, // initFromInventTable
NoYes::Yes, // calcInventQty
NoYes::Yes, // searchMarkup
NoYes::Yes); // searchPrice
}


Posting Invoice from Sales order


The FormLetter classes define how to post different status updates against sales orders and purchase orders. When a posting is being performed is that the parm tables are Populated with data, and then the update itself is performed.

Post the Invoice from sales order

static void postSalesInvoice(Args _args)
{
// Define a classvariable according to the
// type of posting being performed
SalesFormLetter_Invoice invoice;
SalesTable salesTable;
;
// Select the salesTable to update
salesTable = SalesTable::find(“CEU-545352″);
// Create a new object of the SalesFormLetter_Invoice
// by using the construct-method in SalesFormLetter
invoice = SalesFormLetter::construct(DocumentStatus::Invoice);
// Post the invoice
invoice.update(salesTable,
SystemDateGet(),
SalesUpdate::All,
AccountOrder::None,
false,
true); // Set to true to print the invoice
}
 

 The below picture shows the class hierarchy of FormLetter with all its subclasses: we can easily custamize the above logic to post different status updates against sales orders.

Sales (Confirmation, Picking list, Packing slip, Invoice )




Print Performa Sales Invoice



static void Test_SalesFormLetter(Args _args)
{
    SalesFormLetter letter=SalesFormLetter::construct(DocumentStatus::Invoice);
    SalesTable sale = SalesTable::find('ORDR00000229');
;
    ttsBegin;
    letter.update(sale, systemDateGet(), SalesUpdate::PickingList, AccountOrder::None, 
true, true);
    ttsCommit;
}


Print Sales Invoice


public void printInvoiceReport(PurchId _purchId)
{
    ReportRun report;
    RecordSortedList List = new RecordSortedList(tableNum(VendInvoiceJour));
 
    VendInvoiceJour VendInvoiceJour = VendInvoiceJour::findFromPurchId(_purchId);
    PurchFormLetter PurchFormLetter;
    ;
 
    if (VendInvoiceJour.RecId)
    {
 
        report = new ReportRun(new Args(ReportStr(PurchInvoice)));
 
        List.ins(VendInvoiceJour);
        report.args().object(List);
        report.query().interactive(false);
        report.report().interactive(false);
        report.args().parmEnum(0);
        report.args().parmEnumType(920);
 
        report.args().name("KeepSettings");
        report.args().caller(PurchFormLetter);
        report.setTarget(PrintMedium::Screen);
        report.printJobSettings().setTarget(PrintMedium::Screen);
        report.printJobSettings().preferredTarget(PrintMedium::Screen);
 
        PurchFormLetter     =   PurchFormLetter::construct(DocumentStatus::Invoice);
        PurchFormLetter.updatePrinterSettingsFormLetter(
  report.printJobSettings().packPrintJobSettings(), 
  PrintSetupOriginalCopy::Original);
        PurchFormLetter.updatePrinterSettingsFormLetter(
  report.printJobSettings().packPrintJobSettings(), 
  PrintSetupOriginalCopy::Copy);
 
        // print invoice
        VendInvoiceJour.printJournal(PurchFormLetter);
    }
}

Create return Order from code


 static void SR_CreateReturnOrderAfterInvoice(Args _args)
 {
 CustInvoiceJour _invoiceRec;
 str _returnReason;
 CustInvoiceTrans custInvoiceTrans;
 SalesLine salesLine;
 SalesTable newRetOrder;
 CustInvoiceJour custInvoiceJour;

SalesTable createReturnOrderHeader(CustInvoiceJour invoiceRec)
 {

SalesTable old, newRec;
 boolean bChecksOk = true;
 ;

old = SalesTable::find(invoiceRec.SalesId);
 newRec.initReturnFromSalesTable(old);
 newRec.CustAccount = old.CustAccount;

newRec.initFromCustTable();

newRec.CustInvoiceId = invoiceRec.InvoiceId;
 newRec.ReturnDeadline = today();
 newRec.ReturnReasonCodeId = ’21′; // Defective
 newRec.SalesType = SalesType::ReturnItem;
 newRec.SalesTaker = SysCompanyUserInfo::current().EmplId;

if ( newRec.ReturnReasonCodeId == ” && CustParameters::find().ReturnOrdersReasonReq ||
 newRec.ReturnReasonCodeId != ” && !ReturnReasonCode::exist(newRec.ReturnReasonCodeId) )
 {
 checkFailed(strfmt(“@SYS26332″, fieldid2pname(tablenum(SalesTable), fieldnum(SalesTable, ReturnReasonCodeId))));
 bChecksOk = false;
 }

if ( bChecksOk && newRec.validateWrite())
 {
 newRec.insert();
 }
 else
 {
 throw error(“@SYS18722″);
 }

return newRec;
 }

ttsbegin;

// first we need to create the sales order header for the return order
 select custInvoiceJour where custInvoiceJour.RefNum == RefNum::SalesOrder && custInvoiceJour.InvoiceId == ’101231′;

newRetOrder = createReturnOrderHeader(custInvoiceJour);

while select * from custInvoiceTrans where custInvoiceTrans.SalesId == custInvoiceJour.SalesId
 && custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
 && custInvoiceTrans.InvoiceDate == custInvoiceJour.InvoiceDate
 && custInvoiceTrans.numberSequenceGroup == custInvoiceJour.numberSequenceGroup
 {
 // now we need to populate all the necessary fields for the new salesline
 // using the existing invoice and the new sales order
 salesLine.initFromCustInvoiceTrans(custInvoiceTrans);
 salesLine.initFromSalesTable(newRetOrder);

// udpate the quantity
 salesLine.ExpectedRetQty = -custInvoiceTrans.Qty;

if (salesLine.ExpectedRetQty > 0)
 {
 error(“@SYS53512″);
 ttsabort;
 }

// set the quantity and amount fields
 salesLine.LineAmount = salesLine.returnLineAmount();
 salesLine.SalesQty = 0;
 salesLine.InventTransIdReturn = custInvoiceTrans.InventTransId;

//create the line
 salesLine.createLine(true, false, false, false, false, false, false, false, salesLine.InventTransId);

// clear the buffer
 salesLine.clear();
 }

ttscommit;

info(strfmt(‘Newly created return order is %1′, newRetOrder.SalesId));

}

Referred from : http://msdax.wordpress.com/