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'];
$company_id = $row['company_id'];
$sql_company = mysqli_query($mysqli,"SELECT * FROM settings, companies WHERE settings.company_id = companies.company_id AND companies.company_id = $company_id");
$row = mysqli_fetch_array($sql_company);
$company_name = $row['company_name'];
$config_company_address = $row['config_company_address'];
$config_company_city = $row['config_company_city'];
$config_company_state = $row['config_company_state'];
$config_company_zip = $row['config_company_zip'];
$config_company_phone = $row['config_company_phone'];
if(strlen($config_company_phone)>2){
$config_company_phone = substr($row['config_company_phone'],0,3)."-".substr($row['config_company_phone'],3,3)."-".substr($row['config_company_phone'],6,4);
}
$config_company_email = $row['config_company_email'];
$config_invoice_logo = $row['config_invoice_logo'];
//Mark downloaded in history
mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = '$invoice_status', history_description = 'Invoice downloaded', history_created_at = NOW(), invoice_id = $invoice_id, company_id = $company_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 .= "
| $item_name |
$item_description |
$item_quantity |
$$item_price |
$$item_tax |
$$item_total |
";
}
$html = '
Date: '.$invoice_date.'
Due: '.$invoice_due.'
BILL TO:
'.$client_name.' '.$client_address.' '.$client_city.' '.$client_state.' '.$client_zip.'
'.$client_email.' '.$client_phone.' |
|
| Product |
Description |
Qty |
Price |
Tax |
Total |
'.$invoice_items.'
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).' |
'.$config_invoice_footer.'
';
$mpdf = new \Mpdf\Mpdf([
'margin_left' => 5,
'margin_right' => 5,
'margin_top' => 48,
'margin_bottom' => 25,
'margin_header' => 10,
'margin_footer' => 10
]);
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("$company_name - Invoice");
$mpdf->SetAuthor("$company_name");
if($invoice_status == 'Paid'){
$mpdf->SetWatermarkText("Paid");
}
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output("$invoice_date-$company_name-Invoice$invoice_number.pdf",'D');
}else{
echo "GTFO!!!";
}
}
if(isset($_GET['pdf_quote'], $_GET['url_key'])){
$quote_id = intval($_GET['pdf_quote']);
$url_key = $_GET['url_key'];
$sql = mysqli_query($mysqli,"SELECT * FROM quotes, clients
WHERE quotes.client_id = clients.client_id
AND quotes.quote_id = $quote_id
AND quotes.quote_url_key = '$url_key'"
);
if(mysqli_num_rows($sql) == 1){
$row = mysqli_fetch_array($sql);
$quote_id = $row['quote_id'];
$quote_number = $row['quote_number'];
$quote_status = $row['quote_status'];
$quote_date = $row['quote_date'];
$quote_amount = $row['quote_amount'];
$quote_note = $row['quote_note'];
$quote_url_key = $row['quote_url_key'];
$client_id = $row['client_id'];
$client_name = $row['client_name'];
$client_address = $row['client_address'];
$client_city = $row['client_city'];
$client_state = $row['client_state'];
$client_zip = $row['client_zip'];
$client_email = $row['client_email'];
$client_phone = $row['client_phone'];
if(strlen($client_phone)>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'];
$company_id = $row['company_id'];
$sql_company = mysqli_query($mysqli,"SELECT * FROM settings, companies WHERE settings.company_id = companies.company_id AND companies.company_id = $company_id");
$row = mysqli_fetch_array($sql_company);
$company_name = $row['company_name'];
$config_company_address = $row['config_company_address'];
$config_company_city = $row['config_company_city'];
$config_company_state = $row['config_company_state'];
$config_company_zip = $row['config_company_zip'];
$config_company_phone = $row['config_company_phone'];
if(strlen($config_company_phone)>2){
$config_company_phone = substr($row['config_company_phone'],0,3)."-".substr($row['config_company_phone'],3,3)."-".substr($row['config_company_phone'],6,4);
}
$config_company_email = $row['config_company_email'];
$config_invoice_logo = $row['config_invoice_logo'];
$sql_items = mysqli_query($mysqli,"SELECT * FROM invoice_items WHERE quote_id = $quote_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;
$items .= "
| $item_name |
$item_description |
$item_quantity |
$$item_price |
$$item_tax |
$$item_total |
";
}
$html = '
Date: '.$quote_date.'
TO:
'.$client_name.' '.$client_address.' '.$client_city.' '.$client_state.' '.$client_zip.'
'.$client_email.' '.$client_phone.' |
|
| Product |
Description |
Qty |
Price |
Tax |
Total |
'.$items.'
Notes '.$quote_note.' |
Subtotal: |
$ '.number_format($sub_total,2).' |
| Tax: |
$ '.number_format($total_tax,2).' |
| Total: |
$ '.number_format($quote_amount,2).' |
'.$config_quote_footer.'
';
$mpdf = new \Mpdf\Mpdf([
'margin_left' => 5,
'margin_right' => 5,
'margin_top' => 48,
'margin_bottom' => 25,
'margin_header' => 10,
'margin_footer' => 10
]);
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("$company_name - Quote");
$mpdf->SetAuthor("$company_name");
$mpdf->SetWatermarkText("Quote");
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output("$quote_date-$company_name-Quote$quote_number.pdf",'D');
}else{
echo "GTFO!!!";
}
}
if(isset($_GET['approve_quote'], $_GET['url_key'])){
$quote_id = intval($_GET['approve_quote']);
$url_key = $_GET['url_key'];
$sql = mysqli_query($mysqli,"SELECT * FROM quotes
WHERE quotes.quote_id = $quote_id
AND quotes.quote_url_key = '$url_key'"
);
if(mysqli_num_rows($sql) == 1){
mysqli_query($mysqli,"UPDATE quotes SET quote_status = 'Approved' WHERE quote_id = $quote_id");
mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Approved', history_description = 'Client approved Quote!', history_created_at = NOW(), quote_id = $quote_id, company_id = $company_id");
$_SESSION['alert_message'] = "Quote approved";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}else{
echo "GTFO!!";
}
}
if(isset($_GET['reject_quote'], $_GET['url_key'])){
$quote_id = intval($_GET['reject_quote']);
$url_key = $_GET['url_key'];
$sql = mysqli_query($mysqli,"SELECT * FROM quotes
WHERE quotes.quote_id = $quote_id
AND quotes.quote_url_key = '$url_key'"
);
if(mysqli_num_rows($sql) == 1){
mysqli_query($mysqli,"UPDATE quotes SET quote_status = 'Rejected' WHERE quote_id = $quote_id");
mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Rejected', history_description = 'Client rejected Quote!', history_created_at = NOW(), quote_id = $quote_id, company_id = $company_id");
$_SESSION['alert_message'] = "Quote rejected";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}else{
echo "GTFO!!";
}
}
?>