From 632714cd17980faa20fbca894beec9e41d8c56e4 Mon Sep 17 00:00:00 2001 From: "johnny@pittpc.com" Date: Sat, 6 Feb 2021 00:18:37 -0500 Subject: [PATCH] Added Tax ID to each line item, also if tax % changes the new recurring will automatically get updated and sent with the right tax rate DB Structure updated --- cron.php | 38 ++++++-- db.sql | 3 +- edit_invoice_item_modal.php | 14 +-- edit_quote_item_modal.php | 10 +- edit_recurring_item_modal.php | 10 +- guest_view_invoice.php | 4 +- invoice.php | 16 ++-- logs.php | 23 ++++- post.php | 166 ++++++++++++++++++++++++++-------- quote.php | 12 ++- recurring_invoice.php | 12 ++- tickets.php | 2 - 12 files changed, 220 insertions(+), 90 deletions(-) diff --git a/cron.php b/cron.php index c493ac44..0bbc1b43 100644 --- a/cron.php +++ b/cron.php @@ -167,6 +167,7 @@ while($row = mysqli_fetch_array($sql_companies)){ //Send Recurring Invoices that match todays date and are active + //Loop through all recurring that match today's date and is active $sql_recurring = mysqli_query($mysqli,"SELECT * FROM recurring, clients WHERE clients.client_id = recurring.client_id AND recurring.recurring_next_date = CURDATE() AND recurring.recurring_status = 1 AND recurring.company_id = $company_id"); while($row = mysqli_fetch_array($sql_recurring)){ @@ -199,7 +200,7 @@ while($row = mysqli_fetch_array($sql_companies)){ $new_invoice_id = mysqli_insert_id($mysqli); - //Copy Items from original invoice to new invoice + //Copy Items from original recurring invoice to new invoice $sql_invoice_items = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE recurring_id = $recurring_id ORDER BY item_id ASC"); while($row = mysqli_fetch_array($sql_invoice_items)){ @@ -208,17 +209,40 @@ while($row = mysqli_fetch_array($sql_companies)){ $item_description = mysqli_real_escape_string($mysqli,$row['item_description']); //SQL Escape incase of , $item_quantity = $row['item_quantity']; $item_price = $row['item_price']; - $item_subtotal = $row['item_price']; - $item_tax = $row['item_tax']; - $item_total = $row['item_total']; + $item_subtotal = $row['item_subtotal']; + $tax_id = $row['tax_id']; - mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$item_name', item_description = '$item_description', item_quantity = $item_quantity, item_price = '$item_price', item_subtotal = '$item_subtotal', item_tax = '$item_tax', item_total = '$item_total', item_created_at = NOW(), invoice_id = $new_invoice_id, company_id = $company_id"); + //Recalculate Item Tax since Tax percents can change. + if($tax_id > 0){ + $sql = mysqli_query($mysqli,"SELECT * FROM taxes WHERE tax_id = $tax_id AND company_id = $company_id"); + $row = mysqli_fetch_array($sql); + $tax_percent = $row['tax_percent']; + $item_tax_amount = $item_subtotal * $tax_percent / 100; + }else{ + $item_tax_amount = 0; + } + + $item_total = $item_subtotal + $item_tax_amount; + + //Update Recurring Items with new tax + mysqli_query($mysqli,"UPDATE invoice_items SET item_tax = '$item_tax_amount', item_total = '$item_total', item_updated_at = NOW(), tax_id = $tax_id WHERE item_id = $item_id"); + + //Insert Items into New Invoice + mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$item_name', item_description = '$item_description', item_quantity = $item_quantity, item_price = '$item_price', item_subtotal = '$item_subtotal', item_tax = '$item_tax_amount', item_total = '$item_total', item_created_at = NOW(), tax_id = $tax_id, invoice_id = $new_invoice_id, company_id = $company_id"); + } mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Sent', history_description = 'Invoice Generated from Recurring!', history_created_at = NOW(), invoice_id = $new_invoice_id, company_id = $company_id"); - //update the recurring invoice with the new dates - mysqli_query($mysqli,"UPDATE recurring SET recurring_last_sent = CURDATE(), recurring_next_date = DATE_ADD(CURDATE(), INTERVAL 1 $recurring_frequency), recurring_updated_at = NOW() WHERE recurring_id = $recurring_id"); + //Update Recurring Balances by tallying up recurring items also update recurring dates + $sql_recurring_total = mysqli_query($mysqli,"SELECT SUM(item_total) AS recurring_total FROM invoice_items WHERE recurring_id = $recurring_id"); + $row = mysqli_fetch_array($sql_recurring_total); + $new_recurring_amount = $row['recurring_total']; + + mysqli_query($mysqli,"UPDATE recurring SET recurring_amount = '$new_recurring_amount', recurring_last_sent = CURDATE(), recurring_next_date = DATE_ADD(CURDATE(), INTERVAL 1 $recurring_frequency), recurring_updated_at = NOW() WHERE recurring_id = $recurring_id"); + + //Also update the newly created invoice with the new amounts + mysqli_query($mysqli,"UPDATE invoices SET invoice_amount = '$new_recurring_amount' WHERE invoice_id = $new_invoice_id"); if($config_recurring_auto_send_invoice == 1){ $sql = mysqli_query($mysqli,"SELECT * FROM invoices, clients diff --git a/db.sql b/db.sql index 70b6c8a4..ab2bb9c7 100644 --- a/db.sql +++ b/db.sql @@ -379,6 +379,7 @@ CREATE TABLE `invoice_items` ( `item_total` decimal(15,2) NOT NULL, `item_created_at` datetime NOT NULL, `item_updated_at` datetime DEFAULT NULL, + `tax_id` int(11) DEFAULT NULL, `quote_id` int(11) DEFAULT NULL, `recurring_id` int(11) DEFAULT NULL, `invoice_id` int(11) DEFAULT NULL, @@ -928,4 +929,4 @@ CREATE TABLE `vendors` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2021-02-04 21:53:53 +-- Dump completed on 2021-02-06 0:17:15 diff --git a/edit_invoice_item_modal.php b/edit_invoice_item_modal.php index 81d5eab3..fed83a8b 100644 --- a/edit_invoice_item_modal.php +++ b/edit_invoice_item_modal.php @@ -1,10 +1,10 @@