1  #include <iostream>
  2  #include <iomanip>
  3  
  4  #include <string>
  5  #include <mysql.h>
  6  
  7  using namespace std;
  8  
  9  #include "sutil.h"
 10  
 11  /**
 12     Finds a customer with a given customer number.
 13     @param connection the database connection
 14     @param custnum the customer number
 15     @return true if a customer with the given number exists
 16  */
 17  bool find_customer(MYSQL* connection, string custnum)
 18  {
 19     string query = "SELECT * FROM Customer WHERE Customer_Number = '"
 20        + custnum + "'";
 21     if (mysql_query(connection, query.c_str()) != 0)
 22     {
 23        cout << "Error: " << mysql_error(connection) << "\n";
 24        return false;
 25     }
 26     MYSQL_RES* result = mysql_store_result(connection);
 27     if (result == NULL)
 28     {
 29        cout << "Error: " << mysql_error(connection) << "\n";
 30        return false;
 31     }
 32     bool r = mysql_num_rows(result) > 0;
 33     mysql_free_result(result);
 34     return r;
 35  }
 36  
 37  /**
 38     Finds a product with a given product code.
 39     @param connection the database connection
 40     @param prodcode the product code
 41     @return true if a product with the given code exists
 42  */
 43  bool find_product(MYSQL* connection, string prodcode)
 44  {
 45     string query = "SELECT * FROM Product WHERE Product_Code = '"
 46        + prodcode + "'";
 47     if (mysql_query(connection, query.c_str()) != 0)
 48     {
 49        cout << "Error: " << mysql_error(connection) << "\n";
 50        return false;
 51     }
 52     MYSQL_RES* result = mysql_store_result(connection);
 53     if (result == NULL)
 54     {
 55        cout << "Error: " << mysql_error(connection) << "\n";
 56        return false;
 57     }
 58     bool r = mysql_num_rows(result) > 0;
 59     mysql_free_result(result);
 60     return r;
 61  }
 62  
 63  /**
 64     Adds an invoice to the database.
 65     @param connection the database connection
 66     @param custnum the customer number
 67     @param payment the payment amount
 68     @return the automatically assigned invoice number
 69  */
 70  string add_invoice(MYSQL* connection, string custnum, double payment)
 71  {
 72     string query = "SELECT MAX(Invoice_Number) FROM Invoice";
 73     if (mysql_query(connection, query.c_str()) != 0)
 74     {
 75        cout << "Error: " << mysql_error(connection) << "\n";
 76        return "";
 77     }
 78     MYSQL_RES* result = mysql_store_result(connection);
 79     if (result == NULL)
 80     {
 81        cout << "Error: " << mysql_error(connection) << "\n";
 82        return "";
 83     }
 84     int rows = mysql_num_rows(result);
 85     if (rows == 0) return "";
 86     MYSQL_ROW row = mysql_fetch_row(result);
 87     int max = string_to_int(row[0]);
 88     mysql_free_result(result);
 89  
 90  
 91     string invnum = int_to_string(max + 1);
 92     string command = "INSERT INTO Invoice VALUES ('" + invnum + "', '"
 93        + custnum + "', " + double_to_string(payment) + ")";
 94     if (mysql_query(connection, command.c_str()) != 0)
 95     {
 96        cout << "Error: " << mysql_error(connection) << "\n";
 97        return "";
 98     }
 99     return invnum;
100  }
101  
102  /**
103     Adds an item to the database.
104     @param connection the database connection
105     @param invnum the invoice number
106     @param prodcode the product code
107     @param quantity the quantity
108  */
109  void add_item(MYSQL* connection, string invnum, string prodcode,
110     int quantity)
111  {
112     string command = "INSERT INTO Item VALUES ('" + invnum + "', '"
113        + prodcode + "', " + int_to_string(quantity) + ")";
114     if (mysql_query(connection, command.c_str()) != 0)
115     {
116        cout << "Error: " << mysql_error(connection) << "\n";
117        return;
118     }
119  }
120  
121  int main()
122  {
123     MYSQL* connection = mysql_init(NULL);
124  
125     if(mysql_real_connect(connection, NULL, NULL, NULL,
126           "bigcpp", 0, NULL, 0) == NULL)
127     {
128        cout << "Error: " << mysql_error(connection) << "\n";
129        return 1;
130     }
131  
132     cout << "Enter customer number: ";
133     string custnum;
134     cin >> custnum;
135  
136     if (!find_customer(connection, custnum))
137     {
138        cout << "Customer not found.\n";
139        mysql_close(connection);
140        return 0;
141     }
142  
143     cout << "Enter payment: ";
144     double payment;
145     cin >> payment;
146  
147     string invnum = add_invoice(connection, custnum, payment);
148     if (invnum == "")
149     {
150        mysql_close(connection);
151        return 0;
152     }
153  
154     bool more = true;
155     while (more)
156     {
157        cout << "Enter product code, - when done: ";
158        string prodcode;
159        cin >> prodcode;
160        if (prodcode == "-") more = false;
161        else
162        {
163           if (find_product(connection, prodcode))
164           {
165              cout << "Enter quantity: ";
166              int quantity;
167              cin >> quantity;
168              add_item(connection, invnum, prodcode, quantity);
169           }
170           else cout << "Product not found.\n";
171        }
172     }
173     cout << "Added invoice " << invnum << "\n";
174     mysql_close(connection);
175     return 0;
176  }