From 5221a3676ea2a9bc092f399718752c94a8be6e70 Mon Sep 17 00:00:00 2001 From: o-psi Date: Sun, 15 Oct 2023 22:14:36 -0500 Subject: [PATCH] quote sort logic --- post/quote.php | 60 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/post/quote.php b/post/quote.php index 7b679d3c..fa4daa68 100644 --- a/post/quote.php +++ b/post/quote.php @@ -150,6 +150,7 @@ if (isset($_POST['add_quote_item'])) { $qty = floatval($_POST['qty']); $price = floatval($_POST['price']); $tax_id = intval($_POST['tax_id']); + $item_order = intval($_POST['item_order']); $subtotal = $price * $qty; @@ -164,7 +165,7 @@ if (isset($_POST['add_quote_item'])) { $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 @@ -433,3 +434,60 @@ if(isset($_POST['export_client_quotes_csv'])){ 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"]); + + } + +} \ No newline at end of file