Skip to content

Commit

Permalink
Merge pull request #769 from o-psi/other-sorts
Browse files Browse the repository at this point in the history
Sorting Recurring and Quotes
  • Loading branch information
johnnyq authored Oct 16, 2023
2 parents cce1dc8 + 2d061c0 commit 9818c57
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 8 deletions.
43 changes: 43 additions & 0 deletions database_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,49 @@
}
//

// Be sure to change database_version.php to reflect the version you are updating to here
// Please add this same comment block to the bottom of this file, and update the version number.
// Uncomment Below Lines, to add additional database updates
//
if (CURRENT_DATABASE_VERSION == '0.8.9') {
// Insert queries here required to update to DB version 0.8.9
// Update existing quotes and recurrings so that item_order is set to item_id
$sql_quotes = mysqli_query($mysqli, "SELECT quote_id FROM quotes WHERE quote_id IS NOT NULL");
$sql_recurrings = mysqli_query($mysqli, "SELECT recurring_id FROM recurring WHERE recurring_id IS NOT NULL");

foreach ($sql_quotes as $row) {
$quote_id = $row['quote_id'];
$sql_quote_items = mysqli_query($mysqli, "SELECT item_id FROM quote_items WHERE item_quote_id = '$quote_id' ORDER BY item_id ASC");
$item_order = 1;
foreach ($sql_quote_items as $row) {
$item_id = $row['item_id'];
mysqli_query($mysqli, "UPDATE quote_items SET item_order = '$item_order' WHERE item_id = '$item_id'");
$item_order++;
//Log changes made to quote
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Modify', log_description = 'Updated item_order to item_id: $item_order'");
}
}

foreach ($sql_recurrings as $row) {
$recurring_id = $row['recurring_id'];
$sql_recurring_items = mysqli_query($mysqli, "SELECT item_id FROM recurring_items WHERE item_recurring_id = '$recurring_id' ORDER BY item_id ASC");
$item_order = 1;
foreach ($sql_recurring_items as $row) {
$item_id = $row['item_id'];
mysqli_query($mysqli, "UPDATE recurring_items SET item_order = '$item_order' WHERE item_id = '$item_id'");
$item_order++;
//Log changes made to recurring
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Recurring', log_action = 'Modify', log_description = 'Updated item_order to item_id: $item_order'");
}
}


//
// Then, update the database to the next sequential version
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.8.10'");
}
//

} else {
// Up-to-date
}
2 changes: 1 addition & 1 deletion database_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* It is used in conjunction with database_updates.php
*/

DEFINE("LATEST_DATABASE_VERSION", "0.8.9");
DEFINE("LATEST_DATABASE_VERSION", "0.8.10");
2 changes: 1 addition & 1 deletion guest_view_quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
</div>
</div>

<?php $sql_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_quote_id = $quote_id ORDER BY item_id ASC"); ?>
<?php $sql_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_quote_id = $quote_id ORDER BY item_order ASC"); ?>

<div class="row mb-4">
<div class="col-md-12">
Expand Down
42 changes: 40 additions & 2 deletions post/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
$qty = floatval($_POST['qty']);
$price = floatval($_POST['price']);
$tax_id = intval($_POST['tax_id']);
$item_order = intval($_POST['item_order']);

$subtotal = $price * $qty;

Expand All @@ -261,7 +262,7 @@

$total = $subtotal + $tax_amount;

mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$name', item_description = '$description', item_quantity = $qty, item_price = $price, item_subtotal = $subtotal, item_tax = $tax_amount, item_total = $total, item_tax_id = $tax_id, item_recurring_id = $recurring_id");
mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$name', item_description = '$description', item_quantity = $qty, item_price = $price, item_subtotal = $subtotal, item_tax = $tax_amount, item_total = $total, item_tax_id = $tax_id, item_order = $item_order, item_recurring_id = $recurring_id");

//Update Recurring Balances

Expand Down Expand Up @@ -1085,6 +1086,42 @@
}



