-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_p.c
126 lines (105 loc) · 3.98 KB
/
order_p.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
#include <stdio.h>
#include <mysql/mysql.h>
#include "order_p.h"
static void refresh() {
system("clear");
}
void addPurchaseOrder(MYSQL *conn) {
int item_id, quantity;
printf("Enter Item ID for purchase: ");
scanf("%d", &item_id);
printf("Enter Quantity to purchase: ");
scanf("%d", &quantity);
char query[256];
snprintf(query, sizeof(query), "INSERT INTO Purchase_Manufacture (item_id, quantity, status, order_date) VALUES (%d, %d, 'pending',NOW())", item_id, quantity);
if (mysql_query(conn, query)) {
fprintf(stderr, "Failed to add purchase order: %s\n", mysql_error(conn));
} else {
printf("Purchase order added successfully.\n");
}
}
void viewPurchaseOrders(MYSQL *conn) {
MYSQL_RES *res;
MYSQL_ROW row;
if (mysql_query(conn, "SELECT * FROM Purchase_Manufacture")) {
fprintf(stderr, "Failed to fetch purchase orders: %s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Purchase ID | Item ID | Quantity | Status | Order Date | Completed Order Date\n");
printf("------------------------------------------------------------------------------------\n");
while ((row = mysql_fetch_row(res))) {
printf("%-11s | %-7s | %-8s | %-9s | %-19s | %-15s\n",
row[0], row[1], row[2], row[3], row[4] ? row[4] : "NULL", row[5] ? row[5] : "NULL");
}
mysql_free_result(res);
}
void completePurchaseOrder(MYSQL *conn) {
int purchase_id, item_id, quantity;
printf("Enter Purchase Order ID to complete: ");
scanf("%d", &purchase_id);
char selectQuery[256];
snprintf(selectQuery, sizeof(selectQuery),
"SELECT item_id, quantity FROM Purchase_Manufacture WHERE purchase_id=%d AND status='pending'",
purchase_id);
if (mysql_query(conn, selectQuery)) {
fprintf(stderr, "Failed to fetch purchase order details: %s\n", mysql_error(conn));
return;
}
MYSQL_RES *res = mysql_store_result(conn);
if (res == NULL || mysql_num_rows(res) == 0) {
printf("No pending purchase order found with ID: %d\n", purchase_id);
mysql_free_result(res);
return;
}
MYSQL_ROW row = mysql_fetch_row(res);
item_id = atoi(row[0]);
quantity = atoi(row[1]);
mysql_free_result(res);
char query[256];
snprintf(query, sizeof(query), "UPDATE Purchase_Manufacture SET status='completed' , completed_order = NOW() WHERE purchase_id=%d", purchase_id);
if (mysql_query(conn, query)) {
fprintf(stderr, "Failed to complete purchase order: %s\n", mysql_error(conn));
return;
}
char insertQuery[256];
snprintf(insertQuery, sizeof(insertQuery),
"INSERT INTO Transaction (item_id, type, quantity, tran_date) VALUES (%d, 'purchase', %d, NOW())",
item_id, quantity);
if (mysql_query(conn, insertQuery)) {
fprintf(stderr, "Failed to insert transaction record: %s\n", mysql_error(conn));
} else {
printf("Purchase order completed and transaction record added successfully.\n");
}
}
void manageOrders_p(MYSQL *conn) {
int choice;
do {
printf("\n\033[1;34m--- Purchase Orders Management ---\033[0m\n");
printf("1. Add Purchase Order\n");
printf("2. View Purchase Orders\n");
printf("3. Complete Purchase Order\n");
printf("\033[31m4. Return to Main Menu\033[0m\n");
printf("\033[1;32mEnter your choice: \033[0m ");
scanf("%d", &choice);
switch (choice) {
case 1:
refresh();
addPurchaseOrder(conn);
break;
case 2:
refresh();
viewPurchaseOrders(conn);
break;
case 3:
refresh();
completePurchaseOrder(conn);
break;
case 4:
printf("\nReturning to Main Menu.\n");
return;
default:
printf("Invalid choice.\n");
}
} while(1);
}