<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	
	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
	require('pdf/fpdf.php');
		
	$po_id	=	base64_decode(base64_decode(base64_decode($_GET['po_id']))); 
	$flg	=	base64_decode(base64_decode(base64_decode($_GET['flg']))); 
	if(isset($_GET['inflg'])){ $inflg = 1; }else{ $inflg = 2; }
	
	$SLCTf = "SELECT  * FROM po_entry WHERE po_entry_id = '$po_id'";
	$QURYf = mysqli_query($connect,$SLCTf);		 
	$FTCHf = mysqli_fetch_object($QURYf);
	
	$SLCT_tp = "SELECT * FROM party_det WHERE party_det_id = '$FTCHf->party_det_id'";
	$QURY_tp = mysqli_query($connect,$SLCT_tp);
	$FTCH_tp = mysqli_fetch_object($QURY_tp);
	
	$tot_cnt = '';
	$h_br = '0'; //THIS IS FOR SETTING THE BORDER IN PDF PAGE FOR HEADER
	$h_br1 = '0'; //THIS IS FOR SETTING THE BORDER IN PDF PAGE FOR HEADER
	$tot_dies = 0;	
	$i=1;
	$i3=1;
	$des_id = array();
	$des_det_id = array();
	$des_ext = array();
	$del_date = '';
	
	$SLCT_ORDR 	= "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(pe.design_det_id) AS tot_cnt FROM po_entry_det pe,design_det dd WHERE dd.design_det_id=pe.design_det_id AND pe.cancelled = 'no' AND pe.po_entry_id = '$po_id' AND dd.design_det_id = pe.design_det_id ORDER BY pe.master_cat_id,dd.design_no,pe.po_entry_det_id ASC";
	$QURY_ORDR 	= mysqli_query($connect,$SLCT_ORDR);
	$FTCH_ORDR 	= mysqli_fetch_object($QURY_ORDR);
	$ord_qty 	= $FTCH_ORDR->tot_cnt;
	$ord_die 	= $FTCH_ORDR->tot_pic;
	
	if($flg==1) // Order Designs
	{
		$h_br1='T';
		$h_br='0';		
		$SLCT = "SELECT  * FROM po_entry_det pe,design_det dd WHERE pe.cancelled = 'no' AND pe.po_entry_id = '$po_id' AND dd.design_det_id = pe.design_det_id ORDER BY pe.master_cat_id,dd.design_no,pe.po_entry_det_id ASC";
	}
	else if($flg==2) // Finish Designs
	{
		$h_br='T';
		$h_br1='0';
		
		$SLCT_t = "SELECT COUNT(po_entry_det_id) AS tot_cnt FROM po_entry_det WHERE cancelled = 'no' AND po_entry_id = '$po_id'";
		$QURY_t = mysqli_query($connect,$SLCT_t);
		$FTCH_t = mysqli_fetch_object($QURY_t);
		$tot_cnt = $FTCH_t->tot_cnt;	
				
		$SLCT = "SELECT  * FROM po_entry_det pe,design_det dd WHERE pe.po_entry_id = '$po_id' AND pe.cancelled = 'no' AND pe.del_status='yes' AND dd.design_det_id = pe.design_det_id ORDER BY pe.master_cat_id,dd.design_no,pe.po_entry_det_id ASC";	
	}
	else if($flg==3) // Delivery Designs
	{
		$h_br='T';
		$h_br1='0';
		
		$po_del_id	=	base64_decode(base64_decode(base64_decode($_GET['po_del_id']))); 
		
		$SLCT_del = "SELECT * FROM  `po_delivery` WHERE  `po_delivery_id` ='$po_del_id'";
		$QURY_del = mysqli_query($connect,$SLCT_del);
		$FTCH_del = mysqli_fetch_object($QURY_del);
		$del_date = $FTCH_del->delivery_date;
		
		$SLCT_po_ids = "SELECT IFNULL( GROUP_CONCAT(  `po_delivery_id` ) , 0 ) as pd FROM  `po_delivery` WHERE  `po_entry_id` ='$po_id' AND STR_TO_DATE(  `delivery_date` ,  '%d-%m-%Y' ) < STR_TO_DATE(  '$del_date',  '%d-%m-%Y' )";
		$QURY_po_ids = mysqli_query($connect,$SLCT_po_ids);
		$FTCH_po_ids = mysqli_fetch_object($QURY_po_ids);
		if($FTCH_po_ids->pd!=0)
		{
			$po_delivery_ids = $FTCH_po_ids->pd;
			
			$SLCT_pen_qty = "SELECT count(*) as pen_qty FROM `po_delivery_det` WHERE `po_delivery_id` in ($po_delivery_ids)";
			$QURY_pen_qty = mysqli_query($connect,$SLCT_pen_qty);
			if(mysqli_num_rows($QURY_pen_qty)>0)
			{
				$FTCH_pen_qty = mysqli_fetch_object($QURY_pen_qty);
				$tot_pen_qty = $FTCH_pen_qty->pen_qty;
			}
		}
		else
		{
			$tot_pen_qty=0;
		}
		
		$SLCT_t = "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(po_entry_det_id) AS tot_cnt FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND cancelled = 'no' AND po_entry_id = '$po_id'";
		$QURY_t = mysqli_query($connect,$SLCT_t);
		$FTCH_t = mysqli_fetch_object($QURY_t);
		$tot_cnt = $FTCH_t->tot_cnt;
		
		$SLCT = "SELECT  * FROM po_delivery_det pe,design_det dd WHERE pe.cancelled = 'no' AND pe.po_delivery_id = '$po_del_id'   AND dd.design_det_id = pe.design_det_id ORDER BY pe.master_cat_id,dd.design_no,pe.po_delivery_det_id ASC";
	}
		//echo $SLCT."<br/>";
	$QURY = mysqli_query($connect,$SLCT);
	
	while($FTCH = mysqli_fetch_object($QURY))
	{
		$design_id	=	base64_encode(base64_encode(base64_encode($FTCH->design_det_id)));
		
		$SLCT_cat = "SELECT * FROM  master_cat WHERE  master_cat_id= '$FTCH->master_cat_id'";
		$QURY_cat = mysqli_query($connect,$SLCT_cat);
		$FTCH_cat = mysqli_fetch_object($QURY_cat);
		
		$SLCT_or = "SELECT COUNT(design_det_id) AS or_cnt FROM po_entry_det WHERE cancelled = 'no' AND design_det_id = '$FTCH->design_det_id' AND fin_status = 'no'"; 
		$QURY_or = mysqli_query($connect,$SLCT_or);
		$FTCH_or = mysqli_fetch_object($QURY_or);
		
		$SLCT_st = "SELECT COUNT(design_det_id) AS st_cnt FROM stock_det WHERE st_fin_status = 'no' AND design_det_id = '$FTCH->design_det_id'";
		$QURY_st = mysqli_query($connect,$SLCT_st);
		$FTCH_st = mysqli_fetch_object($QURY_st);
		
		$or_cnt[$i] = $FTCH_or->or_cnt;
		$st_cnt[$i] = $FTCH_st->st_cnt;
		
		$des_det_id[$i] = $FTCH->design_det_id;
		$des_id[$i] = $FTCH_cat->master_cat_name." ".$FTCH->design_no; 
		
		if($FTCH->mas_fin_ext!=''){ if(file_exists("../mas_fin_images/L.".$FTCH->mas_fin_ext)){ $des_ext[$i] = "../mas_fin_images/L.".$FTCH->mas_fin_ext; }else{ $des_ext[$i] = "../design_images/not_found/not-found.jpg"; } }
		else if($FTCH->cad_fin_ext!=''){ if(file_exists("../cad_fin_images/L.".$FTCH->cad_fin_ext)){  $des_ext[$i] = "../cad_fin_images/L.".$FTCH->cad_fin_ext; }else{ $des_ext[$i] = "../design_images/not_found/not-found.jpg"; } }
		else if($FTCH->imag_ext!=''){ if(file_exists("../design_images/L.".$FTCH->cad_fin_ext)){ $des_ext[$i] = "../design_images/L.".$FTCH->imag_ext; }else{ $des_ext[$i] = "../design_images/not_found/not-found.jpg"; } }
		
		$tot_dies = $tot_dies + $FTCH->master_pieces;
		$i++;
	}
	$tot_die = $tot_dies;
	
	$ro_vl = 5; //this is important when u change the table column u should change this
	$arr_len = sizeof($des_ext);
	if($flg!=1){ $po_die_qty = $tot_cnt; $pen_qty = $po_die_qty-($arr_len+$tot_pen_qty); $pen_die=$ord_die-$tot_dies; } else{ $po_die_qty = $arr_len; $pen_qty = '0'; $pen_die=0; } //this is for po entry qty
	if($arr_len > $ro_vl)
	{ 
		$cnt = $arr_len/$ro_vl;
		$cnt = floor($cnt+1);
	}
	else
	{
		$cnt =1;
	}
		
	$pdf = new FPDF('P','mm','A4');		
	//THIS IS FOR CREATING THE OBJECTS	 
	$pdf->AddPage();	
	$pdf->SetAutoPageBreak(false); 	
	$pdf->SetMargins(0.5,0.5,0.5,0.5);
	$br=0; //this is for border value;
	$br_l='L'; //this is for border value for table data;
	
	$pdf->SetX(0);
	$pdf->SetY(0);
	
	$pdf->SetFont('Times','',11);
	
	/********************** OLD CODING  **********************/
