<?php

class M_apotek extends CI_Model{		
	
	
	public function get_purchase_apotek($id_obat=null)
	{
		$this->db->select('p.tgl, pi.tgl_kadaluarsa, s.name, pi.qty, pi.price, s.id as id_supplier, p.id as id_purchase, p.no_inv');
                $this->db->from('siak_tr_purchase p');
                $this->db->join('siak_tr_purchase_item pi', 'p.id = pi.id_purchase');
                $this->db->join('bbmc_re_apotik_obat ao','ao.id_re_apotik_obat = pi.id_obat');
                $this->db->join('siak_tr_supplier s','s.id = p.id_supp');
                $this->db->where('p.deleted','0');
                $this->db->where('ao.deleted','0');
                $this->db->where('pi.deleted','0');
                if($id_obat!=null){ $this->db->where('pi.id_obat',$id_obat);}        
                $q = $this->db->get();

                return $q;
	}

	public function get_receive_apotek($id_obat=null)
	{
		$this->db->select('r.tgl, ri.qty, r.ref, r.id as id_receipt, ri.dari');
                $this->db->from('siak_tr_receipt r');
                $this->db->join('siak_tr_receipt_item ri','r.id = ri.id_receipt');
                $this->db->where('r.deleted','0');
                if($id_obat!=null){ $this->db->where('ri.id_obat',$id_obat);}        
                $q = $this->db->get();

                return $q;
	}

    /*
        public function get_sisa_stok($id_obat=null){


                 $this->db->select('sum(qty) as ttl_qty');
                 $this->db->from('siak_tr_purchase_item');
                 $this->db->where('id_obat !=','0');
                 $this->db->where('id_obat',$id_obat);
                 $this->db->group_by('id_obat');
                 $this->db->where('deleted','0');
                 $q = $this->db->get();

                 if($q->num_rows()>=1){
                        $purchase = $q->row()->ttl_qty;
                 }else{
                        $purchase = 0;
                 }
                

                 $this->db->select('sum(qty) as ttl_qty');
                 $this->db->from('siak_tr_inv_item');
                 $this->db->where('id_obat !=','0');
                 $this->db->where('id_obat',$id_obat);
                 $this->db->group_by('id_obat');
                 $this->db->where('deleted','0');
                 $q = $this->db->get();

                 if($q->num_rows()>=1){
                        $invoice = $q->row()->ttl_qty;
                 }else{
                        $invoice = 0;
                 }


                $this->db->select('sum(qty) as ttl_qty');
                 $this->db->from('siak_tr_receipt_item');
                 $this->db->where('id_obat !=','0');
                 $this->db->where('id_obat',$id_obat);
                 $this->db->group_by('id_obat');
                 $this->db->where('deleted','0');
                 $q = $this->db->get();

                 if($q->num_rows()>=1){
                        $receipt = $q->row()->ttl_qty;
                 }else{
                        $receipt = 0;
                 }


                        
                return $purchase-$invoice-$receipt;
        }
        */

        public function get_pembelian_tertinggi($id_obat=null)
        {
           
                $this->db->select('max(price) as price');
                $this->db->from('siak_tr_purchase_item');
                $this->db->where('id_obat !=','0');
                $this->db->where('id_obat', $id_obat);
                $this->db->where('deleted','0');
                $q = $this->db->get();
                if($q->num_rows()>=1){
                        $purchase = $q->row()->price;
                }else{
                        $purchase = 0;
                }
                
                return $purchase!=''?$purchase:'0';

        }

        public function get_nama_pasien_by_kunjungan($id_re_kunjungan)
        {
                $this->db->select('p.nama_pasien, p.nrm');
                $this->db->from('bbmc_tr_kunjungan k');
                $this->db->join('bbmc_tr_pasien p','p.id_re_pasien = k.id_re_pasien');
                $this->db->where('p.deleted','0');
                $this->db->where('k.deleted','0');
                $this->db->where('k.id_re_kunjungan',$id_re_kunjungan);
                $q = $this->db->get();
                if($q->num_rows()==1){
                        $ret = "BBMC-".$q->row()->nrm." ".$q->row()->nama_pasien;
                }else{
                        $ret = '';
                }

                return $ret;
        }

