Skip to content

Commit

Permalink
Merge pull request #790 from twetech/Discounts-pt-2
Browse files Browse the repository at this point in the history
Update Quotes and Recurrings with discounts
  • Loading branch information
johnnyq authored Oct 25, 2023
2 parents 903e943 + 62d3cab commit 430e148
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 119 deletions.
1 change: 1 addition & 0 deletions client_quotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
$quote_status = nullable_htmlentities($row['quote_status']);
$quote_date = nullable_htmlentities($row['quote_date']);
$quote_expire = nullable_htmlentities($row['quote_expire']);
$quote_discount = floatval($row['quote_discount_amount']);
$quote_amount = floatval($row['quote_amount']);
$quote_currency_code = nullable_htmlentities($row['quote_currency_code']);
$quote_created_at = nullable_htmlentities($row['quote_created_at']);
Expand Down
7 changes: 4 additions & 3 deletions client_recurring_invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
}
$recurring_next_date = nullable_htmlentities($row['recurring_next_date']);
$recurring_amount = floatval($row['recurring_amount']);
$recurring_discount = floatval($row['recurring_discount_amount']);
$recurring_currency_code = nullable_htmlentities($row['recurring_currency_code']);
$recurring_created_at = nullable_htmlentities($row['recurring_created_at']);
$category_id = intval($row['category_id']);
Expand All @@ -106,9 +107,9 @@
<td><?php echo $recurring_next_date; ?></td>
<td><?php echo $category_name; ?></td>
<td>
<span class="p-2 badge badge-<?php echo $status_badge_color; ?>">
<?php echo $status; ?>
</span>
<span class="p-2 badge badge-<?php echo $status_badge_color; ?>">
<?php echo $status; ?>
</span>
</td>
<td>
<div class="dropdown dropleft text-center">
Expand Down
12 changes: 12 additions & 0 deletions guest_view_quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
$quote_status = nullable_htmlentities($row['quote_status']);
$quote_date = nullable_htmlentities($row['quote_date']);
$quote_expire = nullable_htmlentities($row['quote_expire']);
$quote_discount = floatval($row['quote_discount_amount']);
$quote_amount = floatval($row['quote_amount']);
$quote_currency_code = nullable_htmlentities($row['quote_currency_code']);
$quote_note = nullable_htmlentities($row['quote_note']);
Expand Down Expand Up @@ -232,6 +233,17 @@
<div class="col-sm-3 offset-sm-2">
<table class="table table-borderless">
<tbody>
<?php
if ($quote_discount > 0) {
?>
<tr class="border-bottom">
<td>Discount</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $quote_discount, $quote_currency_code); ?></td>
</tr>
<?php
$sub_total = $sub_total - $quote_discount;
}
?>
<tr class="border-bottom">
<td>Subtotal</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $sub_total, $quote_currency_code); ?></td>
Expand Down
47 changes: 37 additions & 10 deletions post/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
header("Location: invoice.php?invoice_id=$new_invoice_id");

}

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

$invoice_id = intval($_POST['invoice_id']);
Expand Down Expand Up @@ -172,8 +173,6 @@

}



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

$client = intval($_POST['client']);
Expand Down Expand Up @@ -210,8 +209,18 @@
$category = intval($_POST['category']);
$scope = sanitizeInput($_POST['scope']);
$status = intval($_POST['status']);
$recurring_discount = floatval($_POST['recurring_discount']);

//Calculate new total
$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_recurring_id = $recurring_id");
$recurring_amount = 0;
while($row = mysqli_fetch_array($sql)) {
$item_total = floatval($row['item_total']);
$recurring_amount = $recurring_amount + $item_total;
}
$recurring_amount = $recurring_amount - $recurring_discount;

mysqli_query($mysqli,"UPDATE recurring SET recurring_scope = '$scope', recurring_frequency = '$frequency', recurring_next_date = '$next_date', recurring_category_id = $category, recurring_status = $status WHERE recurring_id = $recurring_id");
mysqli_query($mysqli,"UPDATE recurring SET recurring_scope = '$scope', recurring_frequency = '$frequency', recurring_next_date = '$next_date', recurring_category_id = $category, recurring_discount_amount = $recurring_discount, recurring_amount = $recurring_amount, recurring_status = $status WHERE recurring_id = $recurring_id");

mysqli_query($mysqli,"INSERT INTO history SET history_status = '$status', history_description = 'Recurring modified', history_recurring_id = $recurring_id");

Expand Down Expand Up @@ -277,14 +286,23 @@

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
//Get Discount

$sql = mysqli_query($mysqli,"SELECT * FROM recurring WHERE recurring_id = $recurring_id");
$row = mysqli_fetch_array($sql);
$recurring_discount = floatval($row['recurring_discount_amount']);

