2){ $client_phone = substr($row['client_phone'],0,3)."-".substr($row['client_phone'],3,3)."-".substr($row['client_phone'],6,4); } $client_website = $row['client_website']; if(mysqli_num_rows($sql) == 1){ //Mark downloaded in history mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = '$invoice_status', history_description = 'Invoice downloaded', invoice_id = $invoice_id"); $sql_payments = mysqli_query($mysqli,"SELECT * FROM payments, accounts WHERE payments.account_id = accounts.account_id AND payments.invoice_id = $invoice_id ORDER BY payments.payment_id DESC"); //Add up all the payments for the invoice and get the total amount paid to the invoice $sql_amount_paid = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE invoice_id = $invoice_id"); $row = mysqli_fetch_array($sql_amount_paid); $amount_paid = $row['amount_paid']; $balance = $invoice_amount - $amount_paid; $sql_items = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE invoice_id = $invoice_id ORDER BY item_id ASC"); while($row = mysqli_fetch_array($sql_items)){ $item_id = $row['item_id']; $item_name = $row['item_name']; $item_description = $row['item_description']; $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']; $total_tax = $item_tax + $total_tax; $sub_total = $item_price * $item_quantity + $sub_total; $invoice_items .= "
| BILL TO: '.$client_name.' '.$client_address.' '.$client_city.' '.$client_state.' '.$client_zip.' '.$client_email.' '.$client_phone.' |
| Product | Description | Qty | Price | Tax | Total |
Notes'.$invoice_note.' |
Subtotal: | $ '.number_format($sub_total,2).' | |||
| Tax: | $ '.number_format($total_tax,2).' | ||||
| Total: | $ '.number_format($invoice_amount,2).' | ||||
| Paid: | $ '.number_format($amount_paid,2).' | ||||
| Balance: | $ '.number_format($balance,2).' | ||||