<?php
 
require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

class modWorkdayOperation {

public $pw_id;
public $pa_id;
public $pr_id;
public $fraction_list;
public $minute_list;
public $start_range;
public $duration_range;
public $startime;
public $pwoperations;
public $operations;
public $no_operation;
public $in_operation;
public $no_observe;
public $in_observe;
public $op_types;
public $form_defs;

  public function __construct($lo_id, $pw_id, $pp_id, $op_id, $pwo_id) {

    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->pw_id = isset($pw_id) ? $pw_id : 0;
    $this->pp_id = isset($pp_id) ? $pp_id : 0;
    $this->op_id = isset($op_id) ? $op_id : 0;
    $this->pwo_id = isset($pwo_id) ? $pwo_id : 0;
    
print_r ("pp:$this->pp_id pw:$this->pw_id op:$this->op_id pwo:$this->pwo_id");
    

    $this->selectors = 
    [["index"=>"10","refine"=>"10","filter"=>"10","label"=>"Project: Parcel","field"=>"sumOperationDefinitions","table"=>"sumDefinedOperations","sumid"=>$pp_id,"selid"=>$pp_id,"width"=>"50%","type"=>"READONLY","attr"=>"disabled"],
     ["index"=>"01","refine"=>"00","filter"=>"00","label"=>"Work Event","field"=>"fkyProjectWorkevent","table"=>"sumControlInstances","sumid"=>$pw_id,"selid"=>$pw_id,"width"=>"50%","type"=>"HEADER","attr"=>"workdayoperation"]];    

    $this->fraction_list  = "00,25,50,75";
    $this->minute_list    = "00,15,30,45";
    $this->start_range    = "00,24";
    $this->duration_range = "00,09";
    $startime      = "08:30";

    $dal = new DAL();
    
    $sql = "SELECT name FROM login WHERE id = $this->lo_id LIMIT 1 ";
    $this->login_name = $dal->query($sql)->name;
    

    $sql = "SELECT CI.instance AS field, CF.keytype, " .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, CF.action, " .
           "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
           "FROM controlinstance CI, controlform CF, " .
           "controlentry COL, controlentry LAB " . 
           "WHERE LAB.controlinstance_id = CI.id " .
           "AND LAB.property IN ('label', 'noLabel') " .
           "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
           "AND CF.controlinstance_id = CI.id AND CF.formhandler_id = " .
           "(SELECT FH.id FROM controlinstance FH " .
           "WHERE FH.instance = 'frmOperationDefinition') ";
    $this->odform_data = $dal->query($sql);  
   
    $sql = "SELECT CI.instance AS field, CF.keytype,  " .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, CF.action, " .
           "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
           "FROM controlinstance CI, controlform CF, " .
           "controlentry COL, controlentry LAB " . 
           "WHERE LAB.controlinstance_id = CI.id " .
           "AND LAB.property IN ('label', 'noLabel') " .
           "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
           "AND CF.controlinstance_id = CI.id AND CF.formhandler_id = " .
           "(SELECT FH.id FROM controlinstance FH " .
           "WHERE FH.instance = 'frmDailyOperation') ";
    $this->doform_data = $dal->query($sql); 

    $this->defop_hdr = ["0", "Operation Name", "Operation Type"];

    $this->dlyop_hdr = ["0", "Date", "Operation Name", "Type", "Number of Crew", "Duration"];

//    $sql = "SELECT OP.id, OP.name AS descr " .
//           "FROM projectworkdayoperation PWO, operation OP " .
//           "WHERE PWO.projectworkevent_id = $this->pw_id AND OP.id = PWO.operation_id " .
//           "ORDER BY OP.name " ;
//    $this->operation_names = $dal->query($sql);

//    $sql = "SELECT PWP.id, " .
//           "CASE WHEN " .
//              "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
//                "THEN CONCAT(surname,', ',firstname) " . 
//                "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
//           "END AS text " .
//           "FROM  (personnel PE, projectworkdaypersonnel PWP) " .
//           "LEFT JOIN projectworkdayoperationpersonnel POP " .
//           "ON (POP.projectworkdaypersonnel_id = PWP.id AND POP.projectworkdayoperation_id  = 0) " .
//           "WHERE PWP.projectworkevent_id = $this->pw_id AND PE.id = PWP.personnel_id AND POP.id IS NULL " .
//           "ORDER BY text ";
//    $this->no_operation = $dal->query($sql);

//    $sql = "SELECT PWP.id, " .
//           "CASE WHEN " .
//              "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
//                "THEN CONCAT(surname,', ',firstname) " . 
//                "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
//           "END AS text " .
//           "FROM personnel PE, projectworkdaypersonnel PWP, " .
//           "projectworkdayoperationpersonnel POP, projectworkdayoperation PWO " .
//           "WHERE PWP.projectworkevent_id = $this->pw_id AND POP.projectworkdayoperation_id = 0 " .
//           "AND PE.id = PWP.personnel_id " .
//           "ORDER BY text ";
//    $this->in_operation = $dal->query($sql);

//    $sql = "SELECT POB.id, " .
//           "CASE WHEN " .
//             "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
//               "THEN CONCAT(surname,', ',firstname) " . 
//               "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
//           "END AS text " .
//           "FROM (contact CO, projectworkdayobserver POB) " . 
//           "LEFT JOIN projectworkdayoperationobserver POO " .
//           "ON (POO.projectworkdayobserver_id = POB.id AND POO.projectworkdayoperation_id = 0) " .
//           "WHERE POB.projectworkevent_id = $this->pw_id AND CO.id = POB.contact_id " .
//           "AND POO.id IS NULL " .
//           "ORDER BY text ";
//    $this->no_observe = $dal->query($sql);

//    $sql = "SELECT POB.id, " .
//           "CASE WHEN " .
//             "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
//               "THEN CONCAT(surname,', ',firstname) " . 
//             "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
//           "END AS text " .
//           "FROM contact CO, projectworkdayobserver POB, projectworkdayoperationobserver POO " .
//           "WHERE POB.projectworkevent_id = $this->pw_id AND CO.id = POB.contact_id " .
//           "AND POO.projectworkdayobserver_id = POB.id AND POO.projectworkdayoperation_id = 0 " .
//           "ORDER BY text ";
//    $this->in_observe = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " . 
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM operationtype WHERE active = 1";
    $this->op_types = $dal->query($sql);
    
//    $sql = "SELECT id, linename, units FROM activitybudget WHERE project_id = $this->pr_id";
//    $this->budget_lines = $dal->query($sql);

    $blank = array ("id"=>0,     
          "projectparcel_id"=>0, "projectparcel_id_text"=>"",
          "name"=>"",            "name_text"=>"",    
          "operationtype_id"=>0, "operation_id_text"=>"");
    $this->odform_def0 = $this->toObject ($blank);

    $blank = array ("id"=>0,
          "operation_id"=>0,        "operation_id_text"=>"", 
          "projectworkevent_id"=>0, "projectworkevent_id_text"=>"", 
          "startime"=>"00:00",      "startime_text"=>"", 
          "breakdur"=>"0.00",       "breakdur_text"=>"",
          "stoptime"=>"00:00",      "stoptime_text"=>"",
          "duration"=>"0.00",       "duration_text"=>"");
    $this->doform_def0 = $this->toObject ($blank);
    
  }

  public function get($prop) {
    return $this->$prop;
  }

  private function toObject($array) {
    $object = new stdClass();
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $value = $this->toObject($value);
      }
      $object->$key = $value;
    }
    return $object;
  }

}

?>
