diff --git a/post/invoice.php b/post/invoice.php index 6d3922f57..4c2c0574e 100644 --- a/post/invoice.php +++ b/post/invoice.php @@ -396,6 +396,7 @@ $qty = floatval($_POST['qty']); $price = floatval($_POST['price']); $tax_id = intval($_POST['tax_id']); + $item_order_id = intval($_POST['item_order_id']); $subtotal = $price * $qty; @@ -410,7 +411,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_invoice_id = $invoice_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_order_id = $item_order_id, item_tax_id = $tax_id, item_invoice_id = $invoice_id"); //Update Invoice Balances @@ -1082,3 +1083,61 @@ exit; } + + +if (isset($_POST['update_invoice_item_order'])) { + + if ($_POST['update_invoice_item_order'] == 'up') { + $item_id = intval($_POST['item_id']); + $item_invoice_id = intval($_POST['item_invoice_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_invoice_id = $item_invoice_id AND item_order_id = $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_id = $new_item_order WHERE item_id = $item_id"); + } + + + + mysqli_query($mysqli,"UPDATE invoice_items SET item_order_id = $item_order WHERE item_invoice_id = $item_invoice_id AND item_order_id = $new_item_order"); + mysqli_query($mysqli,"UPDATE invoice_items SET item_order_id = $new_item_order WHERE item_id = $item_id"); + + $_SESSION['alert_message'] = "Item moved up"; + + header("Location: " . $_SERVER["HTTP_REFERER"]); + + } + + if ($_POST['update_invoice_item_order'] == 'down') { + $item_id = intval($_POST['item_id']); + $item_invoice_id = intval($_POST['item_invoice_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_id = $item_order WHERE item_invoice_id = $item_invoice_id AND item_order_id = $new_item_order"); + mysqli_query($mysqli,"UPDATE invoice_items SET item_order_id = $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