<?php 
ob_start();
$PageSecurity = 4;
$PageName = 'Purchase Requisition';
include('includes/DefinePOClass.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');

$sqlPoApp1=DB_query("SELECT map_flag FROM vendorItemMap_flag order by id desc",$db);
$PoApp1=DB_fetch_array($sqlPoApp1);
$mapFlag=$PoApp1['map_flag'];


$maxAssign="select max(no_of_assignee) as numMax from conf_purch_approval where company_id='".$_SESSION['ACGCompanyId']."' and doc_type='PR'";
$maxAssignExec=DB_query($maxAssign,$db);
$maxAssignDATA=DB_fetch_array($maxAssignExec);
 $maximAss=$maxAssignDATA['numMax'];


$arrayItem=explode(",",$_REQUEST['q']);
$newarrayItem=array_filter($arrayItem);
$newString=implode(",",$newarrayItem);
$detailsArray=explode(',', $newString);

foreach($detailsArray as $reqdetID)
        {
    $parentQry="select requsitionno from requisitionorderdetails where id='".$reqdetID."'";
       $parentQryExec=DB_query($parentQry,$db);
       $parentQryData=DB_fetch_array($parentQryExec);
       $parentID[]=$parentQryData['requsitionno'];
    
}

if($newString!='')
{
    if($_REQUEST['flag']=='false')
    {
        $parentIDd=array_unique($parentID);
        $u=0;
       foreach($parentIDd as $mainReqId)
        {
           
            $ItemQuery2="select a.app_quantity as qty , b.mst_itemid as ItemID FROM pr_head_approval a inner join requisitionorderdetails b on a.requiNo=b.id where a.requiNo in (".$newString.") and a.sr_no='".$maximAss."' and a.requisition_id='".$mainReqId."'";
             $ItemQueryExec=DB_query($ItemQuery2,$db);
              while($ItemQueryData=DB_fetch_array($ItemQueryExec))
          {
              $mapQtyarrays[]=$ItemQueryData['qty'];
              $itemidarrays[]=$ItemQueryData['ItemID'];
          }
           $mapQtystr=implode(',', $mapQtyarrays);
          $mapQtyarray[$u]=$mapQtystr;
           unset($mapQtyarrays);
          
          $itemIDstr=implode(',', $itemidarrays);
          $itemidarray[$u]=$itemIDstr;
           unset($itemidarrays);
          
         
          $u++;
        }
       if($mapQtyarray!=''){ $itemQty=$mapQtyarray;}
        if($itemidarray!=''){ $itemids=$itemidarray;}
        
    }
     else {
        $ItemQuery2="select SUM(a.app_quantity) as qty , b.mst_itemid as ItemID FROM pr_head_approval a inner join requisitionorderdetails b on a.requiNo=b.id where a.requiNo in (".$newString.") and a.sr_no='".$maximAss."' group by b.mst_itemid";
        $ItemQueryExec=DB_query($ItemQuery2,$db);
        while($ItemQueryData=DB_fetch_array($ItemQueryExec))
        {
            $mapQtyarray[]=$ItemQueryData['qty'];
            $itemidarray[]=$ItemQueryData['ItemID'];
        }
        if($mapQtyarray!=''){ $itemQty=implode(',', $mapQtyarray);}
        if($itemidarray!=''){ $itemids=implode(',', $itemidarray);}
    }


    
}

if($parentID!=''){ $parentIDS=array_unique($parentID); }
if($parentIDS!=''){ $parentStr=implode(',', $parentIDS);}

//echo $itemids;

?>

<form id="myformin" name="AsignVendor" action="" method="post" onsubmit="return validateForm()" >
<input type="hidden" name="requiDetIds" id="requiDetIds" value="<?php echo $newString; ?>" />
<?php if($_REQUEST['flag']=='false')
    { 
    foreach($itemQty as $mapqty) {
    ?>
<input type="hidden" name="mappedQty[]" id="mappedQty" value="<?php echo $mapqty; ?>" />
    <?php } ?>
<?php
 foreach($itemids as $itemidVl) {
?>
<input type="hidden" name="ItemID[]" id="ItemID" value="<?php echo $itemidVl; ?>" />
 <?php }

 foreach($parentIDS as $parentIDVal) {
?>
<input type="hidden" name="ReqiAId[]" id="ReqiAId" value="<?php echo $parentIDVal; ?>" />
 <?php }
 
 } else { ?>
<input type="hidden" name="mappedQty" id="mappedQty" value="<?php echo $itemQty; ?>" />
<input type="hidden" name="ItemID" id="ItemID" value="<?php echo $itemids; ?>" />
<input type="hidden" name="ReqiAId" id="ReqiAId" value="<?php echo $parentStr; ?>" />
    <?php } ?>
