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

This commit is contained in:
johnny@pittpc.com
2021-02-06 00:18:37 -05:00
parent 46e0147026
commit 632714cd17
12 changed files with 220 additions and 90 deletions

View File

@@ -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