Script to add CC Fee to Invoice - getting an error!

SOLVED

I know there are several posts about adding a line to an invoice to charge a fee.  I've modified a script I found either here or in a Sage scripting class, but for some reason, I can't get it to work this time.

When I put it on the Pre-Write event, it doesn't update the Invoice total after the CC Fee line is added.  When I put it on Post-Write, I get an error "' 'SY_SalesTaxClass' does not have correct permissions" and does not save the changes.  

I'm very frustrated!  I have another script that adds a line on Pre-Write, but I can't do that for this script because we need to get the totals, including the Sales Tax Amount, in order to calculate the fee.

Can anyone see what is wrong in my script, or has a suggestion to fix this?  Here it is!

CCRate= .03 : ordertotal=0 : taxable=0 : nontaxable=0 : freight=0 : discount=0

ccfee=0 : salestax=0 : found=False : sItemCode = "" : ccOrig=0

retVal = oBusObj.GetValue("FreightAmt", freight)
retVal = oBusObj.GetValue("TaxableAmt", taxable)
retVal = oBusObj.GetValue("NonTaxableAmt", nontaxable)
retVal = oBusObj.GetValue("DiscountAmt", discount)
retVal = oBusObj.GetValue("SalesTaxAmt", salestax)

msgbox "freight = " & freight
msgbox "taxable = " & taxable
msgbox "nontaxable = " & nontaxable

ordertotal = freight+taxable+nontaxable+salestax-discount

ccfee = ordertotal * CCrate

If ccfee >0 Then

'First scroll through the lines to see if the fee is already there
Set oLines = oBusObj.AsObject(oBusObj.Lines) 'get handle to invoice detail object
oLines.MoveFirst()
Do While CBool(oLines.EOF) = False
retVal = oLines.GetValue("ItemCode$", sItemCode)

If sItemCode = "/CCFEE" Then

'Fee is found, update the line
retVal = oLines.GetValue("ExtensionAmt", ccOrig)
ordertotal = ordertotal - ccOrig
msgbox "ordertotal = " & ordertotal
ccfee = ordertotal * CCrate
msgbox "cc fee " & ccfee
retVal = oLines.SetValue("ExtensionAmt", ccfee)
retWrite = oLines.Write()
found=True
If retWrite <> 0 Then
' Just fall through
Exit Do
End If 'retWrite
End If 'item code
retVal = oLines.MoveNext()
Loop
msgbox found

'Fee item is not found, add the line
If not found then
retVal = oLines.AddLine()
retVal = oLines.SetValue("ItemCode$","/CCFEE")
retVal = oLines.SetValue("ExtensionAmt", ccfee)
retVal = oLines.Write()
If retVal <>0 then
oScript.LinesAdded = 1
End If
End If 'not found

End if 'ccfee

  • 0

    Sage 100 Standard v2019.  Do I need to deactivate the procedure before I write?  I bet I do.

  • +1 in reply to hyanaga
    verified answer

    See CalculateTotals, SalesTaxRecalclation, RecalculateFreightFromSalesOrder. No need to deactivate because you can let the system write it for you as you are in a pre-write event (can work in both sales order entry and invoice data entry). Rather than update an existing line fee why not just:

    1. scan through and delete any existing fee lines
    2. recalculate sales tax
    3. if processing an invoice recalculate the freight and totals
    4. calculate your fee based on the updated totals
    5. add your fee line
    6. recalculate sales tax
    7. if processing an invoice recalculate the freight and totals

    If you ship out of state you should consider adding a UDT of states that don't allow charging a credit card fee and whether all your divisions would charge a fee. This is what has worked for my clients.

  • 0 in reply to hyanaga

    Hi !

    Can I assume this is a script attached to SO Invoice Header?  And Pre-Write is definitely where you want to do this, because we'd have to go a different route after the fact.    Has the right methods to look at.  CalculateTotals() probably being the one you need.  And as he said, you shouldn't need to deactivate because you are on the pre-write you are allowing the regular write to occur not doing our own.

    E

  • 0 in reply to connex

    I've only added / recalculated a line fee in a PreTotals event (getting amounts from a line loop), but that doesn't allow inclusion of tax amounts in the fee calculation.  This looks like an excellent technique to use when that is required.

  • +1 in reply to connex
    verified answer

    The SalesTaxRecalculation method appears to handle both recalculating taxes and recalculating the header totals.

    If this is on the pre-write event attached to SO_InvoiceHeader, after calling the final SalesTaxRecalculation method, you need to follow it up with a call to the CheckForCreditMemo method otherwise if the SalesTaxRecalculation method is called against a credit memo, it will mess up the sign of the totals and your postings will be all messed up.

  • 0 in reply to David Speck

    This did it!  I added retVal = oBusObj.SalesTaxRecalculation after the script updates/adds the CC fee, and it works perfectly on Pre-Write.  Thank you all for your input!

  • 0 in reply to hyanaga

    Hi hyanaga, happy to see that the community helped you figure out a solution!Tada

    If you have a minute, tell us about your great experience on Sage City, take this short 3 question survey

  • 0 in reply to connex

    Unfortunately, the client has now informed me that they often change the Sales Tax amount for clients who have "special arrangements" with the state.  I suggested that they set up custom Tax Codes and Tax Schedules for those clients, but that will take time.  

    In the meantime, we can't use the SalesTaxRecalculation, and CalculateTotals does nothing.  Am I using it wrong?  I just replaced oBusObj.SalesTaxRecalculation() with OBusObj.CalculateTotals, but it doesn't appear to do anything on an Invoice, and gives me an error on Sales Orders.  Any thoughts?

  • 0 in reply to hyanaga

    I'm not aware of any other method that calculates the totals.

    If you can identify these orders, then you can attempt to use the SalesTaxCalcObj object handle property to read the tax amount first so you can store it in a variable, use SalesTaxRecalculation, and the set the stored tax amount back.  Both SO_SalesOrderTaxSummary and SO_InvoiceTaxSummary have a column called Overridden$ that you might be able to use for the purpose of identifying which ones have been changed. 

    You can use the SalesTaxCalcObj's GetValue to retrieve the Overridden$ column value. but if multiple tax codes are applied, you should save off the current key, loop through and check each one, should probably use a array or dictionary to hold the key and amount, then used the saved key to return to that key.