<?php
date_default_timezone_set("Asia/Kolkata");
 error_reporting(0);
ob_start();
session_start();
include("../../include/db.php");


$session_branch=$_SESSION['branch'];
$session_user_type=$_SESSION['user_type'];
  $branch_name=$_GET['branch_name'];
  $fabric_types=$_GET['fabric_types'];

   function get_tot_qty_jobcard($jobcard_no)
  {
  $sql_user="select sum(qty) as qty from jobcard_sublist where jobcard_no='$jobcard_no'";
  $rs_user=mysql_query($sql_user);
  while($rsdata_user=mysql_fetch_object($rs_user))
  { 
  $qty12=$rsdata_user->qty;
  }
  return $qty12;
  }


  
$cust_name=$_GET['customer_name'];
$from_date=$_GET['from_date'];
$to_date=$_GET['to_date'];
$status=$_GET['jobtype'];
$current_date=date('Y-m-d');
$job_card_num=$_GET['job_card_num'];
$fabric_types=$_GET['fabric_types'];
$value=explode(',',$_GET['fabric_types']);
 
                   for($i=0;$i<count($value);$i++){                  
                    $fabric1.=$value[$i];                 
				    
				   if($i<(count($value)-1)) {
                   $fabric1.="','";
                  
                   }
				   }

if($cust_name!=""){ $customer_name1= "cust_name='$cust_name'"; }else{$customer_name1=''; }
if($from_date!=""){ $from_date1= "DATE_FORMAT( entry_date,'%Y-%m-%d' )>='$from_date'"; }else{$from_date1="DATE_FORMAT(entry_date,'%Y-%m-%d' )='$cur_dates'"; }
if($to_date!=""){ $to_date1 = "DATE_FORMAT(entry_date,'%Y-%m-%d' )<='$to_date'";}else{$to_date1="DATE_FORMAT(entry_date,'%Y-%m-%d' )='$cur_dates'";}
if($status!=""){ $jobtype1 = "job_type='$status'";}else{$jobtype1='';}
if($job_card_num!=""){ $job_card_nums = "jobcard_no='$job_card_num'";}else{$job_card_nums='';}
if($fabric_types!=""){ $fabric_type1 = "fabric_type IN ('$fabric1')";}else{$fabric_type1='';}

$all_value10 = $customer_name1.";".$from_date1.";".$to_date1.";".$jobtype1.";".$job_card_nums.";".$fabric_type1;
$all_array10 = explode(';',$all_value10);
foreach($all_array10 as $value10)
{ 
if($value10!='')
{
$get_query103 .= $value10." AND ";
}
}	  

if($from_date!=""){ $from_date2= $from_date; }else{$from_date2=$cur_date; }
if($to_date!=""){ $to_date2 = $to_date;}else{$to_date2=$cur_date;}
if($cust_name!=""){ $cust_name1=$cust_name;} else {$cust_name1="ALL" ;} 
if($job_card_num!=""){ $Jobtype1=$job_card_num;} else {$Jobtype1="ALL" ;}
if($fabric_types!=""){ $fabric_types1=$fabric_types;} else {$fabric_types1="ALL" ;}



$blog=array('','Jobcard Creation Report','','');


foreach($blog as $icon)
{
$output		.= '"'.$icon.'",';
}

$output .="\n";
$output .="\n"; 
$output .="From Date".",".$from_date2.","."To Date".",".$to_date2.","."Customer Name".",".$cust_name1.","."Jobtype".",".$Jobtype1.","."\n"; 
$output .="\n"; 

 
 
     $output .='"SNo","Date","Jobcard No","Created By","Customer Name","Job Type","Fabric Type","Total Qty","Total Value",'."\n";
     // $table_values_new=array(ItemGroupName);
	 $table_values=array(SNo,Date,JobcardNo,CreatedBy,CustomerName,JobType,FabricType,TotalQty,TotalValue);
   
   if(($from_date=='')&&($to_date==''))
  {
   $sql="select * from  jobcard_main where  DATE_FORMAT( entry_date,  '%Y-%m-%d' )='$current_date'  order by jobcard_no ASC";
  } 
  else
  {
   $sql="select * from  jobcard_main where $get_query103 	main_id!='' order by jobcard_no ASC ";
  }
    $rs=mysql_query($sql);
	$sno=0;
   		while($rsdata=mysql_fetch_array($rs))
  			{
   				$sno=$sno+1;
				$from=$from+1;
   				$main_id=$rsdata['main_id'];
   				$jobcard_no=$rsdata['jobcard_no'];
				$created_by=$rsdata['created_by'];
	   			$cust_name=$rsdata['cust_name'];
	   			$entry_date=$rsdata['entry_date'];
				$job_type=$rsdata['job_type'];
	   			$fabric_type=$rsdata['fabric_type'];
	   			$overall_value=$rsdata['overall_value'];
	   			$net_total=$rsdata['net_total'];
          $tot_qty=get_tot_qty_jobcard($jobcard_no);
          $get_qty=mysql_fetch_array(mysql_query("select qty from   jobcard_sublist where jobcard_no='$jobcard_no'"));
          $qtys=$get_qty[qty];
		  
			 
			foreach ($table_values as $val) 
			{
				
				if($val=='SNo'){$output.='"'.$sno.'",';}
				if($val=='Date'){$output.='"'.date("d-m-Y",strtotime($entry_date)).'",';}
				if($val=='JobcardNo'){$output.='"'.$jobcard_no.'",';}
				if($val=='CreatedBy'){$output.='"'.$created_by.'",';}
				if($val=='CustomerName'){$output.='"'.$cust_name.'",';}
				if($val=='JobType'){$output.='"'.$job_type.'",';}
				if($val=='FabricType'){$output.='"'.$fabric_type.'",';}
				if($val=='TotalQty'){$output.='"'.$tot_qty.'",';}
				if($val=='TotalValue'){$output.='"'.number_format($net_total,2).'",';}
				
				
				

if(($val!='SNo')&&($val!='Date')&&($val!='JobcardNo')&&($val!='CreatedBy')&&($val!='CustomerName')&&($val!='JobType')&&($val!='FabricType')&&($val!='TotalQty')&&($val!='TotalValue')){$output .='"'.$val.'",';}
} 
 $output .="\n";
		
					
				
				 
	$net_tot+=$net_total;
  $net_qty+=$tot_qty;
 
}
 
$output .="".",".",".",".",".",".",".Total.",".round($net_qty).",".round($net_tot)."\n\n";

$date=date('d-m-Y H:i:s');
$filename =  "Jobcard Creation Report". $date.".csv";
header('Content-type: application/xls');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;



exit;
	


?>
<style>
br{
	
	mso-data-placement:same-cell;
	}
</style>