Crearea situatiilor lunare din pontajele angajatilor

In acest exemplu vom construi un document direct in baza de date. Vom crea unul pentru fiecare dintre operatorii nostri si vom introduce orele lucrate pentru o anumita luna. Avand in vedere operatia care trebuie executata, acest script este o actiune asociata direct programului.

Grup Script

Program

Eveniment

Actiune program


output.clear()

dataform = program.newdataform()

dataform.settitle("Select month")

months = {"January","February","March","April","May","June","July","August","September","October","November","December"}

actualmonth = utility.datemonth(utility.getnow())

actualyear = utility.dateyear(utility.getnow())

actualmonths = null


if actualmonth == 1 then actualmonths = "January" end

if actualmonth == 2 then actualmonths = "February" end

if actualmonth == 3 then actualmonths = "March" end

if actualmonth == 4 then actualmonths = "April" end

if actualmonth == 5 then actualmonths = "May" end

if actualmonth == 6 then actualmonths = "June" end

if actualmonth == 7 then actualmonths = "July" end

if actualmonth == 8 then actualmonths = "August" end

if actualmonth == 9 then actualmonths = "September" end

if actualmonth == 10 then actualmonths = "October" end

if actualmonth == 11 then actualmonths = "November" end

if actualmonth == 12 then actualmonths = "December" end


dataform.addfieldlist("Month","MONTH",months,actualmonths)

dataform.addfieldinteger("Year","YEAR",actualyear)

dataform.show()


if dataform.closewithx == true then

do return end

end

Mai intai cream un dataform pentru a intreba utilizatorul care luna si an vor trebui analizate. Pentru comoditate, vom seta valorile implicite cu luna si anul curente.

In cazul in care utilizatorul inchide dataform-ul cu x, o regula buna va fi blocarea executiei script-ului.

table_rows = database.getsql("SELECT * FROM work_hour WHERE eli=0")

rows = table_rows.getrows()

for i = 1,table_rows.countrows() do

m = utility.datemonth(rows[i].getvalue("data"))

y = utility.dateyear(rows[i].getvalue("data"))

wrk = rows[i].getvalue("gguid_worker")

if m == actualmonth and y == actualyear then

vote = false

for i,worker in pairs(workers) do

if worker == wrk then

vote = true

end

end

if vote == false then

table.insert(worker,wrk)

end

end

end

Dupa ce am recuperat luna si anul pentru a fi analizate, incepem sa transformam tabelul work_hour pentru a intercepta ce operatori au lucrat in acel interval de timp. In aceasta faza salvam doar gguid-ul operatorului, deoarece va fi folosit pentru a efectua cautari pe tabele. De aici vom sti si cate documente vom crea.

for i,worker in pairs(workers) do

workername = ""

hourlyrate = 0

tworker = database.getsql("SELECT * FROM workers WHERE gguid='" .. worker .."' AND eli=0")

if tworker.countrows() > 0 then

rowso = tworker.getrows()

workername = rowso[1].getvalue("name")

hourlyrate = rowso[1].getvalue("hourly_rate")

end

Incepem sa rasfoim toti operatorii si, in primul rand, recuperam numele si tariful orar pentru fiecare.

Atentie: ciclul for nu este inchis, deoarece trebuie adaugate si partile ulterioare ale script-ului!

if workername ~= "" then

-- start to see if I find a monthly with the required characteristics

gguidmonth = ""

account = 0

