$balance){
$_SESSION['alert_message'] = "Payment is more than the balance";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}else{
mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = '$amount', account_id = $account, payment_method = '$payment_method', payment_reference = '$reference', invoice_id = $invoice_id");
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_total_payments_amount = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payments_amount FROM payments WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql_total_payments_amount);
$total_payments_amount = $row['payments_amount'];
//Get the invoice total
$sql = mysqli_query($mysqli,"SELECT * FROM invoices, clients WHERE invoices.client_id = clients.client_id AND invoices.invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql);
$invoice_amount = $row['invoice_amount'];
$invoice_number = $row['invoice_number'];
$client_name = $row['client_name'];
$client_email = $row['client_email'];
//Calculate the Invoice balance
$invoice_balance = $invoice_amount - $total_payments_amount;
//Format Amount
$formatted_amount = number_format($amount,2);
$formatted_invoice_balance = number_format($invoice_balance,2);
//Determine if invoice has been paid then set the status accordingly
if($invoice_balance == 0){
$invoice_status = "Paid";
if($email_receipt == 1){
$mail = new PHPMailer(true);
try {
//Mail Server Settings
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $config_smtp_host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $config_smtp_username; // SMTP username
$mail->Password = $config_smtp_password; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $config_smtp_port; // TCP port to connect to
//Recipients
$mail->setFrom($config_mail_from_email, $config_mail_from_name);
$mail->addAddress("$client_email", "$client_name"); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Payment Recieved - Invoice INV-$invoice_number";
$mail->Body = "Hello $client_name,
You are paid in full, we have recieved your payment of $$formatted_amount on $date for invoice INV-$invoice_number by $payment_method.
If you have any questions please contact us at the number below.
~
$config_company_name
Automated Billing Department
$config_company_phone";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
mysqli_query($mysqli,"INSERT INTO invoice_history SET invoice_history_date = CURDATE(), invoice_history_status = 'Sent', invoice_history_description = 'Emailed Receipt!', invoice_id = $invoice_id");
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
}else{
$invoice_status = "Partial";
if($email_receipt == 1){
$mail = new PHPMailer(true);
try {
//Mail Server Settings
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $config_smtp_host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $config_smtp_username; // SMTP username
$mail->Password = $config_smtp_password; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $config_smtp_port; // TCP port to connect to
//Recipients
$mail->setFrom($config_mail_from_email, $config_mail_from_name);
$mail->addAddress("$client_email", "$client_name"); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Partial Payment Recieved for Invoice INV-$invoice_number";
$mail->Body = "Hello $client_name,
We have recieved your payment of $$formatted_amount on $date for invoice INV-$invoice_number by $payment_method with a balance of $$formatted_invoice_balance.
If you have any questions please contact us at the number below.
~
$config_company_name
Automated Billing Department
$config_company_phone";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
mysqli_query($mysqli,"INSERT INTO invoice_history SET invoice_history_date = CURDATE(), invoice_history_status = 'Sent', invoice_history_description = 'Emailed Receipt!', invoice_id = $invoice_id");
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
}
//Update Invoice Status
mysqli_query($mysqli,"UPDATE invoices SET invoice_status = '$invoice_status' WHERE invoice_id = $invoice_id");
//Add Payment to History
mysqli_query($mysqli,"INSERT INTO invoice_history SET invoice_history_date = CURDATE(), invoice_history_status = '$invoice_status', invoice_history_description = 'INVOICE payment added', invoice_id = $invoice_id");
$_SESSION['alert_message'] = "Payment added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
}
if(isset($_GET['delete_payment'])){
$payment_id = intval($_GET['delete_payment']);
$sql = mysqli_query($mysqli,"SELECT * FROM payments WHERE payment_id = $payment_id");
$row = mysqli_fetch_array($sql);
$invoice_id = $row['invoice_id'];
$deleted_payment_amount = $row['payment_amount'];
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_total_payments_amount = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS total_payments_amount FROM payments WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql_total_payments_amount);
$total_payments_amount = $row['total_payments_amount'];
//Get the invoice total
$sql = mysqli_query($mysqli,"SELECT * FROM invoices WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql);
$invoice_amount = $row['invoice_amount'];
//Calculate the Invoice balance
$invoice_balance = $invoice_amount - $total_payments_amount + $deleted_payment_amount;
//Determine if invoice has been paid
if($invoice_balance == 0){
$invoice_status = "Paid";
}else{
$invoice_status = "Partial";
}
//Update Invoice Status
mysqli_query($mysqli,"UPDATE invoices SET invoice_status = '$invoice_status' WHERE invoice_id = $invoice_id");
//Add Payment to History
mysqli_query($mysqli,"INSERT INTO invoice_history SET invoice_history_date = CURDATE(), invoice_history_status = '$invoice_status', invoice_history_description = 'INVOICE payment deleted', invoice_id = $invoice_id");
mysqli_query($mysqli,"DELETE FROM payments WHERE payment_id = $payment_id");
$_SESSION['alert_message'] = "Payment deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['email_invoice'])){
$invoice_id = intval($_GET['email_invoice']);
$sql = mysqli_query($mysqli,"SELECT * FROM invoices, clients
WHERE invoices.client_id = clients.client_id
AND invoices.invoice_id = $invoice_id"
);
$row = mysqli_fetch_array($sql);
$invoice_id = $row['invoice_id'];
$invoice_number = $row['invoice_number'];
$invoice_status = $row['invoice_status'];
$invoice_date = $row['invoice_date'];
$invoice_due = $row['invoice_due'];
$invoice_amount = $row['invoice_amount'];
$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'];
$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_price |
$item_quantity |
$$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.' |
|
| Item |
Description |
Unit Cost |
Quantity |
Tax |
Line 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 due: |
$ '.number_format($balance,2).' |
'.$config_invoice_footer.'
';
$mpdf = new \Mpdf\Mpdf([
'margin_left' => 20,
'margin_right' => 15,
'margin_top' => 48,
'margin_bottom' => 25,
'margin_header' => 10,
'margin_footer' => 10
]);
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("$config_company_name - Invoice");
$mpdf->SetAuthor("$config_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("uploads/$invoice_date-$config_company_name-Invoice$invoice_number.pdf", 'F');
$mail = new PHPMailer(true);
try{
//Mail Server Settings
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $config_smtp_host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $config_smtp_username; // SMTP username
$mail->Password = $config_smtp_password; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $config_smtp_port; // TCP port to connect to
//Recipients
$mail->setFrom($config_mail_from_email, $config_mail_from_name);
$mail->addAddress("$client_email", "$client_name"); // Add a recipient
// Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->addAttachment("uploads/$invoice_date-$config_company_name-Invoice$invoice_number.pdf"); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
if($invoice_status == 'Paid'){
$mail->Subject = "Copy of Invoice $invoice_number";
$mail->Body = "Hello $client_name,
Attached to this email is a copy of your invoice marked paid.
If you have any questions please contact us at the number below.
~
$config_company_name
Automated Billing Department
$config_company_phone";
}else{
$mail->Subject = "Invoice $invoice_number - $invoice_date - Due on $invoice_due";
$mail->Body = "Hello $client_name,
Attached to this email is your invoice. Please make all checks payable to $config_company_name and mail to
$config_company_address
$config_company_city $config_company_state $config_company_zip
before $invoice_due.
If you have any questions please contact us at the number below.
~
$config_company_name
Automated Billing Department
$config_company_phone";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
}
$mail->send();
echo 'Message has been sent';
mysqli_query($mysqli,"INSERT INTO invoice_history SET invoice_history_date = CURDATE(), invoice_history_status = 'Sent', invoice_history_description = 'Emailed Invoice!', invoice_id = $invoice_id");
//Don't chnage the status to sent if the status is anything but draf
if($invoice_status == 'Draft'){
mysqli_query($mysqli,"UPDATE invoices SET invoice_status = 'Sent', client_id = $client_id WHERE invoice_id = $invoice_id");
}
$_SESSION['alert_message'] = "Invoice has been sent";
header("Location: " . $_SERVER["HTTP_REFERER"]);
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
unlink("uploads/$invoice_date-$config_company_name-Invoice$invoice_number.pdf");
}
if(isset($_GET['pdf_invoice'])){
$invoice_id = intval($_GET['pdf_invoice']);
$sql = mysqli_query($mysqli,"SELECT * FROM invoices, clients
WHERE invoices.client_id = clients.client_id
AND invoices.invoice_id = $invoice_id"
);
$row = mysqli_fetch_array($sql);
$invoice_id = $row['invoice_id'];
$invoice_number = $row['invoice_number'];
$invoice_status = $row['invoice_status'];
$invoice_date = $row['invoice_date'];
$invoice_due = $row['invoice_due'];
$invoice_amount = $row['invoice_amount'];
$invoice_note = $row['invoice_note'];
$invoice_category_id = $row['category_id'];
$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'];
$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_price |
$item_quantity |
$$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.' |
|
| Item |
Description |
Unit Cost |
Quantity |
Tax |
Line 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 due: |
$ '.number_format($balance,2).' |
'.$config_invoice_footer.'
';
$mpdf = new \Mpdf\Mpdf([
'margin_left' => 20,
'margin_right' => 15,
'margin_top' => 48,
'margin_bottom' => 25,
'margin_header' => 10,
'margin_footer' => 10
]);
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("$config_company_name - Invoice");
$mpdf->SetAuthor("$config_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();
}
if(isset($_POST['edit_invoice_note'])){
$invoice_id = intval($_POST['invoice_id']);
$invoice_note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['invoice_note']));
mysqli_query($mysqli,"UPDATE invoices SET invoice_note = '$invoice_note' WHERE invoice_id = $invoice_id");
$_SESSION['alert_message'] = "Notes added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_contact'])){
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$title = strip_tags(mysqli_real_escape_string($mysqli,$_POST['title']));
$phone = strip_tags(mysqli_real_escape_string($mysqli,$_POST['phone']));
$phone = preg_replace("/[^0-9]/", '',$phone);
$email = strip_tags(mysqli_real_escape_string($mysqli,$_POST['email']));
if($_FILES['file']['tmp_name']!='') {
$path = "uploads/contact_photos/";
$path = $path . time() . basename( $_FILES['file']['name']);
$file_name = basename($path);
move_uploaded_file($_FILES['file']['tmp_name'], $path);
}
mysqli_query($mysqli,"INSERT INTO contacts SET contact_name = '$name', contact_title = '$title', contact_phone = '$phone', contact_email = '$email', contact_photo = '$path', client_id = $client_id");
$_SESSION['alert_message'] = "Contact added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_contact'])){
$contact_id = intval($_POST['contact_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$title = strip_tags(mysqli_real_escape_string($mysqli,$_POST['title']));
$phone = strip_tags(mysqli_real_escape_string($mysqli,$_POST['phone']));
$phone = preg_replace("/[^0-9]/", '',$phone);
$email = strip_tags(mysqli_real_escape_string($mysqli,$_POST['email']));
mysqli_query($mysqli,"UPDATE contacts SET contact_name = '$name', contact_title = '$title', contact_phone = '$phone', contact_email = '$email' WHERE contact_id = $contact_id");
$_SESSION['alert_message'] = "Contact updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_contact'])){
$contact_id = intval($_GET['delete_contact']);
mysqli_query($mysqli,"DELETE FROM contacts WHERE contact_id = $contact_id");
$_SESSION['alert_message'] = "Contact deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_location'])){
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$address = strip_tags(mysqli_real_escape_string($mysqli,$_POST['address']));
$city = strip_tags(mysqli_real_escape_string($mysqli,$_POST['city']));
$state = strip_tags(mysqli_real_escape_string($mysqli,$_POST['state']));
$zip = strip_tags(mysqli_real_escape_string($mysqli,$_POST['zip']));
$phone = strip_tags(mysqli_real_escape_string($mysqli,$_POST['phone']));
$phone = preg_replace("/[^0-9]/", '',$phone);
$hours = strip_tags(mysqli_real_escape_string($mysqli,$_POST['hours']));
mysqli_query($mysqli,"INSERT INTO locations SET location_name = '$name', location_address = '$address', location_city = '$city', location_state = '$state', location_zip = '$zip', location_phone = '$phone', location_hours = '$hours', client_id = $client_id");
$_SESSION['alert_message'] = "Location added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_location'])){
$location_id = intval($_POST['location_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$address = strip_tags(mysqli_real_escape_string($mysqli,$_POST['address']));
$city = strip_tags(mysqli_real_escape_string($mysqli,$_POST['city']));
$state = strip_tags(mysqli_real_escape_string($mysqli,$_POST['state']));
$zip = strip_tags(mysqli_real_escape_string($mysqli,$_POST['zip']));
$phone = strip_tags(mysqli_real_escape_string($mysqli,$_POST['phone']));
$phone = preg_replace("/[^0-9]/", '',$phone);
$hours = strip_tags(mysqli_real_escape_string($mysqli,$_POST['hours']));
mysqli_query($mysqli,"UPDATE locations SET location_name = '$name', location_address = '$address', location_city = '$city', location_state = '$state', location_zip = '$zip', location_phone = '$phone', location_hours = '$hours' WHERE location_id = $location_id");
$_SESSION['alert_message'] = "Location updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_location'])){
$location_id = intval($_GET['delete_location']);
mysqli_query($mysqli,"DELETE FROM locations WHERE location_id = $location_id");
$_SESSION['alert_message'] = "Location deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_asset'])){
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$type = strip_tags(mysqli_real_escape_string($mysqli,$_POST['type']));
$make = strip_tags(mysqli_real_escape_string($mysqli,$_POST['make']));
$model = strip_tags(mysqli_real_escape_string($mysqli,$_POST['model']));
$serial = strip_tags(mysqli_real_escape_string($mysqli,$_POST['serial']));
$location = intval($_POST['location']);
$vendor = intval($_POST['vendor']);
$contact = intval($_POST['contact']);
$purchase_date = strip_tags(mysqli_real_escape_string($mysqli,$_POST['purchase_date']));
$warranty_expire = strip_tags(mysqli_real_escape_string($mysqli,$_POST['warranty_expire']));
$note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['note']));
mysqli_query($mysqli,"INSERT INTO assets SET asset_name = '$name', asset_type = '$type', asset_make = '$make', asset_model = '$model', asset_serial = '$serial', location_id = $location, vendor_id = $vendor, contact_id = $contact, asset_purchase_date = '$purchase_date', asset_warranty_expire = '$warranty_expire', asset_note = '$note', client_id = $client_id");
if(!empty($_POST['username'])) {
$asset_id = mysqli_insert_id($mysqli);
$username = strip_tags(mysqli_real_escape_string($mysqli,$_POST['username']));
$password = strip_tags(mysqli_real_escape_string($mysqli,$_POST['password']));
$description = "$type - $name";
mysqli_query($mysqli,"INSERT INTO logins SET login_description = '$description', login_username = '$username', login_password = '$password', asset_id = $asset_id, client_id = $client_id");
}
$_SESSION['alert_message'] = "Asset added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_asset'])){
$asset_id = intval($_POST['asset_id']);
$login_id = intval($_POST['login_id']);
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$type = strip_tags(mysqli_real_escape_string($mysqli,$_POST['type']));
$make = strip_tags(mysqli_real_escape_string($mysqli,$_POST['make']));
$model = strip_tags(mysqli_real_escape_string($mysqli,$_POST['model']));
$serial = strip_tags(mysqli_real_escape_string($mysqli,$_POST['serial']));
$location = intval($_POST['location']);
$vendor = intval($_POST['vendor']);
$contact = intval($_POST['contact']);
$purchase_date = strip_tags(mysqli_real_escape_string($mysqli,$_POST['purchase_date']));
$warranty_expire = strip_tags(mysqli_real_escape_string($mysqli,$_POST['warranty_expire']));
$note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['note']));
$username = strip_tags(mysqli_real_escape_string($mysqli,$_POST['username']));
$password = strip_tags(mysqli_real_escape_string($mysqli,$_POST['password']));
$description = "$type - $name";
mysqli_query($mysqli,"UPDATE assets SET asset_name = '$name', asset_type = '$type', asset_make = '$make', asset_model = '$model', asset_serial = '$serial', location_id = $location, vendor_id = $vendor, contact_id = $contact, asset_purchase_date = '$purchase_date', asset_warranty_expire = '$warranty_expire', asset_note = '$note' WHERE asset_id = $asset_id");
//If login exists then update the login
if($login_id > 0){
mysqli_query($mysqli,"UPDATE logins SET login_description = '$description', login_username = '$username', login_password = '$password' WHERE login_id = $login_id");
}else{
//If Username is filled in then add a login
if(!empty($_POST['username'])) {
mysqli_query($mysqli,"INSERT INTO logins SET login_description = '$description', login_username = '$username', login_password = '$password', asset_id = $asset_id, client_id = $client_id");
}
}
$_SESSION['alert_message'] = "Asset updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_asset'])){
$asset_id = intval($_GET['delete_asset']);
mysqli_query($mysqli,"DELETE FROM assets WHERE asset_id = $asset_id");
$_SESSION['alert_message'] = "Asset deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_login'])){
$client_id = intval($_POST['client_id']);
$description = strip_tags(mysqli_real_escape_string($mysqli,$_POST['description']));
$web_link = strip_tags(mysqli_real_escape_string($mysqli,$_POST['web_link']));
$username = strip_tags(mysqli_real_escape_string($mysqli,$_POST['username']));
$password = strip_tags(mysqli_real_escape_string($mysqli,$_POST['password']));
$note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['note']));
$vendor_id = intval($_POST['vendor']);
$asset_id = intval($_POST['asset']);
$application_id = intval($_POST['application']);
mysqli_query($mysqli,"INSERT INTO logins SET login_description = '$description', login_web_link = '$web_link', login_username = '$username', login_password = '$password', login_note = '$note', vendor_id = $vendor_id, asset_id = $asset_id, application_id = $application_id, client_id = $client_id");
$_SESSION['alert_message'] = "Login added";
header("Location: client.php?client_id=$client_id&tab=logins");
}
if(isset($_POST['edit_login'])){
$login_id = intval($_POST['login_id']);
$description = strip_tags(mysqli_real_escape_string($mysqli,$_POST['description']));
$web_link = strip_tags(mysqli_real_escape_string($mysqli,$_POST['web_link']));
$username = strip_tags(mysqli_real_escape_string($mysqli,$_POST['username']));
$password = strip_tags(mysqli_real_escape_string($mysqli,$_POST['password']));
$note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['note']));
mysqli_query($mysqli,"UPDATE logins SET login_description = '$description', login_web_link = '$web_link', login_username = '$username', login_password = '$password', login_note = '$note' WHERE login_id = $login_id");
$_SESSION['alert_message'] = "Login updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_login'])){
$login_id = intval($_GET['delete_login']);
mysqli_query($mysqli,"DELETE FROM logins WHERE login_id = $login_id");
$_SESSION['alert_message'] = "Login deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_file'])){
$client_id = intval($_POST['client_id']);
$new_name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['new_name']));
if($_FILES['file']['tmp_name']!='') {
$path = "uploads/client_files/$client_id/";
$path = $path . basename( $_FILES['file']['name']);
$file_name = basename($path);
move_uploaded_file($_FILES['file']['tmp_name'], $path);
$ext = pathinfo($path);
$ext = $ext['extension'];
}
mysqli_query($mysqli,"INSERT INTO files SET file_name = '$path', file_ext = '$ext', client_id = $client_id");
$_SESSION['alert_message'] = "File uploaded";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_file'])){
$file_id = intval($_GET['delete_file']);
$sql_file = mysqli_query($mysqli,"SELECT * FROM files WHERE file_id = $file_id");
$row = mysqli_fetch_array($sql_file);
$file_name = $row['file_name'];
unlink($file_name);
mysqli_query($mysqli,"DELETE FROM files WHERE file_id = $file_id");
$_SESSION['alert_message'] = "File deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_note'])){
$client_id = intval($_POST['client_id']);
$subject = strip_tags(mysqli_real_escape_string($mysqli,$_POST['subject']));
$note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['note']));
mysqli_query($mysqli,"INSERT INTO notes SET note_subject = '$subject', note_body = '$note', client_id = $client_id");
$_SESSION['alert_message'] = "Note added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_note'])){
$note_id = intval($_POST['note_id']);
$subject = strip_tags(mysqli_real_escape_string($mysqli,$_POST['subject']));
$note = strip_tags(mysqli_real_escape_string($mysqli,$_POST['note']));
mysqli_query($mysqli,"UPDATE notes SET note_subject = '$subject', note_body = '$note' WHERE note_id = $note_id");
$_SESSION['alert_message'] = "Note updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_note'])){
$note_id = intval($_GET['delete_note']);
mysqli_query($mysqli,"DELETE FROM notes WHERE note_id = $note_id");
$_SESSION['alert_message'] = "Note deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_network'])){
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$network = strip_tags(mysqli_real_escape_string($mysqli,$_POST['network']));
$gateway = strip_tags(mysqli_real_escape_string($mysqli,$_POST['gateway']));
$dhcp_range = strip_tags(mysqli_real_escape_string($mysqli,$_POST['dhcp_range']));
mysqli_query($mysqli,"INSERT INTO networks SET network_name = '$name', network = '$network', network_gateway = '$gateway', network_dhcp_range = '$dhcp_range', client_id = $client_id");
$_SESSION['alert_message'] = "Network added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_network'])){
$network_id = intval($_POST['network_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$network = strip_tags(mysqli_real_escape_string($mysqli,$_POST['network']));
$gateway = strip_tags(mysqli_real_escape_string($mysqli,$_POST['gateway']));
$dhcp_range = strip_tags(mysqli_real_escape_string($mysqli,$_POST['dhcp_range']));
mysqli_query($mysqli,"UPDATE networks SET network_name = '$name', network = '$network', network_gateway = '$gateway', network_dhcp_range = '$dhcp_range' WHERE network_id = $network_id");
$_SESSION['alert_message'] = "Network updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_network'])){
$network_id = intval($_GET['delete_network']);
mysqli_query($mysqli,"DELETE FROM networks WHERE network_id = $network_id");
$_SESSION['alert_message'] = "Network deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_domain'])){
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$registrar = intval($_POST['registrar']);
$webhost = intval($_POST['webhost']);
$expire = strip_tags(mysqli_real_escape_string($mysqli,$_POST['expire']));
mysqli_query($mysqli,"INSERT INTO domains SET domain_name = '$name', domain_registrar = $registrar, domain_webhost = $webhost, domain_expire = '$expire', client_id = $client_id");
$_SESSION['alert_message'] = "Domain added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_domain'])){
$domain_id = intval($_POST['domain_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$registrar = intval($_POST['registrar']);
$webhost = intval($_POST['webhost']);
$expire = strip_tags(mysqli_real_escape_string($mysqli,$_POST['expire']));
mysqli_query($mysqli,"UPDATE domains SET domain_name = '$name', domain_registrar = $registrar, domain_webhost = $webhost, domain_expire = '$expire' WHERE domain_id = $domain_id");
$_SESSION['alert_message'] = "Domain updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_domain'])){
$domain_id = intval($_GET['delete_domain']);
mysqli_query($mysqli,"DELETE FROM domains WHERE domain_id = $domain_id");
$_SESSION['alert_message'] = "Domain deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['add_software'])){
$client_id = intval($_POST['client_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$type = strip_tags(mysqli_real_escape_string($mysqli,$_POST['type']));
$license = strip_tags(mysqli_real_escape_string($mysqli,$_POST['license']));
mysqli_query($mysqli,"INSERT INTO software SET software_name = '$name', software_type = '$type', software_license = '$license', client_id = $client_id");
if(!empty($_POST['username'])) {
$software_id = mysqli_insert_id($mysqli);
$username = strip_tags(mysqli_real_escape_string($mysqli,$_POST['username']));
$password = strip_tags(mysqli_real_escape_string($mysqli,$_POST['password']));
mysqli_query($mysqli,"INSERT INTO logins SET login_username = '$username', login_password = '$password', software_id = $software_id, client_id = $client_id");
}
$_SESSION['alert_message'] = "Software added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_POST['edit_software'])){
$software_id = intval($_POST['software_id']);
$name = strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']));
$type = strip_tags(mysqli_real_escape_string($mysqli,$_POST['type']));
$license = strip_tags(mysqli_real_escape_string($mysqli,$_POST['license']));
mysqli_query($mysqli,"UPDATE software SET software_name = '$name', software_type = '$type', software_license = '$license' WHERE software_id = $software_id");
$_SESSION['alert_message'] = "Software updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if(isset($_GET['delete_software'])){
$software_id = intval($_GET['delete_software']);
mysqli_query($mysqli,"DELETE FROM software WHERE software_id = $software_id");
$_SESSION['alert_message'] = "Software deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
?>