Monday, 13 November 2023

A better way of calculating the dollar value of sales invoices

I wrote the other day about code to calculate the dollar value of sales invoices. I had written code that iterated by means of a cursor over the lines of an invoice and calculated the dollar value of each line, then summing these values. I was sure that there was a better way of doing this but I hadn't managed to find this better way. While I was describing the problem, I saw the quick way of getting the dollar value, using values in the connected transaction. This is yet another case of rubber duck debugging: describing the problem in words leads to a solution. Here is the code that is stored as a trigger in the 'func' form.

/*********************************************************** TEST_CNVINV2DOLLAR - Convert an invoice's total to dollars 11/11/23: New, much simpler version that doesn't require a cursor and so can be used multiple times in a container procedure. SUB 6200 - Input: :IV Output: :QP ************************************************************/ SUB 6200; :VAT62 = :SUM62 = :EXCHANGE62 = :QP = 0.0; :TMP62 = 0E9; :DEBIT62 = '\0'; SELECT INVOICES.VAT, INVOICES.DEBIT, FNCTRANS.SUM2, FNCTRANS.EXCHANGE2 INTO :VAT62, :DEBIT62, :SUM62, :EXCHANGE62 FROM INVOICES, FNCTRANS WHERE INVOICES.FNCTRANS = FNCTRANS.FNCTRANS AND INVOICES.IV = :IV; :TMP62 = :VAT62 / :EXCHANGE62; :QP = (:DEBIT62 = 'C' ? -1 : 1) * (:SUM62 - :TMP62); RETURN;

Short and simple.

No comments:

Post a Comment