Computerized billing system
see youtube : https://youtu.be/f3Zn7DiCOIk
copy and paste on address bar
you need to install "mysql-connector-c++-1.1.4-linux-glibc2.5-x86-64bit.rpm" for connection of c++ with MySQL server. This rpm must be installed in MySQL client where you are building software in C++ IDE.
I presume that you have successfully configured DNS server in one VM of Centos 7, second VM of MySQL server as client of DNS server, third DNS client VM for MySQL client and Code Blocks IDE for C++ Command line interface ( CLI ). Now you have to do following things :-
you need to change in classConn.cpp:- server name, database name, user name, password. so that you can connect to mysql server. As :-
static char *opt_host = "serverora.db.net";// if you are in localhost change it to "localhost"
static char *opt_user_name = "rahul"; //username
static char *opt_password = "rahul"; // password
static unsigned int opt_port = 3306;
static char *opt_socket_name = NULL;
static char *opt_db_name = "cbs"; // database name
static unsigned int opt_flags = 0;
I presume that you have successfully installed MySQL server and client in VM : centos 6.10 as server and centos 7.5 as client. I am using code::blocks 20.03 IDE.Run project in full screen terminal.
Do following settings :-
Note : all libraries must be added serially as given.
/**********************************************
*
* file : main.cpp
*
***********************************************/
#include <iostream>
#include "classGetchoice.h"
#include "classConn.h"
#include "classDraw.h"
#include "classGetchar.h"
#include "classProduct.h"
#include "classCustomer.h"
#include "classBill.h"
using namespace std;
int main()
{
classGetchoice *get = new classGetchoice;
classDraw *draw = new classDraw;
classGetchar *getc = new classGetchar;
classConn *conn = new classConn;
classProduct *prd = new classProduct;
classCustomer *customer = new classCustomer;
classBill *bill = new classBill;
int choice = 0;
do
{
draw->clrscr();
draw->drawRect();
choice = get->getchoice(conn->menu);
switch(choice)
{
case 1:
prd->showAllProduct();
break;
case 2:
prd->displayProduct();
break;
case 3:
prd->productEnter();
break;
case 4:
prd->displayProduct("PRODUCT DISPLAY");
break;
case 5:
bill->billing();
break;
case 6:
bill->todaysSales();
break;
case 7:
prd->productModify();
break;
case 8:
customer->modifyCustomerRecord();
break;
case 9:
customer->showCustomer();
break;
case 10:
bill->modifyBill();
break;
case 11:
bill->viewBill();
break;
case 12:
bill->delEntry();
break;
case 13:
draw->clrscr();
exit(0);
}
}while(choice != 13);
return 0;
}
/**********************************************
*
* file : classBill.h
*
***********************************************/
#ifndef CLASSBILL_H
#define CLASSBILL_H
#include <mysql.h>
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <bits/stdc++.h>
#include "classConn.h"
#include "classDraw.h"
#include "classGetchar.h"
#include "classProduct.h"
#include "classSqlProcess.h"
#include "classValid.h"
#include "classGetchoice.h"
#include "classCustomer.h"
class classProduct;
struct date
{
int yy, mm, dd;
};
struct Vprd
{
Vprd();
~Vprd();
int pid;
std::string pname;
int prate;
int pquantity;
int ptotal;
};
class classBill
{
public:
classBill();
virtual ~classBill();
void billing();
std::vector<std::string> salesmenu;
void todaysSales();
static int statbillno;
void modifyBill();
void viewBill();
void delEntry();
protected:
private:
classDraw *draw;
classProduct *prd;
classSqlProcess *sqlp;
classGetchar *getc;
classCustomer *customer;
classGetchoice *get;
Vprd *vprd;
Vprd vp;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
winsize w;
bool flag;
std::string sql;
std::string productname;
std::string strdate;
std::string strmobileno;
std::string strcname;
std::string strcaddress;
std::string stramt;
std::string strquantity;
std::string strmaxid;
std::string strstock;
std::string strtotal;
std::string stramount;
std::string strprdid;
std::string strqty;
std::string strbillno;
std::string strcustomerid;
std::string strproductid;
std::string strproductname;
std::string strrate;
std::string strcmd;
std::string strid;
std::string strvpid;
std::string strchoice;
std::vector<std::string>cnamevec;
std::vector<std::string>::iterator cnvciter;
std::vector<std::string>vecBillmod;
std::vector<std::string>::iterator strbillmoditer;
std::vector<int> prdvec;
std::vector<int>::iterator prditer;
std::vector<Vprd> vprdvec;
std::vector<Vprd>::iterator vprditer;
std::string::iterator striter;
std::string stryesno;
char yesno;
char escch;
char ch;
int* totallen;
int qstate;
int productno;
int stock;
int rate;
int tlen1, tlen2;
int quantity;
int qty;
int total;
int billno;
int maxid;
int gamount;
int gamountfinal;
int i;
int lines;
int lines2;
int len;
int id;
int prdid;
float gtotal;
float netamount;
float discount;
unsigned long productid;
unsigned long customerid;
unsigned long mobileno;
unsigned long cmobileno;
unsigned long l;
};
#endif // CLASSBILL_H
/**********************************************
*
* file : classBill.cpp
*
***********************************************/
#include "classBill.h"
using std::cout;
using std::cin;
using std::string;
Vprd::Vprd()
{
pid = 0;
pname.clear();
prate = 0;
pquantity = 0;
ptotal = 0;
}
Vprd::~Vprd(){}
classBill::classBill()
{
//ctor
salesmenu.push_back("Product ID : ");
salesmenu.push_back("Product Name : ");
salesmenu.push_back("Stock : ");
salesmenu.push_back("Rate : ");
salesmenu.push_back("Quantity : ");
salesmenu.push_back("Total : ");
cnamevec.push_back("Customer Name : ");
cnamevec.push_back("Address : ");
cnamevec.push_back("Mobile No : ");
vecBillmod.push_back("Bill No : ");
vecBillmod.push_back("Product ID : ");
vecBillmod.push_back("Product Name : ");
vecBillmod.push_back("Rate : ");
vecBillmod.push_back("Quantity : ");
vecBillmod.push_back("Total : " );
vecBillmod.push_back("Customer Name : ");
vecBillmod.push_back("Customer ID : ");
vecBillmod.push_back("Date Of Sale : ");
gtotal = 0;
customerid = 0;
draw = new classDraw;
mysql = new MYSQL;
totallen = new int;
getc = new classGetchar;
get = new classGetchoice;
sqlp = new classSqlProcess;
res = new MYSQL_RES;
prd = new classProduct;
customer = new classCustomer;
vprd = new Vprd;
i = 0;
}
classBill::~classBill()
{
//dtor
delete draw;
delete mysql;
delete res;
delete totallen;
delete sqlp;
delete getc;
delete get;
delete prd;
}
void classBill::billing()
{
int const x = 31;
draw->clrscr();
draw->drawRect();
mysql = classConn::connection();
mysql->reconnect = true;
l = get->printMenu(cnamevec, "Customer Details");
do
{
draw->gotoxy(18, 5);
getline(cin, strcname);
flag = classValid::nameValidity(strcname);
if(flag == false)
{
strcname.clear();
}
if(!strcname.compare("q"))
{
return;
}
}while(strcname.empty());
do
{
draw->gotoxy(18, 6);
getline(cin, strcaddress);
if(!strcaddress.compare("q"))
{
return;
}
}while(strcaddress.empty());
do
{
draw->gotoxy(18, 7);
getline(cin, strmobileno);
if(!strmobileno.compare("q"))
{
return;
}
mobileno = classValid::intValidity(strmobileno);
if(mobileno == 0)
{
strmobileno.clear();
}
}while(strmobileno.empty());
sql = "select max(billno) from tableBilling;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
billno = std::stoi(row[0]);
}
else
{
billno = 0;
}
}
mysql_free_result(res);
}
sql = "select max(billno) from tableCustomers;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
int billno2 = std::stoi(row[0]);
strbillno = row[0];
if(billno2 > billno)
{
sql = "delete from tableCustomers where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2 , 9);
cout << "Duplicate entry for bill unique is deleted";
getc->getch();
}
else
{
draw->gotoxy(2, 9);
cout << "Error in deleting duplicate bill in tableCustomers : " << mysql_error(mysql);
getc->getch();
}
}
}
}
strbillno = std::to_string(++billno);
mysql_free_result(res);
}
sql = "select max(id) from tableCustomers ;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
maxid = std::stoi(row[0]);
}
else
{
maxid = 0;
}
}
strmaxid = std::to_string(++maxid);
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 11);
cout << "Error in max(id) from tableCustomers : " << mysql_error(mysql);
getc->getch();
return;
}
sql = "insert into tableCustomers values('"+strmaxid+"', '"+strcname+"', '"+strcaddress+"', '"+strmobileno+"', '"+strbillno+"');";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 8);
cout << "Customer record inserted";
getc->getch();
}
else
{
draw->gotoxy(2, 8);
cout << "error in insert into tableCustomers : " << mysql_error(mysql);
getc->getch();
}
LABEL1:
do
{
draw->clrscr();
draw->drawRect();
gamount = 0;
productid = prd->displayProduct("PRODUCT DISPLAY AND BILLING");
strprdid = std::to_string(productid);
if(productid == 0)
{
return;
}
sql = "select productname from tableProductRecords where productid = '"+strprdid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 11);
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) == nullptr)
{
cout << "Invalid product selection of incorrect product id";
getc->getch();
productid = 0;
}
mysql_free_result(res);
}
}while(productid == 0);
if(productid > 0)
{
draw->clrscr();
draw->drawRect();
get->printMenu(salesmenu, "Billing");
draw->gotoxy(45 ,3);
cout << "Bill No : " << strbillno;
draw->gotoxy(31, 5);
cout << productid;
sql = "select productname, stock, rate from tableProductRecords where productid = '"+strprdid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql,sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 6);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 6);
strproductname = row[0];
cout << strproductname;
draw->gotoxy(x, 7);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 7);
stock = std::stoi(row[1]);
cout << stock;
draw->gotoxy(x, 8);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 8);
rate = std::stoi(row[2]);
cout << rate;
}
mysql_free_result(res);
}
sql = "select p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, tableBilling as b where b.billno = '"+
strbillno +"' and productname in(select productname from tableProductRecords where productid = b.productid);";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 17);
cout << "Product purchased : ";
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 20, totallen, 0);
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 18);
cout << "Error in Product purchased : " << mysql_error(mysql);
getc->getch();
return;
}
do
{
draw->gotoxy(x, 9);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 9);
getline(cin, strquantity);
if(!strquantity.compare("q"))
{
goto LABEL1;
}
if(!strquantity.empty())
{
quantity = classValid::intValidity(strquantity);
if(quantity == 0)
{
draw->gotoxy(x, 9);
cout << "Invalid quantity";
strquantity.clear();
getc->getch();
}
}
else
{
quantity = 0;
}
}while(quantity == 0);
qty = stock - quantity;
if(qty >= 0)
{
strqty = std::to_string(qty);
total = quantity * rate;
gtotal += total;
draw->gotoxy(x, 10);
cout << total;
}
else
{
draw->gotoxy(10, 12);
cout << "Insufficient stock, available only : " << stock;
getc->getch();
goto LABEL1;
}
strstock = std::to_string(stock);
strquantity = std::to_string(quantity);
strrate = std::to_string(rate);
strtotal = std::to_string(total);
date *dt = new date;
time_t now = time(0);
tm *ltm = std::localtime(&now);
dt->yy = ltm->tm_year;
dt->mm = ltm->tm_mon + 1;
dt->dd = ltm->tm_mday;
strdate = std::to_string(1900 + dt->yy)+ "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
draw->gotoxy(45, 4);
cout << "Date : " << strdate;
sql = "select max(id) from tableBilling;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
maxid = 0;
}
else
{
maxid = std::stoi(row[0]);
}
strmaxid = std::to_string(++maxid);
}
mysql_free_result(res);
}
else
{
draw->gotoxy(10, 13);
cout << "error in max(id) billing : " << mysql_error(mysql);
getc->getch();
}
sql = "select max(id) from tableCustomers;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
customerid = 0;
draw->gotoxy(10, 13);
cout << "Error : Customer record not found, please enter customer record";
getc->getch();
return;
}
else
{
customerid = std::stoi(row[0]);
}
}
strcustomerid = std::to_string(customerid);
mysql_free_result(res);
}
sql = "insert into tableBilling(id, productid, quantity, dateofsale, total, billno, customerid) values('"+
strmaxid +"','"+strprdid+"','"+strquantity+"','"+strdate+"', '"+strtotal+"','"+strbillno+"','"+strcustomerid+"');";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(11, 11);
cout << "Billing data inserted successfully";
sql = "select customername from tableCustomers where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
strcname = row[0];
draw->gotoxy(2, 18);
cout << "Customer Name : " << strcname;
}
mysql_free_result(res);
}
}
else
{
draw->gotoxy(1, 15);
cout << "Error in insert into billing : " << mysql_error(mysql);
getc->getch();
}
sql = "select p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, tableBilling as b where b.billno = '"+
strbillno +"' and productname in(select productname from tableProductRecords where productid = b.productid);";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(1, 15);
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 20, totallen, 0);
mysql_free_result(res);
}
else
{
draw->gotoxy(1, 15);
cout << "Error in billing data select : " << mysql_error(mysql);
getc->getch();
}
sql = "update tableProductRecords set stock = '"+strqty+"' where productid = '"+strprdid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(x, 7);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 7);
cout << strqty;
draw->gotoxy(11, 12);
cout << "Stock data modified succesfully";
getc->getch();
}
else
{
draw->gotoxy(1, 15);
cout << "Error in update tableProductRecords : " << mysql_error(mysql);
getc->getch();
}
sql = "select sum(total) from tableBilling where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
gamount = std::stoi(row[0]);
}
}
mysql_free_result(res);
}
else
{
draw->gotoxy(2, lines + 25);
cout << "Error in sum gamount : " << mysql_error(mysql);
getc->getch();
}
draw->gotoxy(x, lines + 25);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, lines + 25);
cout << "Grand Total : " << gamount;
getc->getch();
draw->gotoxy(11, 13);
cout << "Want to add more records : ";
do
{
yesno = cin.get();
if(toupper(yesno) == 'Y' || toupper(yesno) == 'N' || int(yesno) == 10)
{
draw->gotoxy(35, 13);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 35; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(35 ,13);
}
}while(toupper(yesno) != 'Y' && toupper(yesno) != 'N');
if(toupper(yesno) == 'Y')
{
getc->getch();
goto LABEL1;
}
else if(toupper(yesno) == 'N')
{
return;
}
}
}
void classBill::todaysSales()
{
mysql = classConn::connection();
mysql->reconnect = true;
draw->clrscr();
draw->drawRect();
draw->gotoxy(15, 3);
cout << "Sales By Date";
draw->gotoxy(13, 4);
cout << "------------------";
draw->gotoxy(10, 5);
cout << "Enter date to see sale : ( yyyy/MM/ dd ) : ";
do
{
flag = true;
draw->gotoxy(55, 5);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 55; i < w.ws_col -1; i++)
{
cout << " ";
}
draw->gotoxy(55, 5);
getline(cin, strdate);
if(strdate.empty())
{
flag = true;
}
else
{
flag = classValid::isValidDate(strdate);
if(!flag)
{
draw->gotoxy(10, 6);
cout << "Invalid date please re-enter ";
strdate.clear();
getc->getch();
draw->gotoxy(10, 6);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 10; i < w.ws_col -1; i++)
{
cout << " ";
}
}
}
}while(flag == false);
if(strdate.empty())
{
date *dt = new date;
time_t now = time(0);
tm *ltm = std::localtime(&now);
dt->yy = ltm->tm_year;
dt->mm = ltm->tm_mon + 1;
dt->dd = ltm->tm_mday;
strdate = std::to_string(1900 + dt->yy) +"/" + std::to_string(dt->mm) +"/" +std::to_string(dt->dd);
draw->gotoxy(10, 6);
cout << "Date : " << strdate;
}
else
{
draw->gotoxy(10, 6);
cout << "Date : " << strdate;
}
sql = "select distinct c.customername, c.contactaddress, c.mobileno, b.dateofsale, b.billno from tableCustomers as c, tableBilling as b ";
sql += " where c.billno = b.billno and b.dateofsale = '"+strdate+"' order by b.billno;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 7, totallen, 0);
mysql_free_result(res);
tlen1 = *totallen;
}
else
{
draw->gotoxy(2, lines + 10);
cout << "Error in first sql in todays sale : " <<mysql_error(mysql);
getc->getch();
}
sql = "select p.productname, p.rate, b.quantity, b.total, b.billno from tableProductRecords as p, tableBilling as b ";
sql += " where b.dateofsale = '"+strdate+"' and p.productname in (select productname from tableProductRecords where productid ";
sql += " = b.productid) order by b.billno";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
lines2 = sqlp->process_result_set(mysql, res, lines + 13, totallen ,tlen1);
mysql_free_result(res);
}
else
{
draw->gotoxy(10, 7);
cout << "Error in todays sale : " << mysql_error(mysql);
getc->getch();
return;
}
sql = "select sum(total) from tableBilling where dateofsale = '"+strdate+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
gtotal = std::stoi(row[0]);
}
else
{
gtotal = 0;
}
}
mysql_free_result(res);
}
else
{
draw->gotoxy(10, lines + 25);
cout << "Error in sum(total) : " << mysql_error(mysql);
getc->getch();
}
draw->gotoxy(10 + *totallen , lines + 15 + lines2);
cout << " Grand total : " << gtotal;
getc->getch();
}
void classBill::modifyBill()
{
draw->clrscr();
draw->drawRect();
int const x = 18;
mysql = classConn::connection();
mysql->reconnect = true;
l = get->printMenu(vecBillmod, "Billing Modification");
do
{
draw->gotoxy(x, 5);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 5);
getline(cin, strbillno);
if(!strbillno.compare("q"))
{
return;
}
if(!strbillno.compare("l"))
{
draw->gotoxy(x, 5);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 5);
sql = "select max(billno) from tableBilling;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
strbillno = row[0];
billno = std::stoi(row[0]);
}
}
mysql_free_result(res);
}
draw->gotoxy(x, 5);
cout << billno;
}
l = classValid::intValidity(strbillno);
if(l == 0)
{
draw->gotoxy(x, 5);
cout << "Invalid product id please re-enter";
strbillno.clear();
getc->getch();
}
}while(strbillno.empty());
sql = "select p.productid, p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, ";
sql += " tableBilling as b where b.billno = '"+strbillno +"' and p.productname in(select productname ";
sql += " from tableProductRecords where productid = b.productid) order by p.productid";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 17, totallen, 0);
len = *totallen;
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 12);
cout << "Error in sql strbillno : " << mysql_error(mysql);
getc->getch();
}
sql = "select customername, id from tableCustomers where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if(row[0] != nullptr)
{
draw->gotoxy(x, 11);
for(i = x; i <w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 11);
strcname = row[0];
cout << strcname;
}
if(row[1] != nullptr)
{
draw->gotoxy(x, 12);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 12);
strcustomerid = row[1];
cout << strcustomerid;
}
}
mysql_free_result(res);
}
sql = "select dateofsale from tableBilling where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if(row[0] != nullptr)
{
draw->gotoxy(x, 13);
for(i = x; i <w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 13);
strdate = row[0];
cout << strdate;
}
}
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 14);
cout << "Error in dateofsale : " << mysql_error(mysql);
getc->getch();
}
draw->gotoxy(x, 5);
cout << strbillno;
LABEL1:
draw->gotoxy(2, 14);
cout << "Command : d for delete, a for add, q for return : ";
do
{
draw->gotoxy(54, 14);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 54; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(54, 14);
getline(cin, strcmd);
if(strcmd.compare("a") && strcmd.compare("d") && strcmd.compare("q"))
{
draw->gotoxy(54, 14);
cout << "Invalid Command";
strcmd.clear();
getc->getch();
}
}while(strcmd.compare("a") && strcmd.compare("d") && strcmd.compare("q"));
if(!strcmd.compare("q"))
{
return;
}
else if(!strcmd.compare("a"))
{
draw->gotoxy(x, 6);
prdid = prd->displayProduct("Billing Modification");
draw->clrscr();
draw->drawRect();
get->printMenu(vecBillmod, "Billing Modification");
strprdid = std::to_string(prdid);
vprdvec.clear();
sql = "select p.productid, p.productname, p.rate, p.stock from tableProductRecords as p where ";
sql += " p.productid = '"+strprdid+"' order by p.productid;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->clrscr(1, 17, w.ws_col - 1, w.ws_row -1);
draw->drawRect();
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
vp.pid = std::stoi(row[0]);
}
if(row[1] != nullptr)
{
vp.pname = row[1];
}
if(row[2] != nullptr)
{
vp.prate = std::stoi(row[2]);
}
if(row[3] != nullptr)
{
stock = std::stoi(row[3]);
}
vp.pquantity = 0;
vp.ptotal = 0;
}
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 15);
cout << "Error in vp : " << mysql_error(mysql);
getc->getch();
return;
}
draw->gotoxy(x, 5);
cout << strbillno;
draw->gotoxy(x, 6);
cout << vp.pid;
draw->gotoxy(x, 7);
cout << vp.pname;
draw->gotoxy(x, 8);
cout << vp.prate;
draw->gotoxy(x, 11);
cout << strcname;
draw->gotoxy(x, 12);
cout << strcustomerid;
draw->gotoxy(x, 13);
cout << strdate;
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 9);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 9);
getline(cin, strquantity);
if(!strquantity.compare("q"))
{
return;
}
vp.pquantity = classValid::intValidity(strquantity);
if(vp.pquantity == 0)
{
draw->gotoxy(x, 9);
cout << "Invalid quantity ";
strquantity.clear();
}
}while(strquantity.empty());
vp.ptotal = vp.prate * vp.pquantity;
draw->gotoxy(x, 10);
cout << vp.ptotal;
qty = stock - vp.pquantity;
strqty = std::to_string(qty);
if(qty < 0)
{
draw->gotoxy(2, 15);
cout << "insufficient stock only available : " << stock;
goto LABEL1;
}
sql = "select max(id) from tableBilling;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
id = std::stoi(row[0]);
}
else
{
id = 0;
}
strid = std::to_string(++id);
}
mysql_free_result(res);
}
strvpid = std::to_string(vp.pid);
strquantity = std::to_string(vp.pquantity);
strtotal = std::to_string(vp.ptotal);
sql = "insert into tableBilling values('"+strid+"','"+strvpid+"','"+strquantity+"','"+strdate+"',";
sql +=" '"+strtotal+"','"+strbillno+"','"+strcustomerid+"');";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 16);
cout << "Data inserted successfully";
getc->getch();
sql = "update tableProductRecords set stock = '"+strqty+"' where productid = '"+std::to_string(vp.pid)+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 17);
cout << "ProductRecords stock data modified";
getc->getch();
}
else
{
draw->gotoxy(2, 17);
cout << "ProductRecords : unable to update stock data";
getc->getch();
}
}
else
{
draw->gotoxy(2, lines + 25);
cout << "Error in insert into tableBilling : " << mysql_error(mysql);
getc->getch();
}
sql = "select p.productid, p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, ";
sql += " tableBilling as b where b.billno = '"+strbillno +"' and p.productname in(select productname ";
sql += " from tableProductRecords where productid = b.productid) order by p.productid";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
sqlp->process_result_set(mysql, res, 20, totallen, 0);
mysql_free_result(res);
getc->getch();
}
getc->getch();
}
else if(!strcmd.compare("d"))
{
vprdvec.clear();
draw->gotoxy(x, 6);
sql = "select p.productid, p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, ";
sql += " tableBilling as b where b.billno = '"+strbillno +"' and p.productname in(select productname ";
sql += " from tableProductRecords where productid = b.productid) order by p.productid";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
while((row = mysql_fetch_row(res)) != nullptr)
{
Vprd vp;
if(row[0] != nullptr)
{
vp.pid = std::stoi(row[0]);
}
if(row[1] != nullptr)
{
vp.pname = row[1];
}
if(row[2] != nullptr)
{
vp.prate = std::stoi(row[2]);
}
if(row[3] != nullptr)
{
vp.pquantity = std::stoi(row[3]);
}
if(row[4] != nullptr)
{
vp.ptotal = std::stoi(row[4]);
}
vprdvec.push_back(vp);
}
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 15);
cout << "Error in vprdvec : " << mysql_error(mysql);
getc->getch();
return;
}
if(vprdvec.empty())
{
draw->gotoxy(2, 15);
cout << "Product does not exist";
getc->getch();
return;
}
for(vprditer = vprdvec.begin(); ; vprditer++)
{
escch = getc->getch();
if(vprditer == vprdvec.end())
{
vprditer = vprdvec.begin();
}
vp = *vprditer;
if((int(escch)) == 9)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 6);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 6);
cout << vp.pid;
strprdid = std::to_string(vp.pid);
draw->gotoxy(x, 7);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 7);
cout << vp.pname;
draw->gotoxy(x, 8);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 8);
cout << vp.prate;
draw->gotoxy(x, 9);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 9);
cout << vp.pquantity;
draw->gotoxy(x, 10);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 10);
cout << vp.ptotal;
draw->gotoxy(x, 6);
}
else if(int(escch) == 10)
{
draw->gotoxy(2, lines + 25);
cout << "Really want to delete : ";
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
do
{
draw->gotoxy(27, lines + 25);
for(i = 27; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(27, lines + 25);
cin.get(yesno);
}while(toupper(yesno)!= 'Y' && toupper(yesno)!= 'N' && toupper(yesno)!= 'Q');
if(toupper(yesno) == 'Q' || toupper(yesno) == 'N')
{
return;
}
else if(toupper(yesno) == 'Y')
{
if(!strprdid.empty() && !strbillno.empty() && !strcustomerid.empty())
{
sql = "select id, quantity from tableBilling where productid = '"+strprdid+"' and ";
sql += " billno = '"+strbillno+"' and customerid = '"+strcustomerid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
strid = row[0];
}
if(row[1] != nullptr)
{
strquantity = row[1];
quantity = std::stoi(row[1]);
}
}
mysql_free_result(res);
}
sql = "select stock from tableProductRecords where productid = '"+strprdid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
stock = std::stoi(row[0]);
}
}
mysql_free_result(res);
}
stock = stock + quantity;
strstock = std::to_string(stock);
sql = "update tableProductRecords set stock = '"+strstock+"' where productid = '"+strprdid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, lines +25);
cout << "data updated to product record";
getc->getch();
}
else
{
draw->gotoxy(2, 18);
cout << "stock data updation in tableProductRecords failed : " << mysql_error(mysql);
getc->getch();
}
sql = "delete from tableBilling where id = '"+strid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 15);
cout << "Data has been deleted";
getc->getch();
sql = "select p.productid, p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, ";
sql += " tableBilling as b where b.billno = '"+strbillno +"' and p.productname in(select productname ";
sql += " from tableProductRecords where productid = b.productid) order by p.productid";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->clrscr(1, 17, w.ws_col - 1, w.ws_row -1);
draw->drawRect();
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 17, totallen, 0);
draw->gotoxy(x, 5);
mysql_free_result(res);
getc->getch();
}
if(lines == 0)
{
sql = " delete from tableCustomers where id = '"+strcustomerid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for( i = 2; i < w.ws_col - 1; i++)
{
cout << " ";
}
cout << "customer deleted from table";
getc->getch();
}
sql = "select sum(total) from tableBilling where billno = '"+strbillno+"'";
sql += " and customerid = '"+strcustomerid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
draw->gotoxy(15, lines + 22);
for(i = 15; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(15 ,lines + 22);
gtotal = std::stoi(row[0]);
cout << "Grand Total : " << gtotal;
getc->getch();
}
}
mysql_free_result(res);
}
}
}
}
else
{
draw->gotoxy(2 ,15);
cout << "strprdid or strbillno or strcustomerid is null";
getc->getch();
}
}
}
else if(escch == 'q')
{
return;
}
}
}
draw->gotoxy(2, lines + 26);;
cout << "do you want to add or delete more (y / n): ";
do
{
draw->gotoxy(45, lines + 26);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 45; i < w.ws_col -1 ; i++)
{
cout << " ";
}
draw->gotoxy(45, lines + 26);
getline(cin, strchoice);
if(!strchoice.compare("Y") || !strchoice.compare("y"))
{
goto LABEL1;
}
else if(!strchoice.compare("N") || !strchoice.compare("n"))
{
return;
}
}while(strchoice.compare("Y") && strchoice.compare("y") && strchoice.compare("N") && strchoice.compare("n"));
}
void classBill::viewBill()
{
mysql = classConn::connection();
mysql->reconnect = true;
int const x = 19;
draw->clrscr();
draw->drawRect();
int l = get->printMenu(cnamevec, "Viewing Bill");
draw->gotoxy(2, 4);
cout << "Enter Bill No : ";
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 4);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 4);
getline(cin, strbillno);
if(!strbillno.compare("q"))
{
return;
}
if(!strbillno.compare("l"))
{
draw->gotoxy(x, 4);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x ,4);
sql = "select max(billno) from tableBilling;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
strbillno = row[0];
billno = std::stoi(strbillno);
draw->gotoxy(x, 4);
cout << strbillno;
}
}
mysql_free_result(res);
}
}
billno = classValid::intValidity(strbillno);
if(billno == 0)
{
strbillno.clear();
draw->gotoxy(x, 4);
cout << "Invalid Bill number";
getc->getch();
}
}while(strbillno.empty());
sql = "select customername, contactaddress, mobileno from tableCustomers where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if(row[0] != nullptr)
{
draw->gotoxy(x, 5);
for(i= x; i < w.ws_col - 1; i++)
{
cout << " ";
}draw->gotoxy(x, 5);
cout << row[0];
}
if(row[1] != nullptr)
{
draw->gotoxy(x, 6);
for(i= x; i < w.ws_col - 1; i++)
{
cout << " ";
}draw->gotoxy(x, 6);
cout << row[1];
}
if(row[1] != nullptr)
{
draw->gotoxy(x, 7);
for(i= x; i < w.ws_col - 1; i++)
{
cout << " ";
}draw->gotoxy(x, 7);
cout << row[2];
}
}
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 8);
cout << "Error in select customer name : " << mysql_error(mysql);
getc->getch();
}
sql = "select p.productid, p.productname, p.rate, b.quantity, b.total from tableProductRecords as p, ";
sql += " tableBilling as b where b.billno = '"+strbillno +"' and p.productname in(select productname ";
sql += " from tableProductRecords where productid = b.productid) order by p.productid";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
draw->clrscr(1, 10, w.ws_col -1, w.ws_row - 1);;
draw->drawRect();
lines = sqlp->process_result_set(mysql, res, 10, totallen, 0);
mysql_free_result(res);
}
else
{
draw->gotoxy(2, lines + 20);
cout << "Error in select of view : " << mysql_error(mysql);
getc->getch();
}
sql = "select suml(total) from tableBilling where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
draw->gotoxy(10, lines + 15);
cout << "Grand total : " << row[0];
}
}
mysql_free_result(res);
}
getc->getch();
}
void classBill::delEntry()
{
sql = "select max(billno) from tableBilling;";
mysql = classConn::connection();
mysql->reconnect = true;
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
billno = std::stoi(row[0]);
}
mysql_free_result(res);
}
sql = "select max(billno) from tableCustomers;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
int billno2 = std::stoi(row[0]);
strbillno = row[0];
if(billno2 > billno)
{
sql = "delete from tableCustomers where billno = '"+strbillno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 23);
cout << "duplicate entry for bill unique is deleted";
getc->getch();
}
else
{
draw->gotoxy(2, 23);
cout << " Error in deleting duplicate bill in tableCustomers : " << mysql_error(mysql);
}
}
}
}
mysql_free_result(res);
}
}
/**********************************************
*
* file : classConn.h
*
***********************************************/
#ifndef CLASSCONN_H
#define CLASSCONN_H
#include <mysql.h>
#include <my_global.h>
#include <my_sys.h>
#include <vector>
#include <string>
#include <iostream>
class classConn
{
public:
classConn();
virtual ~classConn();
std::vector<std::string> menu;
static int qstate;
static MYSQL_ROW row;
static MYSQL_RES *res;
static MYSQL_FIELD *field;
static MYSQL *mysql;
static MYSQL* connection();
protected:
private:
int * totallen;
};
#endif // CLASSCONN_H
/**********************************************
*
* file : classConn.cpp
*
***********************************************/
#include "classConn.h"
static char *opt_host = "serverora.db.net";
static char *opt_user_name = "rahul";
static char *opt_password = "rahul";
static unsigned int opt_port = 3306;
static char *opt_socket_name = NULL;
static char * opt_databse_name = "cbsdb";
static unsigned int opt_flags = 0;
classConn::classConn()
{
//ctor
menu.push_back("1. Show All Products");
menu.push_back("2. Display Product By Product ID");
menu.push_back("3. Enter Product Details");
menu.push_back("4. View An Existing Product Record");
menu.push_back("5. Billing");
menu.push_back("6. Today's Sale");
menu.push_back("7. Modify Product Record");
menu.push_back("8. Modify Customer Record");
menu.push_back("9. View Customer Record");
menu.push_back("10. Modify Bill");
menu.push_back("11. View Bill");
menu.push_back("12. Delete Latest Invalid Entry");
menu.push_back("13. Exit");
totallen = new int;
}
MYSQL *classConn::mysql = nullptr;
int classConn::qstate = 0;
MYSQL_ROW classConn::row = 0;
MYSQL_RES *classConn::res = nullptr;
MYSQL_FIELD *classConn::field = nullptr;
classConn::~classConn()
{
//dtor
}
MYSQL * classConn::connection()
{
mysql_init(mysql);
if(mysql_library_init(0, NULL, NULL))
{
std::cout << "error in mysql lib init " << std::endl;
exit(1);
}
mysql = mysql_init(mysql);
if(mysql == NULL)
{
std::cout << "mysql connector is NULL";
exit(2);
}
mysql = mysql_real_connect(mysql, opt_host, opt_user_name, opt_password, opt_databse_name, opt_port, opt_socket_name, opt_flags);
if(mysql == 0)
{
std::cout << "mysql_real_connect returned 0";
return 0;
}
return mysql;
}
/**********************************************
*
* file : classCustomer.h
*
***********************************************/
#ifndef CLASSCUSTOMER_H
#define CLASSCUSTOMER_H
#include <iostream>
#include <mysql.h>
#include <sys/ioctl.h>
#include <cstdio>
#include <cstdlib>
#include <bits/stdc++.h>
#include "classConn.h"
#include "classDraw.h"
#include "classGetchar.h"
#include "classSqlProcess.h"
#include "classGetchoice.h"
class classCustomer
{
public:
classCustomer();
virtual ~classCustomer();
void showCustomer();
void deleteCustomer(int id, int x, int y1);
void modifyCustomerRecord();
protected:
private:
winsize w;
std::vector<std::string> customerMenu;
std::vector<int> idvec;
std::vector<int>::iterator iter;
std::string strid;
std::string strcname;
std::string straddress;
std::string strmobileno;
std::string strbillno;
std::string sql;
std::string strchoice;
std::string strnameid;
std::string strcid;
classDraw *draw;
classGetchar *getc;
classGetchoice * get;
classSqlProcess *sqlp;
int id;
int billno;
int qstate;
int choice;
int len;
int lines;
int i;
bool f;
char escch;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
int *totallen;
unsigned long mobileno;
};
#endif // CLASSCUSTOMER_H
/**********************************************
*
* file : classCustomer.cpp
*
***********************************************/
#include "classCustomer.h"
using std::cout;
using std::cin;
using std::string;
classCustomer::classCustomer()
{
//ctor
draw = new classDraw;
mysql = new MYSQL;
getc = new classGetchar;
sqlp = new classSqlProcess;
get = new classGetchoice;
customerMenu.push_back("ID : ");
customerMenu.push_back("Customer Name : ");
customerMenu.push_back("Customer Address : ");
customerMenu.push_back("Mobile No : ");
customerMenu.push_back("Bill No : ");
totallen = new int;
}
classCustomer::~classCustomer()
{
//dtor
delete mysql;
delete draw;
delete getc;
delete get;
delete sqlp;
delete totallen;
}
void classCustomer::showCustomer()
{
draw->clrscr();
draw->drawRect();
int const x1= 20;
int l = get->printMenu(customerMenu, "Customer Details");
draw->gotoxy(20, 6);
do
{
draw->gotoxy(20, 6);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for( i = 20; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(20, 6);
getline(cin, strcname);
if(!strcname.compare("q"))
{
return;
}
bool f= classValid::nameValidity(strcname);
if(f == false)
{
draw->gotoxy(20, 6);
cout << "Enter valid name";
strcname.clear();
getc->getch();
}
}while(strcname.empty());
sql = "select * from tableCustomers where customername like '%"+strcname+"%';";
mysql = classConn::connection();
mysql->reconnect = true;
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 14, totallen, 0);
len = *totallen;
mysql_free_result(res);
}
getc->getch();
}
void classCustomer::deleteCustomer(int id, int x, int y)
{
string strid = std::to_string(id);
sql = "delete from tableCustomers where id = '"+strid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(x, y);
cout << "customer record deleted from table id : " << strid;
getc->getch();
}
}
void classCustomer::modifyCustomerRecord()
{
draw->clrscr();
draw->drawRect();
int const x1 = 20;
int l = get->printMenu(customerMenu, "Modify Customer Details");
draw->gotoxy(20, 6);
do
{
draw->gotoxy(x1, 6);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 6);
getline(cin, strcname);
if(!strcname.compare("q"))
{
return;
}
f = classValid::nameValidity(strcname);
if(f == false)
{
draw->gotoxy(x1, 6);
cout << "Enter valid name";
strcname.clear();
getc->getch();
}
}while(strcname.empty());
sql = "select * from tableCustomers where customername like '%"+strcname+"%';";
mysql = classConn::connection();
mysql->reconnect = true;
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 14, totallen, 0);
len = *totallen;
mysql_free_result(res);
}
sql = "select id from tableCustomers where customername like '%"+strcname+"%';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
idvec.clear();
res = mysql_store_result(mysql);
while((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
idvec.push_back(std::stoi(row[0]));
}
}
if(idvec.empty())
{
draw->gotoxy(2, 10);
cout << "Customer data not exist";
getc->getch();
return;
}
draw->gotoxy(x1, 5);
for(iter = idvec.begin();; iter++)
{
escch = getc->getch();
if((int(escch)) == 9)
{
strnameid = std::to_string(*iter);
draw->gotoxy(2, 10);
cout<< "Search string : ";
draw->gotoxy(18, 10);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(18, 10);
cout << strnameid;
mysql = classConn::connection();
sql = "select *from tableCustomers where id = '"+strnameid+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
draw->gotoxy(x1, 5);
for(i = x1; i < len -2; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5);
cout << row[0];
draw->gotoxy(x1, 6);
for(i = x1; i < len -2; i++)
{
cout << " ";
}
draw->gotoxy(x1, 6);
cout << row[1];
draw->gotoxy(x1, 7);
for(i = x1; i < len -2; i++)
{
cout << " ";
}
draw->gotoxy(x1, 7);
cout << row[2];
draw->gotoxy(x1, 8);
for(i = x1; i < len -2; i++)
{
cout << " ";
}
draw->gotoxy(x1, 8);
cout << row[3];
draw->gotoxy(x1, 9);
for(i = x1; i < len -2; i++)
{
cout << " ";
}
draw->gotoxy(x1, 9);
cout << row[4];
}
mysql_free_result(res);
}
else
{
draw->gotoxy(1, lines + 18);
cout << "Error in tab : " << mysql_error(mysql);
getc->getch();
}
}
else if(int(escch) == 10)
{
iter--;
id = *iter;
strnameid = std::to_string(id);
break;
}
if(iter == idvec.end())
{
iter = idvec.begin();
iter--;
}
}
}
else
{
draw->gotoxy(2, 11);
cout << "Error in else of customername like : " << mysql_error(mysql);
getc->getch();
}
do
{
draw->gotoxy(2, 12);
cout << "Which data do you want to modify ( 1 .. 5) d for delete, q for return : ";
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(73, 12);
for(i = 73; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(73, 12);
getline(cin, strchoice);
choice = classValid::intValidity(strchoice);
if(choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 &&
strchoice.compare("d") && strchoice.compare("q"))
{
draw->gotoxy(70, 12);
cout << "Invalid input";
getc->getch();
}
}while(choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 &&
strchoice.compare("d") && strchoice.compare("q"));
if(!strchoice.compare("d"))
{
mysql = classConn::connection();
strid = std::to_string(id);
sql = "delete from tableCustomers where id = '"+strid+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 13);
cout << "Customer data deleted of id : " << id;
getc->getch();
return;
}
}
else if(!strchoice.compare("q"))
{
return;
}
draw->gotoxy(x1, 5 + --choice);
switch(choice)
{
case 0:
{
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x1, 5 + choice);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5 + choice);
getline(cin, strid);
if(!strid.compare("q"))
{
return;
}
f = classValid::intValidity(strid);
if(f == 0)
{
strid.clear();
}
if(!strid.empty())
{
sql = "select customername from tableCustomers where id = '"+strid+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
draw->gotoxy(x1, 5 + choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5 + choice);
cout << " ID already exist, please re-enter id ";
strid.clear();
getc->getch();
}
mysql_free_result(res);
}
}
}while(strid.empty());
sql = "update tableCustomers set id = '"+strid+"' where id = '"+strnameid+"';";
strnameid = strid;
}
break;
case 1:
{
do
{
draw->gotoxy(x1, 5 + choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5 + choice);
getline(cin, strcname);
if(!strcname.compare("q"))
{
return;
}
f = classValid::nameValidity(strcname);
if(f == false)
{
strcname.clear();
}
}while(strcname.empty());
sql = "update tableCustomers set customername = '"+strcname+"' where id = '"+strnameid+"';";
}
break;
case 2:
{
do
{
draw->gotoxy(x1, 5 + choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5 + choice);
getline(cin, straddress);
if(!straddress.compare("q"))
{
return;
}
}while(straddress.empty());
sql = "update tableCustomers set contactaddress = '"+straddress+"' where id = '"+strnameid+"';";
}
break;
case 3:
{
do
{
draw->gotoxy(x1, 5 + choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5 + choice);
getline(cin, strmobileno);
if(!strmobileno.compare("q"))
{
return;
}
f = classValid::intValidity(strmobileno);
if(f == 0)
{
strmobileno.clear();
}
}while(strmobileno.empty());
sql = "update tableCustomers set mobileno = '"+strmobileno+"' where id = '"+strnameid+"';";
}
break;
case 4:
{
do
{
draw->gotoxy(x1, 5 + choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5 + choice);
getline(cin, strbillno);
if(!strbillno.compare("q"))
{
return;
}
f = classValid::intValidity(strbillno);
if(f == 0)
{
strbillno.clear();
}
}while(strbillno.empty());
sql = "update tableCustomers set billno = '"+strbillno+"' where id = '"+strnameid+"';";
}
break;
default:
{
draw->gotoxy(2, lines + 20);
cout << "please enter valid choice";
}
}
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
if(choice >= 0 && choice <= 5)
{
draw->gotoxy(2, lines + 20);
cout << "Data updated successfully";
getc->getch();
}
else
{
draw->gotoxy(2, lines + 20);
cout << " updaiton failed incorrect choice : " << choice;
getc->getch();
}
}
}
/**********************************************
*
* file : classDraw.h
*
***********************************************/
#ifndef CLASSDRAW_H
#define CLASSDRAW_H
#include <iostream>
#include <cstdio>
#include <sys/ioctl.h>
#include <unistd.h>
class classDraw
{
public:
classDraw();
virtual ~classDraw();
void gotoxy(int x, int y);
void clrscr();
void clrscr(int x1, int y1, int x2, int y2);
void drawRect();
protected:
private:
struct winsize w;
int *totallen;
};
#endif // CLASSDRAW_H
/**********************************************
*
* file : classDraw.cpp
*
***********************************************/
#include "classDraw.h"
using std::cout;
classDraw::classDraw()
{
//ctor
totallen = new int;
}
classDraw::~classDraw()
{
//dtor
delete totallen;
}
void classDraw::gotoxy(int x, int y)
{
printf("%c[%d;%df", 0x1B, y, x);
}
void classDraw::clrscr()
{
gotoxy(0, 0);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int i = 0; i < w.ws_row; i++)
{
for(int j = 0; j < w.ws_col; j++)
{
std::cout << " ";
}
}
gotoxy(0, 0);
}
void classDraw::clrscr(int x1, int y1, int x2, int y2)
{
for(int x = x1; x <= x2; x++)
{
for(int y = y1; y <= y2; y++)
{
gotoxy(x, y);
std::cout << " ";
}
}
gotoxy(x1, y1);
}
void classDraw::drawRect()
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int i = 0; i <= w.ws_col; i++)
{
gotoxy(i, 1);
cout << "-";
gotoxy(i, w.ws_row);
cout << "-";
}
for(int j = 0; j < w.ws_row; j++)
{
gotoxy(1, j);
cout << "|";
gotoxy(w.ws_col, j);
cout << "|";
}
gotoxy(0, 0);
}
/**********************************************
*
* file : classGetchar.h
*
***********************************************/
#ifndef CLASSGETCHAR_H
#define CLASSGETCHAR_H
#include <termio.h>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <unistd.h>
static struct termios oldterm, newterm;
class classGetchar
{
public:
classGetchar();
virtual ~classGetchar();
char getch();
char getche();
void initTermios(bool echo);
void resetTermios(void);
protected:
private:
termios oldterm;
termios newterm;
char getch_(bool echo);
};
#endif // CLASSGETCHAR_H
/**********************************************
*
* file : classGetchar.cpp
*
***********************************************/
#include "classGetchar.h"
classGetchar::classGetchar()
{
//ctor
}
classGetchar::~classGetchar()
{
//dtor
}
void classGetchar::initTermios(bool echo)
{
tcgetattr(0, &oldterm);
newterm = oldterm;
newterm.c_lflag &= ~ICANON;
newterm.c_lflag &= echo ? ECHO : ~ECHO;
tcsetattr(0, TCSANOW, &newterm);
}
void classGetchar::resetTermios()
{
tcsetattr(0, TCSANOW, &oldterm);
}
char classGetchar::getch_(bool echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
char classGetchar::getch()
{
return getch_(false);
}
char classGetchar::getche()
{
return getch_(true);
}
/**********************************************
*
* file : classGetchoice.h
*
***********************************************/
#ifndef CLASSGETCHOICE_H
#define CLASSGETCHOICE_H
#include <vector>
#include "classConn.h"
#include "classDraw.h"
#include "classValid.h"
#include "classGetchar.h"
class classGetchoice :public classDraw, public classGetchar
{
public:
classGetchoice();
virtual ~classGetchoice();
int printMenu(std::vector<std::string> menu, std::string header);
int getchoice(std::vector<std::string> choices);
protected:
private:
int * totallen;
winsize w;
int i;
int l;
int space;
int choosen;
std::vector<std::string> menu;
std::vector<std::string>::const_iterator iter;
std::string strch;
MYSQL *mysql;
MYSQL_ROW row;
MYSQL_RES *res;
classDraw *draw;
};
#endif // CLASSGETCHOICE_H
/**********************************************
*
* file : classGetchoice.cpp
*
***********************************************/
#include "classGetchoice.h"
using std::cout;
using std::cin;
using std::string;
using std::vector;
classGetchoice::classGetchoice()
{
//ctor
totallen = new int;
draw = new classDraw;
}
classGetchoice::~classGetchoice()
{
//dtor
delete totallen;
delete draw;
}
int classGetchoice::printMenu(vector<string> menu, string header)
{
gotoxy(15, 2);
cout << header;
gotoxy(13, 3);
int len = header.length();
space = 13 + 3 + len;
for(i = 13; i <= space; i++)
{
cout << "-";
}
l = 5;
iter = menu.begin();
while(iter!= menu.end())
{
gotoxy(2, l);
cout << *iter++;
l++;
}
return l;
}
int classGetchoice::getchoice(vector<string> choices)
{
mysql = classConn::connection();
mysql->reconnect = true;
int l = 0;
int m = 0;
drawRect();
l = printMenu(choices, "MAIN MENU");
gotoxy(15, ++l);
do
{
gotoxy(15, l);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int i = 15; i < w.ws_col - 1; i++)
{
cout << " ";
}
gotoxy(15, l);
cout << "Enter Choice : ";
getline(cin, strch);
m = classValid::intValidity(strch);
}while(m < 1 || m > 13);
return m;
}
/**********************************************
*
* file : classProduct.h
*
***********************************************/
#ifndef CLASSPRODUCT_H
#define CLASSPRODUCT_H
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <sys/ioctl.h>
#include <X11/Xlib.h>
#include "classConn.h"
#include "classGetchoice.h"
#include "classDraw.h"
#include "classGetchar.h"
#include "classSqlProcess.h"
#include "classValid.h"
class classConn;
class classGetchar;
class classGetchoice;
class classDraw;
class classSqlProcess;
class classValid;
class classProduct
{
public:
classProduct();
virtual ~classProduct();
void showAllProduct();
unsigned long displayProduct();
unsigned long displayProduct(std::string header);
void productEnter();
classProduct* productSearch(int prdno);
void productModify();
protected:
private:
bool f;
struct winsize w;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
classSqlProcess *sqlp;
classDraw *draw;
classGetchar *getc;
classGetchoice *get;
std::vector<std::string> productRecords;
std::vector<int> prdids;
std::vector<int>::iterator iter;
std::string sql;
std::string strrate;
std::string strmaxpid;
std::string strstock;
std::string strprdid;
std::string strproductid;
std::string productname;
std::string strch;
std::string stryesno;
unsigned long productid;
unsigned long prdid;
int *totallen;
int intch;
int i;
int maxpid;
int qstate;
int billno;
int stock;
int rate;
int len;
int lines;
char ch;
char yesno;
};
#endif // CLASSPRODUCT_H
/**********************************************
*
* file : classProduct.cpp
*
***********************************************/
#include "classProduct.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
classProduct::classProduct()
{
//ctor
productRecords.push_back("Product Name : ");
productRecords.push_back("Stock : ");
productRecords.push_back("Rate : ");
productRecords.push_back("ProductID : ");
get = new classGetchoice;
getc = new classGetchar;
sqlp = new classSqlProcess;
draw = new classDraw;
totallen = new int;
res = new MYSQL_RES;
}
classProduct::~classProduct()
{
//dtor
delete get;
delete getc;
delete draw;
delete res;
delete sqlp;
delete totallen;
}
void classProduct::showAllProduct()
{
char choose;
draw->clrscr();
draw->drawRect();
int l = 5;
int i = 0;
int gtotalofstock = 0;
draw->gotoxy(15, 5);
char ch;
cout << "Welcome To Computer Shop";
draw->gotoxy(15, 6);
cout<< "Show All Items Menu\n";
do
{
gtotalofstock = 0;
do
{
draw->gotoxy(2, 7);
cout << "Enter Product name -> p | Stock -> s |\n| Rate -> r | ProductID -> i : ";
ch = getc->getche();
draw->gotoxy(31, 8);
ch = toupper(ch);
}while(ch != 'P' && ch != 'S' && ch != 'R' && ch != 'I' && int(ch) != 10);
if(ch == 'P')
{
sql = "select *from tableProductRecords order by productname asc;";
}
else if(ch == 'S')
{
sql = "select *from tableProductRecords order by stock asc;";
}
else if(ch == 'R')
{
sql = "select *from tableProductRecords order by Rate asc;";
}
else if(ch == 'I')
{
sql = "select *from tableProductRecords order by productid asc;";
}
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
i = sqlp->process_result_set(mysql, res, 11, totallen, 0);
mysql_free_result(res);
}
sql = "select rate, stock from tableProductRecords;";
mysql->reconnect = true;
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
while((row = mysql_fetch_row(res)) != nullptr)
{
gtotalofstock += std::stoi(row[0]) * std::stoi(row[1]);
}
mysql_free_result(res);
}
draw->gotoxy(2, 9);
cout << "Grand total of stock rate : " << gtotalofstock << " " << "INR";
draw->gotoxy(31, 8);
}while(int(ch) != 10);
}
unsigned long classProduct::displayProduct()
{
mysql = classConn::connection();
mysql->reconnect = true;
strprdid.clear();
do
{
draw->clrscr();
do
{
draw->drawRect();
draw->gotoxy(25, 3);
cout << "Search By Product ID";
draw->gotoxy(23, 4);
cout << "-------------------------";
draw->gotoxy(11, 5);
cout << "Enter Product ID : ";
draw->gotoxy(31, 5);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 31; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(31, 5);
cout << strprdid;
ch = getc->getche();
intch = int(ch);
if(intch == 127 && strprdid.length() > 0)
{
draw->gotoxy(31, 5);
for(i = 31; i < w.ws_col - 1; i++)
{
cout << " ";
}
strprdid = strprdid.substr(0, strprdid.length() - 1);
draw->gotoxy(31, 5);
cout << strprdid;
}
else
{
strprdid += ch;
}
prdid = classValid::intValidity(strprdid);
if(prdid == 0 && !strprdid.empty() && intch != 10)
{
draw->gotoxy(31, 5);
cout << "invalid input";
getc->getch();
strprdid.clear();
}
}while(prdid == 0 && intch != 10);
if(intch != 10)
{
sql = "select * from tableProductRecords where productid like '%"+strprdid+"%';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->drawRect();
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(1, 11);
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 10, totallen, 0);
getc->getch();
mysql_free_result(res);
}
else
{
draw->gotoxy(10, 20);
strprdid.clear();
cout << "Error in product search by id : " << mysql_error(mysql);
getc->getch();
}
}
}while(intch != 10);
return prdid;
}
unsigned long classProduct::displayProduct(string header)
{
unsigned long l = 0, l2 = 0;
unsigned long prdid;
int intch;
int length = 0;
int x1 = 24;
int lines = 0;
string prdin = "";
string prdin2 = "";
string sql;
string strbillno;
char ch;
char escch;
bool flag = false;
mysql = classConn::connection();
mysql->reconnect = true;
prdin.clear();
do
{
draw->clrscr();
draw->drawRect();
get->printMenu(productRecords, header);
draw->gotoxy(2, 5);
cout << "Enter Product Name : ";
draw->gotoxy(x1, 5);
cout << prdin;
ch = getc->getche();
intch = int(ch);
string prdsearch;
if(intch == 27)
{
return 0;
}
else if(intch == 127 && prdin.length() > 0)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x1, 5);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
prdin = prdin.substr(0, prdin.length() -1);
}
else if(intch == 10 && prdin.length() == 0)
{
prdid = 0;
return 0;
}
else if(intch == 9)
{
sql = "select productid from tableProductRecords where productname like '"+prdin+"%' order by productname;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->clrscr(2, 10, w.ws_col - 1, w.ws_row-1);
draw->gotoxy(22, 5);
res = mysql_store_result(mysql);
prdids.clear();
while((row = mysql_fetch_row(res)) != nullptr)
{
prdids.push_back(std::stoi(row[0]));
}
if(prdids.empty())
{
prdid = 0;
draw->gotoxy(2, 9);
cout << "Product does not exist";
getc->getch();
return 0;
}
iter= prdids.begin();
while((int(escch = getc->getch())) == 9 && iter != prdids.end())
{
strprdid = std::to_string(*iter++);
sql = "select *from tableProductRecords where productname like '"+prdin+"%' order by productname;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->clrscr(1, 10, w.ws_col - 1, w.ws_row - 1);
draw->drawRect();
res = mysql_store_result(mysql);
lines = sqlp->process_result_set(mysql, res, 10, totallen, 0);
len = *totallen;
mysql_free_result(res);
}
mysql = classConn::connection();
sql = "select * from tableProductRecords where productid = '"+strprdid+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res))!= nullptr)
{
draw->gotoxy(x1, 5);
for(int y = x1; y < len -4; y++)
{
cout << " ";
}
draw->gotoxy(x1, 5);
cout << row[0];
prdin2 = row[0];
draw->gotoxy(x1, 6);
for(int y = x1; y < len -4; y++)
{
cout << " ";
}
draw->gotoxy(x1, 6);
cout << std::stoi(row[1]);
draw->gotoxy(x1, 7);
for(int y = x1; y < len -4; y++)
{
cout << " ";
}
draw->gotoxy(x1, 7);
cout << std::stoi(row[2]);
draw->gotoxy(x1, 8);
for(int y = x1; y < len -4; y++)
{
cout << " ";
}
draw->gotoxy(x1, 8);
prdid = std::stol(row[3]);
cout << prdid;
draw->gotoxy(x1 + prdin.length(), 5);
}
mysql_free_result(res);
}
else
{
draw->gotoxy(1, 11);
cout << "Error in 2nd sql : " << mysql_error(mysql);
getc->getch();
}
if(iter == prdids.end())
{
iter = prdids.begin();
}
draw->gotoxy(2, 9);
cout << "Search string : " << prdin;
draw->gotoxy(x1, 5);
// getc->getch();
}
}
prdin = prdin2;
}
else
{
prdin += ch;
}
}while(intch != 10);
return prdid;
}
void classProduct::productEnter()
{
int x1 = 18;
draw->clrscr();
draw->drawRect();
mysql = classConn::connection();
mysql->reconnect = true;
get->printMenu(productRecords, "Product Enter");
draw->gotoxy(x1, 5);
productname.clear();
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x1, 5);
for(i = 30; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 5);
getline(cin, productname);
if(!productname.compare("q"))
{
return;
}
}while(productname.empty());
do
{
draw->gotoxy(x1, 6);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 6);
getline(cin, strstock);
if(!strstock.compare("q"))
{
return;
}
stock = classValid::intValidity(strstock);
if(stock == 0)
{
draw->gotoxy(x1, 6);
cout << "invalid input";
getc->getch();
}
}while(stock == 0);
do
{
draw->gotoxy(x1, 7);
for(i = x1; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x1, 7);
getline(cin, strrate);
if(!strrate.compare("q"))
{
return;
}
rate = classValid::intValidity(strrate);
if(rate == 0)
{
draw->gotoxy(x1, 7);
cout << "invalid rate";
getc->getch();
}
}while(rate == 0);
sql = "select max(productid) from tableProductRecords;";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res))!= nullptr)
{
draw->gotoxy(x1, 8);
if(row[0] == nullptr)
{
maxpid = 0;
}
else
{
maxpid = std::stoi(row[0]);
}
strmaxpid = std::to_string(++maxpid);
}
mysql_free_result(res);
}
else
{
draw->gotoxy(2, 15);
cout << "Error in select max : " << mysql_error(mysql);
getc->getch();
}
draw->gotoxy(x1, 8);
cout << strmaxpid;
draw->gotoxy(15, 10);
cout << "Really want to save data (y / n): ";
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(50, 10);
for(i = 50; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(50, 10);
getline(cin, stryesno);
}while(stryesno.compare("Y") && stryesno.compare("y") &&
stryesno.compare("N") && stryesno.compare("n"));
if(!stryesno.compare("y") || !stryesno.compare("y"))
{
sql ="insert into tableProductRecords values('"+productname+"', '"+strstock+"','"+strrate+"','"+strmaxpid+"');";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(15, 11);
cout << "Data inserted successfully";
getc->getch();
}
else
{
draw->gotoxy(2,11);
cout << "Error in insert into tableProductRecords in product enter : " << mysql_error(mysql);
getc->getch();
return;
}
}
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(15, 12);
for(i = 42; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(15, 12);
cout << "Do you want to insert more records : ";
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(50, 12);
for(i = 46; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(50, 12);
getline(cin, stryesno);
}while(stryesno.compare("Y") && stryesno.compare("y") &&
stryesno.compare("N") && stryesno.compare("n"));
if(!stryesno.compare("y") || !stryesno.compare("y"))
{
productEnter();
}
else
{
return;
}
}
classProduct* classProduct::productSearch(int prdno)
{
mysql = classConn::connection();
string strprdno = std::to_string(prdno);
sql = "select *from tableProductRecords where productid = '"+strprdno+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
productname = row[0];
stock = std::stoi(row[1]);
rate = std::stoi(row[2]);
productid = std::stoi(row[3]);
}
mysql_free_result(res);
}
return this;
}
void classProduct::productModify()
{
long l = displayProduct("PRODUCT MODIFY");
string strchoice;
string strprdno = std::to_string(l);
if(l == 0)
{
return;
}
int choice;
sql = "select * from tableProductRecords where productid = '"+strprdno+"';";
mysql = classConn::connection();
mysql->reconnect = true;
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->clrscr(1, 12, w.ws_col -1, w.ws_row -1);
res = mysql_store_result(mysql);
draw->gotoxy(1, 10);
sqlp->process_result_set(mysql, res, 10, totallen, 0);
do
{
draw->gotoxy(2, 15);
cout << "Which data you want to modify (1 .. 4) d for delete, q for return : ";
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(i = 70; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(70, 15);
getline(cin, strchoice);
choice = classValid::intValidity(strchoice);
if(choice != 1 && choice != 2 && choice != 3 && choice != 4 && strchoice.compare("q") && strchoice.compare("d"))
{
draw->gotoxy(70, 15);
cout << "invalid input";
getc->getch();
}
}while(choice != 1 && choice != 2 && choice != 3 && choice != 4 && strchoice.compare("q") && strchoice.compare("d"));
if(!strchoice.compare("q"))
{
return;
}
if(!strchoice.compare("d"))
{
mysql = classConn::connection();
sql = "delete from tableProductRecords where productid = '"+strprdno+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(2, 16);
cout << "Product data deleted of productid : " << strprdno;
getc->getch();
return;
}
}
int x = 24;
draw->gotoxy(x, 5 + --choice);
switch(choice)
{
case 0:
{
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 5 + choice);
for(i = 23; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 5 + choice);
getline(cin, productname);
if(!productname.compare("q"))
{
return;
}
}while(productname.empty());
sql = "update tableProductRecords set productname = '"+productname+"' where productid = '"+strprdno+"';";
}
break;
case 1:
{
do
{
draw->gotoxy(x, 5 +choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 5 + choice);
for(i = 23; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 5 + choice);
getline(cin, strstock);
if(!strstock.compare("q"))
{
return;
}
stock = classValid::intValidity(strstock);
if(stock == 0)
{
strstock.clear();
}
}while(strstock.empty());
sql = "update tableProductRecords set stock = '"+strstock+"' where productid = '"+strprdno+"';";
}
break;
case 2:
{
do
{
draw->gotoxy(x, 5 +choice);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 5 + choice);
for(i = 23; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 5 + choice);
getline(cin, strrate);
if(!strrate.compare("q"))
{
return;
}
rate = classValid::intValidity(strrate);
if(rate == 0)
{
strrate.clear();
}
}while(strrate.empty());
sql = "update tableProductRecords set rate = '"+strrate+"' where productid = '"+strprdno+"';";
}
break;
case 3:
{
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(x, 5 + choice);
for(i = x; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(x, 5 + choice);
getline(cin, strproductid);
if(!strproductid.compare("q"))
{
return;
}
productid = classValid::intValidity(strproductid);
if(productid == 0)
{
strproductid.clear();
}
}while(strproductid.empty());
sql = "select productname from tableProductRecords where productid = '"+strprdno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] != nullptr)
{
draw->gotoxy(x, 5 + choice);
cout << "ID already exist, please re-enter id";
getc->getch();
}
else
{
sql = "update tableProductRecords set productid = '"+strproductid+"' where productid = '"+strprdno+"';";
strprdno = strproductid;
}
}
}
}
break;
default :
draw->gotoxy(1, 16);
cout << "please enter valid option";
sql.clear();
}
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
draw->gotoxy(5, 18);
cout << "Data update successfully and modified data is : ";
sql = "select * from tableProductRecords where productid = '"+strprdno+"';";
mysql = classConn::connection();
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->clrscr(1, 21, w.ws_col - -1, w.ws_row - 1);
draw->drawRect();
res = mysql_store_result(mysql);
draw->gotoxy(1, 21);
sqlp->process_result_set(mysql, res, 21, totallen, 0);
mysql_free_result(res);
getc->getch();
}
}
else
{
draw->gotoxy(5, 19);
cout << "Error : " << mysql_error(mysql);
getc->getch();
}
}
else
{
draw->gotoxy(5, 15);
cout << "Error in else of product modify : " << mysql_error(mysql);
getc->getch();
}
draw->gotoxy(5, 19);
// do
// {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(5, 19);
for(i = 5; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(5, 19);
cout << "Want to modify more : ";
//cin.get(yesno);
do
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
draw->gotoxy(27, 19);
for(i = 27; i < w.ws_col - 1; i++)
{
cout << " ";
}
draw->gotoxy(27, 19);
getline(cin, stryesno);
}while(stryesno.compare("Y") && stryesno.compare("y") &&
stryesno.compare("N") && stryesno.compare("n"));
if(!stryesno.compare("y") || !stryesno.compare("y"))
{
l = displayProduct();;
productModify();
}
else
{
return;
}
// }while();
}
/**********************************************
*
* file : classSqlProcess.h
*
***********************************************/
#ifndef CLASSSQLPROCESS_H
#define CLASSSQLPROCESS_H
#include <string>
#include <iostream>
#include <cstring>
#include <mysql.h>
#include "classDraw.h"
#include "classGetchar.h"
#include "classGetchoice.h"
class classSqlProcess
{
public:
classSqlProcess();
virtual ~classSqlProcess();
void printDashes(MYSQL_RES *res, int offset);
int process_result_set(MYSQL * mysql, MYSQL_RES *res, int offset, int *totallen2, int big);
protected:
private:
classDraw *draw;
classGetchar *getc;
classGetchoice *get;
bool flag;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *field;
int *totallen;
int k;
int nums;
int i;
};
#endif // CLASSSQLPROCESS_H
/**********************************************
*
* file : classSqlProcess.cpp
*
***********************************************/
#include "classSqlProcess.h"
using std::cout;
classSqlProcess::classSqlProcess()
{
//ctor
flag = true;
field = new MYSQL_FIELD;
totallen = new int;
draw = new classDraw;
get = new classGetchoice;
getc = new classGetchar;
}
classSqlProcess::~classSqlProcess()
{
//dtor
delete field;
delete totallen;
delete draw;
delete get;
delete getc;
}
void classSqlProcess::printDashes(MYSQL_RES *res, int offset)
{
unsigned int i, j, k;
mysql_field_seek(res, 0);
cout << "+";
int numf = mysql_num_fields(res);
for(int i = 0; i < numf; i++)
{
field = mysql_fetch_field(res);
for(k = offset; k < field->max_length + offset; k++)
{
cout << "-";
}
cout << "+";
}
}
int classSqlProcess::process_result_set(MYSQL *mysql, MYSQL_RES *res, int offset, int *totallen, int big)
{
unsigned int columnLen;
unsigned int i;
int offset1 = 1;
int totallen2 = 0;
int tlen = 0;
int times = 0;
int j = 0;
int l = 0;
int intlines = 0;
int intl = 0;
int k = offset;
mysql_field_seek(res, 0);
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
nums = mysql_num_fields(res);
for(int i = 0; i < nums; i++)
{
field = mysql_fetch_field(res);
columnLen = strlen(field->name);
if(columnLen < field->max_length)
{
columnLen = field->max_length;
}
if(columnLen < 4 && !(IS_NOT_NULL(field->flags)))
{
columnLen = 4;
}
field->max_length = columnLen;
totallen2 = tlen += columnLen;
}
k = offset;
draw->gotoxy(1, k);
printDashes(res, 0);
draw->gotoxy(1, ++k);
cout << "|";
mysql_field_seek(res, 0);
for(i = 0; i < nums; i++)
{
field = mysql_fetch_field(res);
cout << std::setw(field->max_length) << field->name << "|";
}
draw->gotoxy(1, ++k);
printDashes(res, 0);
while((row = mysql_fetch_row(res)) != nullptr && k < w.ws_row - 1)
{
draw->gotoxy(l, ++k);
mysql_field_seek(res, 0);
cout << "|";
for(i = 0; i < nums; i++)
{
field = mysql_fetch_field(res);
if(row[i] == nullptr)
{
cout << std::setw(field->max_length) << "NULL" << "|";
}
else
{
cout << std::setw(field->max_length) << row[i] << "|";
}
}
if(k == w.ws_row - 1)
{
times++;
k = 2;
if(big > totallen2)
{
l = (totallen2 + nums + 4) * times + (big - totallen2);
}
else
{
l = (totallen2 + nums + 4) * times;
}
draw->gotoxy(l, k);
printDashes(res, l);
}
}
if(row == nullptr)
{
draw->gotoxy(l, ++k);
printDashes(res, l);
}
if(l == 0)
{
if(big > totallen2)
{
*totallen = totallen2 + nums + 4 + (big - totallen2);
}
else
{
*totallen = totallen2 + nums + 4;
}
}
else
{
if(big > totallen2)
{
*totallen = l + 4 + (big -totallen2);
}
else
{
*totallen = l + 4;
}
}
int l2 = mysql_num_rows(res);
return l2;
}
/**********************************************
*
* file : classValid.h
*
***********************************************/
#ifndef CLASSVALID_H
#define CLASSVALID_H
#include <string>
class classValid
{
public:
classValid();
virtual ~classValid();
static bool dateValidity(std::string date);
static bool nameValidity(std::string strname);
static void splitDate(std::string date, int &y, int &m, int &d);
static bool isValidDate(std::string date);
static bool isLeap(int year);
static unsigned long intValidity(std::string strnum);
protected:
private:
};
#endif // CLASSVALID_H
/**********************************************
*
* file : classValid.cpp
*
***********************************************/
#include "classValid.h"
classValid::classValid()
{
//ctor
}
classValid::~classValid()
{
//dtor
}
unsigned long classValid::intValidity(std::string lnum)
{
unsigned long num = 0;
int i = 0;
char ch_num = 0;
std::string::iterator iter;
if(lnum.length() == 0)
{
return 0;
}
for(iter = lnum.begin(); iter != lnum.end(); iter++)
{
ch_num = *iter;
i = ch_num - 48;
if(i >=0 && i <= 9)
{
num = num * 10 + i;
}
else
{
return 0;
}
}
return num;
}
bool classValid::nameValidity(std::string strname)
{
std::string::iterator iter;
char ch;
for(iter = strname.begin(); iter != strname.end(); iter++)
{
ch = char(*iter);
if((ch <'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && ch != ' ' )
{
return false;
}
}
return true;
}
void classValid::splitDate(std::string date, int &y, int &m, int &d)
{
std::string yy = date.substr(0, 4);
std::string mm = date.substr(5, 2);
std::string dd = date.substr(8, 2);
y = std::stoi(yy);
m = std::stoi(mm);
d = std::stoi(dd);
}
bool classValid::isValidDate(std::string date)
{
int y, m, d;
if( (date.compare(4, 1, "/", 1)!= 0 && date.compare(4, 1, "-", 1)!= 0) ||
(date.compare(7, 1, "/", 1)!=0 && date.compare(7, 1, "-", 1) != 0) || date.length() != 10)
{
return false;
}
splitDate(date, y, m, d);
if( y < 1900 || y > 2100)
{
return false;
}
if(d == 0 || m ==0 || y ==0)
{
return false;
}
if(m == 2)
{
if(d == 30 || d == 31 || (d == 29 && isLeap(y)))
{
return false;
}
}
else if(m == 4 || m == 6 || m == 9 || m ==11)
{
if(d == 31)
{
return false;
}
}
return true;
}
bool classValid::isLeap(int y)
{
if((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
{
return true;
}
return false;
}
/**********************************************
*
* file : ReadMe.txt
*
***********************************************/
you need to change in file classConn.cpp
static char *opt_host = "serverora.db.net";// your server name if you are in localhost change it to // "localhost".
static char *opt_user_name = "rahul"; // your user name
static char *opt_password = "rahul"; // password
static unsigned int opt_port = 3306; // port number in which mysql communicates from server to client
static char *opt_socket_name = NULL; // default is "TCP/IP"
static char * opt_databse_name = "cbsdb"; // database name
static unsigned int opt_flags = 0; // not neccessary
most of the time while you are inputting data, if you want to go back, press q or enter key or esc key
for latest entry press l.
option 1:
---------
press s for arrange table by stock asc.
press r by rate
press i by id
press p by product name
press enter for go back to main menu.
option 2:
---------
press digit 1 - 9 it will immediately show table containing one of that digit. then enter digit for further table of productid.
to return press enter.
option 3:
---------
input : product name, stock, rate. prodcut id will be generated automatically.
press enter.
press y for yes, n for no. other than y and n nothing will be accepted.
if you press yes data will be inserted.
want more press y / n
option 4:
---------
press double tab, to get complete list of products. then iterate by pressing tab key(key code = 9).
there is problem of segmention fault. if you press continusly after some time there is sqgmentation fault.
i could not solve it.
for exxample, if you want to filter input of some chars of product. as enter "corsa" press tab, this will show table, product name containing "corsa...". press two times enter to go back.
option 5 :
---------
enter name, address, mobileno. press enter -> customer record inserted. then input some text and press tab or press tab twice. select product by iterating, press enter.
if you want to go back press q or enter quantity and enter. data will be inserted.
press enter, want to enter more if you choose y, you will get back to enter product name else return main menu. Product will be added to customer table if you choosen y and entered product name.
let if you entered custoemr name and exited without adding product name. then there will be
an extra entry in tableCustomers and no link in tableBilling. this is fault for billno unique constraint in tableCustomers. so we need to delete it in tableCustomers. if we enter next customer name then this entry will be deleted automatically, but we can do it also by option 12 in main menu.
option 6:
---------
you may need to see table in full screen mode.
press enter to see currect date ( todays sale):
there is problem of displaying grand total. Please solve it by yourself. I could not found it's solution.
enter date to see sale of that day. here two tables, one for customer and second for product. we need big that is totallen of first table and added difference of totallen2 from second table adn totalen of first tabe. You may remove column dateofsale from first table. you need to change in sql statement.
press enter to get out of here.
option 7:
---------
press double tab to see products, to iterate press tab.
press enter to select product. press enter to see table. follow guidline to change data.
option 8:
---------
enter some chars of name and press enter. press tab to iterate. press enter to select for comand. press 1 - 5 and change. or q for return.
option 9:
---------
enter some chars for searching customer. And see details.
option 10:
----------
enter l for latest entry or bill no to search. press key for command. as a : produces product entry menu. select product and press enter twice. input quantity or press q for return. this will be added to product purchased. then it will ask to add or delete more if y : command option will be asked. choose whatyou need.
option 11:
----------
enter bill no and see detail.
option 12:
----------
delete latest invalid entry that was done in option 5.
option 13:
----------
exit.
No comments:
Post a Comment