/*	if($inflg == 1)
	{
		$pdf->Cell(0,6,'Company Name : '.$FTCH_tp->cmp_name,$br,0,'C');
	}*/

	/********************** NEW CHANGES  **********************/
	if($_SESSION['admin_designation'] == 'super_admin')
	{
		$pdf->Cell(0,6,'Company Name : '.$FTCH_tp->cmp_name,$br,0,'C');
	}


	$pdf->Cell(296,2,'',0,1,'C');//for empty space 
	$pdf->Cell(60,16,'Purchase Order No : '.$FTCHf->po_no,$br,0,'C');
	$pdf->SetFont('Times','',11);
	$pdf->Cell(70,16,'Purchase Order Date : '.str_replace("-", "/",$FTCHf->po_date),$br,0,'R'); 
	$pdf->Cell(25,16);
	$pdf->Cell(45,16,'Total Designs : '.$ord_qty.'('.$ord_die.')',$br,1);
	
	if($inflg==1)
	{
		$SLCT_CHK = "SELECT  * FROM po_entry_det WHERE cancelled = 'no' AND po_entry_id = '$po_id' AND fin_status = 'no'";
		$QURY_CHK = mysqli_query($connect,$SLCT_CHK);
		if(mysqli_num_rows($QURY_CHK)>0)
		{		
			$SLCT_STO1 = "SELECT COUNT(des_cnt) AS st_cnt, SUM(tot_pic) AS tot_pic FROM(SELECT master_pieces AS tot_pic, COUNT(stock_det.design_det_id) AS des_cnt FROM po_entry_det,stock_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_id = '$po_id' AND po_entry_det.fin_status = 'no' AND po_entry_det.cancelled = 'no' AND po_entry_det.design_det_id = stock_det.design_det_id AND stock_det.st_fin_status='no' GROUP BY po_entry_det.design_det_id) AS TBL1";
			$QURY_STO1 = mysqli_query($connect,$SLCT_STO1);
			$FTCH_STO1 = mysqli_fetch_object($QURY_STO1);
			$SLCT_STO2 = "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(po_entry_det.design_det_id) AS st_cnt FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_id = '$po_id' AND fin_status = 'no' AND cancelled = 'no' ORDER BY po_entry_det.design_det_id ASC";
			$QURY_STO2 = mysqli_query($connect,$SLCT_STO2);
			$FTCH_STO2 = mysqli_fetch_object($QURY_STO2);
			$SLCT_STO3 = "SELECT COUNT(des_cnt) AS st_cnt, SUM(tot_pic) AS tot_pic FROM(SELECT master_pieces AS tot_pic, COUNT(po_entry_det.design_det_id) AS des_cnt FROM po_entry_det,piece_loading,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_det.fin_status='no' AND po_entry_det.cancelled='no' AND po_entry_det.po_entry_id!='$po_id' AND piece_loading.design_det_id = po_entry_det.design_det_id AND piece_status = 'loading' AND po_entry_det.design_det_id IN(SELECT design_det_id FROM po_entry_det WHERE po_entry_id = '$po_id' AND po_entry_det.fin_status='no' AND po_entry_det.cancelled='no') GROUP BY po_entry_det.design_det_id) AS TBL1";
			$QURY_STO3 = mysqli_query($connect,$SLCT_STO3);
			$FTCH_STO3 = mysqli_fetch_object($QURY_STO3);
			
			$box_qty = ($FTCH_STO2->st_cnt-($FTCH_STO1->st_cnt+$FTCH_STO3->st_cnt));
			$box_die = ($FTCH_STO2->tot_pic-($FTCH_STO1->tot_pic+$FTCH_STO3->tot_pic));
			if($box_qty<0)
			{
				$box_qty =0;
				$box_die =0;
			}		
			else
			{
				$box_qty = $box_qty;
				$box_die = $box_die;
			}
			
			$pdf->SetFont('Times','',11);
			$pdf->Cell(296,2,'',0,1,'C');//for empty space 
			$pdf->Cell(70,6,'Stock Designs : '.$FTCH_STO1->st_cnt.'('.$FTCH_STO1->tot_pic.')',$br,0,'C');
			
			$pdf->SetFont('Times','',11);
			$pdf->Cell(45,6,'Box Designs : '.$box_qty.'('.$box_die.')',$br,0,'C');
			
			$pdf->SetFont('Times','',11);
			$pdf->Cell(70,6,'Another Order Designs : '.$FTCH_STO3->st_cnt.'('.$FTCH_STO3->tot_pic.')',$br,1,'R'); 
		}
	}	
	$pdf->SetFont('Times','',11);
	//$pdf->Cell(296,6,'',$h_br1,1,'C');//for empty space 
	
	if($flg!=1)
	{
	
	
	$pdf->Cell(60,6,'Delivery Date :'.str_replace("-", "/",$del_date),$br,0,'C');
	
	$pdf->SetFont('Times','',11);
	$pdf->Cell(80,6,'Delivery Designs Quantity : '.$arr_len.'('.$tot_dies.')',$br,0,'C');	
	$pdf->Cell(65,6,'Pending Designs Quantity : '.$pen_qty.'('.$pen_die.')',$br,1,'R');
	
	$pdf->SetFont('Times','',11);
	$pdf->Cell(296,6,'',$h_br,1,'C');//for empty space 
	}
	
	
	
	$l=1; 
	$tot_ro = '';
	$image_height = 40;
	$image_width = 40;
	$pdf->SetFont('Times','',10);
	for($incr=1;$incr<=$cnt;$incr++) 
	{
		for($k=1;$k<=$ro_vl;$k++)
		{
			if(isset($des_ext[$l]))
			{
				$start_x = $pdf->GetX();
   				$start_y = $pdf->GetY();
   
				$pdf->Image($des_ext[$l],$start_x,$start_y,$image_height,$image_width);
				$pdf->SetXY($start_x,$start_y+40); // this is for design no cordinate positions
				$pdf->Cell(40,5,$des_id[$l],0,0,'C');//for design no 
				 
				if($flg==1 && $inflg==1)
				{
				 	$pdf->SetXY($start_x,$start_y+46); // this is for design no cordinate positions
				 	$pdf->Cell(40,5,$or_cnt[$l]. " / " .$st_cnt[$l],0,0,'C');//for stock and pending 
				}
				 
				$pdf->SetXY($start_x + $image_width + 2, $start_y); //this is for image cordinate positions
				if($l==25)
				{
					$pdf->SetY(-15);
					$pdf->SetFont('Arial','I',8);
					$pdf->Cell(0,10,'Page '.$pdf->PageNo()." / {nb}",0,0,'C');
					$pdf->SetFont('Times','',10);
				}
				$tot_ro = $l % 25 ; 
				if($tot_ro == 0)
				{ 
				 	$pdf->AddPage();  
				 	$pdf->AliasNbPages();
					$pdf->SetY(-15);
				    // Select Arial italic 8
				    $pdf->SetFont('Arial','I',8);
				    // Print centered page number
				    $pdf->Cell(0,10,'Page '.$pdf->PageNo()." / {nb}",0,0,'C');
				    $pdf->SetFont('Times','',10); 
				}				 
			}
			$l++;
		}
		if($tot_ro!=0){	$pdf->SetXY(0, $start_y+$image_height+13); }else if($tot_ro==0){ $pdf->SetXY(0,0); }		
	}
	$pdf->Output(); //THIS IS FOR OUTPUT OF THE P
?>