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

class JobAssignmentAttachmentClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= JobAssignmentAttachmentTable::C_TABLE ;
		$this->fldid 	= JobAssignmentAttachmentTable::C_ID ;
	}

	function __destruct() {
	}

    function getTotalAttachmentbyDetailId($detailId) {
        $sql = "SELECT COUNT(*) AS JUMLAH FROM " .JobAssignmentAttachmentTable::C_TABLE . 
        " WHERE " . JobAssignmentAttachmentTable::C_DETAIL_ID . " = " . $detailId;

        $rows = $this->db->getTable($sql) ;
        
        if(is_null($rows) || count($rows) == 0){
            return null;
        }else {
            return $rows[0]['JUMLAH'];
        }
    }
    
    function getDocs($id) {     
        $sql = "SELECT * FROM " .JobAssignmentAttachmentTable::C_TABLE . 
        " WHERE " . JobAssignmentAttachmentTable::C_DETAIL_ID . " = " . $id;
        
        $rows = $this->db->getTable($sql) ;
        
        if(is_null($rows) || count($rows) == 0){
            return null;
        }else {
            return $rows;
        }
    }

    function getDetailDocs($id,$docid) {     
        $sql = "SELECT * FROM " .JobAssignmentAttachmentTable::C_TABLE . 
        " WHERE " . JobAssignmentAttachmentTable::C_DETAIL_ID . " = " . $id .
        " AND " . JobAssignmentAttachmentTable::C_ATTACH_ID . " = " . $docid;

        $rows = $this->db->getTable($sql) ;
        
        if(is_null($rows) || count($rows) == 0){
            return null;
        }else {
            return $rows;
        }
    }

    function deleteDoc($id, $doc_id) {      
        $sql = "delete from " . $this->tbl 
            . " where " . $this->db->fieldParam(JobAssignmentAttachmentTable::C_DETAIL_ID) 
            . " and " . $this->db->fieldParam(JobAssignmentAttachmentTable::C_ATTACH_ID);

        $params = array() ;
        $params[] = $this->db->valueParam(JobAssignmentAttachmentTable::C_DETAIL_ID,$id) ;
        $params[] = $this->db->valueParam(JobAssignmentAttachmentTable::C_ATTACH_ID,$doc_id) ;
        
        return $this->db->deleteRows($sql,$params) ;
    }

    function deleteDocs($id) {      
        $sql = "delete from " . $this->tbl 
            . " where " . $this->db->fieldParam(JobAssignmentAttachmentTable::C_ID) ;

        $params = array() ;
        $params[] = $this->db->valueParam(JobAssignmentAttachmentTable::C_ID,$id) ;
        
        return $this->db->deleteRows($sql,$params) ;
    }

    function updateJobAssignmentDocId($id,$newId,$headerId){
        try {
            $sql = "UPDATE " . JobAssignmentAttachmentTable::C_TABLE . 
                    " SET " . JobAssignmentAttachmentTable::C_DETAIL_ID . " = " . $newId .
                    ", " . JobAssignmentAttachmentTable::C_ID . " = " . $headerId . " " .
                    " WHERE " . JobAssignmentAttachmentTable::C_DETAIL_ID . " = " . $id;

            $this->db->executeSql($sql);
        } catch (Exception $e){
            Log::write("[UpdateDoc] " . $e->getMessage());
        }
    }
}
?>