<?php

include("config.inc.php");
require_once('Spreadsheet/Excel/Writer.php');

session_start();
if($_SESSION['dbName']==""){
	header("location:../index.php");
}


set_time_limit (0);
ini_set("memory_limit","3000M");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$transType = $_GET['transType'];

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse, ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ifNull(Module_ProjectDt,0) As Module_ProjectDt, 
			 ifNull(Module_CostCenterDt,0) As Module_CostCenterDt ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


//Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
 
//Sending HTTP headers
if ($transType == 'AD')
	$workbook->send('Exp_AdjustmentExcel.xls');
else if ($transType == 'TR')
	$workbook->send('Exp_TransferExcel.xls');
else if ($transType == 'PI')
	$workbook->send('Exp_PurchaseExcel.xls');
else 
	$workbook->send('Exp_ExcelImport.xls');
 
//Creating a worksheet
if ($transType == 'AD')
	$worksheet=&$workbook->addWorksheet('Adjustment Excel');
else if ($transType == 'TR')
	$worksheet=&$workbook->addWorksheet('Transfer Excel');
else if ($transType == 'PI')
	$worksheet=&$workbook->addWorksheet('Purchase Excel');
else 
	$worksheet=&$workbook->addWorksheet('Import Excel');
$worksheet->setLandscape();
 
//set all columns same width
$columnWidth = 10;

//Title Format 
$sheetTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'size'=>10));

//Column Title Format  
$columnTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'top'=>1,
  'bottom'=>1 ,
  'size'=>15,
  'color'=>0x0A,
  ));
  
//Elie and tarek Format

$regularleftFormatNum1 =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ));
$regularrightFormatNum1 =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ));  

$boldleftFormatNum1 =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  
$boldrightFormatNum1 =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ,
  'bold'=>1
  )); 
  
$grouplevel1FormatNum1 =& $workbook->addFormat(array('size'=>10,
  'align'=>'left',
  'bold'=>1,
  'color'=>0x0C
  )); 
$grouplevel2FormatNum1 =& $workbook->addFormat(array('size'=>10,
  'align'=>'left',
  'bold'=>0,
  'color'=>0x0A
  )); 
$grouplevel3FormatNum1 =& $workbook->addFormat(array('size'=>9,
  'align'=>'left',
  'italic'=>1,
  'color'=>0x11
  ));   
$grouplevel1totalFormatNum1 =& $workbook->addFormat(array('size'=>10,
  'align'=>'right',
  'bold'=>1,
  'color'=>0x0C
  ));  
$grouplevel2totalFormatNum1 =& $workbook->addFormat(array('size'=>10,
  'align'=>'right',
  'bold'=>0,
  'color'=>0x0A
  ));  
$grouplevel3totalFormatNum1 =& $workbook->addFormat(array('size'=>9,
  'align'=>'right',
  'italic'=>1,
  'color'=>0x11
  ));    



$regularleftFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$regularrightFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$boldleftFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$boldrightFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');  
$grouplevel1FormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$grouplevel2FormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$grouplevel3FormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$grouplevel1totalFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$grouplevel2totalFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$grouplevel3totalFormatNum1->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');


// Elie and Tarek Format  
  
  
 
  
// Regular Format
$regularleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ));
$regularrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ));  
  
$TitleFormat =& $workbook->addFormat(array('size'=>11,
  'align'=>'left',
  'bold'=>1
  ));

$boldleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  
$boldrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ,
  'bold'=>1
  )); 
  
$grouplevel1Format =& $workbook->addFormat(array('size'=>10,
  'align'=>'left',
  'bold'=>1,
  'color'=>0x0C
  )); 
$grouplevel2Format =& $workbook->addFormat(array('size'=>10,
  'align'=>'left',
  'bold'=>0,
  'color'=>0x0A
  )); 
$grouplevel3Format =& $workbook->addFormat(array('size'=>9,
  'align'=>'left',
  'italic'=>1,
  'color'=>0x11
  ));   
$grouplevel1totalFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'right',
  'bold'=>1,
  'color'=>0x0C
  ));  
$grouplevel2totalFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'right',
  'bold'=>0,
  'color'=>0x0A
  ));  
$grouplevel3totalFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right',
  'italic'=>1,
  'color'=>0x11
  ));    
  
$seloptionFormat =& $workbook->addFormat(array('size'=>9,
  'align'=> 'left'  ,
  'valign'=> 'top'  ,
  'bold'=> 1
  ));
$seloptionFormat->setTextWrap();  
  
$RowNum = 0;
$ColNum = 0;

if ($transType == 'AD'){
	$worksheet->setColumn ($ColNum, $ColNum, 20);
	$worksheet->write($RowNum,$ColNum,"Item Code", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 20);
	$worksheet->write($RowNum,$ColNum,"Warehouse", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 10);
	$worksheet->write($RowNum,$ColNum,"Quantity", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 15);
	$worksheet->write($RowNum,$ColNum,"Cost Price", $boldleftFormat);
	$ColNum += 1;
	
	if ($Module_Project == 1 && $Module_ProjectDt == 1){
		$worksheet->setColumn ($ColNum, $ColNum, 20);
		$worksheet->write($RowNum,$ColNum,"Project Code", $boldleftFormat);
		$ColNum += 1;
	}
	
	if ($Module_CostCenter == 1 && $Module_CostCenterDt == 1){
		$worksheet->setColumn ($ColNum, $ColNum, 20);
		$worksheet->write($RowNum,$ColNum,"Costcenter Code", $boldleftFormat);
		$ColNum += 1;
	}
}	
  
else if ($transType == 'TR'){
	$worksheet->setColumn ($ColNum, $ColNum, 20);
	$worksheet->write($RowNum,$ColNum,"Item Code", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 30);
	$worksheet->write($RowNum,$ColNum,"Item Description", $boldleftFormat);
	$ColNum += 1;
	
	$worksheet->setColumn ($ColNum, $ColNum, 20);
	$worksheet->write($RowNum,$ColNum,"From Warehouse Code", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 20);
	$worksheet->write($RowNum,$ColNum,"To Warehouse Code", $boldleftFormat);
	$ColNum += 1;
	
	$worksheet->setColumn ($ColNum, $ColNum, 10);
	$worksheet->write($RowNum,$ColNum,"Quantity", $boldleftFormat);
	$ColNum += 1;
}	
else if ($transType == 'PI'){
	$worksheet->setColumn ($ColNum, $ColNum, 20);
	$worksheet->write($RowNum,$ColNum,"Item Code", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 30);
	$worksheet->write($RowNum,$ColNum,"Item Description", $boldleftFormat);
	$ColNum += 1;
	
	$worksheet->setColumn ($ColNum, $ColNum, 10);
	$worksheet->write($RowNum,$ColNum,"Quantity", $boldleftFormat);
	$ColNum += 1;
	$worksheet->setColumn ($ColNum, $ColNum, 15);
	$worksheet->write($RowNum,$ColNum,"Price", $boldleftFormat);
	$ColNum += 1;
	
	
}	
$workbook->close();
?>