Commit bb4e9e40 authored by Malcolm Blaney's avatar Malcolm Blaney

Grower information can now be shown instead of supplier on the purchase page

and in invoices. Stock module can override the supplier name when the available
only stock list is exported. Manager module can now import the orders
generated by the Invoice module.
parent f5f06769
......@@ -700,6 +700,7 @@ class Invoice extends Base {
$detail = new Detail($this->user, $this->owner);
$all_users = $detail->AllUsers();
$showGrower = $this->Substitute("purchase-show-grower") == "true";
$cell_style = 'style="background-color: #f0f0f0"';
$subject = $this->Substitute("invoice-order-subject");
$sender = $this->Substitute("invoice-sender", "/!host/",
......@@ -751,8 +752,10 @@ class Invoice extends Base {
$cell_style.'>Price</td></tr>'."\n";
for ($i = 0; $i < count($next_week); $i++) {
$name = $next_week[$i]["name"];
$message .= '<tr><td>'.$name.'</td><td>'.$next_week[$i]["supplier"].
'</td><td>'.$next_week[$i]["quantity"].'</td><td>$'.
$supplier = $showGrower ? $next_week[$i]["grower"] :
$next_week[$i]["supplier"];
$message .= '<tr><td>'.$name.'</td><td>'.$supplier.'</td><td>'.
$next_week[$i]["quantity"].'</td><td>$'.
number_format($next_week[$i]["price"], 2, ".", "").'/'.
$products[$name]["unit"].'</td></tr>'."\n";
}
......@@ -929,12 +932,15 @@ class Invoice extends Base {
'>Product</td><td '.$cell_style.'>Supplier</td><td '.$cell_style.
'>Quantity</td><td '.$cell_style.'>Price</td><td '.$cell_style.
'>Total</td></tr>'."\n";
$showGrower = $this->Substitute("purchase-show-grower") == "true";
$data = $purchase->Data($start, $end, $user);
for ($i = 0; $i < count($data); $i++) {
$name = $data[$i]["name"];
$message .= '<tr><td>'.$name.'</td><td>'.$data[$i]["supplier"].
'</td><td>'.$data[$i]["quantity"].'</td><td>$'.$data[$i]["price"].
'/'.$products[$name]["unit"].'</td><td>$'.$data[$i]["total"].
$supplier = $showGrower ? $data[$i]["grower"] : $data[$i]["supplier"];
$message .= '<tr><td>'.$name.'</td><td>'.$supplier.'</td><td>'.
$data[$i]["quantity"].'</td><td>$'.$data[$i]["price"].'/'.
$products[$name]["unit"].'</td><td>$'.$data[$i]["total"].
'</td></tr>'."\n";
}
if ($this->Substitute("surcharge") == "true") {
......@@ -954,7 +960,7 @@ class Invoice extends Base {
}
// Next display user info via substitutions for the group.
$message .= $this->FormatInvoiceAfterPurchase($user, $reference,
$invoice_number);
$invoice_number, $surcharge);
// Next pre order information is displayed, if in use.
if ($pre_order_available) {
if ($purchase_value == 0) {
......@@ -1024,20 +1030,20 @@ class Invoice extends Base {
}
private function FormatInvoiceAfterPurchase($user, $reference,
$invoice_number) {
$invoice_number, $surcharge) {
$roster = new Roster($this->user, $this->owner);
$volunteer = $roster->Description($user);
$text = "";
$surcharge = "";
$surcharge_description = "";
// The invoice number is passed in again here if there were purchases
// so that different substitutions can be used. The invoice number itself
// is not used again though.
if ($invoice_number != 0) {
$text = $this->Substitute("invoice-after-purchase");
// Only show the surcharge description when there are purchases.
if ($this->Substitute("surcharge") == "true") {
if ($surcharge != 0 && $this->Substitute("surcharge") == "true") {
$payment = new Payment($this->user, $this->owner);
$surcharge = $payment->SurchargeDescription();
$surcharge_description = $payment->SurchargeDescription();
}
}
$text .= $this->Substitute("invoice-user-info",
......@@ -1045,8 +1051,8 @@ class Invoice extends Base {
"/!server/"),
array($volunteer, $user, $reference,
$this->user->config->ServerName()));
if ($surcharge !== "") {
$text .= "<p>".$surcharge."<p>\n";
if ($surcharge_description !== "") {
$text .= "<p>".$surcharge_description."<p>\n";
}
return $text;
}
......
This diff is collapsed.
......@@ -214,7 +214,7 @@ class Stock extends Base {
'maxlength="100">'.
'</div>'.
'<div class="form-spacing">'.
'<label for="stock-growing-input">Growing Method:</label>'.
'<label for="stock-growing-input">Grower:</label>'.
'<input id="stock-growing-input" type="text" size="30" '.
'maxlength="200"><span id="stock-grower-info"></span>'.
'</div>'.
......@@ -439,17 +439,20 @@ class Stock extends Base {
$mysqli = connect_db();
$query = 'SELECT name, stock.user, unit, base_price, wholesale_price, '.
'retail_price, category, composite FROM stock LEFT JOIN users ON '.
'stock.user = users.user WHERE '.$organiser->GroupQuery().
' ORDER BY name, user';
'retail_price, category, order_available, purchase_available, composite '.
'FROM stock LEFT JOIN users ON stock.user = users.user WHERE '.
$organiser->GroupQuery().' ORDER BY name, user';
if ($result = $mysqli->query($query)) {
while ($stock = $result->fetch_assoc()) {
$available = $stock["order_available"] == "1" ||
$stock["purchase_available"] == "1";
$object[] = array("name" => $stock["name"],
"user" => $stock["user"],
"unit" => $stock["unit"],
"price" => (float)$stock["base_price"],
"wholesale" => (float)$stock["wholesale_price"],
"retail" => (float)$stock["retail_price"],
"available" => $available,
"composite" => (int)$stock["composite"]);
}
$result->close();
......@@ -590,6 +593,27 @@ class Stock extends Base {
$mysqli->close();
}
public function Grower($name, $user) {
// Return the supplier name if grower isn't set.
$grower = $user;
$mysqli = connect_db();
$query = 'SELECT grower FROM stock WHERE name = "'.$name.'" AND '.
'user = "'.$user.'"';
if ($result = $mysqli->query($query)) {
if ($stock = $result->fetch_assoc()) {
if ($stock["grower"] !== "") {
$grower = $stock["grower"];
}
}
$result->close();
}
else {
$this->Log("Stock->Grower: ".$mysqli->error);
}
$mysqli->close();
return $grower;
}
public function Increase($product_list) {
if ($this->Substitute("stock-track-quantity") != "true") return;
......@@ -1131,15 +1155,20 @@ class Stock extends Base {
$object = array();
$mysqli = connect_db();
$show_available = $mysqli->escape_string($_POST["showAvailable"]);
$available_query = $show_available == "true" ?
' AND (order_available = 1 OR purchase_available = 1)' : "";
$available_query = "";
$override_user = "";
if ($show_available == "true") {
$available_query = ' AND (order_available = 1 OR purchase_available = 1)';
$override_user = $this->Substitute("stock-supplier-override");
}
$query = 'SELECT name, stock.user, unit, pack_size, base_price, '.
'wholesale_price, retail_price, category, grower, order_available, '.
'purchase_available, taxable, quantity FROM stock LEFT JOIN users ON '.
'stock.user = users.user WHERE ('.$organiser->GroupQuery().') '.
'stock.user = users.user WHERE ('.$organiser->GroupQuery().')'.
$available_query.' ORDER BY name, user';
if ($result = $mysqli->query($query)) {
while ($stock = $result->fetch_assoc()) {
$user = $override_user !== "" ? $override_user : $stock["user"];
$base_price = (float)$stock["base_price"];
$base_price = number_format($base_price, 2, ".", "");
$wholesale_price = (float)$stock["wholesale_price"];
......@@ -1147,7 +1176,7 @@ class Stock extends Base {
$retail_price = (float)$stock["retail_price"];
$retail_price = number_format($retail_price, 2, ".", "");
$object[] = array("name" => $stock["name"],
"user" => $stock["user"],
"user" => $user,
"quantity" => (float)$stock["quantity"],
"unit" => $stock["unit"],
"size" => (float)$stock["pack_size"],
......
......@@ -159,15 +159,17 @@ function toggleImport(){$("#manager-form .import").toggle();if($("#manager-form
else{$(this).html("Hide import form.");}}
function loadImportData(){currentImport=0;importData=[];if(!window.FileReader){alert("Sorry your browser doesn't support reading files.");$("#manager-import-file").val("");return;}
var file=$("#manager-import-file").get(0).files[0];var reader=new FileReader();reader.onload=function(e){var data=e.target.result.match(/^(.*)[\r\n]*$/gm);if(!data){alert("Imported file format doesn't match");$("#manager-import-file").val("");return;}
if(importData.length===0){processExportData(data);}
if(importData.length===0){processExports(data);}
if(importData.length===0&&!processOrders(data)){$("#manager-import-file").val("");return;}
if(importData.length===0){alert("Couldn't process data from imported file.");$("#manager-import-file").val("");}
else{$(".import .info").html("<br>Purchases found in imported data file. "+"Processing <b>1/"+importData.length+"</b>:");$(".manager .remove").button("option","label","skip");$(".manager .back").show();showImportData();}};reader.readAsText(file);}
function showPreviousImport(){if(currentImport>1){currentImport-=2;showImportData();}
return false;}
function showImportData(){if(currentImport===importData.length){exitImport("Data processing complete.");return;}
var row=importData[currentImport++];$("#manager-username-input").val(row.user);$("#manager-product-input").val(row.name);$("#manager-supplier-input").val(row.supplier);$("#manager-quantity-input").val(row.quantity);$("#manager-price-input").val(row.price);$(".manager-price-info").html("");var dateFields=row.date.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/);if(!dateFields||dateFields.length!==4){$("#payment-date-input").val("");alert("Date format in imported purchase doesn't match");}
var productFound=false;var row=importData[currentImport++];$("#manager-username-input").val(row.user);$("#manager-product-input").val(row.name);$("#manager-supplier-input").val(row.supplier);$("#manager-quantity-input").val(row.quantity);$("#manager-price-input").val(row.price);$(".manager-price-info").html("");var dateFields=row.date.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/);if(!dateFields||dateFields.length!==4){$("#manager-date-input").val("");alert("Date format in imported purchase doesn't match");}
else{var day=parseInt(dateFields[3],10);var month=parseInt(dateFields[2],10);var year=parseInt(dateFields[1],10);$("#manager-date-input").val(dobrado.formatDate(year,month,day));}
$.each(manager.products,function(index,item){if(item.name===row.name&&item.user===row.supplier){currentProduct=item;if(item.unit==="variable"){$("#manager-price-input").val(row.price).attr("readonly",false);$("#manager-quantity-input").spinner("disable");}
$.each(manager.products,function(index,item){if(item.name===row.name&&(item.user===row.supplier||row.supplier===""&&item.available)){productFound=true;if(row.supplier===""){$("#manager-supplier-input").val(item.user);}
currentProduct=item;if(item.unit==="variable"){$("#manager-price-input").val(row.price).attr("readonly",false);$("#manager-quantity-input").spinner("disable");}
else{$("#manager-price-input").val("$"+row.total+" @ ($"+
row.price+"/"+item.unit+")");$("#manager-price-input").attr("readonly",true);var priceLevel=manager.buyerGroup[row.user];if(!priceLevel){priceLevel="retail";}
if(item[priceLevel].toFixed(2)!==row.price){$(".manager-price-info").html("Stock price now $"+
......@@ -175,7 +177,20 @@ item[priceLevel].toFixed(2)+"/"+
item.unit);}
$("#manager-quantity-input").spinner("enable");}
return false;}});$(".import .info").html("<br>Purchases found in imported data file. "+"Processing <b>"+currentImport+"/"+
importData.length+"</b>:");}
function processExportData(data){$.each(data,function(i,item){item=item.replace(/\".*?\"/g,function(match){match=match.replace(/,/g,"");return match.replace(/\"/g,"");});var row=item.match(/^([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,\r\n]*)[\r\n]*$/);if(row&&row.length===8){if(i===0){if(row[1]!=="date"||row[2]!=="user"||row[3]!=="name"||row[4]!=="supplier"||row[5]!=="quantity"||row[6]!=="price"||row[7]!=="total"){return false;}}
importData.length+"</b>:");if(!productFound){alert("Couldn't match supplier for: "+row.name+".\n"+"Please check stock availability.");}}
function processExports(data){$.each(data,function(i,item){item=item.replace(/\".*?\"/g,function(match){match=match.replace(/,/g,"");return match.replace(/\"/g,"");});var row=item.match(/^([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,\r\n]*)[\r\n]*$/);if(row&&row.length===8){if(i===0){if(row[1]!=="date"||row[2]!=="user"||row[3]!=="name"||row[4]!=="supplier"||row[5]!=="quantity"||row[6]!=="price"||row[7]!=="total"){return false;}}
else{importData.push({date:row[1],user:row[2],name:row[3],supplier:row[4],quantity:row[5],price:row[6],total:row[7]});}}});}
function exitImport(message){$(".import .info").html(message);$(".stock .remove").button("option","label","remove");$(".manager .back").hide();$("#manager-import-file").val("");importData=[];currentImport=0;}})();
\ No newline at end of file
function processOrders(data){var processed=true;var format="";var purchaseDate="";var purchaseUser="";var price=0;var quantity=0;var groups=[];$.each(data,function(i,item){item=item.replace(/\".*?\"/g,function(match){match=match.replace(/,/g,"");return match.replace(/\"/g,"");});var row=item.match(/^([^,]*),([^,]*),([^,]*),([^,]*),?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,([^,\r\n]*),*[\r\n]*$/);if(row&&row.length>=6){if(i===0){if(row[1]==="Product"&&row[2]==="Quantity"&&row[3]==="Pack Size"&&row[4]==="Packs"&&row[5]==="Price"){if($("#manager-username-input").val()===""){alert("Please select a username for this import.");processed=false;return false;}
if($("#manager-date-input").val()===""){alert("Please enter a purchase date for this import.");processed=false;return false;}
purchaseUser=$("#manager-username-input").val();purchaseDate=$.datepicker.formatDate("yy-mm-dd",$("#manager-date-input").datepicker("getDate"));format="single";return true;}
if(row[1]==="Product"&&row[2]==="Co-op"&&row[3]==="Quantity"&&row[4]==="Pack Size"&&row[5]==="Packs"&&row[6]==="Price"){if($("#manager-date-input").val()===""){alert("Please enter a purchase date for this import.");processed=false;return false;}
purchaseDate=$.datepicker.formatDate("yy-mm-dd",$("#manager-date-input").datepicker("getDate"));format="vertical";return true;}
if(row[1]==="Product"){for(var j=2;j<row.length-4;j++){if(row[j]==="Total"){break;}
groups.push(row[j]);}
if(row[groups.length+2]==="Total"&&row[groups.length+3]==="Pack Size"&&row[groups.length+4]==="Packs"&&row[groups.length+5]==="Price"){if($("#manager-date-input").val()===""){alert("Please enter a purchase date for this import.");processed=false;return false;}
purchaseDate=$.datepicker.formatDate("yy-mm-dd",$("#manager-date-input").datepicker("getDate"));format="horizontal";return true;}}
processed=false;return false;}
else if(format==="single"){price=row[5].match(/^\$([0-9.]+)/)[1];quantity=parseFloat(row[2]);if(quantity&&quantity>0){importData.push({date:purchaseDate,user:purchaseUser,name:row[1],supplier:"",quantity:quantity,price:price,total:(quantity*price).toFixed(2)});}}
else if(format==="vertical"){price=row[6].match(/^\$([0-9.]+)/)[1];quantity=parseFloat(row[3]);if(quantity&&quantity>0){importData.push({date:purchaseDate,user:row[2],name:row[1],supplier:"",quantity:quantity,price:price,total:(quantity*price).toFixed(2)});}}
else if(format==="horizontal"){price=row[groups.length+5].match(/^\$([0-9.]+)/)[1];$.each(groups,function(j,user){quantity=parseFloat(row[j+2]);if(quantity&&quantity>0){importData.push({date:purchaseDate,user:user.toLowerCase(),name:row[1],supplier:"",quantity:quantity,price:price,total:(quantity*price).toFixed(2)});}});}}});return processed;}
function exitImport(message){$(".import .info").html(message);$(".manager .remove").button("option","label","remove");$(".manager .back").hide();$("#manager-import-file").val("");importData=[];currentImport=0;}})();
\ No newline at end of file
......@@ -1144,7 +1144,11 @@ if (!this.dobrado.manager) {
}
// Go through each of the purchase import routines looking for a match.
if (importData.length === 0) {
processExportData(data);
processExports(data);
}
if (importData.length === 0 && !processOrders(data)) {
$("#manager-import-file").val("");
return;
}
if (importData.length === 0) {
alert("Couldn't process data from imported file.");
......@@ -1180,6 +1184,7 @@ if (!this.dobrado.manager) {
return;
}
var productFound = false;
var row = importData[currentImport++];
$("#manager-username-input").val(row.user);
$("#manager-product-input").val(row.name);
......@@ -1190,7 +1195,7 @@ if (!this.dobrado.manager) {
// Date is assumed to be YYYY-MM-DD in imported data.
var dateFields = row.date.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/);
if (!dateFields || dateFields.length !== 4) {
$("#payment-date-input").val("");
$("#manager-date-input").val("");
alert("Date format in imported purchase doesn't match");
}
else {
......@@ -1201,7 +1206,14 @@ if (!this.dobrado.manager) {
}
// Set currentProduct.
$.each(manager.products, function(index, item) {
if (item.name === row.name && item.user === row.supplier) {
// The supplier isn't set when importing orders, it relies on product
// availability requiring unique names so the supplier can be matched.
if (item.name === row.name && (item.user === row.supplier ||
row.supplier === "" && item.available)) {
productFound = true;
if (row.supplier === "") {
$("#manager-supplier-input").val(item.user);
}
currentProduct = item;
if (item.unit === "variable") {
$("#manager-price-input").val(row.price).attr("readonly", false);
......@@ -1233,9 +1245,13 @@ if (!this.dobrado.manager) {
$(".import .info").html("<br>Purchases found in imported data file. " +
"Processing <b>" + currentImport + "/" +
importData.length + "</b>:");
if (!productFound) {
alert("Couldn't match supplier for: " + row.name + ".\n" +
"Please check stock availability.");
}
}
function processExportData(data) {
function processExports(data) {
$.each(data, function(i, item) {
// Before checking the line format, need to check for any quotes, remove
// commas between the quotes, and then remove the quotes themselves.
......@@ -1262,9 +1278,132 @@ if (!this.dobrado.manager) {
});
}
function processOrders(data) {
var processed = true;
var format = "";
var purchaseDate = "";
var purchaseUser = "";
var price = 0;
var quantity = 0;
var groups = [];
$.each(data, function(i, item) {
// Before checking the line format, need to check for any quotes, remove
// commas between the quotes, and then remove the quotes themselves.
item = item.replace(/\".*?\"/g, function(match) {
match = match.replace(/,/g, "");
return match.replace(/\"/g, "");
});
// The row length is variable due to the orders format adding a column
// for each of the groups in "horizontal" mode. When only one group there
// are just 5 columns with one optional extra, "vertical" mode has one
// more column labelled "Co-op", allow for 20 groups in "horizontal" mode.
var row = item.match(/^([^,]*),([^,]*),([^,]*),([^,]*),?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,?([^,]*)?,([^,\r\n]*),*[\r\n]*$/);
if (row && row.length >= 6) {
if (i === 0) {
// The first row should be the header, make sure it matches one of
// the 3 formats.
if (row[1] === "Product" && row[2] === "Quantity" &&
row[3] === "Pack Size" && row[4] === "Packs" &&
row[5] === "Price") {
// This is the single group format, but the group name is not
// supplied. Check if a username is already given, or exit.
if ($("#manager-username-input").val() === "") {
alert("Please select a username for this import.");
processed = false;
return false;
}
// The orders format does not include dates, so the user needs to
// have it already selected.
if ($("#manager-date-input").val() === "") {
alert("Please enter a purchase date for this import.");
processed = false;
return false;
}
purchaseUser = $("#manager-username-input").val();
purchaseDate = $.datepicker.formatDate("yy-mm-dd",
$("#manager-date-input").datepicker("getDate"));
format = "single";
return true;
}
if (row[1] === "Product" && row[2] === "Co-op" &&
row[3] === "Quantity" && row[4] === "Pack Size" &&
row[5] === "Packs" && row[6] === "Price") {
if ($("#manager-date-input").val() === "") {
alert("Please enter a purchase date for this import.");
processed = false;
return false;
}
purchaseDate = $.datepicker.formatDate("yy-mm-dd",
$("#manager-date-input").datepicker("getDate"));
format = "vertical";
return true;
}
if (row[1] === "Product") {
// Assume this is "horizontal" format and store each of the groups,
// which should already exist as users. Once all groups have been
// found can check the remaining column headers.
for (var j = 2; j < row.length - 4; j++) {
if (row[j] === "Total") {
break;
}
groups.push(row[j]);
}
if (row[groups.length + 2] === "Total" &&
row[groups.length + 3] === "Pack Size" &&
row[groups.length + 4] === "Packs" &&
row[groups.length + 5] === "Price") {
if ($("#manager-date-input").val() === "") {
alert("Please enter a purchase date for this import.");
processed = false;
return false;
}
purchaseDate = $.datepicker.formatDate("yy-mm-dd",
$("#manager-date-input").datepicker("getDate"));
format = "horizontal";
return true;
}
}
processed = false;
return false;
}
else if (format === "single") {
price = row[5].match(/^\$([0-9.]+)/)[1];
quantity = parseFloat(row[2]);
if (quantity && quantity > 0) {
importData.push({ date: purchaseDate, user: purchaseUser,
name: row[1], supplier: "", quantity: quantity, price: price,
total: (quantity * price).toFixed(2) });
}
}
else if (format === "vertical") {
price = row[6].match(/^\$([0-9.]+)/)[1];
quantity = parseFloat(row[3]);
if (quantity && quantity > 0) {
importData.push({ date: purchaseDate, user: row[2],
name: row[1], supplier: "", quantity: quantity, price: price,
total: (quantity * price).toFixed(2) });
}
}
else if (format === "horizontal") {
price = row[groups.length + 5].match(/^\$([0-9.]+)/)[1];
$.each(groups, function(j, user) {
quantity = parseFloat(row[j + 2]);
if (quantity && quantity > 0) {
importData.push({ date: purchaseDate, user: user.toLowerCase(),
name: row[1], supplier: "", quantity: quantity, price: price,
total: (quantity * price).toFixed(2) });
}
});
}
}
});
return processed;
}
function exitImport(message) {
$(".import .info").html(message);
$(".stock .remove").button("option", "label", "remove");
$(".manager .remove").button("option", "label", "remove");
$(".manager .back").hide();
$("#manager-import-file").val("");
importData = [];
......
This diff is collapsed.
......@@ -69,6 +69,16 @@ if (!this.dobrado.purchase) {
return;
}
if (dobrado.localStorage()) {
if (localStorage.purchase) {
// Load the local purchase data without requesting it from the server,
purchase = JSON.parse(localStorage.purchase);
}
}
else {
$(".purchase .warning").show();
}
// Check if there is data that needs saving before leaving the page.
window.addEventListener("beforeunload", function(event) {
if (purchase && purchase.processed && purchase.processed.length !== 0) {
......@@ -159,12 +169,18 @@ if (!this.dobrado.purchase) {
allUserGridId = "#" + $(this).attr("id");
}
});
// If a grid module is on the page initialise columns.
// If a grid module is on the page initialise columns. Can configure grids
// to show either the supplier name or grower info in the supplier column.
var supplier = "supplier";
if (purchase && purchase.showGrower === true) {
supplier = "grower";
}
if ($(".grid").length !== 0) {
$(".grid").hide();
var purchaseColumns =
[{ id : "product", name: "Product", field: "name", width: 210 },
{ id : "supplier", name: "Supplier", field: "supplier", width: 140 },
{ id : "supplier", name: "Supplier", field: supplier, width: 140 },
{ id : "date", name: "Date", field: "date", width: 110,
formatter: Slick.Formatters.Timestamp },
{ id : "quantity", name: "Quantity", field: "quantity", width: 100 },
......@@ -196,7 +212,7 @@ if (!this.dobrado.purchase) {
var allAvailableColumns =
[{ id : "product", name: "Product", field: "name", width: 210,
sortable: true },
{ id : "supplier", name: "Supplier", field: "supplier", width: 140,
{ id : "supplier", name: "Supplier", field: supplier, width: 140,
sortable: true },
{ id : "date", name: "Date", field: "date", width: 110,
sortable: true, formatter: Slick.Formatters.Timestamp,
......@@ -234,7 +250,7 @@ if (!this.dobrado.purchase) {
var sign = args.sortAsc ? 1 : -1;
var value1 = row1[field];
var value2 = row2[field];
if (field !== "name" && field !== "supplier") {
if (field !== "name" && field !== supplier) {
value1 = parseFloat(value1);
value2 = parseFloat(value2);
}
......@@ -305,31 +321,17 @@ if (!this.dobrado.purchase) {
}
}
if (dobrado.localStorage()) {
if (localStorage.purchase) {
// Load the local purchase data without requesting it from the server,
purchase = JSON.parse(localStorage.purchase);
// First check if the local data has been saved. If it has then
// all data should be refreshed when the page is loaded. (If there's
// no data, might not have to reload and can go to sync instead).
if (purchase.processed && purchase.processed.length === 0 &&
purchaseDataStored()) {
loadProducts();
}
// Otherwise update the current time to stay in sync with the server,
// and check if data needs saving and/or refreshing.
else {
sync();
}
}
// Check if the local data has been saved. If it has then all data
// should be refreshed when the page is loaded. (If there's no data,
// might not have to reload and can go to sync instead).
if (!purchase || (purchase.processed && purchase.processed.length === 0 &&
purchaseDataStored())) {
loadProducts();
}
// Otherwise update the current time to stay in sync with the server,
// and check if data needs saving and/or refreshing.
else {
$(".purchase .warning").show();
}
if (!purchase) {
loadProducts();
sync();
}
});
......@@ -593,7 +595,7 @@ if (!this.dobrado.purchase) {
$("#purchase-price-input").attr("readonly", true);
$("#purchase-quantity-input").spinner("enable");
}
if (currentProduct.grower) {
if (!purchase.showGrower && currentProduct.grower) {
$("#purchase-grower-info").html("Grower info: " +
currentProduct.grower);
}
......@@ -635,7 +637,7 @@ if (!this.dobrado.purchase) {
$("#purchase-price-input").attr("readonly", true);
$("#purchase-quantity-input").val("").spinner("enable");
}
if (currentProduct.grower) {
if (!purchase.showGrower && currentProduct.grower) {
$("#purchase-grower-info").html("Grower info: " +
currentProduct.grower);
}
......@@ -923,12 +925,12 @@ if (!this.dobrado.purchase) {
return false;
}
update(user, product, supplier, price, currentProduct.price,
quantity, time, accumulative, true);
update(user, product, supplier, currentProduct.grower, price,
currentProduct.price, quantity, time, accumulative, true);
return false;
}
function update(user, product, supplier, price, basePrice,
function update(user, product, supplier, grower, price, basePrice,
quantity, time, accumulative, updateGrid) {
// If the user hasn't specified a different date, need to record a
// timestamp for the item that is relative to the server, so calculate
......@@ -973,7 +975,7 @@ if (!this.dobrado.purchase) {
quantity += item.quantity;
}
purchase.data[user][i] = { name: product, date: purchaseTime,
supplier: supplier,
supplier: supplier, grower: grower,
quantity: quantity,
price: price.toFixed(2),
basePrice: basePrice.toFixed(2),
......@@ -990,7 +992,7 @@ if (!this.dobrado.purchase) {
// Otherwise add the new purchase to the list.
if (newItem) {
purchase.data[user].push({ name: product, date: purchaseTime,
supplier: supplier,
supplier: supplier, grower: grower,
quantity: quantity,
price: price.toFixed(2),
basePrice: basePrice.toFixed(2),
......@@ -1230,6 +1232,7 @@ if (!this.dobrado.purchase) {
allUserData.push({ user: user, date: purchase.date, quantity: quantity,
unit: currentProduct.unit, name: product,
supplier: currentProduct.user,
grower: currentProduct.grower,
price: currentProduct[price].toFixed(2),
basePrice: currentProduct.price.toFixed(2),
total: total.toFixed(2) });
......@@ -1280,6 +1283,7 @@ if (!this.dobrado.purchase) {
total = currentItem.quantity * product[price];
allAvailableData.push({ date: currentItem.date, name: product.name,
supplier: product.user,
grower: product.grower,
quantity: currentItem.quantity,
unit: product.unit,
price: product[price].toFixed(2),
......@@ -1290,8 +1294,9 @@ if (!this.dobrado.purchase) {
}
else {
allAvailableData.push({ date: purchase.date, name: product.name,
supplier: product.user, quantity: 0,
unit: product.unit,
supplier: product.user,
grower: product.grower,
quantity: 0, unit: product.unit,
price: product[price].toFixed(2),
basePrice: product.price.toFixed(2),
total: "0.00" });
......@@ -1318,7 +1323,7 @@ if (!this.dobrado.purchase) {
$.each(purchase.products, function(index, item) {
if (item.name === data.name) {
currentProduct = item;
if (currentProduct.grower) {
if (!purchase.showGrower && currentProduct.grower) {
$("#purchase-all-available-grower-info").html("Grower info: " +
currentProduct.grower);
}
......@@ -1356,8 +1361,8 @@ if (!this.dobrado.purchase) {
alert("Please note!\n" +
"This product is usually purchased in whole quantities.");
}
update(user, item.name, item.supplier, price, basePrice,
item.quantity, item.date, false, true);
update(user, item.name, item.supplier, updateProduct.grower,
price, basePrice, item.quantity, item.date, false, true);
}
else {
removePurchase(user, item.name, item.date);
......@@ -1394,8 +1399,8 @@ if (!this.dobrado.purchase) {
alert("Please note!\n" +
"This product is usually purchased in whole quantities.");
}
update(item.user, item.name, item.supplier, price, basePrice,
item.quantity, item.date, false, false);
update(item.user, item.name, item.supplier, updateProduct.grower,
price, basePrice, item.quantity, item.date, false, false);
}
else {
removePurchase(item.user, item.name, item.date);
......
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