File: /var/www/grapossconnect/admin-dash/api/invoice-excel.php
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
require_once('../_dbconfig.php');
global $wpdb;
$curDate = date('Y-m-d');
//session_start();
// check session for subquery execution
if ($_SESSION['date-filter']) {
$subquery = $_SESSION["date-filter"];
} else {
$subquery = " MONTH(transaction_date) = MONTH(now()) AND YEAR(transaction_date) = YEAR(now())";
}
if (isset($_SESSION['get-table'])) {
$show_table = $_SESSION["get-table"];
} else{
$show_table = " grp_gepl_invoice";
}
$csv_output = 'S.no, Invoice ID, Vendor, Payment For, Domain Name, Transaction Type, Year Extn, Amount,OrderID, Transaction Id,Payment Type, GSTin, Gst Name, Name, State, Transaction Date, Issue Date, Status';
$csv_output .= "\n";
$getDataQuery = "SELECT invoice_id, from_domain, if_vendor, transaction_type, payment_for, payment_type, order_id, product_mrp, gepl_amount, net_amount, customer_name, customer_gst_name, customer_gstin, customer_state_name, transaction_id1, transaction_date, invoice_issue_date, year_extn, invoice_status, last_update_date FROM $show_table WHERE $subquery ORDER BY invoice_id DESC";
$getData = $conn->query($getDataQuery);
$i = 1;
if ($getData && $getData->num_rows > 0) {
foreach ($getData as $vd) {
if ($vd['invoice_status'] == 1) {
$status = "Success";
} else {
$status = "Cancelled";
}
$csv_output .= $i . ',';
$csv_output .= $vd['invoice_id'] . ',';
$csv_output .= $vd['if_vendor'] . ',';
$csv_output .= str_replace( ',', '-', $vd['payment_for']). ',';
$csv_output .= $vd['from_domain'] . ',';
$csv_output .= str_replace( ',', '|', $vd['transaction_type']) . ',';
$csv_output .= $vd['year_extn'] . ',';
$csv_output .= $vd['product_mrp'] . ',';
$csv_output .= str_replace( ',', '-', $vd['order_id']). ',';
$csv_output .= "`".$vd['transaction_id1'] . ',';
$csv_output .= $vd['payment_type'] . ',';
$csv_output .= $vd['customer_gstin'] . ',';
$csv_output .= $vd['customer_gst_name'] . ',';
$csv_output .= $vd['customer_name'] . ',';
$csv_output .= $vd['customer_state_name'] . ',';
$csv_output .= $vd['transaction_date'] . ',';
$csv_output .= $vd['invoice_issue_date'] . ',';
$csv_output .= $status . ',';
$csv_output .= "\n";
$i++;
}
} else {
$csv_output = "No Record Found!";
}
$filename = "Invoice-Detail";
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=" . $filename . ".csv");
print $csv_output;
exit;