<?php
require_once (PATH_TABLES . "activity/JobServiceAttendeeTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class JobServiceAttendeeClass extends MasterBase {
	private $flddtlid;
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= JobServiceAttendeeTable::C_TABLE ;
		$this->fldid 	= JobServiceAttendeeTable::C_ID ;
		$this->flddtlid = JobServiceAttendeeTable::C_DETAIL_ID ;
	}

	function __destruct() {
	}
	
	function getRecordByDetailId($detalId){
        $sql = "SELECT * FROM " .$this->tbl ;
        $sql .= " WHERE " . JobServiceAttendeeTable::C_DETAIL_ID . " = " . $detalId;
        
        $rows = $this->db->getTable($sql);

        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        
        return null;
    }
	
	function getServiceTotalHoursByDetailId($detalId){
        $sql = "SELECT CASE WHEN " .
				" DATEDIFF(minute, CONVERT(varchar, ".JobServiceAttendeeTable::C_START_DATE.", 120), CONVERT(varchar, ".JobServiceAttendeeTable::C_END_DATE.",120)) > 0 THEN DATEDIFF(minute, CONVERT(varchar, ".JobServiceAttendeeTable::C_START_DATE.", 120), CONVERT(varchar, ".JobServiceAttendeeTable::C_END_DATE.",120)) ".
				" ELSE 0 END AS TOTAL_MINUTES " .
				" FROM " .$this->tbl .
				" WHERE " . JobServiceAttendeeTable::C_DETAIL_ID . " = " . $detalId;
        
        $rows = $this->db->getTable($sql);
		
		if(!is_null($rows) && count($rows) > 0){
			return $rows[0]['TOTAL_MINUTES'];
        }
        
        return 0;
    }

    function updateRecordByAssignId($assignId,$datas) {
		$sql = "update " . $this->tbl . " set " ;
		$fs = "" ;
		$params = array() ;
		if (is_array($datas) && count($datas) > 0) {
			foreach ($datas as $data) {
				$sql .= $fs . $this->db->fieldParam($data['field']) ;
				$fs = ", " ;
				$params[] = $this->db->valueParam($data['field'],$data['value']) ;
			}
			$sql .= " where " . $this->db->fieldParam($this->flddtlid) ;
			$params[] = $this->db->valueParam($this->flddtlid,$assignId) ;
			return $this->db->updateRow($sql,$params) ;
		} else  {
			return false ;
		}
	}
}
?>