Skip to content

Commit

Permalink
Merge pull request #762 from o-psi/Invoice-items-order
Browse files Browse the repository at this point in the history
Sort invoice items
  • Loading branch information
johnnyq authored Oct 14, 2023
2 parents 21a5006 + ed269e1 commit 766df19
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
23 changes: 20 additions & 3 deletions database_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -1405,12 +1405,29 @@
// 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.8') {
if (CURRENT_DATABASE_VERSION == '0.8.8') {
// Insert queries here required to update to DB version 0.8.9
mysqli_query($mysqli, "ALTER TABLE `invoice_items` ADD `item_order` INT(11) NOT NULL DEFAULT 0 AFTER `item_total`");
// Update existing invoices so that item_order is set to item_id
$sql_invoices = mysqli_query($mysqli, "SELECT invoice_id FROM invoices WHERE invoice_id IS NOT NULL");
foreach ($sql_invoices as $row) {
$invoice_id = $row['invoice_id'];
$sql_invoice_items = mysqli_query($mysqli, "SELECT item_id FROM invoice_items WHERE item_invoice_id = '$invoice_id' ORDER BY item_id ASC");
$item_order = 1;
foreach ($sql_invoice_items as $row) {
$item_id = $row['item_id'];
mysqli_query($mysqli, "UPDATE invoice_items SET item_order = '$item_order' WHERE item_id = '$item_id'");
$item_order++;
//Log changes made to invoice
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Invoice', 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.9'");
//}
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.8.9'");
}
//

} else {
Expand Down
1 change: 1 addition & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ CREATE TABLE `invoice_items` (
`item_subtotal` decimal(15,2) NOT NULL DEFAULT 0.00,
`item_tax` decimal(15,2) NOT NULL DEFAULT 0.00,
`item_total` decimal(15,2) NOT NULL DEFAULT 0.00,
`item_order` int(11) NOT NULL DEFAULT 0,
`item_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`item_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`item_archived_at` datetime DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion guest_view_invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
}

// Invoice individual items
$sql_invoice_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_invoice_id = $invoice_id ORDER BY item_id ASC");
$sql_invoice_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_invoice_id = $invoice_id ORDER BY item_order ASC");

?>

Expand Down
21 changes: 20 additions & 1 deletion invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
</div>
</div>

<?php $sql_invoice_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_invoice_id = $invoice_id ORDER BY item_id ASC"); ?>
<?php $sql_invoice_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_invoice_id = $invoice_id ORDER BY item_order ASC"); ?>

<div class="row mb-4">
<div class="col-md-12">
Expand All @@ -256,6 +256,7 @@
<table class="table">
<thead>
<tr>
<th class="text-left">Sort</th>
<th class="d-print-none"></th>
<th>Item</th>
<th>Description</th>
Expand Down Expand Up @@ -283,10 +284,26 @@
$tax_id = intval($row['item_tax_id']);
$total_tax = $item_tax + $total_tax;
$sub_total = $item_price * $item_quantity + $sub_total;
$item_order = intval($row['item_order']);

?>

<tr>
<td>
<div class="d-print-none">
<?php if ($invoice_status !== "Paid" && $invoice_status !== "Cancelled") { ?>
<form action="post.php" method="post">
<input type="hidden" name="invoice_id" value="<?php echo $invoice_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_invoice_item_order" value="up"><i class="fa fa-fw fa-arrow-up"></i></button>
<button class="btn btn-sm btn-light" type="submit" name="update_invoice_item_order" value="down"><i class="fa fa-fw fa-arrow-down"></i></button>
</form>
<?php } ?>

</div>
</td>

<td class="d-print-none">
<?php if ($invoice_status !== "Paid" && $invoice_status !== "Cancelled") { ?>
<div class="dropdown">
Expand Down Expand Up @@ -323,6 +340,8 @@
<tr class="d-print-none" <?php if ($invoice_status == "Paid" || $invoice_status == "Cancelled") { echo "hidden"; } ?>>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="invoice_id" value="<?php echo $invoice_id; ?>">
<input type="hidden" name="item_order" value="<?php echo mysqli_num_rows($sql_invoice_items) + 1; ?>">
<td></td>
<td></td>
<td>
<input type="text" class="form-control" id="name" name="name" placeholder="Item" required>
Expand Down
61 changes: 60 additions & 1 deletion post/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,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 @@ -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 = $item_order, item_tax_id = $tax_id, item_invoice_id = $invoice_id");

//Update Invoice Balances

Expand Down Expand Up @@ -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 = $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_invoice_id = $item_invoice_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_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 = $item_order WHERE item_invoice_id = $item_invoice_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"]);

}

}

0 comments on commit 766df19

Please sign in to comment.