<?php
ob_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
global $db; 

  if(isset($_REQUEST['Save']))
  {
     //Start Save Approve Data
    
  	        $i=0;
               while($i< count($_REQUEST['work_indent_id']))
               {$j=0;
                     $wk_ind_no=$_REQUEST['work_indent_id'][$i];
                     $parent_id=$s->GetFieldValue("tpl_work_indent_app","work_indent_id='".$wk_ind_no."' and conf_wk_in_id='".$_REQUEST['id']."'","id"); 
                     
                  while($j< count($_REQUEST['itemID'][$wk_ind_no]))
                  {
                       $wk_ind_asst_d=$_REQUEST['itemID'][$wk_ind_no][$j];
                    saveDataServiceItem($parent_id,$wk_ind_no,$wk_ind_asst_d);
                   
                   $result_set_App=DB_query("select COUNT(*) as assetCount from  tpl_work_indent_asset_details where work_indent_asset_id='".$wk_ind_asst_d."'",$db); 
                   $row_set_App=DB_fetch_array($result_set_App);
                   $result_get_Reg=DB_query("select COUNT(*) as rejectCount from tpl_work_indent_app_details where work_indent_asset_id='".$wk_ind_asst_d."' and status='R' ",$db);
                     $row_get_Reg=DB_fetch_array($result_get_Reg);
                     if($row_set_App['assetCount'] == $row_get_Reg['rejectCount'])
                     {
          DB_query("update tpl_work_indent_asset set asset_app_status='1' where id='".$wk_ind_asst_d."'",$db);
                     }


             $j++; }
 
                //Start Count Original Activity
               /* $resultAssetCount1=DB_query("select COUNT(*) as mainCount from tpl_work_indent_asset_details where work_indent_id='".$wk_ind_no."'",$db);
       $rowMainC1=DB_fetch_array($resultAssetCount1);

       $resultAppCount1=DB_query("select COUNT(*) as appCount from tpl_work_indent_app_details where work_indent_id='".$wk_ind_no."' and status='A'",$db);
       $rowAppC1=DB_fetch_array($resultAppCount1);
                //End Count Original Activity
                 $status='';
                if($rowMainC1['mainCount']==$rowAppC1['appCount'])
                  $status='Y';
                else
                $status='P';  */

               DB_query("update tpl_work_indent_app set app_date='".date('Y-m-d')."', status='Y' where work_indent_id='".$_REQUEST['work_indent_id'][$i]."' and conf_wk_in_id='".$_REQUEST['id']."'",$db);

                //Start Set Final Status of Work indent Number
                 $result_setFinal_status=DB_query("SELECT COUNT(*) AS AppCount FROM tpl_work_indent_app WHERE work_indent_id='".$wk_ind_no."' AND STATUS='Y'",$db);
                 $row_setFinal_status=DB_fetch_array($result_setFinal_status);
                 $finalCount=$row_setFinal_status['AppCount'];
                  if($totalCount==$finalCount)
                  {
                    DB_query("update tpl_work_indent set status='Y' where id='".$wk_ind_no."'",$db);
                  }

                 //End   Set Final Status of Work indent Number
              



          $i++;}// while($i< count($_REQUEST['work_indent_id']))
     


     //End Save Approve Data
                
          
   
     
prnMsg( _('Record Saved Successfully'),'success');

//echo "<script>location.href='index.php?route=work_indent_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";

  }// if(isset($_REQUEST['Save']))
 function saveDataServiceItem($parent_id,$wk_ind_no,$wk_ind_asst_d)
     {global $db;
      $queryService='';
     $queryService="insert into tpl_work_indent_app_details( parent_id,work_indent_id,work_indent_asset_id,work_asset_details_id,status) values ";
      $subQueryService='';$k=0;$activity_status='';
               while($k < count($_REQUEST['activity_defId'][$wk_ind_asst_d]) )
                     {
                      $activity_id1=$_REQUEST['activity_defId'][$wk_ind_asst_d][$k];
                       $rdname='rdApprove'.$activity_id1;
                       if($_REQUEST[$rdname]==1)
                        $activity_status='A';
                      else
                        $activity_status='R';

                 $subQueryService.=($k==0)?'(':',(';    
                 $subQueryService.="'".$parent_id."','".$wk_ind_no."','".$wk_ind_asst_d."','".$activity_id1."','".$activity_status."')";     
               $k++; }
         
         // echo $queryService.$subQueryService;
            if($subQueryService!='')
            DB_query($queryService.$subQueryService,$db); 
           

     }//function saveDataServiceItem()

