mirror of
https://github.com/itflow-org/itflow
synced 2026-08-05 07:07:14 +00:00
Add Export CSV to the new combined income page
This commit is contained in:
@@ -146,13 +146,27 @@ $summary_total_income = floatval($row['total_income']);
|
||||
<div class="card card-dark">
|
||||
<div class="card-header py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-hand-holding-usd mr-2"></i>Income</h3>
|
||||
<?php if (lookupUserPermission("module_sales") >= 2) { ?>
|
||||
<div class="card-tools">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/revenue/revenue_add.php" data-modal-size="lg">
|
||||
<i class="fas fa-plus mr-2"></i>New Revenue
|
||||
</button>
|
||||
<?php if (lookupUserPermission("module_sales") >= 2) { ?>
|
||||
<div class="btn-group">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/revenue/revenue_add.php" data-modal-size="lg">
|
||||
<i class="fas fa-plus mr-2"></i>New Revenue
|
||||
</button>
|
||||
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown"></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item text-dark ajax-modal" href="#"
|
||||
data-modal-url="modals/income/income_export.php?<?php echo $client_url; ?>type=<?php echo urlencode($type_filter); ?>&account=<?php echo $account_filter; ?>&method=<?php echo urlencode($_GET['method'] ?? ''); ?>&dtf=<?php echo $dtf; ?>&dtt=<?php echo $dtt; ?>&q=<?php echo urlencode($q ?? ''); ?>">
|
||||
<i class="fa fa-fw fa-download mr-2"></i>Export
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<?php } else { ?>
|
||||
<button type="button" class="btn btn-default ajax-modal"
|
||||
data-modal-url="modals/income/income_export.php?<?php echo $client_url; ?>type=<?php echo urlencode($type_filter); ?>&account=<?php echo $account_filter; ?>&method=<?php echo urlencode($_GET['method'] ?? ''); ?>&dtf=<?php echo $dtf; ?>&dtt=<?php echo $dtt; ?>&q=<?php echo urlencode($q ?? ''); ?>">
|
||||
<i class="fa fa-fw fa-download mr-2"></i>Export
|
||||
</button>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
|
||||
174
agent/modals/income/income_export.php
Normal file
174
agent/modals/income/income_export.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
require_once '../../../includes/modal_header.php';
|
||||
|
||||
// Pre-fill from the income page's current filters
|
||||
|
||||
// Client - carried through from the client-scoped Income page
|
||||
$client_id = intval($_GET['client_id'] ?? 0);
|
||||
|
||||
// Type Filter
|
||||
$income_types_array = ['Payment', 'Revenue'];
|
||||
if (isset($_GET['type']) && !empty($_GET['type']) && in_array($_GET['type'], $income_types_array)) {
|
||||
$type_filter = $_GET['type'];
|
||||
} else {
|
||||
$type_filter = '';
|
||||
}
|
||||
|
||||
// Account Filter
|
||||
if (isset($_GET['account']) && !empty($_GET['account'])) {
|
||||
$account_filter = intval($_GET['account']);
|
||||
} else {
|
||||
$account_filter = '';
|
||||
}
|
||||
|
||||
// Payment Method Filter
|
||||
if (isset($_GET['method']) && !empty($_GET['method'])) {
|
||||
$method_filter = escapeHtml($_GET['method']);
|
||||
} else {
|
||||
$method_filter = '';
|
||||
}
|
||||
|
||||
// Search Filter
|
||||
if (isset($_GET['q']) && !empty($_GET['q'])) {
|
||||
$q_filter = $_GET['q'];
|
||||
} else {
|
||||
$q_filter = '';
|
||||
}
|
||||
|
||||
// Date Filter - ignore the all-time defaults so the date fields stay clean
|
||||
if (isset($_GET['dtf']) && !empty($_GET['dtf']) && $_GET['dtf'] !== '1970-01-01') {
|
||||
$date_from_filter = escapeHtml($_GET['dtf']);
|
||||
} else {
|
||||
$date_from_filter = '';
|
||||
}
|
||||
if (isset($_GET['dtt']) && !empty($_GET['dtt']) && $_GET['dtt'] !== '2099-12-31') {
|
||||
$date_to_filter = escapeHtml($_GET['dtt']);
|
||||
} else {
|
||||
$date_to_filter = '';
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fa fa-fw fa-download mr-2"></i>Exporting Income to CSV</h5>
|
||||
<button type="button" class="close text-white" data-dismiss="modal">
|
||||
<span>×</span>
|
||||
</button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="client_id" value="<?= $client_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<div class="form-group">
|
||||
<label>Search</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-search"></i></span>
|
||||
</div>
|
||||
<input type="text" class="form-control" name="q" value="<?= stripslashes(escapeHtml($q_filter)) ?>" placeholder="Source, client, account, method, reference or amount">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Type</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-hand-holding-usd"></i></span>
|
||||
</div>
|
||||
<select class="form-control select2" name="type">
|
||||
<option value="">- All Types -</option>
|
||||
|
||||
<?php foreach ($income_types_array as $income_type_option) { ?>
|
||||
<option <?php if ($type_filter == $income_type_option) { echo "selected"; } ?> value="<?= $income_type_option ?>"><?= $income_type_option ?></option>
|
||||
<?php } ?>
|
||||
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Account</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-piggy-bank"></i></span>
|
||||
</div>
|
||||
<select class="form-control select2" name="account">
|
||||
<option value="">- All Accounts -</option>
|
||||
|
||||
<?php
|
||||
$sql_accounts_filter = mysqli_query($mysqli, "SELECT account_id, account_name FROM accounts
|
||||
WHERE EXISTS (SELECT 1 FROM payments WHERE payment_account_id = account_id)
|
||||
OR EXISTS (SELECT 1 FROM revenues WHERE revenue_account_id = account_id)
|
||||
ORDER BY account_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql_accounts_filter)) {
|
||||
$filter_account_id = intval($row['account_id']);
|
||||
$filter_account_name = escapeHtml($row['account_name']);
|
||||
?>
|
||||
<option <?php if ($account_filter == $filter_account_id) { echo "selected"; } ?> value="<?= $filter_account_id ?>"><?= $filter_account_name ?></option>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Payment Method</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-credit-card"></i></span>
|
||||
</div>
|
||||
<select class="form-control select2" name="method">
|
||||
<option value="">- All Payment Methods -</option>
|
||||
|
||||
<?php
|
||||
$sql_payment_methods_filter = mysqli_query($mysqli, "SELECT DISTINCT payment_method AS method FROM payments WHERE payment_method != ''
|
||||
UNION SELECT DISTINCT revenue_payment_method FROM revenues WHERE revenue_payment_method != ''
|
||||
ORDER BY method ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql_payment_methods_filter)) {
|
||||
$filter_method = escapeHtml($row['method']);
|
||||
?>
|
||||
<option <?php if ($method_filter == $filter_method) { echo "selected"; } ?> value="<?= $filter_method ?>"><?= $filter_method ?></option>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Date From</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
|
||||
</div>
|
||||
<input type="date" class="form-control" name="date_from" value="<?= $date_from_filter ?>" max="2999-12-31">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Date To</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
|
||||
</div>
|
||||
<input type="date" class="form-control" name="date_to" value="<?= $date_to_filter ?>" max="2999-12-31">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="export_income_csv" class="btn btn-primary text-bold"><i class="fas fa-fw fa-download mr-2"></i>Download CSV</button>
|
||||
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
175
agent/post/income.php
Normal file
175
agent/post/income.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Income POST request handler
|
||||
*/
|
||||
|
||||
if (!defined('FROM_POST_HANDLER')) {
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
exit;
|
||||
}
|
||||
|
||||
if (isset($_POST['export_income_csv'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_financial');
|
||||
|
||||
$date_from = escapeSql($_POST['date_from']);
|
||||
$date_to = escapeSql($_POST['date_to']);
|
||||
$account = intval($_POST['account']);
|
||||
|
||||
// Client Filter - carried over from the client-scoped Income page
|
||||
$client_id = intval($_POST['client_id'] ?? 0);
|
||||
if ($client_id) {
|
||||
enforceClientAccess();
|
||||
$payment_client_query = "AND invoice_client_id = $client_id";
|
||||
$revenue_client_query = "AND revenue_client_id = $client_id";
|
||||
$client_name = getFieldById('clients', $client_id, 'client_name');
|
||||
$file_name_prepend = "$client_name-";
|
||||
} else {
|
||||
// Default - any
|
||||
$payment_client_query = '';
|
||||
$revenue_client_query = '';
|
||||
$file_name_prepend = "$session_company_name-";
|
||||
}
|
||||
|
||||
// Type Filter
|
||||
$income_types_array = ['Payment', 'Revenue'];
|
||||
if (!empty($_POST['type']) && in_array($_POST['type'], $income_types_array)) {
|
||||
$type_query = "AND (income_type = '" . escapeSql($_POST['type']) . "')";
|
||||
} else {
|
||||
// Default - any
|
||||
$type_query = '';
|
||||
}
|
||||
|
||||
// Account Filter
|
||||
if ($account) {
|
||||
$account_query = "AND (income_account_id = $account)";
|
||||
} else {
|
||||
// Default - any
|
||||
$account_query = '';
|
||||
}
|
||||
|
||||
// Payment Method Filter
|
||||
if (!empty($_POST['method'])) {
|
||||
$method_query = "AND (income_method = '" . escapeSql($_POST['method']) . "')";
|
||||
} else {
|
||||
// Default - any
|
||||
$method_query = '';
|
||||
}
|
||||
|
||||
// Search Filter - mirrors the income page search box
|
||||
$q = escapeSql($_POST['q']);
|
||||
if (!empty($q)) {
|
||||
$search_query = "AND (income_source LIKE '%$q%' OR income_client LIKE '%$q%' OR income_account LIKE '%$q%' OR income_method LIKE '%$q%' OR income_reference LIKE '%$q%' OR income_amount LIKE '%$q%')";
|
||||
} else {
|
||||
// Default - any
|
||||
$search_query = '';
|
||||
}
|
||||
|
||||
// Date Filter
|
||||
if (!empty($date_from) && !empty($date_to)) {
|
||||
$date_query = "AND DATE(income_date) BETWEEN '$date_from' AND '$date_to'";
|
||||
} else {
|
||||
$date_query = '';
|
||||
}
|
||||
|
||||
// Same union as income.php - payments applied to an invoice, and standalone revenues.
|
||||
// Transfers between accounts are stored as a linked expense + revenue pair, so the revenue leg
|
||||
// is excluded here (transfer_id IS NULL) - moving your own money is not income.
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT * FROM (
|
||||
SELECT
|
||||
'Payment' AS income_type,
|
||||
payment_id AS income_id,
|
||||
payment_date AS income_date,
|
||||
payment_created_at AS income_created_at,
|
||||
CONCAT(invoice_prefix, invoice_number) AS income_source,
|
||||
NULL AS income_description,
|
||||
client_name AS income_client,
|
||||
payment_amount AS income_amount,
|
||||
payment_currency_code AS income_currency_code,
|
||||
payment_method AS income_method,
|
||||
payment_reference AS income_reference,
|
||||
payment_account_id AS income_account_id,
|
||||
account_name AS income_account
|
||||
FROM payments
|
||||
LEFT JOIN invoices ON payment_invoice_id = invoice_id
|
||||
LEFT JOIN clients ON invoice_client_id = client_id
|
||||
LEFT JOIN accounts ON payment_account_id = account_id
|
||||
WHERE payment_archived_at IS NULL
|
||||
$payment_client_query
|
||||
$access_permission_query
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
'Revenue',
|
||||
revenue_id,
|
||||
revenue_date,
|
||||
revenue_created_at,
|
||||
category_name,
|
||||
revenue_description,
|
||||
client_name,
|
||||
revenue_amount,
|
||||
revenue_currency_code,
|
||||
revenue_payment_method,
|
||||
revenue_reference,
|
||||
revenue_account_id,
|
||||
account_name
|
||||
FROM revenues
|
||||
LEFT JOIN categories ON revenue_category_id = category_id
|
||||
LEFT JOIN clients ON revenue_client_id = client_id
|
||||
LEFT JOIN accounts ON revenue_account_id = account_id
|
||||
LEFT JOIN transfers ON transfer_revenue_id = revenue_id
|
||||
WHERE revenue_archived_at IS NULL
|
||||
AND transfer_id IS NULL
|
||||
$revenue_client_query
|
||||
) AS income
|
||||
WHERE 1 = 1
|
||||
$date_query
|
||||
$type_query
|
||||
$account_query
|
||||
$method_query
|
||||
$search_query
|
||||
ORDER BY income_date ASC, income_created_at ASC, income_type ASC, income_id ASC"
|
||||
);
|
||||
|
||||
$num_rows = mysqli_num_rows($sql);
|
||||
if ($num_rows > 0) {
|
||||
$delimiter = ",";
|
||||
$enclosure = '"';
|
||||
$escape = '\\'; // backslash
|
||||
$filename = sanitizeFilename($file_name_prepend . "Income-" . date('Y-m-d_H-i-s') . ".csv");
|
||||
|
||||
//create a file pointer
|
||||
$f = fopen('php://memory', 'w');
|
||||
|
||||
//set column headers
|
||||
$fields = array('Date', 'Type', 'Source', 'Description', 'Client', 'Amount', 'Currency', 'Payment Method', 'Reference', 'Account');
|
||||
fputcsv($f, $fields, $delimiter, $enclosure, $escape);
|
||||
|
||||
//output each row of the data, format line as csv and write to file pointer
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$lineData = array($row['income_date'], $row['income_type'], $row['income_source'], $row['income_description'], $row['income_client'], $row['income_amount'], $row['income_currency_code'], $row['income_method'], $row['income_reference'], $row['income_account']);
|
||||
fputcsv($f, array_map('escapeCsvFormula', $lineData), $delimiter, $enclosure, $escape);
|
||||
}
|
||||
|
||||
//move back to beginning of file
|
||||
fseek($f, 0);
|
||||
|
||||
//set headers to download file rather than displayed
|
||||
header('Content-Type: text/csv');
|
||||
header('Content-Disposition: attachment; filename="' . $filename . '";');
|
||||
|
||||
//output all remaining data on a file pointer
|
||||
fpassthru($f);
|
||||
}
|
||||
|
||||
logAudit("Income", "Export", "$session_name exported $num_rows income record(s) to CSV file");
|
||||
|
||||
exit;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user