-
Notifications
You must be signed in to change notification settings - Fork 3
/
stock.cpp
277 lines (236 loc) · 7.16 KB
/
stock.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
/*
* Copyright 2006-2013, Clemens Zeidler, [email protected]
* Distributed under the terms of the MIT License.
*/
#include "stock.h"
#include <Message.h>
#define DEBUG 1
#include <Debug.h>
basket_f::basket_f(time_t d)
{
date = d;
sum = 0;
saved = false;
}
basket_f::~basket_f()
{
trade_info* trade;
for (int k = 0; ; k++) {
trade = (trade_info*)trades.ItemAt(k);
if (trade == NULL)
break;
delete trade;
}
}
trade_info::trade_info()
{
state = TRADE_STATE_NONE;
}
product_f::product_f()
{
barcode = "";
prize = 0;
supplies = 0;
valid = true;
}
void
product_f::Archive(BMessage *msg)
{
msg->AddInt32("id", id);
msg->AddString("name", name);
msg->AddString("barcode", barcode);
msg->AddString("comment", comment);
msg->AddFloat("prize", prize);
msg->AddInt32("supplies", supplies);
msg->AddBool("valid", valid);
msg->AddInt64("insertdate",(int64)insertdate);
}
void
product_f::Instantiate(BMessage *msg)
{
msg->FindInt32("id", &id);
msg->FindString("name", &name);
msg->FindString("barcode", &barcode);
msg->FindString("comment", &comment);
msg->FindFloat("prize", &prize);
msg->FindInt32("supplies", &supplies);
msg->FindBool("valid", &valid);
int64 insertwhen;
msg->FindInt64("insertdate", &insertwhen);
insertdate = insertwhen;
}
Stock::Stock(BString filename)
{
fDatabase = new SQLiteConnection(filename.String());
// set up tables. This shouldn't harm already existing tables.
// products:
fDatabase->Exec("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, barcode TEXT, comment TEXT, prize FLOAT, supplies INTEGER, valid INTEGER, insertdate DATE )");
fDatabase->Exec("CREATE INDEX products_id_index ON products (id)");
fDatabase->Exec("CREATE INDEX products_name_index ON products (name)");
fDatabase->Exec("CREATE INDEX products_barcode_index ON products (barcode)");
fDatabase->Exec("CREATE INDEX products_comment_index ON products (comment)");
fDatabase->Exec("CREATE INDEX products_prize_index ON products (prize)");
fDatabase->Exec("CREATE INDEX products_supplies_index ON products (supplies)");
fDatabase->Exec("CREATE INDEX products_valid_index ON products (valid)");
fDatabase->Exec("CREATE INDEX products_insertdate_index ON products (insertdate)");
// trades:
fDatabase->Exec("CREATE TABLE trades (id integer primary key, productid integer, prize float, date datetime, number integer)");
fDatabase->Exec("CREATE INDEX trades_id_index ON trades (id)");
fDatabase->Exec("CREATE INDEX trades_productid_index ON trades (productid)");
fDatabase->Exec("CREATE INDEX trades_prize_index ON trades (prize)");
fDatabase->Exec("CREATE INDEX trades_date_index ON trades (date)");
fDatabase->Exec("CREATE INDEX trades_number_index ON trades (number)");
}
Stock::~Stock()
{
delete fDatabase;
}
int32
Stock::AddProduct(product_f &product)
{
fLock.Lock();
BString query = "INSERT INTO products (name, barcode, comment, prize, supplies, valid, insertdate) VALUES ('";
query += product.name; query += "','";
query += product.barcode; query += "','";
query += product.comment; query += "','";
query << product.prize; query += "','";
query << product.supplies; query += "','";
if (product.valid)
query << "1','";
else
query << "0','";
query << product.insertdate;
query += "');";
PRINT(("query string %s\n", query.String() ));
fDatabase->Exec(query.String());
fDatabase->Exec("COMMIT");
query = "SELECT id FROM products WHERE barcode='";
query += product.barcode;
query += "';";
fDatabase->Exec(query.String());
int32 productId = -1;
if (fDatabase->NumRows() >= 0)
productId = atoi(fDatabase->GetValue(0,"id").c_str());
fLock.Unlock();
return productId;
}
void
Stock::UpdateProduct(uint id, product_f &product)
{
fLock.Lock();
BString query = "UPDATE products ";
query += "SET name='"; query += product.name; query +="' ";
query += ", barcode='"; query += product.barcode; query +="' ";
query += ", comment='"; query += product.comment; query +="' ";
query += ", prize='"; query << product.prize; query +="' ";
query += ", supplies='"; query << product.supplies; query +="' ";
query += ", valid='";
if (product.valid)
query << "1'";
else
query << "0'";
query += ", insertdate='"; query << product.insertdate; query +="' ";
query += "WHERE id="; query << id;
fDatabase->Exec(query.String());
fDatabase->Exec("COMMIT");
fLock.Unlock();
}
int32
Stock::AddTrade(trade_f &trade)
{
fLock.Lock();
BString query = "INSERT INTO trades (productid, prize, date, number) VALUES ('";
query << trade.productid; query += "','";
query << trade.prize; query += "','";
query << trade.date; query += "','";
query << trade.number;
query += "');";
fDatabase->Exec(query.String());
fDatabase->Exec("COMMIT");
query = "SELECT id FROM trades WHERE productid='";
query << trade.productid;
query += "';";
fDatabase->Exec(query.String());
int32 tradeId = -1;
if(fDatabase->NumRows() >= 0){
tradeId = atoi(fDatabase->GetValue(0,"id").c_str());
}
PRINT(("new trade id %i\n", tradeId));
fLock.Unlock();
return tradeId;
}
void
Stock::UpdateTrade(uint id, trade_f &trade)
{
fLock.Lock();
BString query = "UPDATE trades ";
query += "SET productid='"; query << trade.productid; query +="' ";
query += ", prize='"; query << trade.prize; query +="' ";
query += ", date='"; query << trade.date; query +="' ";
query += ", number='"; query << trade.number; query +="' ";
query += "WHERE id="; query << id;
fDatabase->Exec(query.String());
fDatabase->Exec("COMMIT");
fLock.Unlock();
}
void
Stock::RemoveTrade(uint id)
{
fLock.Lock();
BString query = "DELETE FROM trades WHERE id=";
query << id;
fDatabase->Exec(query.String());
fDatabase->Exec("COMMIT");
fLock.Unlock();
}
status_t
Stock::GetProducts(BList* list)
{
list->MakeEmpty();
fLock.Lock();
BString query = "SELECT * FROM products";
fDatabase->Exec(query.String());
for (int i = 0; i < fDatabase->NumRows(); i++) {
product_f *product = new product_f;
list->AddItem(product);
product->id = atoi(fDatabase->GetValue(i,0).c_str());
product->name = fDatabase->GetValue(i,1).c_str();
product->barcode = fDatabase->GetValue(i,2).c_str();
product->comment = fDatabase->GetValue(i,3).c_str();
product->prize = atof(fDatabase->GetValue(i,4).c_str());
product->supplies = atoi(fDatabase->GetValue(i,5).c_str());
product->valid = atoi(fDatabase->GetValue(i,6).c_str());
product->insertdate = atoi(fDatabase->GetValue(i,7).c_str());
}
fLock.Unlock();
return B_OK;
}
status_t
Stock::GetTrades(BList* list)
{
fLock.Lock();
BString query = "SELECT * FROM trades";
fDatabase->Exec(query.String());
BString result;
for (int i = 0; i < fDatabase->NumRows(); i++) {
trade_f *trade = new trade_f;
list->AddItem(trade);
trade->id = atoi(fDatabase->GetValue(i,0).c_str());
trade->productid = atoi(fDatabase->GetValue(i,1).c_str());
trade->prize = atof(fDatabase->GetValue(i,2).c_str());
trade->date = atoi(fDatabase->GetValue(i,3).c_str());
trade->number = atoi(fDatabase->GetValue(i,4).c_str());
}
fLock.Unlock();
return B_OK;
}
status_t
Stock::ResetProductsCount()
{
fLock.Lock();
BString query = "UPDATE products SET supplies=0";
fDatabase->Exec(query.String());
status_t status = fDatabase->Exec("COMMIT");
fLock.Unlock();
return status;
}