Sunday, 25 August 2013

Customize X++ Purchase Requisitions AX 2012 - Transfer Sales tax and Item Sales Tax for all Line Items

Today we are going to customize the Purchase Requisition functionality. This post is more than a little customization as it will give you a greater understanding of tables such as:
  1. SourceDocumentLine
  2. SourceDocumentHeader
  3. TaxUncommited


User Requirement

 "When a new purchase order is created from releasing a purchase requisition, all of the tax information must be transferred on to the purchase order lines"

Task

Add the sales tax and item sales tax group to each purchase requisition line from the PurchReqAddVendor form so it will be easier for the user to attach the SalesTax and Item SalesTax group to the Purchase Requsition line.

Process Flow

After the Purchase Requisition is approved it will appear in the 'Release Purchase Order Requisition List Page'

Select the Purchase Requisition lines we want to use to create a new PO and from the action pane click on the Create Purchase Order button.

The PurchReqAddVendor forms opens up. We will be doing our customizations on this form.




So right before creating the purchase order the sales tax information can be tagged and all the line items will also be updated with the sales tax information.

Step 1

Go to the PurchReqAddVendor form to find out what data source the form is using.


It is using PurchReqLine as a data source, the PurchReqLine table already contains the TaxGroup and TaxItemGroup fields. So simply add both of these fields to the forms grid.













Step 2

Declare 2 variables for the taxGroup and taxItemGroup. These variables will be used to store the information that is entered on the form. We have to store the is information because the datasource on the PurchReqAddVendor form is temporary.


 taxGroup       _taxGroup;  
 taxItemGroup   _taxItemGroup;  

Step 3

Go to the taxGroup and taxItemGroup fields in the PurchReqLine data source. Right Click and override the modified method on both fields.

Public Void modified()  
{  
   super();  
   taxGroup = PurchReqLine.taxGroup;  
}  

Public Void modified()  
{  
   super();  
   taxItemGroup = PurchReqLine.taxItemGroup;  
} 

Step 4

Update the PurchReqLine table, but we will be updating the PurchReqLine table after the Purchase Requisition has already been approved. Doing this will cause problems when confirming the resulting purchase order. The error will be caused by the source document line and source document header table.

Adding a tax item group to our requisition will create a record in in the source document line table because AX 2012 has a table that deals with all the tax that has been applied to documents but not posted. This information is stored in the TaxUncommited table as well as a line is created in the source document line table.

Explanation Source Document Header and Line

The Source Document Header table contains information related to different kinds of documents in AX 2012. The Source Document Header sets a status each record in the table.

The source document line contains related information related to the the lines of different documents in AX 2012. This table has a relation to the source document header table and also contains a similar status.

When the Purchase Requisition is created the status in the source document header and line is Draft. When the Requisition gets approved this status changes to Completed.

Updating the PurchReqLine with the taxGroup and taxItemgroup will also require us to update the status of the related record in the SourceDocumentHeader table.

The source document table line status will also have to be updated for all the records that are related to the taxUncommited table.

Finally here is the code to update the TaxGroup and TaxItemGroup.

  for (purchReqLineRecord = PurchReqLine_ds.getFirst();  
      purchReqLineRecord;  
      purchReqLineRecord = PurchReqLine_ds.getNext())  
   {  
       if (!purchReqLineRecord.VendAccount)  
       throw error("@SYS326568");  
       vendorMap.insert(PurchReqLine::findLine(purchReqLineRecord.PurchReqTable,purchReqLineRecord.LineNum).RecId,purchReqLineRecord.VendAccount);  
       ttsBegin;  
       PurchReqLIneLoc = PurchReqLine::findLine(purchReqLineRecord.PurchReqTable,purchReqLineRecord.LineNum);  
       SourceDocumentLine = SourceDocumentLine::find(PurchReqLineLOc.SourceDocumentLine);  
       SourceDocumentHeader = SourceDocumentHeader::find(SourceDocumentLine.SourceDocumentHeader);  
       
       //Purchase Requisition Line Update
       PurchReqLIneLoc.TaxGroup = taxGroup;  
       PurchReqLIneLoc.TaxItemGroup = taxItemgroup;  
       PurchReqLIneLoc.selectForUpdate(true);  
       PurchReqLIneLoc.update();      
       
       //SourceDocumentHeader Status
       SourceDocumentHeader.AccountingStatus = SourceDocumentAccountingStatus::Completed;  
       SourceDocumentHeader.update(); 
       SourceDocumentHeader.selectForUpdate(true); 
       
       //TaxUncommitted Related Lines
       insert_recordSet SourceDocumentLineLoc  
       setting AccountingStatus = SourceDocumentLineAccountingStatus::Completed  
       where SourceDocumentLineLoc.ParentSourceDocumentLine == SourceDocumentLine.RecId;  
       ttsCommit;  
   }  

3 comments:

  1. Thank you so much for your explanation. Helped a lot.

    Regards,
    Ramya

    ReplyDelete
  2. You have mention about PO confirmation error which will appear if source document status is changed. I am facing such error while confirming the PO, how to resolve that, any idea??

    ReplyDelete
  3. Nice one ! it worked for me.
    Thanks

    ReplyDelete