<?php
error_reporting(0);  
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="panel-heading">
              <div class="panel-title"> <span class="glyphicon glyphicon-list-alt"></span>Stock Valuation Entry</div>
              <div class="panel-btns pull-right margin-left">     
                
              </div>
            </div>

<form method="post" action="">
<div class="panel-footer">
<table width="50%" border="0" cellspacing="5" cellpadding="3"  align="center" >
	<tr>

    	<td><strong>Branch<span style="color:#FF0000">*</span></strong></td>
        <td><?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql1=DB_query($ss1,$db);
	  }
	  else
	  {
		$ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql1=DB_query($ss1,$db);
	  }
	?>
			<select name="site" id="site"  class="form-control input-sm">
                <option value="0">Select Site</option>
                <?php 
				
				while($site_row = DB_fetch_array($site_sql1))
				{
					$p='';
					if($site_row['loccode'] == $_POST['site'])	
					$p="selected";
				?>
                <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
                <?php } ?>
        	</select>
		</td>
      
        <td >Item :</td>
        <td>
        <select name="item" id="item">
        <option value="">select Item</option>
          <?php 
          $item_sql=DB_query("select id,StockID,item_name from item_master",$db);
          while($row3=DB_fetch_array($item_sql))
          { 
            $c='';
            if($_REQUEST['item']==$row3['id'])
              {$c="selected";}
          ?>
                  <option value="<?php echo $row3['id'];?>" <?php echo $c;?>/><?php echo $row3['item_name'];?></option>
                  <?php 
    
          }
   
        ?>

        </select>
        </td>
        
       
   <td width="20%" style="padding-left:20px;"><input type="submit" name="view" value="Process Stock Valuation" onclick="return checkval()" class="btn btn-primary"></td>
    </tr>
</table>
	</div>	 
</form>


<div class="panel-body">
             
              <div class="table-responsive">
                <table class="table table-bordered table-striped">
  
  <tr class="row1">
	 <th><strong>S.R.</strong></th>
      <th><strong>Date</strong></th>
      <th><strong>Item Name</strong></th>
	   <th><strong>Particular</strong></th>
      <th><strong>Transaction No.</strong></th>
       <th width="17%"><center>Received
    </center></th>
    <th width="17%"><center>Issued
    </center></th>
    <th><center>Closing Balance</center></th>
  </tr>
  <tr><td colspan="5"></td>
    <td style="margin:0px; padding:0px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <th align="left">QTY.</th>
 <th align="left" >Rate</th>
    <th align="left" >Value</th>
  </tr>
</table>	</td>
    <td style="padding:0px; margin:0px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th align="left">QTY.</th>
   <th align="left" >Rate</th>
    <th align="left">Value</th>
  </tr>
</table>	</td>
    <td style="padding:0px; margin:0px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th align="left" >QTY.</th>
   <th align="left" >Rate</th>
    <th align="left">Value</th>
  </tr>