        public function cek_biaya_racikan($id_re_kunjungan)
        {
                $cek = '';
                $cek_jml_obat = '';
                $ada_racikan = '0';
                $total_obat = '0';
                $this->db->where('id_re_kunjungan', $id_re_kunjungan);
                $q = $this->Content_m->select_db('bbmc_tr_pengobatan_pasien');
                foreach ($q->result() as $key ) {
                  if($cek != $key->id_pengobatan){
                    $cek = $key->id_pengobatan;
                    //$total_obat = $total_obat + 1;
                  }else if($cek == $key->id_pengobatan){
                    $ada_racikan = $ada_racikan + 1; 
                    $cek = '';
                  }

                  
                  if($cek_jml_obat != $key->id_pengobatan){
                    $cek_jml_obat = $key->id_pengobatan;
                    $total_obat = $total_obat + 1;
                  }
                  
                 
                }


                $data['jumlah_racikan'] = $ada_racikan;
                $data['total_obat'] = $total_obat;
                return $data;
        }

        public function get_pengobatan_pasien($id_re_kunjungan)
        {
            $this->db->select('pp.id_obat, pp.qty, ao.harga_jual, ao.harga_bpjs, ao.harga_shhc, ao.harga_inhealth, ao.harga_mega, ao.nama_obat, ao.sediaan, ao.jenis_obat, pp.id_pengobatan');
            $this->db->from('bbmc_tr_pengobatan_pasien pp');
            $this->db->join('bbmc_re_apotik_obat ao','ao.id_re_apotik_obat = pp.id_obat');
            $this->db->where('pp.deleted','0');
            $this->db->where('ao.deleted','0');
            $this->db->where('pp.id_re_kunjungan',$id_re_kunjungan);
            $q = $this->db->get();

            return $q;
        }

        public function get_list_obat_pasien($id_re_kunjungan, $untuk, $ttl=null, $valu_only=null)
        {       
                $ttl_kurangi = 0;
                $this->db->where('id_re_kunjungan',$id_re_kunjungan);
                $q_pengobatan = $this->Content_m->select_db('bbmc_tr_pengobatan');
                foreach ($q_pengobatan->result() as $key) { 
                  if($untuk == 'obat'){
                      echo "- ";
                  }
                  
                  $ix=0;
                  $umum = 0;
                  $kurangi = 0;
                  $this->db->where('id_pengobatan',$key->id_re_pengobatan);
                  $q_pengobatan_pasien = $this->Content_m->select_db('bbmc_tr_pengobatan_pasien');
                  foreach ($q_pengobatan_pasien->result() as $keyb ) {
                      if($ix>0){
                        if($valu_only == null){echo "<span style='opacity:0'>- </span>";}
                      }

                      $get_obat = $this->Content_m->get_row('bbmc_re_apotik_obat','id_re_apotik_obat',$keyb->id_obat);
                      $nama_obat = $get_obat->nama_obat;
                      $sediaan = $get_obat->sediaan;
                      $jenis_obat = $get_obat->jenis_obat;
                      $qty = $keyb->qty;
                      $harga_jual = $get_obat->harga_jual;
                      $harga_bpjs = $get_obat->harga_jual;
                      $harga_shhc = $get_obat->harga_jual;
                      $harga_inhealth = $get_obat->harga_jual;
                      $harga_mega = $get_obat->harga_jual;
                      
                      $harga_jual = $harga_jual*$qty;

                      if($untuk == 'obat'){
                        echo ucwords(strtolower($nama_obat))." ".$sediaan." ".$jenis_obat." @".$this->Content_m->currency_edit2($get_obat->harga_jual)." x ".$qty."pcs <br>";
                      }else if($untuk == '1'){
                        //harga umum
                        if($ttl != 'ttl'){echo $this->Content_m->currency($harga_jual)."<br>";}
                      }else if($untuk == '2'){
                        //harga_bpjs
                        //$kurangi = $harga_bpjs*$qty;
                        $kurangi = $harga_jual;
                        if($ttl != 'ttl'){echo '<span style="color:green">- '.$this->Content_m->currency($kurangi)."</span><br>";}
                      }else if($untuk == '3'){
                        //harga shhc
                        //$kurangi = $harga_shhc*$qty;
                        $kurangi = $harga_jual;
                        if($ttl != 'ttl'){echo '<span style="color:green">- '.$this->Content_m->currency($kurangi)."</span><br>";}
                      }else if($untuk == '4'){
                        //harga inhealth
                        //$kurangi = $harga_inhealth*$qty;
                        $kurangi = $harga_jual;
                        if($ttl != 'ttl'){echo '<span style="color:green">- '.$this->Content_m->currency($kurangi)."</span><br>";}
                      }else if($untuk == '5'){
                        //harga mega
                        //$kurangi = $harga_mega*$qty;
                        $kurangi = $harga_jual;
                        if($ttl != 'ttl'){echo '<span style="color:green">- '.$this->Content_m->currency($kurangi)."</span><br>";}
                      }

                      if($ttl == 'ttl'){
                          if($valu_only == null){echo $this->Content_m->currency($harga_jual-$kurangi)."<br>";}
                      }
                      $ttl_kurangi = $ttl_kurangi + ($harga_jual-$kurangi);
                      $ix++;
                  }


              }

              return $ttl_kurangi;

        }

