-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpos.c
412 lines (405 loc) · 18.3 KB
/
pos.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
// Welcome to the program. The declaration of the functions and the library used is in .h file
#include "main.h"
CUSTOMER CurrentCustomer;
int isCustomerHasId;
INVENTORY inventoryHist[1000]; // Save 1000 Transaction per a purchase
int inventoryCounter[1000]; // Count the amount of each inventory
int inventoryHistRecordCount = 0; // Iterator of inventoryHist
int histStart, histStop; // Line pointer for printing the transaction list
double subTotal, tax, total, pointEarn, totalProfit, discount, pointUsed;
char promotionId[140];
double promotionPrice;
int savedToDatabase;
int screenStep = 0;
void cashierInterface (int customerIdNotFound) {
//Interface that will ask for customer ID
// Initial Values
isCustomerHasId = 0;
inventoryHistRecordCount = 0;
subTotal = 0, tax = 0, total = 0, pointEarn = 0, totalProfit = 0;
promotionPrice = 0;
discount = 0;
pointUsed = 0;
savedToDatabase = 0;
screenClear ();
char buffer[140];
sprintf (buffer, "Cashier: %s %s", Session.user.firstname, Session.user.lastname);
banner (Setting.storeName, Setting.storeAddress, "", buffer);
bannerBlankBorder ();
sprintf (buffer, "My name is %s %.1s. and I am your cashier", Session.user.firstname, Session.user.lastname);
bannerBlankBorderTextCen (buffer);
bannerBlankBorder ();
if ( customerIdNotFound )
bannerBlankBorderTextCen ("Customer not found");
else
bannerBlankBorder ();
bannerBlankBorderTextCen ("Please scan / enter in customer ID");
for ( int i = 25; i > 0; i-- )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to quit | Press ENTER to skip | Type 'B' to back");
bannerFullBorder ();
bannerUserInput ();
// Receives input + let user type in the member ID or type N or type ENTER or type B
// Call this if the input is not N or B -> delay (5); cashierInterfaceInventory ();
char customerId[MAX_LNG_ID];
if ( superscanf (customerId)) {
if ( strlen (customerId) == 1 ) {
// User type the switch
switch ( toupper (customerId[0])) {
case 'Q':
terminate ();
break;
case 'B':
switchHub ();
break;
default:
cashierInterface (0);
}
} else {
// User input the customerId
if ( customerSelectById (customerId, CurrentCustomer.firstname, CurrentCustomer.lastname,
&CurrentCustomer.gender, &CurrentCustomer.point, &CurrentCustomer.totalBuy)) {
isCustomerHasId = 1;
strcpy (CurrentCustomer.id, customerId);
cashierInterfaceInventory (0);
} else {
cashierInterface (1);
}
}
} else { // Press only ENTER to skip
cashierInterfaceInventory (0);
}
}
void cashierInterfaceInventory (int isError) {
// Interface that will remove the item from the database
screenClear ();
char buffer1[140], buffer2[140];
int maxItemOnScreen = 21;
histStart = 0 + (inventoryHistRecordCount / (maxItemOnScreen + 1)) * maxItemOnScreen;
histStop = inventoryHistRecordCount;
sprintf (buffer1, "%s %s is serving you today", Session.user.firstname, Session.user.lastname);
if ( isCustomerHasId )
sprintf (buffer2, "Welcome back, K. %s %s", CurrentCustomer.firstname, CurrentCustomer.lastname);
else
sprintf (buffer2, "Welcome back, Customer");
banner (Setting.storeName, Setting.storeAddress, buffer1, buffer2);
bannerBlankBorder ();
bannerBlankBorderTextLeft (
" ID | Item name | Count | Price "); // Keep these on forever
bannerBlankBorderTextLeft (
" ------------------ | ------------------------------------------------------------------------------------- | ---------- | ---------- ");
// Following by LAST `maxItemOnScreen` items that have been scanned....
int lineCounter = 0;
int i = histStart;
while ( i < histStop ) {
printf (":: %-18s | %-85s | %10d | %10.2lf ::\n", inventoryHist[i].id, inventoryHist[i].name,
inventoryCounter[i], inventoryCounter[i] * inventoryHist[i].price);
i++;
lineCounter++;
}
for ( ; lineCounter < maxItemOnScreen; lineCounter++ )
bannerBlankBorderTextLeft (
" | | | ");
char text[140];
bannerBlankBorder ();
sprintf (text, "Sub Total %.2f", subTotal);
bannerBlankBorderText (text); // Total Price - 7% (Shows only 2 significant point!!)
sprintf (text, "Tax %.2f", tax);
bannerBlankBorderText (text); // VAT calculated from total price
sprintf (text, "Total %.2f", total);
bannerBlankBorderText (text); // Total Price
if ( isCustomerHasId ){
sprintf (text, "Points you will earned %.2f", pointEarn);
bannerBlankBorderText (text);
} else
bannerBlankBorder ();
if ( isError )
bannerBlankBorderTextCen ("No item in the inventory");
else
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to quit | Press ENTER to finish | Type 'V' to void");
bannerFullBorder ();
bannerUserInput ();
// Buffer
char inventoryName[MAX_LNG_SCREEN];
double inventoryPrice;
double inventoryProfit; // Profit per item
unsigned int inventoryCategoryId; // Category ID
unsigned int inventoryRemain;
// Receives item ID -> Query on database -> Find a match (if stock = 0 means no match) -> Show a match -> Add price to the total -> Remove stock from DB of 1
char inventoryIdInput[MAX_LNG_SCREEN];
int isDuplicate;
if ( superscanf (inventoryIdInput)) {
if ( strlen (inventoryIdInput) == 1 ) { // User type the switch
switch ( toupper (inventoryIdInput[0])) {
case 'Q':
terminate ();
break;
case 'V':
cashierInterface (0);
break;
default:
cashierInterfaceInventory (0);
}
} else { // User scan the inventory's barcode
if ( inventorySelectById (inventoryIdInput, inventoryName, &inventoryPrice, &inventoryProfit,
&inventoryCategoryId, &inventoryRemain)) {
isDuplicate = 0;
for ( int i = 0; i < inventoryHistRecordCount; i++ ) {
if ( strcmp (inventoryHist[i].id, inventoryIdInput) == 0 ) {
isDuplicate = 1; // Duplicate inventory detector
inventoryHist[i].remain--;
inventoryCounter[i]++;
break;
}
}
if ( !isDuplicate ) {
strcpy (inventoryHist[inventoryHistRecordCount].id, inventoryIdInput);
strcpy (inventoryHist[inventoryHistRecordCount].name, inventoryName);
inventoryHist[inventoryHistRecordCount].price = inventoryPrice;
inventoryHist[inventoryHistRecordCount].profit = inventoryProfit;
inventoryHist[inventoryHistRecordCount].categoryId = inventoryCategoryId;
inventoryHist[inventoryHistRecordCount].remain = inventoryRemain - 1;
inventoryCounter[inventoryHistRecordCount] = 1;
inventoryHistRecordCount++;
}
total += inventoryPrice;
tax = total * 0.07;
subTotal = total - tax;
pointEarn = Setting.priceToPoint * total;
totalProfit += inventoryProfit;
cashierInterfaceInventory (0);
} else
cashierInterfaceInventory (1);
}
} else
cashierInterfaceDiscount (0);
}
void cashierInterfaceDiscount (int errorCode) {
// Interface that will ask for discount (voucher and points)
if ( inventoryHistRecordCount == 0 ) { // If no input of inventory then return back to add the inventory again
cashierInterfaceInventory (0);
screenClear ();
char buffer1[140], buffer2[140];
sprintf (buffer1, "%s %s is serving you today", Session.user.firstname, Session.user.lastname);
if ( isCustomerHasId )
sprintf (buffer2, "Welcome back, K. %s %s", CurrentCustomer.firstname, CurrentCustomer.lastname);
else
sprintf (buffer2, "Welcome back, Customer");
banner (Setting.storeName, Setting.storeAddress, buffer1, buffer2);
bannerBlankBorder ();
bannerBlankBorderTextCen ("Scan a Voucher / Discount Code");
bannerBlankBorder ();
char text[140];
int promotionStatus; // 0 = Used | 1 = Active
if ( isCustomerHasId ) { // Customer is a Member
sprintf (text, "You have %.2lf points", CurrentCustomer.point);
bannerBlankBorderTextCen (text);
if ( errorCode == 1 ) {
for ( int i = 26; i > 0; i-- )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Invalid Promotion Code, Please try another one");
} else
for ( int i = 26; i > 0; i-- )
bannerBlankBorder ();
bannerBlankBorderTextCen (
"Type 'Q' to quit | Type 'V' to void | Press ENTER to skip | Type 'P' to use point | Type 'A' to add more item");
bannerFullBorder ();
bannerUserInput ();
if ( superscanf (promotionId)) {
if ( strlen (promotionId) == 1 ) {
switch ( toupper (promotionId[0])) {
case 'Q':
terminate ();
break;
case 'V':
cashierInterface (0);
break;
case 'P':
// Use point
cashierInterfaceResult (1);
break;
case 'A':
cashierInterfaceInventory (0);
break;
default:
cashierInterfaceDiscount (0);
}
} else { // Use Voucher
if ( promotionSelectById (promotionId, &promotionPrice, &promotionStatus) && promotionStatus == 1 ) {
discount = promotionPrice;
cashierInterfaceResult (0);
} else
cashierInterfaceDiscount (1);
}
} else { // Skipping
cashierInterfaceResult (0);
}
} else { // Customer is not a member
for ( int i = 27; i > 0; i-- )
bannerBlankBorder ();
bannerBlankBorderTextCen (
"Type 'Q' to quit | Type 'V' to void | Press ENTER to skip | Type 'A' to add more item");
bannerFullBorder ();
bannerUserInput ();
if ( superscanf (promotionId)) {
if ( strlen (promotionId) == 1 ) { // Use Command
switch ( toupper (promotionId[0])) {
case 'Q':
terminate ();
break;
case 'V':
cashierInterface (0);
break;
case 'A':
cashierInterfaceInventory (0);
break;
default:
cashierInterfaceDiscount (0);
}
} else { // Use Voucher
if ( promotionSelectById (promotionId, &promotionPrice, &promotionStatus) && promotionStatus == 1 ) {
discount = promotionPrice;
cashierInterfaceResult (0);
} else
cashierInterfaceDiscount (1);
}
} else {
cashierInterfaceResult (0);
}
}
}
}
void cashierInterfaceResult (int usePoint) {
// Interface that will show the total (just like the receipt)
screenClear ();
// Saving the purchase into DB ----------------------------------------------------------
int i, j;
if ( !savedToDatabase ) { // Doing this one time !!!
if ( usePoint ) { // Use point
double discountByPoint =
CurrentCustomer.point / Setting.pointToPrice; // Money that will be use to discount from totalPrice
if ( discountByPoint <= total ) {
discount = discountByPoint;
pointUsed = CurrentCustomer.point;
customerUpdatePoint (CurrentCustomer.id, 0);
} else {
discount = total;
pointUsed = Setting.pointToPrice * total;
customerUpdatePoint (CurrentCustomer.id, Setting.pointToPrice * total);
}
} else if ( discount > 0 ) {
// Use Voucher + Change status of voucher from Active -> Used
promotionUpdateStatus (promotionId, 0);
// Discount boundary
discount = (discount < total) ? discount : total;
}
// Saving all transactions into the Database
for ( i = 0; i < inventoryHistRecordCount; i++ ) {
// Decreasing items the in stock
inventoryUpdateRemain (inventoryHist[i].id, inventoryHist[i].remain);
for ( j = 0; j < inventoryCounter[i]; j++ )
transactionInsert (RecordCount.purchase, inventoryHist[i].id);
}
// Update total points & total buy of customer
if ( isCustomerHasId ) { // If customer is a member
customerUpdatePoint (CurrentCustomer.id, CurrentCustomer.point + pointEarn - pointUsed);
customerUpdatetotalBuy (CurrentCustomer.id, CurrentCustomer.totalBuy + total - discount);
}
// Saving a purchase into the Database
if ( isCustomerHasId )
purchaseInsert (total, discount, totalProfit, CurrentCustomer.id, Session.user.id);
else
purchaseInsert (total, discount, totalProfit, "", Session.user.id);
savedToDatabase = 1;
}
char buffer1[140], buffer2[140];
int maxItemOnScreen = 18;
histStart = 0 + screenStep;
histStop = inventoryHistRecordCount;
sprintf (buffer1, "%s %s is serving you today", Session.user.firstname, Session.user.lastname);
if ( isCustomerHasId )
sprintf (buffer2, "Welcome back, K. %s %s", CurrentCustomer.firstname, CurrentCustomer.lastname);
else
sprintf (buffer2, "Welcome back, Customer");
banner (Setting.storeName, Setting.storeAddress, buffer1, buffer2);
char text[140];
bannerBlankBorder ();
bannerBlankBorderTextLeft (
" ID | Item name | Count | Price "); // Keep these on forever
bannerBlankBorderTextLeft (
" ------------------ | ---------------------------------------------------------------------------------- | ----------- | ------------ ");
// Shows a total of 25 item at a time. Press ENTER to go to the next page
int lineCounter = 0;
i = histStart;
while ( i < histStop ) {
if ( lineCounter == maxItemOnScreen )
break;
printf (":: %-18s | %-82s | %11d | %12.2lf ::\n", inventoryHist[i].id, inventoryHist[i].name,
inventoryCounter[i], inventoryCounter[i] * inventoryHist[i].price);
i++;
lineCounter++;
}
for ( ; lineCounter < maxItemOnScreen; lineCounter++ )
bannerBlankBorderTextLeft (
" | | | ");
if ( isCustomerHasId ) {
// If customer is a member
bannerBlankBorderText ("|----------Membership----------| |----------Purchase---------|");
strcpy (text, "");
sprintf (text, "| Points you have : %8.2f | | Sub Total : %11.2f |", CurrentCustomer.point, subTotal);
bannerBlankBorderText (text);
sprintf (text, "| Points you used : %8.2f | | VAT : %11.2f |", pointUsed, tax);
bannerBlankBorderText (text);
strcpy (text, "");
sprintf (text, "| Points you earn : %8.2f | | Discount : %+11.2f |", pointEarn, -discount);
bannerBlankBorderText (text);
bannerBlankBorderText ("|______________________________| |___________________________|");
sprintf (text, "| Total : %8.2f | | Total : %11.2f |", CurrentCustomer.point - pointUsed + pointEarn, total - discount);
} else {
// If customer is not a member
bannerBlankBorderText ("|----------Purchase---------|");
strcpy (text, "");
sprintf (text, "| Sub Total : %11.2f |", subTotal);
bannerBlankBorderText (text);
sprintf (text, "| VAT : %11.2f |", tax);
bannerBlankBorderText (text);
strcpy (text, "");
sprintf (text, "| Discount : %+11.2f |", -discount);
bannerBlankBorderText (text);
bannerBlankBorderText ("|___________________________|");
sprintf (text, "| Total : %11.2f |", total - discount);
}
bannerBlankBorderText (text);
bannerBlankBorderTextCen ("_______________________________________________________________");
strcpy (text, "");
sprintf (text, "THANK YOU FOR SHOPPING AT %s", Setting.storeName);
bannerBlankBorderTextCen (text);
bannerBlankBorder ();
if ( screenStep < inventoryHistRecordCount - maxItemOnScreen )
bannerBlankBorderTextCen (
"Type 'Q' to quit | Press ENTER to view more | Type 'V' to skip (Finish purchase)");
else
bannerBlankBorderTextCen ("Type 'Q' to quit | Press ENTER to finish purchase");
bannerFullBorder ();
bannerUserInput ();
char userInput[5];
if ( superscanf (userInput)) {
switch ( toupper (userInput[0])) {
case 'V':
cashierInterface (0);
break;
case 'Q':
switchHub ();
break;
default:
cashierInterfaceResult (usePoint);
}
} else { // Press ENTER
if ( screenStep < inventoryHistRecordCount - maxItemOnScreen ) {
screenStep++;
cashierInterfaceResult (0);
} else
cashierInterface (0);
}
}