tme = database.getsql("SELECT * FROM monthly

WHERE eli=0 and gguid_worker='" ..worker .. "' and month='" .. actualmonths .. "' and year=" .. tostring(actualyear) .."")

if tme.countrows() == 0 then

--I have to create a new card

gguidmonth = utility.gguid()

sql = "INSERT INTO monthly(gguid,tid,eli,arc,ut, uta,exp,gguidp,ind,tap,dsp,dsc,dsq1,dsq2,utc,tidc,gguid_worker,workername,[month],[year], hourly_rate,date) VALUES ("sql = sql .. "'" .. gguidmonth .. "'," .. tostring(utility.tid()) .. ",0,0,'','','',''," ..tostring(database.getind("monthly")) .. ",'','','',0,0,'',0,'" ..worker .. "','" .. utility.convap(workername) .. "','" .. actualmonths .."'," ..tostring(actualyear) .. "," ..tostring(hourlyrate) .. ",#" .. utility.formatdate(utility.getnow(),"yyyy-MM-dd") .. "#)"

database.setsql(sql)

else

rows = tme.getrows()

gguidmonth = rows[1].getvalue("gguid")

total_account = rows[1].getvalue("total_accounts")

--I eliminate the detail lines

database.setsql("DELETE * FROM monthly_rows WHERE gguidp='" .. gguidmonth .. "'")

end



Luam in considerare doar operatorii care au un nume valid si apoi vedem daca exista un lunar pentru acest operator, cu luna si anul solicitate. Daca nu exista, il cream, altfel il recuperam pe cel precedent eliminand liniile din interiorul sau, deoarece le vom recrea.

Retineti sirul de creare care contine toate campurile de gestionare Nios4 si campurile proprii tabelului. De asemenea, reamintim ca, pentru a evita erorile de functionare, tuturor campurilor trebuie sa li se ofere intotdeauna o valoare deoarece conditia null nu este gestionata.

Atentie: chiar si in acest caz if-ul initial nu este inchis.

--I begin to read the reports

table_rows = database.getsql("SELECT * FROM work_hour WHERE eli=0 and gguid_worker='" .. worker .. "'")

totalhour = 0

rows = table_rows.getrows()

for i = 1,table_rows.countrows() do

m = utility.datemonth(rows[i].getvalue("data"))

y = utility.dateyear(rows[i].getvalue("data"))

wrk = rows[i].getvalue("gguid_worker")

if m == actualmonth and y == actualyear then

- I create the row and save it

gguidr = utility.gguid()

sql = "INSERT INTO monthly_rows(gguid,tid,eli,arc,ut,uta,exp,gguidp,ind,tap,dsp,dsc,dsq1,dsq2,utc,tidc) VALUES ("sql = sql .. "'" .. gguidr .. "'," .. tostring(utility.tid()) .. ",0,0,'','','','" .. gguidmonth .. "'," .. tostring(database.getind("monthly_rows")) .. ",'monthly','','',0,0,'',0)"

database.setsql(sql)

- add the missing values

sql = "UPDATE monthly_rows SET date=#" .. utility.formatdate(rows[i].getvalue("data"),"yyyy-MM-dd") .. "#,"

sql = sql .. "gguid_customer='" .. rows[i].getvalue("gguid_customer") .. "', customer='" .. utility.convap(rows[i].getvalue("cliente")) .. "',"

sql = sql .. "total_hour=" .. utility.convi(rows[i].getvalue("total_hour")) .. ","

sql = sql .. "work_done=" .. utility.convap(rows[i].getvalue("work_done")) .. "',"

sql = sql .. "site='" .. utility.convap(rows[i].getvalue("site")) .. "'"

sql = sql .. " WHERE gguid='" .. gguidr .. "'"

database.setsql(sql)

totalhour = totalhour + rows[i].getvalue("total_hour")

end

end


Acum ca am creat sau am recuperat capul documentului situatiei noastre lunare, sa incepem sa extrapolam liniile din pontajul de ore in functie de operator. In acest caz, doua interogari de scriere sunt efectuate pentru comoditate: prima, stabileste noua linie prin introducerea valorilor pentru program in timp ce cea de a doua, introduce datele noastre.

-- update the head of the monthly

total_month = totalhour * hourlyrate

saldo = totale_mensile – totale_acconto

sql = "UPDATE monthly SET total_month=" .. utility.convi(total_month) .. ",total_hour=" .. utility.convi(totalhour) .. ",balance=".. utility.convi(balance) .. " WHERE gguid='" .. gguidmonth .."'" database.setsql(sql)

end

end

program.refreshsection("monthly")


Dupa rasfoirea tuturor liniilor, actualizam capul situatiei lunare introducand pretul total si cat am platit deja operatorului.

Inchidem conditia if care priveste numele operatorului si ciclul for.

In cele din urma, fortam programul sa actualizeze sectiunile care folosesc tabelul situatiilor lunare pentru a afisa documentele create.