<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Apotik extends CI_Controller
{

    // CONSTRUCT
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('M_rekamMedis');
        $this->load->model('M_apotek');
        $this->load->model('M_Utama');
        $this->load->model('M_user');
        $this->load->helper('url');

        if (!$this->session->userdata('username')) {
            redirect('login');
        }
    }


    public function index()
    {
        $tgl = date('Y-m-d');

        $tglParam = $this->input->get('tgl');
        if (!empty($tglParam)) {
            $tgl = $tglParam;
        }
        $data['tgl'] = $tgl;
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['pasien'] = $this->db->query(
            "Select rh.ResepId,rh.CaseID,c.Description,c.[Intake Date],C.Category,d.FirstName as dokter,c.CompanyCode,cp.CompanyName,rh.Jenis,rh.DoctorId,cd.NoAntrian,cd.alergiobat,e.alergi From Resep_Header rh 
            join Cases c on c.[Case ID] = rh.CaseID 
            join CaseDetail cd on cd.[Case ID] = c.[Case ID]
            join Doctor d on d.DoctorID = rh.DoctorId
            join erm_pemeriksaan_fisik e on e.case_id = c.[Case ID]
            join Company cp on cp.CompanyCode = c.CompanyCode where rh.AddTrans = 0 AND c.[Intake Date]= '$tgl' ORDER BY c.[Case ID] DESC"
        )->result_array();


        $data['Countpasien'] = $this->db->query(
            "SELECT * FROM Resep_Header h, Cases c, Doctor d WHERE h.[CaseID]=c.[Case ID] AND h.DoctorID=d.DoctorID AND h.Status = 1 AND StatusPembuatan  IS NULL  AND c.[Intake Date]= '$tgl' ORDER BY InTakeDate ASC"
        )->num_rows();


        $data['obat'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['jumlahSatuanPakai'] = $this->db->query("SELECT * FROM JumlahSatuanPakai ORDER BY JumlahSatuanPakai ASC")->result_array();
        $data['caraPakai'] = $this->db->query("SELECT * FROM Resep_CaraPakai ORDER BY CaraPakai ASC")->result_array();
        $data['dosis'] = $this->db->query("SELECT * FROM DosisPakai")->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/index', $data);
        $this->load->view('properti/footer', $data);
    }

    public function detailApotekRacikan($id)
    {
        extract($_GET);
        $idCase = $case;
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['rDetail'] = $this->db->query("SELECT * FROM Resep_Detail RD, Pharmacy P WHERE RD.PharmacyCode=P.PharmacyCode AND ResepId = '$id'")->result_array();
        $data['tampilracik'] = $this->M_rekamMedis->tampilracik2($id)->result_array($id);
        $data['pharmacy'] = $this->M_rekamMedis->pharmacy()->result_array();
        $data['obatmodal'] = $this->M_rekamMedis->obatmodal()->result_array();
        // =====================================================================================
        // $this->db->query("UPDATE Resep_Header SET [AddTrans] = '1' WHERE ResepId = '$id'");

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/detailApotekRacikan', $data);
        $this->load->view('properti/footer', $data);
    }


    public function detailApotekTrans($id)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Ymd');
        extract($_GET);
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['rDetail'] = $this->db->query("SELECT * FROM Resep_Detail RD, Pharmacy P WHERE RD.PharmacyCode=P.PharmacyCode AND ResepId = '$id'")->result_array();
        $data['isEnabledButtonAddTransaction'] = true;

        $wareHouseCode = $this->session->userdata('wareHouseCode');

        foreach ($data['rDetail'] as $dataDetail) {
            $aPharmacyCode = $dataDetail['PharmacyCode'];
            $checkStock = $this->db->query(" EXEC Sp_SisaStok '$tgl' , '$wareHouseCode', '$aPharmacyCode'   ")->row_array()['TotQty'];
            if ($dataDetail['iQty'] > $checkStock) {
                $data['isEnabledButtonAddTransaction'] = false;
            }
        }
        $data['obat'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['jumlahSatuanPakai'] = $this->db->query("SELECT * FROM JumlahSatuanPakai ORDER BY JumlahSatuanPakai ASC")->result_array();
        $data['caraPakai'] = $this->db->query("SELECT * FROM Resep_CaraPakai ORDER BY CaraPakai ASC")->result_array();
        $data['dosis'] = $this->db->query("SELECT * FROM DosisPakai")->result_array();
        // =====================================================================================
        // $this->db->query("UPDATE Resep_Header SET [AddTrans] = '1' WHERE ResepId = '$id'");

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/detailApotekTrans', $data);
        $this->load->view('properti/footer', $data);
    }

    public function submitHeader($resepId)
    {
        date_default_timezone_set("Asia/Jakarta");
        $CaseID = $this->input->post('CaseID');
        $PatientCode = $this->input->post('PatientCode');
        $Transaction_Date = $this->input->post('Transaction_Date');
        $Transaction_Time = $this->input->post('Transaction_Time');
        $DoctorID = $this->input->post('DoctorID');
        $RefferedBy = $this->input->post('RefferedBy');
        $Class = $this->input->post('Class');
        // $Room = $this->input->post('Room');
        $UserCreated = $this->input->post('UserCreated');
        $DateCreated = $this->input->post('DateCreated');
        $TransStatus = $this->input->post('TransStatus');
        $Type = $this->input->post('Type');
        //$RoomUnit = $this->input->post('RoomUnit');
        $Cara = $this->input->post('Cara');
        // $Ket = str_replace(" ", "", $this->input->post('Ket'));
        $Dosis = $this->input->post('Dosis');
        $Unit  = $this->input->post('Unit');
        $Poli  = $this->input->post('poli');
        $userCreat = $UserCreated[0];

        $no = $this->db->query("SELECT * FROM SystemNo WHERE SystemName = 'Trans'")->row_array();
        $noAwal = $no['NoNumeric'] + 1;
        $this->db->query("UPDATE SystemNo SET NoNumeric = '$noAwal' WHERE SystemName = 'Trans'");
        $nomor = '3' . $no['NoNumeric'];

        // $no = $this->db->query("SELECT * FROM Penomoran WHERE ModulAktivitas = 'Trans'")->row_array();
        // $noAwal = $no['NoUrut'] + 1;
        // $this->db->query("UPDATE Penomoran SET NoUrut = '$noAwal' WHERE ModulAktivitas = 'Trans'");
        // $nomor = '2' . $no['NoUrut'];

        $this->db->query("
                INSERT INTO ApotikHeader (
                    TransactionID,
                    [Case ID],
                    PatientCode,
                    Transaction_Date,
                    Transaction_Time,
                    PresNo,
                    DoctorID,
                    RefferedBy,
                    Class,
                    UserCreated,
                    DateCreated,
                    TransStatus,
                    Type,
                    LastUserModified,
                    LastDateModified,
                    LockStatus
                    ) 
                VALUES (
                    '$nomor',
                    '$CaseID',
                    '$PatientCode',
                    '$Transaction_Date',
                    '$Transaction_Time',
                    '$resepId',
                    '$DoctorID',
                    '$RefferedBy',
                    '$Class',
                    '$userCreat',
                    '$DateCreated',
                    '$TransStatus',
                    '$Type',
                    '$userCreat',
                    '$Transaction_Time',
                    '0'
                )
            ");

        $cek = $_POST['cek'];
        $TransactionID = $nomor;
        $CaseIDs = $this->input->post('CaseIDs');
        $DrugsID = $this->input->post('DrugsID');
        $aCase = $CaseIDs[0];
        $Qty = $this->input->post('Qty');
        $DateCreated = date('Y-m-d H:i:s');
        $jumlah_dipilih1 = count($cek);

        $Charge = $this->input->post('Charge');
        $NettPrice = $this->input->post('NettPrice');
        $UnitPrice = $this->input->post('UnitPrice');
        $FromDepo = "2";
        $PPN = $this->input->post('PPN');

        // ADD ACTIVITY JURNAL
        $TransID = $nomor;
        $UserID = $this->input->post('IdComp');
        $CompID = $this->input->post('IdUser');
        $companyCode = $this->input->post('company');

        for ($x = 0; $x < $jumlah_dipilih1; $x++) {
            $this->db->query("INSERT INTO [Activity Drugs] (
                TransactionID,
                [Case ID],
                Unit,
                DrugsID,
                Charge,
                Qty,
                BasicPrice,
                UnitPrice,
                FromDepo,
                Shift,
                DateCreated,
                UserCreated,
                PPN,
                Dosis,
                Cara
            ) VALUES (
                '$TransactionID',
                '$CaseIDs[$x]',
                '$Unit[$x]',
                '$DrugsID[$x]',
                '$Charge[$x]',
                '$Qty[$x]',
                '$NettPrice[$x]',
                '$UnitPrice[$x]',
                '$FromDepo',
                '1',
                '$DateCreated',
                '$UserCreated[$x]',
                '$PPN[$x]',
                '$Dosis[$x]',
                '$Cara[$x]'
            )");
        }


        $cases = $this->db->query("Select CompanyCode from Cases where [Case ID] = '$CaseID' ")->row_array();
        if ($cases['CompanyCode'] == '102') {
            $this->db->query("exec ACC_PharmacyDepoJournalBPJS'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        } else {
            $this->db->query("exec ACC_PharmacyDepoJournal'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        }

        $this->db->query("UPDATE Resep_Header SET [AddTrans] = '1' WHERE ResepId = '$resepId'");

        $this->db->query("UPDATE Resep_Header SET [Status] = '2' WHERE ResepId = '$resepId'");

        $this->session->set_flashdata("success", "Ok!");
        redirect('apotik/recordPharmacy/' . $TransactionID);
    }



    public function submitHeaderRacikan($resepId)
    {
        date_default_timezone_set("Asia/Jakarta");
        $CaseID = $this->input->post('CaseID');
        $PatientCode = $this->input->post('PatientCode');
        $Transaction_Date = $this->input->post('Transaction_Date');
        $Transaction_Time = $this->input->post('Transaction_Time');
        $DoctorID = $this->input->post('DoctorID');
        $RefferedBy = $this->input->post('RefferedBy');
        $Class = $this->input->post('Class');
        // $Room = $this->input->post('Room');
        $UserCreated = $this->input->post('UserCreated');
        $DateCreated = $this->input->post('DateCreated');
        $TransStatus = $this->input->post('TransStatus');
        $Type = $this->input->post('Type');
        //$RoomUnit = $this->input->post('RoomUnit');
        $Cara = $this->input->post('Cara');
        // $Ket = str_replace(" ", "", $this->input->post('Ket'));
        $Dosis = $this->input->post('Dosis');
        $Unit  = $this->input->post('Unit');
        $Poli = $this->input->post('poli');
        $companyCode = $this->input->post('company');
        $userCreat = $UserCreated[0];

        $no = $this->db->query("SELECT * FROM SystemNo WHERE SystemName = 'Trans'")->row_array();
        $noAwal = $no['NoNumeric'] + 1;
        $this->db->query("UPDATE SystemNo SET NoNumeric = '$noAwal' WHERE SystemName = 'Trans'");
        $nomor = '3' . $no['NoNumeric'];

        // $no = $this->db->query("SELECT * FROM Penomoran WHERE ModulAktivitas = 'Trans'")->row_array();
        // $noAwal = $no['NoUrut'] + 1;
        // $this->db->query("UPDATE Penomoran SET NoUrut = '$noAwal' WHERE ModulAktivitas = 'Trans'");
        // $nomor = '2' . $no['NoUrut'];

        $this->db->query("
                INSERT INTO ApotikHeader (
                    TransactionID,
                    [Case ID],
                    PatientCode,
                    Transaction_Date,
                    Transaction_Time,
                    PresNo,
                    DoctorID,
                    RefferedBy,
                    Class,
                    UserCreated,
                    DateCreated,
                    TransStatus,
                    Type,
                    LastUserModified,
                    LastDateModified,
                    LockStatus
                    ) 
                VALUES (
                    '$nomor',
                    '$CaseID',
                    '$PatientCode',
                    '$Transaction_Date',
                    '$Transaction_Time',
                    '$resepId',
                    '$DoctorID',
                    '$RefferedBy',
                    '$Class',
                    '$userCreat',
                    '$DateCreated',
                    '$TransStatus',
                    '$Type',
                    '$userCreat',
                    '$Transaction_Time',
                    '0'
                )
            ");

        $cek = $_POST['cek'];
        $TransactionID = $nomor;
        $CaseIDs = $this->input->post('CaseIDs');
        $DrugsID = $this->input->post('DrugsID');
        $aCase = $CaseIDs[0];
        $Qty = $this->input->post('Qty');
        $DateCreated = date('Y-m-d H:i:s');
        $jumlah_dipilih1 = count($cek);

        $Charge = $this->input->post('Charge');
        $BasicPrice = $this->input->post('BasicPrice');
        $UnitPrice = $this->input->post('UnitPrice');
        $NettPrice = $this->input->post('NettPrice');
        $FromDepo = "2";
        $PPN = $this->input->post('PPN');

        for ($x = 0; $x < $jumlah_dipilih1; $x++) {
            $this->db->query("INSERT INTO [Activity Drugs] (
                TransactionID,
                [Case ID],
                Unit,
                DrugsID,
                Charge,
                Qty,
                BasicPrice,
                UnitPrice,
                FromDepo,
                Shift,
                DateCreated,
                UserCreated,
                PPN,
                Dosis,
                Cara
            ) VALUES (
                '$TransactionID',
                '$CaseIDs[$x]',
                '$Unit[$x]',
                '$DrugsID[$x]',
                '$Charge[$x]',
                '$Qty[$x]',
                '$NettPrice[$x]',
                '$UnitPrice[$x]',
                '$FromDepo',
                '1',
                '$DateCreated',
                '$UserCreated[$x]',
                '$PPN[$x]',
                '$Dosis[$x]',
                '$Cara[$x]'
            )");
        }
        $this->session->set_flashdata("success", "Ok!");

        // ADD ACTIVITY JURNAL
        $TransID = $nomor;
        $UserID = $this->input->post('IdComp');
        $CompID = $this->input->post('IdUser');

        $cases = $this->db->query("Select CompanyCode from Cases where [Case ID] = '$CaseID' ")->row_array();
        if ($cases['CompanyCode'] == '102') {
            $this->db->query("exec ACC_PharmacyDepoJournalBPJS'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        } else {
            $this->db->query("exec ACC_PharmacyDepoJournal'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        }

        $this->db->query("UPDATE Resep_Header SET [Status] = '2' WHERE ResepId = '$resepId'");
        $this->db->query("UPDATE Resep_Header SET [AddTrans] = '1' WHERE ResepId = '$resepId'");

        redirect('apotik/recordPharmacyRacikan/' . $TransactionID);
    }

    public function recordPharmacy($transaksi)
    {
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['TansID'] = $this->db->query(
            "SELECT * FROM ApotikHeader WHERE TransactionID = '$transaksi'"
        )->row_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/recordPharmacy', $data);
        $this->load->view('properti/footer', $data);
    }

    public function prosesPembuatan($Trx, $case)
    {
        // ---------------------------------------------------------------------
        $this->db->query("UPDATE ApotikHeader SET LockStatus = '1' WHERE TransactionID = '$Trx'");
        $this->session->set_flashdata("success", "Berhasil Ditambahkan!");
        redirect('apotik/recordPharmacy/' . $Trx);
    }

    public function prosesPembuatanRacikan($Trx, $case)
    {
        $this->db->query("UPDATE ApotikHeader SET LockStatus = '1' WHERE TransactionID = '$Trx'");
        redirect('apotik/recordPharmacyRacikan/' . $Trx);
    }

    public function listRecord()
    {
        date_default_timezone_set("Asia/Jakarta");
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $tgl = date('Y-m-d');
        $data['pasien'] = $this->db->query(
            "SELECT TOP 700 * FROM ApotikHeader AH, Cases C, Doctor D WHERE C.[Intake Date] = '$tgl' And AH.[Case ID] = C.[Case ID] AND  AH.DoctorID = D.DoctorID ORDER BY Transaction_Date DESC"
        )->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/listRecord', $data);
        $this->load->view('properti/footer', $data);
    }

    public function record($id)
    {
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['TansID'] = $this->db->query(
            "SELECT * FROM ApotikHeader AH, Cases C, Doctor D WHERE AH.[Case ID] = C.[Case ID] AND  AH.DoctorID = D.DoctorID AND AH.[Case ID] = '$id' ORDER BY TransactionID DESC"
        )->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/record', $data);
        $this->load->view('properti/footer', $data);
    }

    public function recordPharmacyRacikan($transaksi)
    {
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['TansID'] = $this->db->query(
            "SELECT * FROM ApotikHeader WHERE TransactionID = '$transaksi'"
        )->row_array();


        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/recordPharmacyRacikan', $data);
        $this->load->view('properti/footer', $data);
    }

    public function hapusobatdokter($id, $caseid)
    {
        $this->db->delete('Resep_Detail', ['PharmacyCode' => $id]);
        $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert"> Data Berhasil Dihapus </div>');
        redirect('RekamMedis/pemeriksaan' . '/' . $caseid);
    }

    public function hapusobatracikdokter($id, $case)
    {
        $this->db->query("DELETE FROM Resep_Racikan_D WHERE IdRacikan= '$id'");
        $this->db->query("DELETE FROM Resep_Racikan_H WHERE IdRacikan= '$id'");
        $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert"> Data Berhasil Dihapus </div>');
        redirect('RekamMedis/pemeriksaan' . '/' . $case);
    }

    public function hapusobatdetail($id, $case)
    {
        $idresep = $this->uri->segment(5);
        $this->db->delete('Resep_Detail', ['PharmacyCode' => $id]);
        $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert"> Data Berhasil Dihapus </div>');
        redirect('Apotik/rDetail' . '/' . $case . '/' . $idresep);
    }

    public function rDetail($id, $idracik)
    {
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['obat'] = $this->M_rekamMedis->obatparmasi($idracik)->result_array();
        $data['obat2'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['JumlahSatuanPakai'] = $this->db->query("SELECT * FROM JumlahSatuanPakai ORDER BY JumlahSatuanPakai ASC")->result_array();
        $data['caraPakai'] = $this->db->query("SELECT * FROM Resep_CaraPakai ORDER BY CaraPakai ASC")->result_array();
        $data['aturan'] = $this->db->query("SELECT * FROM Resep_AturanPakai ORDER BY AturanPakai ASC")->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/rDetail', $data);
        $this->load->view('properti/footer', $data);
    }

    public function rRacikDetail($id, $idracik)
    {
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['obat'] = $this->M_rekamMedis->obatparmasi($idracik)->result_array();
        $data['obat2'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['JumlahSatuanPakai'] = $this->db->query("SELECT * FROM JumlahSatuanPakai ORDER BY JumlahSatuanPakai ASC")->result_array();
        $data['dosis'] = $this->M_rekamMedis->dosis()->result_array();
        $data['pharmacy'] = $this->M_rekamMedis->pharmacy()->result_array();
        $data['caraPakai'] = $this->db->query("SELECT * FROM Resep_CaraPakai ORDER BY CaraPakai ASC")->result_array();
        $data['AturanPakai'] = $this->db->query("SELECT * FROM Resep_AturanPakai ORDER BY AturanPakai ASC")->result_array();
        $data['obatmodal'] = $this->M_rekamMedis->obatmodal()->result_array();
        //========================================================================================================
        $idracik = $this->uri->segment(4);
        $data['tampilracik'] = $this->M_rekamMedis->tampilracik($idracik)->result_array();
        $data['headerRacik'] = $this->M_rekamMedis->headerRacik($idracik)->row_array();
        $data['counttampilracik'] = $this->M_rekamMedis->tampilracik($idracik)->num_rows();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/rRacikDetail', $data);
        $this->load->view('properti/footer', $data);
    }

    public function hapusracikandokter($id, $case)
    {
        $this->db->query("DELETE FROM Resep_Racikan_D WHERE PharmacyCode= '$id'");
        $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert"> Data Berhasil Dihapus </div>');
        redirect('RekamMedis/pemeriksaan' . '/' . $case);
    }

    public function pharmacy()
    {
        extract($_GET);
        date_default_timezone_set("Asia/Jakarta");
        // $tgl = date('Y-m-d');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['cases'] = $this->db->query(
            "SELECT * FROM Cases WHERE [Intake Date] = '$tgl' ORDER BY [Case ID] DESC"
        )->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/viewpharmacy/pharmacy', $data);
        $this->load->view('properti/footer', $data);
    }

    public function cari()
    {
        extract($_GET);
        date_default_timezone_set("Asia/Jakarta");
        // $tgl = date('Y-m-d');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();

        if (isset($mr)) {
            $data['cases'] = $this->db->query(
                "SELECT * FROM Cases WHERE [PatientCode] = '$mr' ORDER BY [Case ID] DESC"
            )->result_array();
        }

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/viewpharmacy/Cari', $data);
        $this->load->view('properti/footer', $data);
    }

    public function transaksiPharmacy($caseID)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['pasienDetail'] = $this->db->query(
            "SELECT * FROM Cases WHERE [Case ID] = '$caseID'"
        )->row_array();
        $data['apotikDetail'] = $this->db->query("SELECT * FROM ApotikHeader WHERE [Case ID] = '$caseID'")->result_array();
        $data['obat'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['caraPakai'] = $this->M_rekamMedis->caraPakai()->result_array();
        $data['dosis'] = $this->db->query("SELECT * FROM Resep_AturanPakai")->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/viewpharmacy/transaksiPharmacy', $data);
        $this->load->view('properti/footer', $data);
    }

    public function addPharmacy($caseID)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['pasienDetail'] = $this->db->query(
            "SELECT * FROM Cases WHERE [Case ID] = '$caseID'"
        )->row_array();
        $data['apotikDetail'] = $this->db->query("SELECT * FROM ApotikHeader WHERE [Case ID] = '$caseID'")->result_array();
        $data['obat'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['caraPakai'] = $this->M_rekamMedis->caraPakai()->result_array();
        $data['dosis'] = $this->db->query("SELECT * FROM Resep_AturanPakai")->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/viewpharmacy/addPharmacy', $data);
        $this->load->view('properti/footer', $data);
    }

    public function prosesAddPharmacy()
    {
        date_default_timezone_set("Asia/Jakarta");
        $TransactionID = $this->input->post('TransactionID');
        $caseID = $this->input->post('caseID');
        $DrugsId = $this->input->post('DrugsId');
        $Qty = $this->input->post('Qty');
        $dosis = $this->input->post('dosis');
        $caraPakai = $this->input->post('caraPakai');
        $UserCreated = $this->input->post('user');
        $keterangan = $this->input->post('ket');

        $utama = $this->db->query("Select * From Utama")->row_array();
        $getCase = $this->db->query("SELECT Category,LastOP FROM Cases WHERE [Case ID] = '$caseID'")->row_array();
        $aPharmacyCode = $DrugsId;
        $aCase = $caseID;
        $Poli = $getCase["LastOP"];
        $aQuery = $this->db->query("exec Sp_DrugsPriceWeb  '" . $aPharmacyCode . "'   ")->row_array();
        // RUMUS
        $a = $aQuery['DrugsPrice'] * $Qty;
        if ($getCase['Category'] == 'OP') {
            $b = ($utama['PPN'] / 100) * $a;
        } else {
            $b = 0;
        }
        $c = $aQuery['NettPrice'];
        $d = $aQuery['DrugsPrice'];
        $squery = $this->db->query("Select * From Pharmacy where PharmacyCode = '$aPharmacyCode' ")->row_array();
        $Unit = $squery['PharmacyUnit'];

        $Charge = number_format($a);
        $BasicPrice = number_format($c);
        $UnitPrice = number_format($d);
        $PPN = number_format($b);
        $DateCreated = date('Y-m-d H:i:s');
        $this->db->query("INSERT INTO [Activity Drugs] (
                TransactionID,
                [Case ID],
                Unit,
                DrugsID,
                Charge,
                Qty,
                BasicPrice,
                UnitPrice,
                FromDepo,
                Shift,
                DateCreated,
                UserCreated,
                PPN,
                Dosis,
                Cara,
                Ket
        ) VALUES (
            '$TransactionID',
            '$caseID',
            '$Unit',
            '$DrugsId',
            '$Charge',
            '$Qty',
            '$BasicPrice',
            '$UnitPrice',
            '2',
            '1',
            '$DateCreated',
            '$UserCreated',
            '$PPN',
            '$dosis',
            '$caraPakai',
            '$keterangan'
        )");

        $cases = $this->db->query("Select CompanyCode from Cases where [Case ID] = '$caseID' ")->row_array();
        if ($cases['CompanyCode'] == '102') {
            $this->db->query("exec ACC_PharmacyDepoJournalBPJS'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        } else {
            $this->db->query("exec ACC_PharmacyDepoJournal'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        }

        redirect("apotik/addPharmacy/" . $caseID . "?isi=" . $TransactionID);
    }

    public function hapusPharmacy($id)
    {
        $TransactionID = $this->uri->segment(4);
        $this->db->query("DELETE FROM [Activity Drugs] WHERE ID = '$id'");
        $this->db->query("exec ACC_PharmacyDepoJournal '" . $TransactionID . "' , '" . 0 . "' , '" . 0 . "', '" . 0 . "' ");
        redirect($_SERVER['HTTP_REFERER']);
    }

    public function editPharmacy($caseID)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['pasienDetail'] = $this->db->query(
            "SELECT * FROM Cases WHERE [Case ID] = '$caseID'"
        )->row_array();
        $data['apotikDetail'] = $this->db->query("SELECT * FROM ApotikHeader WHERE [Case ID] = '$caseID'")->result_array();
        $data['obat'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['caraPakai'] = $this->M_rekamMedis->caraPakai()->result_array();
        $data['dosis'] = $this->db->query("SELECT * FROM Resep_AturanPakai")->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/viewpharmacy/editPharmacy', $data);
        $this->load->view('properti/footer', $data);
    }

    public function prosesUpdatePharmacy($id)
    {
        $data['user'] = $UserCreated = $this->M_user->getUserSession()->row_array();
        $caseID = $this->input->post('caseID');
        $TransactionID = $this->input->post('TransactionID');
        $DrugsId = $this->input->post('DrugsId');
        $Qty = $this->input->post('Qty');
        $dosis = $this->input->post('dosis');
        $jumlahSatuanPakai = $this->input->post('jumlahSatuanPakai');
        $caraPakai = $this->input->post('caraPakai');
        $ket = $this->input->post('ket');

        $getCase = $this->db->query("SELECT LastOP,Category FROM Cases WHERE [Case ID] = '$caseID'")->row_array();
        $aPharmacyCode = $DrugsId;
        $aCase = $caseID;
        $Poli = $getCase['LastOP'];
        $aQuery = $this->db->query("exec Sp_DrugsPriceWeb  '" . $aPharmacyCode . "'   ")->row_array();
        $qnetprice = $this->db->query("SELECT [NettPrice] FROM [Pharmacy] WHERE [PharmacyCode] = '$aPharmacyCode' ")->row_array();
        $utama = $this->db->query("Select * From Utama")->row_array();
        // RUMUS
        $a = $aQuery['DrugsPrice'] * $Qty;
        if ($getCase['Category'] == 'OP') {
            $b = ($utama['PPN'] / 100) * $a;
        } else {
            $b = 0;
        }
        $c = $qnetprice['NettPrice'];
        $d = $aQuery['DrugsPrice'];
        $squery = $this->db->query("Select * From Pharmacy where PharmacyCode = '$aPharmacyCode' ")->row_array();
        $Unit = $squery['PharmacyUnit'];

        $Charge = number_format($a);
        $BasicPrice = number_format($c);
        $UnitPrice = number_format($d);
        $PPN = number_format($b);

        $this->db->query("UPDATE [Activity Drugs] 
            SET
            DrugsID = '$DrugsId',
            Charge = '$Charge',
            Qty = '$Qty',
            BasicPrice = '$BasicPrice',
            UnitPrice = '$UnitPrice',
            Dosis = '$dosis',
            Cara = '$caraPakai',
            Ket = '$ket',
            PPN = '$PPN' WHERE ID = '$id'
        ");

        // ADD ACTIVITY JURNAL
        $UserID = $this->input->post('IdUser');
        $CompID = $this->input->post('IdComp');

        $cases = $this->db->query("Select CompanyCode from Cases where [Case ID] = '$caseID' ")->row_array();
        if ($cases['CompanyCode'] == '102') {
            $this->db->query("exec ACC_PharmacyDepoJournalBPJS'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        } else {
            $this->db->query("exec ACC_PharmacyDepoJournal'" . $TransactionID . "' , '" . $Poli . "' , '" . $UserID . "', '" . $CompID . "' ");
        }

        redirect('apotik/addPharmacy/' . $caseID . '?isi=' . $TransactionID);
    }

    public function prosesHeader($id)
    {
        date_default_timezone_set("Asia/Jakarta");
        $caseID = $this->input->post('caseID');
        $PatientCode = $this->input->post('PatientCode');
        $DoctorID = $this->input->post('DoctorID');
        $Class = $this->input->post('Class');
        $Room = $this->input->post('Room');
        $user = $this->input->post('user');

        $Transaction_Date = date('Y-m-d');
        $Transaction_Time = date('Y-m-d H:i:s');


        $no = $this->db->query("SELECT * FROM SystemNo WHERE SystemName = 'Trans'")->row_array();
        $noAwal = $no['NoNumeric'] + 1;
        $this->db->query("UPDATE SystemNo SET NoNumeric = '$noAwal' WHERE SystemName = 'Trans'");
        $nomor = '3' . $no['NoNumeric'];

        $this->db->query("
                INSERT INTO ApotikHeader (
                    TransactionID,
                    [Case ID],
                    PatientCode,
                    Transaction_Date,
                    Transaction_Time,
                    PresNo,
                    DoctorID,
                    Class,
                    UserCreated,
                    DateCreated,
                    Type,
                    LastUserModified,
                    LastDateModified,
                    LockStatus
                    ) 
                VALUES (
                    '$nomor',
                    '$caseID',
                    '$PatientCode',
                    '$Transaction_Date',
                    '$Transaction_Time',
                    '$nomor',
                    '$DoctorID',
                    '$Class',
                    '$user',
                    '$Transaction_Time',
                    'SALE',
                    '$user',
                    '$Transaction_Time',
                    '0'
                )
            ");


        $qdata = $this->db->query("select TOP 1  a.TransactionID from ApotikHeader a order by a.TransactionID desc")->row_array();
        $transactionID = $qdata['TransactionID'];

        redirect('apotik/addPharmacy/' . $caseID . '?isi=' . $transactionID);
        //$this->session->set_flashdata("success", "Berhasil ditambah!");
        //redirect('apotik/transaksiPharmacy/' . $caseID);
    }

    public function hapusTransaksi($id, $cases)
    {
        $this->db->query("DELETE FROM ApotikHeader WHERE TransactionID = '$id'");
        $this->db->query("DELETE FROM [Activity Drugs] WHERE TransactionID = '$id'");
        $this->session->set_flashdata("success", "Berhasil dihapus!");
        redirect("apotik/transaksiPharmacy/" . $cases);
    }

    public function ApotikAddTransaksiObat($id)
    {
        extract($_GET);
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;
        $data['user'] = $this->M_user->getUserSession()->row_array();
        // $data['rDetail'] = $this->db->query("SELECT * FROM Resep_Detail RD, Pharmacy P WHERE RD.PharmacyCode=P.PharmacyCode AND ResepId = '$id'")->result_array();
        // $data['tampilracik'] = $this->M_rekamMedis->tampilracik2()->result_array();
        $data['pharmacy'] = $this->M_rekamMedis->pharmacy()->result_array();
        $data['obatmodal'] = $this->M_rekamMedis->obatmodal()->result_array();
        $data['caraPakai'] = $this->db->query("SELECT * FROM Resep_CaraPakai ORDER BY CaraPakai ASC")->result_array();
        $data['aturan'] = $this->db->query("SELECT * FROM Resep_AturanPakai ORDER BY AturanPakai ASC")->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('apotek/AddTransaksiObat', $data);
        $this->load->view('properti/footer', $data);
    }
}
