Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cloudwalker

macrumors newbie
Original poster
Aug 9, 2006
4
0
London, UK
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?
 
Percentages are nice, because they represent numerical factors as nice round numbers - and they do this by multiplying them by 100.

So working out 20% of something is the same as multiplying it by a factor of 0.2 (which is 20 divided by 100). You have to remember this... computers are too simple to 'understand' percentages, so you have to divide by 100 before you set out.

So Item + VAT can best be written as:

Item + (Item * (VAT/100))

Say Item is 50 and VAT is 20

50 + (50 * (20/100) )
50 + (50 * 0.2)
50 + 10
60

Does this help? (I put in some unnecessary brackets to make things clearer)


Edit:

When I say that you multiply by a factor of 0.2 to find VAT, that JUST gives you the VAT itself. The total is the original price + the VAT.

Putting the original price in the formula is the same as multiplying by 1, so:

Total = Item + VAT
Total = Item + (Item * 0.2)
and
Total = Item * 1.2

are all the same.

If you want to multiply an item price up by a lot of percentages, you must add 1 to each of the factors to get what you want.

So in your example, the multiplier for VAT is = 1 + (20/100) = 1.2
and for markup it's = 1+ (10/100) = 1.1

So in your example, Total = 100 * 1.2 * 1.1 = 132 (try it!)
 
Last edited:
Code:
num1 * num2 * num3

As firestarter says you need to condition those numbers to have them work.

It helps to give the variables meaningful names.

Code:
cost*(1+(markupRate/100))*(1+(vatRate/100))

Would do the trick if you enter the markup and vat as percentages.

You can compute them separately if you want to e.g.

Code:
markupMult=(1+(markupRate/100))
vatMult=(1+(vatRate/100))
cost*markupMult*vatMult

B
 
Thanks for the help.

Someone on another board actually tweaked my script so that it works.

In the spirit of friendship here's the final code:

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​

The one change I am looking to make is to insert some code towards the end to limit the result to two decimal places.

Any ideas?
 
All sorted now.

The crucial bit of code is this:


set num3 to the text returned of the result
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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.