-
Notifications
You must be signed in to change notification settings - Fork 4
/
reportsdialog.cpp
281 lines (250 loc) · 8.92 KB
/
reportsdialog.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
#include "reportsdialog.h"
#include "ui_reportsdialog.h"
#include <QMessageBox>
#include <QSqlError>
#include <QDateEdit>
#include <QFont>
// pageborderclass
class PrintBorder : public PagePrepare
{
public:
virtual void preparePage(QPainter *painter);
static int pageNumber;
};
int PrintBorder::pageNumber = 0;
void PrintBorder::preparePage(QPainter *painter) { // print a border on each page
QRect rec = painter->viewport();
painter->setPen(QPen(QColor(0, 0, 0), 1));
painter->drawRect(rec);
painter->translate(10, painter->viewport().height() - 10);
painter->drawText(0, 0, QString("Page %1").arg(pageNumber));
pageNumber += 1;
}
//<---------------------------------------------------------------
ReportsDialog::ReportsDialog(QSqlDatabase *database, QWidget *parent) :
QDialog(parent),
db(database),
ui(new Ui::ReportsDialog)
{
ui->setupUi(this);
if(!db->isOpen()){
if(!db->open())
QMessageBox::critical(this,"Database","Database is not opened.");
reject();
}
//initialization
initializeDateEdits();
setupConnections();
initializeModels();
setupModels();
setupDrawingVariables();
}
void ReportsDialog::initializeDateEdits()
{
//dealer tab
ui->dealerfrom_dateEdit->setDate(QDate::currentDate().addDays(-7));
ui->dealerto_dateEdit->setDate(QDate::currentDate());
//supplier tab
ui->supplierfrom_dateEdit_2->setDate(QDate::currentDate().addDays(-7));
ui->supplierto_dateEdit_2->setDate(QDate::currentDate());
}
void ReportsDialog::initializeModels()
{
dealer_model = new QSqlQueryModel(this);
supplier_model = new QSqlQueryModel(this);
dealer_payments_model = new QSqlQueryModel(this);
supplier_payments_model = new QSqlQueryModel(this);
}
void ReportsDialog::setupModels()
{
setupDealerTabModel();
setupSupplierTabModel();
}
void ReportsDialog::setupConnections()
{
//connections
//dealer tab
connect(ui->dealerto_dateEdit,&QDateEdit::dateChanged,
this,&ReportsDialog::loadDealerPayments);
connect(ui->dealerfrom_dateEdit,&QDateEdit::dateChanged,
this,&ReportsDialog::loadDealerPayments);
connect(ui->dealer_comboBox,&QComboBox::currentTextChanged,
this,&ReportsDialog::loadDealerPayments);
//supplier tab
connect(ui->supplierto_dateEdit_2,&QDateEdit::dateChanged,
this,&ReportsDialog::loadSupplierPayments);
connect(ui->supplierfrom_dateEdit_2,&QDateEdit::dateChanged,
this,&ReportsDialog::loadSupplierPayments);
connect(ui->supplier_comboBox_2,&QComboBox::currentTextChanged,
this,&ReportsDialog::loadSupplierPayments);
}
void ReportsDialog::setupDealerTabModel()
{
/// Combo box
// execute query
QSqlQuery q(*db);
q.prepare("select name from dealers");
if(!q.exec()){
QSqlError err = q.lastError();
QMessageBox::critical(this,"Error","Could not load the"
" dealers "+err.text());
return;
}
dealer_model->setQuery(q);
//set views
ui->dealer_comboBox->setModel(dealer_model);
//load payment details
loadDealerPayments();
}
void ReportsDialog::setupSupplierTabModel()
{
//execute queries
QSqlQuery q(*db);
q.prepare("select name from supplier");
if(!q.exec()){
QSqlError err = q.lastError();
QMessageBox::critical(this,"Error","Could not load the"
" supplier "+err.text());
return;
}
//set views
supplier_model->setQuery(q);
ui->supplier_comboBox_2->setModel(supplier_model);
loadSupplierPayments();
}
void ReportsDialog::setupDrawingVariables()
{
colStretch = QVector<int>() << 5 << 10 << 10 << 10 << 10 << 10;
headers = QVector<QString>() << "ID" << "پارٹی" << "رقم" << "تاریخ"
<< "وقت" << "Nature";
_factoryName = "786 پلاسٹکس";
}
void ReportsDialog::loadDealerPayments()
{
QSqlQuery q(*db);
q.prepare("select serial,dealer,money,date,time,nature from payments"
" where dealer=? and date>? and date<? and nature=?");
q.bindValue(0,ui->dealer_comboBox->currentText());
q.bindValue(1,ui->dealerfrom_dateEdit->date().addDays(-1).toString("yyyy-MM-dd"));
q.bindValue(2,ui->dealerto_dateEdit->date().addDays(1).toString("yyyy-MM-dd"));
q.bindValue(3,"Recieved");
if(!q.exec()){
QSqlError err = q.lastError();
QMessageBox::critical(this,"Error","Could not load the"
" payments "+err.text());
return;
}
//set view
dealer_payments_model->setQuery(q);
ui->dealer_tableview->setModel(dealer_payments_model);
}
void ReportsDialog::loadSupplierPayments()
{
QSqlQuery q(*db);
q.prepare("select * from payments"
" where dealer=? and date>? and date<? and nature=?");
q.bindValue(0,ui->supplier_comboBox_2->currentText());
q.bindValue(1,ui->supplierfrom_dateEdit_2->date().addDays(-1).toString("yyyy-MM-dd"));
q.bindValue(2,ui->supplierto_dateEdit_2->date().addDays(1).toString("yyyy-MM-dd"));
q.bindValue(3,"Paid");
if(!q.exec()){
QSqlError err = q.lastError();
QMessageBox::critical(this,"Error","Could not load the"
" payments "+err.text());
return;
}
//set view
supplier_payments_model->setQuery(q);
ui->supplier_tableView->setModel(supplier_payments_model);
}
void ReportsDialog::dealerUglyPrint(QPrinter *printer) {
// ---------------- death-to-designers ------------------
QPainter uglyPainter;
if(!uglyPainter.begin(printer)) {
qWarning() << "can't start printer";
return;
}
TablePrinter uglyTablePrinter(&uglyPainter, printer);
uglyTablePrinter.setPen(QPen(QColor(0, 0, 0), 3, Qt::SolidLine)); // pen for borders
uglyTablePrinter.setHeaderColor(QColor(Qt::black));
uglyTablePrinter.setContentColor(Qt::black);
QFont font1; // font for headers
font1.setPixelSize(16);
font1.setBold(true);
font1.setWeight(QFont::DemiBold);
QFont font2; // font for content
font2.setItalic(true);
uglyTablePrinter.setHeadersFont(font1);
uglyTablePrinter.setContentFont(font2);
PrintBorder *printB = new PrintBorder;
printB->pageNumber = 1;
uglyTablePrinter.setPagePrepare(printB);
uglyPainter.setPen(QPen(Qt::black));
QFont titleFont;
titleFont.setFamily("arial");
titleFont.setWeight(QFont::Black);
titleFont.setPixelSize(26);
uglyPainter.setFont(titleFont);
uglyPainter.drawText(uglyPainter.viewport().width()/2 - 40, 40, _factoryName);
uglyPainter.translate(0, 60); // start print point
uglyTablePrinter.setCellMargin(10, 5, 5, 5);
uglyTablePrinter.setPageMargin(100, 40, 40, 40);
if(!uglyTablePrinter.printTable(ui->dealer_tableview->model(), colStretch, headers)) {
qDebug() << uglyTablePrinter.lastError();
}
uglyPainter.end();
delete printB;
}
void ReportsDialog::supplierUglyPrint(QPrinter *printer)
{
// ---------------- death-to-designers ------------------
QPainter uglyPainter;
if(!uglyPainter.begin(printer)) {
qWarning() << "can't start printer";
return;
}
TablePrinter uglyTablePrinter(&uglyPainter, printer);
uglyTablePrinter.setPen(QPen(QColor(0, 0, 0), 3, Qt::SolidLine)); // pen for borders
uglyTablePrinter.setHeaderColor(QColor(Qt::black));
uglyTablePrinter.setContentColor(Qt::black);
QFont font1; // font for headers
font1.setBold(true);
font1.setWeight(QFont::DemiBold);
QFont font2; // font for content
font2.setItalic(true);
uglyTablePrinter.setHeadersFont(font1);
uglyTablePrinter.setContentFont(font2);
PrintBorder *printB = new PrintBorder;
printB->pageNumber = 1;
uglyTablePrinter.setPagePrepare(printB);
uglyPainter.setPen(QPen(Qt::black));
uglyPainter.drawText(uglyPainter.viewport().width()/2 - 40, 40, _factoryName);
uglyPainter.translate(0, 60); // start print point
uglyTablePrinter.setCellMargin(10, 5, 5, 5);
uglyTablePrinter.setPageMargin(100, 40, 40, 40);
if(!uglyTablePrinter.printTable(ui->supplier_tableView->model(), colStretch, headers)) {
qDebug() << uglyTablePrinter.lastError();
}
uglyPainter.end();
delete printB;
}
ReportsDialog::~ReportsDialog()
{
delete ui;
}
void ReportsDialog::on_printDealerPayment_toolButton_clicked()
{
QPrintPreviewDialog *dialog = new QPrintPreviewDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog,&QPrintPreviewDialog::paintRequested,
this,&ReportsDialog::dealerUglyPrint);
dialog->exec();
}
void ReportsDialog::on_printsupplierpayments_toolbutton_clicked()
{
QPrintPreviewDialog *dialog = new QPrintPreviewDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog,&QPrintPreviewDialog::paintRequested,
this,&ReportsDialog::supplierUglyPrint);
dialog->exec();
}