<?php 
ob_start();
error_reporting(0);
if(isset($_REQUEST['createIndent'])){
  //echo '<pre>'; //print_r($_REQUEST);
  foreach($_REQUEST['chkitem'] as $key1=>$value1)
  {
        foreach ($value1 as $key => $value) {
          $qty1=array_sum($value);
          $data[]=array('reqid'=>$value,'itemid'=>$key1,'qty'=>$qty1,'unitid'=>$key);
        }
   }
 
unset($_SESSION['data']);
$_SESSION['indentsite']=$_REQUEST['sitecode'];
 $_SESSION['data']=$data;
//exit;

    /* foreach($_REQUEST['chkitem'] as $key1=>$value1)
   {
      // foreach ($value1 as $key => $value) {
          $qty1=array_sum($value1);
      // }      
      
       $_SESSION['indentsite']=$_REQUEST['sitecode'];
       $data[]=array('reqid'=>$value1,'itemid'=>$key1,'qty'=>$qty1);
   } */
   // unset($_SESSION['data']);
 //$_SESSION['data']=$data;
 //print_r($_SESSION['data']);

 $s->pageLocation("index.php?route=POModule/create_process_indent_order".$rurl); 
   
  /*$data=array();
   foreach($_REQUEST['hidindentid'] as $key1=>$value1)
   {   
      // print_r($_REQUEST['chkitem'][$key1]);
       
   foreach($_REQUEST['chkitem'][$key1] as $key2)
   {
      // echo $key2;
  $itemid=$_REQUEST['hiditemid'][$key1][$key2];  
       
  $data[]=array(' reqid'=>$_REQUEST['hidindentid'][$key1],'itemid'=>$_REQUEST['hiditemid'][$key1][$key2],'qty'=>$_REQUEST['hidqtyid'][$itemid][$key2]);      
    }
  }
  // exit;
  //print_r($data);
  //exit;
 unset($_SESSION['data']);
 $_SESSION['data']=$data;
//exit;
 $s->pageLocation("index.php?route=POModule/create_process_indent_order".$rurl); */
 }
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Purchase Order Approval</h1>
<?php 
//echo "select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' and site='".$_REQUEST['sitecode']."'";
$vendor_sql=DB_query("select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' and site='".$_REQUEST['sitecode']."'",$db);
while($vendor_res=DB_fetch_assoc($vendor_sql))
{ ?>
<input type="hidden" class="allvendors" id="vendor_<?php echo $vendor_res['id'] ?>" value="<?php echo $vendor_res['assigned_items'] ?>" > 
<?php } ?>

