<?php
require_once('Spreadsheet/Excel/Writer.php');

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


include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$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");

$querylog="select fi_showcostprice,FI_ItemSelling1,FI_ItemSelling2,FI_ItemSelling3,FI_ItemSelling4,FI_ItemSelling5 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);
}
 

$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, report_color1, report_color2, ifNull(InventoryRMaskqty,0) As InventoryRMaskqty, Base1, Base2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
 
}

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse ".
          " 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_Grn,0) As Module_Grn, ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ifNull(Module_Heatno,0) As Module_Heatno From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in ( select warehouse_code from ".$_SESSION['dbLabel'].".loginstk ".
		 "                                          where user_account='".$_SESSION['useraccount']."' ".
		 "                                          and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '') ) ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist  = mysql_num_rows($resultgroup);
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);
	
if($adtype==1)	
	$ReportTitle = 'List of Adjustment\In\Out';
else if($adtype==2)		
	$ReportTitle = 'List of Stock In';
else if($adtype==3)		
	$ReportTitle = 'List of Stock Out';
else if($adtype==4)		
	$ReportTitle = 'List of Adjustment';
		
//Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

//Sending HTTP headers
$workbook->send('Exp_ListofAdjustmentByItem.xls');
 
//Creating a worksheet
$worksheet=&$workbook->addWorksheet('List of of Adjustment');
$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,
  ));
  
 
  
// Regular Format
$regularleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ));
$regularrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ));  
$regularrightFormatNum =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ));  
$regularcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  ));    
  
$regularleftFormat1 =& $workbook->addFormat(array('size'=>8,
  'align'=>'left'  ));
$regularrightFormat1 =& $workbook->addFormat(array('size'=>8,
  'align'=>'right'  ));    
$regularrightFormat1Num =& $workbook->addFormat(array('size'=>8,
  '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
  )); 
$boldrightFormatNum =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ,
  'bold'=>1
  )); 
$boldcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  ,
  'valign'=>'top'  ,
  '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();

$regularrightFormatNum->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$regularrightFormat1Num->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$boldrightFormatNum->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');
$grouplevel1totalFormat->setNumFormat('_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)');

if ($cur == 'currency1'){
   $cur='null';  
   $displaycur = '';
}   
else {
   $displaycur = $cur;
   $cur="'".$cur."'";
}  

$SelectedOptions = "Filter by: \n";
if ($fromdate != '')      $SelectedOptions .= "-> Date From: ". $fromdate . "  To: ". $todate ."\n";	
if ($displaycur != '')    $SelectedOptions .= "-> Currency: ". $displaycur ."<br>";	
if ($fromitem != '')      $SelectedOptions .= "-> Item From: ". $fromitem . "  To: ". $toitem ."\n";

if($Module_Heatno==1 and $vShowHeatno==1)
if ($fromheatno != '')      $SelectedOptions .= "-> HeatNo From: ". $fromheatno . "  To: ". $toheatno ."\n";

if ($fromgroup != '')     $SelectedOptions .= "-> Group From: ". $fromgroup . "  To: ". $togroup ."\n";
if ($fromsubgroup != '')  $SelectedOptions .= "-> SubGroup From: ". $fromsubgroup . "  To: ". $tosubgroup ."\n";
if ($fromcategory != '')  $SelectedOptions .= "-> Category From: ". $fromcategory . "  To: ". $tocategory ."\n";
if ($fromproject != '')   $SelectedOptions .= "-> Project From: ". $fromproject . "  To: ". $toproject . "\n";
if ($fromcostcent != '')  $SelectedOptions .= "-> Costcent From: ". $fromcostcent . "  To: ". $tocostcent . "\n";
if ($fromwarehouse != '') $SelectedOptions .= "-> Warehouse From: ". $fromwarehouse . "  To: ". $towarehouse . "\n";
if ($fromsupp != '')  	  $SelectedOptions .= "-> Supplier From: ". $fromsupp . "  To: ". $tosupp . "\n";


if (!empty($fromdate))   $fromdate = explodeword($fromdate);
if (!empty($todate))     $todate = explodeword($todate);  


