<?php 
session_name('CAKEPHP'); session_start();
ob_start();
$PageSecurity = 11;
include('includes/session.inc');
include('includes/function_lib.php');
include('includes/SQL_CommonFunctions.inc');
//echo $country;
global $db;
global $s;
if($_REQUEST['n']!='')
{ 
    if($_REQUEST['type']=='J')
    { 
                $completejobcard_sql=DB_query("Select job_id from tpl_breakdown_mgmt_assig where  tpl_breakdown_mgmt_assig.CompanyId='".$_SESSION['ACGCompanyId']."' and  saft_appv=1 and dept_appv=1 and process_appv=1 and completion_status=1  order by id",$db);
        $completejobcard_arr=array();
         while($completejobcard_row=DB_fetch_array($completejobcard_sql))
        {
           $completejobcard_arr[]=$completejobcard_row['job_id'];
        }
        //echo "<pre>"; print_r($completejobcard_arr);exit;
         /* $usedjobcard_array=array();
          $sql_jobcard_used=DB_query("SELECT DISTINCT jobcard_id FROM material_request_slip WHERE mrs_type='J'",$db);
          while($row_used_jobcard=DB_fetch_array($sql_jobcard_used))
          {
            $usedjobcard_array[]=$row_used_jobcard['jobcard_id'];
          }
          //echo "<pre>"; print_r($usedjobcard_array);exit;
          $completeusedjobcard_array1=array_merge($completejobcard_arr,$usedjobcard_array);
          $completeusedjobcard_array=array_unique($completeusedjobcard_array1);*/
          $sql22="select BM.id,BM.tt_no from tpl_breakdown_mgmt AS BM Left Join tpl_breakdown_mgmt_assig AS MA ON BM.id=MA.job_id where MA.CompanyId='".$_SESSION['ACGCompanyId']."' and  MA.saft_appv=1 and MA.dept_appv=1 and MA.process_appv=1 and MA.completion_status=0";
          //if(!empty($completejobcard_arr))
         //{
           // $usedjobcard=implode(",",$completejobcard_arr);
          //   $sql22.=" and id NOT IN (".$usedjobcard.")";
         //}
            $sql22.=" and BM.site='".$_REQUEST['n']."' and BM.status='1'";
        $data='';
        $data.="<option value='0'>Select</option>";
        $sql_machine=DB_query($sql22,$db);
        while($row=DB_fetch_array($sql_machine))
        {
            $data.="<option value='".$row['id']."'>".$row['tt_no']."</option>";
        }
        echo $data;
    }else{
        $data='';
        $data.="<option value='0'>Select</option>";
        $sql_machine=DB_query("select * from spare_part_kits",$db);
        while($row=DB_fetch_array($sql_machine))
        {
            $data.="<option value='".$row['id']."'>".$s->getField('machine_center','id',$row['kitid'],'code')."--".$s->getField('machine_center','id',$row['kitid'],'name')."--".$s->getField('machine_center','id',$row['kitid'],'sr_no')."</option>";
        }
        echo $data;
    }
}else if($_REQUEST['jobcard']!=''){
    if($_REQUEST['type']=='J')
    {
        $row=DB_fetch_array(DB_query("select * from tpl_breakdown_mgmt where id='".$_REQUEST['jobcard']."'",$db));
        $data.=date("d/m/Y",strtotime($row['asset_rec_date_time']));

        $data1='';
        $data1.="<table cellpadding='0' border='0' width='100%'>";
        $data1.="<tr>";
        $data1.="<th width='4%'><strong>S. No.</strong></th>";
        $data1.="<th width='24%'><strong>Spare Code</strong></th>";
        $data1.="<th width='24%'><strong>Spare Name</strong></th>";
        $data1.="<th width='24%'><strong>Unit</strong></th>";
        $data1.="<th width='24%'><strong>QTY.</strong></th>";
        $data1.="</tr>";

        $sql_breakdown=DB_query("select asset_id from tpl_breakdown_mgmt_machine where  master_id='".$_REQUEST['jobcard']."'",$db);
        $i=1;
        while($row_break=DB_fetch_array($sql_breakdown))
        {
            $data1.="<tr>";
            $data1.="<th width='4%'><strong><input type='hidden' name='asset[]' value='".$row_break['asset_id']."' /> ".$i."</strong></th>";
            $data1.="<th width='24%'><strong>".$s->getField('machine_center','id',$row_break['asset_id'],'code')."</strong></th>";
            $data1.="<th width='24%'><strong>".$s->getField('machine_center','id',$row_break['asset_id'],'name')."</strong></th>";
            $data1.="<th width='24%'><strong></strong></th>";
            $data1.="<th width='24%'><strong></strong></th>";
            $data1.="</tr>";
            
            $kitid=$s->getField('spare_part_kits','kitid',$row_break['asset_id'],'id');

            $sql_breakdown1=DB_query("select * from spare_part_kits_details where parant_id='".$kitid."'",$db);
            $sql_row=DB_num_rows($sql_breakdown1);
            if($sql_row>0)
            {  
                $data1.="<tr>";
                $data1.="<td colspan='5'>";
                $data1.="<table cellpadding='0' border='0' width='100%'>";
                $m=1;
                while($row_break1=DB_fetch_array($sql_breakdown1))
                {
                    $data1.="<tr>";
                    $data1.="<td width='4%'><input type='hidden' name='spare[".$row_break['asset_id']."][]' value='".$row_break1['id']."' /><input type='hidden' name='item[".$row_break['asset_id']."][]' value='".$row_break1['item_id']."' />".$m."</td>";
                    $data1.="<td width='24%'>".$s->getField('item_master','id',$row_break1['item_id'],'StockID')."</td>";
                    $data1.="<td width='24%'>".$s->getField('item_master','id',$row_break1['item_id'],'item_name')."</td>";
                    $data1.="<td width='24%'><input type='hidden' name='unit[".$row_break['asset_id']."][]' value='".$s->getField('item_master','id',$row_break1['item_id'],'mst_unitid')."' />".$s->getField('unitsofmeasure','unitid',$s->getField('item_master','id',$row_break1['item_id'],'mst_unitid'),'unitname')."</td>";
                    $data1.="<td width='24%'><input type='text' name='qty[".$row_break['asset_id']."][]' value='' /></td>";
                    $data1.="</tr>";
                    $m++;
                }
                $data1.="</table>";
                $data1.="</td>";
                $data1.="</tr>";
            }
          $i++;    
        }
        $data1.="</table>";

        echo $data."__".$data1;
    }else{
        $sql_breakdown11=DB_query("select kitid from spare_part_kits where id='".$_REQUEST['jobcard']."'",$db);
        $row_break11=DB_fetch_array($sql_breakdown11);

        $sql_breakdown22=DB_query("select end_meter_reading, reading_unit from tpl_fixed_asset_logbook_details where asset_code='".$row_break11['kitid']."' order by id desc limit 0,1",$db);
        $row_break22=DB_fetch_array($sql_breakdown22);

        $data='';
        $data.=$row_break22['end_meter_reading'];

        $datas='';
        $datas.=$row_break22['reading_unit'];

        $data1='';
        $data1.="<table cellpadding='0' border='0' width='100%'>";
        $data1.="<tr>";
        $data1.="<th width='4%'><strong>S. No.</strong></th>";
        $data1.="<th width='24%'><strong>Spare Code</strong></th>";
        $data1.="<th width='24%'><strong>Spare Name</strong></th>";
        $data1.="<th width='24%'><strong>Unit</strong></th>";
        $data1.="<th width='24%'><strong>QTY.</strong></th>";
        $data1.="</tr>";

        $sql_breakdown=DB_query("select kitid from spare_part_kits where id='".$_REQUEST['jobcard']."'",$db);
        $i=1;
        while($row_break=DB_fetch_array($sql_breakdown))
        {
            $row_break['asset_id']=$row_break['kitid'];
            $data1.="<tr>";
            $data1.="<th width='4%'><strong><input type='hidden' name='asset[]' value='".$row_break['asset_id']."' /> ".$i."</strong></th>";
            $data1.="<th width='24%'><strong>".$s->getField('machine_center','id',$row_break['asset_id'],'code')."</strong></th>";
            $data1.="<th width='24%'><strong>".$s->getField('machine_center','id',$row_break['asset_id'],'name')."</strong></th>";
            $data1.="<th width='24%'><strong></strong></th>";
            $data1.="<th width='24%'><strong></strong></th>";
            $data1.="</tr>";
            
            $kitid=$s->getField('spare_part_kits','kitid',$row_break['asset_id'],'id');

            $sql_breakdown1=DB_query("select * from spare_part_kits_details where parant_id='".$kitid."'",$db);
            $sql_row=DB_num_rows($sql_breakdown1);
            if($sql_row>0)
            {  
                $data1.="<tr>";
                $data1.="<td colspan='5'>";
                $data1.="<table cellpadding='0' border='0' width='100%'>";
                $m=1;
                while($row_break1=DB_fetch_array($sql_breakdown1))
                {
                    $data1.="<tr>";
                    $data1.="<td width='4%'><input type='hidden' name='spare[".$row_break['asset_id']."][]' value='".$row_break1['id']."' /><input type='hidden' name='item[".$row_break['asset_id']."][]' value='".$row_break1['item_id']."' />".$m."</td>";
                    $data1.="<td width='24%'>".$s->getField('item_master','id',$row_break1['item_id'],'StockID')."</td>";
                    $data1.="<td width='24%'>".$s->getField('item_master','id',$row_break1['item_id'],'item_name')."</td>";
                    $data1.="<td width='24%'><input type='hidden' name='unit[".$row_break['asset_id']."][]' value='".$s->getField('item_master','id',$row_break1['item_id'],'mst_unitid')."' />".$s->getField('unitsofmeasure','unitid',$s->getField('item_master','id',$row_break1['item_id'],'mst_unitid'),'unitname')."</td>";
                    $data1.="<td width='24%'><input type='text' name='qty[".$row_break['asset_id']."][]' value='' /></td>";
                    $data1.="</tr>";
                    $m++;
                }
                $data1.="</table>";
                $data1.="</td>";
                $data1.="</tr>";
            }
          $i++;    
        }
        $data1.="</table>";
        echo $data."__".$datas."__".$data1;
    }
}
?>

 
   