-
Notifications
You must be signed in to change notification settings - Fork 3
/
fairtrade.cpp
629 lines (550 loc) · 16.1 KB
/
fairtrade.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
* Copyright 2006-2013, Clemens Zeidler, [email protected]
* Distributed under the terms of the MIT License.
*/
#include "fairtrade.h"
#include <Roster.h>
#include <Entry.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <Path.h>
#include <image.h>
#include <File.h>
#include <unistd.h>
#define DEBUG 1
#include <Debug.h>
TradeApp::TradeApp(char *signature)
:
BApplication(signature)
{
//read config
BPath path;
if (find_directory (B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append ("fairtrade");
BPath configP(path.Path());
configP.Append(kConfigFile);
fConfig.ReadConfig(configP.Path());
BPath dbP;
if(fConfig.databasePath.FindFirst("/") != 0){
//relative path
dbP.SetTo(path.Path());
dbP.Append(fConfig.databasePath);
} else
dbP.SetTo(fConfig.databasePath);
//create dir if not exist
BString dbDir(dbP.Path());
dbDir.Truncate(dbDir.IFindLast("/"));
BDirectory dir;
dir.CreateDirectory(dbDir.String(), &dir);
//set export path
fExportPath = fConfig.csvExportPath;
if(fExportPath.FindFirst("/") != 0){
//relative path
fExportPath = path.Path();
fExportPath+= fConfig.csvExportPath;
}
HideCursor();
fStock = new Stock(dbP.Path());
fMainWindow = new MainWindow(BRect(100, 300, 400, 700), "Fair Trade", fConfig);
fMainWindowMessenger = new BMessenger(fMainWindow);
fProducts = new BList;
fTrades = new BList;
fBaskets = new BList;
fMainWindow->fSellView->SetBasketsList(fBaskets);
fSellNewProduct = false;
fMainWindow->Show();
//read data:
product_f *prod;
fStock->GetProducts(fProducts);
BMessenger messenger(fMainWindow);
for (int k = 0; ; k++) {
prod = (product_f*)fProducts->ItemAt(k);
if (prod == NULL)
break;
BMessage productmsg(ADD_PRODUCT);
productmsg.AddPointer("product", prod);
messenger.SendMessage(&productmsg);
}
fStock->GetTrades(fTrades);
trade_f *trade;
for (int k = 0; ; k++) {
trade = (trade_f*)fTrades->ItemAt(k);
if (trade == NULL)
break;
product_f *product = FindProduct(trade->productid);
if (product != NULL) {
basket_f *basket = FindBasket(trade->date);
if (basket == NULL) {
basket = new basket_f(trade->date);
basket->saved = true;
fBaskets->AddItem(basket);
}
basket->sum+= trade->prize * trade->number;
trade_info *tradeinfo = new trade_info;
tradeinfo->trade = trade;
tradeinfo->product = product;
tradeinfo->basket = basket;
tradeinfo->oldnumber = trade->number;
basket->trades.AddItem(tradeinfo);
}
}
}
TradeApp::~TradeApp()
{
//cleaning
delete fMainWindowMessenger;
delete fStock;
product_f *prod;
for (int k = 0; k < fProducts->CountItems(); k++) {
prod = (product_f*)fProducts->ItemAt(k);
delete prod;
}
delete fProducts;
trade_f *trade;
for (int k = 0; k < fTrades->CountItems(); k++) {
trade = (trade_f*)fTrades->ItemAt(k);
delete trade;
}
delete fTrades;
basket_f *basket;
for (int k = 0; k < fBaskets->CountItems(); k++) {
basket = (basket_f*)fBaskets->ItemAt(k);
delete basket;
}
delete fBaskets;
}
void
TradeApp::MessageReceived(BMessage *msg)
{
BDirectory dir;
BList list;
product_f product;
BString barcode;
int32 tradetime;
basket_f *basket;
int8 number;
trade_info *tradeInfo;
int32 arg_c;
char **arg_v;
int32 return_value;
thread_id exec_thread;
switch (msg->what) {
case UNVALID_PRODUCT:
product.Instantiate(msg);
product.valid = false;
InsertProductToDatabase(&product);
break;
case SAVE_PRODUCT:
product.Instantiate(msg);
InsertProductToDatabase(&product);
break;
case SELL_PRODUCT:
msg->FindString("barcode",&barcode);
msg->FindInt32("tradetime",&tradetime);
SellProduct(barcode,tradetime);
break;
case SAVE_TRADE:
msg->FindPointer("basket", (void**)&basket);
if (basket != NULL)
SaveBasket(basket);
break;
case UPDATE_TRADE:
msg->FindInt8("number",&number);
msg->FindPointer("tradeinfo", (void**)&tradeInfo);
UpdateTrade(tradeInfo, number);
break;
case REMOVE_TRADE:
msg->FindPointer("tradeinfo", (void**)&tradeInfo);
RemoveTrade(tradeInfo);
break;
case RESET_PRODUCTS_COUNT:
PRINT(("ResetProductsCount\n"));
fStock->ResetProductsCount();
for (int i = 0; i < fProducts->CountItems(); i++) {
product_f* product = (product_f*)fProducts->ItemAt(i);
product->supplies = 0;
BMessage msg(UPDATE_PRODUCT);
msg.AddPointer("product", product);
fMainWindowMessenger->SendMessage(&msg);
}
break;
case QUIT:
//create the export dir if not exist
dir.CreateDirectory(fExportPath.String(), &dir);
ExportTable();
if(fConfig.shutdownOnExit){
//shutdown
arg_c = 1;
arg_v = (char**)malloc(sizeof(char*) * (arg_c + 1));
BPath bin;
find_directory(B_SYSTEM_BIN_DIRECTORY, &bin);
bin.Append("shutdown");
arg_v[0] = strdup(bin.Path());
arg_v[1] = NULL;
exec_thread = load_image(arg_c, (const char**)arg_v, (const char**)environ);
free(arg_v);
wait_for_thread(exec_thread, &return_value);
PRINT(("shutdown\n"));
}
be_app->PostMessage(B_QUIT_REQUESTED);
break;
default:
BApplication::MessageReceived(msg);
}
}
void
TradeApp::InsertProductToDatabase(product_f *product)
{
product_f *prod = FindProduct(product->barcode);
if (prod != NULL ) {
fStock->UpdateProduct(prod->id, *product);
BMessage msg;
product->id = prod->id;
product->Archive(&msg);
UpdateProduct(&msg);
} else {
time(&(product->insertdate));
int32 productId = fStock->AddProduct(*product);
product->id = productId;
BMessage msg;
product->Archive(&msg);
AddProduct(&msg);
}
}
void
TradeApp::AddProduct(BMessage *archive)
{
product_f *product = new product_f;
product->Instantiate(archive);
fProducts->AddItem(product);
BMessage msg(ADD_PRODUCT);
msg.AddPointer("product",product);
fMainWindowMessenger->SendMessage(&msg);
}
void
TradeApp::UpdateProduct(BMessage *archive)
{
product_f product;
product.Instantiate(archive);
product_f *prod = FindProduct(product.barcode);
if (prod != NULL)
prod->Instantiate(archive);
BMessage msg(UPDATE_PRODUCT);
msg.AddPointer("product", prod);
fMainWindowMessenger->SendMessage(&msg);
}
void
TradeApp::SellProduct(BString barcode,time_t date)
{
product_f *prod = FindProduct(barcode);
if (prod == NULL) {
product_f product;
product.barcode = barcode;
product.prize = 0;
BWindow* editWindow = new EditProductWindow(BRect(150,200,700,700),
"edit product", &product, true,
fMainWindow->fSellView->InitTradeTime());
editWindow->Show();
return;
}
//check if not valid
if(!prod->valid){
prod->valid = true;
InsertProductToDatabase(prod);
}
basket_f *basket = FindBasket(date);
if (basket == NULL) {
basket = new basket_f(date);
fBaskets->AddItem(basket);
}
trade_info *oldTradeInfo = FindTrade(prod->id,basket);
if (oldTradeInfo == NULL) {
basket->sum+= prod->prize;
trade_f *trade = new trade_f;
trade->productid = prod->id;
trade->prize = prod->prize;
trade->date = date;
trade->number = 1;
fTrades->AddItem(trade);
trade_info *tradeinfo = new trade_info;
tradeinfo->trade = trade;
tradeinfo->product = prod;
tradeinfo->basket = basket;
tradeinfo->state = TRADE_STATE_ADD;
basket->trades.AddItem(tradeinfo);
BMessage msg(ADD_TRADE);
msg.AddPointer("tradeinfo",tradeinfo);
tradeinfo->basket->saved = false;
fMainWindowMessenger->SendMessage(&msg);
} else if(oldTradeInfo->state == TRADE_STATE_REMOVE) {
oldTradeInfo->state = TRADE_STATE_UPDATE;
oldTradeInfo->trade->number = 1;
BMessage msg(ADD_TRADE);
msg.AddPointer("tradeinfo",oldTradeInfo);
basket->sum+= prod->prize;
oldTradeInfo->basket->saved = false;
fMainWindowMessenger->SendMessage(&msg);
} else {
oldTradeInfo->trade->number+= 1;
BMessage msg(UPDATE_TRADE);
msg.AddPointer("tradeinfo", oldTradeInfo);
basket->sum+= prod->prize;
oldTradeInfo->basket->saved = false;
fMainWindowMessenger->SendMessage(&msg);
}
}
void
TradeApp::UpdateTrade(trade_info *tradeInfo, int32 number)
{
float prize = tradeInfo->trade->prize;
tradeInfo->basket->sum-=prize * tradeInfo->trade->number;
tradeInfo->basket->sum+=prize * number;
tradeInfo->trade->number = number;
if (tradeInfo->state != TRADE_STATE_ADD)
tradeInfo->state = TRADE_STATE_UPDATE;
tradeInfo->basket->saved = false;
BMessage msg(UPDATE_TRADE);
msg.AddPointer("tradeinfo", tradeInfo);
fMainWindowMessenger->SendMessage(&msg);
}
void
TradeApp::RemoveTrade(trade_info *tradeInfo)
{
tradeInfo->state = TRADE_STATE_REMOVE;
tradeInfo->basket->sum-= tradeInfo->trade->prize * tradeInfo->trade->number;
tradeInfo->basket->saved = false;
tradeInfo->trade->number = 0;
BMessage msg(UPDATE_BASKET);
fMainWindowMessenger->SendMessage(&msg);
}
product_f*
TradeApp::FindProduct(int32 id)
{
product_f *prod;
for (int k = 0; ; k++) {
prod = (product_f*)fProducts->ItemAt(k);
if (prod == NULL)
break;
if (prod->id == id)
return prod;
}
return NULL;
}
trade_info*
TradeApp::FindTrade(int32 productId, basket_f *basket)
{
trade_info *tradeInfo;
for (int k = 0; ; k++) {
tradeInfo = (trade_info*)basket->trades.ItemAt(k);
if (tradeInfo == NULL)
break;
if (tradeInfo->trade->productid == productId)
return tradeInfo;
}
return NULL;
}
product_f*
TradeApp::FindProduct(BString barcode)
{
product_f *prod;
for (int k = 0; ; k++) {
prod = (product_f*)fProducts->ItemAt(k);
if (prod == NULL)
break;
if (prod->barcode == barcode)
return prod;
}
return NULL;
}
basket_f*
TradeApp::FindBasket(time_t date)
{
basket_f *basket;
for (int k = 0; ; k++) {
basket = (basket_f*)fBaskets->ItemAt(k);
if (basket == NULL)
break;
if (basket->date == date)
return basket;
}
return NULL;
}
void
TradeApp::SaveBasket(basket_f *basket)
{
trade_info *info;
for (int k = 0; ; k++) {
info = (trade_info*)basket->trades.ItemAt(k);
if (info == NULL)
break;
product_f* product = info->product;
int32 deltaNumber;
switch (info->state) {
case TRADE_STATE_ADD:
info->trade->id = fStock->AddTrade(*(info->trade));
info->state = TRADE_STATE_NONE;
//update the product supplies
info->product->supplies-= info->trade->number;
info->oldnumber = info->trade->number;
fStock->UpdateProduct(info->trade->productid, *(info->product));
break;
case TRADE_STATE_UPDATE:
fStock->UpdateTrade(info->trade->id,*(info->trade));
deltaNumber = info->trade->number - info->oldnumber;
info->product->supplies-= deltaNumber;
info->oldnumber = info->trade->number;
fStock->UpdateProduct(info->trade->productid, *(info->product));
info->state = TRADE_STATE_NONE;
break;
case TRADE_STATE_REMOVE:
fStock->RemoveTrade(info->trade->id);
fTrades->RemoveItem(info->trade);
deltaNumber = info->trade->number - info->oldnumber;
info->product->supplies-= deltaNumber;
fStock->UpdateProduct(info->trade->productid, *(info->product));
delete info->trade;
basket->trades.RemoveItem(info);
delete info;
break;
}
basket->saved = true;
//update product list
BMessage msgUpdate(UPDATE_PRODUCT);
msgUpdate.AddPointer("product", product);
BMessage reply;
fMainWindowMessenger->SendMessage(&msgUpdate, &reply);
}
if (basket->trades.CountItems() == 0){
fBaskets->RemoveItem(basket);
delete basket;
}
}
void
TradeApp::ExportTable()
{
time_t timeNow;
time(&timeNow);
struct tm *structNow = localtime(&timeNow);
//printf("%d.%d.%d\n", structNow->tm_mday, structNow->tm_mon + 1, structNow->tm_year + 1900);
BString dateString = "";
dateString << structNow->tm_mday; dateString+= "_";
dateString << structNow->tm_mon + 1; dateString+= "_";
dateString << structNow->tm_year + 1900; dateString+= "_";
dateString << structNow->tm_hour; dateString+= "-";
dateString << structNow->tm_min;
//export products
BString productsFileName = fExportPath;
productsFileName+= "/Produkte_";
productsFileName+= dateString;
productsFileName+= ".cvs";
BFile productFile(productsFileName.String(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
BString lableString = "Barcode\tName\tKommentar\tPreis\tBestand\tEingefuegt\n";
productFile.Write(lableString.String(),lableString.Length());
product_f *prod;
for (int k = 0; ; k++) {
prod = (product_f*)fProducts->ItemAt(k);
if (prod == NULL)
break;
if (!prod->valid)
continue;
BString outString = "";
outString << prod->barcode; outString+= "\t";
outString << prod->name; outString+= "\t";
outString << prod->comment; outString+= "\t";
outString << prod->prize; outString+= "\t";
outString << prod->supplies; outString+= "\t";
struct tm *structInsertDate = localtime(&(prod->insertdate));
outString << structInsertDate->tm_mday; outString+= ".";
outString << structInsertDate->tm_mon + 1; outString+= ".";
outString << structInsertDate->tm_year + 1900; outString+= " ";
outString << structInsertDate->tm_hour; outString+= ":";
outString << structInsertDate->tm_min;
outString+= "\n";
productFile.Write(outString.String(),outString.Length());
}
//trade list
BString tradeSimpleFileName = fExportPath;
tradeSimpleFileName+= "/Verkaeufe_";
tradeSimpleFileName+= dateString;
tradeSimpleFileName+= ".cvs";
BFile tradeSimpleFile(tradeSimpleFileName.String(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
lableString = "Tag\tSumme\n";
tradeSimpleFile.Write(lableString.String(),lableString.Length());
BString tradeDetailFileName = fExportPath;
tradeDetailFileName+= "/Verkaeufe_Detailiert_";
tradeDetailFileName+= dateString;
tradeDetailFileName+= ".cvs";
BFile tradeDetailFile(tradeDetailFileName.String(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
lableString = "Datum\tProdukt\tBarcode\tVerkaufspreis\tAnzahl\n";
tradeDetailFile.Write(lableString.String(),lableString.Length());
basket_f *basket;
//neaded for simple list:
float daySum = 0;
int32 ydayOfTrade = -1;
int32 yearOfTrade = -1;
int32 dayOfTrade = -1;
int32 monthOfTrade = -1;
BString outSimpleString = "";
struct tm *time;
for (int k = 0; ; k++) {
basket = (basket_f*)fBaskets->ItemAt(k);
if (basket == NULL) {
if(dayOfTrade > 0){
outSimpleString = "";
outSimpleString << time->tm_mday; outSimpleString+= ".";
outSimpleString << time->tm_mon + 1; outSimpleString+= ".";
outSimpleString << time->tm_year + 1900; outSimpleString+= "\t";
outSimpleString << daySum; outSimpleString+= "\n";
tradeSimpleFile.Write(outSimpleString.String(),outSimpleString.Length());
}
break;
}
if (basket->saved) {
time = localtime(&(basket->date));
//simple trade list
if (k == 0) {
ydayOfTrade = time->tm_yday;
dayOfTrade = time->tm_mday;
monthOfTrade = time->tm_mon;
yearOfTrade = time->tm_year;
}
if (time->tm_yday == ydayOfTrade && time->tm_year == yearOfTrade)
daySum+= basket->sum;
else {
outSimpleString = "";
outSimpleString << dayOfTrade; outSimpleString+= ".";
outSimpleString << monthOfTrade + 1; outSimpleString+= ".";
outSimpleString << yearOfTrade + 1900; outSimpleString+= "\t";
outSimpleString << daySum; outSimpleString+= "\n";
tradeSimpleFile.Write(outSimpleString.String(),outSimpleString.Length());
ydayOfTrade = time->tm_yday;
dayOfTrade = time->tm_mday;
monthOfTrade = time->tm_mon;
yearOfTrade = time->tm_year;
daySum = basket->sum;
}
//detail trade list
BString outDetailString = "";
trade_info *tradeInfo;
for (int k = 0; ; k++) {
tradeInfo = (trade_info*)basket->trades.ItemAt(k);
if (tradeInfo == NULL)
break;
outDetailString << time->tm_mday; outDetailString+= ".";
outDetailString << time->tm_mon + 1; outDetailString+= ".";
outDetailString << time->tm_year + 1900; outDetailString+= " ";
outDetailString << time->tm_hour; outDetailString+= ":";
outDetailString << time->tm_min; outDetailString+= "\t";
outDetailString << tradeInfo->product->name; outDetailString+= "\t";
outDetailString << tradeInfo->product->barcode; outDetailString+= "\t";
outDetailString << tradeInfo->trade->prize; outDetailString+= "\t";
outDetailString << tradeInfo->trade->number; outDetailString+= "\n";
}
outDetailString+= "Summe\t";
outDetailString << basket->sum;
outDetailString+="\n\n";
tradeDetailFile.Write(outDetailString.String(),outDetailString.Length());
}
}
}