$fromcur       = GetSQLValueString($fromcur, 'text');
$fromitem      = GetSQLValueString($fromitem, 'text');
$toitem        = GetSQLValueString($toitem, 'text');
$fromheatno    = GetSQLValueString($fromheatno, 'text');
$toheatno      = GetSQLValueString($toheatno, 'text');
$fromproject   = GetSQLValueString($fromproject, 'text');
$toproject     = GetSQLValueString($toproject, 'text');
$fromcostcent  = GetSQLValueString($fromcostcent, 'text');
$tocostcent    = GetSQLValueString($tocostcent, 'text');
$fromgroup     = GetSQLValueString($fromgroup, 'text');
$togroup       = GetSQLValueString($togroup, 'text');
$fromsubgroup  = GetSQLValueString($fromsubgroup, 'text');
$tosubgroup    = GetSQLValueString($tosubgroup, 'text');
$fromcategory  = GetSQLValueString($fromcategory, 'text');
$tocategory    = GetSQLValueString($tocategory, 'text');
$fromwarehouse = GetSQLValueString($fromwarehouse, 'text');
$towarehouse   = GetSQLValueString($towarehouse, 'text');
$fromdate	   = GetSQLValueString($fromdate, 'date');
$todate        = GetSQLValueString($todate, 'date');   
$fromsupp  	   = GetSQLValueString($fromsupp, 'text');   
$tosupp    	   = GetSQLValueString($tosupp, 'text');   
	
//Display Report Header
$worksheet->write(1,0,$CompanyName, $TitleFormat);
$worksheet->write(1,3,$ReportTitle, $TitleFormat);
               
$worksheet->write(1,7,"Printed Date:", $boldleftFormat);
$worksheet->write(1,8,$printdate[2]."/".$printdate[1]."/".$printdate[0], $regularleftFormat);
$worksheet->write(2,7,"Time:", $boldleftFormat);		
$worksheet->write(2,8,$dt[1]." ".$dt[2], $regularleftFormat);

//Displaying Selected Options	
// Merge cells from row 3, col 0 to row 3, col 0
$worksheet->setMerge(3, 0, 8, 6);
$worksheet->write(3,0,$SelectedOptions, $seloptionFormat);  

$RowNum    = 10;
$ColumnNum = 0;	   

//header1

	if ($period == 1 ) {
		$worksheet->write($RowNum,$ColumnNum,'', $boldleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum,$ColumnNum,'', $boldleftFormat);
		$ColumnNum += 1;
	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

		if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
		else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
		else if ($y == $toyear) { $vMFrom = 1; $vMTo = $tomonth; }
		else { $vMFrom = 1; $vMTo = 12; }
		
		for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
			$worksheet->write($RowNum,$ColumnNum,getMonth($m)." ".$y, $boldleftFormat);
			$ColumnNum += 1;
		}
	}
} 
$RowNum++;
$ColumnNum = 0;

	$worksheet->setColumn ($ColumnNum, $ColumnNum, 15);
	$worksheet->write($RowNum,$ColumnNum,'Item', $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
	$worksheet->write($RowNum,$ColumnNum,'Desc.', $boldleftFormat);
	$ColumnNum += 1;
			 
	if($period == 1){	 
		for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
				$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
				$worksheet->write($RowNum,$ColumnNum,'Qty', $boldleftFormat);
				$ColumnNum += 1;
				$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
				$worksheet->write($RowNum,$ColumnNum,'Amount', $boldleftFormat);
				$ColumnNum += 1;
			}
		}
	}
	else{
		$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
		$worksheet->write($RowNum,$ColumnNum,'Qty', $boldleftFormat);
		$ColumnNum += 1;
		$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
		$worksheet->write($RowNum,$ColumnNum,'Amount', $boldleftFormat);
		$ColumnNum += 1;
	}
		
$RowNum++;
$ColumnNum = 0;


if($adtype==2)
	$adjustmenttype='i';
else if($adtype==3)
	$adjustmenttype='o';
else if($adtype==4)
	$adjustmenttype='a';
else $adjustmenttype='';
 

if($availgroupby==1 or $availgroupby=='' or $availgroupby=='11') {
$query = " Select AdjustmentDt.Item_Code,Item_Description, Sum(ifNull(AdjustmentDt_Quantity,0)), Sum(ifNull(AdjustmentDt_PriceBase1,0) * ifNull(AdjustmentDt_Quantity,0))
			, Adjustment.Costcent_Code, Adjustment.Project_Code
			From Adjustment,AdjustmentDt,Items
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 
			and AdjustmentDt.Item_Code = Items.Item_Code
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
	
		$query = $query."Group by AdjustmentDt.Item_Code
							order by AdjustmentDt.Item_Code";
			}
else if($availgroupby==2)		{	
if ($period == 1){
$query = " Select  Item_Code,Item_Description,Project_Code,Project_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Project_Code,Project_Name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." Project.Project_Code as Project_Code,Project.Project_Name as Project_Name From Adjustment,AdjustmentDt
			Left Join Project on project.Project_code =  AdjustmentDt.Project_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by Project_Code,Item_Code
			order by Project_Code,Item_Code";		
$query = $query." ) as q2";		
} else{
$query = $query."Select  Items.Item_Code,Item_Description, Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total, ";
$query = $query."Project.Project_Code,Project.Project_Name
			From Adjustment,AdjustmentDt
			Left Join Project on project.Project_code =  AdjustmentDt.Project_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
		
		$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		
		$query = $query."Group by AdjustmentDt.Project_Code,Items.Item_Code
			order by AdjustmentDt.Project_Code,Items.Item_Code";
}			
		$groupedby='Project'; //echo $query;
		
		}	
