"
+ id="advancedFilter">
+
+
+
+ Canned Date
+
+
+ value="custom">Custom
+
+ value="today">Today
+
+ value="yesterday">Yesterday
+
+ value="thisweek">This Week
+
+ value="lastweek">Last Week
+
+ value="thismonth">This Month
+
+ value="lastmonth">Last Month
+
+ value="thisyear">This Year
+
+ value="lastyear">Last Year
+
-
+
-
-
-
+
+
+ ?>
\ No newline at end of file
diff --git a/post.php b/post.php
index 40a878d09..978d7d474 100644
--- a/post.php
+++ b/post.php
@@ -43,6 +43,8 @@
require_once "post/file.php";
+require_once "post/inventory.php";
+
require_once "post/invoice.php";
require_once "post/location.php";
diff --git a/post/expense.php b/post/expense.php
index 0ff648a20..f2bcac440 100644
--- a/post/expense.php
+++ b/post/expense.php
@@ -8,13 +8,34 @@
require_once 'post/expense_model.php';
+ $extended_alert_description = '';
+
+
+ if (!empty($product)) {
+ $product_id = $product;
+ $product = 1;
+
+ $cost = $amount / $product_quantity;
+
+ // Insert into inventory qty times
+ for ($i = 0; $i < $product_quantity; $i++) {
+ if (!is_null($client)) {
+ mysqli_query($mysqli,"INSERT INTO inventory SET inventory_product_id = $product_id, inventory_quantity = 1, inventory_vendor_id = $vendor, inventory_location_id = 1, inventory_cost = $cost, inventory_client_id = $client");
+ } else {
+ mysqli_query($mysqli,"INSERT INTO inventory SET inventory_product_id = $product_id, inventory_quantity = 1, inventory_vendor_id = $vendor, inventory_location_id = 1, inventory_cost = $cost");
+ }
+
+ }
+ $extended_alert_description = '. Product added to inventory';
+ }
+
+
mysqli_query($mysqli,"INSERT INTO expenses SET expense_date = '$date', expense_amount = $amount, expense_currency_code = '$session_company_currency', expense_account_id = $account, expense_vendor_id = $vendor, expense_client_id = $client, expense_category_id = $category, expense_description = '$description', expense_reference = '$reference'");
$expense_id = mysqli_insert_id($mysqli);
// Check for and process attachment
- $extended_alert_description = '';
if ($_FILES['file']['tmp_name'] != '') {
if ($new_file_name = checkFileUpload($_FILES['file'], array('jpg', 'jpeg', 'gif', 'png', 'pdf'))) {
@@ -26,10 +47,10 @@
move_uploaded_file($file_tmp_path, $dest_path);
mysqli_query($mysqli,"UPDATE expenses SET expense_receipt = '$new_file_name' WHERE expense_id = $expense_id");
- $extended_alert_description = '. File successfully uploaded.';
+ $extended_alert_description = $extended_alert_description . '. File successfully uploaded';
} else {
$_SESSION['alert_type'] = "error";
- $extended_alert_description = '. Error uploading file. Check upload directory is writable/correct file type/size';
+ $extended_alert_description = $extended_alert_description . '. Error uploading file. Check upload directory is writable/correct file type/size';
}
}
diff --git a/post/expense_model.php b/post/expense_model.php
index 7224ebcf5..0061a3442 100644
--- a/post/expense_model.php
+++ b/post/expense_model.php
@@ -7,3 +7,5 @@
$category = intval($_POST['category']);
$description = sanitizeInput($_POST['description']);
$reference = sanitizeInput($_POST['reference']);
+$product = intval($_POST['product']);
+$product_quantity = intval($_POST['product_quantity']);
diff --git a/post/inventory.php b/post/inventory.php
new file mode 100644
index 000000000..ac77573ab
--- /dev/null
+++ b/post/inventory.php
@@ -0,0 +1,97 @@
+";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+}
+
+if (isset($_POST['add_inventory_locations'])) {
+
+ validateAdminRole();
+
+ require_once("inventory_model.php");
+
+ // Add Inventory Location
+ $sql = mysqli_query(
+ $mysqli,
+ "INSERT INTO inventory_locations SET inventory_location_name = '$inventory_location_name', inventory_location_description = '$inventory_location_description', inventory_location_address = '$inventory_location_address', inventory_location_city = '$inventory_location_city', inventory_location_state = '$inventory_location_state', inventory_location_zip = '$inventory_location_zip', inventory_location_country = '$inventory_location_country', inventory_location_user_id = $inventory_location_user_id"
+ );
+
+ // Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Inventory', log_action = 'Create', log_description = '$session_name created inventory location $inventory_location_name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
+
+ $_SESSION["alert_message"] = "Inventory location $inventory_location_name added successfully";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+}
+
+if (isset($_POST['edit_inventory_locations'])) {
+
+ validateAdminRole();
+
+ require_once("inventory_model.php");
+
+ // Edit Inventory Location
+ $sql = mysqli_query(
+ $mysqli,
+ "UPDATE inventory_locations SET inventory_location_name = '$inventory_location_name', inventory_location_description = '$inventory_location_description', inventory_location_address = '$inventory_location_address', inventory_location_city = '$inventory_location_city', inventory_location_state = '$inventory_location_state', inventory_location_zip = '$inventory_location_zip', inventory_location_country = '$inventory_location_country', inventory_location_user_id = $inventory_location_user_id WHERE inventory_location_id = $inventory_location_id"
+ );
+
+ // Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Inventory', log_action = 'Edit', log_description = '$session_name edited inventory location $inventory_location_name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
+
+ $_SESSION["alert_message"] = "Inventory location $inventory_location_name edited successfully";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+
+}
+
+if (isset($_GET['archive_inventory_location'])) {
+
+ validateAdminRole();
+
+ $inventory_location_id = intval($_GET['archive_inventory_location']);
+
+ // Archive Inventory Location
+ $sql = mysqli_query(
+ $mysqli,
+ "UPDATE inventory_locations SET inventory_location_archived_at = NOW(), inventory_location_user_id = 0 WHERE inventory_location_id = $inventory_location_id"
+ );
+
+ // Move all inventory to default location
+ $sql = mysqli_query(
+ $mysqli,
+ "UPDATE inventory SET inventory_location_id = 1 WHERE inventory_location_id = $inventory_location_id"
+ );
+ $num_rows = mysqli_affected_rows($mysqli);
+
+ if ($num_rows > 0) {
+ $items_moved_message = ", $num_rows items moved to default location";
+ } else {
+ $items_moved_message = ", No items were in archived location";
+ }
+
+ // Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Inventory', log_action = 'Archive', log_description = '$session_name archived inventory location $inventory_location_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
+
+ $_SESSION["alert_message"] = "Inventory location $inventory_location_id archived successfully$items_moved_message";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+}
\ No newline at end of file
diff --git a/post/inventory_model.php b/post/inventory_model.php
new file mode 100644
index 000000000..1d13eeefa
--- /dev/null
+++ b/post/inventory_model.php
@@ -0,0 +1,16 @@
+ $balance) {
- $_SESSION['alert_message'] = "Payment is more than the balance";
- header("Location: " . $_SERVER["HTTP_REFERER"]);
+ $payment_is_credit = true;
+
+ // Calculate the overpayment amount
+ $credit_amount = $amount - $balance;
+
+ // Set the payment amount to the invoice balance
+ $amount = $balance;
} else {
- mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = $amount, payment_currency_code = '$currency_code', payment_account_id = $account, payment_method = '$payment_method', payment_reference = '$reference', payment_invoice_id = $invoice_id");
+ $payment_is_credit = false;
+ }
- // Get Payment ID for reference
- $payment_id = mysqli_insert_id($mysqli);
- //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 payment_invoice_id = $invoice_id");
- $row = mysqli_fetch_array($sql_total_payments_amount);
- $total_payments_amount = floatval($row['payments_amount']);
+ mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = $amount, payment_currency_code = '$currency_code', payment_account_id = $account, payment_method = '$payment_method', payment_reference = '$reference', payment_invoice_id = $invoice_id");
- //Get the invoice total
- $sql = mysqli_query($mysqli,"SELECT * FROM invoices
- LEFT JOIN clients ON invoice_client_id = client_id
- LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id AND contact_primary = 1
- WHERE invoice_id = $invoice_id"
- );
+ // Get payment ID for reference
+ $payment_id = mysqli_insert_id($mysqli);
- $row = mysqli_fetch_array($sql);
- $invoice_amount = floatval($row['invoice_amount']);
- $invoice_prefix = sanitizeInput($row['invoice_prefix']);
- $invoice_number = intval($row['invoice_number']);
- $invoice_url_key = sanitizeInput($row['invoice_url_key']);
- $invoice_currency_code = sanitizeInput($row['invoice_currency_code']);
- $client_id = intval($row['client_id']);
- $client_name = sanitizeInput($row['client_name']);
- $contact_name = sanitizeInput($row['contact_name']);
- $contact_email = sanitizeInput($row['contact_email']);
- $contact_phone = sanitizeInput(formatPhoneNumber($row['contact_phone']));
- $contact_extension = preg_replace("/[^0-9]/", '',$row['contact_extension']);
- $contact_mobile = sanitizeInput(formatPhoneNumber($row['contact_mobile']));
+ if($payment_is_credit) {
+ //Create a credit for the overpayment
+ mysqli_query($mysqli,"INSERT INTO credits SET credit_amount = $credit_amount, credit_currency_code = '$currency_code', credit_date = '$date', credit_reference = 'Overpayment: $reference', credit_client_id = (SELECT invoice_client_id FROM invoices WHERE invoice_id = $invoice_id), credit_payment_id = $payment_id, credit_account_id = $account");
+ // Get credit ID for reference
+ $credit_id = mysqli_insert_id($mysqli);
+
+ //Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Credit', log_action = 'Create', log_description = 'Credit for Overpayment', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
+ }
- $sql = mysqli_query($mysqli,"SELECT * FROM companies WHERE company_id = 1");
- $row = mysqli_fetch_array($sql);
+ //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 payment_invoice_id = $invoice_id");
+ $row = mysqli_fetch_array($sql_total_payments_amount);
+ $total_payments_amount = floatval($row['payments_amount']);
- $company_name = sanitizeInput($row['company_name']);
- $company_country = sanitizeInput($row['company_country']);
- $company_address = sanitizeInput($row['company_address']);
- $company_city = sanitizeInput($row['company_city']);
- $company_state = sanitizeInput($row['company_state']);
- $company_zip = sanitizeInput($row['company_zip']);
- $company_phone = sanitizeInput(formatPhoneNumber($row['company_phone']));
- $company_email = sanitizeInput($row['company_email']);
- $company_website = sanitizeInput($row['company_website']);
- $company_logo = sanitizeInput($row['company_logo']);
+ //Get the invoice total
+ $sql = mysqli_query($mysqli,"SELECT * FROM invoices
+ LEFT JOIN clients ON invoice_client_id = client_id
+ LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id AND contact_primary = 1
+ WHERE invoice_id = $invoice_id"
+ );
- // Sanitize Config vars from get_settings.php
- $config_invoice_from_name = sanitizeInput($config_invoice_from_name);
- $config_invoice_from_email = sanitizeInput($config_invoice_from_email);
+ $row = mysqli_fetch_array($sql);
+ $invoice_amount = floatval($row['invoice_amount']);
+ $invoice_prefix = sanitizeInput($row['invoice_prefix']);
+ $invoice_number = intval($row['invoice_number']);
+ $invoice_url_key = sanitizeInput($row['invoice_url_key']);
+ $invoice_currency_code = sanitizeInput($row['invoice_currency_code']);
+ $client_id = intval($row['client_id']);
+ $client_name = sanitizeInput($row['client_name']);
+ $contact_name = sanitizeInput($row['contact_name']);
+ $contact_email = sanitizeInput($row['contact_email']);
+ $contact_phone = sanitizeInput(formatPhoneNumber($row['contact_phone']));
+ $contact_extension = preg_replace("/[^0-9]/", '',$row['contact_extension']);
+ $contact_mobile = sanitizeInput(formatPhoneNumber($row['contact_mobile']));
- //Calculate the Invoice balance
- $invoice_balance = $invoice_amount - $total_payments_amount;
+ $sql = mysqli_query($mysqli,"SELECT * FROM companies WHERE company_id = 1");
+ $row = mysqli_fetch_array($sql);
- $email_data = [];
+ $company_name = sanitizeInput($row['company_name']);
+ $company_country = sanitizeInput($row['company_country']);
+ $company_address = sanitizeInput($row['company_address']);
+ $company_city = sanitizeInput($row['company_city']);
+ $company_state = sanitizeInput($row['company_state']);
+ $company_zip = sanitizeInput($row['company_zip']);
+ $company_phone = sanitizeInput(formatPhoneNumber($row['company_phone']));
+ $company_email = sanitizeInput($row['company_email']);
+ $company_website = sanitizeInput($row['company_website']);
+ $company_logo = sanitizeInput($row['company_logo']);
+
+ // Sanitize Config vars from get_settings.php
+ $config_invoice_from_name = sanitizeInput($config_invoice_from_name);
+ $config_invoice_from_email = sanitizeInput($config_invoice_from_email);
- //Determine if invoice has been paid then set the status accordingly
- if ($invoice_balance == 0) {
+ //Calculate the Invoice balance
+ $invoice_balance = $invoice_amount - $total_payments_amount;
+
+ $email_data = [];
+
+ //Determine if invoice has been paid then set the status accordingly
+ if ($invoice_balance == 0) {
- $invoice_status = "Paid";
- if ($email_receipt == 1) {
+ $invoice_status = "Paid";
+
- $subject = "$company_name Payment Received - Invoice $invoice_prefix$invoice_number";
- $body = "Hello $contact_name,
We have received your payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " for invoice
$invoice_prefix$invoice_number . Please keep this email as a receipt for your records.
Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "
Thank you for your business!
--
$company_name - Billing Department
$config_invoice_from_email
$company_phone";
- // Queue Mail
- $email = [
- 'from' => $config_invoice_from_email,
- 'from_name' => $config_invoice_from_name,
- 'recipient' => $contact_email,
- 'recipient_name' => $contact_name,
- 'subject' => $subject,
- 'body' => $body
- ];
+ if ($email_receipt == 1) {
- $email_data[] = $email;
+ $subject = "$company_name Payment Received - Invoice $invoice_prefix$invoice_number";
+ $body = "Hello $contact_name,
We have received your payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " for invoice
$invoice_prefix$invoice_number . Please keep this email as a receipt for your records.
Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "
Thank you for your business!
--
$company_name - Billing Department
$config_invoice_from_email
$company_phone";
+ // Queue Mail
+ $email = [
+ 'from' => $config_invoice_from_email,
+ 'from_name' => $config_invoice_from_name,
+ 'recipient' => $contact_email,
+ 'recipient_name' => $contact_name,
+ 'subject' => $subject,
+ 'body' => $body
+ ];
- // Get Email ID for reference
- $email_id = mysqli_insert_id($mysqli);
+ $email_data[] = $email;
- // Email Logging
+ // Get Email ID for reference
+ $email_id = mysqli_insert_id($mysqli);
- $_SESSION['alert_message'] = "Email receipt sent ";
+ // Email Logging
- mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Emailed Receipt!', history_invoice_id = $invoice_id");
+ $_SESSION['alert_message'] = "Email receipt sent ";
- }
+ mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Emailed Receipt!', history_invoice_id = $invoice_id");
- } else {
+ }
- $invoice_status = "Partial";
+ } else {
- if ($email_receipt == 1) {
+
+ $invoice_status = "Partial";
+
+ if ($email_receipt == 1) {
$subject = "$company_name Partial Payment Received - Invoice $invoice_prefix$invoice_number";
$body = "Hello $contact_name,
We have received partial payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " and it has been applied to invoice
$invoice_prefix$invoice_number . Please keep this email as a receipt for your records.
Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "
Thank you for your business!
~
$company_name - Billing
$config_invoice_from_email
$company_phone";
- // Queue Mail
- $email = [
- 'from' => $config_invoice_from_email,
- 'from_name' => $config_invoice_from_name,
- 'recipient' => $contact_email,
- 'recipient_name' => $contact_name,
- 'subject' => $subject,
- 'body' => $body
- ];
-
- $email_data[] = $email;
- // Get Email ID for reference
- $email_id = mysqli_insert_id($mysqli);
+ // Queue Mail
+ $email = [
+ 'from' => $config_invoice_from_email,
+ 'from_name' => $config_invoice_from_name,
+ 'recipient' => $contact_email,
+ 'recipient_name' => $contact_name,
+ 'subject' => $subject,
+ 'body' => $body
+ ];
- // Email Logging
+ $email_data[] = $email;
- $_SESSION['alert_message'] .= "Email receipt sent ";
+ // Get Email ID for reference
+ $email_id = mysqli_insert_id($mysqli);
- mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Payment Receipt sent to mail queue ID: $email_id!', history_invoice_id = $invoice_id");
+ // Email Logging
- }
+ $_SESSION['alert_message'] .= "Email receipt sent ";
- }
+ mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Payment Receipt sent to mail queue ID: $email_id!', history_invoice_id = $invoice_id");
- // Add emails to queue
- if (!empty($email)) {
- addToMailQueue($mysqli, $email_data);
}
- //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 history SET history_status = '$invoice_status', history_description = 'Payment added', history_invoice_id = $invoice_id");
+ // Add emails to queue
+ if (!empty($email)) {
+ addToMailQueue($mysqli, $email_data);
+ }
- //Logging
- mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = '$payment_amount', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id");
+ //Update Invoice Status
+ mysqli_query($mysqli,"UPDATE invoices SET invoice_status = '$invoice_status' WHERE invoice_id = $invoice_id");
- if ($email_receipt == 1) {
- mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Payment receipt for invoice $invoice_prefix$invoice_number queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id");
- }
+ //Add Payment to History
+ mysqli_query($mysqli,"INSERT INTO history SET history_status = '$invoice_status', history_description = 'Payment added', history_invoice_id = $invoice_id");
- $_SESSION['alert_message'] .= "Payment added";
+ //Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = '$payment_amount', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id");
- header("Location: " . $_SERVER["HTTP_REFERER"]);
+ if ($email_receipt == 1) {
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Payment receipt for invoice $invoice_prefix$invoice_number queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id");
}
+
+ $_SESSION['alert_message'] .= "Payment added";
+
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
}
+
if (isset($_POST['add_bulk_payment'])) {
$client_id = intval($_POST['client_id']);
$date = sanitizeInput($_POST['date']);
$bulk_payment_amount = floatval($_POST['amount']);
$bulk_payment_amount_static = floatval($_POST['amount']);
- $total_account_balance = floatval($_POST['balance']);
+ $total_client_balance = floatval($_POST['balance']);
$account = intval($_POST['account']);
$currency_code = sanitizeInput($_POST['currency_code']);
$payment_method = sanitizeInput($_POST['payment_method']);
@@ -772,11 +796,15 @@
$email_receipt = intval($_POST['email_receipt']);
// Check if bulk_payment_amount exceeds total_account_balance
- if ($bulk_payment_amount > $total_account_balance) {
- $_SESSION['alert_type'] = "error";
- $_SESSION['alert_message'] = "Payment exceeds Client Balance.";
- header("Location: " . $_SERVER["HTTP_REFERER"]);
- exit;
+ if ($bulk_payment_amount > $total_client_balance) {
+ // Create new credit for the overpayment
+ $credit_amount = $bulk_payment_amount - $total_client_balance;
+ $bulk_payment_amount = $total_client_balance;
+
+ // Add Credit
+ $credit_query = "INSERT INTO credits SET credit_amount = $credit_amount, credit_currency_code = '$currency_code', credit_date = '$date', credit_reference = 'Overpayment: $reference', credit_client_id = $client_id, credit_account_id = $account";
+ mysqli_query($mysqli, $credit_query);
+ $credit_id = mysqli_insert_id($mysqli);
}
// Get Invoices
@@ -1428,3 +1456,159 @@
header("Location: post.php?add_ticket_to_invoice=$invoice_id");
}
+
+if (isset($_GET['apply_credit'])) {
+ $credit_id = intval($_GET['apply_credit']);
+
+ $credit_sql = mysqli_query($mysqli,"SELECT * FROM credits WHERE credit_id = $credit_id");
+ $credit_row = mysqli_fetch_array($credit_sql);
+
+ $client_id = intval($credit_row['credit_client_id']);
+ $credit_amount = floatval($credit_row['credit_amount']);
+ $credit_currency_code = sanitizeInput($credit_row['credit_currency_code']);
+
+ $client_balance = getClientBalance($mysqli, $client_id);
+
+ if ($client_balance < $credit_amount) {
+ //create a new credit for the remaining amount
+ $new_credit_amount = $credit_amount - $client_balance;
+ $new_credit_query = "INSERT INTO credits credit_date = CURDATE(), credit_amount = $new_credit_amount, credit_client_id = $client_id, credit_currency_code = '$credit_currency_code', credit_reference = 'Credit Applied'";
+ mysqli_query($mysqli, $new_credit_query);
+ $new_credit_id = mysqli_insert_id($mysqli);
+ }
+ // Delete the original credit
+ mysqli_query($mysqli,"DELETE FROM credits WHERE credit_id = $credit_id");
+
+ // Apply payments similar to add bulk payment
+
+ // Get Invoices
+ $sql_invoices = "SELECT * FROM invoices
+ WHERE invoice_status != 'Draft'
+ AND invoice_status != 'Paid'
+ AND invoice_status != 'Cancelled'
+ AND invoice_client_id = $client_id
+ ORDER BY invoice_number ASC";
+ $result_invoices = mysqli_query($mysqli, $sql_invoices);
+ $invoice_applied_count = 0;
+
+ // Loop Through Each Invoice
+ while ($row = mysqli_fetch_array($result_invoices)) {
+ $invoice_id = intval($row['invoice_id']);
+ $invoice_prefix = sanitizeInput($row['invoice_prefix']);
+ $invoice_number = intval($row['invoice_number']);
+ $invoice_amount = floatval($row['invoice_amount']);
+ $invoice_url_key = sanitizeInput($row['invoice_url_key']);
+ $invoice_balance_query = "SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE payment_invoice_id = $invoice_id";
+ $result_amount_paid = mysqli_query($mysqli, $invoice_balance_query);
+ $row_amount_paid = mysqli_fetch_array($result_amount_paid);
+ $amount_paid = floatval($row_amount_paid['amount_paid']);
+ $invoice_balance = $invoice_amount - $amount_paid;
+
+
+ if ($credit_amount <= 0) {
+ break; // Exit the loop if no credit amount is left
+ }
+
+ if ($invoice_balance <= 0) {
+ continue; // Skip the invoice if it's already paid
+ }
+
+ if ($credit_amount >= $invoice_balance) {
+ $payment_amount = $invoice_balance;
+ $invoice_status = "Paid";
+ } else {
+ $payment_amount = $credit_amount;
+ $invoice_status = "Partial";
+ }
+
+ $invoice_applied_count++;
+
+ // Subtract the payment amount from the credit amount
+ $credit_amount -= $payment_amount;
+
+ // Get Invoice Remain Balance
+ $remaining_invoice_balance = $invoice_balance - $payment_amount;
+
+ // Add Payment
+ $payment_query = "INSERT INTO payments SET payment_date = CURDATE(), payment_amount = $payment_amount, payment_invoice_id = $invoice_id, payment_account_id = 1, payment_currency_code = '{$credit_row['credit_currency_code']}', payment_reference = 'Credit Applied'";
+ mysqli_query($mysqli, $payment_query);
+ $payment_id = mysqli_insert_id($mysqli);
+
+ // Update Invoice Status
+ $update_invoice_query = "UPDATE invoices SET invoice_status = '{$invoice_status}' WHERE invoice_id = {$invoice_id}";
+ mysqli_query($mysqli, $update_invoice_query);
+
+ // Add Payment to History
+ $history_description = "Payment added";
+ $add_history_query = "INSERT INTO history (history_status, history_description, history_invoice_id) VALUES ('{$invoice_status}', '{$history_description}', {$invoice_id})";
+ mysqli_query($mysqli, $add_history_query);
+
+ // Add to Email Body Invoice Portion
+
+ $email_body_invoices .= "
Invoice
$invoice_prefix$invoice_number - Outstanding Amount: " . numfmt_format_currency($currency_format, $invoice_balance, $currency_code) . " - Payment Applied: " . numfmt_format_currency($currency_format, $payment_amount, $currency_code) . " - New Balance: " . numfmt_format_currency($currency_format, $remaining_invoice_balance, $currency_code);
+
+ } // End Invoice Loop
+
+ //Todo add option to send receipts
+ $email_receipt = 1;
+
+ // Send Email
+ if ($email_receipt == 1) {
+
+ // Get Client / Contact Info
+ $sql_client = mysqli_query($mysqli,"SELECT * FROM clients
+ LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id
+ AND contact_primary = 1
+ WHERE client_id = $client_id"
+ );
+
+ $row = mysqli_fetch_array($sql_client);
+ $client_name = sanitizeInput($row['client_name']);
+ $contact_name = sanitizeInput($row['contact_name']);
+ $contact_email = sanitizeInput($row['contact_email']);
+
+ $sql_company = mysqli_query($mysqli,"SELECT company_name, company_phone FROM companies WHERE company_id = 1");
+ $row = mysqli_fetch_array($sql_company);
+
+ $company_name = sanitizeInput($row['company_name']);
+ $company_phone = sanitizeInput(formatPhoneNumber($row['company_phone']));
+
+ // Sanitize Config vars from get_settings.php
+ $config_invoice_from_name = sanitizeInput($config_invoice_from_name);
+ $config_invoice_from_email = sanitizeInput($config_invoice_from_email);
+
+ $subject = "Payment Received - Multiple Invoices";
+ $body = "Hello $contact_name,
Thank you for your payment of " . numfmt_format_currency($currency_format, $bulk_payment_amount_static, $currency_code) . " We\'ve applied your payment to the following invoices, updating their balances accordingly:
$email_body_invoices
We appreciate your continued business!
Sincerely,
$company_name - Billing
$config_invoice_from_email
$company_phone";
+
+ // Queue Mail
+ mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$contact_email', email_recipient_name = '$contact_name', email_from = '$config_invoice_from_email', email_from_name = '$config_invoice_from_name', email_subject = '$subject', email_content = '$body'");
+
+ // Get Email ID for reference
+ $email_id = mysqli_insert_id($mysqli);
+
+ // Email Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Bulk Payment receipt for multiple Invoices queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session");
+
+ $_SESSION['alert_message'] .= "Email receipt sent and ";
+
+ } // End Email
+
+ // Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = 'Bulk Payment of $bulk_payment_amount_static', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id");
+
+ $_SESSION['alert_message'] .= "Credit applied to $credit_applied_count invoices";
+
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+}
+
+if (isset($_GET['delete_credit'])) {
+ $credit_id = intval($_GET['delete_credit']);
+
+ mysqli_query($mysqli,"DELETE FROM credits WHERE credit_id = $credit_id");
+
+ //Logging
+ mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Credit', log_action = 'Delete', log_description = 'Credit $credit_id deleted', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
+
+ $_SESSION['alert_message'] = "Credit deleted";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+}
\ No newline at end of file
diff --git a/post/ticket.php b/post/ticket.php
index e24168bf8..320350aab 100644
--- a/post/ticket.php
+++ b/post/ticket.php
@@ -1252,6 +1252,43 @@
mysqli_query($mysqli, "INSERT INTO invoice_items SET item_name = '$item_name', item_description = '$item_description', item_quantity = $qty, item_price = $price, item_subtotal = $subtotal, item_tax = $tax_amount, item_total = $total, item_order = 1, item_tax_id = $tax_id, item_invoice_id = $invoice_id");
+
+ // Check for products in db and add to invoice
+ $ticket_products_sql = mysqli_query($mysqli, "SELECT * FROM ticket_products WHERE ticket_product_ticket_id = $ticket_id");
+ while ($row = mysqli_fetch_array($ticket_products_sql)) {
+ $product_id = intval($row['ticket_product_product_id']);
+ $product_qty = floatval($row['ticket_product_quantity']);
+
+ $sql = mysqli_query($mysqli, "SELECT * FROM products WHERE product_id = $product_id");
+ $row = mysqli_fetch_array($sql);
+ $product_name = sanitizeInput($row['product_name']);
+ $product_description = sanitizeInput($row['product_description']);
+ $product_price = floatval($row['product_price']);
+ $product_tax_id = intval($row['product_tax_id']);
+
+ $product_subtotal = $product_price * $product_qty;
+
+ if ($product_tax_id > 0) {
+ $sql = mysqli_query($mysqli, "SELECT * FROM taxes WHERE tax_id = $product_tax_id");
+ $row = mysqli_fetch_array($sql);
+ $product_tax_percent = floatval($row['tax_percent']);
+ $product_tax_amount = $product_subtotal * $product_tax_percent / 100;
+ } else {
+ $product_tax_amount = 0;
+ }
+
+ $product_total = $product_subtotal + $product_tax_amount;
+
+ $total = $total + $product_total;
+
+ mysqli_query($mysqli, "INSERT INTO invoice_items SET item_name = '$product_name', item_description = '$product_description', item_quantity = $product_qty, item_price = $product_price, item_subtotal = $product_subtotal, item_tax = $product_tax_amount, item_total = $product_total, item_order = 1, item_tax_id = $product_tax_id, item_invoice_id = $invoice_id");
+
+ // Add product to string for internal note
+ $product_ticket_note .= ".
$product_qty x $product_name added to invoice";
+ }
+
+
+
//Update Invoice Balances
$sql = mysqli_query($mysqli, "SELECT * FROM invoices WHERE invoice_id = $invoice_id");
@@ -1264,9 +1301,10 @@
mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Draft', history_description = 'Invoice created from Ticket $ticket_prefix$ticket_number', history_invoice_id = $invoice_id");
// Add internal note to ticket, and link to invoice in database
- mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Created invoice
$config_invoice_prefix$invoice_number for this ticket.', ticket_reply_type = 'Internal', ticket_reply_time_worked = '00:01:00', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $ticket_id");
+ mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Created invoice
$config_invoice_prefix$invoice_number for this ticket$product_ticket_note.', ticket_reply_type = 'Internal', ticket_reply_time_worked = '00:01:00', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $ticket_id");
mysqli_query($mysqli, "UPDATE tickets SET ticket_invoice_id = $invoice_id WHERE ticket_id = $ticket_id");
+
// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Invoice', log_action = 'Create', log_description = '$config_invoice_prefix$invoice_number created from Ticket $ticket_prefix$ticket_number', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
@@ -1764,3 +1802,90 @@
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
+
+
+if (isset($_POST['add_ticket_products'])) {
+
+ validateTechRole();
+
+ $ticket_id = intval($_POST['ticket_id']);
+ $product_id = intval($_POST['product_id']);
+ $qty = intval($_POST['quantity']);
+
+ //find user inventory location
+ $sql = mysqli_query($mysqli, "SELECT * FROM inventory_locations WHERE inventory_location_user_id = $session_user_id");
+ $num_rows = mysqli_num_rows($sql);
+
+ if ($num_rows == 1) {
+ $row = mysqli_fetch_array($sql);
+ $session_location_id = intval($row['inventory_location_id']);
+ } elseif ($num_rows > 1) {
+ $_SESSION['alert_type'] = "error";
+ $_SESSION['alert_message'] = "You have more than one inventory location set. Please contact your administrator";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+ exit;
+ } else {
+ $_SESSION['alert_type'] = "error";
+ $_SESSION['alert_message'] = "You do not have an inventory location set. Please contact your administrator";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+ exit;
+ }
+
+ //check qty against inventory
+ $sql = mysqli_query($mysqli, "SELECT SUM(inventory_quantity) as inventory_quantity FROM inventory WHERE inventory_product_id = $product_id AND inventory_location_id = $session_location_id GROUP BY inventory_product_id, inventory_location_id;");
+ $row = mysqli_fetch_array($sql);
+ $inventory_qty = intval($row['inventory_quantity']);
+ if ($qty > $inventory_qty) {
+ $_SESSION['alert_type'] = "error";
+ $_SESSION['alert_message'] = "You do not have enough inventory to add that quantity, QTY: $qty, Inventory: $inventory_qty in location $session_location_id, $num_rows rows found.";
+ header("Location: " . $_SERVER["HTTP_REFERER"]);
+ exit;
+ }
+
+
+
+ // Add to DB
+ mysqli_query($mysqli, "INSERT INTO ticket_products SET ticket_product_ticket_id = $ticket_id, ticket_product_product_id = $product_id, ticket_product_quantity = $qty");
+
+ // Delete one item per qty
+ mysqli_query($mysqli, "UPDATE inventory SET inventory_quantity = inventory_quantity - 1 WHERE inventory_product_id = $product_id AND inventory_location_id = $session_location_id AND inventory_quantity > 0 LIMIT $qty");
+
+ // Logging
+ mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Modify', log_description = '$session_name added product to ticket', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $ticket_id");
+
+ $_SESSION['alert_message'] = "Product added to ticket";
+ header("Location: ". $_SERVER["HTTP_REFERER"]);
+
+}
+
+if (isset($_GET['delete_ticket_product'])) {
+
+ validateTechRole();
+
+ $ticket_product_id = intval($_GET['delete_ticket_product']);
+ $ticket_id = intval($_GET['ticket_id']);
+
+ // Get product ID
+ $sql = mysqli_query($mysqli, "SELECT * FROM ticket_products WHERE ticket_product_id = $ticket_product_id");
+ $row = mysqli_fetch_array($sql);
+ $product_id = intval($row['ticket_product_product_id']);
+ $qty = intval($row['ticket_product_quantity']);
+
+ // Delete
+ mysqli_query($mysqli, "DELETE FROM ticket_products WHERE ticket_product_id = $ticket_product_id");
+
+ //find user's inventory location
+ $sql = mysqli_query($mysqli, "SELECT * FROM inventory_locations WHERE inventory_location_user_id = $session_user_id");
+ $row = mysqli_fetch_array($sql);
+ $session_location_id = intval($row['inventory_location_id']);
+
+ // Restore inventory quantity
+ mysqli_query($mysqli, "UPDATE inventory SET inventory_quantity = inventory_quantity + 1 WHERE inventory_product_id = $product_id AND inventory_location_id = $session_location_id AND inventory_quantity = 0 LIMIT $qty");
+
+ // Logging
+ mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Modify', log_description = '$session_name deleted product from ticket', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $ticket_id");
+
+ $_SESSION['alert_message'] = "Product removed from ticket. Please see administrator to return inventory";
+ header("Location: ". $_SERVER["HTTP_REFERER"]);
+
+}
diff --git a/products.php b/products.php
index f8cd081ef..d3d2c7a69 100644
--- a/products.php
+++ b/products.php
@@ -43,6 +43,11 @@