Commit 9138fa0f authored by Mal's avatar Mal

Invoice module bug fixes and email formatting.

Added a generic AddPurchase method to the Purchase module required
for adding surcharges. Stock->AvailableProducts now returns data
in two formats.
parent 734b2f64
......@@ -54,6 +54,9 @@ class Invoice extends Base {
if ($surcharge == 0) {
$surcharge = $payment->Factory(array("Surcharge", $purchases));
if ($surcharge != 0) {
if ($query !== "") {
$query .= ", ";
}
$query .= '("'.$user.'", '.time().', "surcharge", 1, '.
$surcharge.', "'.$this->user->name.'")';
}
......@@ -61,9 +64,9 @@ class Invoice extends Base {
}
}
if ($query !== "") {
if (!$mysqli->query('INSERT INTO purchase VALUES '.$query)) {
$this->Log('Invoice->Callback 1: '.$mysqli->error);
}
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$purchase->Factory(array("AddPurchase", $query));
}
$object = $this->Data();
}
......@@ -75,7 +78,6 @@ class Invoice extends Base {
$sold = (float)$mysqli->escape_string($us_data["sold"]);
$balance = (float)$mysqli->escape_string($us_data["balance"]);
$credit = $mysqli->escape_string($us_data["credit"]);
error_log("Sending invoice for ".$user);
// Get the users first name if they have entered it.
$name = "";
......@@ -93,59 +95,82 @@ class Invoice extends Base {
if ($name === "") {
$name = $user;
}
$message = '<p>Dear '.$name.',</p>'.
'<p>Thank you very much for being part of the Turnstyle co-op!<p>';
$message = '<html><head><title>Invoice for this week</title></head>'."\n".
'<body>'."\n".
'<p>Dear '.$name.',</p>'."\n".
'<p>Thank you very much for being part of the Turnstyle co-op!<p>'."\n";
if ($balance > 0) {
if ($credit == 1) {
$message .= '<p>Your balance is currently $'.$balance.
' and you have chosen to keep this as credit in the system.</p>';
$message .= '<p>Your balance is currently <b>$'.
number_format($balance, 2, ".", "").'</b> and you have chosen '.
'to keep this as credit in the system.</p>'."\n";
}
else {
$message .= '<p>Your balance is currently $'.$balance.
' please expect a payment from our finance team shortly.</p>';
$message .= '<p>Your balance is currently <b>$'.
number_format($balance, 2, ".", "").'</b>, '.
'please expect a payment from our finance team shortly.</p>'."\n";
}
}
else if ($balance < 0) {
$message .= '<p>Your balance is currently $'.$balance.' please make '.
'a payment to:<br>Bank: MECU<br>Account Name: Bulk Buyers<br>'.
'Account number: 12031260<br>BSB: 313140</p>';
$banking = new Module($this->user, $this->owner, "banking",
$this->config);
$all_settings = $banking->Factory("AllSettings");
$message .= '<p>Your currently owe <b>$'.
number_format($balance * -1, 2, ".", "").'</b>, '.
'please make a payment to:<br>Bank: MECU<br>Account Name: '.
'Bulk Buyers<br>Account number: 12031260<br>BSB: 313140<br>'."\n".
'Using your reference: <b>'.$all_settings[$user]["reference"].'</b>'.
'</p>'."\n";
}
$message .= '<p>Please contact turnstyle.finance@gmail.com if you have '.
'any questions about your account.</p>';
'any questions about your account.<br>You can also log in at '.
$_SERVER["HTTP_HOST"].' to view your transactions.</p>'."\n";
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$stock = new Module($this->user, $this->owner, "stock", $this->config);
$products = $stock->Factory(array("AvailableProducts", false));
$start = strtotime("-6 days");
$end = time();
$cell_style = 'style="background-color: #f0f0f0"';
if ($purchases != 0) {
$message .= '<p>Your purchases for this week:</p>';
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$data = $purchase->Data($start, $end, $user);
$message .= '<table><tr><td>Name</td><td>Quantity</td><td>Price</td>'.
'<td>Total</td></tr>';
$message .= '<p>Your purchases for this week:</p>'."\n";
$data = $purchase->Factory(array("Data", $start, $end, $user));
$message .= '<table border="0" cellspacing="5"><tr><td '.$cell_style.
'>Name</td><td '.$cell_style.'>Quantity</td><td '.$cell_style.
'>Price</td><td '.$cell_style.'>Total</td></tr>'."\n";
for ($i = 0; $i < count($data); $i++) {
$message .= '<tr><td>'.$data[$i]["name"].'</td><td>'.
$data[$i]["quantity"].'</td><td>'.$data[$i]["price"].'</td><td>'.
$data[$i]["total"].'</td></tr>';
$name = $data[$i]["name"];
$message .= '<tr><td>'.$name.'</td><td>'.
$data[$i]["quantity"].'</td><td>$'.$data[$i]["price"].'/'.
$products[$name]["unit"].'</td><td>$'.
$data[$i]["total"].'</td></tr>'."\n";
}
$message .= '<tr><td></td><td></td><td>Subtotal</td><td>'.$purchases.
'</td></tr>'.
'<tr><td></td><td></td><td>Surcharge</td><td>'.$surcharge.
'</td></tr>'.
'<tr><td></td><td></td><td>Total</td><td>'.$purchases + $surcharge.
'</td></tr></table>';
$message .= '<tr><td></td><td></td><td>Subtotal</td><td>$'.
number_format($purchases, 2, ".", "").'</td></tr>'."\n".
'<tr><td></td><td></td><td>Surcharge</td><td>$'.
number_format($surcharge, 2, ".", "").'</td></tr>'."\n".
'<tr><td></td><td></td><td><b>Total</b></td><td><b>$'.
number_format($purchases + $surcharge, 2, ".", "").
'</b></td></tr></table>'."\n";
}
if ($sold != 0) {
$message .= '<p>Your sold items for this week:</p>';
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$data = $purchase->Sold($start, $end, $user);
$message .= '<table><tr><td>Name</td><td>Quantity</td><td>Price</td>'.
'<td>Total</td></tr>';
$message .= '<p>Your sold items for this week:</p>'."\n";
$data = $purchase->Factory(array("Sold", $start, $end, $user));
$message .= '<table border="0" cellspacing="5"><tr><td '.$cell_style.
'>Name</td><td '.$cell_style.'>Quantity</td><td '.$cell_style.
'>Price</td><td '.$cell_style.'>Total</td></tr>'."\n";
for ($i = 0; $i < count($data); $i++) {
$message .= '<tr><td>'.$data[$i]["name"].'</td><td>'.
$data[$i]["quantity"].'</td><td>'.$data[$i]["price"].'</td><td>'.
$data[$i]["total"].'</td></tr>';
$name = $data[$i]["name"];
$message .= '<tr><td>'.$name.'</td><td>'.
$data[$i]["quantity"].'</td><td>$'.$data[$i]["price"].'/'.
$products[$name]["unit"].'</td><td>$'.
$data[$i]["total"].'</td></tr>'."\n";
}
$message .= '</table>';
$message .= '</table>'."\n";
}
$message .= '</body></html>';
$email = "";
$query = 'SELECT email FROM users WHERE user="'.$user.'"';
if ($result = $mysqli->query($query)) {
......@@ -159,8 +184,10 @@ class Invoice extends Base {
}
if ($email !== "") {
$subject = "Invoice for this week";
$headers = "From: noreply@".$_SERVER["HTTP_HOST"].
"\r\nCc: mblaney+cc@gmail.com";
$headers = "MIME-Version: 1.0\r\n".
"Content-type: text/html; charset=iso-8859-1\r\n".
"From: noreply@".$_SERVER["HTTP_HOST"]."\r\n".
"Bcc: turnstyle.finance@gmail.com\r\n";
if (!mail($email, $subject, $message, $headers)) {
$object["error"] = "Email to ".$user." (".$email.
") not accepted for delivery.";
......
......@@ -276,6 +276,10 @@ class Purchase extends Base {
$timestamp = $fn[2];
return $this->AllOutstanding($payment_totals, $timestamp);
}
if ($name == "AddPurchase" && count($fn) == 2) {
$query = $fn[1];
return $this->AddPurchase($query);
}
return;
}
if ($fn == "Total") return $this->Total();
......@@ -387,7 +391,7 @@ class Purchase extends Base {
$mysqli = connect_db();
$query = 'SELECT timestamp, name, quantity, price FROM purchase WHERE '.
'timestamp >='.$start.' AND timestamp <='.$end.' AND user="'.$user.
'" ORDER BY timestamp, name';
'" AND name != "surcharge" ORDER BY timestamp, name';
if ($result = $mysqli->query($query)) {
while ($purchase = $result->fetch_assoc()) {
$quantity = (float)$purchase["quantity"];
......@@ -709,6 +713,15 @@ class Purchase extends Base {
return $outstanding;
}
private function AddPurchase($query) {
if ($query === "") return;
$mysqli = connect_db();
if (!$mysqli->query('INSERT INTO purchase VALUES '.$query)) {
$this->Log('Purchase->AddPurchase: '.$mysqli->error);
}
$mysqli->close();
}
private function NewUser() {
$mysqli = connect_db();
$query = 'INSERT INTO purchase_totals VALUES ("'.$this->user->name.'", 0)';
......
......@@ -160,6 +160,10 @@ class Stock extends Base {
$user = $fn[1];
return $this->Supplier($user);
}
if ($name == "AvailableProducts" && count($fn) == 2) {
$array = $fn[1];
return $this->AvailableProducts($array);
}
return;
}
if ($fn == "AvailableProducts") return $this->AvailableProducts();
......@@ -259,18 +263,28 @@ class Stock extends Base {
// Private functions below here ////////////////////////////////////////////
private function AvailableProducts() {
private function AvailableProducts($array = true) {
$object = array();
$mysqli = connect_db();
$query = 'SELECT name, user, unit, price, category FROM stock '.
'WHERE available="1" ORDER BY name';
if ($result = $mysqli->query($query)) {
while ($stock = $result->fetch_assoc()) {
$object[] = array("name" => $stock["name"],
"user" => $stock["user"],
"unit" => $stock["unit"],
"price" => (float)$stock["price"],
"category" => $stock["category"]);
// The grid module requires this data as an indexed array, but it's
// also useful to have an associative array keyed on product names.
if ($array) {
$object[] = array("name" => $stock["name"],
"user" => $stock["user"],
"unit" => $stock["unit"],
"price" => (float)$stock["price"],
"category" => $stock["category"]);
}
else {
$object[$stock["name"]] = array("user" => $stock["user"],
"unit" => $stock["unit"],
"price" => (float)$stock["price"],
"category" => $stock["category"]);
}
}
$result->close();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment