-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpretrazivanjesredstavad1.cpp
198 lines (175 loc) · 7.24 KB
/
pretrazivanjesredstavad1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "pretrazivanjesredstavad1.h"
PretrazivanjeSredstavaD1::PretrazivanjeSredstavaD1(QWidget *parent, int userId)
: QDialog(parent),
ui(new Ui::PretrazivanjeSredstavaD1)
{
ui->setupUi(this);
_userId = userId;
QSqlQuery query;
query.prepare( "SELECT tip FROM nalog WHERE id = ?");
query.bindValue(0, _userId);
query.exec();
if(query.next())
tipNaloga = query.value(0).toString();
setModelSredstva();
setModelGodina();
setModelOrgJedinica();
setModelDobavljac();
setModelStatusSredstva();
setModelZaposleni();
}
PretrazivanjeSredstavaD1::~PretrazivanjeSredstavaD1()
{
delete ui;
}
void PretrazivanjeSredstavaD1::on_btnIzadji_clicked()
{
this->close();
}
void PretrazivanjeSredstavaD1::setModelSredstva()
{
QComboBox *view = ui->cbSredstva;
modelSredstva = new QSqlQueryModel();
modelSredstva->setQuery("select -1 as A, 'Svi' as B union select * from artikal;");
view->setModel(modelSredstva);
view->setModelColumn(1);
view->setCurrentIndex(view->findText("Svi"));
}
void PretrazivanjeSredstavaD1::setModelGodina()
{
QComboBox *view = ui->cbGodina;
modelGodina = new QSqlQueryModel();
modelGodina->setQuery("select 'Sve' union select distinct(substr(datum, -4)) as godina from spisak order by godina;");
view->setModel(modelGodina);
view->setModelColumn(0);
view->setCurrentIndex(view->findText("Sve"));
}
void PretrazivanjeSredstavaD1::setModelOrgJedinica()
{
QComboBox *view = ui->cbOrgJedinica;
modelOrgJedinica = new QSqlQueryModel();
if(!tipNaloga.compare("rukovodilac"))
{
modelOrgJedinica->setQuery("select o.id_ogranka, o.naziv \
from ogranak o join nalog n on o.id_ogranka = n.id_ogranka \
where n.id = " + QString("%1").arg(_userId) + ";");
view->setModel(modelOrgJedinica);
view->setModelColumn(1);
view->setDisabled(true);
}
else if(!tipNaloga.compare("zaposleni") || !tipNaloga.compare("referent_sluzbe") || !tipNaloga.compare("magacioner") || !tipNaloga.compare("sekretar"))
{
modelOrgJedinica->setQuery("select o.id_ogranka, o.naziv \
from ogranak o join nalog n on o.id_ogranka = n.id_ogranka \
where n.id = " + QString("%1").arg(_userId) + ";");
view->setModel(modelOrgJedinica);
view->setModelColumn(1);
view->setDisabled(true);
}
else
{
modelOrgJedinica->setQuery("select -1, 'Sve', -1, -1 union select * from ogranak;");
view->setModel(modelOrgJedinica);
view->setModelColumn(1);
view->setCurrentIndex(view->findText("Sve"));
}
}
void PretrazivanjeSredstavaD1::setModelDobavljac()
{
QComboBox *view = ui->cbDobavljac;
modelDobavljac = new QSqlQueryModel();
modelDobavljac->setQuery("select -1, 'Svi', -1, -1 union select * from dobavljac;");
view->setModel(modelDobavljac);
view->setModelColumn(1);
view->setCurrentIndex(view->findText("Svi"));
}
void PretrazivanjeSredstavaD1::setModelStatusSredstva()
{
QComboBox *view = ui->cbStatusSredstva;
modelStatusSredstva = new QSqlQueryModel();
modelStatusSredstva->setQuery("select 'Svi' union select distinct(status) from stavka;");
view->setModel(modelStatusSredstva);
view->setModelColumn(0);
view->setCurrentIndex(view->findText("Svi"));
}
void PretrazivanjeSredstavaD1::setModelZaposleni()
{
QComboBox *view = ui->cbZaposleni;
modelZaposleni = new QSqlQueryModel();
if(!tipNaloga.compare("rukovodilac"))
{
modelZaposleni->setQuery("select -1, 'Svi' union select id, ime || ' ' || prezime from nalog \
where tip = 'zaposleni' \
and id_ogranka = (select id_ogranka from nalog where id = " + QString("%1").arg(_userId) + ");");
view->setModel(modelZaposleni);
view->setModelColumn(1);
view->setCurrentIndex(view->findText("Svi"));
}
else if(!tipNaloga.compare("zaposleni") || !tipNaloga.compare("referent_sluzbe") || !tipNaloga.compare("magacioner") || !tipNaloga.compare("sekretar"))
{
modelZaposleni->setQuery("select id, ime || ' ' || prezime from nalog where id = " + QString("%1").arg(_userId) + ";");
view->setModel(modelZaposleni);
view->setModelColumn(1);
view->setDisabled(true);
}
else
{
modelZaposleni->setQuery("select -1, 'Svi' union select id, ime || ' ' || prezime from nalog;");
view->setModel(modelZaposleni);
view->setModelColumn(1);
view->setCurrentIndex(view->findText("Svi"));
}
}
void PretrazivanjeSredstavaD1::on_btnPretrazi_clicked()
{
QTreeView *view = ui->tvPretraga;
modelTablePretraga = new QSqlQueryModel(this);
QString query = "select id_stavke, a.ime, substr(sp.datum,4,2), substr(sp.datum,-4), n.ime || ' ' || n.prezime, o.naziv, d.naziv, st.status, st.trazena_kol, st.odobrena_kol \
from stavka st join artikal a on st.id_artikla = a.id_artikla \
join spisak sp on sp.id_spiska = st.id_spiska \
join nalog n on sp.id_naloga = n.id \
join ogranak o on n.id_ogranka = o.id_ogranka \
left outer join dobavljac d on st.id_dobavljaca = d.id_dobavljaca where 1=1";
if(ui->cbSredstva->currentText().compare("Svi"))
query +=" and id_stavke = " + modelSredstva->data(modelSredstva->index(ui->cbSredstva->currentIndex(),0)).toString();
if(ui->cbMesec->currentIndex())
{
if(ui->cbMesec->currentIndex() < 10)
query +=" and substr(sp.datum,4,2) = '0" + QString("%1").arg(ui->cbMesec->currentIndex()) + "'";
else
query +=" and substr(sp.datum,4,2) = '" + QString("%1").arg(ui->cbMesec->currentIndex()) + "'";
}
if(ui->cbGodina->currentText().compare("Sve"))
query +=" and substr(sp.datum,-4) = '" + ui->cbGodina->currentText() + "'";
if(!tipNaloga.compare("zaposleni"))
{
query +=" and n.id = " + QString("%1").arg(_userId);
}
else if(ui->cbZaposleni->currentText().compare("Svi"))
query +=" and n.id = " + modelZaposleni->data(modelZaposleni->index(ui->cbZaposleni->currentIndex(),0)).toString();
if(!tipNaloga.compare("rukovodilac"))
{
query +=" and o.id_ogranka = " + modelOrgJedinica->data(modelOrgJedinica->index(ui->cbOrgJedinica->currentIndex(),0)).toString();
}
else if(ui->cbOrgJedinica->currentText().compare("Sve"))
query +=" and o.id_ogranka = " + modelOrgJedinica->data(modelOrgJedinica->index(ui->cbOrgJedinica->currentIndex(),0)).toString();
if(ui->cbDobavljac->currentText().compare("Svi"))
query +=" and d.id_dobavljaca = " + modelDobavljac->data(modelDobavljac->index(ui->cbDobavljac->currentIndex(),0)).toString();
if(ui->cbStatusSredstva->currentText().compare("Svi"))
query +=" and st.status like '" + ui->cbStatusSredstva->currentText() + "'";
modelTablePretraga->setQuery(query);
modelTablePretraga->setHeaderData(1, Qt::Horizontal, "Naziv sredstva");
modelTablePretraga->setHeaderData(2, Qt::Horizontal, "Mesec");
modelTablePretraga->setHeaderData(3, Qt::Horizontal, "Godina");
modelTablePretraga->setHeaderData(4, Qt::Horizontal, "Zaposleni");
modelTablePretraga->setHeaderData(5, Qt::Horizontal, "Organizaiona jedinica");
modelTablePretraga->setHeaderData(6, Qt::Horizontal, "Dobavljac");
modelTablePretraga->setHeaderData(7, Qt::Horizontal, "Stanje");
modelTablePretraga->setHeaderData(8, Qt::Horizontal, "Trazena kolicina");
modelTablePretraga->setHeaderData(9, Qt::Horizontal, "Odobrena kolicina");
view->setModel(modelTablePretraga);
view->setSortingEnabled(true);
view->setColumnHidden(0, true);
if(modelTablePretraga->rowCount()==0)
QMessageBox::warning(this, "Pretraga", "Ne postoje stavke za trazene vrednosti. Pokusajte ponovo.");
}