The eccoMAGIC Forums
http://forums.eccoMAGIC.com/forum/YaBB.pl
the SLANG EXTENSION and Guest Programming >> Slang Rules Scripts, Functions, Examples and HOW TO >> LUA Rule to Calculate Folder Values with Circular Reference
http://forums.eccoMAGIC.com/forum/YaBB.pl?num=1205120808

Message started by rb on 03/09/08 at 22:46:47

Title: LUA Rule to Calculate Folder Values with Circular Reference
Post by rb on 03/09/08 at 22:46:47

The approach can be used to set up a rule that
calculates values in one folder based on values in another OR vice versa!

Download the attached Ecco file, to explore!

For example, we may have the simple relation:
Cost=Rate*Hrs where Cost is in one folder and Hrs is in another.
If we change Cost, we want EccoExt to calculate the resulting Hrs
(=Cost/Rate)
If we change Hrs, we want EccoExt to calculate the resulting
Cost(=Rate*Hrs)

I setup 4 number folders:
Cost, Hrs, PrCost, PrHrs

PrCost & PrHrs are dummy folders that holds the previous values (only used
for the LUA Rule to work). The LUA script is then copied into any folder as an "Auto Assign Rule"
(e.g. Folder named "LUA_Rules"). The two columns Cost and Hrs are added to the Notepad (e.g. My Projects)

If you set the Cost=1,000, Hrs is calculated to 20. If you then change Hrs to 30, Cost is calculated to 1,500, etc. Try doing this in Excel!

LUA Script to Copy:

++:L:
cRate=50     -- The Rate is $50/Hr
vCost=get_folder_value("Cost",item_id)
vHrs=get_folder_value("Hrs",item_id)
vPrCost=get_folder_value("PrCost",item_id)     -- Get Previous Value
vPrHrs=get_folder_value("PrHrs",item_id)         -- Get Previous Value

if vPrCost~=vCost then         -- Cost changed => Calulate Hrs & set new PrCost, PrHrs
  vHrs=vCost/cRate
  set_folder_value("Hrs",item_id,vHrs)
  set_folder_value("PrHrs",item_id,vHrs)
  set_folder_value("PrCost",item_id,vCost)
elseif vPrHrs~=vHrs then       -- Hrs Changed => Calulate Cost & set new PrCost, PrHrs
  vCost=vHrs*cRate
  set_folder_value("Cost",item_id,vCost)
  set_folder_value("PrCost",item_id,vCost)
  set_folder_value("PrHrs",item_id,vHrs)
end


Title: Re: LUA Rule to Calculate Folder Values with Circular Reference
Post by Admin on 03/10/08 at 21:00:25

Off topic replies have been moved to This Thread (http://forums.eccoMAGIC.com/forum/YaBB.pl?num=1205200826)

The eccoMAGIC Forums » Powered by YaBB 2.1!
YaBB © 2000-2005. All Rights Reserved.