-
Notifications
You must be signed in to change notification settings - Fork 0
/
hduplicate.cpp
131 lines (101 loc) · 3.91 KB
/
hduplicate.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
#include "hduplicate.h"
#include "ui_hduplicate.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlQueryModel>
#include <QDebug>
#include <QSqlError>
HDuplicate::HDuplicate(int ocliente, int oprodotto,QSqlDatabase pdb,QWidget *parent) :
QWidget(parent),
ui(new Ui::HDuplicate)
{
ui->setupUi(this);
db=pdb;
QSqlTableModel *modclients=new QSqlTableModel(0,db);
cliente=ocliente;
prodotto=oprodotto;
// // qDebug()<<QString::number(cliente)<<QString::number(prodotto);
modclients->setTable("anagrafica");
modclients->setSort(1,Qt::AscendingOrder);
modclients->setFilter("cliente=1 or subcliente=1");
modclients->select();
ui->cbClienti->setModel(modclients);
ui->cbClienti->setModelColumn(1);
connect(ui->cbClienti,SIGNAL(currentIndexChanged(int)),this,SLOT(getRecipes()));
}
HDuplicate::~HDuplicate()
{
delete ui;
}
void HDuplicate::getRecipes()
{
QSqlQuery q(db);
QString sql;
QSqlQueryModel *modrecipes=new QSqlQueryModel();
int cliente=ui->cbClienti->model()->index(ui->cbClienti->currentIndex(),0).data(0).toInt();
// sql="select ricette.ID,prodotti.ID,prodotti.descrizione,associazioni.visualizza from prodotti, ricette, associazioni where ricette.ID=associazioni.ID_ricetta and prodotti.ID=ricette.ID_prodotto and ricette.ID_cliente=:idcliente";
// sql="SELECT ricette.ID,prodotti.ID,prodotti.descrizione from prodotti,ricette where ricette.ID_prodotto=prodotti.ID and prodotti.ID in (select ricette.ID_prodotto from ricette where ID_cliente=:idcliente) order by prodotti.descrizione";
sql="select ricette.ID,prodotti.ID,prodotti.descrizione from prodotti, ricette, associazioni where ricette.ID=associazioni.ID_ricetta and prodotti.ID=ricette.ID_prodotto and associazioni.ID_cliente=:idcliente";
q.prepare(sql);
q.bindValue(":idcliente",QVariant(cliente));
q.exec();
modrecipes->setQuery(q);
ui->cbRicette->setModel(modrecipes);
ui->cbRicette->setModelColumn(2);
// // qDebug()<<q.lastQuery()<<q.lastError()<<QString::number(cliente)<<q.boundValue(0).toString();
}
void HDuplicate::duplicate()
{
QSqlQuery q(db);
QString sql;
int ncli, npro;
ncli=ui->cbClienti->model()->index(ui->cbClienti->currentIndex(),0).data(0).toInt();
npro= ui->cbRicette->model()->index(ui->cbRicette->currentIndex(),1).data(0).toInt();
sql="SELECT olio,vaso,tappo,etichette,scatole,note from schede where prodotto=:prodold and cliente=:clientold";
q.prepare(sql);
q.bindValue(":prodold",QVariant(npro));
q.bindValue(":clientold",QVariant(ncli));
q.exec();
q.first();
QVariant olio,vaso,tappo,etichette,scatole,note;
olio= q.value(0);
vaso=q.value(1);
tappo=q.value(2);
etichette=q.value(3);
scatole=q.value(4);
note=q.value(5);
// // qDebug()<<olio.toString()<<q.lastError().text();
sql="delete from schede where cliente=:cliente and prodotto=:prodotto";
q.prepare(sql);
q.bindValue(":cliente",QVariant(cliente));
q.bindValue(":prodotto",QVariant(prodotto));
q.exec();
sql="insert into schede (prodotto,cliente,olio,vaso,tappo,etichette,scatole,note) values (:prodotto,:cliente,:olio,:vaso,:tappo,:etichette,:scatole,:note)";
q.prepare(sql);
q.bindValue(":prodotto",QVariant(prodotto));
q.bindValue(":cliente",QVariant(cliente));
q.bindValue(":olio",olio);
q.bindValue(":vaso",vaso);
q.bindValue(":tappo",tappo);
q.bindValue(":etichette",etichette);
q.bindValue(":scatole",scatole);
q.bindValue(":note",note);
bool b=q.exec();
if (!b)
{
qDebug()<<q.lastQuery()<<q.lastError().text();
}
else
{
emit update();
}
}
void HDuplicate::on_pushButton_clicked()
{
duplicate();
}
void HDuplicate::on_pushButton_2_clicked()
{
close();
}