-
Notifications
You must be signed in to change notification settings - Fork 4
/
paydialog.cpp
316 lines (282 loc) · 10.8 KB
/
paydialog.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
#include "paydialog.h"
#include "ui_paydialog.h"
#include <QSqlQuery>
#include "database.h"
#include <QPainter>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QDate>
PayDialog::PayDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::PayDialog)
{
ui->setupUi(this);
//去掉问号按钮
setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
payTableModel = new PayTableModel;
ui->tabPay->setModel(payTableModel);
ui->tabPay->verticalHeader()->setHidden(true);
ui->tabPay->horizontalHeader()->setAlternatingRowColors(true);
ui->tabPay->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// ui->tabPay->horizontalHeader()->setStretchLastSection(true);
}
PayDialog::~PayDialog()
{
delete ui;
delete payTableModel;
}
void PayDialog::setUser(int userId)
{
setWindowTitle(Database::getUserNameById(userId) + "【"
+ Database::getUserAddressById(userId) + "】 - "
+ "缴费详情");
payTableModel->setUser(userId);
}
/**
* 缴费窗口的表格Model
* 继承于QAbstractTableModel
*/
PayTableModel::PayTableModel()
{
}
int PayTableModel::rowCount(const QModelIndex &parent) const
{
if(parent.isValid()) return 0;
return datas.size();
}
int PayTableModel::columnCount(const QModelIndex &parent) const
{
if(parent.isValid()) return 0;
return headers.size();
}
QVariant PayTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid()) return QVariant();
if(role == Qt::TextAlignmentRole){
return Qt::AlignCenter;
}else if(role == Qt::DisplayRole){
return datas.at(index.row()).at(index.column());
}
return QVariant();
}
QVariant PayTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role != Qt::DisplayRole || orientation == Qt::Vertical) return QVariant();
return headers.at(section);
}
bool PayTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(index.column() == 4 && role == Qt::EditRole){
bool ok;
qreal v = value.toReal(&ok);
if(ok){
datas[index.row()][index.column()] = QString::number(v);
datas[index.row()][5] = QString::number(datas.at(index.row()).at(3).toDouble() * v);
//修改总金额
qreal allMoney = 0.0;
for(int i = 0; i < datas.size() - 1; i++){
allMoney += datas.at(i).at(5).toDouble();
}
datas[datas.size() - 1][5] = QString::number(allMoney);
return true;
}
}else if(index.column() == 6 && role == Qt::EditRole){
if(!value.toString().isEmpty())
datas[index.row()][6] = value.toString();
}
return false;
}
Qt::ItemFlags PayTableModel::flags(const QModelIndex &index) const
{
//单价可编辑,但是最后一行是总价,所以datas.size() - 1
if(index.column() == 4 && index.row() != datas.size() - 1){
return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}else if(index.column() == 6){
return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}else{
return Qt::ItemIsEnabled;
}
}
void PayTableModel::setUser(int userId)
{
this->userId = userId;
QDate currentDate(QDate::currentDate());
beginResetModel();
headers.clear();
headers<<"项目"<<"上次读数"<<"本次读数"<<"数量"<<"单价"<<"金额"<<"日期";
datas.clear();
QSqlQuery query(QSqlDatabase::database());
query.prepare(QString("SELECT meter_id,meter_name,max(quarter1,quarter2,quarter3,quarter4),"
"pay_record FROM y%1 WHERE user_id=? ORDER BY meter_id DESC").arg(currentDate.year()));
query.bindValue(0, userId);
query.exec();
qreal allMoney = 0.0;
while(query.next()){
QStringList row;
row<<query.value("meter_name").toString(); //项目
int cnt = 0; //缴费量
if(query.value("meter_id").toInt() == 0){
row<<""<<""<<query.value("pay_record").toString();
cnt = query.value("pay_record").toInt();
double cleanUnitPrice = Database::getCleanUnitPrice();
row<<QString::number(cleanUnitPrice); //单价
row<<QString::number(cnt * cleanUnitPrice); //金额
}else{
row<<query.value("pay_record").toString(); //上次读数
row<<query.value(2).toString(); //本次读数
cnt = row.at(2).toInt() - row.at(1).toInt();
row<<QString::number(cnt); //缴费量
double waterUnitPrice = Database::getWaterUnitPrice();
row<<QString::number(waterUnitPrice); //单价
row<<QString::number(cnt * waterUnitPrice); //金额
}
row<<QString("%1.%2").arg(currentDate.year()).arg(currentDate.month());
row<<query.value("meter_id").toString(); //meter_id
//普通水表欠费 或 卫生费
// if(cnt > 0 || (query.value("meter_id").toInt() == 0 && query.value("pay_record").toInt() > 0)){
if(cnt >= 0){
allMoney += row.at(5).toDouble();
datas.append(row);
}
}
QStringList lastRow;
lastRow<<"总计"<<""<<""<<""<<""<<QString::number(allMoney)<<"";
datas.append(lastRow);
endResetModel();
}
QString PayTableModel::upperMoney(qreal x)
{
QStringList NUM_C;
NUM_C<<"零"<<"壹"<<"贰"<<"叁"<<"肆"<<"伍"<<"陆"<<"柒"<<"捌"<<
"玖"<<"拾"<<"佰"<<"仟"<<"万"<<"亿";
QString strNum = QString("%1").arg(x);
int j;
int length = strNum.size();
int dotPos = strNum.indexOf('.');
if(dotPos != -1){
length = dotPos;
}
QString result;
if(length == 1 && strNum[0] == '0')
result.append(NUM_C[0]);
else {
for(int i = 0; i < length; i++) {
if(strNum.at(i) == '0') {
int offset = (length - i) % 4 == 0 ? 4 : (length - i) % 4;
for(j = i + 1; j < i + offset; j++) {
if(strNum[j] != '0')
break;
}
if(j < i + offset && (strNum[i - 1] != '0' || (length - i) % 4 == 0))
result.append(NUM_C[strNum.at(i).toLatin1() - '0']);
} else if(strNum[i] == '1') {
if(length % 4 != 2 || i != 0)
result.append(NUM_C[strNum.at(i).toLatin1() - '0']);
} else {
result.append(NUM_C[strNum.at(i).toLatin1() - '0']);
}
switch((length - i) % 4) {
case 0://千
if(strNum[i] != '0')
result.append(NUM_C[12]);
break;
case 1: {
switch((length - i) / 4) {
case 1:
if(length > 8 && strNum[i] == '0' && strNum[i - 1] == '0' && strNum[i - 2] == '0' && strNum[i - 3] == '0');
else
result.append(NUM_C[13]);//万
break;
case 2:
result.append(NUM_C[14]);//亿
break;
}
break;
}
case 2: { //十
if(strNum[i] != '0')
result.append(NUM_C[10]);
break;
}
case 3://百
if(strNum[i] != '0')
result.append(NUM_C[11]);
break;
}
}
if(dotPos == -1){
result.append("元整");
}else{
result.append("元");
if(strNum.size() - length > 1){
result.append(NUM_C[strNum.at(length + 1).toLatin1() - '0']).append("角");
}
if(strNum.size() - length > 2){
result.append(NUM_C[strNum.at(length + 2).toLatin1() - '0']).append("分");
}
}
}
return result;
}
void PayTableModel::printPreview(QPrinter* printer)
{
QPainter painter(printer);
qreal cellWidth = printer->width() / 9; //excel显示宽度72像素
qreal cellHeight = cellWidth / 72 * 20; //excel显示高度20像素
qreal offsetHeight = 0;
QFont font("宋体");
font.setPointSize(11); //设置字体大小为11
painter.setFont(font);
painter.drawText(cellWidth * 4, cellHeight * 3 - cellHeight * 0.4, Database::getUserNameById(userId)); //户主
painter.drawText(cellWidth * 6, cellHeight * 3 - cellHeight * 0.4, Database::getUserAddressById(userId)); //地址
qreal allMoney = 0.0;
QString info = QString("%1:").arg(Database::getUserNameById(userId));
int curRow = 5;
for(int i = 0; i < datas.size() - 1; i++){
if(i > 0){
offsetHeight = cellHeight / 2;
}
if(datas.at(i).at(5).toDouble() > 0){
info.append(datas.at(i).at(0));
info.append(datas.at(i).at(5));
info.append(" ");
}
allMoney += datas.at(i).at(5).toDouble();
painter.drawText(cellWidth * 1, cellHeight * curRow + offsetHeight, datas.at(i).at(0)); //收费项目
painter.drawText(cellWidth * 2, cellHeight * curRow + offsetHeight, datas.at(i).at(1)); //上次读数
painter.drawText(cellWidth * 3, cellHeight * curRow + offsetHeight, datas.at(i).at(2)); //本次读数
painter.drawText(cellWidth * 4, cellHeight * curRow + offsetHeight, datas.at(i).at(3)); //缴费量
painter.drawText(cellWidth * 5 - cellWidth * 0.3, cellHeight * curRow + offsetHeight, datas.at(i).at(4)); //单价
painter.drawText(cellWidth * 6 - cellWidth * 0.3, cellHeight * curRow + offsetHeight, datas.at(i).at(5)); //金额
painter.drawText(cellWidth * 7 - cellWidth * 0.5, cellHeight * curRow + offsetHeight, datas.at(i).at(6)); //日期
curRow++;
}
painter.drawText(cellWidth * 5, cellHeight * 14, "实付:"); //实付
painter.drawText(cellWidth * 6, cellHeight * 14, QString("%1 元").arg(allMoney)); //实付
painter.drawText(cellWidth * 4, cellHeight * 18, upperMoney(allMoney)); //金额大写
painter.drawText(cellWidth * 5 - cellWidth * 0.5, cellHeight * 20, "樊"); //收费单位
//收入
Database::income(allMoney, info);
}
void PayDialog::on_btnboxPay_accepted()
{
//设置打印机
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setPageOrientation(QPageLayout::Portrait);
QPagedPaintDevice::Margins m;
m.left = m.right = 17.8;
m.top = m.bottom = 19.1;
printer.setMargins(m);
//生成预览
// QPrintPreviewDialog previewDialog(&printer, this);
// connect(&previewDialog, SIGNAL(paintRequested(QPrinter*)),
// payTableModel, SLOT(printPreview(QPrinter*)));
// previewDialog.exec();
//直接打印
QPrintDialog printDialog(&printer, this);
if(printDialog.exec() == QDialog::Accepted){
payTableModel->printPreview(&printer);
}
}