<form name="frm" id="frm" action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
<tr class="row1">
    <td width="13%" align="right"><strong>Branch:<font color="red">*</font></strong></td>
    <td width="20%"><select name="sitecode" id="sitecode">
    <?php    
    if($_SESSION['AssignedBranch']!='')
    {     
    $sqlsite=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'  and loccode in ('".$_SESSION['AssignedBranch']."')",$db);
    }
    else
    {
    $sqlsite=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
    }  
    
    while($site_row=DB_fetch_array($sqlsite))
    {
        $p='';
        if($_REQUEST['sitecode']==$site_row['loccode'])
        $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"><strong>From Date:</strong></td>
  <td width="20%"><input type="text" readonly name="fdates" value="<?php if($_REQUEST['fdates']=='') { echo ConvertSQLDate($_SESSION['financialYrStart']);} else { echo $_REQUEST['fdates'];} ?>" class="datepicker"/></td>
  <td width="13%" align="right"><strong>To Date:</strong></td>
  <td><input type="text" readonly name="toDates" value="<?php if($_REQUEST['toDates']=='') { echo date("d/m/Y"); } else { echo $_REQUEST['toDates']; } ?>" class="datepicker"/></td>
</tr>
<tr class="row1">
  <td style="text-align:center;" colspan="6"><input type="submit" name="search" class="search" value="Search" onclick="return validation()"/></td>
</tr>
</table>
<?php
 if(isset($_REQUEST['search']))
  { 
        $fromdate=$_REQUEST['fdates'];
        $todate=$_REQUEST['toDates'];  
        $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];
          
        $Site=$_REQUEST['sitecode'];
        $filter=array();
        if($_REQUEST['toDates']!='' && $_REQUEST['fdates']!='' )
        {
        $filter[]="prdate between '".$from."' and '".$to."'";
        }
        if($_REQUEST['sitecode']!='0')
        {
        $filter[]="sitecode='".$Site."'";
        }
              if(!empty($filter))
        {
                  $filter1=implode(" and ",$filter);

        $sql = "SELECT A.requisitionno,A.prdate,A.Reqno,A.sitecode,A.requition_by FROM materialrequisitions A
               inner join requisitionorderdetails B on A.requisitionno=B.requsitionno  WHERE $filter1   AND A.CompanyId='".$_SESSION['ACGCompanyId']."' 
               AND (A.final_app_status='2' OR A.final_app_status='3') AND A.po_status='0'  and (A.mapped_vendor_status='0' || A.mapped_vendor_status=2)
                and A.MRN_Receive_status=0 and B.App_quantity!='0.00'  group by A.requisitionno ASC";
      $PurchOrdersResult = DB_query($sql,$db);
      }
    ?>
  <br/> 
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border:1px solid #808080;">
 <?php $j = 1;$k=0; $i = 0;
 if(DB_num_rows($PurchOrdersResult)>0)
{
  while ($row=DB_fetch_array($PurchOrdersResult)) 
  {
 //$sql = "SELECT A.indentPRno,B.mst_itemid FROM purchorders INNER JOIN purchorderdetails ON A.id=B.parantId WHERE indentPRno='.$row['requisitionno'].'";      
 ?>

<tr style="background-color:#e4e4e4;">  
  <td colspan="6"><input type="hidden" name="site1" id="site1" value="<?php echo $GetPendIndent['sitecode']; ?>">    
  <?php echo '<strong>Branch :</strong> <span class="inner">'.$s->GetFieldValue('locations',"loccode='".$row['sitecode']."'",'locationname').'</span>'; ?>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
  <?php echo '<strong>Indent By :</strong> <span class="inner">'.$s->GetFieldValue('empmaster',"id='".$row['requition_by']."'",'emp_name').'</span>'; ?>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
  <input type="hidden" name="hidindentid[]" id="hidindentid_<?php echo $j; ?>" value="<?php echo $row['requisitionno'];?>"><?php echo '<strong>Requisition No. :</strong> <span class="inner">'.$row['Reqno'].'</span>'; ?>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
  <?php echo '<strong>Date :</strong> <span class="inner">'.ConvertSQLDate($row['prdate']).'</span>'; ?></td>
</tr>

      <tr>       
        <th width="4%">S. No.</th>
        <th width="12%">Item Code</th>
        <th>Item Name</th>
        <th width="18%">Unit</th>
        <th width="10%">Approved Qty</th>
        <th width="5%">Action</th>
           <!--  <td class="tabledata"><strong>Process PO/View</strong></td>-->
      </tr>
      <?php       
      /*echo "SELECT mst_itemid,App_quantity,uom,qty_type FROM requisitionorderdetails WHERE requsitionno='".$row['requisitionno']."' 
        AND approval_status<>1 AND  mapped_vendor_status=0";*/
      /*$sqlDetails = DB_query("SELECT mst_itemid,App_quantity,uom,qty_type FROM requisitionorderdetails WHERE requsitionno='".$row['requisitionno']."' 
        AND approval_status<>1 AND po_status='0' and mapped_vendor_status=0",$db);*/
        $sqlDetails = DB_query("SELECT mst_itemid,App_quantity,uom,qty_type FROM requisitionorderdetails WHERE requsitionno='".$row['requisitionno']."' 
        AND approval_status<>1 AND  mapped_vendor_status=0",$db);
      while($rowDetails=DB_fetch_array($sqlDetails))
     { 
          if($rowDetails['App_quantity']!='0.00')
          {
          $sqlItem = DB_query("SELECT StockID,item_name,mst_unitid FROM item_master WHERE id='".$rowDetails['mst_itemid']."'",$db);
          $rowItem=DB_fetch_array($sqlItem);
          ?>
        <tr bgcolor="#FFFFFF">
          <td><?php echo $i+1;?></td>
          <td id="get_index"><input type="hidden" name="hiditemid[<?php echo $row['requisitionno'];?>][<?php echo $rowDetails['mst_itemid']; ?>]" id="hiditemid_<?php echo $j; ?>_<?php echo $i+1; ?>" value="<?php echo $rowDetails['mst_itemid'];?>"><?php echo $rowItem['StockID'];?></td>
          <td><?php echo $rowItem['item_name'];?></td>
          <td id="get_index1"><input type="hidden" name="unit[<?php echo $row['requisitionno'];?>][<?php echo $rowDetails['mst_itemid']; ?>]" id="hidqtytype_<?php echo $j; ?>_<?php echo $i+1; ?>" value="<?php echo $rowDetails['uom']?>">
            <?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$rowDetails['uom']."'",'unitname'); ?></td>
          <td style="text-align:right;"><input type="hidden" name="hidqtyid[<?php echo $row['requisitionno'];?>][<?php echo $rowDetails['mst_itemid']; ?>]" id="hidqtyid_<?php echo $j; ?>_<?php echo $i+1; ?>" value="<?php echo $rowDetails['App_quantity'];?>"><?php echo $rowDetails['App_quantity'];?></td>
          <td><input type="checkbox" name="chkitem[<?php echo $rowDetails['mst_itemid']; ?>][<?php echo $rowDetails['uom']?>][<?php echo $row['requisitionno'];?>]" id="chkitem_<?php echo $i+1; ?>" value='<?php echo $rowDetails['App_quantity'];?>' class="checkitem" /></td>
        </tr>
   <?php $i++;$k++;} }?>

<?php $j++;}?>
<tr><td colspan="6"><center><input type="submit" name="createIndent" value="Create Indent Purchase Order" class="search" onclick="return validate();"/></center></td></tr>
<?php }else{?>
<tr>
  <td colspan="6"><font color="#FF0000">No Record Found!</font></td>
</tr>
<?php }?>
<input type="hidden" id="count" value="<?php echo $k;?>">
<input type="hidden" id="countmain" value="<?php echo $j;?>">
</table>

<?php } ?>