        public function cek_resep($id_re_kunjungan)
        {
           $this->db->select('id_obat, id_pengobatan');
           $this->db->from('bbmc_tr_pengobatan_pasien');
           $this->db->where('id_re_kunjungan',$id_re_kunjungan);
           $this->db->where('deleted','0');
           $q = $this->db->get()->num_rows();

           return $q;
        }
        
        public function get_all_resep($id_re_kunjungan)
        {
           $this->db->select('pp.id_obat, pp.id_pengobatan, pp.qty , pp.cara_pemakaian, ao.nama_obat, ao.harga_jual, ao.harga_bpjs, ao.harga_shhc, ao.harga_inhealth, ao.harga_mega');
           $this->db->from('bbmc_tr_pengobatan_pasien pp');
           $this->db->join('bbmc_re_apotik_obat ao','ao.id_re_apotik_obat = pp.id_obat');
           $this->db->where('pp.id_re_kunjungan',$id_re_kunjungan);
           $this->db->where('pp.deleted','0');
           $q = $this->db->get();

           return $q;
        }

        public function get_all_stok_static()
        {
          $arr_obat = array();

          $this->db->select('stok_display, id_re_apotik_obat, stok_awal');
          $this->db->from('bbmc_re_apotik_obat');
          $this->db->where('deleted','0');
          $q = $this->db->get();
          foreach ($q->result() as $key ) {
              @$arr_obat[$key->id_re_apotik_obat] = $key->stok_display+$key->stok_awal;
          }
          return $arr_obat;
        }

        public function get_all_stok()
        {
           $arr_obat = array();

           $this->db->select('sum(qty) as ttl_qty, id_obat');
           $this->db->from('siak_tr_purchase_item');
           $this->db->where('id_obat !=','0');
           $this->db->group_by('id_obat');
           $this->db->where('deleted','0');
           $q = $this->db->get();
           //echo $this->db->last_query();
           foreach ($q->result() as $key ) {
              @$arr_obat[$key->id_obat] = $key->ttl_qty;
           }

           $this->db->select('sum(qty) as ttl_qty, id_obat');
           $this->db->from('siak_tr_inv_item');
           $this->db->where('id_obat !=','0');
           $this->db->group_by('id_obat');
           $this->db->where('deleted','0');
           $q = $this->db->get();

           foreach ($q->result() as $key ) {
              @$arr_obat[$key->id_obat] = @$arr_obat[$key->id_obat] - $key->ttl_qty;
           }

           $this->db->select('sum(qty) as ttl_qty');
           $this->db->from('siak_tr_receipt_item');
           $this->db->where('id_obat !=','0');
           $this->db->group_by('id_obat');
           $this->db->where('deleted','0');
           $q = $this->db->get();

           foreach ($q->result() as $key ) {
              @$arr_obat[$key->id_obat] = @$arr_obat[$key->id_obat] - $key->ttl_qty;
           }


           return $arr_obat;
           
        }

        public function get_sisa_stok($id_obat=null){


           $this->db->select('sum(qty) as ttl_qty');
           $this->db->from('siak_tr_purchase_item');
           $this->db->where('id_obat !=','0');
           $this->db->where('id_obat',$id_obat);
           $this->db->group_by('id_obat');
           $this->db->where('deleted','0');
           $q = $this->db->get();

           if($q->num_rows()>=1){
                  $purchase = $q->row()->ttl_qty;
           }else{
                  $purchase = 0;
           }
          

           $this->db->select('sum(qty) as ttl_qty');
           $this->db->from('siak_tr_inv_item');
           $this->db->where('id_obat !=','0');
           $this->db->where('id_obat',$id_obat);
           $this->db->group_by('id_obat');
           $this->db->where('deleted','0');
           $q = $this->db->get();

           if($q->num_rows()>=1){
                  $invoice = $q->row()->ttl_qty;
           }else{
                  $invoice = 0;
           }


           $this->db->select('sum(qty) as ttl_qty');
           $this->db->from('siak_tr_receipt_item');
           $this->db->where('id_obat !=','0');
           $this->db->where('id_obat',$id_obat);
           $this->db->group_by('id_obat');
           $this->db->where('deleted','0');
           $q = $this->db->get();

           if($q->num_rows()>=1){
                  $receipt = $q->row()->ttl_qty;
           }else{
                  $receipt = 0;
           }


                  
          return $purchase-$invoice-$receipt;
          
        }                


}

?>