In this example we’ll build a small order grouper inside the invoice. With a single script we can collect what our customer owes us and create one document.
Script Group
Form
Event
Action
if dataview.getvalue("gguid_cliente") == "" then
dataview.showerror("Select the customer!")
do return end
end
First we check that the customer has been selected in the new invoice. Obviously, if no customer has been chosen we can’t go on.
tabellafattura = dataview.getatable("fattura_rows")
tabella= database.getsql("SELECT * FROM ordini WHERE gguid_cliente='" .. dataview.getvalue("gguid_cliente") .. "' AND eli=0 AND fatturato=0")
righe = tabella.getrows()
for i=1,tabella.countrows() do
data = righe[i].getvalue("data")
numero = righe[i].getvalue("numero")
descrizione = "Order No. " .. numero .. " of " .. data
--ADD THE ORDER DESCRIPTION ROW
rigafattura = tabellafattura.adddatarow()
rigafattura.setvalue("descrizione",descrizione)
tabellarighe= database.getsql("SELECT * FROM ordini_rows WHERE gguidp='" .. righe[i].getvalue("gguid") .. "' AND eli=0")
righeordini = tabellarighe.getrows()
for j=1,tabellarighe.countrows() do
--ADD ALL THE ORDER ROWS
rigafattura2 = tabellafattura.adddatarow()
rigafattura2.setvalue("codice", righeordini[j].getvalue("codice"))
rigafattura2.setvalue("descrizione", righeordini[j].getvalue("descrizione"))
rigafattura2.setvalue("qta",righeordini[j].getvalue("qta"))
rigafattura2.setvalue("prezzo", righeordini[j].getvalue("prezzo"))
rigafattura2.setvalue("prezzototale",righeordini[j].getvalue("prezzototale"))
end
--ADD AN EMPTY ROW TO SEPARATE THE ORDERS
rigafattura = tabellafattura.adddatarow()
rigafattura.setvalue("descrizione","")
--CHANGE THE ORDER STATUS SO IT ISN'T INVOICED TWICE
database.setsql("UPDATE ordini SET tid=" .. tostring(utility.tid()) .. ",fatturato=1 WHERE gguid='" .. righe[i].getvalue("gguid") .. "'")
database.addsyncbox("ordini",righe[i].getvalue("gguid"))
end
--FORCE THE INVOICE TO UPDATE
dataview.update()
The procedure is made of two loops. The first extracts the orders, selecting them by customer and by the value fatturato=0. This prevents two invoices from including the same order.
Once we have the orders, we add a first row to the invoice showing the order number and date.
Then we extract the rows of the current order and start the second loop to add all its rows to the invoice.
We then add an empty row between one order and the next, so they are easier to read in the invoice body. At the same time we change the order status by setting Fatturato to 1, so the order is not picked up by another invoice. When both loops are done, we force the invoice calculations to update.
