<?php

class Rmsin_mdl extends Trxin_mdl {

    function __construct() {

        parent::__construct();
		$this->pk = 'matin_id';
    }
	
// rm subcont --dim
function getHdr($page,$rows,$key='',$KdJnsDok=''){
    $offset = ($page-1)*$rows;
    $result = array();
    $cond_KdJnsDok = "";
    if ($KdJnsDok != ""){
        $cond_KdJnsDok = " AND a.KdJnsDok = '$KdJnsDok' ";
    }

    if ($this->matin_type != ""){
        $cond_matin_type = " AND a.matin_type = '$this->matin_type' ";
    }
    
    $q = "SELECT distinct a.*,a.".$this->pk." AS id,
          DATE_FORMAT(matin_date,'%d/%m/%Y') AS Fmatin_date,
          DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
          DATE_FORMAT(TgAju,'%d/%m/%Y') AS FTgAju,
          c.matin_name,d.NmPrshn,f.UrJnsDok,
          g.NmPrshn AS company,
          FORMAT(cif,2) AS cif,
          FORMAT(tot_amount,2) AS tot_amount,
          a.tot_volume
          FROM ".$this->tbl." a
          LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=a.".$this->pk3."
          LEFT JOIN ".$this->tbl4." d ON d.".$this->pk4."=a.supplier
          LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."=a.".$this->pk6."
          LEFT JOIN ".$this->tbl4." g ON g.".$this->pk4."=a.kd_cmpy
         WHERE mat_type IN (".$this->mat_type.")
         $cond_matin_type	
         $cond_KdJnsDok";
    $q .= " AND (a.matin_no LIKE '%$key%' OR d.NmPrshn LIKE '%$key%' 
            OR a.NoDaf LIKE '%$key%' OR a.po_no LIKE '%$key%')
          ORDER BY matin_id DESC";
    $rstot=$this->db->query($q)->result();
    //echo $q;
    $q .= " LIMIT $offset,$rows";
    $rs=$this->db->query($q)->result();
    
    $result["total"] = count($rstot);
    $result["rows"] = $rs;
    
    return $result;
}

function getHdrDet($page,$rows,$key='',$KdJnsDok=''){
    $offset = ($page-1)*$rows;
    $result = array();
    $cond_KdJnsDok = "";
    if ($KdJnsDok != ""){
        $cond_KdJnsDok = " AND a.KdJnsDok = '$KdJnsDok' ";
    }

    if ($this->matin_type != ""){
        $cond_matin_type = " AND a.matin_type = '$this->matin_type' ";
    }
    
    $q = "SELECT distinct a.*,a.".$this->pk." AS id,
          DATE_FORMAT(matin_date,'%d/%m/%Y') AS Fmatin_date,
          DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
          DATE_FORMAT(TgAju,'%d/%m/%Y') AS FTgAju,
          c.matin_name,d.NmPrshn,f.UrJnsDok,
          g.NmPrshn AS company,
          FORMAT(cif,2) AS cif,
          FORMAT(tot_amount,2) AS tot_amount,
          a.tot_volume
          FROM ".$this->tbl." a
          LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=a.".$this->pk3."
          LEFT JOIN ".$this->tbl4." d ON d.".$this->pk4."=a.supplier
          LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."=a.".$this->pk6."
          LEFT JOIN ".$this->tbl4." g ON g.".$this->pk4."=a.kd_cmpy
         WHERE mat_type IN (".$this->mat_type.")
         $cond_matin_type	
         $cond_KdJnsDok";
    $q .= " AND (a.matin_no LIKE '%$key%' OR d.NmPrshn LIKE '%$key%' 
            OR a.NoDaf LIKE '%$key%' OR a.po_no LIKE '%$key%')
          ORDER BY matin_id DESC";
    $rstot=$this->db->query($q)->result();
    //echo $q;
    $q .= " LIMIT $offset,$rows";
    $rs=$this->db->query($q)->result();
    
    $result["total"] = count($rstot);
    $result["rows"] = $rs;
    
    return $result;
}

}