sba/admin_user.h
2022-09-06 23:38:59 +08:00

376 lines
11 KiB
C

//include std lib if they havent been included
#ifndef STD_LIB_H
#define STD_LIB_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#endif
#ifndef MENU_H
#define MENU_H
#include "menu.h"
#endif //MENU_H
//list of functions
void inv_control();
void tran_control();
void user_control();
void admin_control();
struct user_row *prompt_user();
int admin_menu_user_choices(char* name,char* role);
void admin_menu(){
system("cls");
welcome_message();
//the selection menu
// if(login valid)
struct user_row *user = prompt_user();
if(user == NULL){//invalid user
return;
}
int choice = 0;
do
{
choice = admin_menu_user_choices(user->name,user->role);
switch (choice)
{
case 1://action with inventory
inv_control();
break;
case 2://action with transction
// tran_control();
break;
case 3://action with user
// user_control();
break;
case 4://action with admin user register
// admin_control();
break;
case 5://Exit
break;
default://invalid input ,should not happen,as it is already catch in above function
printf("Invalid choice\n");
break;
}
}while(choice != 5);
return;
}
//func for main for admin
struct user_row *prompt_user(){
long user_id = 0;
printf("Please tap your card(student card, staff card,etc)(or input the id)(0 to cancel)\n>");
scanf("%ld", &user_id);
if(user_id == 0){
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return NULL;
}
struct user_row *user = get_user(user_id);
if(user == NULL){
printf("User not found\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return NULL;
}else if(!is_admin(user->role)){
printf("You aren't an admin\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return NULL;
}else{
printf("Welcome %s(%s)\n", user->name,user->role);
printf("press any key to continue\n");
fflush(stdin);
getchar();
}
return user;
}
int admin_menu_user_choices(char* name,char* role){
int choice = 0;
do{
system("cls");
printf("Welcome %s(%s)\n", name,role);
printf("Please select an option:\n");
printf("1. Inventory control\n");
printf("2. Transaction control\n");
printf("3. User control\n");
printf("4. Admin user register\n");
printf("5. Exit and Logout\n");
printf(">");
scanf("%d", &choice);
if(choice < 1 || choice > 5){
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
getchar();
}
}while(choice < 1 || choice > 5);
return choice;
}
//invetory control
void add_item();
void remove_item(struct inventory db,int index);
void update_item(struct inventory db,int index);
void admin_list_pages(struct inventory db);
struct inventory item_control(struct inventory db,int index);
int prompt_inv_control(){
int choice = 0;
do{
system("cls");
printf("Please select an option:\n");
printf("1. List(allow all operation include add)\n");
printf("2. Add item(shortcut to add item)\n");
printf("3. Exit\n");
printf(">");
scanf("%d", &choice);
if(choice < 1 || choice > 3){
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
getchar();
}
}while(choice < 1 || choice > 3);
return choice;
}
void inv_control(){
int choice = 0;
do{
choice = prompt_inv_control();
switch (choice){
case 1://list
//add a new element with product name new item
//for item control
struct inventory db = read_db_invt();
db.row = (struct inventory_row *)realloc(db.row, sizeof(struct inventory_row) * (db.db.row_count + 1));
db.db.row_count += 1;
strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM");
db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
list_page(db,NULL,db.db.row_count,item_control);
break;
case 2://add item
add_item();
break;
case 3://exit
break;
default://should not happen
printf("Invalid choice\n");
break;
}
}while(choice != 3);
}
void show_item_admin(struct inventory db,int index){
system("cls");
printf("Product: %s\n", db.row[index].product);
printf("Catergory: %s\n", db.row[index].category);
printf("Brand: %s\n", db.row[index].brand);
printf("Price: $%lf\n", db.row[index].price);
printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode);
getchar();
return;
}
struct inventory item_control(struct inventory db,int index){
int choice = 0;
if(db.row[index].barcode == -1024){//new item
add_item();
struct inventory temp = read_db_invt();//reappend new item at back
temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1));
temp.db.row_count += 1;
strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM");
temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
return temp;
}
do
{
system("cls");
show_item_admin(db,index);
printf("Operations\n");
printf("0 exit\n");
printf("1 update item\n");
printf("2 remove item\n");
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice);
switch (choice)
{
case 1:
update_item(db,index);
break;
case 2:
remove_item(db,index);
break;
default:
break;
}
} while (choice != 0);
struct inventory temp = read_db_invt();
temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1));
temp.db.row_count += 1;
strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM");
temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
return temp;
}
char* prompt_item(char* prompt){
char* item = malloc(sizeof(char) * 100);
do{
printf("%s",prompt);
fflush(stdin);
scanf("%[^\n]",item);
if(strcmp(item,"") == 0){
printf("Invalid input, try again?(y/n)\n");
char temp[100];
fflush(stdin);
scanf("%[^\n]",temp);
if(strcmp(temp,"n") == 0){
return NULL;
}
}
}while(strcmp(item,"") == 0);
return item;
}
void add_item(){
char* temp;
system("cls");
printf("Add new item\n");
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
temp = prompt_item("Please input the name: \n>");
if(temp == NULL){
free(item);
return;
}
strcpy(item->product,temp);
temp = prompt_item("Please input the brand: \n>");
if(temp == NULL){
free(item);
return;
}
strcpy(item->brand,temp);
temp = prompt_item("Please input the category: \n>");
if(temp == NULL){
free(item);
return;
}
strcpy(item->category,temp);
temp = prompt_item("Please input the price: \n>");
if(temp == NULL){
free(item);
return;
}
item->price = atof(temp);
temp = prompt_item("Please input the stock: \n>");
if(temp == NULL){
free(item);
return;
}
item->stock = atoi(temp);
temp = prompt_item("Please input the barcode: \n>");
if(temp == NULL){
free(item);
return;
}
item->barcode = atol(temp);
if(item == NULL || !append_inventory(item)){
printf("Failed to add item\n");
}else{
printf("Item added\n");
}
printf("press any key to continue\n");
fflush(stdin);
getchar();
}
void update_item(struct inventory db,int index){
char temp[100];
printf("Update item(empty value to not change the value)\n");
printf("Please input the name(original:%s)\n>",db.row[index].product);
fflush(stdin);
scanf("%[^\n]", temp);//input until /n
if(strcmp(temp,"") != 0){//check if temp is not empty
strcpy(db.row[index].product,temp);//if yes, copy temp to product
}//else preserve the original value
printf("Please input the brand(orginal:%s)\n>",db.row[index].brand);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
strcpy(db.row[index].brand,temp);
}
printf("Please input the category(orginal:%s)\n>",db.row[index].category);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
strcpy(db.row[index].category,temp);
}
printf("Please input the price(orginal:$%.1lf)\n>",db.row[index].price);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].price = atof(temp);
}
printf("Please input the stock(orginal:%d)\n>",db.row[index].stock);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].stock = atoi(temp);
}
printf("Please input the barcode(original:%ld)\n>",db.row[index].barcode);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].barcode = atol(temp);
}
if(!update_db_invt(db)){
printf("Failed to update item\n");
}else{
printf("Item updated\n");
}
printf("press any key to continue\n");
fflush(stdin);
getchar();
}
void remove_item(struct inventory db,int index){
printf("Remove item\n");
printf("Are you sure you want to remove this item? (y/n)\n");
char choice;
fflush(stdin);
scanf("%c", &choice);
db.row[index].isdeleted = true;
if(choice == 'y'){
if(!update_db_invt(db)){
printf("Failed to remove item\n");
}else{
printf("Item removed\n");
}
}else{
printf("Item not removed\n");
}
printf("press any key to continue\n");
fflush(stdin);
getchar();
}