<?php
require_once (PATH_TABLES . "activity/JobServiceAttendeeChecklistTable.php") ;
require_once (PATH_TABLES . "activity/ChecklistHeaderTable.php") ;
require_once (PATH_TABLES . "activity/ChecklistDetailTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class JobServiceAttendeeChecklistClass extends MasterBase {
	private $fldchkid;
	private $fldhdrid;
	private $flddtlid;
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= JobServiceAttendeeChecklistTable::C_TABLE ;
		$this->fldid 	= JobServiceAttendeeChecklistTable::C_ID ;
		$this->fldchkid = JobServiceAttendeeChecklistTable::C_CHECKLIST_ID ;
		$this->fldhdrid = JobServiceAttendeeChecklistTable::C_HEADER_ID ;
		$this->flddtlid = JobServiceAttendeeChecklistTable::C_DETAIL_ID ;
	}

	function __destruct() {
	}
	
	function updateServiceChecklistRemark($serviceid,$checklistid,$remark){
		$sql = "UPDATE " . $this->tbl . 
                " SET " . JobServiceAttendeeChecklistTable::C_REMARK . " = '".$remark. "'".
                " WHERE " . $this->fldid . " = " . $serviceid. " AND " . $this->fldchkid . " = " . $checklistid. ""; 
        try{
            $this->db->executeSql($sql);
        } catch (Exception $e){
            Log::write("[ChecklistDetailClass]" . $e->getMessage());
        }
	}
	
	function updateServiceChecklist($serviceid,$checklistid,$check){
		$sql = "UPDATE " . $this->tbl . 
                " SET " . JobServiceAttendeeChecklistTable::C_CHECKED . " = ".$check. "".
                " WHERE " . $this->fldid . " = " . $serviceid. " AND " . $this->fldchkid . " = " . $checklistid. "";  
        try{
            $this->db->executeSql($sql);
        } catch (Exception $e){
            Log::write("[ChecklistDetailClass]" . $e->getMessage());
        }
	}
	
	function getRecordByChecklistId($checklistId){
        $sql = "SELECT * FROM ".$this->tbl.
				" WHERE ".$this->fldchkid." = ".$checklistId.
				" ORDER BY ".$this->flddtlid;
        
        $rows = $this->db->getTable($sql);

        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        
        return null;
    }
	
	function getJobServiceChecklist($serviceId){
        $sql = "SELECT C.*,H.".ChecklistHeaderTable::C_NAME.",D.".ChecklistDetailTable::C_NAME.
		" FROM " .$this->tbl." AS C ".
		" LEFT OUTER JOIN " .ChecklistHeaderTable::C_TABLE." AS H ".
		" ON H." .ChecklistHeaderTable::C_ID." = C.".$this->fldhdrid.
		" LEFT OUTER JOIN " .ChecklistDetailTable::C_TABLE." AS D ".
		" ON D." .ChecklistDetailTable::C_ID." = C.".$this->flddtlid.
		" WHERE C." . $this->fldid . " = " . $serviceId;
		
        $rows = $this->db->getTable($sql);

        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        
        return null;
    }
}
?>