Sage 100

 View Only
  • 1.  Invoice Out of Balance caused by a Table PreWrite Invoice Header Script run from outside of manual Invoice Data Entry

    Posted 11-03-2021 17:11
    Edited by Alnoor Cassim 11-04-2021 01:02
    This is more of an FYI. I have a solution but was wondering if anyone else ran into this. Hard to describe this. 

    For v2020.2 Advanced, a portion of a Header PreWrite script conditionally inserts misc item (type 5) lines as surcharges in S/O Invoice Data Entry the charge amt. It uses InsertLine not AddLine. In manual entry it works fine. A typical invoice has 5 regular items, 4 of which are subject to surcharge and so 4 misc item lines are inserted after each regular item for a total of 9. Prior to invoicing, they use Shipping Data Entry along with Starship and scanning with Scanco WMS . However, I can dupe issue on my system v2019.4 Standard with just Shipping and Invoice Data Entry. 

    So everything was fine until they ran an existing V/I job that updates Invoice Date and Ship Date on many batches/invoices. The Data tab only has BatchNo, InvoiceNo, InvoiceDate, and ShipDate. No line fields. The import fires off the PreWrite script which is by design, to add or recalc the surcharges. This is important because it is the primary means to get the surcharges in there to begin with. Due to conflicts in Shipping (Starship, Scanco scanning, and the infernal Pkg Tracking feature which has never been improved since invented), I'm not firing off the script there. Note if it was misc charge (type 3) lines, I could definitely run in Shipping w/o issue.

    The problem is after import if you review these invoices, the Lines total is correct and you see the surcharge lines. But the Nontaxable Amt and Taxable Amt do not reflect that and stay unchanged. So it is Out of Balance (OOB). But if I make a quick change to the invoice (e.g. set the header comment) and save, it causes script to fire off again and recalc, which corrects the OOB.

    ** The problem is forever fixed by moving the script to Pre-Totals but it introduced a new problem with V/I job as since there are no line fields, it won't fire off Pre-Totals. So I added some Perform Logic on Before Write of Header to just AddLine() and Delete() so the script would fire off. Also for the 1000s of OOB invoices that were exactly off by the amount of the surcharge, I created a button script utility to find the OOBs and then send an AddLine() and Delete() so the main script would get triggered to fix those.

    Earlier this year for S/O Entry, as you know, there were critical OOBs that were occurring in v2019 - 2021 for which there was a hotfix. I did check to see if that fix (Browse Filter reset) was needed. It turns out that condition is not present but I added the fix code anyway to Inv Data Entry as a test and it made no difference. 

    Here is the snippet. It uses InsertLine with an "A" (A=after, B=before) instead of AddLine b/c I need to specifically place the misc item after the item code. AddLine always adds a new line at the bottom. I wonder if the problem doesn't occur with AddLine.

    If nAmtToAdd <> 0 Then
    	r=oScript.DebugPrint("TypeOfLine=1 so about to InsertLine for Item Code: " & sItemCode)
    	rV	= oLines.InsertLine(sLineSeqNo,"A")
    	rV	= oLines.SetValue("ItemCode$", sMiscCode)
    	rV	= oLines.SetValue("QuantityOrdered", nQO) 'To avoid hard stop of Qty Shipped exceeds Qty Ordered message
    	rV	= oLines.SetValue("QuantityShipped", nQS)
    	rV	= oLines.SetValue("QuantityBackordered", nQBO)'If nQBO>0 system will use this for ExtensionAmt calc
    	rV	= oLines.SetValue("UnitPrice", nItemSurchargeUnitAmt)
    
    	rW	= oLines.Write() : r=oScript.DebugPrint("rW after InsertLine for TypeOfLine=1 = " & rW)
    	If rW = 0 Then
    		sMsg	= "oLines.Write failed on InsertLine-A for TypeOfLine=1 with " & oLines.LastErrorMsg
    		r		= oScript.DebugPrint(sMsg)
    	End If
    End If 'check if nAmtToAdd>0
    ​


    ------------------------------
    Alnoor Cassim

    Email: alnoor@asifocus.com
    Ph: 949-689-9887
    Orange County, CA
    ------------------------------


  • 2.  RE: Invoice Out of Balance caused by a Table PreWrite Invoice Header Script run from outside of manual Invoice Data Entry

    Posted 11-04-2021 01:27
    Edited by Dan Burleson 11-04-2021 01:35
    That is always how I've seen it work; even after adding lines as with your import. I just assumed that the Table PreWrite event occurred after the document totals were calculated. Why would there even need to be a PreTotals event if the totals were calculated after a PreWrite? I assumed that the PreWrite was for non-header objects. What am I missing?

    ------------------------------
    Dan Burleson
    Software Consultant
    Connex Software
    Corvallis OR
    541-224-6642
    ------------------------------



  • 3.  RE: Invoice Out of Balance caused by a Table PreWrite Invoice Header Script run from outside of manual Invoice Data Entry

    Posted 11-04-2021 12:55
    Edited by David Speck II 11-04-2021 12:54
    If I have to recalculate totals in a header pre-write script, I call the SalesTaxRecalculation method.  I'm not sure what the difference is between it and SalesTaxCalculation but the former seems to get the job done.  Both seem to be amond the only methods I have found that will actually recalculate the header's totals from zero unlike the normal method that adds or subtracts to the existing values.  You might remember back with the issue where the browse filter wasn't being set correctly under specific circumstances and would cause the lines total to get out of sync with the header's totals and adding or deleting lines did not correct it but toggling the tax schedule did.  However, if your script is on the SO_InvoiceHeader, you need to follow either method with CheckForCreditMemo otherwise the sign of the totals will be incorrect on credit memo invoices.

    ------------------------------
    David Speck II
    Tennessee Software Solutions
    ------------------------------



  • 4.  RE: Invoice Out of Balance caused by a Table PreWrite Invoice Header Script run from outside of manual Invoice Data Entry

    Posted 11-04-2021 16:33
    David - Hey I think you nailed it! I'm just kicking myself here not having thought of that as I've used SalesTaxRecalculation in mods but didn't think of that here (damn it!) Now I'm pretty sure this is done for us  when the Pre-Write of S/O Entry runs but NOT in the Pre-Write of Invoice Data Entry (unless you're sitting in the UI at the time apparently). Running SalesTax(Re)calculation method updates the internal memory file that holds the current NontaxableAmt and TaxableAmt and this would have definitely fixed my OOB.

    Also when we click on Totals tab, you probably know that is primarily when SalesTaxCalculation runs (explaining the reliability of Pre-Totals) as well as when the Tax Schedule, Ship To Code is changed. I'm sure other events too. Good catch on using CheckForCreditMemo for credit memos.

    Thank you!


    ------------------------------
    Alnoor Cassim

    Email: alnoor@asifocus.com
    Ph: 949-689-9887
    Orange County, CA
    ------------------------------



  • 5.  RE: Invoice Out of Balance caused by a Table PreWrite Invoice Header Script run from outside of manual Invoice Data Entry

    Posted 11-06-2021 21:26
    Hi Dan - I'm pretty sure the original reason for Pre-Totals was the visual for the user to immediately see changes in the Lines panel after they clicked Totals panel, before they had to save the document. You're right that it runs after the initial doc totals are created (which is after the huge sales tax calc routine). UI or not, it's wonderful to have. In my view however, Pre-Write should recalc the doc totals. I want to say in S/O Entry that is what happens already based on so many past scripts we've all done but I'd need to test to confirm. This appears to be something unique to Invoice Data Entry.

    Note the V/I job only updates Invoice Date and Ship Date on the invoices created from Shipping, no lines. For most invoices, it's the 1st time the script is adding surcharges. However, the Ship Date itself is part of the formula (using date math) for determining the surcharge amt. Pre-Write serves as a nice V/I hook to fire off the recalc (except that Totals are OOB unless I call oBusObj.SalesTaxRecalculation()) . However Pre-Totals won't fire off from V/I unless I'm adding a line field in Data tab. I don't want to have to tell my customer they need to change their import file to include lines so I'd prefer sticking to Pre-Write. I ended up doing this simple Perform Logic on Before Write of Header of the V/I job to fire off the Pre-Totals but my view is whether I'm scripting or doing M/D or ISV work, Pre-Write should work as is:

    coHeader'Lines'AddLine(); coHeader'Lines'Delete()

    ------------------------------
    Alnoor Cassim

    Email: alnoor@asifocus.com
    Ph: 949-689-9887
    Orange County, CA
    ------------------------------



  • 6.  RE: Invoice Out of Balance caused by a Table PreWrite Invoice Header Script run from outside of manual Invoice Data Entry

    Posted 11-07-2021 00:48
    Alnoor - Thanks for circling around with the deeper dive. I see the challenge now. FWIW the lack of total calculation on an Insert works the same for me in SODE and AddLine () doesn't change things either.

    ------------------------------
    Dan Burleson
    Software Consultant
    Connex Software
    Corvallis OR
    541-224-6642
    ------------------------------