else if($availgroupby==3){	
if ($period == 1){
$query = " Select  Item_Code,Item_Description,Costcent_Code,Costcent_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Costcent_Code,Costcent_Name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." Costcent.Costcent_Code as Costcent_Code,Costcent.Costcent_Name as Costcent_Name From Adjustment,AdjustmentDt
			Left Join Costcent on Costcent.Costcent_code =  AdjustmentDt.Costcent_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by costcent_code,Item_Code
			order by costcent_code,Item_Code";		
$query = $query." ) as q2";		
} else{
		
$query = " Select  Items.Item_Code,Item_Description,Sum(ifNull(AdjustmentDt_Quantity,0)), Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)),Costcent.Costcent_Code,Costcent.Costcent_Name
			From Adjustment,AdjustmentDt
			Left Join Costcent on Costcent.Costcent_code =  AdjustmentDt.Costcent_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
		
		
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
		
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		
		$query = $query." Group by AdjustmentDt.Costcent_Code,Items.Item_Code
			order by AdjustmentDt.Costcent_Code,Items.Item_Code";
	}
		$groupedby='Costcenter';	
}	
else if($availgroupby==4){	
if ($period == 1){
$query = " Select  Item_Code,Item_Description,Warehouse_Code,Warehouse_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Warehouse_Code,Warehouse_Name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." Warehouse.Warehouse_Code as Warehouse_Code,Warehouse.Warehouse_Name as Warehouse_Name From Adjustment,AdjustmentDt
			Left Join Warehouse on Warehouse.Warehouse_code =  AdjustmentDt.Warehouse_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by Warehouse_code,Item_Code
			order by Warehouse_code,Item_Code";		
$query = $query." ) as q2";		
} else{		
$query = " Select  Items.Item_Code,Item_Description,Sum(ifNull(AdjustmentDt_Quantity,0)),Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)),Warehouse.Warehouse_Code,Warehouse.Warehouse_Name
			From Adjustment,AdjustmentDt
			Left Join Warehouse on Warehouse.Warehouse_code =  AdjustmentDt.Warehouse_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
		
		
			$query = $query." Group by AdjustmentDt.Warehouse_Code,Items.Item_Code
			order by AdjustmentDt.Warehouse_Code,Items.Item_Code";
			}
		$groupedby='Warehouse';	
}	
else if($availgroupby==5){	
if ($period == 1){
$query = " Select  Item_Code,Item_Description,Group_Code,Group_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Group_Code,Group_Name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." Items.Group_Code as Group_Code,Grouping.Group_Name as Group_Name From Adjustment,AdjustmentDt
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Left Join Grouping on Grouping.Group_code =  Items.Group_code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by Group_Code,Item_Code
			order by Group_Code,Item_Code";		
$query = $query." ) as q2";		
} else{	
		
$query = " Select  Items.Item_Code,Item_Description,Sum(ifNull(AdjustmentDt_Quantity,0)),Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)),Items.Group_Code,Grouping.Group_Name
			From Adjustment,AdjustmentDt
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Left Join Grouping on Grouping.Group_code =  Items.Group_code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id  
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
		
		
			$query = $query." Group by Items.Group_Code,Items.Item_Code
			order by Items.Group_Code,Items.Item_Code";
			}
		$groupedby='Item Group';	
}	
else if($availgroupby==6){	
if ($period == 1){
$query = " Select  Item_Code,Item_Description,SubGroup_Code,SubGroup_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." SubGroup_Code,SubGroup_Name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." Items.SubGroup_Code as SubGroup_Code,SubGroup.SubGroup_Name as SubGroup_Name From Adjustment,AdjustmentDt
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Left Join SubGroup on SubGroup.SubGroup_code =  Items.SubGroup_code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by SubGroup_Code,Item_Code
			order by SubGroup_Code,Item_Code";		
$query = $query." ) as q2";		
} else{	
				
$query = " Select  Items.Item_Code,Item_Description,Sum(ifNull(AdjustmentDt_Quantity,0)),Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)),Items.SubGroup_Code,SubGroup.SubGroup_Name
			From Adjustment,AdjustmentDt
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Left Join SubGroup on SubGroup.SubGroup_code =  Items.SubGroup_code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id  
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
		
		
			$query = $query." 		
			Group by Items.SubGroup_Code,Items.Item_Code
			order by Items.SubGroup_Code,Items.Item_Code";
			}
		$groupedby='Item SubGroup';	
}	
else if($availgroupby==7){	
if ($period == 1){
$query = " Select  Item_Code,Item_Description,Category_code,Category_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Category_code,Category_Name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." Items.Category_code as Category_code,Category.Category_Name as Category_Name From Adjustment,AdjustmentDt
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Left Join Category on Category.Category_code =  Items.Category_code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by Category_code,Item_Code
			order by Category_code,Item_Code";		
$query = $query." ) as q2";		
} else{	
				
$query = " Select  Items.Item_Code,Item_Description,Sum(ifNull(AdjustmentDt_Quantity,0)),Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)),Items.Category_code,Category.Category_Name
			From Adjustment,AdjustmentDt
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Left Join Category on Category.Category_code =  Items.Category_code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 	 
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
		
		
			$query = $query." 	
			Group by Items.Category_code,Items.Item_Code
			order by Items.Category_code,Items.Item_Code";
			}
		$groupedby='Item Category';	
}			

