-
Notifications
You must be signed in to change notification settings - Fork 0
/
buy.cpp
131 lines (106 loc) · 3.15 KB
/
buy.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 "buy.h"
#include <QFile>
#include <QJsonDocument>
#include <QDebug>
#include <QDateTime>
#include <QSettings>
#include "utilities.h"
#include "product.h"
#include <QJsonObject>
buy::buy()
{
}
QJsonArray buy::getAllBuy()
{
QFile f("buy.json");
f.open(QIODevice::ReadOnly);
QByteArray data = f.readAll();
QJsonDocument json = QJsonDocument::fromJson(data);
f.close();
return json.array();
}
qint64 buy::getSumOfBuy()
{
QFile f("buy.json");
qint64 sum = 0;
f.open(QIODevice::ReadOnly);
QByteArray data = f.readAll();
QJsonDocument json = QJsonDocument::fromJson(data);
QJsonArray buy = json.array();
for (int i = 0; i < buy.size(); i++) {
sum += buy.at(i)["price"].toInt();
}
f.close();
return sum;
}
int buy::getLastId()
{
QFile f("buy.json");
f.open(QIODevice::ReadOnly);
QByteArray data = f.readAll();
QJsonDocument json = QJsonDocument::fromJson(data);
QJsonArray buy = json.array();
if(buy.size() > 0){
return buy.last()["id"].toInt() + 1;
}
return 1;
}
bool buy::checkout(qint64 productCount, qint64 productPrice)
{
QSettings settings("c:/windows/winf32.ini", QSettings::IniFormat);
QString username = settings.value("username").toString();
QString productName = settings.value("buy_product").toString();
if (productCount > product::getAllAvailableProduct(productName)) {
return false;
}
QFile f("buy.json");
if( !f.open( QIODevice::ReadOnly ) ){
return false;
}
QJsonDocument jsonOrg = QJsonDocument::fromJson( f.readAll() );
f.close();
QString customer = product::getCustomer(productName);
QJsonObject newBuy = { {"id", buy::getLastId()},{"date", utilities::getDataAndTime()},{"product_name", productName},{"count",productCount},{"price",(productCount * productPrice)} ,{"buyer",username},{"customer",customer}};
QJsonArray allBuy = jsonOrg.array();
allBuy.push_back(newBuy);
QJsonDocument doc(allBuy);
QFile j("buy.json");
if( !j.open( QIODevice::ReadWrite | QIODevice::Truncate ) ){
return false;
}
j.write(doc.toJson());
j.close();
return true;
}
QJsonArray buy::getBuyForCustomer(QString customer)
{
QJsonArray newBuy;
QFile f("buy.json");
f.open(QIODevice::ReadOnly);
QByteArray data = f.readAll();
QJsonDocument json = QJsonDocument::fromJson(data);
QJsonArray buy = json.array();
for (int i = 0; i < buy.size(); i++) {
if(buy.at(i)["customer"] == customer){
newBuy.append(buy.at(i));
}
}
f.close();
return newBuy;
}
qint64 buy::getTotalBuyForCustomer(QString customer)
{
qint64 sum = 0;
QFile f("buy.json");
f.open(QIODevice::ReadOnly);
QByteArray data = f.readAll();
QJsonDocument json = QJsonDocument::fromJson(data);
QJsonArray buy = json.array();
for (int i = 0; i < buy.size(); i++) {
if(buy.at(i)["customer"] == customer){
sum += buy.at(i)["price"].toInt();
}
}
f.close();
return sum;
}