-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcomposizionelotto.cpp
461 lines (295 loc) · 10.4 KB
/
hcomposizionelotto.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "hcomposizionelotto.h"
#include "ui_hcomposizionelotto.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include "hprint.h"
#include <QCursor>
#include <QShortcut>
#include "nouse.h"
#include "haddcomponent.h"
#include <QMessageBox>
#include "huser.h"
HComposizioneLotto::HComposizioneLotto( HUser * p_user,QSqlDatabase pdb, int idLotto, QString descrizione, HComposizioneLotto *parentf,QWidget *parent) :
QWidget(parent),
ui(new Ui::HComposizioneLotto)
{
ui->setupUi(this);
//ui->pushButton_2->setVisible(false);
//ui->pbUse->setVisible(false);
//ui->checkBox->setVisible(false);
db=pdb;
id=idLotto;
parf=parentf;
user=p_user;
det=new QShortcut(QKeySequence("F5"),this);
connect(det,SIGNAL(activated()),this,SLOT(getDetails()));
tipo=getTipo(id);
qDebug()<<"TIPO:"<<tipo;
desc=descrizione;
ui->leDesc->setText(descrizione);
if(tipo!=1)
{
this->setWindowTitle("Composizione Lotto");
getLotComposition();
ui->pbAdd->setVisible(true);
ui->pbRemove->setVisible(true);
}
else
{
this->setWindowTitle("Uso Lotto");
ui->pbAdd->setVisible(false);
ui->pbRemove->setVisible(false);
getLotUse();
}
}
HComposizioneLotto::~HComposizioneLotto()
{
delete ui;
}
void HComposizioneLotto::getDetails()
{
// HComposizioneLotto *f;
int lotid=mod->index(ui->tableView->selectionModel()->currentIndex().row(),0).data(0).toInt();
QModelIndex ixlot;
QModelIndex ixpro;
if (tipo != 1)
{
ixlot=mod->index(ui->tableView->selectionModel()->currentIndex().row(),2);
ixpro=mod->index(ui->tableView->selectionModel()->currentIndex().row(),3);
}
else
{
ixlot=mod->index(ui->tableView->selectionModel()->currentIndex().row(),2);
ixpro=mod->index(ui->tableView->selectionModel()->currentIndex().row(),4);
}
QString desc=ixlot.data(0).toString()+" - "+ixpro.data(0).toString();
parf=new HComposizioneLotto(0,db,lotid,desc);
// parf=f;
parf->show();
}
void HComposizioneLotto::getLotComposition()
{
QSqlQuery q(db);
QString sql;
/* int lotid=mod->index(ui->tableView->selectionModel()->currentIndex().row(),0).data(0).toInt();
lotid=mod->index(ui->tableView->selectionModel()->currentIndex().row(),0).data(0).toInt();
QModelIndex ixlot=mod->index(ui->tableView->selectionModel()->currentIndex().row(),2);
QModelIndex ixpro=mod->index(ui->tableView->selectionModel()->currentIndex().row(),3);
QString pdesc=ixlot.data(0).toString()+" - "+ixpro.data(0).toString();*/
sql="select lotdef.ID,operazioni.ID,operazioni.data as 'DATA',lotdef.lot as 'LOTTO',prodotti.descrizione as 'PRODOTTO',anagrafica.ragione_sociale as 'FORNITORE', operazioni.quantita as 'QUANTITA\\'',unita_di_misura.descrizione as 'UNITA\\'',lotdef.giacenza as 'GIACENZA' from operazioni,prodotti,lotdef,anagrafica,unita_di_misura where prodotti.ID=operazioni.IDprodotto and lotdef.ID=operazioni.IDlotto and anagrafica.ID=lotdef.anagrafica and unita_di_misura.ID=operazioni.um and operazioni.ID in (SELECT operazione from composizione_lot where ID_lotto=:lotid)";
q.prepare(sql);
q.bindValue(":lotid",QVariant(id));
q.exec();
mod=new QSqlQueryModel();
mod->setQuery(q);
if(mod->rowCount()>0)
{
ui->tableView->setModel(mod);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch );
ui->tableView->setColumnHidden(0,true);
ui->tableView->setCurrentIndex(ui->tableView->model()->index(0,0));
}
else
{
ui->tableView->setVisible(false);
NoUse *w=new NoUse();
ui->verticalLayout->addWidget(w);
w->SetLabelText("Ricerca composizione non applicabile per il tipo di lotto selezionato");
w->show();
}
// setAttribute(Qt::WA_DeleteOnClose);
// close();
}
//}
void HComposizioneLotto::getLotUse()
{
QSqlQuery q(db);
QString sql;
mod=new QSqlQueryModel();
sql="select lotdef.ID,lotdef.data as 'DATA',lotdef.lot as 'LOTTO',lotdef.EAN as 'LOT. ESTERNO',prodotti.descrizione as 'PRODOTTO',anagrafica.ragione_sociale AS 'CLIENTE' from lotdef,prodotti,operazioni,anagrafica,composizione_lot where prodotti.ID=lotdef.prodotto and lotdef.ID =composizione_lot.ID_lotto and anagrafica.ID=lotdef.anagrafica and operazioni.ID=composizione_lot.operazione and operazioni.IDlotto =:id ORDER BY lotdef.data desc";
q.prepare(sql);
q.bindValue(":id",id);
q.exec();
mod->setQuery(q);
ui->tableView->setModel(mod);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch );
ui->tableView->setColumnHidden(0,true);
ui->tableView->setCurrentIndex(ui->tableView->model()->index(0,0));
if(mod->rowCount()<1)
{
ui->tableView->setVisible(false);
NoUse *w=new NoUse();
w->SetLabelText("Lotto non ancora utilizzato o ricerca uso del lotto non applicabile a questa tipologia di lotto");
ui->verticalLayout->addWidget(w);
w->show();
}
// QString pdesc=mod->index(ui->tableView->selectionModel()->currentIndex().row(),2).data(0).toString() + " - " + mod->index(ui->tableView->selectionModel()->currentIndex().row(),3).data(0).toString() ;
// ui->leDesc->setText(pdesc);
}
void HComposizioneLotto::on_tableView_doubleClicked(const QModelIndex &index)
{
Q_UNUSED(index);
getDetails();
}
void HComposizioneLotto::on_pushButton_3_clicked()
{
close();
}
void HComposizioneLotto::print()
{
if(tipo<2)
{
printUse();
}
else
{
printComposition();
}
}
void HComposizioneLotto::printComposition()
{
HPrint *f =new HPrint();
QApplication::setOverrideCursor(Qt::WaitCursor);
int rows=ui->tableView->model()->rowCount();
int actrow=1;
int cols=ui->tableView->model()->columnCount();
f->append("COMPOSIZIONE LOTTO: "+ ui->leDesc->text().toUpper(),false);
f->append("",false);
QTextTable *tb=f->addTable(rows+3,cols);
f->append("",false);
f->toggleImageUI(false);
f->showMaximized();
QString txt;
int r,c;
txt="DATA";
f->writeTableContent(tb,0,1,txt);
txt="LOTTO";
f->writeTableContent(tb,0,2,txt);
txt="PRODOTTO";
f->writeTableContent(tb,0,3,txt);
txt="FORNITORE";
f->writeTableContent(tb,0,4,txt);
txt="QUANTITA'";
f->writeTableContent(tb,0,5,txt);
txt="UNITA'";
f->writeTableContent(tb,0,6,txt);
txt="GIACENZA";
f->writeTableContent(tb,0,7,txt);
double qta=0.0;
for (r=0;r<rows;r++)
{
actrow = r+2;
for (c=1; c<cols; c++)
{
bool ok=false;
if (c==5)
{
double lq=ui->tableView->model()->index(r,c).data(0).toDouble(&ok);
if(ok)
{
qta+=lq;
}
}
if (c==5 || c ==7)
{
txt=QString::number(ui->tableView->model()->index(r,c).data(0).toDouble(),'f',3);
}else{
txt=ui->tableView->model()->index(r,c).data(0).toString();
}
f->writeTableContent(tb,actrow,c,txt);
}
QApplication::processEvents();
}
txt=QString::number(qta,'f',3);
qDebug()<<txt;
actrow++;
f->writeTableContent(tb,actrow,4,"QUANTITA' TOTALE:");
f->writeTableContent(tb,actrow,5,txt);
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
void HComposizioneLotto::printUse()
{
HPrint *f =new HPrint();
QApplication::setOverrideCursor(Qt::WaitCursor);
int actrow=2;
f->append("USO LOTTO "+ ui->leDesc->text().toUpper(),false);
f->append("",false);
int rows=ui->tableView->model()->rowCount();
int cols=ui->tableView->model()->columnCount();
QTextTable *tb=f->addTable(rows+2,cols);
f->showMaximized();
int r,c;
QString txt;
txt="DATA";
f->writeTableContent(tb,0,1,txt);
txt="LOTTO";
f->writeTableContent(tb,0,2,txt);
txt="LOTTO ESTERNO";
f->writeTableContent(tb,0,3,txt);
txt="PRODOTTO";
f->writeTableContent(tb,0,4,txt);
txt="CLIENTE";
f->writeTableContent(tb,0,5,txt);
for (r=0;r<rows;r++)
{
actrow=r+2;
qDebug()<<rows<<r<<actrow;
for (c=1; c<cols; c++)
{
txt=ui->tableView->model()->index(r,c).data(0).toString();
f->writeTableContent(tb,actrow,c,txt);
}
}
QApplication::processEvents();
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
void HComposizioneLotto::removeComponent()
{
int operation=ui->tableView->model()->index(ui->tableView->currentIndex().row(),1).data(0).toInt();
db.transaction();
QSqlQuery q(db);
QString sql="delete from composizione_lot where operazione=:operazione";
q.prepare(sql);
q.bindValue(":operazione",operation);
q.exec();
mod->query().exec();
if (QMessageBox::question(this,QApplication::applicationName(),"Confermere l'eliminazione?",QMessageBox::Ok|QMessageBox::Cancel)==QMessageBox::Ok)
{
db.commit();
}
else
{
db.rollback();
}
mod->query().exec();
}
int HComposizioneLotto::getTipo(int idl)
{
QSqlQuery q(db);
q.prepare("SELECT tipo FROM lotdef where ID=:id");
q.bindValue(0,idl);
q.exec();
q.next();
return q.value(0).toInt();
}
void HComposizioneLotto::on_pbRemove_clicked()
{
removeComponent();
getLotComposition();
}
void HComposizioneLotto::refresh()
{
getLotComposition();
}
void HComposizioneLotto::on_pbAdd_clicked()
{
HAddComponent *f=new HAddComponent(id,user,db);
connect(f,SIGNAL(done()),this,SLOT(refresh()));
f->show();
}
void HComposizioneLotto::on_pbPrint_clicked()
{
print();
}