</table></td>
  
  </tr>
  
   
    <?php
    
    if(isset($_POST['view']))
    {
    if($_REQUEST['item']=='')
    {
     $sql="select stock_inward.id as id, stock_inward.site as site,stock_inward.mst_itemid as item,stock_inward.inwardqty as qty,
          stock_inward.inward_rate as rate ,stock_inward.inwared_value  as amount
          ,stock_inward.date  as date,stock_inward.trasaction as trasaction,
          stock_inward.type as type,stock_inward.against_id as typeid,stock_inward.against_tra_id as transid
          from stock_inward where site='".$_REQUEST['site']."' ORDER BY date asc, TIME(date) asc";
    }else{
      $sql="select stock_inward.id as id, stock_inward.site as site,stock_inward.mst_itemid as item,stock_inward.inwardqty as qty,
          stock_inward.inward_rate as rate ,stock_inward.inwared_value  as amount
          ,stock_inward.date  as date,stock_inward.trasaction as trasaction,
          stock_inward.type as type,stock_inward.against_id as typeid,stock_inward.against_tra_id as transid
          from stock_inward where site='".$_REQUEST['site']."' and mst_itemid='".$_REQUEST['item']."' ORDER BY date asc, TIME(date) asc";
    }      

      $query=DB_query($sql,$db);
     
      while($row=DB_fetch_array($query))
      {
      $data[]=array('slipno'=>$row['trasaction'],'itemid'=>$row['item'],
	'date'=>$row['date'],'reciveqty'=>$row['qty'],'issueqty'=>'','doc'=>'','description'=>$row['type'],
          'r_rate'=>$row['rate'],'r_amount'=>$row['amount'],'i_rate'=>'','i_amount'=>'' ,'qty'=>$row1['qty'],'id'=>$row1['id']);
      }
          
      if($_REQUEST['item']=='')
      {
       $sql2="select stock_outward.id as id, stock_outward.site as site,stock_outward.mst_itemid as item,stock_outward.outward_qty
           as qty,stock_outward.outward_rate as rate ,stock_outward.outward_value  as amount,
          stock_outward.date  as date,stock_outward.trasaction as trasaction,
          stock_outward.type as type,stock_outward.type_id as typeid,stock_outward.against_qty as transid
          from stock_outward where site='".$_REQUEST['site']."'  ORDER BY date asc, TIME(date) asc";
       }else{
         $sql2="select stock_outward.id as id, stock_outward.site as site,stock_outward.mst_itemid as item,stock_outward.outward_qty
           as qty,stock_outward.outward_rate as rate ,stock_outward.outward_value  as amount,
          stock_outward.date  as date,stock_outward.trasaction as trasaction,
          stock_outward.type as type,stock_outward.type_id as typeid,stock_outward.against_qty as transid
          from stock_outward where site='".$_REQUEST['site']."' and mst_itemid='".$_REQUEST['item']."'  ORDER BY date asc, TIME(date) asc";
       }   
       
       $query1=DB_query($sql2,$db);
     
      while($row1=DB_fetch_array($query1))
      {
        $data[]=array('slipno'=>$row1['trasaction'],'itemid'=>$row1['item'],
	'date'=>$row1['date'],'reciveqty'=>'','issueqty'=>$row1['qty'],'doc'=>'','description'=>$row1['type']
                ,'i_rate'=>$row1['rate'],'i_amount'=>$row1['amount'],'r_rate'=>'','r_amount'=>'','qty'=>$row1['qty'],'id'=>$row1['id']);
      
      }
    foreach ($data as $key => $row2)
    {
    $volume[$key]= $row2['date'];
	 $edition1[$key]=$row2['date'];
	
    }
   // usort($data, 'date_compare');
   array_multisort($volume,$edition1,SORT_DESC,$data);
   //$query=DB_query($sql,$db);
    $i=1;
    
    $count=count($data);
    if($count>0)
    {
    foreach($data as $key=>$value)
	  {    
    
    
    
    ?>
    
    
	<tr class="row1">
	<td><?php echo $i; ?></td>
	<td><?php echo $value['date']; ?></td>
   <td><?php echo $s->getField('item_master','id',$value['itemid'],'item_name'); ?></td>
	<td><?php echo $value['description']; ?></td>
   <td><?php echo $value['slipno']; ?></td>
    <td style="padding:0px; margin:0px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="4">
		<tr>
    <td width="33%" align="left" style="border-right:#c9c9c9 1px solid; height:40px;"><?php   echo $value['reciveqty'];   $recieve[]=$value['reciveqty'];  ?></td>
   <td width="30%" align="left" style="border-right:#c9c9c9 1px solid;"><?php   echo $value['r_rate'];     ?></td>
  <td align="left" style="text-align:right;"><?php echo $value['r_amount'];   $recieveamount[]=$value['r_amount'];   ?></td>
  </tr>
</table>	</td>
    <td style="padding:0px; margin:0px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td width="33%" align="left" style="border-right:#c9c9c9 1px solid;height:40px;""><?php   echo $value['issueqty'];   $issueqty[]=$value['issueqty'];   ?></td>
   <td width="30%" align="left" style="border-right:#c9c9c9 1px solid;height:40px;""><?php echo $value['i_rate']; ?></td>
    <td align="left" style="text-align:right;"><?php echo $value['i_amount']; $issueamount[]=$value['i_amount'];?></td>
  </tr>
</table></td>
   <td style="padding:0px; margin:0px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="4">
		 <tr>
        <?php
       
        $A[$value['itemid']][]=$value['reciveqty'];
        $B[$value['itemid']][]=$value['issueqty'];
              
        $AA[$value['itemid']][]=$value['r_amount'];
        $BB[$value['itemid']][]=$value['i_amount'];
        
       
         
        
        ?>
        
        <td width="28%" align="left" style="border-right:#c9c9c9 1px solid;"><?php echo  array_sum($A[$value['itemid']])-  array_sum($B[$value['itemid']]); ?></td>
   <td width="36%" align="left" style="border-right:#c9c9c9 1px solid;height:40px;"><?php echo 
     number_format((array_sum($AA[$value['itemid']])-  array_sum($BB[$value['itemid']]))/(array_sum($A[$value['itemid']])- array_sum($B[$value['itemid']])),2);?></td>
    <td align="left" style="text-align:right;"><?php echo array_sum($AA[$value['itemid']])-  array_sum($BB[$value['itemid']]); ?></td>
  </tr>
</table></td>
   
   
  
      
      
	 </tr>
    <?php $i++;} } else { echo "No Record "; }}  ?>
 </table>


</div>
</div>
 
 <script>
  function checkval()
  {
   if(document.getElementById("site").value=='0')
    {
     alert("Must Select Branch");
     return false;
    }
   return true;
  }
  </script>