<input type="hidden" name="ReqiAIdnot" id="ReqiAIdnot" value="<?php echo $parentStr; ?>" />
<input type="hidden" name="flag" id="flag" value="<?php echo $_REQUEST['flag']; ?>" />
<input type="hidden" name="site" id="site" value="<?php echo $_REQUEST['branch']; ?>" />
<input type="hidden" name="postingDate" id="site" value="<?php echo $_REQUEST['postingDate']; ?>" />
<input type="hidden" name="openingDate" id="site" value="<?php echo $_REQUEST['openingDate']; ?>" />
</form>
    <table id="venderData" width="100%" border="0" style="border:1px solid #333333;" align="right" cellspacing="0" cellpadding="3">
        <tr style="height:30px;">
            <td align="center" colspan="3">
                <input type="text" id="searchvender">
                <input class="Search" onclick="search_vendor()" type="button" value="Search" id="Search">
            </td>
        </tr>
<tr style="height:30px;">
	<td style="border-bottom:1px solid #FF0000;"><strong>SrNo.</strong></td>
	<td style="border-bottom:1px solid #FF0000;"><strong>Vendor.</strong></td>
	<td style="border-bottom:1px solid #FF0000;"><strong>Select.</strong></td>
</tr>
<?php 

$SQLmappedVenderList=DB_query("SELECT * FROM indentpr_mapping WHERE indentPrno='".$_REQUEST['VendorId']."' and quotaion_status=0
  ",$db);
if(DB_num_rows($SQLmappedVenderList)>0)
{
while($GetMappedvendor=DB_fetch_array($SQLmappedVenderList))	
{
	$mappedVenArr[]=$GetMappedvendor['mapped_vendor'];
}


$FinalMappedVendorList=implode(",",$mappedVenArr);
$FinalMappedVendorList1=explode(",",$FinalMappedVendorList);
}
else
{
$FinalMappedVendorList1='';
}
$z=0;
if($mapFlag==1)
{
    if($itemidarray!='')
        {
        foreach($itemidarray as $itemIdss)
        {
           $item_id=implode(",",$itemidarray);

           
             $get_item_id=explode(",",$item_id);

             foreach($get_item_id as $val1)
             {

        $vendorQry="SELECT * FROM suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' and FIND_IN_SET( '".$val1."', assigned_items ) order by vendor_name limit 20";
       
         
        //echo $vendorQry;
        $sqlVendor=DB_query($vendorQry,$db);
         }  
        if(DB_num_rows($sqlVendor)>0){
        while($GetVendor=DB_fetch_array($sqlVendor)){
        if(!empty($FinalMappedVendorList1))
        if(in_array($GetVendor['id'],$FinalMappedVendorList1))
        {
                //$disabled='disabled';
        }
        else
        {
                $disabled='';
        }
        ?>
        <tr style="height:25px;">
                <td ><?php echo ($z+1).'.'; ?></td>
                <td><?php echo $GetVendor['vendor_name']; ?></td>
                <td ><input onclick="vendermap(this)" type="checkbox" name="" id="vendorId_<?php echo $z; ?>" <?php echo $disabled; ?> value="<?php echo $GetVendor['id']; ?>"  /></td>
        </tr>
        <?php $z++;} }  else { ?>
            <tr style="height:25px;">
            <td colspan="3" style="color:red;">
                No Record Found !!
            </td>
        </tr>
            <?php } } }  else { ?>
        <tr style="height:25px;">
            <td colspan="3" style="color:red;">
                No Items Selected !!
            </td>
        </tr>
            <?php } ?>
<?php }
else {
    if($itemidarray!='')
        {
            $vendorQry="SELECT * FROM suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' order by vendor_name limit 20";
        
       // echo $vendorQry;
        $sqlVendor=DB_query($vendorQry,$db);
        if(DB_num_rows($sqlVendor)>0){
        while($GetVendor=DB_fetch_array($sqlVendor)){
        if(!empty($FinalMappedVendorList1))
        if(in_array($GetVendor['id'],$FinalMappedVendorList1))
        {
                //$disabled='disabled';
        }
        else
        {
                $disabled='';
        }
        ?>
        <tr style="height:25px;">
                <td ><?php echo ($z+1).'.'; ?></td>
                <td><?php echo $GetVendor['vendor_name']; ?></td>
                <td ><input onclick="vendermap(this)" type="checkbox" name="" id="vendorId_<?php echo $z; ?>" <?php echo $disabled; ?> value="<?php echo $GetVendor['id']; ?>"  /></td>
        </tr>
        <?php $z++;} } else {?>
            <tr style="height:25px;">
                <td colspan="3" style="color:red;">
                    No Record Found !!
                </td>
            </tr>
            <?php } ?>   
<?php } else { ?>
    <tr style="height:25px;">
            <td colspan="3" style="color:red;">
                No Items Selected !!
            </td>
        </tr>
    <?php }  } ?>
</table>