?>
<style type="text/css">
.body_lightbox {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px; 
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}

</style>
<script type="text/javascript">
$.noConflict();
</script>
<?php
      
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<form method="post" action="">
<div class="body_contain">
<div class="tab_div"> 
<form method="post" action="">	
<table width="100%" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="13%"><strong>Branch</strong></td>
	<td width="20%"><select style="width:100%;" name="Site" id="Site" onchange="showIndent(this.value)">
    <option value="0">Select</option>
			<?php
			$ss1 = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
		
			$SiteName=DB_query($ss1,$db);
			while($SiteRow=DB_fetch_array($SiteName))
			{ ?>
			<option value="<?php echo $SiteRow['loccode']?>"<?php echo($_REQUEST['Site']==$SiteRow['loccode']?'selected':'');?>><?php echo $SiteRow['locationname']?></option>	
				
		<?php } ?>
		</select>
	</td>
	<!--<td class="tabledata1"><strong>Department</strong></td>
	<td><select style="width:150px;" name="deptId">
			<?php $DeptName=DB_query("SELECT * FROM deptmaster where deptname='PURCHASE' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			while($DeptIdRow=DB_fetch_array($DeptName))
			{ ?>
			<option value="<?php echo $DeptIdRow['id']?>"<?php echo($_REQUEST['deptId']==$DeptIdRow['id']?'selected':'');?>><?php echo $DeptIdRow['deptname']?></option>	
				
		<?php } ?>
		</select>
	</td>-->
	
	  <td width="13%"><strong>From Date</strong></td>
      <td width="20%"><input type="text" readonly name="fdate" id="fdate" style="width:100%;"  class="datepicker" value="<?php echo $_REQUEST['fdate']; ?>"/></td>
      <td width="13%"><strong>To Date</strong></td>
      <td><input type="text" name="todate" readonly id="todate" style="width:100%;" class="datepicker"  value="<?php echo $_REQUEST['todate'];  ?>"  /></td>
	
	    <!--<td><strong>Indent By</strong></td>-->
	<!--<td ><div id="UserIndentdep">
            <?php
        //$EMPSQL=DB_query("SELECT * FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND emp_site IN ('".$_SESSION['AssignedBranch']."')",$db);?>
	<select style="width:150px;" name="user" id="user">
	<option value="0">All</option>
	<?php //while($GetEmp=DB_fetch_array($EMPSQL)) {?>
	<option value="<?php echo $GetEmp['id']; ?>"<?php echo($_REQUEST['user']==$GetEmp['id']?'selected':''); ?>><?php //echo $GetEmp['emp_name']; ?></option>
	<?php //} ?>
	</select>
            </div>
            </td>--><input type="hidden" name="hidsr_no" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" />
</tr>
<tr class="row1">
	
  <td colspan="6" style="text-align:center;"><input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td>
</tr>
</table>
</form>

<?php 
     if($_REQUEST['Go'])
     {

          $tplSubQuery='';
          if($_REQUEST['Site']!='0')
          {
            $tplSubQuery.=" and site='".$_REQUEST['Site']."'";
          }  

           if($_REQUEST['fdate']!='' && $_REQUEST['todate'])
           {
             $tplSubQuery.=" and work_indent_date between '".FormatDateForSQL($_REQUEST['fdate'])."' and '".FormatDateForSQL($_REQUEST['todate'])."'";
           }
      /* if($_REQUEST['fdate']!='' && $_REQUEST['todate'])
        $tplSubQuery.=" and work_indent_date between '".date('Y-m-d',strtotime($_REQUEST['fdate']))."' and '".date('Y-m-d',strtotime($_REQUEST['todate']))."'";*/
     // echo "select * from tpl_work_indent where status!='Y' ". $tplSubQuery." ";
          $showFlag=false;
          $sql=DB_query("select * from tpl_work_indent where status!='Y' ". $tplSubQuery." ",$db);
      
              // echo "select * from tpl_work_indent where status!='Y' ". $tplSubQuery." "; 

    while($row=DB_fetch_array($sql))
    { 

       $result_Conf_Work=DB_query("select * from conf_work_indent_details where id='".$_REQUEST['id']."'",$db);
        if(DB_num_rows($result_Conf_Work)==1)
        { $rowCofig=DB_fetch_array($result_Conf_Work);
        	$conf_seq_no=$rowCofig['seq_no'];
         $result_App_Work=DB_query("select * from tpl_work_indent_app where conf_wk_in_id='".$_REQUEST['id']."' and work_indent_id='".$row['id']."'",$db); 
           { $rowApp_wk=DB_fetch_array($result_App_Work);
           	  $app_id=$rowApp_wk['id'];
           	  $app_seq_no=$rowApp_wk['seq_no'];
               if(DB_num_rows($result_App_Work)==0)
               {
              DB_query("insert into tpl_work_indent_app (work_indent_id,conf_wk_in_id,seq_no,status) values('".$row['id']."','".$_REQUEST['id']."','".$rowCofig['seq_no']."','N')",$db);
              $app_id=DB_Last_Insert_ID($db,'tpl_work_indent_app','id');
              $app_seq_no=$conf_seq_no;
              }
            }
        }


      $flag=false; $conf_seq_Again=0;         
        if($conf_seq_no==1)
        {
        	$flag=true;
        }
        else
        { 
        	$conf_seq_Again=$conf_seq_no-1;
        	$chkSeq_app=DB_query("select * from tpl_work_indent_app where work_indent_id='".$row['id']."' and seq_no='".$conf_seq_Again."' and status!='N'",$db);
        	if(DB_num_rows($chkSeq_app)==1)
        	{
        	$flag=true;	
        	}
        	else
        	$flag=false;	
            
        }

      if($flag)
       {
     
       $result_Approved=DB_query("select * from tpl_work_indent_app where work_indent_id='".$row['id']."' and seq_no='".$conf_seq_no."'",$db);
       $rowApproved=DB_fetch_array($result_Approved);
           if($rowApproved['status']!='Y')
            {

?>
<div id="divShowStructure">

<table border="1" width="100%" cellpadding="3" style="border:#808080 solid 1px; border-collapse:collapse" align="center">
<tr><td colspan="13">Work Indent No. : <?php echo $row['work_indent_no']; ?> <input type="hidden" name="work_indent_id[]" value="<?php echo $row['id']; ?>" /></td></tr>
<tr bgcolor="#ccc">
    <td width="1%" rowspan="2"></td>
    <td width="1%" rowspan="2">&nbsp;</td>
    <td width="6%" rowspan="2"><strong>Asset Code</strong></td>
    <td width="7%" rowspan="2"><strong>Asset Name</strong></td>
    <td width="15%" rowspan="2"><strong>Justification</strong></td>
   <?php if($_REQUEST['act']=='Edit')
      { ?>
    <td rowspan="2"></td>
    <?php  } ?>
    <td width="80" rowspan="2"><strong>Activity Code</strong></td>
    <td width="250" rowspan="2"><strong>Activity Name</strong></td>
    <td width="70" rowspan="2"><strong>Activity Unit</strong></td>
    <td width="70" rowspan="2"><strong>Activity Qty.</strong></td>
    <td width="90" rowspan="2"><strong>Work Completion Date </strong></td>  
    <td width="120" rowspan="2"><strong>Remark</strong></td>
    <td colspan="2" align="center" style="text-align:center;"><strong>Action</strong></td>
    </tr>
<tr bgcolor="#ccc">
    <?php if($_REQUEST['act']=='Edit')
      { ?>
    <?php  } ?>
    <td width="50"><strong>Approve</strong></td>
    <td><strong>Rejects</strong></td>
  </tr>
<tbody id="dataTable">
<?php  
      $preSeq=$conf_seq_no-1;

           $result_App_id=$s->GetFieldValue("tpl_work_indent_app","work_indent_id='".$row['id']."' and seq_no='".$preSeq."'","id");

      $resultIndent=DB_query("select * from tpl_work_indent_asset where work_indent_id='".$row['id']."'",$db);
      while($rowAsset=DB_fetch_array($resultIndent))
      {
       $resultAssetCount=DB_query("select COUNT(*) as mainCount from tpl_work_indent_asset_details where work_indent_id='".$rowAsset['work_indent_id']."' and work_indent_asset_id='".$rowAsset['id']."'",$db);
       $rowMainC=DB_fetch_array($resultAssetCount);
     
       $resultAppCount=DB_query("select COUNT(*) as appCount from tpl_work_indent_app_details where work_indent_id='".$rowAsset['work_indent_id']."' and work_indent_asset_id='".$rowAsset['id']."'  and status='R'",$db);
       $rowAppC=DB_fetch_array($resultAppCount);
         if($rowMainC['mainCount']!=$rowAppC['appCount'])
         {
       $resultItem=DB_query("select * from machine_center where id='".$rowAsset['asset_id']."'",$db);
      $rowItem=DB_fetch_array($resultItem);
        ?> 
<tr bgcolor="#FFFFFF">
<td valign="top"><input name="delitem[]" style="display:none;" type="checkbox"  id="<?php echo $rowAsset['asset_id']; ?>" /></td>
<td valign="top"><input type="hidden" name="itemID[<?php echo $row['id']; ?>][]" id="itemid" value="<?php echo $rowAsset['id']; ?>" /></td>
<td valign="top"><?php echo $rowItem['code']; ?></td>
<td valign="top"><?php echo $rowItem['name']; ?></td>
<td valign="top"><?php echo $rowAsset['junction_file']; ?></td>
 <?php
    if($_REQUEST['act']=='Edit')
      {
  ?>
<td valign="top"><input type="button" groupid="<?php echo $rowAsset['group_id']; ?>" value="Add Activity" onclick="showpopUpAssetGroup(this)" class="search"><input type="hidden" name="groupType[]" value="<?php echo $rowAsset['group_id']; ?>" /></td><?php } ?>
<td colspan="8" valign="top" style="padding:0px;">
<table class="tableAssetService" cellpadding="3" cellspacing="0" border="0" width="100%" style="border:none;">
<?php  $notIn_id='';
         if($conf_seq_no==1)
         {
          
        $notIn_id=0;
         }
         else
         {
           
           $resultUnApp=DB_query("select work_asset_details_id from tpl_work_indent_app_details where work_indent_id='".$row['id']."' and work_indent_asset_id='".$rowAsset['id']."' and status='R'",$db);
           while($rowUnApp=DB_fetch_array($resultUnApp))
           {
            $notIn_id.=$rowUnApp['work_asset_details_id'].",";
           }
            $notIn_id=substr($notIn_id,0,-1); 
         }
         $notIn_id=($notIn_id=='')?0:$notIn_id;
       
       
      $resultAssetDet=DB_query("select * from tpl_work_indent_asset_details where work_indent_id='".$row['id']."' and work_indent_asset_id='".$rowAsset['id']."' and id NOT IN (".$notIn_id.") ",$db);
      while($rowAssetDet=DB_fetch_array($resultAssetDet))
      {
        $showFlag=true;
        $resultActivity=DB_query("select * from sub_activity_defination where id='".$rowAssetDet['activity_id']."'",$db);
        $rowActivity=DB_fetch_array($resultActivity);
        
        ?> 
<tr> 
<td width="80" style="border-right:#808080 1px solid;"><?php echo $rowActivity['activity_code']; ?><input type="hidden" value="<?php echo $rowAssetDet['id']; ?>" name="activity_defId[<?php echo $rowAsset['id']; ?>][]" /></td>
<td width="250" style="border-right:#808080 1px solid;"><?php echo $rowActivity['activity_name']; ?></td>
<td width="70" style="border-right:#808080 1px solid;"><?php echo $s->getField('unitsofmeasure','unitid',$rowActivity['unit'],'unitname'); ?></td>
<td width="70" style="border-right:#808080 1px solid;"><?php echo $rowAssetDet['activity_qty'] ?></td>
<td width="90" style="border-right:#808080 1px solid;"><?php echo date('d/m/Y',strtotime($rowAssetDet['work_completion_date'])) ?></td>
<td valign="top" width="120" style="border-right:#808080 1px solid;"><?php echo $rowAssetDet['remark'];  ?></td>
<td width="50" style="border-right:#808080 1px solid;"><input type="radio"  value="1" name="rdApprove<?php echo $rowAssetDet['id']  ?>"></td>
<td><input name="rdApprove<?php echo $rowAssetDet['id']  ?>" value="0" type="radio"></td>
</tr>
 <?php } ?>

</table><!--tableAssetService-->  
</td>
</tr>
 <?php } }
    }
 }
}
}
 ?>
  <?php
    if(!$showFlag)
    {?>
  <tr><td style="color:red;" colspan="14">Record not found.</td></tr>
    <?php }
   ?>
</tbody>
</table>
</div>
<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
<input type="hidden" name="" value="0" id="edit_cnt" />
<input type="hidden" name="" value="0" id="rowcount1" />
    
<?php
    if($showFlag)
    {
   ?>  
<p align="center"><input type="submit" name="Save" value="Save" class='search' /></p>
<?php } ?>


</form>
</div>
</div>
</form>


