IMPORTANT ANNOUNCEMENT

These forums were permanently set to read-only mode on July 20, 2022. From that day onwards, no new posting or comment is allowed on the site, but the historical content remains intact and searchable.

A new location for posting questions about PlanetPress Suite is now available:

OL Learn - PlanetPress Classic (opens in new tab)

Page 1 of 2 1 2 >
Topic Options
#57307 - 11/18/19 11:00 AM Calculate total of calculated Extended Prices
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
Need help please. On my invoice form I have calculated extended prices for each line item. No problem with that. I created this:

=curtostr(strtocur(strip('*', XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/ShippedQuantity[1]'))) * strtocur(trim(XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/UnitPrice[1]'))))

I placed it in my text box as a custom data selection in a local variable named 'DETAIL_EXTENTEDPRICE'

Now I need to calculate the total of all iterations to put in a subtotal on the invoice. Can the guru's here please offer advice on getting the totals?

Top
#57308 - 11/18/19 11:16 AM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
If you wan this to show in the same Text Object, you could run this in the PressTalk Before of the Text Object and store the result in a global variable.

Simply do a for loop of your XML nodes and increment the global variable of the result.
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57309 - 11/18/19 12:12 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
Seems like there would be a simple way to calculate the total of the iterations in the local variable?

'DETAIL_EXTENTEDPRICE' is the local variable the iterations are printing based on the calculation.

Would that be a way?

Top
#57310 - 11/18/19 12:24 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
When writing code in a local variable inside a Text object, the result will be shown. It is not its own PressTalk Editor.

What you want, to be able to display and addition to total at the same time, would need to be done in a different way.

You would need to execute a Press Talk object which would call your Text object for 1 line at the time, while giving it the node number which to display. At the same time, your PressTalk object would add the value to the total variable.

It is simpler the way I explained in the first place. Also know that the PressTalk language hasn't been created to handle mathematical calculation. It can do it but if your start playing with value that requires rounding, you might not get the proper decimal amount that you want.
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57311 - 11/18/19 12:36 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
Yes sir. I know that presstalk is not really something designed to do this type of calculations reliably. I attempted to explain that to the customer that the calculations need to be done upstream and not done directly on the form , but, i'm attempting to do it for them. My druthers would be to do it up in the SQL and back to the system of record for the error handling. I called it out to the client but in the meanwhile, i'm working on this problem just in case they insist on the calculations happening on the form.

Also, the total does not show in the same text object. It shows in another text object on the footer. Would that change the method?


Edited by Stephenlnoe (11/18/19 12:37 PM)

Top
#57312 - 11/18/19 12:51 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
Nope...save the total into a global variable and then display it wherever you want.

As far as reliability goes, addition, subtraction and multiplication, are all right. It is division with floating point value that might cause a problem as the rounding in Postscript, which is the underlying language into which PressTalk is converted, isn't a financial programming language.
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57313 - 11/18/19 05:02 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
I tried and came up with this:

&INVOICE_TOTAL:=&INVOICE_TOTAL+curtostr(strtocur(strip('*', XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/ShippedQuantity[1]'))) * strtocur(trim(XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/UnitPrice[1]'))))

I set the global variable as String, no dice, I set the global variable to currency, no dice. I set the global variable to integer, no dice.

Top
#57314 - 11/19/19 08:11 AM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
How about that?

&INVOICE_TOTAL:=&INVOICE_TOTAL+strtocur(strip('*', XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/ShippedQuantity[1]'))) * strtocur(trim(XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/UnitPrice[1]')))

and set your global variable as a measure.

If you leave it as a string, it will concatenate the string version of the amount and not add the amount.
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57315 - 11/19/19 09:00 AM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
jim3108 Offline
OL Expert

Registered: 04/19/10
Posts: 316
Loc: London, UK
You're concatenating a string there rather than adding numbers together.
I would also do this in steps for legibility reasons if nothing else.