else if($availgroupby==8){	
if ($period == 1){
$query = " Select  Costcent_Code,Costcent_Name,Project_Code,Project_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Costcent_Code,Costcent_Name,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Project_Code,Project_Name  from ( Select  AdjustmentDt.Costcent_Code as Costcent_Code,costcent.Costcent_Name as Costcent_Name,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." AdjustmentDt.Project_Code as Project_Code ,project.Project_Name as Project_Name
			From Adjustment,AdjustmentDt 
			Left Join Project on project.Project_code =  AdjustmentDt.Project_code
			Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by Project_Code,Costcent_Code
					order by Project_Code,Costcent_Code";		
$query = $query." ) as q2";		//echo $query;
} else{

$query = " Select  AdjustmentDt.Costcent_Code, Costcent_name,Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total
			,Project.Project_Code,Project.Project_Name
			From Adjustment,AdjustmentDt
			Left Join Project on project.Project_code =  AdjustmentDt.Project_code
			Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		
		$query = $query."Group by AdjustmentDt.Project_Code,AdjustmentDt.Costcent_Code
			order by AdjustmentDt.Project_Code,AdjustmentDt.Costcent_Code";
			}
		$groupedby='Project';
		
		}
else if($availgroupby==9){
if ($period == 1){
$query = " Select  Project_Code,Project_Name,Costcent_Code,Costcent_Name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Costcent_Code,Costcent_Name,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." Project_Code,Project_Name  from ( Select  AdjustmentDt.Costcent_Code as Costcent_Code,costcent.Costcent_Name as Costcent_Name,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." AdjustmentDt.Project_Code as Project_Code ,project.Project_Name as Project_Name
			From Adjustment,AdjustmentDt 
			Left Join Project on project.Project_code =  AdjustmentDt.Project_code
			Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by Costcent_Code,Project_Code
					order by Costcent_Code,Project_Code";		
$query = $query." ) as q2";		//echo $query;
} else{	
$query = " Select  AdjustmentDt.Project_Code, Project_Name,Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total
			,costcent.Costcent_code,Costcent_name
			From Adjustment,AdjustmentDt
			Left Join Project on project.Project_code =  AdjustmentDt.Project_code
			Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
				
		$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
		
		$query = $query."Group by AdjustmentDt.Costcent_Code,AdjustmentDt.Project_Code
			order by AdjustmentDt.Costcent_Code,AdjustmentDt.Project_Code";
			}
		$groupedby='CostCenter';
		
		}
else if($availgroupby==10){
	
if ($period == 1){
$query = " Select  Item_Code,Item_Description, ledger_number, ledger_name ";

	for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query.",SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y." ";
		
		} }
		
$query = $query." from ( Select  Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
		
		} }
$query = $query." ledger_number,ledger_name  from ( Select  Items.Item_Code as Item_Code,Item_Description,";	
for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
						,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
		} }
$query = $query." items.ledger_number as ledger_number,ledger.ledger_name as ledger_name From Adjustment,AdjustmentDt
			Left Join Warehouse on Warehouse.Warehouse_code =  AdjustmentDt.Warehouse_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			left join ledger on ledger.ledger_number = items.ledger_number
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

for ( $y = $fromyear; $y <= $toyear; $y += 1) {

			if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
			else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
			else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
			else { $vMFrom = 1; $vMTo = 12; }
			
			for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
		$query = $query."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
		} }			
$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ".
						" And ifNull(Items.ledger_number,'-1') between ifNull(". $fromsupp .",ifNull(Items.ledger_number,'-1')) and ifNull(". $tosupp.",ifNull(Items.ledger_number,'-1'))  ";
				
		
						
$query = $query." ) as q1 Group by ledger_number,Item_Code
			order by ledger_number,Item_Code";		
