Commit 57c08a5a authored by Malcolm Blaney's avatar Malcolm Blaney

Added a purchase-group select to the Manager module to partition

search results, ended up refactoring most of it. Required a few
supporting changes to Purchase->Search and Stock->AllProducts.
parent b128bffb
Pipeline #5455286 passed with stage
in 1 minute and 34 seconds
This diff is collapsed.
......@@ -1647,8 +1647,8 @@ class Purchase extends Base {
}
public function Search($user, $timestamp, $product, $supplier,
$quantity, $price, $start, $end,
$group = false, $taxable = false) {
$quantity = '', $price = '', $start = 0, $end = 0,
$group = false, $taxable = false, $limit = false) {
$price_query = 'price';
$order_query = 'ORDER BY user, timestamp DESC';
// Allow the export to be sorted by product (same as the orders email).
......@@ -1666,12 +1666,20 @@ class Purchase extends Base {
$all_users = $detail->AllUsers();
}
$export_units = $this->Substitute('purchase-export-units') === 'true';
$stock = new Stock($this->user, $this->owner);
$all_taxable = $taxable ? $stock->AllTaxable() : [];
$export_units = false;
$all_products = [];
if ($this->Substitute('purchase-export-units') === 'true') {
$export_units = true;
// Limit is true when searching based on the current purchase group only.
if ($limit) {
if (isset($_SESSION['purchase-group'])) {
$all_products = $stock->AllProducts(false, $_SESSION['purchase-group']);
}
else {
$all_products = $stock->AllProducts(false, $this->user->group);
}
}
else if ($export_units) {
$all_products = $stock->AllProducts(false);
}
......@@ -1773,8 +1781,16 @@ class Purchase extends Base {
while ($purchase = $result->fetch_assoc()) {
$current_supplier = $purchase['supplier'];
$current_product = $purchase['name'];
// Filter out purchases that are not taxable from search results.
if ($taxable &&
!isset($all_taxable[$current_supplier][$current_product])) continue;
!isset($all_taxable[$current_supplier][$current_product])) {
continue;
}
// Filter out purchases that are not part of the current purchase group.
if ($limit &&
!isset($all_products[$current_supplier][$current_product])) {
continue;
}
$current_user = $purchase['user'];
$current_price = (float)$purchase['price'];
......@@ -2082,7 +2098,7 @@ class Purchase extends Base {
$product_query = 'AND name = "'.$product.'" ';
}
$group_query = '';
if ($organisation) {
if (!$organisation) {
$group_query = 'AND users.system_group = "'.$this->user->group.'" ';
}
if ($start === 0 && $end === 0) {
......
......@@ -595,21 +595,34 @@ class Stock extends Base {
// Public functions that aren't part of interface here /////////////////////
public function AllProducts($array = true) {
public function AllProducts($array = true, $purchase_group = '') {
$object = [];
$organiser = new Organiser($this->user, $this->owner);
$invite = new Invite($this->user, $this->owner);
$groups = $invite->AllBuyingGroups();
$buying_group_query = '';
for ($i = 0; $i < count($groups); $i++) {
$buying_group_query .= ' OR system_group = "'.$groups[$i].'"';
// Some modules want all products in all groups, others want all products
// in a limited set of groups.
$group_query = '';
if ($purchase_group === '') {
$group_query .= $organiser->GroupQuery();
for ($i = 0; $i < count($groups); $i++) {
$group_query .= ' OR system_group = "'.$groups[$i].'"';
}
}
else if ($purchase_group === $this->user->group) {
$group_query .= $organiser->GroupQuery();
}
else if (in_array($purchase_group, $invite->AllBuyingGroups())) {
$group_query .= 'system_group = "'.$purchase_group.'"';
}
else {
// This shouldn't happen so just set to the user's own group.
$group_query .= 'system_group = "'.$this->user->group.'"';
}
$mysqli = connect_db();
$query = 'SELECT name, stock.user, unit, base_price, wholesale_price, '.
'retail_price, category, order_available, purchase_available, taxable, '.
'composite FROM stock LEFT JOIN users ON stock.user = users.user WHERE '.
'category != "hidden" AND ('.
$organiser->GroupQuery().$buying_group_query.') ORDER BY name, user';
'category != "hidden" AND ('.$group_query.') ORDER BY name, user';
if ($result = $mysqli->query($query)) {
while ($stock = $result->fetch_assoc()) {
$available = (int)$stock['order_available'] === 1 ||
......@@ -1466,7 +1479,7 @@ class Stock extends Base {
else {
// Can only remove stock items if no purchases have been made.
$purchase = new Purchase($this->user, $this->owner);
$search = $purchase->Search('', 0, $name, $user, '', '', 0, 0);
$search = $purchase->Search('', 0, $name, $user);
if (count($search) === 0) {
$query = 'INSERT INTO stock_history (name, user, unit, pack_size, '.
'base_price, wholesale_price, retail_price, category, grower, '.
......@@ -2159,7 +2172,7 @@ class Stock extends Base {
if (($total > 0.001 || $total < -0.001) &&
$adjustment < 0.001 && $adjustment > -0.001) {
$purchase = new Purchase($this->user, $this->owner);
$search = $purchase->Search('', 0, $name, $user, '', '', 0, 0);
$search = $purchase->Search('', 0, $name, $user);
$object['remove'] = count($search) === 0;
}
}
......
......@@ -26,7 +26,7 @@
if(!this.dobrado.manager){dobrado.manager={};}
(function(){'use strict';var manager=null;var purchase=[];var managerGrid=null;var managerGridId="";var allProductsGrid=null;var allProductsGridId="";var allProductsData=[];var currentProduct=null;var currentDate=null;var oneDay=86400000;var confirmRemoveMultiple=false;var currentRemove=0;var composite=false;var importData=[];var currentImport=0;var saving=false;$(function(){if($(".manager").length===0){return;}
$("#manager-details-form").dialog({autoOpen:false,modal:true,position:{my:"top",at:"top+50",of:window},title:"Edit User Details",width:400,create:dobrado.fixedDialog});$("#manager-details-form .submit").button().click(editDetails);$(".manager-view-all-dialog").dialog({autoOpen:false,modal:true,position:{my:"top",at:"top+50",of:window},title:"Available Products",width:830,height:520,create:dobrado.fixedDialog,close:function(){if(allProductsGrid){allProductsGrid.gotoCell(0,0);}}});$(".manager .view-all").click(viewAll);$("#manager-form .default-action").click(search);$("#manager-form .edit").button({disabled:true}).click(openDialog);$("#manager-form .submit").button().click(submit);$("#manager-form .search").button().click(search);$("#manager-form .back").button().click(showPreviousImport);$("#manager-form .remove").button().click(remove);$("#manager-form .import-toggle").click(toggleImport);$("#manager-import-file").change(loadImportData);$("#manager-username-input").val("").change(function(){setTimeout(function(){showUser();},10);});$("#manager-username-input").keypress(checkUsernameEnter);$("#manager-product-input").val("").change(function(){setTimeout(function(){showProductFromMenu();},10);});$("#manager-supplier-input").val("").change(function(){setTimeout(function(){showSupplierFromMenu();},10);});$("#manager-price-input").val("");$("#manager-date-input").val("").change(clearSearchDates).datepicker({dateFormat:dobrado.dateFormat});$("#manager-start-date-input").val("").change(clearDate).datepicker({dateFormat:dobrado.dateFormat});$("#manager-end-date-input").val("").change(clearDate).datepicker({dateFormat:dobrado.dateFormat});$("#manager-quantity-input").val("");$("#manager-quantity-input").spinner({min:0,spin:setQuantity,change:setQuantity});$(".manager .toggle-search-options").click(function(){clearSearchDates();clearDate();$(".manager .search-options").toggle();});if($(".grid").length!==0){gridSetup();}
$("#manager-details-form").dialog({autoOpen:false,modal:true,position:{my:"top",at:"top+50",of:window},title:"Edit User Details",width:400,create:dobrado.fixedDialog});$("#manager-details-form .submit").button().click(editDetails);$("#manager-group-select").change(changeGroup);$(".manager-view-all-dialog").dialog({autoOpen:false,modal:true,position:{my:"top",at:"top+50",of:window},title:"Available Products",width:830,height:520,create:dobrado.fixedDialog,close:function(){if(allProductsGrid){allProductsGrid.gotoCell(0,0);}}});$(".manager .view-all").click(viewAll);$("#manager-form .default-action").click(search);$("#manager-form .edit").button({disabled:true}).click(openDialog);$("#manager-form .submit").button().click(submit);$("#manager-form .search").button().click(search);$("#manager-form .back").button().click(showPreviousImport);$("#manager-form .remove").button().click(remove);$("#manager-form .import-toggle").click(toggleImport);$("#manager-import-file").change(loadImportData);$("#manager-username-input").val("").change(function(){setTimeout(function(){showUser();},10);});$("#manager-username-input").keypress(checkUsernameEnter);$("#manager-product-input").val("").change(function(){setTimeout(function(){showProductFromMenu();},10);});$("#manager-supplier-input").val("").change(function(){setTimeout(function(){showSupplierFromMenu();},10);});$("#manager-price-input").val("");$("#manager-date-input").val("").change(clearSearchDates).datepicker({dateFormat:dobrado.dateFormat});$("#manager-start-date-input").val("").change(clearDate).datepicker({dateFormat:dobrado.dateFormat});$("#manager-end-date-input").val("").change(clearDate).datepicker({dateFormat:dobrado.dateFormat});$("#manager-quantity-input").val("");$("#manager-quantity-input").spinner({min:0,spin:setQuantity,change:setQuantity});$(".manager .toggle-search-options").click(function(){clearSearchDates();clearDate();$(".manager .search-options").toggle();});if($(".grid").length!==0){gridSetup();}
dobrado.log("Loading users...","info");$.post("/php/request.php",{request:"manager",action:"list",url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"manager list")){return;}
manager=JSON.parse(response);if(manager.date){$("#manager-date-input").datepicker("setDate",dobrado.formatDate(manager.date));}
$("#manager-username-input").autocomplete({source:manager.users,select:showUser});updateProducts();});});function gridSetup(){$(".grid").each(function(index){if(index===0){managerGridId="#"+$(this).attr("id");}
......@@ -41,6 +41,8 @@ else{return sign* -1;}});allProductsGrid.invalidate();});}
$('.grid').hide();}
function checkUsernameEnter(event){if(event.keyCode!==13){return;}
event.preventDefault();showUser();}
function changeGroup(){dobrado.log("Changing group.","info");$.post("/php/request.php",{request:"manager",action:"changeGroup",group:$("#manager-group-select").val(),url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"manager changeGroup")){return;}
location.reload();});}
function openDialog(){var user=$("#manager-username-input").val();if(user===""){return false;}
$("#manager-details-form").dialog("open");return false;}
function editDetails(){var user=$("#manager-username-input").val();if(user===""){return false;}
......@@ -165,7 +167,7 @@ if(item.quantity>0){if(item.unit==="variable"){item.quantity=1;}
if(composite){dobrado.log("Updating "+product+".","info");}
else{dobrado.log("Saving purchase.","info");}
$.post("/php/request.php",{request:"manager",username:user,timestamp:item.date,product:item.name,supplier:item.supplier,quantity:item.quantity,price:item.price,basePrice:item.basePrice,composite:product,action:"savePurchase",url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"manager updatePurchase")){return;}
var data=JSON.parse(response);purchase.push({date:item.date,user:user,name:item.name,supplier:item.supplier,quantity:item.quantity,price:item.price.toFixed(2),total:item.total});$.each(purchase,function(i,item){if(!data.done&&item.user===user&&item.name==="surcharge"&&item.date===data.date){purchase[i]=data;}});if(managerGrid){managerGrid.setData(purchase);managerGrid.updateRowCount();managerGrid.render();$(managerGridId).show();}});}
var data=JSON.parse(response);purchase.push({date:item.date,user:user,name:item.name,supplier:item.supplier,quantity:item.quantity,price:item.price,total:item.total});$.each(purchase,function(i,item){if(!data.done&&item.user===user&&item.name==="surcharge"&&item.date===data.date){purchase[i]=data;}});if(managerGrid){managerGrid.setData(purchase);managerGrid.updateRowCount();managerGrid.render();$(managerGridId).show();}});}
else{if(composite){dobrado.log("Updating "+product+".","info");}
else{dobrado.log("Removing purchase.","info");}
$.post("/php/request.php",{request:"manager",username:user,timestamp:item.date,product:item.name,supplier:item.supplier,quantity:item.quantity,composite:product,action:"removePurchase",url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"manager updatePurchase")){return;}});}}
......
......@@ -76,6 +76,7 @@ if (!this.dobrado.manager) {
create: dobrado.fixedDialog
});
$("#manager-details-form .submit").button().click(editDetails);
$("#manager-group-select").change(changeGroup);
$(".manager-view-all-dialog").dialog({
autoOpen: false,
......@@ -282,6 +283,21 @@ if (!this.dobrado.manager) {
showUser();
}
function changeGroup() {
dobrado.log("Changing group.", "info");
$.post("/php/request.php", { request: "manager",
action: "changeGroup",
group: $("#manager-group-select").val(),
url: location.href,
token: dobrado.token },
function(response) {
if (dobrado.checkResponseError(response, "manager changeGroup")) {
return;
}
location.reload();
});
}
function openDialog() {
var user = $("#manager-username-input").val();
if (user === "") {
......@@ -1153,7 +1169,7 @@ if (!this.dobrado.manager) {
var data = JSON.parse(response);
purchase.push({ date: item.date, user: user, name: item.name,
supplier: item.supplier, quantity: item.quantity,
price: item.price.toFixed(2), total: item.total });
price: item.price, total: item.total });
// Update the surcharge value in the current purchase list.
$.each(purchase, function(i, item) {
if (!data.done && item.user === user &&
......
......@@ -304,6 +304,7 @@ class Control extends Base {
'"",".control .username","float","right"',
'"",".control .username","font-family","trebuchet ms"',
'"",".control .username","padding","6px"',
'"'.$media.'",".control .username","display","none"',
'"",".control .info","position","absolute"',
'"",".control .info","width","18em"',
'"",".control .info","padding","8px"',
......
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