Set a global variable as Jean-Cédric suggests. Let's assume that is your variable &INVOICE_TOTAL and presuppose it is type currency.
Code:
define(&ShippedQty, currency, strtocur(XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/ShippedQuantity[1]')))
define(&UnitPrice, currency, strtocur(XmlGet('/Document[1]/Page[1]/SalesOrderLines[1]/SalesOrderLine['+inttostr(&currentiteration)+']/UnitPrice[1]')))
&INVOICE_TOTAL := &INVOICE_TOTAL + (&ShippedQty * &UnitPrice)

NB: I wasn't sure on the relevant of stripping out the "*" from the example you give above so I have removed this step.

Place this code in the Text Object in PressTalk Before (as advised above).

Hope this helps.

Regards,

James.

Top
#57324 - 11/25/19 09:40 AM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
Getting closer. I placed the code provided by James (thank you) in the press talk before and it gave me a result in the preview, however when I go to compile the form I get two errors.

"compilation error:PTK0038: Unknown integer variable "currentiteration"

&

"Run error: PTK 0083:[FOOTER_TOTALS] is empty: possible conversion error"

FOOTER_TOTALS is the text box where I put the press talk before and where the total would be inserted.

On the code James provided I'm wondering about the QTY part. Don't know if it makes a difference but it is not currency (with $.00). It is more of an integer (whole number).

Top
#57327 - 11/26/19 07:00 AM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
jim3108 Offline
OL Expert

Registered: 04/19/10
Posts: 316
Loc: London, UK
That shouldn't make a difference as a whole number such as "1" would just be "1.00" in currency format.

The issue appears to be the use of &currentiteration and it not being in scope. This will happen when you don't have a repeat set in the object. Can you confirm that you are doing a line repeat in this Text Object?

Regards,

James.

Top
#57328 - 11/26/19 12:42 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
Thank you James.
Yes, I am doing line repeat to fill in the table detail for 'extended price' I had to manually cycle the iterations because I use a custom data selection. I have to use the custom data selection to format the 'unit price' strtocur and to do the calculation of unit price * qty. Because the iterations do not cycle when using a custom data selection I had to manually increment. This all works perfectly in the table detail.

Now, in the footer detail for 'subtotal' I need the sum totals of all the extended cost line items(which were calculated in the table detail). This could be a separate calculation.

In order to get the extended price I wrote this:

=curtostr(strtocur(strip('*', XmlGet('/Document[1]/Page[1]/CommercialInvoice[1]/CILines[1]/CILine['+inttostr(&currentiteration)+']/ShippedQty[1]'))) * strtocur(trim(XmlGet('/Document[1]/Page[1]/CommercialInvoice[1]/CILines[1]/CILine['+inttostr(&currentiteration)+']/UnitPrice[1]'))))

This formula works for each iteration and cycles correctly for the invoice table inside of the textbox which I'm using line repeat in.

The footer where the 'subtotal' value needs to be is not in that text box where I'm doing line repeat. It is it's own text box.

Top
#57329 - 11/26/19 12:47 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
Then you have to define your own iterator variable, inside your loop, and it should work.
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57330 - 11/26/19 12:48 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
OK, I see where I went wrong.

I needed to add the code to the table detail text box which is doing the line repeat and add the presstalk after to set it into the global variable. Then use the global variable in the footer.

TY!

Top
#57331 - 11/26/19 01:16 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
The only thing left is to add the commas to the amounts because the values are sometimes millions.

I do not see a reference to that type of formatting in my manual. Is this something simple or does it require a plugin? or?

Top
#57332 - 11/26/19 01:22 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
You need to code it yourself. It shouldn't be to complicated.
I would setup a function which receives the current amount (in string format) and returns the formatted string, with commas.

Simply loop through all characters backward, starting at the first one before the decimal "dot".

Every third character, you add a comma.
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57333 - 11/26/19 03:02 PM Re: Calculate total of calculated Extended Prices [Re: Stephenlnoe]
Stephenlnoe Offline
OL Newbie

Registered: 11/07/19
Posts: 20
OK, got it. ty

Top
Page 1 of 2 1 2 >