<?php
include_once("../connect.php");
global $con;
include_once("../session.php");


$accountingSwitchDate = "2017-11-01";

$function = isset($_REQUEST['f'])?$_REQUEST['f']:null;
if($function != null){
    if($function == "downloadSalesTaxReport"){
        downloadSalesTaxReport($_REQUEST['fdate'],$_REQUEST['tdate'], $_REQUEST['st']);
    }elseif($function == "downloadPaymentsTaxReport"){
        downloadPaymentsTaxReport($_REQUEST['fdate'],$_REQUEST['tdate'], $_REQUEST['st']);
    }
}

function downloadSalesTaxReport($fromDate, $toDate){
    global $con;
    
    $fileName = "SalesTaxDetails-$fromDate--$toDate.csv";
    $filePath = "downloads/";
    
    $fp = fopen($filePath . $fileName, 'w');
    
    $query = "SELECT InvoiceID, InvoiceDate,AccountNumber, Name, INVOICE_HEAD.Salesman, Subtotal, Tax1Total, Tax2Total, Tax1Total+Tax2Total as TotalTax, Subtotal+Tax1Total+Tax2Total as InvoiceTotal FROM `INVOICE_HEAD` LEFT JOIN CUSTOMER USING(AccountNumber) WHERE InvoiceDate BETWEEN '" . $fromDate . "' AND '" . $toDate . " 23:59:59' GROUP BY InvoiceID";
    $result = mysqli_query($con, $query);
    $title = array("InvoiceID", "InvoiceDate", "AccountNumber", "CustomerName", "Salesman", "Subtotal", "Tax1", "Tax2", "Total Tax", "TotalInvoice");
    fputcsv($fp, $title,",", '"');
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        fputcsv($fp,$row,",", '"');
    }
    fclose($fp);
    header('Content-type: text/csv'); 
    header('Content-disposition: attachment; filename="'.$fileName .'"');                             
    readfile($filePath . $fileName );          
    exit();
}
function downloadPaymentsTaxReport($fromDate, $toDate){
    global $con;
    $fileName = "PaymentsTaxDetails-$fromDate--$toDate.csv";
    $filePath = "downloads/";
    $fp = fopen($filePath . $fileName, 'w');
    $query = "SELECT PaymentID, InvoiceID, AccountNumber,Name,INVOICE_HEAD.Salesman,PaymentDate, INVOICE_HEAD.InvoiceDate, ROUND(SUM(PaymentAmount),2) as TotalPayment, ROUND(SUM(PaymentAmount),2)-sum(IF(PartialPayment, 0, TaxAmount)) TotalSales, ROUND(SUM(DiscountAmount),2) as TotalDiscount, sum(IF(PartialPayment, 0, TaxAmount)) as TotalTax, SUM(IF(PartialPayment,0,Tax1Total)) Tax1Total, SUM(IF(PartialPayment,0,Tax2Total)) Tax2Total FROM DETAILED_PAYMENT LEFT JOIN INVOICE_HEAD USING(InvoiceID, AccountNumber) LEFT JOIN CUSTOMER USING(AccountNumber) WHERE PaymentDate BETWEEN '$fromDate' AND '$toDate 23:59:59' AND DETAILED_PAYMENT.Void = 0 GROUP BY InvoiceID;";
    
    $result = mysqli_query($con, $query);
    
    $title = array("PaymentID", "InvoiceID", "AccountNumber", "CustomerName", "Salesman", "PaymentDate", "InvoiceDate","TotalPayment", "TotalSales", "TotalDiscount", "TotalTax", "Tax1Total", "Tax2Total");
    fputcsv($fp, $title,",", '"');
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        fputcsv($fp,$row,",", '"');
    }
    fclose($fp);
    header('Content-type: text/csv'); 
    header('Content-disposition: attachment; filename="'.$fileName .'"');                             
    readfile($filePath . $fileName );          
    exit();

    
}

?>