-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemployee.c
More file actions
509 lines (408 loc) · 14.4 KB
/
employee.c
File metadata and controls
509 lines (408 loc) · 14.4 KB
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
#include "employee.h"
#include "common.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
static void add_customer(int sock) {
char buf[BUFFER_SIZE];
char path[100];
snprintf(path, 100, "%s/customers.csv", DATA_DIR);
int new_id = get_next_id_csv(path);
send_message(sock, "Enter username: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
char username[50];
strcpy(username, buf);
send_message(sock, "Enter password: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
char password[50];
strcpy(password, buf);
send_message(sock, "Initial balance: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
double balance = atof(buf);
FILE *fp = fopen(path, "a");
if (!fp) {
send_message(sock, "Error\n");
return;
}
fprintf(fp, "%d,%s,%s,%.2f,1\n", new_id, username, password, balance);
fclose(fp);
snprintf(buf, BUFFER_SIZE, "Customer created! ID: %d\n", new_id);
send_message(sock, buf);
}
static void modify_customer(int sock) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter customer ID: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int cid = atoi(buf);
char path[100], temp_path[100];
snprintf(path, 100, "%s/customers.csv", DATA_DIR);
snprintf(temp_path, 100, "%s/customers.tmp", DATA_DIR);
int fd = open(path, O_RDWR);
if (fd == -1) {
send_message(sock, "Error opening file\n");
return;
}
if (lock_file(fd, F_WRLCK) == -1) {
close(fd);
send_message(sock, "Error acquiring lock\n");
return;
}
FILE *fp = fdopen(dup(fd), "r");
FILE *tmp = fopen(temp_path, "w");
if (!fp || !tmp) {
if (fp) fclose(fp);
if (tmp) fclose(tmp);
unlock_file(fd);
close(fd);
send_message(sock, "Error\n");
return;
}
char line[256];
int found = 0;
fgets(line, sizeof(line), fp);
fprintf(tmp, "id,username,password,balance,active\n");
while (fgets(line, sizeof(line), fp)) {
int id, active;
char user[50], pass[50];
double balance;
trim_string(line);
if (sscanf(line, "%d,%49[^,],%49[^,],%lf,%d", &id, user, pass, &balance, &active) == 5) {
trim_string(user);
trim_string(pass);
if (id == cid) {
found = 1;
send_message(sock, "New username (- to skip): ");
if (read_input(sock, buf, BUFFER_SIZE) && buf[0] != '-') {
strcpy(user, buf);
}
send_message(sock, "New password (- to skip): ");
if (read_input(sock, buf, BUFFER_SIZE) && buf[0] != '-') {
strcpy(pass, buf);
}
}
fprintf(tmp, "%d,%s,%s,%.2f,%d\n", id, user, pass, balance, active);
}
}
fclose(fp);
fclose(tmp);
rename(temp_path, path);
unlock_file(fd);
close(fd);
send_message(sock, found ? "Customer updated\n" : "Customer not found\n");
}
static void view_assigned_loans(int sock, int eid) {
char path[100];
snprintf(path, 100, "%s/loans.csv", DATA_DIR);
FILE *fp = fopen(path, "r");
if (!fp) {
send_message(sock, "No loans\n");
return;
}
char line[512], buffer[200];
send_message(sock, "\n=== Assigned Loans ===\n");
fgets(line, sizeof(line), fp);
int count = 0;
while (fgets(line, sizeof(line), fp)) {
int loan_id, customer_id, tenure;
char loan_type[100], status[50], assigned_employee[50];
double amount;
trim_string(line);
if (sscanf(line, "%d,%d,%lf,%99[^,],%d,%49[^,],%49[^\n]",
&loan_id, &customer_id, &amount, loan_type, &tenure, status, assigned_employee) == 7) {
trim_string(status);
trim_string(assigned_employee);
if (atoi(assigned_employee) == eid) {
snprintf(buffer, sizeof(buffer),
"Loan ID: %d | Customer: %d | Amount: %.2f | Status: %s\n",
loan_id, customer_id, amount, status);
send_message(sock, buffer);
count++;
}
} else {
int loan_id2, customer_id2, assigned_emp2;
char status2[50];
double amount2;
if (sscanf(line, "%d,%d,%lf,%49[^,],%d",
&loan_id2, &customer_id2, &amount2, status2, &assigned_emp2) == 5) {
if (assigned_emp2 == eid) {
snprintf(buffer, sizeof(buffer),
"Loan ID: %d | Customer: %d | Amount: %.2f | Status: %s\n",
loan_id2, customer_id2, amount2, status2);
send_message(sock, buffer);
count++;
}
}
}
}
if (count == 0) {
send_message(sock, "No loans assigned to you\n");
}
fclose(fp);
}
static void process_loan(int sock, int eid) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter loan ID: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int lid = atoi(buf);
send_message(sock, "Approve (1) or Reject (0): ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int approve = atoi(buf);
char path[100], temp_path[100];
snprintf(path, 100, "%s/loans.csv", DATA_DIR);
snprintf(temp_path, 100, "%s/loans.tmp", DATA_DIR);
int fd = open(path, O_RDWR);
if (fd == -1) {
send_message(sock, "Error opening file\n");
return;
}
if (lock_file(fd, F_WRLCK) == -1) {
close(fd);
send_message(sock, "Error acquiring lock\n");
return;
}
FILE *fp = fdopen(dup(fd), "r");
FILE *tmp = fopen(temp_path, "w");
if (!fp || !tmp) {
if (fp) fclose(fp);
if (tmp) fclose(tmp);
unlock_file(fd);
close(fd);
send_message(sock, "Error\n");
return;
}
char line[256];
int found = 0;
int loan_customer_id = -1;
double loan_amount = 0.0;
fgets(line, sizeof(line), fp);
fprintf(tmp, "loan_id,customer_id,amount,loan_type,tenure,status,assigned_employee\n");
while (fgets(line, sizeof(line), fp)) {
int loan_id, customer_id, tenure;
char loan_type[50], status[20], assigned_employee[50];
double amount;
trim_string(line);
if (sscanf(line, "%d,%d,%lf,%49[^,],%d,%19[^,],%49[^\n]",
&loan_id, &customer_id, &amount, loan_type, &tenure, status, assigned_employee) == 7) {
trim_string(loan_type);
trim_string(status);
trim_string(assigned_employee);
if (loan_id == lid) {
// Check if assigned to this employee
char emp_id_str[20];
snprintf(emp_id_str, 20, "%d", eid);
if (strcmp(assigned_employee, emp_id_str) == 0) {
strcpy(status, approve ? "approved" : "rejected");
found = 1;
// Store customer ID and amount if approved
if (approve) {
loan_customer_id = customer_id;
loan_amount = amount;
}
}
}
fprintf(tmp, "%d,%d,%.2f,%s,%d,%s,%s\n",
loan_id, customer_id, amount, loan_type, tenure, status, assigned_employee);
}
}
fclose(fp);
fclose(tmp);
rename(temp_path, path);
unlock_file(fd);
close(fd);
if (!found) {
send_message(sock, "Loan not found or not assigned to you\n");
return;
}
// If loan approved, credit amount to customer's account
if (approve && loan_customer_id != -1) {
char cust_path[100], cust_temp[100];
snprintf(cust_path, 100, "%s/customers.csv", DATA_DIR);
snprintf(cust_temp, 100, "%s/customers.tmp", DATA_DIR);
int cust_fd = open(cust_path, O_RDWR);
if (cust_fd == -1) {
send_message(sock, "Loan processed but couldn't credit amount\n");
return;
}
if (lock_file(cust_fd, F_WRLCK) == -1) {
close(cust_fd);
send_message(sock, "Loan processed but couldn't credit amount\n");
return;
}
FILE *cust_fp = fdopen(dup(cust_fd), "r");
FILE *cust_tmp = fopen(cust_temp, "w");
if (!cust_fp || !cust_tmp) {
if (cust_fp) fclose(cust_fp);
if (cust_tmp) fclose(cust_tmp);
unlock_file(cust_fd);
close(cust_fd);
send_message(sock, "Loan processed but couldn't credit amount\n");
return;
}
char cust_line[256];
double new_balance = 0;
fgets(cust_line, sizeof(cust_line), cust_fp);
fprintf(cust_tmp, "id,username,password,balance,active\n");
while (fgets(cust_line, sizeof(cust_line), cust_fp)) {
int id, active;
char user[50], pass[50];
double balance;
trim_string(cust_line);
if (sscanf(cust_line, "%d,%49[^,],%49[^,],%lf,%d",
&id, user, pass, &balance, &active) == 5) {
trim_string(user);
trim_string(pass);
// Credit loan amount to customer's account
if (id == loan_customer_id) {
balance += loan_amount;
new_balance = balance;
}
fprintf(cust_tmp, "%d,%s,%s,%.2f,%d\n", id, user, pass, balance, active);
}
}
fclose(cust_fp);
fclose(cust_tmp);
rename(cust_temp, cust_path);
unlock_file(cust_fd);
close(cust_fd);
// Add transaction record
FILE *trans_fp = fopen(DATA_DIR "/transactions.csv", "a");
if (trans_fp) {
time_t now = time(NULL);
char timestamp[50];
strftime(timestamp, 50, "%Y-%m-%d %H:%M:%S", localtime(&now));
fprintf(trans_fp, "%d,LOAN_CREDIT,%.2f,%.2f,%s\n",
loan_customer_id, loan_amount, new_balance, timestamp);
fclose(trans_fp);
}
snprintf(buf, BUFFER_SIZE,
"Loan approved! Amount %.2f credited to customer's account\n",
loan_amount);
send_message(sock, buf);
} else if (found) {
send_message(sock, "Loan rejected\n");
}
}
static void view_customer_transactions(int sock) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter customer ID: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int cid = atoi(buf);
FILE *fp = fopen(DATA_DIR "/transactions.csv", "r");
if (!fp) {
send_message(sock, "No transactions\n");
return;
}
char line[256], buffer[200];
send_message(sock, "\n=== Customer Transaction History (Passbook) ===\n");
fgets(line, sizeof(line), fp);
int count = 0;
while (fgets(line, sizeof(line), fp)) {
int customer_id;
char type[20], timestamp[50];
double amount, balance_after;
trim_string(line);
if (sscanf(line, "%d,%19[^,],%lf,%lf,%49[^\n]",
&customer_id, type, &amount, &balance_after, timestamp) == 5) {
if (customer_id == cid) {
snprintf(buffer, 200, "%s: %.2f | Balance: %.2f | %s\n",
type, amount, balance_after, timestamp);
send_message(sock, buffer);
count++;
}
}
}
if (count == 0) {
send_message(sock, "No transactions found for this customer\n");
}
fclose(fp);
}
static void change_password(int sock, int eid) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter current password: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
char old_pass[50];
strcpy(old_pass, buf);
send_message(sock, "Enter new password: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
char new_pass[50];
strcpy(new_pass, buf);
char path[100];
snprintf(path, 100, "%s/employees.csv", DATA_DIR);
int fd = open(path, O_RDWR);
if (fd == -1) {
send_message(sock, "Error opening file\n");
return;
}
if (lock_file(fd, F_WRLCK) == -1) {
close(fd);
send_message(sock, "Error acquiring lock\n");
return;
}
FILE *fp = fdopen(dup(fd), "r");
FILE *tmp = fopen(DATA_DIR "/employees.tmp", "w");
if (!fp || !tmp) {
if (fp) fclose(fp);
if (tmp) fclose(tmp);
unlock_file(fd);
close(fd);
send_message(sock, "Error\n");
return;
}
char line[256];
int found = 0;
fgets(line, sizeof(line), fp);
fprintf(tmp, "id,username,password\n");
while (fgets(line, sizeof(line), fp)) {
int id;
char user[50], pass[50];
trim_string(line);
if (sscanf(line, "%d,%49[^,],%49[^\n]", &id, user, pass) == 3) {
trim_string(user);
trim_string(pass);
if (id == eid) {
if (strcmp(pass, old_pass) == 0) {
strcpy(pass, new_pass);
found = 1;
}
}
fprintf(tmp, "%d,%s,%s\n", id, user, pass);
}
}
fclose(fp);
fclose(tmp);
rename(DATA_DIR "/employees.tmp", path);
unlock_file(fd);
close(fd);
send_message(sock, found ? "Password changed successfully\n" : "Incorrect password\n");
}
void handle_employee(int sock, int eid) {
char buffer[BUFFER_SIZE];
while (1) {
send_message(sock,
"\n=== Employee Menu ===\n"
"1. Add New Customer\n"
"2. Modify Customer Details\n"
"3. Process Loan Applications\n"
"4. View Assigned Loan Applications\n"
"5. View Customer Transactions (Passbook)\n"
"6. Change Password\n"
"7. Logout\n"
"Choice: ");
if (!read_input(sock, buffer, BUFFER_SIZE)) break;
int choice = atoi(buffer);
switch (choice) {
case 1: add_customer(sock); break;
case 2: modify_customer(sock); break;
case 3: process_loan(sock, eid); break;
case 4: view_assigned_loans(sock, eid); break;
case 5: view_customer_transactions(sock); break;
case 6: change_password(sock, eid); break;
case 7: return;
default: send_message(sock, "Invalid choice\n");
}
}
}