LUA example: Update bill of materials prices

In this example we’ll see how to recalculate item prices starting from their bill of materials. In practice, when the price of an item changes, we can find which bills of materials it appears in and recalculate the prices of the parent items from there.

Script Group
Form
Event
Post-save

So let’s see how to use a recursive function, that is, a function that calls itself, to calculate all the levels of the bill of materials of each item passed to it.

The script is linked to the post-save event of the item.

function calcola(gpadre)

--get the bill of materials and read prices and costs

tpadre = database.getsql("SELECT * FROM distintabase_dn WHERE gguidp='" .. gpadre .."'")

nrighep = tpadre.countrows()

righep = tpadre.getrows()

prezzofinale = 0

costofinale = 0

for i = 1,nrighep do

prezzofinale = prezzofinale + righep[i].getvalue("prezzo_totale")

costofinale = costofinale + righep[i].getvalue("costo_totale")

end

 

database.setsql("UPDATE articoli SET aggiornato=1,costo=" .. utility.formatnum(costofinale,2) ..",prezzo=" .. utility.formatnum(prezzofinale,2) .. " WHERE gguid='" .. gpadre .. "'")

database.setsql("UPDATE articoli SET tid=" .. tostring(utility.tid()) .. " WHERE gguid='" .. gpadre .. "'")

database.addsyncbox("articoli",gpadre)

--check whether this item is a child of another one; if so, calculate the other parents

tpadri = database.getsql("SELECT * FROM distintabase_dn WHERE gguid_codice_dn='" .. gpadre .. "'")

 

nrighepadri = tpadri.countrows()

righepadri = tpadri.getrows()

--update prices and costs where the item is used as a child in other bills of materials

 

sprezzo = utility.formatnum(prezzofinale,2)

scosto = utility.formatnum(costofinale,2)

  

gguidpadri = {}

for i = 1,nrighepadri do

database.setsql("UPDATE distintabase_dn SET costo=" .. scosto .. ",prezzo=" .. sprezzo .. " WHERE gguid='" .. righepadri[i].getvalue("gguid") .. "'")

database.setsql("UPDATE distintabase_dn SET costo_totale=costo * qta_dn,prezzo_totale=prezzo * qta_dn WHERE gguid='" .. righepadri[i].getvalue("gguid") .. "'")

database.setsql("UPDATE distintabase_dn SET tid=" ..

tostring(utility.tid()) .. " WHERE gguid='" .. righepadri[i].getvalue("gguid") .. "'")

database.addsyncbox("distintabase_dn", righepadri[i].getvalue("gguid"))

--get the gguids of the parents

table.insert(gguidpadri, righepadri[i].getvalue("gguidp"))

end

--process the items recursively

for i,gp in pairs(gguidpadri) do

calcola(gp)

end

 

end

First we write the recursive function, which takes the gguid of the item to update as its input parameter. It retrieves the bill of materials table, recalculates the price and updates it. During the update we collect the gguids of the items that use it in their own bills of materials and, as you can see, call the same function again, which makes it recursive.

In practice, the system updates the item starting from the lowest level (always the item we changed at the start) and then works backwards to recalculate all its parents.

prezzo = dataview.getvalue("prezzo")

costo = dataview.getvalue("costo")

gguid = dataview.getvalue("gguid")

sprezzo = utility.formatnum(prezzo,2)

scosto = utility.formatnum(costo,2)

--find where the item is used and update price and cost

tdistinta = database.getsql("SELECT * FROM distintabase_dn WHERE gguid_codice_dn='" .. gguid .."'")

nrighe = tdistinta.countrows()

righe = tdistinta.getrows()

gguidpadri = {}

for i = 1,nrighe do

database.setsql("UPDATE distintabase_dn SET costo=" .. scosto .. ",prezzo=" .. sprezzo .. " WHERE gguid='" .. righe[i].getvalue("gguid") .. "'")

database.setsql("UPDATE distintabase_dn SET costo_totale=costo * qta_dn,prezzo_totale=prezzo * qta_dn WHERE gguid='" .. righe[i].getvalue("gguid") .."'")

database.setsql("UPDATE distintabase_dn SET tid=" .. tostring(utility.tid()) .. " WHERE gguid='" .. righe[i].getvalue("gguid") .. "'")

 

database.addsyncbox("distintabase_dn", righe[i].getvalue("gguid"))

--get the gguids of the parents

table.insert(gguidpadri,righe[i].getvalue("gguidp"))

end

--process the items recursively

for i,gp in pairs(gguidpadri) do

calcola(gp)

end

program.refreshsection("articoli")

In this part of the script, the one actually run by the program, we get the item’s information and update prices and costs wherever the item is used in bills of materials. This gives us the gguids of the parents, which are then passed to the Calcola function. From there, for each parent, the function goes up the chain of items, making sure that the change made to the starting item is reflected in all the bills of materials, and therefore in the prices and costs of all the items that use it directly or indirectly.