<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
include ('../inc/repfunctions.php');
include ('../inc/invfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
$today = strftime("%Y-%m-%d", time());    
$dtnow = date("Y-m-d h:i:s", time());    
}else{
header('Location:../index.php');
exit;    
}

if(isset($_POST['expselcsv'])){
    $fdate=$_POST['fdate'];
    $tdate=$_POST['tdate'];
    $output='';
    $sql="SELECT id,invno,type,cusid,name,subtot,totdis,vatamo,aitamo,otamo,freight,less,total,ref,apdate FROM tbl_sales WHERE apdate BETWEEN '$fdate' AND '$tdate' ORDER BY apdate,id ASC";
    $result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $output.='<tr><th>SN</th><th>Date</th><th>Invoice</th><th>Vendor</th><th>MRP</th><th>DIS</th><th>VAT</th><th>AIT</th><th>OTH</th><th>FRE</th>    <th>LES</th><th>Total</th></tr> ';
    while($row = mysqli_fetch_array($result)){
        $mrp=($row['subtot']+$row['totdis']);
        if($row['type']!='CU'){ $customer=get_fild_data('tbl_supplier',$row['cusid'],'name');}else{if($row['cusid']!='0'){$customer= get_fild_data('tbl_customer',$row['cusid'],'name');}else{$customer=$row['name'];}}
    $output.='<tr><td>'.count_id().'</td><td>'.date("d M Y", strtotime($row['apdate'])).'</td><td>'.$row['invno'].'</td><td>'.$customer.'</td><td>'.numtolocal($mrp,'').'</td><td>'.numtolocal($row['totdis'],'').'</td><td>'.numtolocal($row['vatamo'],'').'</td><td>'.numtolocal($row['aitamo'],'').'</td><td>'.numtolocal($row['otamo'],'').'</td><td>'.numtolocal($row['freight'],'').'</td><td>'.numtolocal($row['less'],'').'</td><td>'.numtolocal($row['total'],'').'</td></tr>';	
    }
    // encoding array to json format
    echo $output;
}

if(isset($_POST['exppurcsv'])){
    $fdate=$_POST['fdate'];
    $tdate=$_POST['tdate'];
    $output='';
    $sql="SELECT id,invno,type,supid,subtot,totdis,vatamo,otamo,freight,less,total,ref,apdate FROM tbl_purchase WHERE apdate BETWEEN '$fdate' AND '$tdate' ORDER BY apdate,id ASC";
    $result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $output.='<tr><th>SN</th><th>Date</th><th>Invoice</th><th>Customer</th><th>MRP</th><th>DIS</th><th>VAT</th><th>OTH</th><th>FRE</th><th>LES</th><th>Total</th></tr> ';
    while($row = mysqli_fetch_array($result)){
        $mrp=($row['subtot']+$row['totdis']);
        if($row['type']!='CU'){ $customer=get_fild_data('tbl_supplier',$row['supid'],'name');}else{if($row['supid']!='0'){$customer= get_fild_data('tbl_customer',$row['supid'],'name');}}
    $output.='<tr><td>'.count_id().'</td><td>'.date("d M Y", strtotime($row['apdate'])).'</td><td>'.$row['invno'].'</td><td>'.$customer.'</td><td>'.numtolocal($mrp,'').'</td><td>'.numtolocal($row['totdis'],'').'</td><td>'.numtolocal($row['vatamo'],'').'</td><td>'.numtolocal($row['otamo'],'').'</td><td>'.numtolocal($row['freight'],'').'</td><td>'.numtolocal($row['less'],'').'</td><td>'.numtolocal($row['total'],'').'</td></tr>';	
    }
    // encoding array to json format
    echo $output;
}


if(isset($_POST['expinvcsv'])){
    $fdate=$_POST['fdate'];
    $tdate=$_POST['tdate'];
    $output='';
    $sql="SELECT pid,name,code as pcode,colid,sizid,SUM(p_in) AS poqty,SUM(p_out) AS soqty,cost FROM tbl_traproduct LEFT JOIN (SELECT id,prid,name,code FROM tbl_item WHERE status='1') pro ON pro.id=tbl_traproduct.pid Where brid='$brid' AND apdate BETWEEN '$fdate' AND '$tdate' GROUP BY pid ORDER BY name ASC";
    $result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $output.='<tr><th style="width: 40px !important;">SN</th><th>Name</th><th>SKU/Code</th><th class="text-center">In</th><th class="text-center">Out</th><th class="text-center">Available</th></tr>';
    while($row = mysqli_fetch_array($result) ){
        $avqt=$row['poqty']-$row['soqty'];  

    if($avqt>0){  
    $output.='<tr><td class="text-center">'. count_id().'</td><td>'.$row['name'].'</td><td>'.$row['pcode'].'</td><td class="text-center">'.$row['poqty'].'</td><td class="text-center">'.$row['soqty'].'</td><td class="text-center">'.$avqt.'</td></tr>';	
    }}
    // encoding array to json format
    echo $output;
}

?>