-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewitemorder.cpp
385 lines (355 loc) · 12.5 KB
/
newitemorder.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
#include "newitemorder.h"
#include "ui_newitemorder.h"
NewItemOrder::NewItemOrder(QWidget* parent)
: QWidget(parent)
, ui(new Ui::NewItemOrder)
{
ui->setupUi(this);
setAttribute(Qt::WA_QuitOnClose, false);
setAttribute(Qt::WA_DeleteOnClose, true);
setWindowTitle(QString("发起新入库账目"));
dowhat = ONADDNEWORDER;
ui->itemTable->setColumnCount(6);
QStringList titles;
titles << QString("pid") << QString("品类") << QString("名称") << QString("型号") << QString("数量") << QString("备注");
ui->itemTable->setHorizontalHeaderLabels(titles);
initResItems();
flushBox(1);
messenger=new Messenger();
connect(messenger,&Messenger::gotResponse,this,&NewItemOrder::afterConfirmReply);
ui->resBox->setNotMustHint(true);
ui->nameBox->setNotMustHint(true);
ui->typeBox->setNotMustHint(true);
}
NewItemOrder::~NewItemOrder()
{
delete ui;
}
void NewItemOrder::setOrder(OneResOrder order)
{
for (OneNewItem item : order.newItems) {
addOneItem(item);
}
ui->moreEdit->setText(order.more);
}
void NewItemOrder::setModel(OneResOrder order)
{
setOrder(order);
}
void NewItemOrder::changeOrder(OneResOrder* order) //修改订单 窗口
{
dowhat = ONCHANGEORDER;
theOrder = order;
setOrder(*order);
QString title = QString("修改账目 编号:") + QString::number(order->id);
setWindowTitle(title);
}
bool NewItemOrder::showOrder(int id) //展示订单 窗口
{
QVector<OneResOrder>& orders=dealResOrders;
OneResOrder order;
order.id = id;
QVector<OneResOrder>::iterator it = std::lower_bound(orders.begin(), orders.end(), order);
if (it == orders.end() || it->id != id) {
return false;
}
showOrder(*it);
return true;
}
void NewItemOrder::showOrder(OneResOrder order) //展示订单 窗口
{
dowhat = ONSHOWORDER;
QLayoutItem* child;
while ((child = ui->Layout_add->itemAt(0)) != nullptr) {
ui->Layout_add->removeItem(child);
delete child->widget();
delete child;
child = nullptr;
}
while ((child = ui->Layout_change->itemAt(0)) != nullptr) {
ui->Layout_change->removeItem(child);
delete child->widget();
delete child;
child = nullptr;
}
while ((child = ui->Layout_conferm->itemAt(0)) != nullptr) {
ui->Layout_conferm->removeItem(child);
delete child->widget();
delete child;
child = nullptr;
}
ui->moreEdit->setReadOnly(true);
setOrder(order);
QLabel* label[2];
label[0]=new QLabel(QString("入库时间:"));
ui->Layout_add->addWidget(label[0],0,0);
label[1]=new QLabel(order.starttime.toString(QString("yyyy年MM月dd日")));
ui->Layout_add->addWidget(label[1],0,1);
QString title = QString("当前账目 编号:") + QString::number(order.id);
setWindowTitle(title);
}
/*----------辅助函数--------------*/
void NewItemOrder::addOneItem(OneNewItem item)
{
QString pids=QString::number(item.pid);
QTableWidgetItem* pid = new QTableWidgetItem(pids);
QTableWidgetItem* res = new QTableWidgetItem(item.res);
QTableWidgetItem* name = new QTableWidgetItem(item.name);
QTableWidgetItem* type = new QTableWidgetItem(item.type);
QString u = QString::number(item.number) + " " + item.units;
QTableWidgetItem* num = new QTableWidgetItem(u);
QTableWidgetItem* more = new QTableWidgetItem(item.more);
pid->setFlags(pid->flags() & (~Qt::ItemIsEditable));
res->setFlags(res->flags() & (~Qt::ItemIsEditable));
name->setFlags(name->flags() & (~Qt::ItemIsEditable));
type->setFlags(type->flags() & (~Qt::ItemIsEditable));
num->setFlags(num->flags() & (~Qt::ItemIsEditable));
more->setFlags(more->flags() & (~Qt::ItemIsEditable));
int row = ui->itemTable->rowCount();
ui->itemTable->setRowCount(row + 1);
ui->itemTable->setItem(row, 0, pid);
ui->itemTable->setItem(row, 1, res);
ui->itemTable->setItem(row, 2, name);
ui->itemTable->setItem(row, 3, type);
ui->itemTable->setItem(row, 4, num);
ui->itemTable->setItem(row, 5, more);
}
void NewItemOrder::flushBox(int index)
{
if (index == 1) {
QStringList sl = getResList();
ui->resBox->setStringList(sl);
return;
} else if (index == 2) {
QString res = ui->resBox->currentText();
QStringList sl = getNameList(res);
ui->nameBox->setStringList(sl);
return;
} else if (index == 3) {
QString res = ui->resBox->currentText();
QString name = ui->nameBox->currentText();
QStringList sl = getTypeList(res, name);
ui->typeBox->setStringList(sl);
}
}
/*----------辅助函数end----------*/
/*-------按钮响应---------*/
void NewItemOrder::on_addItem_clicked()
{
QString nums = ui->numEdit->text();
double num = nums.toDouble();
if (num <= 0) {
QMessageBox::information(nullptr, QString("错误"), QString("请正确填入物品数量"));
return;
}
QString res = ui->resBox->currentText();
QString name = ui->nameBox->currentText();
QString type = ui->typeBox->currentText();
QString units = ui->unitsEdit->text();
if(res.length()<=0 || name.length()<=0 || type.length()<=0 || units.length()<=0)
{
QMessageBox::information(nullptr, QString("错误"), QString("请正确填入物品信息"));
return;
}
bool flag=false;
for(QString s : config.itemsList)
{
if(s==res)
{
flag=true;
break;
}
}
if(!flag)
{
QMessageBox::information(nullptr, QString("错误"), QString("不存在名为%0的物品类型").arg(res));
return;
}
if(ui->itemMoreEdit->text().size()>=config.MaxItemMoreSize)
{
QMessageBox::information(nullptr, QString("错误"), QString("备注过长,请勿超过%0个字").arg(config.MaxItemMoreSize));
return;
}
OneNewItem item;
int pid = getResItemPid(res, name, type);
item.isNew=pid==0;
item.pid = pid;
item.number = num;
item.more = ui->itemMoreEdit->text();
item.res=res;
item.name=name;
item.type=type;
item.units=units;
addOneItem(item);
}
void NewItemOrder::on_changeItem_clicked()
{
int row = ui->itemTable->currentRow();
QString res = ui->itemTable->item(row, 1)->text();
QString name = ui->itemTable->item(row, 2)->text();
QString type = ui->itemTable->item(row, 3)->text();
QString num = ui->itemTable->item(row, 4)->text().split(" ")[0];
QString units = ui->itemTable->item(row, 4)->text().split(" ")[1];
QString more = ui->itemTable->item(row, 5)->text();
ui->resBox->setCurrentText(res);
ui->nameBox->setCurrentText(name);
ui->typeBox->setCurrentText(type);
ui->numEdit->setText(num);
ui->itemMoreEdit->setText(more);
ui->unitsEdit->setText(units);
int rowCnt = ui->itemTable->rowCount();
ui->itemTable->removeRow(row);
ui->itemTable->setRowCount(rowCnt - 1);
}
void NewItemOrder::on_deleteItem_clicked()
{
int row = ui->itemTable->rowCount();
int cr = ui->itemTable->currentRow();
ui->itemTable->removeRow(cr);
ui->itemTable->setRowCount(row - 1);
}
void NewItemOrder::on_resBox_currentTextChanged(const QString& arg1)
{
flushBox(2);
}
void NewItemOrder::on_nameBox_currentTextChanged(const QString& arg1)
{
flushBox(3);
}
void NewItemOrder::on_typeBox_currentTextChanged(const QString& arg1)
{
QString u = getUnits(ui->resBox->currentText(), ui->nameBox->currentText(), ui->typeBox->currentText());
if (u != nullptr) {
ui->unitsEdit->setText(u);
}
}
void NewItemOrder::on_confirm_clicked()
{
if (dowhat != ONADDNEWORDER && dowhat != ONCHANGEORDER)
return;
QJsonObject json;
QJsonObject orderInf;
QJsonObject userInf;
userInf.insert("username", thisUser.username);
userInf.insert("password", thisUser.password);
if (dowhat == ONCHANGEORDER)
orderInf.insert("id", QJsonValue(theOrder->id));
if (dowhat == ONADDNEWORDER)
orderInf.insert("starttime", QJsonValue(QDate::currentDate().toString("yyyy-MM-dd")));
if(ui->moreEdit->toPlainText().size()>=config.MaxOrderMoreSize)
{
QMessageBox::information(nullptr, QString("错误"), QString("备注过长,请勿超过%0个字").arg(config.MaxOrderMoreSize));
return;
}
orderInf.insert("more", QJsonValue(ui->moreEdit->toPlainText()));
QJsonArray items;
int row = ui->itemTable->rowCount();
for (int i = 0; i < row; i++) {
QJsonObject item;
int pid=ui->itemTable->item(i, 0)->text().toInt();
int tmpNew=(pid==0)? 1 : 0;
item.insert("isNew",QJsonValue(tmpNew));
item.insert("pid", QJsonValue(pid));
item.insert("res", QJsonValue(ui->itemTable->item(i, 1)->text()));
item.insert("name", QJsonValue(ui->itemTable->item(i, 2)->text()));
item.insert("type", QJsonValue(ui->itemTable->item(i, 3)->text()));
QString s = ui->itemTable->item(i, 4)->text().split(" ")[0];
item.insert("cnt", QJsonValue(s.toDouble()));
s = ui->itemTable->item(i, 4)->text().split(" ")[1];
item.insert("units", QJsonValue(s));
item.insert("more", QJsonValue(ui->itemTable->item(i, 5)->text()));
items.append(QJsonValue(item));
}
if (row == 0) {
QMessageBox::StandardButton result = QMessageBox::question(nullptr, QString("确定?"), QString("订单物品栏为空,确定继续?"));
if (result != QMessageBox::Yes)
return;
}
json.insert("userInformation", QJsonValue(userInf));
json.insert("orderInformation", QJsonValue(orderInf));
json.insert("itemsInformation", QJsonValue(items));
if (dowhat == ONADDNEWORDER)
messenger->newResOrder(json);
else if (dowhat == ONCHANGEORDER)
messenger->changeResOrder(json);
else
return;
ui->confirm->setDisabled(true);
}
void NewItemOrder::on_cancel_clicked()
{
QMessageBox::StandardButton result = QMessageBox::question(nullptr, QString("确定?"), QString("是否放弃本订单"));
if (result == QMessageBox::Yes)
this->close();
return;
}
void NewItemOrder::on_button_readExcel_clicked()
{
QString filename=QFileDialog::getOpenFileName(nullptr, QString("选择文件"), "/", QString("Exel file(*.xlsx)"));
if(!(filename!=nullptr && filename.endsWith("xlsx")))
return ;
QXlsx::Document xlsxx(filename);
xlsxx.selectSheet("Sheet1");
QXlsx::CellRange range;
int rowCount = xlsxx.dimension().rowCount();//获取行数
int colCount = xlsxx.dimension().columnCount();
if(colCount!=6)
{
QMessageBox::warning(nullptr,QString("错误"),QString("文件格式有误,无法读取!"));
return;
}
OneNewItem item;
int pid,successNum=0,Num=rowCount-1;
for(int i = 2; i <= rowCount; i ++)
{
//qDebug()<<QString::number(i)<<xlsxx.read(i,1);
item.res=xlsxx.read(i,1).toString();
item.name=xlsxx.read(i,2).toString();
item.type=xlsxx.read(i,3).toString();
item.number=xlsxx.read(i,4).toDouble();
item.units=xlsxx.read(i,5).toString();
item.more=xlsxx.read(i,6).toString();
pid = getResItemPid(item.res, item.name, item.type);
item.isNew=pid==0;
item.pid = pid;
if (item.number <= 0) continue;
if(item.res.length()<=0 || item.name.length()<=0 || item.type.length()<=0 || item.units.length()<=0)
continue;
if(item.more.size()>=config.MaxItemMoreSize)
continue;
bool flag=false;
for(QString s : config.itemsList)
{
if(s==item.res)
{
flag=true;
break;
}
}
if(!flag)
continue;
addOneItem(item);
successNum++;
}
QMessageBox::information(nullptr,QString("完成"),QString("成功导入%0个,失败%1个").arg(successNum).arg(Num-successNum));
return;
}
/*-------按钮响应end-------*/
/*-------上传函数-------*/
void NewItemOrder::afterConfirmReply(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError) {
if (dowhat == ONADDNEWORDER)
QMessageBox::information(nullptr, QString("完成"), QString("发起账目成功"));
else
QMessageBox::information(nullptr, QString("完成"), QString("修改账目成功"));
reply->deleteLater();
this->close();
emit finishChange();
} else {
QMessageBox::warning(nullptr, QString("错误"), QString("失败,请稍后重试"));
reply->deleteLater();
ui->confirm->setDisabled(false);
}
}
/*-------上传函数end-------*/