Block the posting if one line has negative inventory

poppinspoppins Member Posts: 647
edited 2014-03-27 in NAV Three Tier
Hi everyone,
I want to do some control in the sales orders posting.
Let's say I have an order with 5 lines, and the 3rd line presents a negative inventory.
I want to be able to block the posting of the whole sales order (no shipment header or lines are generated).
How can I do that?
I tried to do some workaround in CU 22 in the function InsertItemLedgEntry
.
.
.
//this is standard code
  IF "Job No." <> '' THEN BEGIN
    ItemLedgEntry."Job No." := "Job No.";
    ItemLedgEntry."Job Task No." := "Job Task No.";
  END;
//my code starts here
   IF ((ItemLedgEntry."Entry Type" IN
        [ItemLedgEntry."Entry Type"::"Negative Adjmt.",
        ItemLedgEntry."Entry Type"::Sale,ItemLedgEntry."Entry Type"::Purchase
        ]) AND (ItemLedgEntry.Quantity <0) )THEN
      BEGIN
        lItem.GET(ItemLedgEntry."Item No.");
        lItem.SETFILTER("Location Filter",ItemLedgEntry."Location Code");
        lItem.CALCFIELDS(Inventory);     
      END;
      IF (lItem.Inventory < ABS(ItemLedgEntry.Quantity)) THEN
            ERROR(Error0001, lItem."No.",ABS(ItemLedgEntry.Quantity),lItem.Inventory);
    END;
//my code ends here
//this is standard code
  ItemLedgEntry.INSERT;
  InsertItemReg(ItemLedgEntry."Entry No.",0,0,0);
END;
With this code, I get the error message and the posting stops, but when I look at table 32 (Item Ledger entry), I find that the two lines (with no negative inventory) have been posted.
How shall I do to prevent the posting of the whole order if there is one or more lines with negative inventory?
Thanks in advance :)

Comments

  • jglathejglathe Member Posts: 639
    Hi,
    poppins wrote:
    How shall I do to prevent the posting of the whole order if there is one or more lines with negative inventory?
    Thanks in advance :)

    my change is in the same function:
    InsertItemLedgEntry(VAR ItemLedgEntry : Record "Item Ledger Entry";TransferItem : Boolean)
    
    WITH ItemJnlLine DO BEGIN
      IF ItemLedgEntry.Open THEN BEGIN
        // mod starts here - extra bracket
        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)) OR
          InvtSetup.PreventNegativeInventory) AND
        // end of mod
          (ItemLedgEntry.Quantity < 0)
        THEN
          ERROR(Text005,ItemLedgEntry."Item No.");
    

    The trick is not to check on negative inventory for the item, but to use the code that's already there:

    - ILE is still open after application
    - Quantity of ILE is negative
    ----> can only be negative stock.

    Now force the exceptions which standard NAV allows to be disabled, and you're done.

    with best regards

    Jens
  • poppinspoppins Member Posts: 647
    jglathe wrote:
    Hi,
    poppins wrote:
    How shall I do to prevent the posting of the whole order if there is one or more lines with negative inventory?
    Thanks in advance :)

    my change is in the same function:
    InsertItemLedgEntry(VAR ItemLedgEntry : Record "Item Ledger Entry";TransferItem : Boolean)
    
    WITH ItemJnlLine DO BEGIN
      IF ItemLedgEntry.Open THEN BEGIN
        // mod starts here - extra bracket
        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)) OR
          InvtSetup.PreventNegativeInventory) AND
        // end of mod
          (ItemLedgEntry.Quantity < 0)
        THEN
          ERROR(Text005,ItemLedgEntry."Item No.");
    

    The trick is not to check on negative inventory for the item, but to use the code that's already there:

    - ILE is still open after application
    - Quantity of ILE is negative
    ----> can only be negative stock.

    Now force the exceptions which standard NAV allows to be disabled, and you're done.

    with best regards

    Jens
    Thanks for the quick answer :mrgreen:
    My question is: if in my order, the first two lines are ok (positive stock), and the 3rd line has negative stock, does your "trick" prevent the insertion of the first two lines??? I don't think so, but correct me if I am wrong....
    My issue is to prevent the insertion of all the lines if there is some lines with negative stock...
  • jglathejglathe Member Posts: 639
    Hi,
    poppins wrote:
    My question is: if in my order, the first two lines are ok (positive stock), and the 3rd line has negative stock, does your "trick" prevent the insertion of the first two lines??? I don't think so, but correct me if I am wrong....
    My issue is to prevent the insertion of all the lines if there is some lines with negative stock...
    It does. "Error" in a posting codeunit leads to the roll-back of the whole transaction so far, unless there are additional COMMIT commands in CU22 (in your example) which must not be there anyway. Try it out in a standard CRONUS database, it works this way.

    with best regards

    Jens
  • David_SingletonDavid_Singleton Member Posts: 5,479
    jglathe wrote:
    ... unless there are additional COMMIT commands in CU22 (in your example) which must not be there anyway.

    :thumbsup:
    David Singleton
  • lubostlubost Member Posts: 625
    Hi,

    I think that testing actual inventory is not enough to prevent negative inventory. In standard NAV it is possible to post entries to the past and these entries can lead to negative inventory in the entries tree. So, you have to test negative inventory for each already posted entries with posting date after actual entry.
  • jglathejglathe Member Posts: 639
    Hi
    lubost wrote:
    Hi,
    In standard NAV it is possible to post entries to the past and these entries can lead to negative inventory in the entries tree.
    No they don't. :mrgreen: Of course they do when you accumulate the quantities by time, but not for the item application logic. The reason for it is an oddity: You can post a negative adjustment in the past which will get an application with a positive adjustment from the future if nothing else is available. You could view this as a bug (I consider it as one). To avoid this as well you need to change the application logic to only consider older open entries than the one you're actually posting / applying to. But beware: There are cases where this restriction will get you into trouble (one is the ominous "RedoApplications" when you apply against a specific entry), at least when you don't start operation with it.

    with best regards

    Jens
Sign In or Register to comment.