-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelSale.cpp
174 lines (155 loc) · 4.81 KB
/
ModelSale.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
#include "ModelSale.h"
ModelSale::ModelSale(QObject *parent)
: QAbstractTableModel(parent)
{
}
QVariant ModelSale::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::TextAlignmentRole){
return Qt::AlignCenter;
}
if (role != Qt::DisplayRole)
return QVariant();
if(orientation == Qt::Horizontal){
QString title;
switch(section){
case ColumnHeaderDate:
title = u8"日期";
break;
case ColumnHeaderProductName:
title = u8"商品名";
break;
case ColumnHeaderTotalPrice:
title = u8"总价格";
break;
case ColumnHeaderSaleCount:
title = u8"销售数量";
break;
case ColumnHeaderRemark:
title = u8"备注";
break;
}
return title;
}
return QString();
}
int ModelSale::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return (int)sales_.size();
}
int ModelSale::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return ColumnHeaderCount;
}
QVariant ModelSale::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if(role == Qt::TextAlignmentRole){
return Qt::AlignCenter;
}
if(role != Qt::DisplayRole){
return QVariant();
}
QVariant ret;
auto& sale = sales_[index.row()];
switch (index.column()) {
case ColumnHeaderDate:
ret = sale->date;
break;
case ColumnHeaderProductName:
if(sale->product){
ret = sale->product->name;
}else{
ret = sale->product_name;
}
break;
case ColumnHeaderTotalPrice:
ret = sale->total_price;
break;
case ColumnHeaderSaleCount:
ret = sale->count;
break;
case ColumnHeaderRemark:
ret = sale->remark;
break;
default:
break;
}
return ret;
}
void ModelSale::sort(int column, Qt::SortOrder order)
{
beginResetModel();
switch (column) {
case ColumnHeaderDate:
std::sort(sales_.begin(),sales_.end(),[order](const SalePointer& item1,const SalePointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->date < item2->date) : (item1->date > item2->date);
});
break;
case ColumnHeaderProductName:
std::sort(sales_.begin(),sales_.end(),[order](const SalePointer& item1,const SalePointer& item2)->bool{
if(item1->product && item2->product){
return order == Qt::AscendingOrder ? (item1->product->name < item2->product->name) : (item1->product->name > item2->product->name);
}else{
return order == Qt::AscendingOrder ? (item1->product_name < item2->product_name) : (item1->product_name > item2->product_name);
}
});
break;
case ColumnHeaderTotalPrice:
std::sort(sales_.begin(),sales_.end(),[order](const SalePointer& item1,const SalePointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->total_price < item2->total_price) : (item1->total_price > item2->total_price);
});
break;
case ColumnHeaderSaleCount:
std::sort(sales_.begin(),sales_.end(),[order](const SalePointer& item1,const SalePointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->count < item2->count) : (item1->count > item2->count);
});
break;
case ColumnHeaderRemark:
std::sort(sales_.begin(),sales_.end(),[order](const SalePointer& item1,const SalePointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->remark < item2->remark) : (item1->remark > item2->remark);
});
break;
default:
break;
}
endResetModel();
}
std::pair<double,double> ModelSale::GetTotalSales()
{
std::pair<double,double> result;
//总成本
result.first = std::accumulate(sales_.begin(),sales_.end(),0,[](double sum,SalePointer& sale)->double{
return sum+sale->product->cost_price*sale->count;
});
//总费用
result.second = std::accumulate(sales_.begin(),sales_.end(),0,[](double sum,SalePointer& sale)->double{
return sum+sale->total_price;
});
return result;
}
SalePointer ModelSale::GetSale(const QModelIndex &index)
{
return sales_[index.row()];
}
void ModelSale::SetSales(std::vector<SalePointer>& sales)
{
beginResetModel();
sales_ = sales;
endResetModel();
}
void ModelSale::RefreshItem(const QModelIndex &index,SalePointer& item)
{
sales_[index.row()] = item;
}
void ModelSale::DeleteItem(const QModelIndex &index)
{
beginRemoveRows(index.parent(),index.row(),index.row());
sales_.erase(sales_.begin()+index.row());
endRemoveRows();
}