</form>

<script>

function GetUnique(inputArray)
{
    var outputArray = []; 
    for (var i = 0; i < inputArray.length; i++)
    {
      if (($.inArray(inputArray[i], outputArray)) == -1)
      {
          outputArray.push(inputArray[i]);
      }
    }
    return outputArray;
}

$(document).ready(function(){  
 $('.checkitem').click(function(){
  var values = [];
  var $i=0; 
  var $checkbx=this.id;
    $('.checkitem').each(function()
    {
      if($(this).is(':checked'))
      { 
        values[$i]=$(this).parent().siblings('#get_index').children()[0].value;
        $i++;
      }
    });

   var  values1=GetUnique(values);
    var length1 = values1.length; 

var flag=0;
     $('.allvendors').each(function(){
        if(this.value!= '')
        {
          var cntr=0; 
          var $ven_items=this.value.split(','); 
          for(var $m=0;$m<$ven_items.length;$m++)
          {
           if($.inArray($ven_items[$m],values1 )!=-1) 
           {
              cntr++;
           }
          }
        if(cntr==length1)
          {
            flag=1;
            return false;
          }          
        }
       });
    if(flag==0)
           {
            alert("Checked Items not mapped with any vendor");
            //alert($checkbx)
            $('#'+$checkbx).removeAttr('checked');
           }



 });
})
  function validation()
    {
       var site=document.getElementById("sitecode").value; 
       if(site==0)
           {
               alert('Select Branch');
               document.getElementById("sitecode").focus();
               return false;
           }
       return true;
    }
 function enableChk(id,cnt){
     id1=id.split("_");
     i=id1[1];
    // var pendQty=document.getElementById("hidpend_qty_"+i).value;
       if(document.getElementById("chkid_"+i).checked==true)
       {
    //document.getElementById("sc_qty_"+i).value=pendQty;
    for(var p=1;p<=cnt;p++){
    document.getElementById("chkitem_"+i+"_"+p).removeAttribute('disabled','');
       }
       }
       else if(document.getElementById("chkid_"+i).checked==false)
       {
     for(var p=1;p<=cnt;p++){
     document.getElementById("chkitem_"+i+"_"+p).checked=false;
     document.getElementById("chkitem_"+i+"_"+p).setAttribute('disabled','true');
          }
       }
    }
    function validate(){ 
        flag=0;
       // var c1= document.getElementById("countmain").value;
        var c2= document.getElementById("count").value;
      
        //for(var p=1;p<c1;p++){
             for(var s=1;s<=c2;s++){
      // alert("Priyanka");
       if(document.getElementById("chkitem_"+s).checked==true)
       {
           flag=1;
           break;
       
          }
       // }
        }
        if(flag==0)
           {
               alert('Select Item');
               return false;
           }
       return true;

    }
    
//    function fillQty(id){
//     id1=id.split("_");
//     i=id1[1];
//     j=id1[2];
//     cnt=document.getElementById("count").value;
//    var arrayCheck=Array(); var arrayUncheck=Array();
//    // var pendQty=document.getElementById("hidpend_qty_"+i).value;
//    //for(var p=1;p<=cnt;p++){
//       if(document.getElementById("chkitem_"+i+"_"+j).checked==true)
//       {  
//           arrayCheck[i]=document.getElementById("chkitem_"+i+"_"+j).value;
//       }
//    //}
// alert(arrayCheck);
//    }
</script>
</div>
</div>