      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     Prints a customer with a given customer number.
     13     @param connection the database connection
     14     @param custnum the customer number
     15  */
     16  void print_customer(MYSQL* connection, string custnum)
     17  {
     18     string query = "SELECT Name, Address, City, State, Zip "
     19        "FROM Customer WHERE Customer_Number = '" + custnum + "'";
     20     if (mysql_query(connection, query.c_str()) != 0)
     21     {
     22        cout << "Error: " << mysql_error(connection) << "\n";
     23        return;
     24     }
     25     MYSQL_RES* result = mysql_store_result(connection);
     26     if (result == NULL)
     27     {
     28        cout << "Error: " << mysql_error(connection) << "\n";
     29        return;
     30     }
     31     int rows = mysql_num_rows(result);
     32     if (rows == 0)
     33     {
     34        cout << "Customer not found.\n";
     35        return;
     36     }
     37
     38     MYSQL_ROW row = mysql_fetch_row(result);
     39     string name = row[0];
     40     string street = row[1];
     41     string city = row[2];
     42     string state = row[3];
     43     string zip = row[4];
     44     mysql_free_result(result);
     45
     46     cout << name << "\n" << street << "\n"
     47        << city << ", " << state << " " << zip << "\n";
     48  }
     49
     50  /**
     51     Prints all items of an invoice
     52     @param connection the database connection
     53     @param invnum the invoice number
     54  */
     55  void print_items(MYSQL* connection, string invnum)
     56  {
     57     string query = "SELECT Item.Quantity, Product.Description, "
     58        "Product.Unit_Price FROM Item, Product WHERE Item.Invoice_Number = '"
     59        + invnum + "'AND Item.Product_Code = Product.Product_Code";
     60     if (mysql_query(connection, query.c_str()) != 0)
     61     {
     62        cout << "Error: " << mysql_error(connection) << "\n";
     63        return;
     64     }
     65     MYSQL_RES* result = mysql_store_result(connection);
     66     if (result == NULL)
     67     {
     68        cout << "Error: " << mysql_error(connection) << "\n";
     69        return;
     70     }
     71     int rows = mysql_num_rows(result);
     72
     73     const int COLUMN_WIDTH = 30;
     74
     75     cout <<
     76        "\n\nDescription                   Price  Qty  Total\n";
     77
     78     for (int r = 1; r <= rows; r++)
     79     {
     80        MYSQL_ROW row = mysql_fetch_row(result);
     81        int quantity = string_to_int(row[0]);
     82        string description = row[1];
     83        double price = string_to_double(row[2]);
     84
     85        cout << description;
     86
     87        // Pad with spaces to fill column
     88
     89        int pad = COLUMN_WIDTH - description.length();
     90        for (int i = 1; i <= pad; i++)
     91           cout << " ";
     92
     93        cout << price
     94           << "   " << quantity
     95           << "   " << price * quantity << "\n";
     96     }
     97
     98     mysql_free_result(result);
     99  }
    100
    101  /**
    102     Gets the amount due on all items of an invoice.
    103     @param connection the database connection
    104     @param invnum the invoice number
    105  */
    106  double get_amount_due(MYSQL* connection, string invnum)
    107  {
    108     string query = "SELECT SUM(Item.Quantity * Product.Unit_Price) "
    109        "FROM Item, Product WHERE Item.Invoice_Number = '"
    110        + invnum + "' AND Item.Product_Code = Product.Product_Code";
    111     if (mysql_query(connection, query.c_str()) != 0)
    112     {
    113        cout << "Error: " << mysql_error(connection) << "\n";
    114        return 0;
    115     }
    116     MYSQL_RES* result = mysql_store_result(connection);
    117     if (result == NULL)
    118     {
    119        cout << "Error: " << mysql_error(connection) << "\n";
    120        return 0;
    121     }
    122     int rows = mysql_num_rows(result);
    123     if (rows == 0) return 0;
    124     MYSQL_ROW row = mysql_fetch_row(result);
    125     double amount_due = string_to_double(row[0]);
    126     mysql_free_result(result);
    127     return amount_due;
    128  }
    129
    130  /**
    131     Prints an invoice.
    132     @param connection the database connection
    133     @param invnum the invoice number
    134  */
    135  void print_invoice(MYSQL* connection, string invnum)
    136  {
    137     string query = "SELECT Customer_Number, Payment FROM Invoice "
    138        "WHERE Invoice_Number = '" + invnum + "'";
    139     if (mysql_query(connection, query.c_str()) != 0)
    140     {
    141        cout << "Error: " << mysql_error(connection) << "\n";
    142        return;
    143     }
    144     MYSQL_RES* result = mysql_store_result(connection);
    145     if (result == NULL)
    146     {
    147        cout << "Error: " << mysql_error(connection) << "\n";
    148        return;
    149     }
    150     int rows = mysql_num_rows(result);
    151     if (rows == 0)
    152     {
    153        cout << "Invoice not found.\n";
    154        return;
    155     }
    156
    157     MYSQL_ROW row = mysql_fetch_row(result);
    158     string custnum = row[0];
    159     double payment = string_to_double(row[1]);
    160     mysql_free_result(result);
    161
    162     cout << "                     I N V O I C E\n\n";
    163
    164     print_customer(connection, custnum);
    165     print_items(connection, invnum);
    166
    167     double amount_due = get_amount_due(connection, invnum);
    168
    169     cout << "\nAMOUNT DUE: $" << amount_due - payment << "\n";
    170  }
    171
    172  int main()
    173  {
    174     MYSQL* connection = mysql_init(NULL);
    175
    176     if(mysql_real_connect(connection, NULL, NULL, NULL,
    177           "bigcpp", 0, NULL, 0) == NULL)
    178     {
    179        cout << "Error: " << mysql_error(connection) << "\n";
    180        return 1;
    181     }
    182
    183     cout << "Enter invoice number: ";
    184     string invnum;
    185
    186     getline(cin, invnum);
    187     print_invoice(connection, invnum);
    188
    189     mysql_close(connection);
    190     return 0;
    191  }