<?php
	
	require('fpdf/fpdf.php');
	include 'exfpdf.php';
 	include 'easyTable.php';
 
 	class Manage
	{
		
		private $con;

		function __construct()
		{
			include_once("../config/connection.php");
			$db = new Database();
			$this->con = $db->connect();
		}
		public function runQuery($query) {
	        $result = mysqli_query($this->con, $query);
	        if($result){
	           
	        while($row=mysqli_fetch_assoc($result)) {
	            $resultset[] = $row;
	            
	        }
	        if(!empty($resultset))
	            return $resultset;
	    }
	    }
	   	
	}	
	$report_type 				=	$_GET['report_type'];
	$product_id 				=	$_GET['product_id'];
	$party_id 					=	$_GET['party_id'];
	$page_name 					=	trim($_GET['page_name']);
	$year 						=	$_GET['year'];
	$month 						=	$_GET['month'];
	$gst_type 					=	$_GET['gst_type'];
	$gst_type_2 				=	$_GET['gst_type_2'];
	$dt_from 					=	$_GET['dt_from'];
	$dt_to 						=	$_GET['dt_to'];
	$gst_page =0;
	$platform_page=0;
	$client_page=0;
	if(!empty($page_name)){
		$m = new Manage();
		/*$rows = $m->runQuery("SELECT id, company, address, city, email, mobile, gst, pan, bank, account, account_no, ifsc_code,remark FROM `company`");
		foreach ($rows as $row) {
			$company_name = $row['company'];
			$company_address = $row['address'].','.$row['city'];
		}
		*/
		$company_name ="Eco Organics";
		$company_address = "Rampur";
		if ($page_name == 'Sauda') {
			$m 		= new Manage();
			$header[]= array('Date','Product Name','Party Name','Drums','Rate','Dalal','Condition','Remarks');	
			$col=8;
			$result = $m->runQuery("SELECT * FROM `vw_sauda` order by int_sauda_id desc");
		}
	}
	//header('Content-Type: application/pdf; filename=' . $page_name);
	
	$pdf=new exFPDF();
	$pdf->AddPage('L');
	$pdf->SetFont('Arial','B',12);
	$x = $pdf->GetX();
	$y = $pdf->GetY();	
	$table1=new easyTable($pdf,3, 'width:100%;');
	$table1->rowStyle('valign:M;padding:2;');
	$table1->easyCell($company_name, 'font-color:#00bfff; font-family:times; font-style:B; font-size:16;align:C;colspan:3;');
	$table1->printRow();
	$table1->rowStyle('valign:M;padding:2;');
	$table1->easyCell($company_address,'font-color:#bfbfbf; font-size:8;align:C;colspan:3;');
	$table1->printRow();
	$table1->rowStyle('valign:M;padding:1;');
	$table1->easyCell('Report', ' font-color:#bfbfbf; font-size:12;align:L;');

    $table1->easyCell($page_name, ' font-color:#bfbfbf; font-size:16;  align:C; ');
    

  	$table1->easyCell(date('d-m-Y'), ' font-color:#bfbfbf; font-size:12; align:R; ');
 	$table1->printRow();
 	$table1->endTable();
	$pdf->Ln();

	$table=new easyTable($pdf,$col, 'width:100%;bgcolor:#fff; line-height:1;border-width:0.001; border:1; border-color:#fff;');
	
	foreach($header as $heading) {
		$table->rowStyle('valign:M;border:LRBT;border-color:#a1a1a1;padding:2;');
		foreach($heading as $column_heading)
			 	
			 $table->easyCell($column_heading);   	  
	}
    $table->printRow();
	
	if($result){
	 $tot_in=0;
	 $tot_out=0;
	 if($page_name=='Batch_regiter'){
		$table->rowStyle('valign:M;border:LRBT;border-color:#a1a1a1;padding:2; background-color:#ccc;');
		$table->easyCell($report_type,'colspan:7');	
		$table->easyCell('');	
		$table->easyCell('');	
		$table->easyCell('');	
		$table->easyCell('');	
		$table->easyCell('');	
		$table->easyCell('');	
		$table->printRow();
	}
	foreach($result as $index=> $row) {
		$pdf->SetFont('Arial','',8);
		$table->rowStyle('valign:M;border:LRBT;border-color:#a1a1a1;padding:2;');
			if($report_type == 1){
				
				if($row["dat_transaction"]){
					$table->easyCell(date_format(date_create($row["dat_transaction"]),'d-m-Y'));
					$table->easyCell($row['purchase_no']);	
					$table->easyCell($row['party_name']);	
					$table->easyCell($row['product_name']);	
					$table->easyCell($row['quantity']);	
					$table->easyCell($row['rate']);	
					$table->easyCell($row['txt_remarks']);	
				}else{
					$table->easyCell('');
					$table->easyCell('');	
					$table->easyCell('');	
					$table->easyCell('Total Purchase Amount');	
					$table->easyCell($row['quantity']);
					$table->easyCell('');	
					$table->easyCell('');
				}
					$table->printRow();

				
			}
			elseif($report_type == 3){
			    
				$table->easyCell(date_format(date_create($row["dat_transaction"]),'d-m-Y'));
				$table->easyCell($row['drum_tag']);	
				$table->easyCell($row['stock_in']);	
				$table->easyCell($row['stock_out']);	
				$table->easyCell($row['production_unit']);	
				$table->easyCell($row['remark']);	
				$table->printRow();
			}
			elseif($report_type == 4){
			   
				
				$total_qty+=$row["quantity"];
				$table->easyCell($row['opening']);	
				$table->easyCell($row['purchase']);	
				$table->easyCell($row['drum']);	
				$table->easyCell($row['tank']);	
				$table->easyCell($row['quantity']);
			
				$table->printRow();
			}
			elseif($report_type==5){
				$table->easyCell($row['dat_transaction']);	
				$table->easyCell($row['product_name']);
				$table->easyCell($row['party_name']);	
				$table->easyCell($row['purchase_no']);	
				$table->easyCell($row['tare_weight']);	
				$table->easyCell($row['gross_weight']);	
				$table->easyCell($row['quantity']);		
				$table->printRow();
			}
			elseif($report_type==6){
				$table->easyCell($row['dat_transaction']);	
				$table->easyCell($row['quantity']);		
				if($row["int_source_opening_id"]){
					$table->easyCell($row["product_name"]); 
				}else{
					$table->easyCell(''); 
				}	
				
				$table->easyCell($row['src_purc_no']);	
				$table->easyCell($row['src_storage']);	
				$table->easyCell($row['src_drum_tag']);		
				$table->easyCell($row['src_batch_no']);		
				if($row["int_target_opening_id"]){
					$table->easyCell($row["product_name"]); 
				}else{
					$table->easyCell(''); 
				}		
				$table->easyCell($row['tar_purc_no']);		
				$table->easyCell($row['sales_order_no']);		
				$table->easyCell($row['tar_storage']);			
				$table->easyCell($row['tar_drum_tag']);		
				$table->easyCell($row['tar_batch_no']);		
				$table->easyCell($row['remark']);		
				$table->printRow();
			}
			elseif($page_name=='Sauda'){
				
					$table->easyCell(date_format(date_create($row["dat_sauda"]),'d-m-Y'));
					$table->easyCell($row['product_name']);	
					$table->easyCell($row['party_name']);
					$table->easyCell($row['int_drums']);	
					$table->easyCell($row['dec_rate']);	
					$table->easyCell($row['txt_dalal']);	
					$table->easyCell($row['txt_condition']);	
					$table->easyCell($row['txt_remark']);		
					$table->printRow();
			}
			else{
				
				foreach($row as $column){
					 $table->easyCell($column);	
				}	
					$table->printRow();
			}
			
		}
	}
	if($page_name=='Batch_regiter'){
		$table->rowStyle('valign:M;border:LRBT;border-color:#a1a1a1;padding:2; bgcolor:#ccc;font-color:#000; font-size:16; ');
		$table->easyCell('');	
		$table->easyCell('');	
		$table->easyCell('');	
		$table->easyCell($tot_in);	
		$table->easyCell('');	
		$table->easyCell($tot_out);	
		$table->easyCell('');	
		$table->printRow();
	}
	$table->endTable();
	
 	$pdf->Output();

?>