<?
session_start();
include_once('../opendb.php');
$brcode=$_SESSION['brcode'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$datefrom=$_REQUEST[datefrom];
$dateto=$_REQUEST[dateto];
$fromdt=implode('-', array_reverse(explode('-', $datefrom)));
$todt=implode('-', array_reverse(explode('-', $dateto)));
$filename ="dispatchpnd.xls";

//$contents.="\t\t\t\t\t\t\t\tBranch :".$brcode."\n";

$contents.="\t\t\t\t\t\t\t\tPENDING DISPATCH REPORT\n";

$contents.="\t\t\t\t\t\t\tShow Report From  ".$fromdt." to ".$todt."\n";

$contents.="Invoice No.\t Invoice Date \t Customer \t Engineer \t Inv Amount\n\n";

    $mdf = $_REQUEST['datefrom'];
    $mdt = $_REQUEST['dateto'];
    
    $g=mysqli_query($db,"select invno,entrydate,customer,engineer,sum(tot) tot from spareinvoice where brcode = '$brcode' and entrydate >='$mdf' and entrydate <='$mdt' group by invno");
   // print "select invno,entrydate,sum(tot) from spareinvoice where brcode = '$brcode' and entrydate >='$mdf' and entrydate <='$mdt' order by entrydate group by invno";
    //print "<tr><th>Invoice No.</th><th>Invoice Date</th><th>Customer</th><th>Engineer</th><th>Inv Amount";
    while($grow=mysqli_fetch_array($g))
    {
        $invdt=implode('-', array_reverse(explode('-', $grow[entrydate])));
        $g1=mysqli_query($db,"select invno from dispatchinvoice where brcode = '$brcode' and invno= '$grow[0]'");
        $sprrow=mysqli_fetch_array($g1);
        if(strlen($sprrow[0])>0)
        {
            $a=1;   
        }
        else
        {
             $contents.="'".$grow[invno]."\t".$invdt."\t".$grow[customer]."\t".$grow[engineer]."\t".$grow[tot]."\n";
            //print "<tr><td>$grow[invno]</td><td>$invdt</td><td>$grow[customer]</td><td>$grow[engineer]</td><td>$grow[tot]</td></tr>";
        }
    } 
    //print "</table>";
    $contents = strip_tags($contents);
  header('Content-type: application/ms-excel');
  header('Content-Disposition: attachment; filename='.$filename);
  print $contents;
?>