File: /var/www/unbox/uc-live/admin-dash/invoice-excel.php
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
include_once('../wp-config.php');
global $wpdb;
$curDate = date('Y-m-d');
session_start();
//show data as per selected date
if ($_SESSION['date-filter']) {
$subquery = $_SESSION["date-filter"];
} else {
$subquery = " AND MONTH(transaction_date) = MONTH(now()) AND YEAR(transaction_date) = YEAR(now())";
}
/// get current finacial details
if (date('m') < 4) { //Upto April
$currentYear = date('Y');
} else { //After April
$currentYear = (date('Y') + 1);
}
if ($_SESSION['fy-filter']) {
$year_filter = $_SESSION["fy-filter"];
$years = (explode("_", $year_filter));
$last_year = $years[0];
$present_year = $years[1];
if ($present_year == $currentYear) {
$subTable = "unbox_invoice";
} else {
$subTable = 'unbox_invoice_' . $year_filter;
}
} else {
$subTable = "unbox_invoice";
}
$csv_output = 'S.no, Invoice Id, Domain, Vendor, Transaction Type,Payment For,Payment Type,Order Id,Product MRP,GST Rate,IGST Amount, CGST Amount,SGST Amount,Taxable Amount, Unbox Amount, Discount Amount, Net Amount, Customer Name, Customer Address, Customer Mobile, Customer Email, GST Name, GSTin, State ID, State Code, State Name, Transaction Id1, Transaction Id2, Transaction Date, Invoice Issue Date, Year Extension, Invoice Status, Cancelled Reason(if any), Reference Name1, Reference ID1, Reference Name2, Reference ID2, Reference Name3, Reference ID3 ';
$csv_output .= "\n";
$getDataQuery = "SELECT invoice_id, from_domain, if_vendor, transaction_type, payment_for, payment_type, order_id, product_mrp, gst_rate, igst_amount, cgst_amount, sgst_amount, taxable_amount, unbox_amount, discount_amount, net_amount, customer_name, customer_address, customer_mobile, customer_email, customer_gst_name, customer_gstin, customer_state_id, customer_state_code, customer_state_name, transaction_id1, transaction_id2, transaction_date, invoice_issue_date,year_extn,invoice_status,system_datetime, cancelled_reason_if_any, cancelled_datetime, ref_name1, ref_id1, ref_name2, ref_id2, ref_name3, ref_id3, last_update_date FROM $subTable WHERE invoice_id !='' $subquery ORDER BY invoice_id desc";
$getData = $wpdb->get_results($getDataQuery);
$searches = array("\r", "\n", "\r\n", ",");
$i = 1;
if (sizeof($getData) > 0) {
foreach ($getData as $vd) {
$csv_output .= $i . ',';
$csv_output .= $vd->invoice_id . ',';
$csv_output .= $vd->from_domain . ',';
$csv_output .= $vd->if_vendor . ',';
$csv_output .= $vd->transaction_type . ',';
$csv_output .= $vd->payment_for . ',';
$csv_output .= str_replace($searches,' ',$vd->payment_type) . ',';
$csv_output .= $vd->order_id . ',';
$csv_output .= $vd->product_mrp . ',';
$csv_output .= $vd->gst_rate . ',';
$csv_output .= $vd->igst_amount . ',';
$csv_output .= $vd->cgst_amount . ',';
$csv_output .= $vd->sgst_amount . ',';
$csv_output .= $vd->taxable_amount . ',';
$csv_output .= $vd->unbox_amount . ',';
$csv_output .= $vd->discount_amount . ',';
$csv_output .= $vd->net_amount . ',';
$csv_output .= str_replace($searches,' ',$vd->customer_name) . ',';
$csv_output .= str_replace($searches,' ',$vd->customer_address) . ',';
$csv_output .= $vd->customer_mobile . ',';
$csv_output .= $vd->customer_email . ',';
$csv_output .= $vd->customer_gst_name . ',';
$csv_output .= $vd->customer_gstin . ',';
$csv_output .= $vd->customer_state_id . ',';
$csv_output .= $vd->customer_state_code . ',';
$csv_output .= $vd->customer_state_name . ',';
$csv_output .= $vd->transaction_id1 . ',';
$csv_output .= $vd->transaction_id2 . ',';
$csv_output .= $vd->transaction_date . ',';
$csv_output .= $vd->invoice_issue_date . ',';
$csv_output .= $vd->year_extn . ',';
$csv_output .= $vd->invoice_status . ',';
$csv_output .= str_replace($searches,' ',$vd->cancelled_reason_if_any) . ',';
$csv_output .= $vd->ref_name1 . ',';
$csv_output .= $vd->ref_id1 . ',';
$csv_output .= $vd->ref_name2 . ',';
$csv_output .= $vd->ref_id2 . ',';
$csv_output .= $vd->ref_name3 . ',';
$csv_output .= $vd->ref_id3 . ',';
$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;