if (isset($_POST['update_recurring_item_order'])) {

$item_id = intval($_POST['item_id']);
$item_recurring_id = intval($_POST['item_recurring_id']);

$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_id = $item_id");
$row = mysqli_fetch_array($sql);
$current_order = intval($row['item_order']);
$update_direction = sanitizeInput($_POST['update_recurring_item_order']);

switch ($update_direction)
{
case 'up':
$new_order = $current_order - 1;
break;
case 'down':
$new_order = $current_order + 1;
break;
}

//Find item_id of current item in $new_order
$other_sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_recurring_id = $item_recurring_id AND item_order = $new_order");
$other_row = mysqli_fetch_array($other_sql);
$other_item_id = intval($other_row['item_id']);
$other_row_str = strval($other_row['item_name']);

mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $new_order WHERE item_id = $item_id");

mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $current_order WHERE item_id = $other_item_id");

$_SESSION['alert_message'] = "recurring Item Order Updated";

header("Location: " . $_SERVER["HTTP_REFERER"]);
}

if (isset($_POST['update_invoice_item_order'])) {

$item_id = intval($_POST['item_id']);
Expand Down Expand Up @@ -1118,4 +1155,5 @@
$_SESSION['alert_message'] = "Invoice Item Order Updated";

header("Location: " . $_SERVER["HTTP_REFERER"]);
}
}

60 changes: 59 additions & 1 deletion post/quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
$qty = floatval($_POST['qty']);
$price = floatval($_POST['price']);
$tax_id = intval($_POST['tax_id']);
$item_order = intval($_POST['item_order']);

$subtotal = $price * $qty;

Expand All @@ -164,7 +165,7 @@

$total = $subtotal + $tax_amount;

mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$name', item_description = '$description', item_quantity = $qty, item_price = $price, item_subtotal = $subtotal, item_tax = $tax_amount, item_total = $total, item_tax_id = $tax_id, item_quote_id = $quote_id");
mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$name', item_description = '$description', item_quantity = $qty, item_price = $price, item_subtotal = $subtotal, item_tax = $tax_amount, item_total = $total, item_tax_id = $tax_id, item_order = $item_order, item_quote_id = $quote_id");

//Update Invoice Balances

Expand Down Expand Up @@ -433,3 +434,60 @@
exit;

}

if (isset($_POST['update_quote_item_order'])) {

if ($_POST['update_quote_item_order'] == 'up') {
$item_id = intval($_POST['item_id']);
$item_quote_id = intval($_POST['item_quote_id']);

$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_id = $item_id");
$row = mysqli_fetch_array($sql);
$item_order = intval($row['item_order']);

$new_item_order = $item_order - 1;

//Check if new item order is used
$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_quote_id = $item_quote_id AND item_order = $new_item_order");

//Redo the entire order of list
while ($row = mysqli_fetch_array($sql)) {
$item_id = intval($row['item_id']);
$item_order = intval($row['item_order']);

$new_item_order = $item_order + 1;

mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $new_item_order WHERE item_id = $item_id");
}



mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $item_order WHERE item_quote_id = $item_quote_id AND item_order = $new_item_order");
mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $new_item_order WHERE item_id = $item_id");

$_SESSION['alert_message'] = "Item moved up";

header("Location: " . $_SERVER["HTTP_REFERER"]);

}

if ($_POST['update_quote_item_order'] == 'down') {
$item_id = intval($_POST['item_id']);
$item_quote_id = intval($_POST['item_quote_id']);

$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_id = $item_id");
$row = mysqli_fetch_array($sql);
$item_order = intval($row['item_order']);

$new_item_order = $item_order + 1;

mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $item_order WHERE item_quote_id = $item_quote_id AND item_order = $new_item_order");
mysqli_query($mysqli,"UPDATE invoice_items SET item_order = $new_item_order WHERE item_id = $item_id");

$_SESSION['alert_message'] = "Item moved down";

header("Location: " . $_SERVER["HTTP_REFERER"]);

}

}
45 changes: 43 additions & 2 deletions quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
</div>
</div>

