]> git.sur5r.net Git - c128-kasse/blob - src/kasse.c
implement depositing when the user cannot purchase an item
[c128-kasse] / src / kasse.c
1 /* 
2  * RGB2R-C128-Kassenprogramm
3  * © 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  */
7 #define _IS_KASSE
8 #include <stdio.h>
9 #include <conio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <cbm.h>
13
14 #include "general.h"
15 #include "config.h"
16 #include "kasse.h"
17 #include "credit_manager.h"
18 #include "c128time.h"
19 #include "print.h"
20 // drucker 4 oder 5
21 // graphic 4,0,10
22
23 static void sane_exit() {
24         save_items();
25         save_credits();
26         exit(1);
27 }
28
29 /* Hauptbildschirm ausgeben */
30 static void print_screen() {
31         BYTE i = 0;
32         char *time = get_time();
33         char profit[10];
34         clrscr();
35         if (format_euro(profit, 10, money) == NULL) {
36                 cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
37                 exit(1);
38         }
39         cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r\
40 \r\nUhrzeit:     %s (wird nicht aktualisiert)\r\
41 Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
42         time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
43         cprintf("\xB0\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB2\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB2\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB2\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xAE\r\n");
44         for (; i < min(status.num_items, 15); ++i) {
45                 if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
46                         cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i].price);
47                         exit(1);
48                 }
49
50                 cprintf("\x7D%2d: %-" xstr(MAX_ITEM_NAME_LENGTH) "s \x7D%s, %3dx \x7D",
51                         i, status.status[i].item_name, profit, status.status[i].times_sold);
52                 if ((i+16) < status.num_items) {
53
54                         if (format_euro(profit, sizeof(profit), status.status[i+16].price) == NULL) {
55                                 cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i+16].price);
56                                 exit(1);
57                         }
58                         cprintf("%2d: %-" xstr(MAX_ITEM_NAME_LENGTH) "s \x7D%s, %3dx \x7D",
59                                 i+16, status.status[i+16].item_name, profit, status.status[i+16].times_sold);
60                 } else cprintf("              \x7D                \x7D");
61                 cprintf("\r\n");
62         }
63         cprintf("\xAD\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB1\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB1\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB1\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xBD\r\n");
64         cprintf(
65 "   s) Daten sichern                 d) Drucken umschalten\r\
66    g) Guthabenverwaltung            z) Zeit setzen\r\
67    f) Freitext verkaufen            q) Beenden\r\n");
68 }
69
70 /*
71  * Prints a line and logs it to file. Every line can be at max 80 characters.
72  *
73  */
74 static void print_log(char *name, int item_price, int einheiten, char *nickname, char *rest) {
75         char *time = get_time();
76         char price[10];
77         /* Format: 
78            Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!) -- 6-stellig
79            Uhrzeit -- 8-stellig
80            Eintragname (= Getränk) -- 9-stellig
81            Preis (in Cents) -- 9-stellig
82            Anzahl -- 2-stellig
83            Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
84            restguthaben (9-stellig)
85
86            + 7 leerzeichen
87            --> 48 zeichen
88            */
89         if (format_euro(price, 10, item_price) == NULL) {
90                 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
91                 exit(1);
92         }
93
94         sprintf(print_buffer, "%c[%3u] %s - %-" xstr(MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r",  17,
95                         status.transaction_id, time, name, price, rest,
96                         einheiten, (*nickname != '\0' ? nickname : "Unbekannt"));
97         status.transaction_id++;
98         print_the_buffer();
99 }
100
101 /* dialog which is called for each bought item */
102 static signed int buy(char *name, unsigned int price) {
103         int negative = 1;
104         char entered[5] = {'1', 0, 0, 0, 0};
105         BYTE i = 0, matches = 0;
106         BYTE c, nickname_len;
107         int einheiten;
108         char *input;
109         char nickname[11];
110         char rest[11];
111         struct credits_t *credit;
112
113         memset(rest, ' ', sizeof(rest));
114         rest[8] = '\0';
115
116         clrscr();
117         cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
118         while (1) {
119                 c = cgetc();
120                 if (c == 13)
121                         break;
122                 cputc(c);
123                 if (c == 27) {
124                         cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
125                         get_input();
126                         return 1;
127                 } else if (c == '-' && i == 0)
128                         negative = -1;
129                 else if (c > 47 && c < 58)
130                         entered[i++] = c;
131         }
132         einheiten = atoi(entered) * negative;
133         
134         toggle_videomode();
135         cprintf("\r\n             *** VERKAUF ***\r\n\r\n");
136         cprintf("%dx %s", einheiten, name);
137         toggle_videomode();
138         
139         cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
140         input = get_input();
141         strncpy(nickname, input, 11);
142         if (*nickname != '\0') {
143                 toggle_videomode();
144                 cprintf(" fuer %s\r\n", nickname);
145                 toggle_videomode();
146         }
147
148         if (*nickname != '\0' && *nickname != 32) {
149                 nickname_len = strlen(nickname);
150                 /* go through credits and remove the amount of money or set nickname
151                  * to NULL if no such credit could be found */
152                 credit = find_credit(nickname);
153                 if (credit != NULL) {
154                         while ((signed int)credit->credit < ((signed int)price * einheiten)) {
155                                 cprintf("%s hat nicht genug Geld. e) einzahlen a) abbruch \r\n", nickname);
156                                 c = cgetc();
157                                 if (c == 'e') {
158                                         deposit_credit(nickname);
159                                 } else {
160                                         return 0;
161                                 }
162                         }
163                         /* substract money */
164                         credit->credit -= (price * einheiten);
165
166                         if (format_euro(rest, 10, credit->credit) == NULL) {
167                                 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
168                                 exit(1);
169                         }
170
171                         cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n",
172                                 nickname, rest);
173                         toggle_videomode();
174                         cprintf("\r\nDein Guthaben betraegt noch %s.\r\n", rest);
175                         toggle_videomode();
176                         get_input();
177                         matches++;
178                 } else {
179                         cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, druecke RETURN...\r\n");
180                         get_input();
181                         return 0;
182                 }
183         } else {
184                 /* Ensure that nickname is NULL if it's empty because it's used in print_log */
185                 *nickname = '\0';
186         }
187         
188         money += price * einheiten;
189         items_sold += einheiten;
190         if (printing == 1)
191                 print_log(name, price, einheiten, nickname, rest);
192
193         return einheiten;
194 }
195
196 void buy_stock(BYTE n) {
197         if (n >= status.num_items || status.status[n].item_name == NULL) {
198                 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
199                 get_input();
200                 return;
201         }
202
203         status.status[n].times_sold += buy(status.status[n].item_name, status.status[n].price);
204 }
205
206 void buy_custom() {
207         BYTE c = 0, i = 0;
208         int negative = 1;
209         char entered[5] = {'1', 0, 0, 0, 0};
210         char *input, name[20];
211         int price;
212
213         clrscr();
214         memset(name, '\0', 20);
215         cprintf("\r\nWas soll gekauft werden?\r\n");
216         input = get_input();
217         strncpy(name, input, 20);
218         if (*name == '\0')
219                 return;
220
221         cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
222         while (1) {
223                 c = cgetc();
224                 if (c == 13)
225                         break;
226                 cputc(c);
227                 if (c == 27) {
228                         cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
229                         get_input();
230                         return;
231                 } else if (c == '-' && i == 0)
232                         negative = -1;
233                 else if (c > 47 && c < 58)
234                         entered[i++] = c;
235         }
236         price = atoi(entered) * negative;
237
238         cprintf("\r\n");
239
240         buy(name, price);
241 }
242
243 void set_time_interactive() {
244         BYTE part[3] = {'0', '0', '\0'};
245         BYTE tp1, tp2, tp3;
246         char *time_input, *time;
247         cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
248         time_input = get_input();
249         part[0] = time_input[0];
250         part[1] = time_input[1];
251         tp1 = atoi(part);
252         part[0] = time_input[2];
253         part[1] = time_input[3];
254         tp2 = atoi(part);
255         part[0] = time_input[4];
256         part[1] = time_input[5];
257         tp3 = atoi(part);
258         set_time(tp1, tp2, tp3);
259
260         time = get_time();
261         cprintf("\r\nZeit gesetzt: %s\r\n", time);
262 }
263
264 int main() {
265         char *c;
266         char *time;
267
268         if (VIDEOMODE == 40)
269                 toggle_videomode();
270         clrscr();
271         /* Set time initially, c128 doesn't know it */
272         set_time_interactive();
273
274         POKE(216, 255);
275
276         /* Load configuration */
277         load_config();
278
279         /* Load items (= drinks) */
280         load_items();
281         /* Load credits */
282         load_credits();
283
284         time = get_time();
285         sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
286         print_the_buffer();
287         sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
288         print_the_buffer();
289
290         sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, zeile %d.\r", 17, time, log_num, log_lines_written);
291         print_the_buffer();
292
293         print_header();
294
295         while (1) {
296                 print_screen();
297                 c = get_input();
298                 /* ...display dialogs eventually */
299                 if (*c > 47 && *c < 58) {
300                         /* if the input starts with a digit, we will interpret it as a number
301                          * for the item to be sold */
302                         buy_stock(atoi(c));
303                         toggle_videomode();
304                         clrscr();
305                         toggle_videomode();
306                 } else if (*c == 'f') {
307                         buy_custom();
308                         toggle_videomode();
309                         clrscr();
310                         toggle_videomode();
311                 } else if (*c == 's') {
312                         save_items();
313                         save_credits();
314                         cprintf("Statefile/Creditfile gesichert, druecke RETURN...\r\n");
315                         get_input();
316                 } else if (*c == 'd') {
317                         /* enable/disable printing */
318                         printing = (printing == 1 ? 0 : 1);
319                         cprintf("Drucken ist nun %s, druecke RETURN...\r\n", 
320                                 (printing == 1 ? "eingeschaltet" : "ausgeschaltet"));
321                         get_input();
322                 } else if (*c == 'g') {
323                         credit_manager();
324                 } else if (*c == 'z') {
325                         set_time_interactive();
326                 } else if (*c == 'q')
327                         break;
328         }
329         clrscr();
330         cprintf("\r\nBYEBYE\r\n");
331
332         return 0;
333 }