Commit b682317e authored by Malcolm Blaney's avatar Malcolm Blaney

New Cart module editing feature: cart items can now have their

positions updated by dragging the item to the new position.
Stock->AllProducts method has a new parameter to show hidden
products. A few other small bug fixes too.
parent febe6383
Pipeline #29961325 passed with stage
in 1 minute and 7 seconds
......@@ -25,6 +25,10 @@ class Cart extends Base {
$us_action = isset($_POST['action']) ? $_POST['action'] : '';
if ($us_action === 'item') return $this->DisplayItem();
if ($us_action === 'checkout') return $this->Checkout();
if ($us_action === 'updatePosition') {
if ($this->user->canEditPage) return $this->UpdatePosition();
return ['error' => 'You don\'t have permission to move cart items.'];
}
if ($us_action === 'list') {
if ($this->user->canEditPage) return $this->ShowSettings();
return ['error' => 'You don\'t have permission to list cart settings.'];
......@@ -1860,21 +1864,14 @@ class Cart extends Base {
private function Tracking($price_level = 'retail') {
$tracking_list = [];
$tracking_removed = '';
$stock = new Module($this->user, $this->owner, 'stock');
$available = $stock->Factory('AvailableProducts');
$mysqli = connect_db();
$user = $mysqli->escape_string($this->Substitute('cart-user'));
$organiser = new Organiser($this->user, $this->owner);
if (!$organiser->MatchUser($user)) {
$mysqli->close();
return $tracking_list;
}
// The cart_tracking table is not updated when syncing with the stock
// module is turned off, so remove here when an item is not found in the.
// module is turned off, so remove items here when they're not found in the
// list of available products.
$stock = new Module($this->user, $this->owner, 'stock');
$available = $stock->Factory('AvailableProducts');
$query = 'SELECT name, supplier, product, modified FROM cart_tracking ' .
'WHERE user = "' . $user . '"';
'WHERE user = "' . $this->owner . '"';
if ($result = $mysqli->query($query)) {
while ($cart_tracking = $result->fetch_assoc()) {
$name = $cart_tracking['name'];
......@@ -1902,8 +1899,8 @@ class Cart extends Base {
$this->Log('Cart->Tracking 1: ' . $mysqli->error);
}
if ($tracking_removed !== '') {
$query = 'DELETE FROM cart_tracking WHERE user = "' . $user . '" AND ' .
'(' . $tracking_removed . ')';
$query = 'DELETE FROM cart_tracking WHERE user = "' . $this->owner . '"' .
' AND (' . $tracking_removed . ')';
if (!$mysqli->query($query)) {
$this->Log('Cart->Tracking 2: ' . $mysqli->error);
}
......@@ -1917,6 +1914,9 @@ class Cart extends Base {
$name = '';
$modified = false;
$mysqli = connect_db();
// cart-user is required because this function is called from the stock
// module, which could have a different page owner. It should be set to
// the owner of the page where the cart module is used for this group.
$user = $mysqli->escape_string($this->Substitute('cart-user'));
if ($user === '') {
$mysqli->close();
......@@ -2003,6 +2003,41 @@ class Cart extends Base {
return ['done' => true];
}
private function UpdatePosition() {
$mysqli = connect_db();
$name = $mysqli->escape_string($_POST['name']);
$old_position = (int)$_POST['oldPosition'];
$new_position = (int)$_POST['newPosition'];
if ($new_position < $old_position) {
// The item has been moved up, move the displaced items down.
$query = 'UPDATE cart_item_page SET item_order = item_order + 1 WHERE ' .
'user = "' . $this->owner . '" AND page = "' . $this->user->page . '"' .
' AND item_order >= ' . $new_position .
' AND item_order < ' . $old_position;
if (!$mysqli->query($query)) {
$this->Log('Cart->UpdatePosition 1: ' . $mysqli->error);
}
}
else if ($new_position > $old_position) {
// The item has been moved down, move the displaced items up.
$query = 'UPDATE cart_item_page SET item_order = item_order - 1 WHERE ' .
'user = "' . $this->owner . '" AND page = "' . $this->user->page . '"' .
' AND item_order > ' . $old_position .
' AND item_order <= ' . $new_position;
if (!$mysqli->query($query)) {
$this->Log('Cart->UpdatePosition 2: ' . $mysqli->error);
}
}
$query = 'UPDATE cart_item_page SET item_order = ' . $new_position .
' WHERE user = "' . $this->owner . '" AND name = "' . $name . '" AND ' .
'page = "' . $this->user->page . '"';
if (!$mysqli->query($query)) {
$this->Log('Cart->UpdatePosition 3: ' . $mysqli->error);
}
$mysqli->close();
return ['done' => true];
}
private function VerifyItem($item, $price, $stock_price) {
$verified = false;
$weight = 0;
......
......@@ -877,7 +877,7 @@ class Report extends Base {
if ($group !== $organiser->Parent()) {
$this->user->group = $group;
}
$result = ['products' => $stock->AllProducts()];
$result = ['products' => $stock->AllProducts(true, '', false)];
$this->user->group = $default_group;
return $result;
}
......
......@@ -890,17 +890,16 @@ class Stock extends Base {
// Public functions that aren't part of interface here /////////////////////
public function AllProducts($array = true, $purchase_group = '') {
public function AllProducts($array = true,
$purchase_group = '', $show_hidden = true) {
$object = [];
$organiser = new Organiser($this->user, $this->owner);
$invite = new Invite($this->user, $this->owner);
$hidden_query = $show_hidden === true ? '' : 'hidden = 0 AND ';
// Some modules want all products in all groups, others want all products
// in a limited set of groups. The Manager module always sets purchase_group
// and should also always list purchases of hidden stock items.
// in a limited set of groups.
$group_query = '';
$hidden_query = '';
if ($purchase_group === '') {
$hidden_query = 'hidden = 0 AND ';
$group_query .= $organiser->GroupQuery();
foreach ($invite->AllBuyingGroups() as $group) {
$group_query .= ' OR system_group = "' . $group . '"';
......@@ -3189,8 +3188,9 @@ class Stock extends Base {
$image = '';
$grower = '';
$description = '';
$retail_price = '';
$order_available = '0';
$retail_price = 0;
$order_available = 0;
$cart_setting = 0;
$category = '';
$old_name = '';
if ($new_name !== '') {
......@@ -3210,7 +3210,7 @@ class Stock extends Base {
$description = $mysqli->escape_string($stock['description']);
$retail_price = (float)$stock['retail_price'];
$order_available = (int)$stock['order_available'];
$cart = (int)$stock['cart'];
$cart_setting = (int)$stock['cart'];
}
$result->close();
}
......@@ -3219,7 +3219,7 @@ class Stock extends Base {
}
$mysqli->close();
if ($cart === 1) {
if ($cart_setting === 1) {
if ($unit === 'each') {
$cart = new Module($this->user, $this->owner, 'cart');
return $cart->Factory('UpdateItem',
......
This diff is collapsed.
This diff is collapsed.
......@@ -81,7 +81,7 @@ var price=purchase.buyerGroup[user];if(!viewPurchases){viewPurchases=true;$('#pu
if($.inArray(user,purchase.nextWeek)===-1){$('#purchase-next-week-input').prop('checked',false);}
else{$('#purchase-next-week-input').prop('checked',true);}
var total=0;if(purchase.total&&purchase.total[user]){total=purchase.total[user];}
var quantity=1;if(purchase.data[user]){$.each(purchase.data[user],function(i,item){if(!item.unit){$.each(purchase.products,function(index,product){if(item.name===product.name){item.unit=product.unit;if(product.quantityAdjustment&&product[price]!==item.price){item.basePrice=decimalString(product.price);item.price=decimalString(product[price]);item.server=false;if(orderMode){quantity=parseFloat(item.quantity)/product.quantityAdjustment;item.quantity=formatFloat(quantity);item.total=decimalString(quantity*product[price]);item.unit='each';}
var quantity=1;if(purchase.data[user]){$.each(purchase.data[user],function(i,item){if(!item.unit){$.each(purchase.products,function(index,product){if(item.name===product.name){item.unit=product.unit;if(product.quantityAdjustment&&decimalString(product[price])!==item.price){item.basePrice=decimalString(product.price);item.price=decimalString(product[price]);item.server=false;if(orderMode){quantity=parseFloat(item.quantity)/product.quantityAdjustment;item.quantity=formatFloat(quantity);item.total=decimalString(quantity*product[price]);item.unit='each';}
else{quantity=parseFloat(item.quantity)*product.quantityAdjustment;item.quantity=formatFloat(quantity);item.total=decimalString(quantity*product[price]);item.unit=product.unit;}}
return false;}});}
if(item.date<tomorrow){total+=parseFloat(item.total);}});if(purchaseGrid&&purchase.data[user].length!==0){$(purchaseGridId).show();purchaseGrid.setData(purchase.data[user]);purchaseGrid.updateRowCount();purchaseGrid.render();purchaseGrid.setSelectedRows([]);}}
......@@ -240,7 +240,7 @@ else{viewPurchases=true;$('#purchase-form .view-all').button('option','label',la
if(allUserGrid){allUserGrid.gotoCell(0,0);$(allUserGridId).hide();}
resetForm();if(purchase.data[user]){if(purchaseGrid&&purchase.data[user].length!==0){$(purchaseGridId).show();purchaseGrid.setData(purchase.data[user]);purchaseGrid.updateRowCount();purchaseGrid.render();purchaseGrid.setSelectedRows([]);}}}
return false;}
function viewAllUsers(){function setAllUsersQuantity(i,item){if(item.name===currentProduct.name){if(currentProduct.quantityAdjustment&&currentProduct[price]!==item.price){item.basePrice=decimalString(currentProduct.price);item.price=decimalString(currentProduct[price]);if(orderMode){quantity=parseFloat(item.quantity)/currentProduct.quantityAdjustment;item.quantity=formatFloat(quantity);item.total=decimalString(quantity*currentProduct[price]);item.unit='each';}
function viewAllUsers(){function setAllUsersQuantity(i,item){if(item.name===currentProduct.name){if(currentProduct.quantityAdjustment&&decimalString(currentProduct[price])!==item.price){item.basePrice=decimalString(currentProduct.price);item.price=decimalString(currentProduct[price]);if(orderMode){quantity=parseFloat(item.quantity)/currentProduct.quantityAdjustment;item.quantity=formatFloat(quantity);item.total=decimalString(quantity*currentProduct[price]);item.unit='each';}
else{quantity=parseFloat(item.quantity)*currentProduct.quantityAdjustment;item.quantity=formatFloat(quantity);item.total=decimalString(quantity*currentProduct[price]);item.unit=currentProduct.unit;}}
else{quantity=item.quantity;}
return false;}}
......
......@@ -623,7 +623,8 @@ if (!this.dobrado.purchase) {
$.each(purchase.products, function(index, product) {
if (item.name === product.name) {
item.unit = product.unit;
if (product.quantityAdjustment && product[price] !== item.price) {
if (product.quantityAdjustment &&
decimalString(product[price]) !== item.price) {
item.basePrice = decimalString(product.price);
item.price = decimalString(product[price]);
item.server = false;
......@@ -1642,7 +1643,7 @@ if (!this.dobrado.purchase) {
function setAllUsersQuantity(i, item) {
if (item.name === currentProduct.name) {
if (currentProduct.quantityAdjustment &&
currentProduct[price] !== item.price) {
decimalString(currentProduct[price]) !== item.price) {
item.basePrice = decimalString(currentProduct.price);
item.price = decimalString(currentProduct[price]);
if (orderMode) {
......
......@@ -490,5 +490,5 @@ function send_confirmed_user_email($user, $new_user, $email, $password) {
$headers .= 'Cc: ' . $cc . "\r\n";
}
dobrado_mail($email, $subject, $us_message, $headers, $bcc,
$sender, $user->name, 'account', 'newAccountEmail');
$sender, $new_user, 'account', 'newAccountEmail');
}
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