<?php $sql_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_quote_id = $quote_id ORDER BY item_id ASC"); ?>
<?php $sql_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_quote_id = $quote_id ORDER BY item_order ASC"); ?>

<div class="row mb-4">
<div class="col-md-12">
Expand Down Expand Up @@ -267,6 +267,7 @@
$item_id = intval($row['item_id']);
$item_name = nullable_htmlentities($row['item_name']);
$item_description = nullable_htmlentities($row['item_description']);
$item_order = intval($row['item_order']);
$item_quantity = number_format(floatval($row['item_quantity']),2);
$item_price = floatval($row['item_price']);
$item_tax = floatval($row['item_tax']);
Expand All @@ -276,11 +277,36 @@
$total_tax = $item_tax + $total_tax;
$sub_total = $item_price * $item_quantity + $sub_total;

?>
// Logic to check if top or bottom arrow should be hidden by looking at max and min of item_order
$sql = mysqli_query($mysqli, "SELECT MAX(item_order) AS item_order FROM invoice_items WHERE item_quote_id = $quote_id");
$row = mysqli_fetch_array($sql);
$max_item_order = intval($row['item_order']);

$sql = mysqli_query($mysqli, "SELECT MIN(item_order) AS item_order FROM invoice_items WHERE item_quote_id = $quote_id");
$row = mysqli_fetch_array($sql);
$min_item_order = intval($row['item_order']);

if ($item_order == $max_item_order) {
$down_hidden = "hidden";
} else {
$down_hidden = "";
}

if ($item_order == $min_item_order) {
$up_hidden = "hidden";
} else {
$up_hidden = "";
}



//This is prefered over the screen seen in the invoice menu.
?>

<tr>
<td class="d-print-none">
<?php if ($quote_status !== "Invoiced" && $quote_status !== "Accepted" && $quote_status !== "Declined") { ?>
<?php echo $item_order; ?>
<div class="dropdown">
<button class="btn btn-sm btn-light" type="button" data-toggle="dropdown">
<i class="fas fa-ellipsis-v"></i>
Expand All @@ -293,6 +319,14 @@
<a class="dropdown-item text-danger confirm-link" href="post.php?delete_quote_item=<?php echo $item_id; ?>">
<i class="fa fa-fw fa-times mr-2"></i>Remove
</a>
<div class="dropdown-divider"></div>
<form class="dropdown-item" action="post.php" method="post">
<input type="hidden" name="item_quote_id" value="<?php echo $quote_id; ?>">
<input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
<input type="hidden" name="item_order" value="<?php echo $item_order; ?>">
<button class="btn btn-sm btn-light" type="submit" name="update_quote_item_order" value="up" <?php echo $up_hidden; ?>><i class="fa fa-fw fa-arrow-up"></i></button>
<button class="btn btn-sm btn-light" type="submit" name="update_quote_item_order" value="down" <?php echo $down_hidden; ?>><i class="fa fa-fw fa-arrow-down"></i></button>
</form>
</div>
</div>
<?php } ?>
Expand All @@ -318,6 +352,13 @@
<tr class="d-print-none" <?php if ($quote_status == "Invoiced" || $quote_status == "Accepted" || $quote_status == "Declined") { echo "hidden"; } ?>>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="quote_id" value="<?php echo $quote_id; ?>">
<input type="hidden" name="item_order" value="<?php
//find largest order number and add 1
$sql = mysqli_query($mysqli, "SELECT MAX(item_order) AS item_order FROM invoice_items WHERE item_quote_id = $quote_id");
$row = mysqli_fetch_array($sql);
$item_order = intval($row['item_order']) + 1;
echo $item_order;
?>">
<td></td>
<td>
<input type="text" class="form-control" name="name" id="name" placeholder="Item" required>
Expand Down
Loading

0 comments on commit 9818c57

Please sign in to comment.