<?php 
	date_default_timezone_set('UTC');
	//require('fpdf/fpdf.php');
    require_once("config/configDB.php");
    require_once('tcpdf/config/tcpdf_config.php');
    require_once("tcpdf/tcpdf.php");

    
    $spare        = is_numeric($_GET['spare']) ? $_GET['spare'] : 0;

    $sqlspareMasterQuery = mysqli_query($conn,"SELECT sm.id,sm.productname as sparename,psd.purchase_eqty as spareqty FROM product_master sm 
    
    LEFT JOIN  purchasedetails as psd ON sm.id=psd.productID
    WHERE sm.id='$spare' and  sm.isdelete=1  and psd.purchase_eqty>0 and psd.status='' and sm.product_type='Spare' GROUP BY psd.productID");

$prow           = mysqli_fetch_array($sqlspareMasterQuery,MYSQLI_ASSOC);
$spareName= $prow['sparename'];

    
    $sql = "SELECT sm.id,sm.productname as sparename,sum(psd.purchase_eqty) as spareqty FROM product_master sm 
		
		LEFT JOIN  purchasedetails as psd ON sm.id=psd.productID
		WHERE  sm.isdelete=1  and psd.purchase_eqty>0 and psd.status='' and sm.product_type='Spare' ";

		if($spare >0)  {
		
			$sql.="  and sm.id='".$spare."' ";
		}
		
		$sql.=" GROUP BY psd.productID ";
    $query=mysqli_query($conn, $sql);
    
    class MYPDF extends TCPDF {

        //Page header
        public function Header() {
            // Logo
            //$image_file = K_PATH_IMAGES.'logo_example.jpg';
            //$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
            // Set font
            $this->SetY(12);
            $this->SetFont('helvetica', 'B', 24);
            // Title
            $this->Cell(0, 25, PDF_HEADER_TITLE, 0, false, 'C', 0, '', 0, false, 'M', 'M');
            $style3 = array('width' => 0.2, 'cap' => 'round', 'join' => 'round', 'dash' => 0, 'color' => array(0, 0, 0));
            //$this->Line(0, 22, 210, 22, $style3);
        }
    
        // Page footer
        public function Footer() {
            // Position at 15 mm from bottom
            $this->SetY(-15);
            // Set font
            $this->SetFont('helvetica', 'I', 8);
            // Page number
            $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
        }
    }

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('thinkofmind.com');
$pdf->SetTitle('Spare Stock Report');
$pdf->SetSubject('Spare Stock Report');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 20);

// add a page
$pdf->AddPage();

$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '10,20,5,10', 'phase' => 10, 'color' => array(255, 0, 0));
$style2 = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$style3 = array('width' => 0.2, 'cap' => 'round', 'join' => 'round', 'dash' => 0, 'color' => array(0, 0, 0));
// Line
//$pdf->Line(5, 10, 5, 30, $style2);


// set some text to print
$txt = <<<EOD
Spare Stock Reports

EOD;

// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

$pdf->SetFont('helvetica', '', 10);

$pdf->Write(2, '', '', 0, 'C', true, 0, false, false, 0);

$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td>Spare Name : $spareName</td>
        

    </tr>
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl, true, false, false, false, '');
//$pdf->Write(1, '', '', 0, 'C', true, 0, false, false, 0);

$pdf->SetFont('helvetica', '', 8);


$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="1">
    <tr>
  
    <th>Spare</th>
   
    <th>Qty</th>
    </tr>
    


EOD;
$pdf->SetFont('helvetica', '', 8);
while($row = mysqli_fetch_array($query))
    {
       
		$spareName		= $row['sparename'];
		

		$spareid    = $row['id'];

		$resultSpareQty = mysqli_query($conn,"SELECT sum(isd.spareqty) as spareqty   FROM internalpro_spare_dtl as isd WHERE isd.spareID='".$spareid."' and  isd.isdelete=1  ");
		$SpareQty    = mysqli_fetch_array($resultSpareQty,MYSQLI_ASSOC);
		$ASpareQty   = ($SpareQty['spareqty']>0) ? $SpareQty['spareqty'] : 0;
		
		/*$resultSpareQtyw = mysqli_query($conn,"SELECT sum(swd.spareqty) as wsqty FROM service_workshop_dtl as swd WHERE swd.isdelete=1 and swd.spareID='".$spareid."' ");
		$wSpareQty    	= mysqli_fetch_array($resultSpareQtyw,MYSQLI_ASSOC);
		$AwSpareQty   	= ($wSpareQty['wsqty']>0) ? $wSpareQty['wsqty'] : 0;*/
		
		$resultP 		= mysqli_query($conn,"SELECT sum(psd.purchase_eqty) as spareqty   FROM purchasedetails as psd WHERE psd.productID='".$spareid."' and  psd.status=''  ");
		$PSpareQty 		= mysqli_fetch_array($resultP,MYSQLI_ASSOC);
		
		
		$resultPwo 		= mysqli_query($conn,"SELECT sum(psd.purchase_eqty) as spareqty   FROM purchasedetailswo as psd WHERE psd.productID='".$spareid."' and  psd.status=''  ");
		$PSpareQtywo 		= mysqli_fetch_array($resultPwo,MYSQLI_ASSOC);
		
		$AwSpareQty   	= ($PSpareQty['spareqty']+$PSpareQtywo['spareqty']) - ($ASpareQty); 
        $AwSpareQty_Arr 	= $AwSpareQty;
        
$tbl.= <<<EOD
    <tr>
        
        <td>$spareName</td>
        <td>$AwSpareQty_Arr</td>
    </tr>
EOD;
    }
$tbl.= <<<EOD
    </table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');

//Close and output PDF document
$pdf->Output('sparestock.pdf', 'I');
   
?>