$new_recurring_amount = floatval($row['recurring_amount']) + $total;

mysqli_query($mysqli,"UPDATE recurring SET recurring_amount = $new_recurring_amount WHERE recurring_id = $recurring_id");
//add up all the items
$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_recurring_id = $recurring_id");
$recurring_amount = 0;
while($row = mysqli_fetch_array($sql)) {
$item_total = floatval($row['item_total']);
$recurring_amount = $recurring_amount + $item_total;
}
$recurring_amount = $recurring_amount - $recurring_discount;

mysqli_query($mysqli,"UPDATE recurring SET recurring_amount = $recurring_amount WHERE recurring_id = $recurring_id");

$_SESSION['alert_message'] = "Recurring Invoice Updated";

Expand Down Expand Up @@ -509,19 +527,28 @@
mysqli_query($mysqli,"UPDATE invoices SET invoice_amount = $new_invoice_amount WHERE invoice_id = $invoice_id");

}elseif ($quote_id > 0) {
//Get Discount Amount
$sql = mysqli_query($mysqli,"SELECT * FROM quotes WHERE quote_id = $quote_id");
$row = mysqli_fetch_array($sql);
$quote_discount = floatval($row['quote_discount_amount']);

//Update Quote Balances by tallying up items
$sql_quote_total = mysqli_query($mysqli,"SELECT SUM(item_total) AS quote_total FROM invoice_items WHERE item_quote_id = $quote_id");
$row = mysqli_fetch_array($sql_quote_total);
$new_quote_amount = floatval($row['quote_total']);
$new_quote_amount = floatval($row['quote_total']) - $quote_discount;

mysqli_query($mysqli,"UPDATE quotes SET quote_amount = $new_quote_amount WHERE quote_id = $quote_id");

} else {
//Update Invoice Balances by tallying up invoice items
//Get Discount Amount
$sql = mysqli_query($mysqli,"SELECT * FROM recurring WHERE recurring_id = $recurring_id");
$row = mysqli_fetch_array($sql);
$recurring_discount = floatval($row['recurring_discount_amount']);

//Update Invoice Balances by tallying up invoice items
$sql_recurring_total = mysqli_query($mysqli,"SELECT SUM(item_total) AS recurring_total FROM invoice_items WHERE item_recurring_id = $recurring_id");
$row = mysqli_fetch_array($sql_recurring_total);
$new_recurring_amount = floatval($row['recurring_total']);
$new_recurring_amount = floatval($row['recurring_total']) - $recurring_discount;

mysqli_query($mysqli,"UPDATE recurring SET recurring_amount = $new_recurring_amount WHERE recurring_id = $recurring_id");

Expand Down Expand Up @@ -990,7 +1017,7 @@

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

} //End Force Recurring
}

if (isset($_POST['export_client_invoices_csv'])) {
$client_id = intval($_POST['client_id']);
Expand Down
3 changes: 2 additions & 1 deletion post/invoice_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
$date = sanitizeInput($_POST['date']);
$category = intval($_POST['category']);
$scope = sanitizeInput($_POST['scope']);
$invoice_discount = floatval($_POST['invoice_discount']);
$invoice_discount = floatval($_POST['invoice_discount']);
$recurring_discount = floatval($_POST['recurring_discount']);
30 changes: 25 additions & 5 deletions post/quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
}

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


include 'post/quote_model.php';
$quote_id = intval($_POST['quote_id']);

$name = sanitizeInput($_POST['name']);
Expand All @@ -168,13 +169,22 @@

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

//Get Discount
$sql = mysqli_query($mysqli,"SELECT * FROM quotes WHERE quote_id = $quote_id");
$row = mysqli_fetch_array($sql);

$new_quote_amount = floatval($row['quote_amount']) + $total;
$quote_discount_amount = floatval($row['quote_discount_amount']);


//add up the total of all items
$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_quote_id = $quote_id");
$quote_amount = 0;
while($row = mysqli_fetch_array($sql)) {
$item_total = floatval($row['item_total']);
$quote_amount = $quote_amount + $item_total;
}
$new_quote_amount = $quote_amount - $quote_discount_amount;

mysqli_query($mysqli,"UPDATE quotes SET quote_amount = $new_quote_amount WHERE quote_id = $quote_id");

$_SESSION['alert_message'] = "Item added";
Expand Down Expand Up @@ -203,7 +213,17 @@

$quote_id = intval($_POST['quote_id']);

mysqli_query($mysqli,"UPDATE quotes SET quote_scope = '$scope', quote_date = '$date', quote_expire = '$expire', quote_category_id = $category WHERE quote_id = $quote_id");
//Calculate the new quote amount
$sql = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE item_quote_id = $quote_id");
$quote_amount = 0;
while($row = mysqli_fetch_array($sql)) {
$item_total = floatval($row['item_total']);
$quote_amount = $quote_amount + $item_total;
}
$quote_amount = $quote_amount - $quote_discount;


