-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartSales.c
executable file
·2821 lines (2163 loc) · 108 KB
/
partSales.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
// partSales.c
// Copyright 2010 - 2011 - 2012 - 2013 - 2014 Michael Rajotte <[email protected]>
// For inventory control, mass listing, searching and editing via a gtktree.
#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 <locale.h>
#include "settings.h"
#include "messages.h"
#include "partSales.h"
/*
TODO:
(1). Build serial # recording.
-----------------
| |
| Edit Serials | <------- Button on toolbar to edit and list the serial numbers for the selected item in stock.
| |
-----------------
(DO THIS STUFF AFTER)
***** For this, going to have to put another hidden column, which records if serial numbers are missing or not. *****
Serial Number Code current status:
-Creation and deletion of serial number database and tables. Table names are based on the product barcode. (COMPLETE).
-When user adjusts the stock quantity with items that have serial numbers, a popup window comes up to choose which serial number items to remove.
(Uses check marks to select a column)
-Multiple columns if the item uses multiple serial numbers per item.
-When user adjusts the stock quantity with items that have serial numbers, a popup window comes up with a editable list store to enter serial numbers.
(Uses check marks to select a column)
-Multiple columns if the item uses multiple serial numbers per item.
-Then modify checkout.c and checkout_submit.c to accept the recorded serial numbers via search pulldown or scanning.
-EXTRA NOTE: Use scan override to bypass "Serial Number not found in stock" when a item serial number was never recorded into stock.
(2). Things to add to items:
-Last ordered -> This can be put in orders.c
-Next Arrival -> This can be put in orders.c
-On Order -> This can be put in orders.c
-Weight
-Location
-Inventory Value
(3). checkIfExist() needs to be fixed. Look at accounts.c checkAccountExist() for proper implementation.
#### Color Coding Table ####
Red = No Stock.
Yellow = Stock is below stock low level indicator.
Purple = Serial numbers missing.
*/
void initalizePartSales(GtkBuilder *builder, gchar *mysqlDatabase, gchar *inventoryTable, GtkWidget *mainWindow) {
/* Load up the old widgets. <-- This is old stuff, delete after when rewriting new code */
//initalizeWidgets(builder);
/* Initialize the intrackInventory structure */
intrackInventory *inventory;
inventory = (intrackInventory*) g_malloc (sizeof (intrackInventory)); // Allocate memory for the data.
inventory->mysqlDatabase = g_strconcat(mysqlDatabase, NULL);
inventory->inventoryTable = g_strconcat(inventoryTable, NULL);
inventory->mainWindow = mainWindow;
inventory->selectedItemCode = g_strconcat(NULL, NULL);
inventory->exportQueryString = g_strconcat(NULL, NULL);
// Setup the selection numbers.
inventory->numberOfParts = 0;
inventory->partCosts = 0;
inventory->partSales = 0;
inventory->partProfits = 0;
inventory->partMargin = 0;
// Setup the difference numbers.
inventory->numberOfPartsPrev = 0;
inventory->partCostsPrev = 0;
inventory->partSalesPrev = 0;
inventory->partProfitsPrev = 0;
inventory->partMarginPrev = 0;
// Exit program button from file / quit.
GtkWidget *exitInventory;
exitInventory = GTK_WIDGET(gtk_builder_get_object(builder, "inventoryCloseButton"));
g_signal_connect(G_OBJECT(exitInventory), "activate", G_CALLBACK(hideGtkWidget), mainWindow);
/* Toolbar buttons */
/*
GtkWidget *addButton;
addButton = GTK_WIDGET(gtk_builder_get_object(builder, "invenAddButton"));
g_signal_connect(G_OBJECT(addButton), "clicked", G_CALLBACK(prepareAddItem), mainWindow);
GtkWidget *categoryButton;
categoryButton = GTK_WIDGET(gtk_builder_get_object(builder, "invenCategoryButton"));
g_signal_connect(G_OBJECT(categoryButton), "clicked", G_CALLBACK(openCategories), mainWindow);
*/
/*
inventory->removeItemButton = GTK_WIDGET(gtk_builder_get_object(builder, "invenDelButton"));
g_signal_connect(G_OBJECT(inventory->removeItemButton), "clicked", G_CALLBACK(deleteItemWindow), inventory);
gtk_widget_set_sensitive(inventory->removeItemButton, FALSE);
*/
/*
inventory->invenViewItemButton = GTK_WIDGET(gtk_builder_get_object(builder, "invenViewItemButton"));
g_signal_connect(G_OBJECT(inventory->invenViewItemButton), "clicked", G_CALLBACK(prepareViewWindow), inventory);
gtk_widget_set_sensitive(inventory->invenViewItemButton, FALSE);
inventory->editItemButton = GTK_WIDGET(gtk_builder_get_object(builder, "invenEditButton"));
g_signal_connect(G_OBJECT(inventory->editItemButton), "clicked", G_CALLBACK(prepareEditWindow), inventory);
gtk_widget_set_sensitive(inventory->editItemButton, FALSE);
*/
GtkWidget *addButton;
addButton = GTK_WIDGET(gtk_builder_get_object(builder, "addSaleButton"));
g_signal_connect(G_OBJECT(addButton), "clicked", G_CALLBACK(prepareSaleAddItem), inventory);
GtkWidget *exportButton;
exportButton = GTK_WIDGET(gtk_builder_get_object(builder, "exportInventoryButton"));
g_signal_connect(G_OBJECT(exportButton), "clicked", G_CALLBACK(prepareExport), inventory);
inventory->returnButton = GTK_WIDGET(gtk_builder_get_object(builder, "returnButton"));
g_signal_connect(G_OBJECT(inventory->returnButton), "clicked", G_CALLBACK(deleteItemWindow), inventory);
gtk_widget_set_sensitive(inventory->returnButton, FALSE);
/*
GtkWidget *importButton;
importButton = GTK_WIDGET(gtk_builder_get_object(builder, "invenImportButton"));
g_signal_connect(G_OBJECT(importButton), "clicked", G_CALLBACK(prepareImport), inventory);
*/
// Information total labels
inventory->numberOfPartsLabel = GTK_WIDGET(gtk_builder_get_object(builder, "numberOfPartsLabel"));
inventory->partCostsLabel = GTK_WIDGET(gtk_builder_get_object(builder, "partCostsLabel"));
inventory->partSalesLabel = GTK_WIDGET(gtk_builder_get_object(builder, "partSalesLabel"));
inventory->partProfitsLabel = GTK_WIDGET(gtk_builder_get_object(builder, "partProfitsLabel"));
inventory->partMarginLabel = GTK_WIDGET(gtk_builder_get_object(builder, "partMarginLabel"));
// Selection information total labels
inventory->numberOfPartsLabel1 = GTK_WIDGET(gtk_builder_get_object(builder, "numberOfPartsLabel1"));
inventory->partCostsLabel1 = GTK_WIDGET(gtk_builder_get_object(builder, "partCostsLabel1"));
inventory->partSalesLabel1 = GTK_WIDGET(gtk_builder_get_object(builder, "partSalesLabel1"));
inventory->partProfitsLabel1 = GTK_WIDGET(gtk_builder_get_object(builder, "partProfitsLabel1"));
inventory->partMarginLabel1 = GTK_WIDGET(gtk_builder_get_object(builder, "partMarginLabel1"));
// Previous Year Information total labels
inventory->numberOfPartsLabelPrev = GTK_WIDGET(gtk_builder_get_object(builder, "numberOfPartsLabelPrev"));
inventory->partCostsLabelPrev = GTK_WIDGET(gtk_builder_get_object(builder, "partCostsLabelPrev"));
inventory->partSalesLabelPrev = GTK_WIDGET(gtk_builder_get_object(builder, "partSalesLabelPrev"));
inventory->partProfitsLabelPrev = GTK_WIDGET(gtk_builder_get_object(builder, "partProfitsLabelPrev"));
inventory->partMarginLabelPrev = GTK_WIDGET(gtk_builder_get_object(builder, "partMarginLabelPrev"));
// Difference Information total labels
inventory->numberOfPartsLabelDiff = GTK_WIDGET(gtk_builder_get_object(builder, "numberOfPartsLabelDiff"));
inventory->partCostsLabelDiff = GTK_WIDGET(gtk_builder_get_object(builder, "partCostsLabelDiff"));
inventory->partSalesLabelDiff = GTK_WIDGET(gtk_builder_get_object(builder, "partSalesLabelDiff"));
inventory->partProfitsLabelDiff = GTK_WIDGET(gtk_builder_get_object(builder, "partProfitsLabelDiff"));
inventory->partMarginLabelDiff = GTK_WIDGET(gtk_builder_get_object(builder, "partMarginLabelDiff"));
// % increase/decrease information total labels
inventory->numberOfPartsLabelPerc = GTK_WIDGET(gtk_builder_get_object(builder, "numberOfPartsLabelPerc"));
inventory->partCostsLabelPerc = GTK_WIDGET(gtk_builder_get_object(builder, "partCostsLabelPerc"));
inventory->partSalesLabelPerc = GTK_WIDGET(gtk_builder_get_object(builder, "partSalesLabelPerc"));
inventory->partProfitsLabelPerc = GTK_WIDGET(gtk_builder_get_object(builder, "partProfitsLabelPerc"));
inventory->partMarginLabelPerc = GTK_WIDGET(gtk_builder_get_object(builder, "partMarginLabelPerc"));
/* Close the window */
GtkWidget *inventoryCloseButtonBar;
inventoryCloseButtonBar = GTK_WIDGET(gtk_builder_get_object(builder, "inventoryCloseButtonBar"));
g_signal_connect(G_OBJECT(inventoryCloseButtonBar), "clicked", G_CALLBACK(hideGtkWidget), mainWindow);
/* Setup the inventory tree */
inventory->inventoryViewport = GTK_WIDGET(gtk_builder_get_object(builder, "inventoryViewport"));
inventory->inventoryTree = gtk_tree_view_new();
setupInventoryTree(inventory);
gtk_container_add(GTK_CONTAINER(inventory->inventoryViewport), inventory->inventoryTree);
inventory->inventorySearchEntry = GTK_WIDGET(gtk_builder_get_object(builder, "inventorySearchEntry"));
g_signal_connect(G_OBJECT(inventory->inventorySearchEntry), "activate", G_CALLBACK(getInventory), inventory);
GtkWidget *inventorySearchButton = GTK_WIDGET(gtk_builder_get_object(builder, "inventorySearchButton"));
g_signal_connect(G_OBJECT(inventorySearchButton), "clicked", G_CALLBACK(getInventory), inventory);
inventory->searchSpinMin = GTK_WIDGET(gtk_builder_get_object(builder, "searchSpinMin"));
inventory->searchSpinMax = GTK_WIDGET(gtk_builder_get_object(builder, "searchSpinMax"));
g_signal_connect(G_OBJECT(inventory->searchSpinMin), "activate", G_CALLBACK(getInventoryNumbers), inventory);
g_signal_connect(G_OBJECT(inventory->searchSpinMax), "activate", G_CALLBACK(getInventoryNumbers), inventory);
GtkWidget *inventoryNumberSearchButton = GTK_WIDGET(gtk_builder_get_object(builder, "inventoryNumberSearchButton"));
g_signal_connect(G_OBJECT(inventoryNumberSearchButton), "clicked", G_CALLBACK(getInventoryNumbers), inventory);
/* Now set the tree so it can handle multiple selections */
//GtkTreeSelection *selection;
inventory->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (inventory->inventoryTree));
gtk_tree_selection_set_mode (inventory->selection, GTK_SELECTION_MULTIPLE);
/* Setup the inventory search checkbox buttons */
inventory->barcodeSearch = GTK_WIDGET(gtk_builder_get_object(builder, "barcodeSearch"));
inventory->descriptionSearch = GTK_WIDGET(gtk_builder_get_object(builder, "descriptionSearch"));
inventory->soldToSearch = GTK_WIDGET(gtk_builder_get_object(builder, "soldToSearch"));
inventory->costSearch = GTK_WIDGET(gtk_builder_get_object(builder, "costSearch")); /* Belongs to number search */
inventory->priceSearch = GTK_WIDGET(gtk_builder_get_object(builder, "priceSearch")); /* Belongs to number search */
/* Setup popup menu when right clicking on the inventory tree */
//inventory->invenMenu = gtk_menu_new();
/*
inventory->invenMenuView = gtk_menu_item_new_with_label("View item");
gtk_menu_shell_append(GTK_MENU_SHELL(inventory->invenMenu), inventory->invenMenuView);
gtk_widget_show(inventory->invenMenuView);
g_signal_connect(G_OBJECT(inventory->invenMenuView), "activate", G_CALLBACK(prepareViewWindow), inventory);
gtk_widget_set_sensitive(inventory->invenMenuView, FALSE);
inventory->invenMenuEdit = gtk_menu_item_new_with_label("Edit item");
gtk_menu_shell_append(GTK_MENU_SHELL(inventory->invenMenu), inventory->invenMenuEdit);
gtk_widget_show(inventory->invenMenuEdit);
g_signal_connect(G_OBJECT(inventory->invenMenuEdit), "activate", G_CALLBACK(prepareEditWindow), inventory);
gtk_widget_set_sensitive(inventory->invenMenuEdit, FALSE);
*/
/*
inventory->invenMenuRemove = gtk_menu_item_new_with_label("Remove item");
gtk_menu_shell_append(GTK_MENU_SHELL(inventory->invenMenu), inventory->invenMenuRemove);
gtk_widget_show(inventory->invenMenuRemove);
g_signal_connect(G_OBJECT(inventory->invenMenuRemove), "activate", G_CALLBACK(deleteItemWindow), inventory);
*/
/* Setup keypress signals on the tree */
g_signal_connect(inventory->inventoryTree, "button-press-event", G_CALLBACK(treeButtonPress), inventory);
g_signal_connect(inventory->inventoryTree, "key-press-event", G_CALLBACK(treeKeyPress), inventory);
/* Load the inventory tree with all data from the database */
//getInventory(NULL, inventory);
g_signal_connect(inventory->mainWindow, "show", G_CALLBACK(getInventory), inventory);
/* ------------------------------------------------------------------------------------------------------------------------------ */
/* Creates the FROM calendar widget and entry box */
inventory->dateEntryFrom = (GtkDateEntry*) g_malloc (sizeof (GtkDateEntry)); // Allocate memory for the data.
inventory->dateEntryFrom->date = g_date_new();
/* today's date */
g_date_set_time_t(inventory->dateEntryFrom->date, time(NULL));
g_date_set_dmy(&inventory->dateEntryFrom->mindate, 1, 1, 1900);
g_date_set_dmy(&inventory->dateEntryFrom->maxdate, 31, 12, 2200);
g_date_subtract_days(inventory->dateEntryFrom->date, 30);
// Entry and button for date from
gchar dateBuffer[256];
g_date_strftime(dateBuffer, 256, "%Y-%m-%d", inventory->dateEntryFrom->date);
inventory->dateEntryFrom->entry = GTK_WIDGET(gtk_builder_get_object(builder, "dateEntryFrom"));
inventory->dateEntryFrom->arrow = GTK_WIDGET(gtk_builder_get_object(builder, "fromToggle"));
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->arrow), "toggled", G_CALLBACK (gtk_dateentry_arrow_press), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->entry), "activate", G_CALLBACK (gtk_dateentry_entry_new), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->entry), "focus-out-event", G_CALLBACK (gtk_dateentry_focus), inventory->dateEntryFrom);
/* our popup window */
inventory->dateEntryFrom->popwin = gtk_window_new (GTK_WINDOW_POPUP);
gtk_widget_set_events (inventory->dateEntryFrom->popwin, gtk_widget_get_events(inventory->dateEntryFrom->popwin) | GDK_KEY_PRESS_MASK);
inventory->dateEntryFrom->frame = gtk_frame_new (NULL);
gtk_container_add (GTK_CONTAINER (inventory->dateEntryFrom->popwin), inventory->dateEntryFrom->frame);
gtk_frame_set_shadow_type (GTK_FRAME (inventory->dateEntryFrom->frame), GTK_SHADOW_OUT);
gtk_widget_show (inventory->dateEntryFrom->frame);
inventory->dateEntryFrom->calendar = gtk_calendar_new ();
gtk_container_add (GTK_CONTAINER (inventory->dateEntryFrom->frame), inventory->dateEntryFrom->calendar);
gtk_widget_show (inventory->dateEntryFrom->calendar);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->popwin), "key_press_event", G_CALLBACK (key_press_popup), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->popwin), "button_press_event", G_CALLBACK (gtk_dateentry_button_press), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->calendar), "prev-year", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->calendar), "next-year", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->calendar), "prev-month", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->calendar), "next-month", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->calendar), "day-selected", G_CALLBACK (gtk_dateentry_calendar_getfrom), inventory->dateEntryFrom);
g_signal_connect (GTK_OBJECT (inventory->dateEntryFrom->calendar), "day-selected-double-click", G_CALLBACK (gtk_dateentry_calendar_select), inventory->dateEntryFrom);
gtk_dateentry_calendar_getfrom(NULL, inventory->dateEntryFrom);
gtk_entry_set_text(GTK_ENTRY(inventory->dateEntryFrom->entry), dateBuffer);
/* ------------------------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------------------------ */
/* Creates the TO calendar widget and entry box */
inventory->dateEntryTo = (GtkDateEntry*) g_malloc (sizeof (GtkDateEntry)); // Allocate memory for the data.
inventory->dateEntryTo->date = g_date_new();
/* today's date */
g_date_set_time_t(inventory->dateEntryTo->date, time(NULL));
g_date_set_dmy(&inventory->dateEntryTo->mindate, 1, 1, 1900);
g_date_set_dmy(&inventory->dateEntryTo->maxdate, 31, 12, 2200);
// Entry and button for date to
inventory->dateEntryTo->entry = GTK_WIDGET(gtk_builder_get_object(builder, "dateEntryTo"));
inventory->dateEntryTo->arrow = GTK_WIDGET(gtk_builder_get_object(builder, "toToggle"));
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->arrow), "toggled", G_CALLBACK (gtk_dateentry_arrow_press), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->entry), "activate", G_CALLBACK (gtk_dateentry_entry_new), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->entry), "focus-out-event", G_CALLBACK (gtk_dateentry_focus), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->entry), "key_press_event", G_CALLBACK (gtk_dateentry_entry_key), inventory->dateEntryTo);
/* our popup window */
inventory->dateEntryTo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
gtk_widget_set_events (inventory->dateEntryTo->popwin, gtk_widget_get_events(inventory->dateEntryTo->popwin) | GDK_KEY_PRESS_MASK);
inventory->dateEntryTo->frame = gtk_frame_new (NULL);
gtk_container_add (GTK_CONTAINER (inventory->dateEntryTo->popwin), inventory->dateEntryTo->frame);
gtk_frame_set_shadow_type (GTK_FRAME (inventory->dateEntryTo->frame), GTK_SHADOW_OUT);
gtk_widget_show (inventory->dateEntryTo->frame);
inventory->dateEntryTo->calendar = gtk_calendar_new ();
gtk_container_add (GTK_CONTAINER (inventory->dateEntryTo->frame), inventory->dateEntryTo->calendar);
gtk_widget_show (inventory->dateEntryTo->calendar);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->popwin), "key_press_event", G_CALLBACK (key_press_popup), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->popwin), "button_press_event", G_CALLBACK (gtk_dateentry_button_press), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->calendar), "prev-year", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->calendar), "next-year", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->calendar), "prev-month", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->calendar), "next-month", G_CALLBACK (gtk_dateentry_calendar_year), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->calendar), "day-selected", G_CALLBACK (gtk_dateentry_calendar_getfrom), inventory->dateEntryTo);
g_signal_connect (GTK_OBJECT (inventory->dateEntryTo->calendar), "day-selected-double-click", G_CALLBACK (gtk_dateentry_calendar_select), inventory->dateEntryTo);
gtk_dateentry_calendar_getfrom(NULL, inventory->dateEntryTo);
/* ------------------------------------------------------------------------------------------------------------------------------ */
}
/* Import a raw .csv file to the database. -> import.c */
static void prepareImport(GtkWidget *widget, intrackInventory *inventory) {
//importDatabase(inventory->mainWindow); /* import.c */
}
/* Export the inventory database. -> export.c */
static void prepareExport(GtkWidget *widget, intrackInventory *inventory) {
mysqlExportSales(NULL, inventory->mainWindow, inventory->exportQueryString); /* export.c */
}
/* Opens a widget popup to prompt user to add a item. -> partSales_add.c */
static void prepareSaleAddItem(GtkWidget *widget, intrackInventory *inventory) {
initalizeSaleAdd(inventory->mainWindow); // -> partSales_add.c
}
/* Opens the serial number editor. -> inventory_serial.c */
static void prepareSerialWindow(GtkWidget *widget, intrackInventory *inventory) {
//initalizeSerialEditor(inventory->mainWindow, inventory->selectedItemCode);
}
/* Opens the category editor window. -> inventory_cat.c */
static void openCategories(GtkWidget *widget, GtkWidget *mainWindow) {
//initalizeCategories(mainWindow); // -> inventory_cat.c
}
/* Open up a window which allows full editing of a item. -> inventory_edit.c */
static void prepareEditWindow(GtkWidget *widget, intrackInventory *inventory) {
//initalizeEditor(inventory->mainWindow, inventory->inventoryTree, inventory->selectedItemCode, &inventory->serial, inventory->invenMenuSerialButton, inventory->invenSerialButton); // -> inventory_edit.c
}
/* Open up a window which shows the item information (picture, info, stats, etc). -> view_item.c */
static void prepareViewWindow(GtkWidget *widget, intrackInventory *inventory) {
//loadViewItem(inventory->mainWindow, inventory->selectedItemCode); // -> view_item.c
}
/* Opens a widget popup to prompt user to add a item. -> inventory_add.c */
static void prepareAddItem(GtkWidget *widget, GtkWidget *mainWindow) {
//initalizeAdd(mainWindow); // -> inventory_add.c
}
/* A widget window to ask for confirmation upon deletion */
static void deleteItemWindow(GtkWidget *widget, intrackInventory *inventory) {
GtkWidget *dialog;
gint widgetDialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(inventory->mainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_YES_NO,
"Remove selected items and return to stock?");
gtk_window_set_title(GTK_WINDOW(dialog), "Return to stock?");
widgetDialog = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
switch (widgetDialog) {
case GTK_RESPONSE_YES:
prepareItemRemoval(NULL, inventory);
break;
case GTK_RESPONSE_NO:
break;
}
}
/* A widget window to ask for confirmation upon setting future dates */
static void setDateWindow(GtkWidget *widget, intrackInventory *inventory) {
GtkWidget *dialog;
gint widgetDialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(inventory->mainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_YES_NO,
"Set Date into the future?");
gtk_window_set_title(GTK_WINDOW(dialog), "Set Date Into Future?");
widgetDialog = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
switch (widgetDialog) {
case GTK_RESPONSE_YES:
prepareItemRemoval(NULL, inventory);
break;
case GTK_RESPONSE_NO:
break;
}
}
/* Pulls inventory data when searching by numbers ie: cost, price and stock */
static void getInventoryNumbers(GtkWidget *widget, intrackInventory *inventory) {
/* Clear out selected item before refreshing the tree */
g_free(inventory->selectedItemCode);
inventory->selectedItemCode = g_strconcat(NULL, NULL);
//gtk_widget_set_sensitive(inventory->removeItemButton, FALSE);
//gtk_widget_set_sensitive(inventory->invenViewItemButton, FALSE);
//gtk_widget_set_sensitive(inventory->editItemButton, FALSE);
gtk_widget_set_sensitive(inventory->returnButton, FALSE);
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, G_TYPE_STRING, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT);
GDate *dateFrom = g_date_new();
GDate *dateTo = g_date_new();
processDate(dateFrom, inventory->dateEntryFrom->entry); // functions.h
processDate(dateTo, inventory->dateEntryTo->entry); // functions.h
float min, max;
min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(inventory->searchSpinMin));
max = gtk_spin_button_get_value(GTK_SPIN_BUTTON(inventory->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(inventory->costSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "cost", NULL, minChar, maxChar, dateFrom, dateTo, FALSE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->priceSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "price", NULL, minChar, maxChar, dateFrom, dateTo, FALSE, FALSE);
g_free(minChar);
g_free(maxChar);
}
else
printMessage(ERROR_SEARCH_NUMBER, inventory->mainWindow);
gtk_tree_view_set_model(GTK_TREE_VIEW(inventory->inventoryTree), GTK_TREE_MODEL(store));
calculateTreeTotals(store, inventory);
g_object_unref(store);
g_date_free(dateFrom), g_date_free(dateTo);
}
/* Prepare to pull inventory data from the database */
static void getInventory(GtkWidget *widget, intrackInventory *inventory) {
/* Clear out selected item before refreshing the tree */
g_free(inventory->selectedItemCode);
inventory->selectedItemCode = g_strconcat(NULL, NULL);
//gtk_widget_set_sensitive(inventory->removeItemButton, FALSE);
//gtk_widget_set_sensitive(inventory->invenViewItemButton, FALSE);
//gtk_widget_set_sensitive(inventory->editItemButton, FALSE);
gtk_widget_set_sensitive(inventory->returnButton, FALSE);
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, G_TYPE_STRING, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT);
GDate *dateFrom = g_date_new();
GDate *dateTo = g_date_new();
processDate(dateFrom, inventory->dateEntryFrom->entry); // functions.h
processDate(dateTo, inventory->dateEntryTo->entry); // functions.h
/* Checks to see if a ' is entered, that causes mysql.h to break */
if(g_strrstr(gtk_entry_get_text(GTK_ENTRY(inventory->inventorySearchEntry)), "'")) {
printMessage("ERROR: ' not allowed.", inventory->mainWindow);
gtk_entry_set_text(GTK_ENTRY(inventory->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(inventory->inventorySearchEntry))) > 2) {
gchar *searchString;
searchString = g_strconcat(gtk_entry_get_text(GTK_ENTRY(inventory->inventorySearchEntry)), NULL);
float min, max;
min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(inventory->searchSpinMin));
max = gtk_spin_button_get_value(GTK_SPIN_BUTTON(inventory->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(inventory->costSearch))) {
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->barcodeSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partNo", searchString, minChar, maxChar, dateFrom, dateTo, TRUE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->descriptionSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partDesc", searchString, minChar, maxChar, dateFrom, dateTo, TRUE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->soldToSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "soldTo", searchString, minChar, maxChar, dateFrom, dateTo, TRUE, FALSE);
pullInventory(inventory, store, inventory->inventoryTable, "invoiceNo", searchString, minChar, maxChar, dateFrom, dateTo, TRUE, FALSE);
}
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->priceSearch))) {
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->barcodeSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partNo", searchString, minChar, maxChar, dateFrom, dateTo, FALSE, TRUE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->descriptionSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partDesc", searchString, minChar, maxChar, dateFrom, dateTo, FALSE, TRUE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->soldToSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "soldTo", searchString, minChar, maxChar, dateFrom, dateTo, FALSE, TRUE);
pullInventory(inventory, store, inventory->inventoryTable, "invoiceNo", searchString, minChar, maxChar, dateFrom, dateTo, FALSE, TRUE);
}
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->priceSearch)) == FALSE && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->costSearch)) == FALSE) {
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->barcodeSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partNo", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->descriptionSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partDesc", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->soldToSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "soldTo", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
pullInventory(inventory, store, inventory->inventoryTable, "invoiceNo", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
}
g_free(minChar);
g_free(maxChar);
}
else {
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->barcodeSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partNo", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->descriptionSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "partDesc", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(inventory->soldToSearch)))
pullInventory(inventory, store, inventory->inventoryTable, "soldTo", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
pullInventory(inventory, store, inventory->inventoryTable, "invoiceNo", searchString, NULL, NULL, dateFrom, dateTo, FALSE, FALSE);
}
g_free(searchString);
gtk_entry_set_text(GTK_ENTRY(inventory->inventorySearchEntry), "");
}
/* If the search entry is less than 3 characters but greater than 0 */
else if(strlen(gtk_entry_get_text(GTK_ENTRY(inventory->inventorySearchEntry))) > 0 && strlen(gtk_entry_get_text(GTK_ENTRY(inventory->inventorySearchEntry))) < 3) {
printMessage(ERROR_SEARCH_TERMS, inventory->mainWindow);
gtk_entry_set_text(GTK_ENTRY(inventory->inventorySearchEntry), "");
}
else
pullInventory(inventory, store, inventory->inventoryTable, NULL, NULL, NULL, NULL, dateFrom, dateTo, FALSE, FALSE); /* This pulls all the inventory out of the database */
gtk_tree_view_set_model(GTK_TREE_VIEW(inventory->inventoryTree), GTK_TREE_MODEL(store));
calculateTreeTotals(store, inventory);
g_object_unref(store);
g_date_free(dateFrom), g_date_free(dateTo);
}
/* Pulls the inventory data out of the database and stores it into a GtkListStore */
static int pullInventory(intrackInventory *inventory, GtkListStore *store, gchar *inventoryTable, gchar *searchWhere, gchar *searchString, gchar *searchNumMin, gchar *searchNumMax, GDate *dateFrom, GDate *dateTo, gboolean costPull, gboolean pricePull) {
GtkTreeIter iter;
int i;
int num_fields;
gchar *query_string;
int query_state;
MYSQL *partsConnection, partsMysql;
MYSQL_RES *partsResult;
MYSQL_ROW partsRow;
mysql_init(&partsMysql);
/* Open connection to the database */
partsConnection = mysql_real_connect(&partsMysql,mysqlServer,mysqlUsername,mysqlPassword,MYSQL_DEFAULT_DATABASE, 0, 0, 0);
if(partsConnection == NULL) {
printf(mysql_error(partsConnection), "%d\n");
printMessage("CONNECTION FAILED", NULL);
mysql_close(partsConnection);
return 1;
}
// Select the database.
query_string = g_strconcat(mysqlDatabase, NULL);
query_state = mysql_select_db(partsConnection, query_string);
// Failed to connect and select database.
if (query_state != 0) {
printf(mysql_error(partsConnection), "%d\n");
printMessage(ERROR_DATABASE, NULL);
g_free(query_string);
mysql_close(partsConnection);
return 1;
}
g_free(query_string);
gchar *searchDateFrom;
gchar *searchDateTo;
gchar *searchDateCompFrom, *searchDateCompTo;
gchar *yearFromChar, *monthFromChar, *dayFromChar;
gchar *yearToChar, *monthToChar, *dayToChar;
gchar *yearFromCompChar, *yearToCompChar;
yearToChar = g_strdup_printf("%i", dateTo->year);
monthToChar = g_strdup_printf("%i", dateTo->month);
dayToChar = g_strdup_printf("%i", dateTo->day);
yearFromChar = g_strdup_printf("%i", dateFrom->year);
monthFromChar = g_strdup_printf("%i", dateFrom->month);
dayFromChar= g_strdup_printf("%i", dateFrom->day);
yearToCompChar = g_strdup_printf("%i", dateTo->year - 1);
yearFromCompChar = g_strdup_printf("%i", dateFrom->year - 1);
searchDateFrom = g_strconcat(yearFromChar, "-", monthFromChar, "-", dayFromChar, " 00:00:00", NULL);
searchDateTo = g_strconcat(yearToChar, "-", monthToChar, "-", dayToChar, " 23:59:59", NULL);
searchDateCompFrom = g_strconcat(yearFromCompChar, "-", monthFromChar, "-", dayFromChar, " 00:00:00", NULL);
searchDateCompTo = g_strconcat(yearToCompChar, "-", monthToChar, "-", dayToChar, " 23:59:59", NULL);
g_free(yearFromChar);
g_free(monthFromChar);
g_free(dayFromChar);
g_free(yearToChar);
g_free(monthToChar);
g_free(dayToChar);
g_free(yearToCompChar);
g_free(yearFromCompChar);
// Search for the user entered query
if(searchString != NULL) {
if(searchNumMin != NULL && searchNumMax != NULL) {
if(costPull == TRUE)
query_string = g_strconcat("SELECT id, partNo, partDesc, cost, price, dateSold, soldTo, invoiceNo FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateFrom, "' AND dateSold < '", searchDateTo, "' AND cost >= ", searchNumMin, " AND cost <= ", searchNumMax, " AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
else if(pricePull == TRUE)
query_string = g_strconcat("SELECT id, partNo, partDesc, cost, price, dateSold, soldTo, invoiceNo FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateFrom, "' AND dateSold < '", searchDateTo, "' AND price >= ", searchNumMin, " AND price <= ", searchNumMax, " AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
else
query_string = g_strconcat("SELECT id, partNo, partDesc, cost, price, dateSold, soldTo, invoiceNo FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateFrom, "' AND dateSold < '", searchDateTo, "' AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
}
else
query_string = g_strconcat("SELECT id, partNo, partDesc, cost, price, dateSold, soldTo, invoiceNo FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateFrom, "' AND dateSold < '", searchDateTo, "' AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
}
else if(searchNumMin != NULL && searchNumMax != NULL)
query_string = g_strconcat("SELECT id, partNo, partDesc, cost, price, dateSold, soldTo, invoiceNo FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateFrom, "' AND dateSold < '", searchDateTo, "' AND ", searchWhere, " >= ", searchNumMin, " AND ", searchWhere, " <= ", searchNumMax, " ORDER BY dateSold DESC", NULL);
else
query_string = g_strconcat("SELECT id, partNo, partDesc, cost, price, dateSold, soldTo, invoiceNo FROM `", SOLD_TABLES, "` WHERE dateSold > '", searchDateFrom, "' AND dateSold < '", searchDateTo, "' ORDER BY dateSold DESC", NULL);
mysql_query(partsConnection, query_string);
g_free(searchDateTo);
g_free(searchDateFrom);
//Keep a copy of the current query for export purposes.
g_free(inventory->exportQueryString);
inventory->exportQueryString = g_strconcat(query_string, NULL);
//If the connection is successful and the query returns a result then the next step is to display those results:
partsResult = mysql_store_result(partsConnection);
num_fields = mysql_num_fields(partsResult);
while ((partsRow = mysql_fetch_row(partsResult))) {
gchar *id, *barcode, *description, *cost, *price, *dateSold, *soldTo, *invoiceNo;
float costFloat = 0.00;
float profitFloat = 0.00;
float priceFloat = 0.00;
float marginFloat = 0.00;
for(i = 0; i < num_fields; i++) {
if(i == 0)
id = g_strconcat(partsRow[i], NULL);
if(i == 1)
barcode = g_strconcat(partsRow[i], NULL);
if(i == 2)
description = g_strconcat(partsRow[i], NULL);
if(i == 3) {
cost = g_strdup_printf("%.2f", atof(partsRow[i]));
costFloat = atof(partsRow[i]);
//cost = g_strconcat(partsRow[i], NULL);
}
if(i == 4) {
price = g_strdup_printf("%.2f", atof(partsRow[i]));
priceFloat = atof(partsRow[i]);
//price = g_strconcat(partsRow[i], NULL);
}
if(i == 5)
dateSold = g_strconcat(partsRow[i], NULL);
if(i == 6)
soldTo = g_strconcat(partsRow[i], NULL);
if(i == 7)
invoiceNo = g_strdup(partsRow[i]);
//g_print("%i\n", i);
}
gchar *profitChar;
gchar *marginChar;
profitChar = g_strdup_printf("%.2f", atof(price) - atof(cost));
profitFloat = priceFloat - costFloat;
marginFloat = ((priceFloat - costFloat) / priceFloat) * 100;
marginChar = g_strdup_printf("%.2f%%", marginFloat);
//*********************************************************************
/* Processing The Dates */
GDate *dateTemp, *dateInvoiced, *dateToday;
int yearScan, monthScan, dayScan;
dateTemp = g_date_new();
dateInvoiced = g_date_new();
dateToday = g_date_new();
g_date_set_time_t(dateToday, time(NULL)); // Get today's date.
sscanf(dateSold, "%d-%d-%d", &yearScan, &monthScan, &dayScan);
gchar *dateStrTemp = g_strdup_printf("%d-%d-%d", yearScan, monthScan, dayScan);
g_date_set_parse(dateInvoiced, dateStrTemp);
g_date_set_parse(dateTemp, dateStrTemp);
//g_date_add_days(dateTemp, -30);
gboolean validInvoiceDate = FALSE;
if(g_date_compare(dateInvoiced, dateFrom) >=0) {
if(g_date_compare(dateInvoiced, dateTo) <= 0)
validInvoiceDate = TRUE;
else
validInvoiceDate = FALSE;
}
else
validInvoiceDate = FALSE;
//************************************************************************
/* Now search the tree store to see if the item already exists in it, we search via the id column 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, ID, &rowtext, -1); // the id column
if(rowtext != NULL) {
// Found item
if(!strcmp(rowtext, id))
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, ID, &rowtext, -1); // the id column
if(rowtext != NULL) {
// Found item
if(!strcmp(rowtext, id)) {
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 && validInvoiceDate == TRUE) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, ID, id, BARCODE, barcode, DESCRIPTION, description, COST, cost, PROFIT, profitChar, MARGIN, marginChar, PRICE, price, SOLDTO, soldTo, LAST_SOLD, dateSold, INVOICENO, invoiceNo, COSTF, costFloat, PROFITF, profitFloat, MARGINF, marginFloat, PRICEF, priceFloat, -1);
}
}
else {
/* This stores all inventory into the tree */
if(validInvoiceDate == TRUE) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, ID, id, BARCODE, barcode, DESCRIPTION, description, COST, cost, PROFIT, profitChar, MARGIN, marginChar, PRICE, price, SOLDTO, soldTo, LAST_SOLD, dateSold, INVOICENO, invoiceNo, COSTF, costFloat, PROFITF, profitFloat, MARGINF, marginFloat, PRICEF, priceFloat, -1);
}
}
g_free(id);
g_free(barcode);
g_free(description);
g_free(cost);
g_free(profitChar);
g_free(marginChar);
g_free(price);
g_free(dateSold);
g_free(soldTo);
g_free(invoiceNo);
g_date_free(dateTemp), g_date_free(dateToday), g_date_free(dateInvoiced), g_free(dateStrTemp);
}
g_free(query_string);
mysql_free_result(partsResult);
//***************************************Prev year comparo*********************
int dateCheckFrom = dateFrom->year - 1;
int dateCheckTo = dateTo->year - 1;
gchar *numberOfPartsCharPrev = NULL;
gchar *partCostsCharPrev = NULL;
gchar *partSalesCharPrev = NULL;
gchar *partProfitsCharPrev = NULL;
gchar *partMarginCharPrev = NULL;
// Only run the date check comparison data on years 2011+ since thats when data recording started.
if(dateCheckFrom > 2010 && dateCheckTo > 2010) {
// Search for the user entered query
if(searchString != NULL) {
if(searchNumMin != NULL && searchNumMax != NULL) {
if(costPull == TRUE)
query_string = g_strconcat("SELECT DISTINCT(id), cost, price FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateCompFrom, "' AND dateSold < '", searchDateCompTo, "' AND cost >= ", searchNumMin, " AND cost <= ", searchNumMax, " AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
else if(pricePull == TRUE)
query_string = g_strconcat("SELECT DISTINCT(id), cost, price FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateCompFrom, "' AND dateSold < '", searchDateCompTo, "' AND price >= ", searchNumMin, " AND price <= ", searchNumMax, " AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
else
query_string = g_strconcat("SELECT DISTINCT(id), cost, price FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateCompFrom, "' AND dateSold < '", searchDateCompTo, "' AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
}
else
query_string = g_strconcat("SELECT DISTINCT(id), cost, price FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateCompFrom, "' AND dateSold < '", searchDateTo, "' AND ", searchWhere, " LIKE '%", searchString, "%'", " ORDER BY dateSold DESC", NULL);
}
else if(searchNumMin != NULL && searchNumMax != NULL)
query_string = g_strconcat("SELECT DISTINCT(id), cost, price FROM `", SOLD_TABLES, "`", " WHERE dateSold > '", searchDateCompFrom, "' AND dateSold < '", searchDateCompTo, "' AND ", searchWhere, " >= ", searchNumMin, " AND ", searchWhere, " <= ", searchNumMax, " ORDER BY dateSold DESC", NULL);
else
query_string = g_strconcat("SELECT DISTINCT(id), cost, price FROM `", SOLD_TABLES, "` WHERE dateSold > '", searchDateCompFrom, "' AND dateSold < '", searchDateCompTo, "' ORDER BY dateSold DESC", NULL);
mysql_query(partsConnection, query_string);
// If the connection is successful and the query returns a result then the next step is to display those results:
partsResult = mysql_store_result(partsConnection);
num_fields = mysql_num_fields(partsResult);
g_free(searchDateCompTo);
g_free(searchDateCompFrom);
float totalCostFloatComp = 0.00;
float totalPriceFloatComp = 0.00;
int iComp = 0;
while ((partsRow = mysql_fetch_row(partsResult))) {
float costFloatComp = 0.00;
float priceFloatComp = 0.00;
for(i = 0; i < num_fields; i++) {
if(i == 1)
costFloatComp = atof(partsRow[i]);
if(i == 2)
priceFloatComp = atof(partsRow[i]);
}
totalCostFloatComp = totalCostFloatComp + costFloatComp;
totalPriceFloatComp = totalPriceFloatComp + priceFloatComp;
iComp++;
}
float profitFloatComp = 0.00;
float marginFloatComp = 0.00;
profitFloatComp = totalPriceFloatComp - totalCostFloatComp;
marginFloatComp = ((totalPriceFloatComp - totalCostFloatComp) / totalPriceFloatComp) * 100;
inventory->numberOfPartsPrev = iComp;
inventory->partCostsPrev = totalCostFloatComp;
inventory->partSalesPrev = totalPriceFloatComp;
inventory->partProfitsPrev = profitFloatComp;
inventory->partMarginPrev = marginFloatComp;
g_free(query_string);
mysql_free_result(partsResult);
query_string = g_strdup_printf("%i", iComp);
numberOfPartsCharPrev = g_strconcat(query_string, NULL);
g_free(query_string);
query_string = g_strdup_printf("%'0.2f", totalCostFloatComp);
partCostsCharPrev = g_strconcat(query_string, NULL);
g_free(query_string);
query_string = g_strdup_printf("%'0.2f", totalPriceFloatComp);
partSalesCharPrev = g_strconcat(query_string, NULL);
g_free(query_string);
query_string = g_strdup_printf("%'0.2f", profitFloatComp);
partProfitsCharPrev = g_strconcat(query_string, NULL);
g_free(query_string);
query_string = g_strdup_printf("%'0.2f%%", marginFloatComp);
partMarginCharPrev = g_strconcat(query_string, NULL);
g_free(query_string);
}
else {
numberOfPartsCharPrev = g_strconcat("-", NULL);
partCostsCharPrev = g_strconcat("-", NULL);
partSalesCharPrev = g_strconcat("-", NULL);
partProfitsCharPrev = g_strconcat("-", NULL);
partMarginCharPrev = g_strconcat("-", NULL);
inventory->numberOfPartsPrev = 0;
inventory->partCostsPrev = 0.00;
inventory->partSalesPrev = 0.00;
inventory->partProfitsPrev = 0.00;
inventory->partMarginPrev = 0.00;
}
gtk_label_set_text(GTK_LABEL(inventory->numberOfPartsLabelPrev), numberOfPartsCharPrev);
gtk_label_set_text(GTK_LABEL(inventory->partCostsLabelPrev), partCostsCharPrev);
gtk_label_set_text(GTK_LABEL(inventory->partSalesLabelPrev), partSalesCharPrev);
gtk_label_set_text(GTK_LABEL(inventory->partProfitsLabelPrev), partProfitsCharPrev);
gtk_label_set_text(GTK_LABEL(inventory->partMarginLabelPrev), partMarginCharPrev);
g_free(numberOfPartsCharPrev), g_free(partCostsCharPrev), g_free(partSalesCharPrev), g_free(partProfitsCharPrev), g_free(partMarginCharPrev);
//***************************************End of prev year comparo*********************
mysql_close(partsConnection);
return 0;
}
/* Gets data about a item before removing it from the part sales and returning it to stock */
static int getItemData(intrackInventory *inventory, removeSale *oldData, gchar *partNo) {
gchar *query_string;
int i;
int num_fields;
int query_state;
if(connectToServer() == 1) {
return 1;
}
// Select the database.
query_string = g_strconcat(inventory->mysqlDatabase, NULL);
query_state = mysql_select_db(connection, query_string);
//g_print("%s\n", 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);
query_string = g_strconcat("SELECT id, partNo, cost, costAvg, stock, price FROM `", INVENTORY_TABLES, "`", " WHERE partNo='", partNo, "'", NULL);
//g_print("%s\n", query_string);
query_state=mysql_query(connection, query_string);
g_free(query_string);
/* Failed to update the data */
if (query_state !=0) {