<?php
$find_date= $_POST["StartDate"];
$find_enddate= $_POST["EndDate"];
$week_no= $_POST["Weeknumber"];
$section= $_POST["Section"];
if ($find_date =="" || $find_enddate =="" ){
   $find_date =date("Y.m.d");
   $find_enddate =date("Y.m.d");
}
$today = date("d/m/Y H:i:s");
$host ="it2008";
$user = "sa";
$pass = "mercury";
$objConnect = mssql_connect($host,$user,$pass);
$objDB = mssql_select_db("smvDB");
$strSQL="select section_no,status_desc,SUM(cum_qty)as cum_qty,SUM(cum_value)cum_value,SUM(les_br_qty)as les_br_qty,SUM(les_br_value)as les_br_value,SUM(normal_qty)as normal_qty,SUM(normal_value)as normal_value,SUM(dir_qty)as dir_qty,SUM(dir_value)as dir_value,sum(goal_point)as goal_point from pd_summary_output 
    where convert(char(10),as_date,102)  = CONVERT(VARCHAR(10), '$find_date',102) ";
switch ($section) {
    case "pd1":
         $strSQL .= "and status_desc in ('Mold Cut','Wax injection','Wax','Wax Setting','Wax Tree') ";
        break;
     case "casting":
         $strSQL .= "and status_desc in ('Wax','Wax Setting','Wax Tree','PD1','casting') ";
        break;
    case "pd2":
        $strSQL .= "and status_desc in ('PD1','Filling','MP','Soldering','Setting','SMV1') ";
        break;
    case "pd3":
        $strSQL .= "and status_desc in ('QC bf Plating','PD3','Plating','QC Plating','Glue Assembly','2Tone','FG Receive') ";
        break;
    case "hp":
        $strSQL .= "and status_desc in ('Hand Polishing','Repair','QC bf Plating','Plating','PD3') ";
        break;
    case "resin":
        $strSQL .= "and status_desc in ('PD2','QC bf Plating','Resin','FG Receive','Export','Qcp1 Soldering','MP') ";
        break;
     case "meeting":
        $strSQL .= "and status_desc in ('Mold Cut','Wax injection','Wax','Wax Setting','Wax Tree') ";
        break;
    default:
       $strSQL="";
}
$strSQL.="group by section_no,status_desc,goal_point order by section_no";
if($strSQL != ""){
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
 $resultArray = array();
while($objResult = mssql_fetch_array($objQuery))
{
   array_push($resultArray,$objResult);
}
mssql_close($objConnect);
$return_json ="";
$totalVal=0;
$totalPD2Val=0;
$totalPD2cum=0;
$totalPD2les=0;
$totalPD2nor=0;
$totalPD2dir=0;
$totalQty=0;
$goalStat="";
foreach ($resultArray as $result)
{
    $totalQty=$result["cum_qty"]+$result["les_br_qty"]+$result["normal_qty"]+$result["dir_qty"];
    $totalVal=($result["cum_value"]+$result["les_br_value"]+$result["normal_value"]+$result["dir_value"])/320;
       if($result["status_desc"] == "PD2" ){
           $totalPD2cum=$result["cum_value"];
           $totalPD2les=$result["les_br_value"];
            $totalPD2nor=$result["normal_value"];
            $totalPD2dir=$result["dir_value"];
            $totalPD2Val =$totalVal;
    }else if($result["status_desc"] == "Resin" ){
        $totalPD2cum +=$result["cum_value"];
           $totalPD2les +=$result["les_br_value"];
            $totalPD2nor +=$result["normal_value"];
            $totalPD2dir +=$result["dir_value"];
        $totalPD2Val  +=$totalVal;
          if($totalVal >$result["goal_point"]){
        $goalStat ="<img src='images/finish.png' style='height:24px; width:24px;'/> &nbsp; ";
    }else{
        $goalStat ="<img src='images/monitor.png' style='height:24px; width:24px;'/> &nbsp; ";
    }
$return_json .="    <tr>
      <td class=\"result_section\">$goalStat<strong>". $result["status_desc"]. "</strong></td>
      <td><strong>" .number_format($result["cum_value"]/320)."</strong></td>
      <td><strong>" .number_format($result["les_br_value"]/320)."</strong></td>  
      <td><strong>" .number_format($result["normal_value"]/320)."</strong></td>
      <td><strong>" .number_format($result["dir_value"]/320)."</strong></td>  
      <td><strong>" .number_format($totalVal)."</strong></td>      
      </tr>"; 
         if($totalPD2Val >$result["goal_point"]){
        $goalStat ="<img src='images/finish.png' style='height:24px; width:24px;'/> &nbsp; ";
    }else{
        $goalStat ="<img src='images/monitor.png' style='height:24px; width:24px;'/> &nbsp; ";
    }
$return_json .="    <tr>
      <td class=\"result_section\">$goalStat<strong>PD2</strong></td>
      <td><strong>" .number_format($totalPD2cum/320)."</strong></td>
      <td><strong>" .number_format($totalPD2les/320)."</strong></td>  
      <td><strong>" .number_format($totalPD2nor/320)."</strong></td>
      <td><strong>" .number_format($totalPD2dir/320)."</strong></td>  
      <td><strong>" .number_format($totalPD2Val)."</strong></td>      
      </tr>"; 
    }
    else {
    if($totalVal >$result["goal_point"]){
        $goalStat ="<img src='images/finish.png' style='height:24px; width:24px;'/> &nbsp; ";
    }else{
        $goalStat ="<img src='images/monitor.png' style='height:24px; width:24px;'/> &nbsp; ";
    }
$return_json .="    <tr>
      <td class=\"result_section\">$goalStat<strong>". $result["status_desc"]. "</strong></td>
      <td><strong>" .number_format($result["cum_value"]/320)."</strong></td>
      <td><strong>" .number_format($result["les_br_value"]/320)."</strong></td>  
      <td><strong>" .number_format($result["normal_value"]/320)."</strong></td>
      <td><strong>" .number_format($result["dir_value"]/320)."</strong></td>  
      <td><strong>" .number_format($totalVal)."</strong></td>      
      </tr>"; 
//<td><strong>" .number_format($result["wip_am"])."</strong></td>
//    <td><strong>" .number_format($result["wip_pm"])."</strong></td>
}
}
}
echo $return_json;
?>