VAT and markup script

I’m trying to make a script that will allow me to enter a cost, apply a VAT percentage to it, apply a mark-up to this total and then show the final grand total.

Essentially the sum that I’m trying to do is this:

Cost + VAT¹ + Mark-up cost² = Total

1 - To be entered by the user
2 - To be entered by the user

Here’s the script that I’ve written so far:

set r to return as text
display dialog "VAT + Mark-up Costing Tool" & r & r & "A small Applescript to help calculate the total print production costs chargeable to the client." & r & r & "© 2011 Creative19" with icon note buttons {"Let's Go To Work", "Not now"} default button {"Let's Go To Work"}
set opr to the button returned of the result
if opr is "Let's Go To Work" then
	display dialog "Production Costs" & r & r & "Enter the total print and finishing" & r & "production costs (Excluding any VAT)" default answer "" buttons {"Next Step", "Cancel"} default button {"Next Step"} --asks for base production cost
	set num1 to the text returned of the result
	display dialog "Value Added Tax" & r & r & "Enter the current UK VAT rate as a % figure" default answer "" buttons {"Next Step", "Cancel"} default button {"Next Step"} --asks for current rate of VAT
	set num2 to the text returned of the result
	display dialog "Creative19 Mark-up" & r & r & "Enter the markup to the client as a % figure" default answer "" buttons {"Calculate It", "Cancel"} default button {"Calculate It"} --asks for mark-up percentage
	set num3 to the text returned of the result
	display dialog "Total Cost to Client: £" & num1 * num2 * num3 buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer
end if

The problem is that the math isn’t currently working.

What should be happening is that the first number, eg: 100 (entered into dialog box 2) should then have a percentage of VAT added (eg: 20 for 20% - added into dialog box 3) followed by a markup of an amount entered into dialog box 4, (eg: 10 for 10%) which should lead the final dialog box to display 132 instead it’s displaying 20000.

I’m dyslexic so math was never my strong suite, but I’m stumped as to how to fix this, (although I bet it’s really simple!).

Can anyone help please?

Hi,

you’re probably looking for this.
The code is a bit schematic to make clear the steps


set r to return as text
display dialog "VAT + Mark-up Costing Tool" & r & r & "A small Applescript to help calculate the total print production costs chargeable to the client." & r & r & "© 2011 Creative19" with icon note buttons {"Let's Go To Work", "Not now"} default button {"Let's Go To Work"}
set opr to the button returned of the result
if opr is "Let's Go To Work" then
	display dialog "Production Costs" & r & r & "Enter the total print and finishing" & r & "production costs (Excluding any VAT)" default answer "" buttons {"Next Step", "Cancel"} default button {"Next Step"} --asks for base production cost
	set num1 to the text returned of the result
	display dialog "Value Added Tax" & r & r & "Enter the current UK VAT rate as a % figure" default answer "" buttons {"Next Step", "Cancel"} default button {"Next Step"} --asks for current rate of VAT
	set num2 to the text returned of the result
	display dialog "Creative19 Mark-up" & r & r & "Enter the markup to the client as a % figure" default answer "" buttons {"Calculate It", "Cancel"} default button {"Calculate It"} --asks for mark-up percentage
	set num3 to the text returned of the result
	set CostsPlusValueAddedTax to num1 + (num1 / 100 * num2)
	set totalCosts to CostsPlusValueAddedTax + (CostsPlusValueAddedTax / 100 * num3)
	display dialog "Total Cost to Client: £" & totalCosts buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer
end if


To add n% to a figure, you multiply it by one hundred and n and divide the result by 100.

display dialog "Total Cost to Client: £" & num1 * (100 + num2) / 100 * (100 + num3) / 100 buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer

-- Or:
display dialog "Total Cost to Client: £" & num1 * (100 + num2) * (100 + num3) / 10000 buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer

PS. I think Stefan’s script does the same thing, but working the figures in a different order.

