<?php
defined('BASEPATH') or exit('No direct script access allowed');
class M_Transaksi extends CI_Model
{

  public function getdokter()
  {
    return  $this->db->query("Select * from Doctor where Active = 'T' order by DoctorID");
  }

  public function getcategory()
  {
    return  $this->db->query("Select * from Category where Status = 'Y'");
  }

  public function gettreatment()
  {
    return  $this->db->query("Select * from MedicalProcedure where Active = 'T'");
  }

  public function gettreatmentedit($idtrx)
  {
    $qcek = $this->db->query("Select RefferedBy from TransactionHeader where TransactionID = '$idtrx'")->row_array();
    $poli = $qcek['RefferedBy'];
    return  $this->db->query("Select * from MedicalProcedure where Active = 'T' and Category = '$poli'");
  }

  public function getedit($id)
  {
    return  $this->db->query("Select * from Activity where ID = '$id'");
  }

  public function getTrans($id)
  {
    return $this->db->query("Select t.TransactionID,c.[Case ID],c.Description,co.CompanyName,t.DateCreated,d.FirstName
        from TransactionHeader t join Doctor d on t.DoctorID = d.DoctorID 
        join Cases c on c.[Case ID] = t.[Case ID]
        join Company co on co.CompanyCode = C.CompanyCode
        where t.[Case ID] = '$id'
        order by TransactionID desc ");
  }

  public function getDetailTrans($id, $idtrx)
  {
    return $this->db->query("Select  th.[Case ID],a.ID,th.TransactionID,a.Activity,a.Charge,a.DoctorID,a.COA, 
        m.Description,th.RefferedBy,d.FirstName,th.Transaction_Date
        From Activity a
        join MedicalProcedure m on m.ProcedureCode = a.Activity
        left join doctor d on d.DoctorID = a.DoctorID
                join TransactionHeader th on th.TransactionID = a.TransactionID
                where th.TransactionID = '$idtrx'");
  }

  public function getDetailDepo($idtrx)
  {
    return $this->db->query("Select  ap.[Case ID],a.ID,ap.TransactionID,a.DrugsID,p.Description,a.Qty,a.Unit,a.UnitPrice,a.Charge,a.Dosis,a.Cara, 
    ap.RefferedBy,d.FirstName,ap.Transaction_Date
    From [Activity Drugs] a
    join Pharmacy p on p.PharmacyCode = a.DrugsID
    join ApotikHeader ap on ap.TransactionID = a.TransactionID
    left join doctor d on d.DoctorID = ap.DoctorID
    where a.TransactionID = '$idtrx'");
  }

  public function tindakan()
  {
    return $this->db->query("SELECT M.[ProcedureCode] ,M.[Description] ,M.[Category] ,M.[Active] ,M.[COA] ,mp.Class ,mp.Price
        FROM [Kalideres].[dbo].[MedicalProcedure] M JOIN MedicalProcedurePrice MP ON M.ProcedureCode = mp.ProcedureCode where Active = 'T'");
  }

  public function getDetailEdit($id)
  {
    return $this->db->query("Select a.ID,th.TransactionID,a.Activity,m.Description,m.Category,a.Charge,a.DoctorID,d.FirstName,a.COA, th.Transaction_Date
        From MedicalProcedure m join Activity a on a.Activity = m.ProcedureCode
        join Doctor d on d.DoctorID = a.DoctorID
        join TransactionHeader th on th.TransactionID = a.TransactionID
        where th.TransactionID = '$id'");
  }

  public function save_batch($data2)
  {
    return $this->db->insert_batch('Activity', $data2);
  }

  public function detail()
  {
    return $this->db->query("Select top 10 a.ID,th.TransactionID,a.Activity,m.Description,m.Category,a.Charge,a.DoctorID,d.FirstName,a.COA, th.Transaction_Date
      From MedicalProcedure m join Activity a on a.Activity = m.ProcedureCode
      join Doctor d on d.DoctorID = a.DoctorID
      join TransactionHeader th on th.TransactionID = a.TransactionID");
  }

  public function detailTransaction($id)
  {
    return $this->db->query("select c.[Case ID],c.PatientCode,c.Description,c.[Case Status], c.[Intake Date],c.[Closed Date],c.Category,
      c.[Presenting Problem],d.FirstName,cp.CompanyName 
      from Cases c join CaseAdmin cd on cd.[Case ID] = c.[Case ID] join Doctor d on d.DoctorID = cd.DoctorID 
      join Company cp on cp.CompanyCode = c.CompanyCode where c.[Case ID] = '$id'");
  }

  public function detailtrx($id)
  {
    return $this->db->query("Select a.ID,th.[Case ID],th.TransactionID,a.Activity,a.Charge,a.DoctorID,a.COA, th.Transaction_Date,th.RefferedBy
    From  Activity a
    join TransactionHeader th on th.TransactionID = a.TransactionID where th.[Case ID] = '$id' ");
  }

  public function total($id)
  {
    return $this->db->query("Select sum(a.Charge) as total
    From  Activity a
    join TransactionHeader th on th.TransactionID = a.TransactionID where th.[Case ID] = '$id'");
  }

  public function detailtrxdepo($id)
  {
    return $this->db->query("Select a.ID,th.[Case ID],a.TransactionID,a.Charge,th.Transaction_Date,th.RefferedBy
    From [Activity Drugs] a join ApotikHeader th on th.TransactionID = a.TransactionID 
	  where a.[Case ID] = '$id'");
  }

  public function totaldepo($id)
  {
    return $this->db->query("Select Sum(Charge + PPn) as total
    From  [Activity Drugs] a
    join ApotikHeader th on th.TransactionID = a.TransactionID where th.[Case ID] = '$id'");
  }
}