$query = $query." ) as q2";		
} else{		
$query = " Select  Items.Item_Code,Item_Description,Sum(ifNull(AdjustmentDt_Quantity,0)),Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)),items.ledger_number,ledger.ledger_Name
			From Adjustment,AdjustmentDt
			Left Join Warehouse on Warehouse.Warehouse_code =  AdjustmentDt.Warehouse_code
			Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
			left join ledger on ledger.ledger_number = items.ledger_number
			Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id
			and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			$query = $query." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$query = $query." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
						" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
						" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
						" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
						" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
						" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
						" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ".
						" And ifNull(Items.ledger_number,'-1') between ifNull(". $fromsupp .",ifNull(Items.ledger_number,'-1')) and ifNull(". $tosupp.",ifNull(Items.ledger_number,'-1'))  ";
		
		
			$query = $query." Group by items.ledger_number,Items.Item_Code
			order by items.ledger_number,Items.Item_Code";
			}
		$groupedby='Supplier';	
		
		}		
//echo $query;
//exit;
$result=mysql_query($query, $connection) or die(mysql_error());
$i=1;
$projectCode='xyu-1';

$vgroupbydetail=$_GET['vgroupbydetail'];
$col=0;

while($rows=mysql_fetch_row($result)){
	if($period==1) {$prjctcode = $rows[2];$prjctname=$row[3];}
	else {$prjctcode = $rows[4];$prjctname=$row[5];}
	
	if(strtoupper($projectCode)!=strtoupper($prjctcode) and ($availgroupby!=1 and $availgroupby!='' and $availgroupby !='11')  ){
		
		if($i>1){
			$RowNum++;
			$ColumnNum = 0;
		}
	if($availgroupby == 2){
		if ($period == 1){
			$queryAdd = " Select  ";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {

				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
			$queryAdd = $queryAdd."SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y.", ";
			
			} }
			
			$queryAdd = $queryAdd." 1 from ( select ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd." sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
				} 
			}
			$queryAdd = $queryAdd." Project_Code  from ( Select  ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
							,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
				} 
			}
			$queryAdd = $queryAdd." Project.Project_Code as Project_Code From Adjustment,AdjustmentDt
								Left Join Project on project.Project_code =  AdjustmentDt.Project_code
								Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
								Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
								and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
				} 
			}			
			$queryAdd = $queryAdd." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
							" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
							" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
							" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
							" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
							" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
							" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
					
			$queryAdd = $queryAdd." and if('$prjctcode' = '',AdjustmentDt.Project_Code is null, AdjustmentDt.Project_Code = '$prjctcode') ";
				
			
							
			$queryAdd = $queryAdd." ) as q1 Group by Project_Code";		
			$queryAdd = $queryAdd." ) as q2"; 	//echo $queryAdd;
		} 			
		else{
				
			$queryAdd = " Select  Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total
							From Adjustment,AdjustmentDt
							Left Join Project on project.Project_code =  AdjustmentDt.Project_code
							Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
						and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
				
				
			$queryAdd = $queryAdd." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$queryAdd = $queryAdd." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
							" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
							" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
							" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
							" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
							" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
							" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
					
			$queryAdd = $queryAdd." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) 
			and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and if('$prjctcode' = '',AdjustmentDt.Project_Code is null, AdjustmentDt.Project_Code = '$prjctcode') ";
			
			$queryAdd = $queryAdd."Group by AdjustmentDt.Project_Code
				order by AdjustmentDt.Project_Code"; 
		}
	}
	if($availgroupby == 3){
		if ($period == 1){
			$queryAdd = " Select  ";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y.", ";
				} 
			}
		
			$queryAdd = $queryAdd." 1 from ( select ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd." sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
				} 
			}
			$queryAdd = $queryAdd." Costcent_code  from ( Select  ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
							,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
				} 
			}
			
			$queryAdd = $queryAdd." costcent.Costcent_code as Costcent_code From Adjustment,AdjustmentDt
						Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
						Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
						and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
				} 
			}			
			$queryAdd = $queryAdd." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
							" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
							" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
							" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
							" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
							" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
							" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
					
			$queryAdd = $queryAdd." and if('$prjctcode' = '',AdjustmentDt.Costcent_Code is null, AdjustmentDt.Costcent_Code = '$prjctcode')  ";
				
		
						
			$queryAdd = $queryAdd." ) as q1 Group by Costcent_Code";		
			$queryAdd = $queryAdd." ) as q2"; 	//echo $queryAdd;
		} 			
		else {	
			$queryAdd = " Select  Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total
							From Adjustment,AdjustmentDt
							Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
							Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
						and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')
						And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) 
						And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) 	
							 and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) 
							and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) 
						 and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) 
						 and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) 
						 And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  
						 And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  
						 And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) 
						and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and if('$prjctcode' = '',AdjustmentDt.Costcent_Code is null, AdjustmentDt.Costcent_Code = '$prjctcode') 
							Group by AdjustmentDt.Costcent_Code
							order by AdjustmentDt.Costcent_Code"; 
		}
	}
	if($availgroupby == 8){
		if ($period == 1){
			$queryAdd = " Select  ";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y.", ";
				} 
			}
		
			$queryAdd = $queryAdd." 1 from ( select ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
					if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
					else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
					else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
					else { $vMFrom = 1; $vMTo = 12; }
					
					for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
						$queryAdd = $queryAdd." sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
					}
			}
			$queryAdd = $queryAdd." Project_Code  from ( Select  ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
									,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
				} 
			}
			$queryAdd = $queryAdd." Project.Project_Code as Project_Code From Adjustment,AdjustmentDt
						Left Join Project on project.Project_code =  AdjustmentDt.Project_code
						Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
						Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
						and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
			
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
				} 
			}			
			$queryAdd = $queryAdd." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
							" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
							" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
							" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
							" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
							" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
							" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
					
			$queryAdd = $queryAdd." and if('$prjctcode' = '',AdjustmentDt.Project_Code is null, AdjustmentDt.Project_Code = '$prjctcode') ";
			$queryAdd = $queryAdd." ) as q1 Group by Project_Code";		
			$queryAdd = $queryAdd." ) as q2"; 	//echo $queryAdd;
		} 			
		else {		
			$queryAdd = " Select  Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total
						From Adjustment,AdjustmentDt
						Left Join Project on project.Project_code =  AdjustmentDt.Project_code
						Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
						Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
						and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			
			$queryAdd = $queryAdd." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) ";
			$queryAdd = $queryAdd." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
							" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
							" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
							" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
							" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
							" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
							" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
					
			$queryAdd = $queryAdd." And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) 
						and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and if('$prjctcode' = '',AdjustmentDt.Project_Code is null, AdjustmentDt.Project_Code = '$prjctcode')  ";
			
			$queryAdd = $queryAdd."Group by AdjustmentDt.Project_Code
								order by AdjustmentDt.Project_Code";
		}
	}
				
	if($availgroupby == 9){
		if ($period == 1){
			$queryAdd = " Select  ";
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."SumQty".getMonth($m).$y.", SumAmount".getMonth($m).$y.", ";
				} 
			}
		
			$queryAdd = $queryAdd." 1 from ( select ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd." sum(SumQty".getMonth($m).$y.") as SumQty".getMonth($m).$y." ,sum(SumAmount".getMonth($m).$y.") as SumAmount".getMonth($m).$y.", ";
				}
			}
			$queryAdd = $queryAdd." Costcent_Code  from ( Select  ";	
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
					if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
					else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
					else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
					else { $vMFrom = 1; $vMTo = 12; }
					for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
						$queryAdd = $queryAdd."if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0), 0) AS SumQty".getMonth($m).$y."
											,if(Month(Adjustment.Adjustment_date)='".$m."' And Year(Adjustment.Adjustment_date)='".$y."', ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0), 0) AS SumAmount".getMonth($m).$y.",";
					} 
			}
			$queryAdd = $queryAdd." costcent.Costcent_Code as Costcent_Code From Adjustment,AdjustmentDt
						Left Join Project on project.Project_code =  AdjustmentDt.Project_code
						Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
						Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
						and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";

			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$queryAdd = $queryAdd."And concat(Year(Adjustment.Adjustment_date),LPAD(Month(Adjustment.Adjustment_date), 2, '0')) between concat(ifNull('".$fromyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($frommonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) and concat(ifNull('".$toyear."', Year(Adjustment.Adjustment_date)),ifNull('".str_pad($tomonth,2,'0',STR_PAD_LEFT)."', LPAD(Month(Adjustment.Adjustment_date), 2, '0'))) ";
				} 
			}			
			$queryAdd = $queryAdd." And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) ".	
							" and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
							" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
							" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
							" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
							" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
							" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";
					
			$queryAdd = $queryAdd." and if('$prjctcode' = '',AdjustmentDt.Costcent_Code is null, AdjustmentDt.Costcent_Code = '$prjctcode') ";
			$queryAdd = $queryAdd." ) as q1 Group by Costcent_Code";		
			$queryAdd = $queryAdd." ) as q2"; 	//echo $queryAdd;
		} 			
		else {
			$queryAdd = " Select  Sum(ifNull(AdjustmentDt_Quantity,0)) as sum_qunatity, Sum(ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_PriceBase1,0)) as sum_total
					From Adjustment,AdjustmentDt
					Left Join Project on project.Project_code =  AdjustmentDt.Project_code
					Left Join costcent on costcent.Costcent_code = AdjustmentDt.Costcent_Code
					Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
				Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 		
				and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')
				And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) 
				And items.item_code between ifNull(".$fromitem.",items.item_code) and ifNull(". $toitem.",items.item_code) 	
					 and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) 
					and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) 
				 and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) 
				 and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) 
				 And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  
				 And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  
				 And date_format(Adjustment.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) 
				and ifNull(". $todate.",date_format(Adjustment.Adjustment_date,'%Y-%m-%d')) and if('$prjctcode' = '',AdjustmentDt.Costcent_Code is null, AdjustmentDt.Costcent_Code = '$prjctcode') 
				Group by AdjustmentDt.Costcent_Code
				order by AdjustmentDt.Costcent_Code"; 
		}
	}				
		$res = mysql_query($queryAdd, $connection);
		$rowsQty=mysql_fetch_row($res);		
		$RowNum++;
		$ColumnNum = 0;	
		$worksheet->write($RowNum, $ColumnNum, $groupedby."-> ".$prjctcode." ".$prjctname, $regularleftFormat);
		$ColumnNum += 1;
		
		if($availgroupby == 2 || $availgroupby == 3 || $availgroupby == 8 || $availgroupby == 9 ){
			if($period==1){
				for ( $y = $fromyear; $y <= $toyear; $y += 1) {

					if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
					else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
					else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
					else { $vMFrom = 1; $vMTo = 12; }
					$jj=0;
					for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
						$worksheet->writeNumber($RowNum, $ColumnNum, round($rowsQty[$jj], $DecimalBase), $regularcenterFormat);
						$ColumnNum += 1;
						$jj=$jj+1;
						$worksheet->writeNumber($RowNum, $ColumnNum, round($rowsQty[$jj], $DecimalBase), $regularcenterFormat);
						$ColumnNum += 1;
						$jj=$jj+1; 
					} 
				}	
			} 
			else {
				$worksheet->writeNumber($RowNum, $ColumnNum, round($rowsQty[0], $DecimalBase), $regularcenterFormat);
				$ColumnNum += 1;
				$worksheet->writeNumber($RowNum, $ColumnNum, round($rowsQty[1], $DecimalBase), $regularcenterFormat);
				$ColumnNum += 1;	
			}	 
		}
	}
	
	if($vgroupbydetail == 1 ){
		$RowNum++;
		$ColumnNum = 0;
		
		$worksheet->write($RowNum, $ColumnNum, $rows[0], $regularleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, $rows[1], $regularleftFormat);
		$ColumnNum += 1;
		$ij=4;
		if ($period == 1){
			for ( $y = $fromyear; $y <= $toyear; $y += 1) {
				if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
				else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
				else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
				else { $vMFrom = 1; $vMTo = 12; }
			
				for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
					$worksheet->write($RowNum, $ColumnNum, $rows[$ij], $regularcenterFormat);
					$ColumnNum += 1;
					$ij=$ij+1;
					$worksheet->writeNumber($RowNum, $ColumnNum, round($rows[$ij], $DecimalBase), $regularcenterFormat);
					$ColumnNum += 1;
					$ij=$ij+1;
				} 
			}
		}
		else {
			$worksheet->write($RowNum, $ColumnNum, $rows[2], $regularcenterFormat);
			$ColumnNum += 1;
			$worksheet->writeNumber($RowNum, $ColumnNum, round($rows[3], $DecimalBase), $regularcenterFormat);
			$ColumnNum += 1;
		}
	}
	
	if($availgroupby==1 or $availgroupby=='' or $availgroupby=='11') {	
		if($vgroupbydetail == 0 ){
			$RowNum++;
			$ColumnNum = 0;	
			
			$worksheet->write($RowNum, $ColumnNum, $rows[0], $regularleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum, $ColumnNum, $rows[1], $regularleftFormat);
			$ColumnNum += 1;
		
			$ij=4;
			if ($period == 1){
				for ( $y = $fromyear; $y <= $toyear; $y += 1) {
					if ($y == $fromyear && $y == $toyear) { $vMFrom = $frommonth; $vMTo = $tomonth; }
					else if ($y == $fromyear) { $vMFrom = $frommonth; $vMTo = 12; }
					else if ($y == $toyear) { $vMFrom = 1; $vMTo = $PToMonth; }
					else { $vMFrom = 1; $vMTo = 12; }
				
					for ( $m = $vMFrom; $m <= $vMTo; $m += 1) {
						$worksheet->write($RowNum, $ColumnNum, $rows[$ij], $regularcenterFormat);
						$ColumnNum += 1;
						$ij=$ij+1;
						$worksheet->writeNumber($RowNum, $ColumnNum, round($rows[$ij], $DecimalBase), $regularcenterFormat);
						$ColumnNum += 1;
						$ij=$ij+1;
					} 
				}
			}
			else{
				$worksheet->write($RowNum, $ColumnNum, $rows[2], $regularcenterFormat);
				$ColumnNum += 1;
				$worksheet->writeNumber($RowNum, $ColumnNum, round($rows[3], $DecimalBase), $regularcenterFormat);
				$ColumnNum += 1;
			}
		}
		
		if($vgroupbydetail == 1 ){
			$RowNum++;
			$ColumnNum = 0;	
			
			$worksheet->write($RowNum,$ColumnNum,'', $boldleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum,$ColumnNum,'Reference', $boldleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum,$ColumnNum,'Date', $boldleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum,$ColumnNum,'Qty', $boldleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum,$ColumnNum,'Curr.', $boldleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum,$ColumnNum,'Amt', $boldleftFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum,$ColumnNum,'Type', $boldleftFormat);
			$ColumnNum += 1;
			
			$RowNum++;
			$ColumnNum = 0;	
				 
			$querydt = " Select Adjustment_Reference,Date_Format(Adjustment.Adjustment_Date,'%d/%m/%Y'), Sum(ifNull(AdjustmentDt_Quantity,0))
						, Sum(ifNull(Adjustmentdt_pricebase1,0) * ifNull(AdjustmentDt_Quantity,0)),Adjustment_EntryType,Adjustment.Currency_Code
						From Adjustment,AdjustmentDt
						Inner Join items on items.Item_Code = AdjustmentDt.Item_Code
						Where Adjustment.Adjustment_id  = AdjustmentDt.Adjustment_id 
						and AdjustmentDt.Item_Code  = '".$rows[0]."' ";
			  
			$querydt = $querydt." And date_format(Adjustmentdt.Adjustment_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Adjustmentdt.Adjustment_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Adjustmentdt.Adjustment_date,'%Y-%m-%d')) ";
			$querydt = $querydt." and Adjustment.Adjustment_EntryType = if('".$adjustmenttype."'='',Adjustment.Adjustment_EntryType,'".$adjustmenttype."')";
			$querydt = $querydt." and ifNull(items.project_code,'-1') between ifNull(".$fromproject.",ifNull(items.project_code,'-1')) and ifNull(". $toproject.",ifNull(items.project_code,'-1')) ".
								" and ifNull(items.costcent_code,'-1') between ifNull(".$fromcostcent.",ifNull(items.costcent_code,'-1')) and ifNull(". $tocostcent.",ifNull(items.costcent_code,'-1')) ".
								" and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ".
								" and ifNull(items.group_code,'-1') between ifNull(". $fromgroup .",ifNull(items.group_code,'-1')) and ifNull(". $togroup.",ifNull(items.group_code,'-1')) ".
								" And ifNull(Items.SubGroup_Code,'-1') between ifNull(". $fromsubgroup .",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1'))  ".
								" And ifNull(Items.Category_Code,'-1') between ifNull(". $fromcategory .",ifNull(Items.Category_Code,'-1')) and ifNull(". $tocategory.",ifNull(Items.Category_Code,'-1'))  ";

			$querydt = $querydt." and ifNull(AdjustmentDt.warehouse_code,'-1') between ifNull(". $fromwarehouse .",ifNull(AdjustmentDt.warehouse_code,'-1')) and ifNull(". $towarehouse.",ifNull(AdjustmentDt.warehouse_code,'-1')) ";
			$querydt = $querydt."group by Adjustment.Adjustment_id,Adjustment_EntryType ";
			$querydt = $querydt."order by Adjustment_Reference,AdjustmentDt.Item_Code";
				
			//echo $querydt;//exit;
			$resultdt=mysql_query($querydt, $connection) or die(mysql_error());
			while($rowsdt=mysql_fetch_row($resultdt)){	
				$worksheet->write($RowNum, $ColumnNum, $rowsdt[0], $regularleftFormat);
				$ColumnNum += 1;
				$worksheet->write($RowNum, $ColumnNum, $rowsdt[1], $regularleftFormat);
				$ColumnNum += 1;
				$worksheet->write($RowNum, $ColumnNum, $rowsdt[2], $regularleftFormat);
				$ColumnNum += 1;
				$worksheet->write($RowNum, $ColumnNum, $Base1, $regularcenterFormat);
				$ColumnNum += 1;
				$worksheet->writeNumber($RowNum, $ColumnNum, $rowsdt[3], $regularcenterFormat);
				$ColumnNum += 1;
				$worksheet->write($RowNum, $ColumnNum, $rowsdt[4], $regularcenterFormat);
				$ColumnNum += 1;
				$RowNum++;
				$ColumnNum = 0;	
			}
		}
		
	}
	
	if($availgroupby==1 or $availgroupby=='' or $availgroupby=='11'){
		$RowNum++;
		$ColumnNum = 0;	
	}	
		
	if($period==1) $projectCode = $rows[2];
	else $projectCode = $rows[4];
		
	$i+=1;	
}
function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

	$trim = explode('/',$date);
	$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
	return $trim;
}

function GetSQLValueString($theValue, $theType) 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}

function getMonth($month){
     return date('M', mktime(0,0,0,$month,1));
}
$workbook->close();


?>