Brilliant stuff Stefan and thanks also to you Neil.

I’ve just tested the script that Stefan wrote and it works fine.

One last question, is there a way of making sure that the final answer is limited to two decimal places?

Nigel. :wink:

set r to return as text
display dialog "VAT + Mark-up Costing Tool" & r & r & "A small Applescript to help calculate the total print production costs chargeable to the client." & r & r & "© 2011 Creative19" with icon note buttons {"Let's Go To Work", "Not now"} default button {"Let's Go To Work"}
set opr to the button returned of the result
if opr is "Let's Go To Work" then
	display dialog "Production Costs" & r & r & "Enter the total print and finishing" & r & "production costs (Excluding any VAT)" default answer "" buttons {"Next Step", "Cancel"} default button {"Next Step"} --asks for base production cost
	set num1 to the text returned of the result
	display dialog "Value Added Tax" & r & r & "Enter the current UK VAT rate as a % figure" default answer "" buttons {"Next Step", "Cancel"} default button {"Next Step"} --asks for current rate of VAT
	set num2 to the text returned of the result
	display dialog "Creative19 Mark-up" & r & r & "Enter the markup to the client as a % figure" default answer "" buttons {"Calculate It", "Cancel"} default button {"Calculate It"} --asks for mark-up percentage
	set num3 to the text returned of the result

	-- Calculate the final result and multiply it by 100.
	-- (num1 * (100 + num2) / 100 * (100 + num3) / 100) * 100 = num1 * (100 + num2) * (100 + num3) / 100
	-- Round to the nearest integer (.5 away from 0)
	-- Get the result div 100 as text & "." & the last two digits as text
	tell num1 * (100 + num2) * (100 + num3) / 100 to ¬
		tell (it div 0.5 - it div 1) to ¬
			set totalCost to "Total Cost to Client: £" & it div 100 & "." & text 2 thru 3 of (100 + it mod 100 as text)
	display dialog totalCost buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer
end if

Oh God! I didn’t realise I’d got your name wrong.

Doh!

Can I blame this on my dyslexia? (and a night of interupted sleep courtesy of a small child?).

Either way thanks for the revision!

:slight_smile:

Nigel…

I don’t suppose you’d know the bit of code I’d need to make the resullt also copy to the clipboard as you press the Calculate It button would you?

If you want exactly the displayed string, add the line

set the clipboard to totalCost

before the last display dialog line.

If you want only the value, replace the part after the 4 comment lines with


.
	tell num1 * (100 + num2) * (100 + num3) / 100 to ¬
		tell (it div 0.5 - it div 1) to ¬
			tell it div 100 & "." & text 2 thru 3 of (100 + it mod 100 as text) to set {totalCostString, totalCost} to {"Total Cost to Client: £" & it, "" & it}
	set the clipboard to totalCost
	display dialog totalCostString buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer
end if

Just a bit playing with Nigel’s amazing “tell chain” :wink:

Brilliant! Thanks so much.

Yes of course you can. :slight_smile: And sorry for this slow response. My own distraction is caring for an elderly mother.

Phew! It makes me feel a bit dyslectic myself! :wink:

You may recall that (100 + num2) is notionally divided by 100 too, but that I cancelled out the division against the multiplication of the end result by 100 prior to rounding. It turns out that (100 + num3)'s division by 100 can be included in the rounding by using larger divisors for that, giving the slightly simpler but even more opaque:


.
	tell num1 * (100 + num2) * (100 + num3) to ¬
		tell (it div 50 - it div 100) to ¬
			tell it div 100 & "." & text 2 thru 3 of (100 + it mod 100 as text) to set {totalCostString, totalCost} to {"Total Cost to Client: £" & it, "" & it}
	set the clipboard to totalCost
	display dialog totalCostString buttons {"Thanks!"} default button {"Thanks!"} --displays the equation and calculates the answer
end if

Having blindly copied the last comment in the script a couple of times, I’ve just noticed it’s wrong!