<?php 
session_name('CAKEPHP'); session_start();
error_reporting(FALSE);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$title = _($s->getField('modules_form','id',$_REQUEST['frmid'],'description'));
$date = date($_SESSION['DefaultDateFormat']);
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmView']=$frmpermission[3];

if($_SESSION['frmView']=='0')
{
	echo "<strong>You are not authorised to view this form. Contact your administrator</strong>"; 
	exit;
}


?>
<link rel="stylesheet" href="css/new_mxerp.css">
<script language="javascript" type="text/javascript">
function showJobCardNo(str)
{ 
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{ // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var data=xmlhttp.responseText.split("__");	
			document.getElementById("storedata7").innerHTML=data[0];
			document.getElementById("storedata2").innerHTML=data[1];
		}
	}
	xmlhttp.open("GET","get_card_no.php?site="+str,true);
	xmlhttp.send();
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
}

function validatefrm()
{
	if(document.getElementById("site").value=='')
	{
		alert("Select Site First");
		document.getElementById("site").focus();
		return false;
	}
	if(document.getElementById("fromdate").value=='')
	{
		alert("Select From Date");
		document.getElementById("fromdate").focus();
		return false;
	}
	if(document.getElementById("todate").value=='')
	{
		alert("Select To Date");
		document.getElementById("todate").focus();
		return false;
	}
	if(document.getElementById("asset_name").value=='')
	{
		alert("Select Asset Name First");
		document.getElementById("asset_name").focus();
		return false;
	}
}
</script>
<div class="body_contain">
<div class="tab_div">
<form action="" method="post">
<h1><?php echo $title;?></h2>
<table width="100%" cellpadding="3" cellspacing="0" border="0">
    <tr class="row1">
        <td width="13%" align="right"><strong>Branch</strong></td>
        <td width="32%">
       	  <select  id = "site" name="site" onchange= "showJobCardNo(this.value)" style="width:98%;">
          		<option value = "">--Select--</option>
          		<?php 
            	$site_sql=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                while($site_row=DB_fetch_array($site_sql))
			 	{?>
          		<option value="<?php echo $site_row['loccode']?>"<?php echo($_REQUEST['site']==$site_row['loccode']?'selected':''); ?>><?php echo $site_row['locationname']?></option>
          		<?php } ?>
        	</select>
        </td>
        <td width="13%" align="right"><strong>Asset Name</strong></td>
        <td>
        	<?php $record=$s->GetRecordById('machine_center',1);
		 	$record=DB_query("Select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);	  ?>
            <div id= "storedata2"><select name="asset_name" id="asset_name" style="width:98%;">
			<option value="0"<?php echo($_REQUEST['asset_name']=='0'?'selected':''); ?>>All</option>
            <?php while($rown=DB_fetch_array($record)){ ?>
            <option  value="<?php echo $rown['id']; ?>"<?php echo($_REQUEST['asset_name']==$rown['id']?'selected':''); ?>><?php echo $rown['code']."---".$rown['name']; ?></option>
            <?php }?>
            </select>
            </div>
        </td>
    </tr>
    <tr class="row1">
        <td align="right"><strong>From Date</strong></td>
        <td><input type="text" name="fromdate" id="fromdate" class="datepicker" value="<?php  echo $_REQUEST['fromdate']?>" style="width:98%;" /></td>
		<td align="right"><strong>To Date</strong></td>
		<td><input type="text" name="todate" id="todate" class="datepicker" value="<?php  echo $_REQUEST['todate']?>"  style="width:98%;"/></td>
	</tr>
	<tr class="row1">
		<td colspan="4" align="center" style="text-align:center;"><input type="submit" name="submit" value="View" id="view" onClick="return validatefrm()"  class="search" /></td>
    </tr>
</table>
</form>

<?php 
if($_REQUEST['submit']!='')
{
	$fromdate=$_REQUEST['fromdate'];
	$todate=$_REQUEST['todate'];
	
	$fromdate_brk=explode('/',$fromdate);
	$from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0];
	$todate_brk=explode('/',$todate);
	$to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0];

	$sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
	$assetname=$s->getField('machine_center','id',$_REQUEST['asset_name'],'name');
	
	$filter=array();
	if($_REQUEST['site']!='')
	{
		$filter[]="tpl_breakdown_mgmt.site = '".$_REQUEST['site']."'";
	}
	if($_REQUEST['asset_name']!='' && $_REQUEST['asset_name']!='0')
	{
		$filter[]="tpl_breakdown_mgmt_machine.asset_id = '".$_REQUEST['asset_name']."'";
	}
	if($_REQUEST['asset_name']=='0')
	{
		$filter[]="1=1";
	}
	if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
		$filter[]="tpl_breakdown_mgmt.asset_rec_date_time BETWEEN '".$from."' and '".$to."'";
	}
	//print_r($filter);
	if($filter!='')
	{
		$filter1=implode(' and ',$filter); 
	}
	
	 $sql12="Select *, tpl_breakdown_mgmt.id as main_id from tpl_breakdown_mgmt join tpl_breakdown_mgmt_machine on (tpl_breakdown_mgmt.id=tpl_breakdown_mgmt_machine.master_id) where $filter1 and tpl_breakdown_mgmt.CompanyId='".$_SESSION['ACGCompanyId']."' group by tpl_breakdown_mgmt_machine.asset_id order by tpl_breakdown_mgmt.asset_rec_date_time ";

	if($_REQUEST['site']!='' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='' && $_REQUEST['asset_name']=='')
	{
		$message="Showing Records of Site ".$sitename." ";
	
	}
	elseif($_REQUEST['site']!='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='' && $_REQUEST['asset_name']=='')
	{
		$message="Showing Records of Site ".$sitename."   From  ".$fromdate." to ".$todate."";
	}
	elseif($_REQUEST['site']!='0' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='' && $_REQUEST['asset_name']!='')
	{
		$message="Showing Records of Site ".$sitename." for  ".$assetname."  From  ".$fromdate." to ".$todate." ";
	}
	else if($_REQUEST['site']!='0' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='' && $_REQUEST['asset_name']!='')
	{
		$message="Showing Records of Site ".$sitename." for ".$assetname."";
	
	}

	
	$_SESSION['asset_consumption']=$sql12;
	$_SESSION['mess']=$message;
	?>
	<p align="right"><a href="asset_consumption_report_print.php" target="_blank" class="search">Print</a></p>
	<h3><strong><?php echo $message; ?></strong></h3>	
		<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:#808080 1px solid; border-collapse:collapse;">
			<tr>
					<!--  <th width="10%"><strong>Job Card No.</strong></th>
	     			<th width="10%"><strong>Date</strong> -->
		  			<th width="10%"><strong>Item Code</strong></th>
					<th><strong>Item Name</strong></th>
		  			<th width="10%"><strong>Unit</strong></th>
					<th width="10%"><strong>Qty</strong></th>
				</tr>
				<?php 
				$i=0;
				$result12=DB_query($sql12,$db);
				while($myrow1=DB_fetch_array($result12))
				{
					$i++;
					extract($myrow1);

					$sqlAset = "SELECT * FROM machine_center WHERE ID= ".$asset_id."";
					$resultDisplay = DB_query($sqlAset,$db);
					$assetRows = DB_fetch_array($resultDisplay);

					// asset group
					$reqDet_sql1 = "SELECT * FROM machine_group_master WHERE id= ".$assetRows['under_group']."";
						$result1 = DB_query($reqDet_sql1,$db);
						$assetRow1 = DB_fetch_array($result1);
				?>
				<tr bgcolor="#fff">
					<td><strong>Asset Code---<?php echo $assetRows['code']; ?></strong></td>
	     			<td><strong>Asset Name---<?php echo $assetRows['name']; ?></strong></td>
		  			<td><strong>Asset Group---<?php echo $assetRow1['machine_gp_location']; ?></strong></td>
		  			<td>&nbsp;</td>
		  			<!-- <?php 
   					$title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
					while($r=DB_fetch_array($title1))
					{ ?> 
					<td><strong><strong><?php echo $r['title_name'];?>---<?php echo $assetRows[$r['db_field_name']]; ?></strong></td>
					<?php } ?> -->
				</tr>
				<?php 
				$ass_id=$s->getField('tpl_breakdown_mgmt_assig','job_id',$main_id,'id');
				$jobcard_id=$s->getField('tpl_breakdown_mgmt_assig','job_id',$main_id,'job_id');

				$mrs_id=$s->getField('material_request_slip','jobcard_id',$jobcard_id,'id');
				//echo "job=".$mrs_id;
				$issue_id_query=DB_query("select id from addissue WHERE mrs_id='".$mrs_id."'",$db);
				$issue_id_array=array();
				while($issue_id_result=DB_fetch_array($issue_id_query))
				{
					$issue_id_array[]=$issue_id_result['id'];
				}
				//echo "<pre>"; print_r($issue_id_array);
			    /* $reqDet_sql="SELECT tbl_asset_assem_item.*,tbl_asset_assem_item_details.* from tbl_asset_assem_item left join tbl_asset_assem_item_details on tbl_asset_assem_item.id=tbl_asset_assem_item_details.parentid where 1"; */
                
                 $reqDet_sql="SELECT tbl_asset_assem_item.*,tbl_asset_assem_item_details.*,tbl_asset_assem_item.id as main_id from tbl_asset_assem_item left join tbl_asset_assem_item_details on tbl_asset_assem_item.id=tbl_asset_assem_item_details.parentid where 1";

				
				if($ass_id!="")
				{
					$reqDet_sql .=" and tbl_asset_assem_item_details.parentid= ".$ass_id;
				}
				$result = DB_query($reqDet_sql,$db);
				if(DB_num_rows($result)>0){
					$item_name = '';
					$total_qty=0;
				while($assetRow = DB_fetch_array($result))
				{
					 //echo $assetRow['tbl_asset_assem_item_details.parentid'];
					//echo  $sql_get_qty="select * from tbl_asset_assem_item_details where parentid='".$assetRow['id']."'";
					$item1=DB_query("select * from item_master where id='".$assetRow['mst_itemid']."' group by id",$db);
      				$item1row=DB_fetch_array($item1);
      				$total_qty+=$assetRow['qty'];

      				if($item_name!=$item1row['item_name'])
      				{
      					//echo "select sum(qty) as qty from tbl_asset_assem_item_details where parentid = '".$assetRow['main_id']."' ";

      					$data = DB_query("select sum(qty) as qty from tbl_asset_assem_item_details where parentid = '".$assetRow['main_id']."' ",$db);
      					$a = DB_fetch_array($data);
      					//print_r($a);
				?>
				<tr bgcolor="#fff">
					<!-- <td align="left" valign="top"><?php  echo $ass_id=$s->getField('tpl_breakdown_mgmt','id',$main_id,'tt_no');?></td>
					<td align="left" valign="top"><?php  $ass_id_date=$s->getField('tpl_breakdown_mgmt','id',$main_id,'asset_rec_date_time'); echo date(
					'd/m/Y',strtotime($ass_id_date)); ?></td>  -->
					<td align="left" valign="top"><?php echo $item1row['StockID']; ?></td>
					<td align="left" valign="top"><?php echo $item1row['item_name']; ?></td>
					<td align="left" valign="top"><?php echo $s->getField('unitsofmeasure','unitid',$item1row['mst_unitid'],'unitname'); ?></td>
					<td align="left" valign="top"><?php echo $a['qty'];//$assetRow['qty']; ?></td>
				</tr>
				<?php	$item_name = $item1row['item_name']; }
					}
				}?>
					
				<?php } ?>
			</table>
			
<?php } ?>
		</div>
	</div>
