<?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 = _('Transfer Issue Register');

 
//modid=6&frmid=422

$form_description=$s->getField('modules_form','id',$_REQUEST['frmid'],'description');
$_SESSION['module_name']=$form_description;

$first_date=date('01/m/Y');
$cur_date=date('d/m/Y');
//echo $form_description;

//For hmis User
$sql_user_store="select hmis_storeid,StoreCode from www_users left join storetype on storetype.id=www_users.hmis_storeid where www_users.id='".$_SESSION['UserID']."'";
$result_user_store=DB_query($sql_user_store,$db);
$rs_user_store=DB_fetch_array($result_user_store);
$main_store=$rs_user_store[0];


$sql_other_store="select store_id,StoreCode from www_users_dtls left join storetype on storetype.id=www_users_dtls.store_id where user_id='".$_SESSION['UserID']."'";
$result_other_store=DB_query($sql_other_store,$db);
while($rs_other_store=DB_fetch_array($result_other_store))
{
  $store[]=$rs_other_store['store_id'];
}
if(!empty($store))
{
  $get_store_id=implode(",",$store);
}
?>

<script language="javascript" type="text/javascript">

function get_itemsubgroup(str)
{
  if(str=="")
  {
  document.getElementById("sub_id").innerHTML="";
  return;
  }
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)
    {
    document.getElementById("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
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)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
function showItems(str)
{
    if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
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)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?partName="+str,true);
xmlhttp.send();
}



