68bool logTransaction(
TransactionType type,
const char* name, uint32_t bloodId, uint32_t quantity,
const char* date,
const char* token) {
70 FILE* file = fopen(
"resources/db/transactions.log",
"a");
72 if (errno != ENOENT) {
73 printf(
"Error opening transaction log file: %s\n", strerror(errno));
78 if (containsPipe(name)) {
79 printf(
"Error: Entity name cannot contain a pipe character.\n");
83 if (type !=
BUY && type !=
SELL) {
84 printf(
"Error: Invalid transaction type.\n");
88 if (!isValidBloodGroup(bloodId)) {
89 printf(
"Error: Invalid blood group.\n");
93 if (strcmp(name,
"") == 0 || quantity <= 0) {
94 printf(
"Error: Invalid transaction parameters.\n");
98 if (!isValidDate(date)) {
99 printf(
"Error: Invalid date format.\n");
104 fprintf(file,
"%s|%s|%u|%u|%s|%s\n", (type ==
BUY ?
"Buy" :
"Sell"), name, bloodId, quantity, date, token);
106 fprintf(file,
"%s|%s|%u|%u|%s\n", (type ==
BUY ?
"Buy" :
"Sell"), name, bloodId, quantity, date);
141bool addTransaction(
TransactionType type,
const char* name, uint32_t bloodId, uint32_t quantity) {
142 if (strcmp(name,
"") == 0 || quantity <= 0) {
143 printf(
"Error: Invalid transaction parameters.\n");
147 if (containsPipe(name)) {
148 printf(
"Error: Entity name cannot contain a pipe character.\n");
152 if (type !=
BUY && type !=
SELL) {
153 printf(
"Error: Invalid transaction type.\n");
157 if (!isBloodAvailable(&bloodId, type)) {
158 printf(
"No stock available for blood group: %s\n", getBloodGroupById(bloodId));
163 if (!validateHospitalCode(name)) {
164 printf(
"Error: Invalid hospital code.\n");
173 printf(
"Enter the date and time of donation (YYYY-MM-DD): ");
174 fgets(date,
sizeof(date), stdin);
175 date[strcspn(date,
"\n")] = 0;
176 if (!isValidDate(date)) {
177 printf(
"Error: Invalid date format.\n");
183 while (stock != NULL) {
184 if (stock->
id == bloodId) {
186 printf(
"Not enough stock for blood group: %s. Available quantity: %u\n", getBloodGroupById(bloodId), stock->
quantity);
196 time_t now = time(NULL);
197 strftime(date,
sizeof(date),
"%Y-%m-%d", localtime(&now));
202 sprintf(token,
"TOKEN_%d", rand() % 10000);
203 printf(
"Sell token generated for %s: %s\n", name, token);
206 if (!logTransaction(type, name, bloodId, quantity, date, type ==
SELL ? token : NULL)) {
224 FILE* file = fopen(
"resources/db/transactions.log",
"r");
226 if (errno == ENOENT) {
227 printf(
"No registered transactions found.\n");
229 printf(
"Error opening transaction log file: %s\n", strerror(errno));
235 bool hasLogs =
false;
236 bool firstLog =
true;
237 char prevLine[256] = { 0 };
239 while (fgets(line,
sizeof(line), file) != NULL) {
242 uint32_t bloodId = 0;
243 uint32_t quantity = 0;
248 printf(
"\nRegistered Transactions:\n");
252 if (sscanf(line,
"%[^|]|%[^|]|%u|%u|%[^|]|%[^|\n]",
262 if (prevLine[0] !=
'\0') {
263 printf(
"\t----------------------------------\n");
266 printf(
"\tType: %s\n"
268 "\tBlood Group: %s\n"
273 getBloodGroupById(bloodId),
277 if (token[0] !=
'\0') {
278 printf(
"\n\tToken: %s\n", token);
281 strncpy(prevLine, line,
sizeof(prevLine) - 1);
282 prevLine[
sizeof(prevLine) - 1] =
'\0';
287 printf(
"No registered transactions found.\n");
Blood manager header file.
BloodStock * bloodHead
Globally exposed blood stock head pointer.
Hospital manager header file.
void displayTransactions(void)
Display all transactions.
Transaction manager header file.
#define MAX_TRANSACTION_TOKEN_LENGTH
Maximum transaction token length.
TransactionType
Transaction type enum.
#define MAX_TRANSACTION_DATE_LENGTH
Maximum transaction date length.