2 * RGB2R-C128-Kassenprogramm
3 * © 2007-2009 phil_fry, sECuRE, sur5r
4 * See LICENSE for license information
17 #include "credit_manager.h"
24 void print_item(BYTE i) {
26 if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
27 cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i].price);
32 textcolor(TC_LIGHT_GRAY);
33 cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s, %3dx ",
34 status.status[i].item_name, profit, status.status[i].times_sold);
37 /* Hauptbildschirm ausgeben */
38 static void print_screen(void) {
40 char *time = get_time();
43 if (format_euro(profit, 10, money) == NULL) {
44 cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
48 cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r\n");
49 textcolor(TC_LIGHT_GRAY);
50 cprintf("\r\nUhrzeit: %s (wird nicht aktualisiert)\r\n"
51 "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
52 time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
53 textcolor(TC_LIGHT_GRAY);
55 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
56 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
57 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
58 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE\r\n");
59 for (; i < min(status.num_items, 15); ++i) {
65 /* if we have more than 15 items, use the second column */
66 if ((i+15) < status.num_items) {
70 cprintf(" \xDD \xDD");
76 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
77 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
78 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
79 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD\r\n");
82 textcolor(TC_LIGHT_GRAY);
83 cprintf(") Daten sichern ");
86 textcolor(TC_LIGHT_GRAY);
87 cprintf(") Guthabenverwaltung\r\n");
90 textcolor(TC_LIGHT_GRAY);
91 cprintf(") Zeit setzen ");
94 textcolor(TC_LIGHT_GRAY);
95 cprintf(") Freitext verkaufen ");
98 textcolor(TC_LIGHT_GRAY);
99 cprintf(") Beenden\r\n");
103 * Prints a line and logs it to file. Every line can be at max 80 characters.
106 static void print_log(char *name, int item_price, int einheiten, char *nickname, char *rest) {
107 char *time = get_time();
110 Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!) -- 6-stellig
112 Eintragname (= Getränk) -- 9-stellig
113 Preis (in Cents) -- 9-stellig
115 Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
116 restguthaben (9-stellig)
121 if (format_euro(price, 10, item_price) == NULL) {
122 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
126 sprintf(print_buffer, "%c[%3u] %s - %-" xstr(MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r", 17,
127 status.transaction_id, time, name, price, rest,
128 einheiten, (*nickname != '\0' ? nickname : "Unbekannt"));
129 status.transaction_id++;
133 /* dialog which is called for each bought item */
134 static signed int buy(char *name, unsigned int price) {
136 char entered[5] = {'1', 0, 0, 0, 0};
137 BYTE i = 0, matches = 0;
138 BYTE c, x, y, nickname_len;
140 char nickname[NICKNAME_MAX_LEN+1];
142 struct credits_t *credit;
144 memset(nickname, '\0', sizeof(nickname));
145 memset(rest, ' ', sizeof(rest));
149 cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
153 /* Buffer-Ende erreicht? */
162 if (c == PETSCII_DEL) {
166 cputcxy(x+i, y, ' ');
171 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
175 if (c == '-' && i == 0) {
178 } else if (c >= PETSCII_0 && c <= PETSCII_9) {
183 /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */
185 einheiten = atoi(entered) * negative;
187 if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
188 cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, druecke RETURN...\r\n");
194 cprintf("\r\n *** VERKAUF ***\r\n\r\n");
195 cprintf("%dx %s", einheiten, name);
198 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
205 input_terminator_t terminator;
207 terminator = get_input_terminated_by(
208 INPUT_TERMINATOR_RETURN |
209 INPUT_TERMINATOR_SPACE,
213 /* Clear the screen from any previous completions */
216 for (i = 1; i < 7; i++) {
217 /* "Completion:" is longer than NICKNAME_MAX_LEN */
218 cclearxy(0, y + i, strlen("Completion:"));
222 if (terminator != INPUT_TERMINATOR_SPACE) {
228 for (i = 0; i < credits.num_items; i++) {
229 if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) != 0) {
236 uniquematch = credits.credits[i].nickname;
239 /* Display the rest of the nickname */
240 textcolor(TC_LIGHT_GREEN);
241 cprintf("%s", uniquematch + strlen(nickname));
242 textcolor(TC_LIGHT_GRAY);
243 strcat(nickname, uniquematch + strlen(nickname));
245 /* Multiple nicknames match what was entered so far. Abort and
246 * display all matches, then prompt the user again. */
247 char completion[NICKNAME_MAX_LEN+1];
248 BYTE len = strlen(nickname);
251 cprintf("\r\nCompletion:\r\n");
253 for (i = 0; i < credits.num_items; i++) {
254 if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
257 if (++matches == 5) {
261 strcpy(completion, credits.credits[i].nickname);
262 *(completion + len) = '\0';
263 cprintf("%s", completion);
264 textcolor(TC_LIGHT_GREEN);
265 cprintf("%c", *(credits.credits[i].nickname + len));
266 textcolor(TC_LIGHT_GRAY);
267 cprintf("%s\r\n", completion + len + 1);
273 if (*nickname != '\0') {
275 cprintf(" fuer %s\r\n", nickname);
279 if (*nickname != '\0' && *nickname != 32) {
280 nickname_len = strlen(nickname);
281 /* go through credits and remove the amount of money or set nickname
282 * to NULL if no such credit could be found */
283 credit = find_credit(nickname);
284 if (credit != NULL) {
285 while ((signed int)credit->credit < ((signed int)price * einheiten)) {
286 if (format_euro(rest, 10, credit->credit) == NULL) {
287 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
290 cprintf("\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n", nickname, rest);
293 deposit_credit(nickname);
298 /* substract money */
299 credit->credit -= (price * einheiten);
301 if (format_euro(rest, 10, credit->credit) == NULL) {
302 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
306 textcolor(TC_LIGHT_GREEN);
307 cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n",
309 textcolor(TC_LIGHT_GRAY);
311 cprintf("\r\nDein Guthaben betraegt noch %s.\r\n", rest);
316 textcolor(TC_LIGHT_RED);
317 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, druecke RETURN...\r\n");
318 textcolor(TC_LIGHT_GRAY);
323 /* Ensure that nickname is NULL if it's empty because it's used in print_log */
327 money += price * einheiten;
328 items_sold += einheiten;
330 print_log(name, price, einheiten, nickname, rest);
335 void buy_stock(BYTE n) {
336 if (n >= status.num_items || status.status[n].item_name == NULL) {
337 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
342 status.status[n].times_sold += buy(status.status[n].item_name, status.status[n].price);
345 void buy_custom(void) {
348 char entered[5] = {'1', 0, 0, 0, 0};
349 char *input, name[20];
353 memset(name, '\0', 20);
354 cprintf("\r\nWas soll gekauft werden?\r\n");
356 strncpy(name, input, 20);
360 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
367 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
370 } else if (c == '-' && i == 0)
372 else if (c > 47 && c < 58)
375 price = atoi(entered) * negative;
382 void set_time_interactive(void) {
383 BYTE part[3] = {'0', '0', '\0'};
385 char *time_input, *time;
386 cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
387 time_input = get_input();
388 part[0] = time_input[0];
389 part[1] = time_input[1];
391 part[0] = time_input[2];
392 part[1] = time_input[3];
394 part[0] = time_input[4];
395 part[1] = time_input[5];
397 set_time(tp1, tp2, tp3);
400 cprintf("\r\nZeit gesetzt: %s\r\n", time);
411 /* Allocate logging buffer memory */
414 /* Set time initially, c128 doesn't know it */
415 set_time_interactive();
419 /* Load configuration */
422 /* Load items (= drinks) */
428 sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
430 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
433 sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17, time, log_num, log_heap_offset);
441 /* ...display dialogs eventually */
442 if (*c > 47 && *c < 58) {
443 /* if the input starts with a digit, we will interpret it as a number
444 * for the item to be sold */
449 } else if (*c == 'f') {
454 } else if (*c == 's') {
458 cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n");
460 } else if (*c == 'g') {
462 } else if (*c == 'z') {
463 set_time_interactive();
464 } else if (*c == 'q')
468 cprintf("\r\nBYEBYE\r\n");