function showsitename160(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];
    }
  }
  xmlhttp.open("GET","get_sitenamedata2.php?site="+str,true);
  xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

  
}
function validate()
{
  if($("#site").val()=="")
  {
    alert("Select Branch");
    $("#site").get(0).focus();
    return false;
  }
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<form method="post" action="">
<h1><?php echo $form_description;?></h1>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="13%" align="right">Branch</td>
            <td width="20%">
      <?php
  
    $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
       $site_sql1=DB_query($ss1,$db);
   
  ?>
      <select name="site" id="site" onchange="showsitename160(this.value)" >
                <option value="">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 width="13%" align="right">Store</td>
      <td width="20%"><div id="storedata7">
                <?php 
                 if($_SESSION['UserType']!='SuperAdmin')
                 {
                     if($get_store_id!='')
                     {
                           $store_sql = "SELECT id, StoreName,storecode FROM storetype where id in(".$main_store.",".$get_store_id.")";
                     }
                     else if($main_store!='')
                     {
                        $store_sql = "SELECT id, StoreName,storecode FROM storetype where id in(".$main_store.")";
                     }
                 }      
                 else
                 {
                  $store_sql = "SELECT id, StoreName,storecode FROM storetype";
                 }    
     $store_result = DB_query($store_sql,$db);
    
    
    ?>
              <select name="store1" >
                  <option value="0">Select</option>
                  <?php
    while($store_row = DB_fetch_array($store_result))
      {
      
    ?>
                  <option value="<?php echo $store_row['id'];?>"<?php echo($_REQUEST['store1']==$store_row['id']?'selected':'')?>><?php echo $store_row['StoreName'];?></option>
                  <?php }?>
              </select>
            </div></td>
<td width="13%" align="right">Item</td>
            <td><div id="storedata7">
                <?php 
     $item_sql = "SELECT id, item_name FROM item_master ORDER BY item_name";
     $item_result = DB_query($item_sql,$db);
    
    
    ?>
                <select name="itemname1" >
                  <option value="0">Select</option>
                  <?php
    while($item_row = DB_fetch_array($item_result))
      {
      
    ?>
                  <option value="<?php echo $item_row['id'];?>"<?php echo($_REQUEST['itemname1']==$item_row['id']?'selected':'')?>><?php echo $item_row['item_name'];?></option>
                  <?php }?>
                </select>
              </div></td>
        </tr>
          <tr class="row1">
            <td align="right">From Date</td>
            <td><input type="text" name="from_date" id="fromdate" class="datepicker" value="<?php if($_REQUEST['from_date']!='') { echo $_REQUEST['from_date']; } else { echo $first_date; }?>"  onchange="javascript:checkdate(this.value)"  ></td>
            <td align="right">To Date</td>
            <td><input type="text"  name="to_date" id="todate" value="<?php if($_REQUEST['to_date']!='') { echo $_REQUEST['to_date']; } else { echo $cur_date; } ?>"  onchange="javascript:checkdate(this.value)" class="datepicker">              
              <input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'>
              <input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'>
            </td>
            <td align="right">Issue Slip No.</td>
            <td><input type="text" name="issue_slipno"  value="<?php echo $_POST['issue_slipno'];?>"></td>
          </tr>
          <tr class="row1">
            <td style="text-align:center;" colspan="6"><input type="submit" class="search" name="view" value="View" onclick="return validate()"><!--<input type="submit" name="print" value="Print">  <input type="submit" name="cancel" value="Cancel">--></td>
          </tr>
        </table>
</form>
<?php  
if($_REQUEST['view']!='')
{
  $fromdate=$_REQUEST['from_date'];
  $todate=$_REQUEST['to_date'];
  
  $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');
  $itemname=$s->getField('item_master','id',$_REQUEST['itemname1'],'item_name');
  $filter=array();
  if($_REQUEST['site']!='')
  $filter[]=" (transfer_issue.sitecode = '".$_REQUEST['site']."' OR transfer_issue.sitecode_to = '".$_REQUEST['site']."') ";
  if($_REQUEST['store1']!='0')
  $filter[]=" transfer_issue.tostore = '".$_REQUEST['store1']."'";
  if($_REQUEST['issue_slipno']!='')
  $filter[]=" transfer_issue.issue_slipno like '%".$_REQUEST['issue_slipno']."%'";
  if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
  $filter[]="transfer_issue.issuedate BETWEEN '$from' and '$to'";
  if($_REQUEST['itemname1']!='0')
  $filter[]="id IN (SELECT transfer_issue_id FROM transfer_issue_details WHERE transfer_issue_details.itemid= '".$_REQUEST['itemname1']."')";
  if($filter!='')
  {
    $filter=implode(' and ',$filter);
  }
  if($_REQUEST['site']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['itemname1']!='0')
  {
    $message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u> From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></i> for <u><span class='fntBig'>".$itemname."</span></u>";
  }
  if($_REQUEST['site']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
  {
    $message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u> From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u>";
  }
  else if($_REQUEST['site']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='' && $_REQUEST['itemname1']!='0')
  {
    $message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u> for <u><span class='fntBig'>".$itemname."</span></u>";
  
  }
  else if($_REQUEST['site']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
  {
    $message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u>";
  
  }
  else if($_REQUEST['site']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['itemname1']!='0')
  {
    $message="Showing Records From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u> for <u><span class='fntBig'>".$itemname."</span></u>";
  
  }
  else if($_REQUEST['site']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
  {
    $message="Showing Records From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u>";
  
  }
  if($filter!='')
  {
  
   $sql ="Select * from transfer_issue where $filter  ";
   }
   
   
   $_SESSION['issuereport']=$sql;
  $_SESSION['mess']=$message;


  $branch=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');

  $_SESSION['branch']="Branch:".$branch;
  $_SESSION['period']="From"."&nbsp".$_REQUEST['from_date']."&nbsp"."to"."&nbsp".$_REQUEST['to_date'];

?>
<p align="center"><strong><?php echo $message; ?></strong> <a href="TransferIssueRegisterPrint.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&itmname=<?php echo $_REQUEST['itemname1']?>" target="_blank" class="search" style="float:right;">Print</a></p>
  <table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
<tr>
      <th width="3%">S. No.</th>
      <th width="5%">STN Date</th>
      <th width="9%">STN No.</th>
      <!-- <th width="12%">From Branch</th> -->
      <th width="12%">To Store</th>
      <th width="120">Item Code</th>
      <th width="360">Item Name</th>
      <th width="100">Batch No.</th>
      <th>Transfer Qty.</th>
      <!-- <td width="97" class="jjrs_tablehead"  style="width:77px;"><strong>Total Price </strong></td>
   <td width="102" class="jjrs_tablehead"  style="width:77px;"><strong>Amount</strong></td>
   -->
    </tr>
    <?php 
 // $sql = "Select * from addissue";
 
  $result=DB_query($sql,$db);
  $sno = 0;
  while($myrow=DB_fetch_array($result))
  {
    $sno += 1;
  ?>
    <tr>
      <td><?php echo $sno;?></td>
      <td><?php echo ConvertSQLDate($myrow['issuedate']);?></td>
      <td><?php echo $myrow['issue_slipno'];?></td>
   <!--   <td><?php echo  $s->GetFieldValue('locations',"loccode='".$myrow['sitecode']."'",'locationname'); ?></td> -->
      <td><?php echo $s->GetFieldValue('storetype',"id='".$myrow['sitecode_to']."'",'StoreName') ; ?></td>
      <td colspan="4" style="padding:0px;" valign="top">
      <div id="amc-tab">
            <table width="100%" border="0" cellpadding="3" cellspacing="0">
        <?php 
        $details_sql="Select * from transfer_issue_details where transfer_issue_id='".$myrow['id']."'";
    if($_REQUEST['itmname']!=0){
     $details_sql .= " AND itemid='".$_REQUEST['itmname']."'";
    }
  //$_SESSION['details_sql']=$details_sql;
  $details_res=DB_query($details_sql,$db);
  $issueqty[]='';
  $req_qty[]='';
  $numrow1=DB_num_rows($details_res);
  if(DB_num_rows($details_res)>0){
  while($details_row=DB_fetch_array($details_res)){ 

    ?>
        <tr>
        <td width="120"><?php echo $s->getField('item_master','id',$details_row['itemid'],'StockID'); ?></td>
        <td width="360"><?php echo $s->getField('item_master','id',$details_row['itemid'],'item_name'); ?></td>
        <td width="100"><?php 
       //echo  $s->GetFieldValue('size_master',"id='".$details_row['size_id']."'",'size');
          echo $details_row['batch_no'];
       ?></td>
        <td width="70" style="text-align:right"><?php echo number_format((float)$details_row['issueqty'],2); 
                        $issueqty[]=$details_row['issueqty']; ?></td>
        <td width="50"><?php 
                  //echo  $s->GetFieldValue('product_grade',"id='".$details_row['uom']."'",'grade');
                   echo $s->GetFieldValue('unitsofmeasure',"unitid='".$details_row['uom']."'",'unitname') ; ?></td>
        <td width="70" style="text-align:right"><?php echo number_format((float)$details_row['issueqty1'],2);
                         $stock_qty2[]=$details_row['issueqty1'];
                 ?></td>           
        <td><?php 
      // $unitname1 = $s->GetFieldValue('item_master',"id='".$details_row['itemid']."'",'mst_unitid');
      //echo $unitname = $s->GetFieldValue('unitsofmeasure',"unitid='".$unitname1."'",'unitname') ;
          echo $s->GetFieldValue('unitsofmeasure',"unitid='".$details_row['uom1']."'",'unitname') ; ?></td>            
        </tr>
        <?php } ?>
        <?php } ?>
      </table>
      </div>
      </td>
    </tr>
    <?php } if($numrow1>0) { ?>
    <tr>
    <!--   <td colspan="7">&nbsp;</td>     
      <td><strong>Total</strong></td>
      <td><?php echo number_format((float)array_sum($issueqty),2); ?></td>
      <td><?php echo number_format((float)array_sum($stock_qty2),2);
    ?></td>
 -->    </tr>
    <?php } else { ?>
    <tr>
      <td colspan="9" align="center"><strong>No Records</strong></td>
    </tr>
    <?php } ?>
  </table>
<?php } ?>
</div>

</div>

<script type="text/javascript">
  function checkdate(date)
{
  var from =document.getElementById('from').value;
  var to =document.getElementById('to').value;
  
    var cdate=date.split("/");
    var sdate=from.split("/");
    var edate=to.split("/");

    cdate[1]=cdate[1]-1;
    sdate[1]=sdate[1]-1;
    edate[1]=edate[1]-1;
       
    var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
    var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
      var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

      if (dateS > dateE) {
          alert("Start Date is not greather then Date End.");
          document.getElementById('fromdate').value=from;
          document.getElementById('todate').value=to;
          
      }

      if (dateC > dateE) {
          alert("Date Not greather than "+to);
         document.getElementById('fromdate').value=from;
          document.getElementById('todate').value=to;
         
      }

      if (dateC < dateS) {
          alert(" Date Not less than "+from);
          document.getElementById('fromdate').value=from;
          document.getElementById('todate').value=to;
         
      } 
  
  
}

</script>