<?php
require_once (PATH_TABLES . "activity/salesquotation/SalesQuotationAttachmentTable.php") ;
require_once (PATH_MODELS . "base/ActivityMasterBase.php") ;

class SalesQuotationAttachmentClass extends ActivityMasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= SalesQuotationAttachmentTable::C_TABLE ;
		 $this->fldid   = SalesQuotationAttachmentTable::C_ID ;
        $this->fldid2   = SalesQuotationAttachmentTable::C_ATTACH_ID ;
	}

	function __destruct() {
	}

    function copySalesQuotation($quot_id, $new_quot_id){
        $modifydate = date_create('now')->format('Y-m-d H:i:s') ;
        $modifyby   = $_SESSION[SE_USERID];
        $ws         = $_SESSION[SE_REMOTE_IP];

        $sql = "INSERT INTO ".SalesQuotationAttachmentTable::C_TABLE." (" .
            SalesQuotationAttachmentTable::C_ID.",".
            SalesQuotationAttachmentTable::C_ATTACH_ID.",".
            SalesQuotationAttachmentTable::C_DESC.",".
            SalesQuotationAttachmentTable::C_DOC_NAME.",".
            SalesQuotationAttachmentTable::C_TYPE.",".
            SalesQuotationAttachmentTable::C_WS_ID.",".
            SalesQuotationAttachmentTable::C_MODIFY_BY.",".
            SalesQuotationAttachmentTable::C_MODIFY_DATE.",".
            SalesQuotationAttachmentTable::C_CREATE_BY.",".
            SalesQuotationAttachmentTable::C_CREATE_DATE.
            ") SELECT " .
            $new_quot_id.",".
            SalesQuotationAttachmentTable::C_ATTACH_ID.",".
            SalesQuotationAttachmentTable::C_DESC.",".
            SalesQuotationAttachmentTable::C_DOC_NAME.",".
            SalesQuotationAttachmentTable::C_TYPE.",".
            "'".$ws."',".
            $modifyby.",".
            "'".$modifydate."',".
            $modifyby.",".
            "'".$modifydate."'".
            " FROM ".SalesQuotationAttachmentTable::C_TABLE.
            " WHERE " . SalesQuotationAttachmentTable::C_ID . " = " .$quot_id ;
        
		$this->db->executeSql($sql);
    }
    
    function getDocs($id, $attach_type=0) {     
        $sql = "SELECT * FROM " .SalesQuotationAttachmentTable::C_TABLE . 
        " WHERE " . SalesQuotationAttachmentTable::C_ID . " = " . $id." AND ".SalesQuotationAttachmentTable::C_TYPE . " = ".$attach_type;
        
        $rows = $this->db->getTable($sql) ;
        
        if(is_null($rows) || count($rows) == 0){
            return null;
        }else {
            return $rows;
        }
    }

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

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

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

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

    function deleteAttachment($id, $doc_id, $attach_type=1) {      
        $sql = "delete from " . $this->tbl 
            . " where " . $this->db->fieldParam(SalesQuotationAttachmentTable::C_ID)
            . " and " . $this->db->fieldParam(SalesQuotationAttachmentTable::C_ATTACH_ID)
            . " and " . $this->db->fieldParam(SalesQuotationAttachmentTable::C_TYPE) ;

        $params = array() ;
        $params[] = $this->db->valueParam(SalesQuotationAttachmentTable::C_ID,$id) ;
        $params[] = $this->db->valueParam(SalesQuotationAttachmentTable::C_ATTACH_ID,$doc_id) ;
        $params[] = $this->db->valueParam(SalesQuotationAttachmentTable::C_TYPE,$attach_type) ;
        
        return $this->db->deleteRows($sql,$params) ;
    }
	function deleteRecordByQuotationId($quot_id){
        $sql = "DELETE FROM " .SalesQuotationAttachmentTable::C_TABLE.
		" WHERE " . SalesQuotationAttachmentTable::C_ID . " = " .$quot_id ;        
        $this->db->executeSql($sql);
    }
}
?>