-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_cat.c
executable file
·2188 lines (1641 loc) · 74.5 KB
/
inventory_cat.c
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// inventory_cat.c
// Copyright 2010-2014 Michael Rajotte <[email protected]>
// Creating item categories and adding items to them.
#include <mysql.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <time.h>
#include <gdk/gdkkeysyms.h>
#include "settings.h"
#include "messages.h"
#include "inventory_cat_structs.h"
#include "inventory_cat.h"
/*
TODO:
*/
void initalizeCategories(GtkWidget *mainWindow) {
GtkBuilder *builder;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, INVENTORY_CATEGORY_FILE, NULL);
intrackCategories *intrackCat;
intrackCat = (intrackCategories*) g_malloc (sizeof (intrackCategories));
intrackCat->mainWindow = GTK_WIDGET(gtk_builder_get_object(builder, "categoriesWindow"));
intrackCat->selectedCat = NULL;
intrackCat->selectedItemCode = NULL;
gtk_window_set_transient_for(GTK_WINDOW(intrackCat->mainWindow), GTK_WINDOW(mainWindow));
gtk_window_set_modal(GTK_WINDOW(intrackCat->mainWindow), TRUE);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(intrackCat->mainWindow), TRUE);
// Close the window
GtkWidget *catCloseButton;
catCloseButton = GTK_WIDGET(gtk_builder_get_object(builder, "catCloseButton"));
g_signal_connect(G_OBJECT(catCloseButton), "clicked", G_CALLBACK(closeWindow), intrackCat);
// Setup the category tree
intrackCat->catViewport = GTK_WIDGET(gtk_builder_get_object(builder, "categoryScroll"));
intrackCat->catTree = gtk_tree_view_new();
setupCategoryTree(intrackCat);
gtk_container_add(GTK_CONTAINER(intrackCat->catViewport), intrackCat->catTree);
//GtkTreeSelection *selection3;
//selection3 = gtk_tree_view_get_selection(GTK_TREE_VIEW(intrackCat->catTree));
//gtk_tree_selection_set_mode(selection3, GTK_SELECTION_MULTIPLE);
// Load the category tree
getCategories(NULL, intrackCat);
// The signals for the cat tree
g_signal_connect(intrackCat->catTree, "button-press-event", G_CALLBACK(cat_button_press), intrackCat);
g_signal_connect(intrackCat->catTree, "key-press-event", G_CALLBACK(cat_key_press), intrackCat);
// Setup the category inventory tree
intrackCat->catInventoryViewport = GTK_WIDGET(gtk_builder_get_object(builder, "categoryScroll2"));
intrackCat->catInventoryTree = gtk_tree_view_new();
setupCatInventoryTree(intrackCat);
gtk_container_add(GTK_CONTAINER(intrackCat->catInventoryViewport), intrackCat->catInventoryTree);
g_signal_connect(intrackCat->catInventoryTree, "button-press-event", G_CALLBACK(treeButtonPress), intrackCat);
GtkTreeSelection *selection;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(intrackCat->catInventoryTree));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
// Setup popup menu when right clicking on the inventory tree
intrackCat->invenMenu = gtk_menu_new();
intrackCat->invenMenuView = gtk_menu_item_new_with_label("View item");
gtk_menu_shell_append(GTK_MENU_SHELL(intrackCat->invenMenu), intrackCat->invenMenuView);
gtk_widget_show(intrackCat->invenMenuView);
g_signal_connect(G_OBJECT(intrackCat->invenMenuView), "activate", G_CALLBACK(prepareViewWindow), intrackCat);
gtk_widget_set_sensitive(intrackCat->invenMenuView, FALSE);
// Setup the inventory tree
/*
intrackCat->inventoryViewport = GTK_WIDGET(gtk_builder_get_object(builder, "inventoryViewport1"));
intrackCat->inventoryTree = gtk_tree_view_new();
setupInventoryTree(intrackCat);
gtk_container_add(GTK_CONTAINER(intrackCat->inventoryViewport), intrackCat->inventoryTree);
GtkTreeSelection *selection2;
selection2 = gtk_tree_view_get_selection(GTK_TREE_VIEW (intrackCat->inventoryTree));
gtk_tree_selection_set_mode(selection2, GTK_SELECTION_MULTIPLE);
*/
/* Setup the inventory search checkbox buttons */
/*
intrackCat->barcodeSearch = GTK_WIDGET(gtk_builder_get_object(builder, "barcodeSearch1"));
intrackCat->nameSearch = GTK_WIDGET(gtk_builder_get_object(builder, "nameSearch1"));
intrackCat->descriptionSearch = GTK_WIDGET(gtk_builder_get_object(builder, "descriptionSearch1"));
intrackCat->manufacturerSearch = GTK_WIDGET(gtk_builder_get_object(builder, "manufacturerSearch1"));
intrackCat->categorySearch = GTK_WIDGET(gtk_builder_get_object(builder, "categorySearch1"));
intrackCat->costSearch = GTK_WIDGET(gtk_builder_get_object(builder, "costSearch1"));
intrackCat->priceSearch = GTK_WIDGET(gtk_builder_get_object(builder, "priceSearch1"));
intrackCat->stockSearch = GTK_WIDGET(gtk_builder_get_object(builder, "stockSearch1"));
intrackCat->inventorySearchEntry = GTK_WIDGET(gtk_builder_get_object(builder, "inventorySearchEntry1"));
g_signal_connect(G_OBJECT(intrackCat->inventorySearchEntry), "activate", G_CALLBACK(getInventory), intrackCat);
GtkWidget *inventorySearchButton = GTK_WIDGET(gtk_builder_get_object(builder, "inventorySearchButton1"));
g_signal_connect(G_OBJECT(inventorySearchButton), "clicked", G_CALLBACK(getInventory), intrackCat);
intrackCat->searchSpinMin = GTK_WIDGET(gtk_builder_get_object(builder, "searchSpinMin1"));
intrackCat->searchSpinMax = GTK_WIDGET(gtk_builder_get_object(builder, "searchSpinMax1"));
g_signal_connect(G_OBJECT(intrackCat->searchSpinMin), "activate", G_CALLBACK(getInventoryNumbers), intrackCat);
g_signal_connect(G_OBJECT(intrackCat->searchSpinMax), "activate", G_CALLBACK(getInventoryNumbers), intrackCat);
GtkWidget *inventoryNumberSearchButton = GTK_WIDGET(gtk_builder_get_object(builder, "inventoryNumberSearchButton1"));
g_signal_connect(G_OBJECT(inventoryNumberSearchButton), "clicked", G_CALLBACK(getInventoryNumbers), intrackCat);
*/
/* Setup the buttons */
//intrackCat->addCatButton = GTK_WIDGET(gtk_builder_get_object(builder, "addCatButton"));
intrackCat->addCatButtonBar = GTK_WIDGET(gtk_builder_get_object(builder, "addCatButtonBar"));
//intrackCat->deleteCatButton = GTK_WIDGET(gtk_builder_get_object(builder, "deleteCatButton"));
intrackCat->deleteCatButtonBar = GTK_WIDGET(gtk_builder_get_object(builder, "deleteCatButtonBar"));
//g_signal_connect(G_OBJECT(intrackCat->addCatButton), "clicked", G_CALLBACK(createCatWindow), intrackCat);
g_signal_connect(G_OBJECT(intrackCat->addCatButtonBar), "clicked", G_CALLBACK(createCatWindow), intrackCat);
//g_signal_connect(G_OBJECT(intrackCat->deleteCatButton), "clicked", G_CALLBACK(deleteCatWindow), intrackCat);
g_signal_connect(G_OBJECT(intrackCat->deleteCatButtonBar), "clicked", G_CALLBACK(deleteCatWindow), intrackCat);
//intrackCat->addCatItems = GTK_WIDGET(gtk_builder_get_object(builder, "addCatItems"));
intrackCat->addCatItemsBar = GTK_WIDGET(gtk_builder_get_object(builder, "addCatItemsBar"));
//intrackCat->deleteCatItems = GTK_WIDGET(gtk_builder_get_object(builder, "deleteCatItems"));
intrackCat->deleteCatItemsBar = GTK_WIDGET(gtk_builder_get_object(builder, "deleteCatItemsBar"));
//g_signal_connect(G_OBJECT(intrackCat->addCatItems), "clicked", G_CALLBACK(prepareLoadItemAdd), intrackCat);
g_signal_connect(G_OBJECT(intrackCat->addCatItemsBar), "clicked", G_CALLBACK(prepareLoadItemAdd), intrackCat);
//g_signal_connect(G_OBJECT(intrackCat->deleteCatItems), "clicked", G_CALLBACK(prepareItemRemove), intrackCat);
//g_signal_connect(G_OBJECT(intrackCat->deleteCatItemsBar), "clicked", G_CALLBACK(prepareItemRemove), intrackCat);
g_signal_connect(G_OBJECT(intrackCat->deleteCatItemsBar), "clicked", G_CALLBACK(removeItemsWindow), intrackCat);
//gtk_widget_set_sensitive(intrackCat->deleteCatButton, FALSE);
gtk_widget_set_sensitive(intrackCat->deleteCatButtonBar, FALSE);
//gtk_widget_set_sensitive(intrackCat->addCatItems, FALSE);
gtk_widget_set_sensitive(intrackCat->addCatItemsBar, FALSE);
//gtk_widget_set_sensitive(intrackCat->deleteCatItems, FALSE);
gtk_widget_set_sensitive(intrackCat->deleteCatItemsBar, FALSE);
g_signal_connect(intrackCat->mainWindow, "destroy", G_CALLBACK(catFreeMemory), intrackCat); /* Free memory when the window widget is destroyed */
// Connect any signals in the builder file.
gtk_builder_connect_signals(builder, NULL);
// Unref and free the memory builder used when loading.
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all(intrackCat->mainWindow);
}
static void closeWindow(GtkWidget *widget, intrackCategories *intrackCat) {
clear_tree_store(intrackCat);
gtk_widget_destroy(intrackCat->mainWindow);
}
/* Free up all the memory used by the category window and structure */
static void catFreeMemory(GtkWidget *widget, intrackCategories *intrackCat) {
g_free(intrackCat->selectedCat);
g_free(intrackCat->selectedItemCode);
clear_tree_store(intrackCat);
if(widget)
gtk_widget_destroy(widget);
g_free(intrackCat);
}
/* Open up a window which shows the item information (picture, info, stats, etc) */
static void prepareViewWindow(GtkWidget *widget, intrackCategories *intrackCat) {
loadViewItem(intrackCat->mainWindow, intrackCat->selectedItemCode);
}
static void prepareLoadItemAdd(GtkWidget *widget, intrackCategories *intrackCat) {
if(intrackCat->selectedCat == NULL) {
printMessage("ERROR: Select a category.", intrackCat->mainWindow);
}
else
categoryAddItems(intrackCat); // -> inventory_cat_items.c
}
/* Remove a item from the category inventory */
static void beginItemRemove(GtkTreeRowReference *ref, intrackCategories *intrackCat) {
GtkTreeIter parent, iter;
GtkTreePath *path;
GtkTreeModel *model;
gchar *whereData;
if(intrackCat->selectedCat != NULL) {
model = gtk_tree_view_get_model(GTK_TREE_VIEW(intrackCat->catInventoryTree));
path = gtk_tree_row_reference_get_path (ref);
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get(model, &iter, CAT_ID, &whereData, -1);
/* Update the database */
inventoryModifyCategory("", whereData, "id");
/* Remove the row from the tree */
gtk_tree_store_remove(GTK_TREE_STORE(model), &iter);
g_free(whereData);
}
}
// Remove a item from the category inventory
static void beginCatRemove(GtkTreeRowReference *ref, intrackCategories *intrackCat) {
GtkTreeIter parent, iter;
GtkTreePath *path;
GtkTreeModel *model;
gchar *whereData;
if(intrackCat->selectedCat != NULL) {
model = gtk_tree_view_get_model(GTK_TREE_VIEW(intrackCat->catTree));
path = gtk_tree_row_reference_get_path (ref);
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get(model, &iter, ID, &whereData, -1);
// Update the database
databaseRemoveCat(whereData);
inventoryModifyCategory("", whereData, "category");
// Write code here to update the taxes and fees database
// the 0 is the mode, in this case, 0 = edit the item. 1 = remove the item
//databaseCategoriesUpdate(whereData, NULL, 1, "feeName", FEES_TABLES, "FeesCategories");
//databaseCategoriesUpdate(whereData, NULL, 1, "taxGroup", TAXES_TABLES, "TaxGroupsCategories");
// Remove the row from the tree
gtk_tree_store_remove(GTK_TREE_STORE(model), &iter);
gchar *rowtext;
if(gtk_tree_model_get_iter_first(model, &iter)) {
gtk_tree_model_get(model, &iter, PARENT, &rowtext, -1);
if(!strcmp(rowtext, whereData)) {
gtk_tree_store_set(GTK_TREE_STORE(model), &iter, PARENT, "0", -1);
}
g_free(rowtext);
}
// Finish off searching the rest of the rows in the store
while(gtk_tree_model_iter_next(model, &iter)) {
gtk_tree_model_get(model, &iter, PARENT, &rowtext, -1);
if(!strcmp(rowtext, whereData)) {
gtk_tree_store_set(GTK_TREE_STORE(model), &iter, PARENT, "0", -1);
}
g_free(rowtext);
}
g_free(whereData);
}
}
// Prepares to remove items from the category inventory
static int prepareItemRemove(GtkWidget *widget, intrackCategories *intrackCat) {
if(intrackCat->selectedCat == NULL) {
printMessage("ERROR: Select a category.", intrackCat->mainWindow);
return 1;
}
GtkTreeSelection *selection;
GtkTreeRowReference *ref;
GtkTreeModel *model;
GList *rows, *ptr, *references = NULL;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(intrackCat->catInventoryTree));
model = gtk_tree_view_get_model(GTK_TREE_VIEW(intrackCat->catInventoryTree));
rows = gtk_tree_selection_get_selected_rows(selection, &model);
ptr = rows;
/* Create tree row references to all of the selected rows */
while(ptr != NULL) {
ref = gtk_tree_row_reference_new(model, (GtkTreePath*) ptr->data);
references = g_list_prepend(references, gtk_tree_row_reference_copy(ref));
gtk_tree_row_reference_free(ref);
ptr = ptr->next;
}
if(references != NULL) {
/* Remove each of the selected rows pointed to by the row reference */
g_list_foreach(references, (GFunc) beginItemRemove, intrackCat);
} else {
printMessage("ERROR: Select items to remove.", intrackCat->mainWindow);
}
/* Refresh the inventory tax group tree */
//getGroupInventory(NULL, inventory);
/* Free the tree paths, tree row references and lists */
g_list_foreach(references, (GFunc) gtk_tree_row_reference_free, NULL);
g_list_foreach(rows, (GFunc) gtk_tree_path_free, NULL);
g_list_free(references);
g_list_free(rows);
return 0;
}
// Prepares to remove categories
static void prepareCatRemoval(GtkWidget *widget, intrackCategories *intrackCat) {
GtkTreeSelection *selection;
GtkTreeRowReference *ref;
GtkTreeModel *model;
GList *rows, *ptr, *references = NULL;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(intrackCat->catTree));
model = gtk_tree_view_get_model(GTK_TREE_VIEW(intrackCat->catTree));
rows = gtk_tree_selection_get_selected_rows(selection, &model);
ptr = rows;
// Create tree row references to all of the selected rows
while(ptr != NULL) {
ref = gtk_tree_row_reference_new(model, (GtkTreePath*) ptr->data);
references = g_list_prepend(references, gtk_tree_row_reference_copy(ref));
gtk_tree_row_reference_free(ref);
ptr = ptr->next;
}
// Remove each of the selected rows pointed to by the row reference
g_list_foreach(references, (GFunc) beginCatRemove, intrackCat);
// Refresh the inventory group tree
//getGroupInventory(NULL, inventory);
/* Free the tree paths, tree row references and lists */
g_list_foreach(references, (GFunc) gtk_tree_row_reference_free, NULL);
g_list_foreach(rows, (GFunc) gtk_tree_path_free, NULL);
g_list_free(references);
g_list_free(rows);
g_free(intrackCat->selectedCat);
intrackCat->selectedCat = NULL;
g_free(intrackCat->selectedItemCode);
intrackCat->selectedItemCode = g_strconcat(NULL, NULL);
getCatInventory(intrackCat->catInventoryTree, intrackCat->selectedCat);
//gtk_widget_set_sensitive(intrackCat->deleteCatButton, FALSE);
gtk_widget_set_sensitive(intrackCat->deleteCatButtonBar, FALSE);
//gtk_widget_set_sensitive(intrackCat->addCatItems, FALSE);
gtk_widget_set_sensitive(intrackCat->addCatItemsBar, FALSE);
//gtk_widget_set_sensitive(intrackCat->deleteCatItems, FALSE);
gtk_widget_set_sensitive(intrackCat->deleteCatItemsBar, FALSE);
}
// Changes a inventory items inventory category
int inventoryModifyCategory(gchar *selectedGroup, gchar *data, gchar *where) {
gchar *query_string;
int query_state;
/* Open MYSQL connection to the database */
if(connectToServer() == 1) {
return 1;
}
// Select the database.
query_string = g_strconcat(mysqlDatabase, NULL);
query_state = mysql_select_db(connection, query_string);
g_free(query_string);
// Failed to connect and select database.
if (query_state != 0) {
printf(mysql_error(connection), "%d\n");
printMessage(ERROR_DATABASE, NULL);
mysql_close(connection);
return 1;
}
query_string = g_strconcat("UPDATE `", INVENTORY_TABLES, "` SET category='", selectedGroup, "' WHERE ", where, "='", data, "'", NULL);
query_state = mysql_query(connection, query_string);
g_free(query_string);
if (query_state != 0) {
printf(mysql_error(connection), "%d\n");
printMessage(ERROR_DATABASE, NULL);
mysql_close(connection);
return 1;
}
mysql_close(connection);
return 0;
}
/* Prepare to pull inventory data from the database */
/*
static void getInventory(GtkWidget *widget, intrackCategories *intrackCat) {
GtkListStore *store;
store = gtk_list_store_new (INVENTORY_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
// Checks to see if a ' is entered, that causes mysql.h to break
if(g_strrstr(gtk_entry_get_text(GTK_ENTRY(intrackCat->inventorySearchEntry)), "'")) {
printMessage("ERROR: ' not allowed.", intrackCat->mainWindow);
gtk_entry_set_text(GTK_ENTRY(intrackCat->inventorySearchEntry), ""); // Clear out the search entry
}
// This will search the inventory for the user query if the search entry is greater than 2 characters
else if(strlen(gtk_entry_get_text(GTK_ENTRY(intrackCat->inventorySearchEntry))) > 2) {
gchar *searchString;
searchString = g_strconcat(gtk_entry_get_text(GTK_ENTRY(intrackCat->inventorySearchEntry)), NULL);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->barcodeSearch)))
pullInventory(store, "barcode", searchString, NULL, NULL);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->nameSearch)))
pullInventory(store, "name", searchString, NULL, NULL);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->descriptionSearch)))
pullInventory(store, "description", searchString, NULL, NULL);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->manufacturerSearch)))
pullInventory(store, "manufacturer", searchString, NULL, NULL);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->categorySearch)))
pullInventory(store, "category", searchString, NULL, NULL);
g_free(searchString);
gtk_entry_set_text(GTK_ENTRY(intrackCat->inventorySearchEntry), "");
}
// If the search entry is less than 3 characters but greater than 0
else if(strlen(gtk_entry_get_text(GTK_ENTRY(intrackCat->inventorySearchEntry))) > 0 && strlen(gtk_entry_get_text(GTK_ENTRY(intrackCat->inventorySearchEntry))) < 3) {
printMessage(ERROR_SEARCH_TERMS, intrackCat->mainWindow);
gtk_entry_set_text(GTK_ENTRY(intrackCat->inventorySearchEntry), "");
}
else
pullInventory(store, NULL, NULL, NULL, NULL); // This pulls all the inventory out of the database
gtk_tree_view_set_model(GTK_TREE_VIEW(intrackCat->inventoryTree), GTK_TREE_MODEL(store));
g_object_unref(store);
}
*/
/* Pulls inventory data when searching by numbers ie: cost, price and stock */
/*
static void getInventoryNumbers(GtkWidget *widget, intrackCategories *intrackCat) {
GtkListStore *store;
store = gtk_list_store_new(INVENTORY_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
float min, max;
min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(intrackCat->searchSpinMin));
max = gtk_spin_button_get_value(GTK_SPIN_BUTTON(intrackCat->searchSpinMax));
if(max >= min) {
gchar *minChar, *maxChar;
minChar = g_strdup_printf("%f", min);
maxChar = g_strdup_printf("%f", max);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->costSearch)))
pullInventory(store, "cost", NULL, minChar, maxChar);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->priceSearch)))
pullInventory(store, "price", NULL, minChar, maxChar);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(intrackCat->stockSearch)))
pullInventory(store, "stock", NULL, minChar, maxChar);
g_free(minChar);
g_free(maxChar);
}
else
printMessage(ERROR_SEARCH_NUMBER, intrackCat->mainWindow);
gtk_tree_view_set_model(GTK_TREE_VIEW(intrackCat->inventoryTree), GTK_TREE_MODEL(store));
g_object_unref(store);
}
*/
/* Pulls the inventory data out of the database and stores it into a GtkListStore */
/*
static int pullInventory(GtkListStore *store, gchar *searchWhere, gchar *searchString, gchar *searchNumMin, gchar *searchNumMax) {
GtkTreeIter iter;
int i;
int num_fields;
gchar *query_string;
int query_state;
if(connectToServer() == 1) {
return 1;
}
// Select the database.
query_string = g_strconcat(mysqlDatabase, NULL);
query_state = mysql_select_db(connection, query_string);
// Failed to connect and select database.
if (query_state != 0) {
printf(mysql_error(connection), "%d\n");
printMessage(ERROR_DATABASE, NULL);
g_free(query_string);
mysql_close(connection);
return 1;
}
g_free(query_string);
// Search for the user entered query
if(searchString != NULL)
query_string = g_strconcat("SELECT id, barcode, name, description, manufacturer, category, cost, costAverage, price, stock, serialRequired, numberOfSerial, lastSold, duty, countryOrigin FROM `", INVENTORY_TABLES, "`", " WHERE ", searchWhere, " LIKE '%", searchString, "%'", NULL);
else if(searchNumMin != NULL && searchNumMax != NULL)
query_string = g_strconcat("SELECT id, barcode, name, description, manufacturer, category, cost, costAverage, price, stock, serialRequired, numberOfSerial, lastSold, duty, countryOrigin FROM `", INVENTORY_TABLES, "`", " WHERE ", searchWhere, " >= ", searchNumMin, " AND ", searchWhere, " <= ", searchNumMax, NULL);
else
query_string = g_strconcat("SELECT id, barcode, name, description, manufacturer, category, cost, costAverage, price, stock, serialRequired, numberOfSerial, lastSold, duty, countryOrigin FROM `", INVENTORY_TABLES, "` LIMIT 0, 1000", NULL);
mysql_query(connection, query_string);
//If the connection is successful and the query returns a result then the next step is to display those results:
result = mysql_store_result(connection);
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
gchar *barcode, *name, *description, *manufacturer, *category, *cost, *costAverage, *price, *stock;
for(i = 0; i < num_fields; i++) {
if(i == 1)
barcode = g_strconcat(row[i], NULL);
if(i == 2)
name = g_strconcat(row[i], NULL);
if(i == 3)
description = g_strconcat(row[i], NULL);
if(i == 4)
manufacturer = g_strconcat(row[i], NULL);
if(i == 5)
category = g_strconcat(row[i], NULL);
if(i == 6)
cost = g_strconcat(row[i], NULL);
if(i == 7)
costAverage = g_strdup_printf("%0.2f", getAverageCost(barcode)); //costAverage = g_strconcat(row[i], NULL);
if(i == 8)
price = g_strconcat(row[i], NULL);
if(i == 9)
stock = g_strconcat(row[i], NULL);
}
// Now search the tree store to see if the item already exists in it, we search via the barcode for identification. This prevents duplicates from getting displayed
if(searchString != NULL || (searchNumMin != NULL && searchNumMax != NULL)) {
gchar *rowtext;
gboolean foundItem = FALSE;
if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL (store), &iter, 0, &rowtext, -1); // the barcode column
if(rowtext != NULL) {
// Found item
if(!strcmp(rowtext, barcode))
foundItem = TRUE;
g_free(rowtext);
}
// Finish off searching the rest of the rows in the store
while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL (store), &iter, 0, &rowtext, -1); // the barcode column
if(rowtext != NULL) {
// Found item
if(!strcmp(rowtext, barcode)) {
foundItem = TRUE;
g_free(rowtext);
break;
}
g_free(rowtext);
}
}
}
// If we didn't find the item in the store, then add it
if(foundItem == FALSE) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, BARCODE, barcode, NAME, name, DESCRIPTION, description, MANUFACTURER, manufacturer, CATEGORY, category, COST, cost, COSTAVG, costAverage, PRICE, price, STOCK, stock, -1);
}
}
else {
// This stores all inventory into the tree
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, BARCODE, barcode, NAME, name, DESCRIPTION, description, MANUFACTURER, manufacturer, CATEGORY, category, COST, cost, COSTAVG, costAverage, PRICE, price, STOCK, stock, -1);
}
g_free(barcode);
g_free(name);
g_free(description);
g_free(manufacturer);
g_free(category);
g_free(cost);
g_free(costAverage);
g_free(price);
g_free(stock);
}
g_free(query_string);
mysql_free_result(result);
mysql_close(connection);
return 0;
}
*/
/* Gets the average cost of the item from the database */
/*
static float getAverageCost(gchar *barcode) {
gchar *costQuery;
int query_state, num_fields, i;
float costAverage;
MYSQL *costConnection, costMysql;
MYSQL_RES *costResult;
MYSQL_ROW costRow;
mysql_init(&costMysql);
costConnection = mysql_real_connect(&costMysql, mysqlServer, mysqlUsername, mysqlPassword, MYSQL_DEFAULT_DATABASE, 0, 0, 0);
costQuery = g_strconcat(mysqlDatabase, NULL);
query_state = mysql_select_db(costConnection, costQuery);
g_free(costQuery);
costQuery = g_strconcat("SELECT costAverage FROM `", mysqlTables, "` WHERE barcode = '", barcode, "'", NULL);
query_state = mysql_query(costConnection, costQuery);
costResult = mysql_store_result(costConnection);
num_fields = mysql_num_fields(costResult);
while ( ( costRow = mysql_fetch_row(costResult)) ) {
for(i = 0; i < num_fields; i++) {
if(i == 0)
costAverage = atof(costRow[i]);
}
}
g_free(costQuery);
mysql_free_result(costResult);
mysql_close(costConnection);
return costAverage;
}
*/
/* Creates a new category */
static void createCatWindow(GtkWidget *widget, intrackCategories *intrackCat) {
intrackCatEntry *catEntry;
catEntry = (intrackCatEntry*) g_malloc (sizeof (intrackCatEntry)); // Allocate memory for the data.
// Create and setup the new input popup window.
catEntry->inputWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//gtk_window_set_default_size(GTK_WINDOW(taxEntry->inputWindow), 320, 220);
gtk_widget_set_size_request(catEntry->inputWindow, 300, 110);
gtk_window_set_title(GTK_WINDOW(catEntry->inputWindow), "Create New Category");
gtk_container_set_border_width(GTK_CONTAINER(catEntry->inputWindow), 10);
// Set the new inputWindow so it is above the original parent window of the program.
gtk_window_set_transient_for(GTK_WINDOW(catEntry->inputWindow), GTK_WINDOW(intrackCat->mainWindow));
// Create a table to layout and organize the text and entry fields.
catEntry->table = gtk_table_new(4, 2, FALSE);
gtk_container_add(GTK_CONTAINER(catEntry->inputWindow), catEntry->table);
gtk_window_set_position(GTK_WINDOW(catEntry->inputWindow), GTK_WIN_POS_CENTER);
catEntry->inputLabel = gtk_label_new("Category Name:");
gtk_table_attach(GTK_TABLE(catEntry->table), catEntry->inputLabel, 0, 1, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
catEntry->inputEntry = gtk_entry_new();
catEntry->sendButton = gtk_button_new_with_label("Ok");
gtk_widget_set_size_request(catEntry->sendButton, 80, 30 );
catEntry->cancelButton = gtk_button_new_with_label("Cancel");
gtk_widget_set_size_request(catEntry->cancelButton, 80, 30 );
gtk_table_set_homogeneous(GTK_TABLE(catEntry->table), FALSE);
gtk_table_attach(GTK_TABLE(catEntry->table), catEntry->inputEntry, 1, 2, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(catEntry->table), catEntry->sendButton, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 1, 5);
gtk_table_attach(GTK_TABLE(catEntry->table), catEntry->cancelButton, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 1, 5);
// Set the positions of my buttons so they are spaced and look good.
gtk_widget_set_uposition(catEntry->sendButton, 200, 60);
gtk_widget_set_uposition(catEntry->cancelButton, 110, 60);
/* Set the windows so windows below can not be grabbed, and set it not resizable and hide the close and maximize buttons */
gtk_window_set_modal(GTK_WINDOW(catEntry->inputWindow), TRUE);
gtk_window_set_resizable(GTK_WINDOW(catEntry->inputWindow), FALSE);
gtk_window_set_deletable(GTK_WINDOW(catEntry->inputWindow), FALSE);
gtk_widget_show_all(catEntry->inputWindow);
// Close the window (destroy the widget)
g_signal_connect(G_OBJECT(catEntry->cancelButton), "clicked", G_CALLBACK(destroyWidget), catEntry->inputWindow);
g_signal_connect(G_OBJECT(catEntry->sendButton), "clicked", G_CALLBACK(databaseCreateCat), catEntry);
g_signal_connect(G_OBJECT(catEntry->sendButton), "clicked", G_CALLBACK(getCategories), intrackCat); /* Refresh the cat tree */
g_signal_connect(G_OBJECT(catEntry->sendButton), "clicked", G_CALLBACK(catAddSensitive), intrackCat);
g_signal_connect(G_OBJECT(catEntry->sendButton), "clicked", G_CALLBACK(destroyWidget), catEntry->inputWindow);
g_signal_connect(G_OBJECT(catEntry->inputWindow), "destroy", G_CALLBACK(freeMemoryCatWindow), catEntry);
gtk_widget_show(catEntry->inputWindow);
}
static void catAddSensitive(GtkWidget *widget, intrackCategories *intrackCat) {
//gtk_widget_set_sensitive(intrackCat->deleteCatButton, FALSE);
gtk_widget_set_sensitive(intrackCat->deleteCatButtonBar, FALSE);
//gtk_widget_set_sensitive(intrackCat->addCatItems, FALSE);
gtk_widget_set_sensitive(intrackCat->addCatItemsBar, FALSE);
//gtk_widget_set_sensitive(intrackCat->deleteCatItems, FALSE);
gtk_widget_set_sensitive(intrackCat->deleteCatItemsBar, FALSE);
}
/* Creates a new category in the database */
static int databaseCreateCat(GtkWidget *widget, intrackCatEntry *catEntry) {
gchar *inputData;
inputData = g_strconcat(gtk_entry_get_text(GTK_ENTRY(catEntry->inputEntry)), NULL);
if(g_strrstr(inputData, "'")) {
printMessage("ERROR: ' not allowed.", NULL);
g_free(inputData);
return 1;
}
/* Now begin category creation */
gchar *query_string;
int query_state;
if(connectToServer() == 1)
return 1;
// Select the database.
query_string = g_strdup(CATEGORY_DATABASE);
query_state = mysql_select_db(connection, query_string);
g_free(query_string);
// Failed to connect and select database.
if (query_state != 0) {
printf(mysql_error(connection), "%d\n");
gchar *errorMessage = g_strdup_printf("%s", mysql_error(connection));
g_free(errorMessage);
mysql_close(connection);
return 1;
}
query_string = g_strconcat("INSERT INTO `", CATEGORY_TABLES, "` (display_name) VALUES ('", inputData, "')", NULL);
query_state=mysql_query(connection, query_string);
// Failed to query the data
if (query_state !=0) {
printf(mysql_error(connection), "%d\n");
printMessage("ERROR: Error connecting to server, check query syntax", NULL);
g_free(query_string);
g_free(inputData);
mysql_close(connection);
return 1;
}
g_free(query_string);
g_free(inputData);
mysql_close(connection);
return 0;
}
// Removes a category in the database
static int databaseRemoveCat(gchar *whereData) {
// Now begin category creation
gchar *query_string;
int query_state;
if(connectToServer() == 1)
return 1;
// Select the database.
query_string = g_strdup(CATEGORY_DATABASE);
query_state = mysql_select_db(connection, query_string);
g_free(query_string);
// Failed to connect and select database.
if (query_state != 0) {
printf(mysql_error(connection), "%d\n");
gchar *errorMessage = g_strdup_printf("%s", mysql_error(connection));
g_free(errorMessage);
mysql_close(connection);
return 1;
}
query_string = g_strconcat("DELETE FROM `", CATEGORY_TABLES, "` WHERE id='", whereData, "'", NULL);
query_state=mysql_query(connection, query_string);
// Failed to query the data
if (query_state !=0) {
printf(mysql_error(connection), "%d\n");
printMessage("ERROR: Error connecting to server, check query syntax", NULL);
g_free(query_string);
mysql_close(connection);
return 1;
}
g_free(query_string);
query_string = g_strconcat("UPDATE `", CATEGORY_TABLES, "` SET parent_id='0' WHERE parent_id='", whereData, "'", NULL);
query_state=mysql_query(connection, query_string);
// Failed to query the data
if (query_state !=0) {
printf(mysql_error(connection), "%d\n");
printMessage("ERROR: Error connecting to server, check query syntax", NULL);
g_free(query_string);
mysql_close(connection);
return 1;
}
g_free(query_string);
mysql_close(connection);
return 0;
}
/* Checks input validation and if the category exists or not */
static int checkCatExist(gchar *inputData) {
/* Do a check to make sure the new tax name is valid */
if(strlen(inputData) < 1) {
printMessage("ERROR: New category name is too short", NULL);
return 1;
}
/* Now begin searching the category names */
gchar *query_string;
int query_state;
int num_fields;
int i;
if(connectToServer() == 1)
return 1;
// Select the database.
query_string = g_strdup(CATEGORY_DATABASE);
query_state = mysql_select_db(connection, query_string);
g_free(query_string);
// Failed to connect and select database.
if (query_state != 0) {
printf(mysql_error(connection), "%d\n");
gchar *errorMessage = g_strdup_printf("%s", mysql_error(connection));
g_free(errorMessage);
mysql_close(connection);
return 1;
}
query_string = g_strconcat("SELECT category FROM `", CATEGORY_TABLES, "` WHERE category='", inputData, "'", NULL);
query_state=mysql_query(connection, query_string);
/* Failed to query the data */
if (query_state !=0) {
printf(mysql_error(connection), "%d\n");
printMessage("ERROR: Error connecting to server, check query syntax", NULL);
g_free(query_string);
mysql_close(connection);
return 1;
}
/* Store the query results into reportResult */
result = mysql_store_result(connection);
num_fields = mysql_num_fields(result);
while ( ( row = mysql_fetch_row(result)) ) {
for(i = 0; i < num_fields; i++) {
/* If the returned result has 1 or more characters, it means it found a taxgroup name already exists, then exit and return */
if(strlen(row[i]) > 0) {
printMessage("ERROR: Category already exists", NULL);
g_free(query_string);
mysql_free_result(result);
mysql_close(connection);
return 1;
}
}
}
g_free(query_string);
mysql_free_result(result);
mysql_close(connection);
return 0;
}
static void freeMemoryCatWindow(GtkWidget *widget, intrackCategories *intrackCat) {
clear_tree_store(intrackCat);
g_free(intrackCat);
}
// Clears the tree store, quicker to erase from memory when the tree is empty.
static void clear_tree_store(intrackCategories *intrackCat) {
GtkTreeStore *store;
GtkTreeIter iter;
store = gtk_tree_store_new(CAT_INVENTORY_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, CAT_ID, "", CAT_BARCODE, "", CAT_DESCRIPTION, "", CAT_MANUFACTURER, "", CAT_EXTRAINFO, "", -1);
gtk_tree_view_set_model(GTK_TREE_VIEW(intrackCat->catInventoryTree), GTK_TREE_MODEL(store));
g_object_unref(store);
}
// Prepare to pull category data from the database
static void getCategories(GtkWidget *widget, intrackCategories *intrackCat) {
GtkTreeStore *store;
store = gtk_tree_store_new (CAT_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
pullCategories(store); // This pulls all the categories out of the database
gtk_tree_view_set_model(GTK_TREE_VIEW(intrackCat->catTree), GTK_TREE_MODEL(store));
g_object_unref(store);
}
// Pulls the category data out of the database and stores it into a GtkListStore
static int pullCategories(GtkTreeStore *store) {
int i;
int num_fields;
gchar *query_string;
int query_state;
// Open MYSQL connection to the database
if(connectToServer() == 1) {
return 1;
}
// Select the database.
query_string = g_strdup(CATEGORY_DATABASE);
query_state = mysql_select_db(connection, query_string);