lister ongoing other small bug fixed, still need to migrate
This commit is contained in:
parent
de75f1bd4f
commit
d61a2abc6a
128
admin_user.h
128
admin_user.h
@ -127,6 +127,13 @@ void inv_control(){
|
|||||||
db.db.row_count += 1;
|
db.db.row_count += 1;
|
||||||
strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM");
|
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
|
db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
|
||||||
|
// char * SortItems[] = {
|
||||||
|
// "Name",
|
||||||
|
// "Price",
|
||||||
|
// "Brand",
|
||||||
|
// "Category",
|
||||||
|
// };
|
||||||
|
// lister(&db,NULL,db.db.row_count,"Inventory",SortItems,4,print_page,sortItems,item_control);
|
||||||
list_page(db,NULL,db.db.row_count,item_control);
|
list_page(db,NULL,db.db.row_count,item_control);
|
||||||
break;
|
break;
|
||||||
case 2://add item
|
case 2://add item
|
||||||
@ -142,16 +149,15 @@ void inv_control(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
char * show_item_admin(struct inventory db,int index){
|
char * show_item_admin(struct inventory db,int index){
|
||||||
char * output = malloc(sizeof(char) * 1024);
|
char * output = (char*)malloc(sizeof(char) * 2048);
|
||||||
sprintf(output,"Product: %s\n", db.row[index].product);
|
sprintf(output,"Product: %s\n", db.row[index].product);
|
||||||
sprintf(output,"Catergory: %s\n", db.row[index].category);
|
sprintf(output+strlen(output),"Catergory: %s\n", db.row[index].category);// strlen to add offset
|
||||||
sprintf(output,"Brand: %s\n", db.row[index].brand);
|
sprintf(output+strlen(output),"Brand: %s\n", db.row[index].brand);
|
||||||
sprintf(output,"Price: $%lf\n", db.row[index].price);
|
sprintf(output+strlen(output),"Price: $%lf\n", db.row[index].price);
|
||||||
sprintf(output,"Stock: %d\n", db.row[index].stock);
|
sprintf(output+strlen(output),"Stock: %d\n", db.row[index].stock);
|
||||||
sprintf(output,"Barcode: %ld\n",db.row[index].barcode);
|
sprintf(output+strlen(output),"Barcode: %ld\n",db.row[index].barcode);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct inventory item_control(struct inventory db,int index){
|
struct inventory item_control(struct inventory db,int index){
|
||||||
int choice = 0;
|
int choice = 0;
|
||||||
if(db.row[index].barcode == -1024){//new item
|
if(db.row[index].barcode == -1024){//new item
|
||||||
@ -164,16 +170,16 @@ struct inventory item_control(struct inventory db,int index){
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
char * item = show_item_admin(db,index);
|
char * item = show_item_admin(db,index);
|
||||||
char welcome[1035];
|
char welcome[4096];
|
||||||
strcpy(welcome,item);
|
strcpy(welcome,item);
|
||||||
strcat(welcome,"Operations");
|
strcat(welcome,"Operations\n");
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
char * items[] = {
|
char * items[] = {
|
||||||
"update item",
|
"update item",
|
||||||
"remove item"
|
"remove item"
|
||||||
};
|
};
|
||||||
choice = choices_selecter(item,2,welcome);
|
choice = choices_selecter(items,2,welcome);
|
||||||
switch (choice)
|
switch (choice)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
@ -195,16 +201,16 @@ struct inventory item_control(struct inventory db,int index){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void add_item(){
|
void add_item(){
|
||||||
system("cls");
|
system("cls");
|
||||||
printf("Add new item\n");
|
printf("Add new item\n");
|
||||||
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
|
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
|
||||||
if(!item_inputer("name",INPUT_TYPE::STRING,item->product) ||
|
if(!item_inputer("name",STRING,&item->product,false) ||
|
||||||
!item_inputer("brand",INPUT_TYPE::STRING,item->brand) ||
|
!item_inputer("brand",STRING,&item->brand,false) ||
|
||||||
!item_inputer("category",INPUT_TYPE::STRING,item->category) ||
|
!item_inputer("category",STRING,&item->category,false) ||
|
||||||
!item_inputer("stock",INPUT_TYPE::FLOAT,item->price) ||
|
!item_inputer("price",DOUBLE,&item->price,false) ||
|
||||||
!item_inputer("barcode",INPUT_TYPE::LONG,item->barcode)
|
!item_inputer("stock",INT,&item->stock,false) ||
|
||||||
|
!item_inputer("barcode",LONG,&item->barcode,false)
|
||||||
){
|
){
|
||||||
free(item);
|
free(item);
|
||||||
return;
|
return;
|
||||||
@ -228,11 +234,11 @@ void update_item(struct inventory db,int index){
|
|||||||
printf("Price: $%lf\n", db.row[index].price);
|
printf("Price: $%lf\n", db.row[index].price);
|
||||||
printf("Stock: %d\n", db.row[index].stock);
|
printf("Stock: %d\n", db.row[index].stock);
|
||||||
printf("Barcode: %ld\n",db.row[index].barcode);
|
printf("Barcode: %ld\n",db.row[index].barcode);
|
||||||
item_inputer("name",INPUT_TYPE::STRING,db.row[index].product,true);
|
item_inputer("name",STRING,&db.row[index].product,true);
|
||||||
item_inputer("brand",INPUT_TYPE::STRING,db.row[index].brand,true);
|
item_inputer("brand",STRING,&db.row[index].brand,true);
|
||||||
item_inputer("category",INPUT_TYPE::STRING,db.row[index].category,true);
|
item_inputer("category",STRING,&db.row[index].category,true);
|
||||||
item_inputer("stock",INPUT_TYPE::FLOAT,db.row[index].price,true);
|
item_inputer("stock",DOUBLE,&db.row[index].price,true);
|
||||||
item_inputer("barcode",INPUT_TYPE::LONG,db.row[index].barcode,true);
|
item_inputer("barcode",LONG,&db.row[index].barcode,true);
|
||||||
if(!update_db_invt(db)){
|
if(!update_db_invt(db)){
|
||||||
printf("Failed to update item\n");
|
printf("Failed to update item\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -458,66 +464,26 @@ void add_tran(){
|
|||||||
row->date = get_date();
|
row->date = get_date();
|
||||||
row->time = get_time();
|
row->time = get_time();
|
||||||
}else{
|
}else{
|
||||||
temp = prompt_item("Please input date:day \n>");
|
if( !item_inputer("date:day",INT,&row->date.day,false) ||
|
||||||
if(temp == NULL){
|
!item_inputer("date:month",INT,&row->date.month,false) ||
|
||||||
free(row);
|
!item_inputer("date:year",INT,&row->date.year,false) ||
|
||||||
return;
|
!item_inputer("time:hour",INT,&row->time.hour,false) ||
|
||||||
}
|
!item_inputer("time:minute",INT,&row->time.minute,false) ||
|
||||||
row->date.day = atoi(temp);
|
!item_inputer("time:second",INT,&row->time.second,false)
|
||||||
temp = prompt_item("Please input date:month \n>");
|
){
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->date.month = atoi(temp);
|
|
||||||
temp = prompt_item("Please input date:year \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->date.year = atoi(temp);
|
|
||||||
temp = prompt_item("Please input time:hour \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->time.hour = atoi(temp);
|
|
||||||
temp = prompt_item("Please input time:minute \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->time.minute = atoi(temp);
|
|
||||||
temp = prompt_item("Please input time:second \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
free(row);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
temp = prompt_item("Please input price \n>");
|
if (
|
||||||
if(temp == NULL){
|
!item_inputer("price",DOUBLE,&row->price,false) ||
|
||||||
|
!item_inputer("quantity",INT,&row->quantity,false) ||
|
||||||
|
!item_inputer("ID",LONG,&row->id,false) ||
|
||||||
|
!item_inputer("barcode",LONG,&row->barcode,false)
|
||||||
|
){
|
||||||
free(row);
|
free(row);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
row->price = atof(temp);
|
|
||||||
temp = prompt_item("Please input quantity \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->quantity = atoi(temp);
|
|
||||||
temp = prompt_item("Please input ID \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->id = atol(temp);
|
|
||||||
temp = prompt_item("Please input barcode \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(row);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
row->barcode = atol(temp);
|
|
||||||
if(row == NULL || !append_transaction_db(row)){
|
if(row == NULL || !append_transaction_db(row)){
|
||||||
printf("Failed to add item\n");
|
printf("Failed to add item\n");
|
||||||
}else{
|
}else{
|
||||||
@ -713,12 +679,14 @@ struct user add_user(struct user db){
|
|||||||
char* temp;
|
char* temp;
|
||||||
int index = db.db.row_count;
|
int index = db.db.row_count;
|
||||||
struct user_row *temprow = realloc(db.row, sizeof(struct user_row));
|
struct user_row *temprow = realloc(db.row, sizeof(struct user_row));
|
||||||
temp = prompt_item("Please input the username\n>");
|
if (
|
||||||
strcpy(temprow->name,temp);
|
!item_inputer("name",STRING,&temprow->name,false) ||
|
||||||
temp = prompt_item("Please input the id\n>");
|
!item_inputer("id",LONG,&temprow->id,false) ||
|
||||||
temprow->id = atol(temp);
|
!item_inputer("role",STRING,&temprow->role,false)
|
||||||
temp = prompt_item("Please input the role\n>");
|
){
|
||||||
strcpy(temprow->role,temp);
|
free(temprow);
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
if(!append_user(temprow)){
|
if(!append_user(temprow)){
|
||||||
printf("Failed to add user\n");
|
printf("Failed to add user\n");
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#name of role that are consider as admin
|
#name of role that are consider as admin
|
||||||
teacher
|
|
||||||
Staff
|
Staff
|
||||||
Admin
|
Admin
|
||||||
|
teacher
|
||||||
|
admin
|
||||||
|
@ -8,3 +8,6 @@ student
|
|||||||
Chan Siu Man
|
Chan Siu Man
|
||||||
student
|
student
|
||||||
20002
|
20002
|
||||||
|
test
|
||||||
|
admin
|
||||||
|
10001
|
||||||
|
@ -176,11 +176,21 @@ void list_items(){
|
|||||||
|
|
||||||
struct inventory db = read_db_invt();
|
struct inventory db = read_db_invt();
|
||||||
struct Map* map = NULL;
|
struct Map* map = NULL;
|
||||||
|
// char * SortItems[] = {
|
||||||
|
// "Name",
|
||||||
|
// "Price",
|
||||||
|
// "Brand",
|
||||||
|
// "Category",
|
||||||
|
// };
|
||||||
|
// FIXME:function for normal list page
|
||||||
|
// lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,item_control);
|
||||||
list_page(db,map,db.db.row_count,show_item);
|
list_page(db,map,db.db.row_count,show_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
//for sorting
|
//for sorting
|
||||||
struct Map* sortItems(struct inventory db, int sort){
|
struct Map* sortItems(struct inventory db, int sort){
|
||||||
|
// struct Map* sortItems(void * ddb, int sort){
|
||||||
|
// struct inventory db = *((struct inventory*)ddb);
|
||||||
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
|
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
|
||||||
for (int i = 0; i < db.db.row_count-1; i++){
|
for (int i = 0; i < db.db.row_count-1; i++){
|
||||||
map[i].key = i;
|
map[i].key = i;
|
||||||
@ -259,6 +269,14 @@ void search_item(){
|
|||||||
map = searchItems(db,searchstr);
|
map = searchItems(db,searchstr);
|
||||||
if(map[0].value > 0){
|
if(map[0].value > 0){
|
||||||
int temp_row = *((int*)map[0].value);
|
int temp_row = *((int*)map[0].value);
|
||||||
|
// char * SortItems[] = {
|
||||||
|
// "Name",
|
||||||
|
// "Price",
|
||||||
|
// "Brand",
|
||||||
|
// "Category",
|
||||||
|
// };
|
||||||
|
//FIXME
|
||||||
|
// lister(&db,map+1,temp_row,"List of items",SortItems,4,print_page,sortItems,item_control);//ofset map, as it is use to store the size
|
||||||
list_page(db,map+1,temp_row,show_item);//ofset map, as it is use to store the size
|
list_page(db,map+1,temp_row,show_item);//ofset map, as it is use to store the size
|
||||||
}else{//empty search
|
}else{//empty search
|
||||||
printf("No result found\n");
|
printf("No result found\n");
|
||||||
|
17
utils.h
17
utils.h
@ -40,7 +40,7 @@ int choices_selecter(char * Items[],int items,char * welcome_messages){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* prompt_item(char* prompt,bool empty_allowed=false){
|
char* prompt_item(char* prompt,bool empty_allowed){
|
||||||
char* item = malloc(sizeof(char) * 100);
|
char* item = malloc(sizeof(char) * 100);
|
||||||
do{
|
do{
|
||||||
printf("%s",prompt);
|
printf("%s",prompt);
|
||||||
@ -59,9 +59,9 @@ char* prompt_item(char* prompt,bool empty_allowed=false){
|
|||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
typedef enum{
|
typedef enum{
|
||||||
INT=1,STRING=2,LONG=3,FLOAT=4
|
INT=1,STRING=2,LONG=3,DOUBLE=4
|
||||||
}INPUT_TYPE;
|
}INPUT_TYPE;
|
||||||
bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=false){
|
bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed){
|
||||||
char output[1024] = "Please input the ";
|
char output[1024] = "Please input the ";
|
||||||
strcat(output,varname);
|
strcat(output,varname);
|
||||||
strcat(output,": \n>");
|
strcat(output,": \n>");
|
||||||
@ -71,16 +71,16 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=
|
|||||||
}
|
}
|
||||||
switch(type){
|
switch(type){
|
||||||
case INT:
|
case INT:
|
||||||
item = atoi(temp);
|
*((int*)item) = atoi(temp);
|
||||||
break;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
strcpy(item,temp);
|
strcpy(item,temp);
|
||||||
break;
|
break;
|
||||||
case LONG:
|
case LONG:
|
||||||
item = atol(temp);
|
*((long*)item) = atol(temp);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case DOUBLE:
|
||||||
item = atof(temp);
|
*((double*)item) = atof(temp);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@ -88,7 +88,8 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct * Map) , struct * Map (*sorter)(void *,int),void * (*showitem)(void *,int)){
|
//TODO: lister still useable
|
||||||
|
void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct Map*) , struct Map* (*sorter)(void *,int),void * (*showitem)(void *,int)){
|
||||||
int choice = -1;
|
int choice = -1;
|
||||||
int page = 0;
|
int page = 0;
|
||||||
int page_size = PAGE_SIZE;
|
int page_size = PAGE_SIZE;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user