mysqli_query($mysqli,"UPDATE quotes SET quote_scope = '$scope', quote_date = '$date', quote_expire = '$expire', quote_discount_amount = '$quote_discount', quote_amount = '$quote_amount', quote_category_id = $category WHERE quote_id = $quote_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Modify', log_description = '$quote_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
Expand Down
1 change: 1 addition & 0 deletions post/quote_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
$expire = sanitizeInput($_POST['expire']);
$category = intval($_POST['category']);
$scope = sanitizeInput($_POST['scope']);
$quote_discount = floatval($_POST['quote_discount']);
12 changes: 12 additions & 0 deletions quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$quote_date = nullable_htmlentities($row['quote_date']);
$quote_expire = nullable_htmlentities($row['quote_expire']);
$quote_amount = floatval($row['quote_amount']);
$quote_discount = floatval($row['quote_discount_amount']);
$quote_currency_code = nullable_htmlentities($row['quote_currency_code']);
$quote_note = nullable_htmlentities($row['quote_note']);
$quote_url_key = nullable_htmlentities($row['quote_url_key']);
Expand Down Expand Up @@ -421,6 +422,17 @@
<div class="col-sm-3 offset-sm-2">
<table class="table table-borderless">
<tbody>
<?php
if ($quote_discount > 0) {
?>
<tr class="border-bottom">
<td>Discount</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $quote_discount, $quote_currency_code); ?></td>
</tr>
<?php
$sub_total = $sub_total - $quote_discount;
}
?>
<tr class="border-bottom">
<td>Subtotal</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $sub_total, $quote_currency_code); ?></td>
Expand Down
11 changes: 11 additions & 0 deletions quote_edit_modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
</div>
</div>


<div class='form-group'>
<label>Discount Amount</label>
<div class='input-group'>
<div class='input-group-prepend'>
<span class='input-group-text'><i class='fa fa-fw fa-dollar-sign'></i></span>
</div>
<input type='number' class='form-control' step="0.01" name='quote_discount' placeholder='Discount Amount' value='<?php echo $quote_discount; ?>'>
</div>
</div>

<div class="form-group">
<label>Scope</label>
<div class="input-group">
Expand Down
1 change: 1 addition & 0 deletions quotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
$quote_date = nullable_htmlentities($row['quote_date']);
$quote_expire = nullable_htmlentities($row['quote_expire']);
$quote_amount = floatval($row['quote_amount']);
$quote_discount = floatval($row['quote_discount']);
$quote_currency_code = nullable_htmlentities($row['quote_currency_code']);
$quote_created_at = nullable_htmlentities($row['quote_created_at']);
$client_id = intval($row['client_id']);
Expand Down
37 changes: 25 additions & 12 deletions recurring_invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
}
$recurring_next_date = nullable_htmlentities($row['recurring_next_date']);
$recurring_amount = floatval($row['recurring_amount']);
$recurring_discount = floatval($row['recurring_discount_amount']);
$recurring_currency_code = nullable_htmlentities($row['recurring_currency_code']);
$recurring_note = nullable_htmlentities($row['recurring_note']);
$category_id = intval($row['recurring_category_id']);
Expand Down Expand Up @@ -353,20 +354,32 @@
<div class="col-sm-3 offset-sm-2">
<table class="table table-borderless">
<tbody>
<tr class="border-bottom">
<td>Subtotal</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $sub_total, $recurring_currency_code); ?></td>
</tr>
<?php if ($total_tax > 0) { ?>
<?php
if ($recurring_discount > 0) {
?>
<tr class="border-bottom">
<td>Discount</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $recurring_discount, $recurring_currency_code); ?></td>
</tr>

<?php
$sub_total = $sub_total - $invoice_discount;
}
?>
<tr class="border-bottom">
<td>Tax</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $total_tax, $recurring_currency_code); ?></td>
<td>Subtotal</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $sub_total, $recurring_currency_code); ?></td>
</tr>
<?php if ($total_tax > 0) { ?>
<tr class="border-bottom">
<td>Tax</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $total_tax, $recurring_currency_code); ?></td>
</tr>
<?php } ?>
<tr class="border-bottom">
<td><strong>Total</strong></td>
<td class="text-right"><strong><?php echo numfmt_format_currency($currency_format, $recurring_amount, $recurring_currency_code); ?></strong></td>
</tr>
<?php } ?>
<tr class="border-bottom">
<td><strong>Total</strong></td>
<td class="text-right"><strong><?php echo numfmt_format_currency($currency_format, $recurring_amount, $recurring_currency_code); ?></strong></td>
</tr>
</tbody>
</table>
</div>
Expand Down
Loading

0 comments on commit 430e148

Please sign in to comment.