Tuesday, May 17, 2016

How to Prevent Negative Inventory - Versions Before 2013 R2

In the Microsoft Dynamics NAV Community Forum many users post questions about how to prevent negative inventory. These questions are related with all the product versions in Dynamics NAV. This blog post will contain the information about "How to Prevent Negative Inventory" in Dynamics NAV. 

In the earlier versions (Before Microsoft Dynamics NAV 2013 R2) there were no option to prevent negative inventory at the time of sales. In earlier versions of Dynamics NAV block the transactions which match the below condition (Code Unit : Item Jnl.-Post Line ). 
 InsertItemLedgEntry(VAR ItemLedgEntry : Record "Item Ledger Entry";TransferItem : Boolean)  
 WITH ItemJnlLine DO BEGIN  
  IF ItemLedgEntry.Open THEN BEGIN  
   IF (((ItemLedgEntry."Entry Type" IN  
    [ItemLedgEntry."Entry Type"::"Negative Adjmt.",  
    ItemLedgEntry."Entry Type"::Consumption]) AND  
    ("Source Type" = "Source Type"::Item)) OR  
    (ItemLedgEntry."Entry Type" = ItemLedgEntry."Entry Type"::Transfer)) AND  
    (ItemLedgEntry.Quantity < 0)  
   THEN  
    ERROR(Text005,ItemLedgEntry."Item No."); 
That means all the sales related transactions are allows so the users can sell the goods to a client even if the stocks are not available in the system inventory. This sometimes create lot of inventory related issues in the company. Therefore many companies request partners to block the negative inventory in their system. 

How to do it in earlier versions :
Original Code :
 InsertItemLedgEntry(VAR ItemLedgEntry : Record "Item Ledger Entry";TransferItem : Boolean)  
 WITH ItemJnlLine DO BEGIN  
  IF ItemLedgEntry.Open THEN BEGIN  
   IF (((ItemLedgEntry."Entry Type" IN  
    [ItemLedgEntry."Entry Type"::"Negative Adjmt.",  
    ItemLedgEntry."Entry Type"::Consumption]) AND  
    ("Source Type" = "Source Type"::Item)) OR  
    (ItemLedgEntry."Entry Type" = ItemLedgEntry."Entry Type"::Transfer)) AND  
    (ItemLedgEntry.Quantity < 0)  
   THEN  
    ERROR(Text005,ItemLedgEntry."Item No.");  

Modification Required:
You required to add below lines of code to the "InsertItemLedgEntry" function in the "Item Jnl.-Post Line" Code Unit. 
// TC 02.201.2016
   IF (ItemLedgEntry.Quantity < 0) THEN  
    ERROR(Text005,ItemLedgEntry."Item No.");  
// TC 02.201.2016 End

Once the code is been added, function will be look like below
 InsertItemLedgEntry(VAR ItemLedgEntry : Record "Item Ledger Entry";TransferItem : Boolean)   
  WITH ItemJnlLine DO BEGIN   
  IF ItemLedgEntry.Open THEN BEGIN   
   IF (((ItemLedgEntry."Entry Type" IN   
   [ItemLedgEntry."Entry Type"::"Negative Adjmt.",   
   ItemLedgEntry."Entry Type"::Consumption]) AND   
   ("Source Type" = "Source Type"::Item)) OR   
   (ItemLedgEntry."Entry Type" = ItemLedgEntry."Entry Type"::Transfer)) AND   
   (ItemLedgEntry.Quantity < 0)   
   THEN   
   ERROR(Text005,ItemLedgEntry."Item No.");  
// TC 02.201.2016
   IF (ItemLedgEntry.Quantity < 0) THEN  
    ERROR(Text005,ItemLedgEntry."Item No.");  
// TC 02.201.2016 End

With the above two lines added to the existing code, Dynamics NAV will provide the required validation to prevent negative inventory. 
If you required to give a more dynamics control to the users, you can define a field in the Inventory Setup Table and  from that you can write the validation. With that your system administrator can change the condition dynamically. 

Result:

In my next blog post I will describe on how to do the prevent negative inventory in the Dynamics NAV 2013 R2, 2015 and 2016 versions. 

Please provide your feedback with a comment. 
Thank you and Regards,
Tharanga Chandrasekara

No comments:

Post a Comment