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

require_once(str_replace("\\","/",APPPATH).'helpers/Secure_area_adm.php');
class Kunjungan extends Secure_area_adm {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url')); 
		$this->load->model('M_apotek');
		$data['jenis_page'] 			 = "front_office/";
		$data['controller_dir'] 	 = 'reg_dir/';
		$data['controller'] 			 = 'kunjungan';
		$data['parent_controller'] = 'kunjungan';
		$data['active'] 					 = 'kunjungan';
		//$data['smt_aktif'] 				 = $this->db->get_where('pddikti_re_semester',array('a_smt_aktif'=>'1'))->row();
		ini_set('max_execution_time', 0);
		ini_set('memory_limit', '-1');		
		$this->load->vars($data);
		$this->load->helper('currency_format_helper');		
	}

	public function index()
	{
		//echo $this->session->userdata('id_cabang');
		//echo $this->session->userdata('userid');
		$data['judul'] ='Kunjungan Pasien';
		$data['page']  ='registrasi/kunjungan/master';
		$data['desc_judul'] ='Menampilkan dan Mengelola Data Kunjungan Pasien';

		$data['breadcumb'][] = array('icon'=>'paste',
																 'link'=>site_url('front_office/reg_dir/registrasi'),
																 'judul'=>'Registrasi');

		$data['breadcumb'][] = array('icon'=>'sign-in',
																 'link'=>'',
																 'judul'=>'Kunjungan');

		//echo $this->session->userdata('userid');
		$this->load->view('template',$data);
	}

	public function kasir()
	{
		//echo $this->session->userdata('id_cabang');
		//echo $this->session->userdata('userid');
		$data['judul'] ='Kunjungan Pasien';
		$data['page']  ='registrasi/kunjungan/master';
		$data['desc_judul'] ='Menampilkan dan Mengelola Data Kunjungan Pasien';


		$data['breadcumb'][] = array('icon'=>'money',
																 'link'=>'',
																 'judul'=>'Pembayaran');
		$data['active'] = 'kasir';
		//echo $this->session->userdata('userid');
		$this->load->view('template',$data);
	}

	public function filter($tgl)
	{
		//echo $this->session->userdata('id_cabang');
		//echo $this->session->userdata('userid');
		$data['judul'] ='Kunjungan Pasien';
		$data['page']  ='registrasi/kunjungan/master';
		$data['desc_judul'] ='Menampilkan dan Mengelola Data Kunjungan Pasien';
		$data['filter_tgl'] = $tgl;

		$data['breadcumb'][] = array('icon'=>'paste',
																 'link'=>site_url('front_office/reg_dir/registrasi'),
																 'judul'=>'Registrasi');

		$data['breadcumb'][] = array('icon'=>'sign-in',
																 'link'=>'',
																 'judul'=>'Kunjungan');

		//echo $this->session->userdata('userid');
		$this->load->view('template',$data);
	}

	public function detail_tindakan($id_re_kunjungan=null, $id_tipe_pasiens)
	{

		if($id_re_kunjungan == null){redirect('front_office/reg_dir/kunjungan/kasir');}
		$data['judul'] ='Tagihan Tindakan';
		$data['page']  ='ugd/detail_tindakan';
		$data['desc_judul'] ='Menampilkan nominal pembayaran dari setiap item tindakan';

		$id_re_pasien = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'id_re_pasien');

		$id_tipe_pasien = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$id_re_pasien,'id_type_pasien');

		$data['id_re_kunjungan'] = $id_re_kunjungan;
		$data['id_tipe_pasien'] = $id_tipe_pasien;

		//$q = $this->Content_m->get_account_tindakan($id_re_kunjungan);
		//echo $this->db->last_query();

		
		
		
		$data['query_tindakan'] = $this->Content_m->get_tindakan_pasien_new($id_re_kunjungan);
		$data['query_laboratorium'] = $this->Content_m->get_tindakan_laboratorium_new($id_re_kunjungan);
		$data['query_radiologi'] = $this->Content_m->get_tindakan_radiologi_new($id_re_kunjungan);

		
		$data['breadcumb'][] = array('icon'=>'money',
																 'link'=>site_url('front_office/reg_dir/kunjungan/kasir'),
																 'judul'=>'Pembayaran');

		$data['breadcumb'][] = array('icon'=>'paste',
																 'link'=>'',
																 'judul'=>'Detail Tindakan');

		$data['active'] = 'kasir';
		//echo $this->session->userdata('userid');
		$this->load->view('template',$data);
	}

	public function print_asuransi($id_re_kunjungan=null, $id_tipe_pasien=null, $pasien=null)
	{
		/*
		$data['id_re_kunjungan'] = $id_re_kunjungan;
		$data['id_tipe_pasien'] = $id_tipe_pasien;
		$this->load->view('front_office/ugd/print_asuransi',$data);
		*/

		//$id_receipt = $this->Content_m->get_value('siak_tr_receipt','id_kunjungan',$id_re_kunjungan,'id');
		$id_invoice = $this->Content_m->get_value('siak_tr_invoice','id_kunjungan',$id_re_kunjungan,'id');
		if($id_invoice == ''){
			echo "<center>Data Kunjungan #$id_re_kunjungan tidak ditemukan, silahkan hubungi tim IT</center>";
		}else{

			$get = str_replace("http://", "", $_SERVER['HTTP_REFERER']);
			$get2 = explode("/", $get);
			if($get2[0] == 'localhost'){
				$path = 'localhost';
			}else{
				$path = '192.168.0.100';
			}

			redirect('http://'.$path.'/bbmc_keuangan/index.php/invoice/view/'.$id_invoice.'/1'.'/'.$pasien);
		}
	}

	public function data_table($dari=null)
	{


		$draw   = $this->input->post('draw', true);
		$start  = $this->input->post('start', true);
		$length = $this->input->post('length', true);
		
		$this->db->start_cache();
		$this->db->from('bbmc_tr_kunjungan kun');
		$this->db->join('bbmc_tr_pasien pasien', 'pasien.id_re_pasien = kun.id_re_pasien', 'left');
		$this->db->where('pasien.deleted','0');
		$this->db->where('kun.deleted','0');
		//$this->db->order_by('tgl_kunjungan','desc');
		if($dari == 'kasir'){
			$this->db->order_by('sudah_dibayar','asc');
			$this->db->order_by('tgl_kunjungan','desc');
		}else{
			$this->db->order_by('tgl_kunjungan','desc');
		}
		
		

		$this->db->stop_cache();
		$this->db->select('kun.id_re_kunjungan');
		$recordsTotal = @$this->db->count_all_results();
		$recordsFiltered = $recordsTotal;

		if($this->input->get('filter') == 'true')
		{
			parse_str($this->input->post('form_filter'), $form_filter);
			$this->db->select('kun.id_re_kunjungan');
			$this->db->start_cache();
			foreach ($form_filter as $kolom => $value) {
				if($value) 
				{
					
				
						if($kolom == 'no_kunjungan') {
							$this->db->where_in("kun.id_kunjungan", $value);
						} elseif ($kolom == 'no_rekam_medis') {
							$this->db->where_in("pasien.nrm", $value);
						} elseif ($kolom == 'type_perawatan') {
							$this->db->where_in("kun.type_perawatan", $value);
						} elseif ($kolom == 'tgl_kunjungan') {
							$this->db->where('kun.tgl_kunjungan >=',$value." 00:00:00"); 
						} elseif ($kolom == 'sampai_tgl') {
							$this->db->where('kun.tgl_kunjungan <=',$value." 23:59:59"); 
						} elseif($kolom == 'id_tipe_pasien'){
							$this->db->where('pasien.id_type_pasien',$value); 
						} else if($kolom == 'nama_pasien') {
							$this->db->like($kolom, $value);
						} 
					
				}
			}
				$this->db->stop_cache();
				$recordsFiltered = $this->db->count_all_results();
		}
		$order = $this->input->post('order', TRUE);
		if (isset($order)) 
		{
			foreach ($this->input->post('order', TRUE) as $key => $value) {
				$orderBy = $_POST['columns'][$value['column']]['data'];
				$orderType = $value['dir'];
				$this->db->order_by($orderBy, $orderType);
			}
		}
		$this->db->select('kun.id_re_kunjungan as id_re_kunjungan, pasien.tgl_registrasi, kun.id_kunjungan as no_kunjungan, kun.tgl_kunjungan as tgl_kunjungan, pasien.nama_pasien, pasien.nrm, kun.rujuk_dari, kun.no_surat, kun.type_perawatan, kun.status_perawatan, kun.pemeriksaan_khusus, kun.keluhan, 
			kun.nama_penanggung, kun.alamat_penanggung, kun.tlp_penanggung, kun.hub_dgn_pasien, 
			kun.batal_periksa, kun.id_alasan_keluar, pasien.id_type_pasien, kun.id_sub_spesialis, kun.sudah_dibayar, kun.status_resep, kun.status_perawatan, kun.is_free');
		if($length != -1){
			$this->db->limit($length, $start);
		}
		
		$data = $this->db->get()->result_array();
		//$asd = '<br>'.$this->db->last_query();
		$this->db->flush_cache();
		$i=$start+1; foreach ($data as $key => $row) {
			
			if($row['batal_periksa'] == '1'){
				$batal_periksa = '<br><span class="label label-sm label-danger" style="font-size:9px">Dibatalkan</span>';
			}else{
				$batal_periksa = '';
			}


			$data[$key]['i'] = $i;

			//$data[$key]['no_kunjungan'] = $row['no_kunjungan'].$batal_periksa;
			$data[$key]['no_kunjungan'] = "BBMC-".$row['nrm'].$batal_periksa;

			$data[$key]['tgl_kunjungan'] = $row['tgl_kunjungan'];
			$data[$key]['tgl_registrasi'] = $row['tgl_registrasi'];

			$data[$key]['nama_pasien'] = $row['nama_pasien'];

			if($row['rujuk_dari'] == '1') {
					$data[$key]['rujukan_dari'] = "Datang Sendiri";
			} elseif ($row['rujuk_dari'] == '2') {                                    
					$data[$key]['rujukan_dari'] = "Rujukan Dokter / Rumah Sakit";
			} elseif ($row['rujuk_dari'] == '3') {
					$data[$key]['rujukan_dari'] = "Dari Ambulance";
			}else{
				$data[$key]['rujukan_dari'] = "";
			}

			$data[$key]['no_surat'] = $row['no_surat'];			

			if($row['type_perawatan'] == '1') {
					$data[$key]['type_perawatan'] = "Rawat Jalan";
			} elseif($row['type_perawatan'] == '2') {                                    
					$data[$key]['type_perawatan'] = "Rawat Inap";
			}else{
				$data[$key]['type_perawatan'] = "";
			}


			if($row['id_alasan_keluar'] == '3'){
				$alasan_keluar = "<br><span class='label label-sm label-warning'>".$this->Content_m->get_value('bbmc_re_alasan_keluar','id_alasan_keluar',$row['id_alasan_keluar'],'alasan_keluar')."</span>";
			}else{
				$alasan_keluar = "";
			}


			$tipe_pasien = $this->Content_m->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$row['id_type_pasien'],'tipe_pasien');
			
			if($row['status_perawatan'] == '1') {
				$data[$key]['status_perawatan'] = "Poliklinik Umum ".$alasan_keluar;
			}elseif($row['status_perawatan'] == '2') {                                    
				$data[$key]['status_perawatan'] = "Poliklinik Gigi ".$alasan_keluar;
			}elseif($row['status_perawatan'] == '3') {       
				$sub_spesialis = ", ".$this->Content_m->get_value('bbmc_re_sub_spesialis','id_sub_spesialis',$row['id_sub_spesialis'], 'sub_spesialis');                             
				$data[$key]['status_perawatan'] = "Poliklinik Spesialis".$sub_spesialis." ".$alasan_keluar;
			}elseif($row['status_perawatan'] == '4') {       
				$data[$key]['status_perawatan'] = "Laboratorium ".$alasan_keluar;
			}elseif($row['status_perawatan'] == '5') {       
				$data[$key]['status_perawatan'] = "Radiologi ".$alasan_keluar;
			}else{
				$data[$key]['status_perawatan'] = '';
			}				

			$data[$key]['status_perawatan'] = $data[$key]['status_perawatan'];
			$data[$key]['id_type_pasien'] = $tipe_pasien;
			if($row['pemeriksaan_khusus'] == 'cek_laboratorium') {
					$data[$key]['pemeriksaan_khusus'] = "Cek Laboratorium";
			} elseif($row['pemeriksaan_khusus'] == 'cek_radiologi') {                                    
					$data[$key]['pemeriksaan_khusus'] = "Cek Radiologi";
			} elseif($row['pemeriksaan_khusus'] == 'cek_laboratorium,cek_radiologi'){
				$data[$key]['pemeriksaan_khusus'] = "Cek Laboratorium, Cek Radiologi";
			}else{
				$data[$key]['pemeriksaan_khusus'] = "";
			}

			$data[$key]['keluhan'] = $row['keluhan'];

			$data[$key]['nama_penanggung'] = $row['nama_penanggung'];

			$data[$key]['alamat_penanggung'] = $row['alamat_penanggung'];

			$data[$key]['tlp_penanggung'] = $row['tlp_penanggung'];

			if($row['hub_dgn_pasien'] == '1') {
					$data[$key]['hub_dgn_pasien'] = "Ayah Kandung";
			} elseif($row['hub_dgn_pasien'] == '2') {                                    
					$data[$key]['hub_dgn_pasien'] = "Ibu Kandung ";
			} elseif($row['hub_dgn_pasien'] == '3') {                                    
					$data[$key]['hub_dgn_pasien'] = "Anak Kandung";
			} elseif($row['hub_dgn_pasien'] == '4') {                                    
					$data[$key]['hub_dgn_pasien'] = "Saudara Kandung ";
			} elseif($row['hub_dgn_pasien'] == '5') {                                    
					$data[$key]['hub_dgn_pasien'] = "Saudara";
			} elseif($row['hub_dgn_pasien'] == '6') {                                    
					$data[$key]['hub_dgn_pasien'] = "Teman";
			} elseif($row['hub_dgn_pasien'] == '7') {                                    
					$data[$key]['hub_dgn_pasien'] = "Tetangga";
			}              
			

			if($dari == 'kasir')
			{

				
				$tagihan = $this->Content_m->get_tagihan_pasien($row['id_re_kunjungan'], $row['id_type_pasien']);
				if($tagihan['cek'] == '0')
				{
					$edit = '<a onclick="return alert(\'Belum ada tagihan\')"  data-toggle="tooltip" class="btn-xs btn-info" style="background:red" data-placement="top" title="Belun ada tagihan" href="javascript:;" ><i class="fa fa-info-circle"></i></a>';   
					$piutang = "<a style='target='_Blank' href=".site_url('front_office/reg_dir/kunjungan/detail_tindakan/'.$row['id_re_kunjungan']).'/'.$row['id_type_pasien'].">Belum ada tagihan</a>";
				}
				else
				{
					$silahkan_bayar = '1';
					$cek_resep = $this->M_apotek->cek_resep($row['id_re_kunjungan']);
					if($cek_resep > 0){
						$ttl_obat = $this->M_apotek->get_list_obat_pasien($row['id_re_kunjungan'], $row['id_type_pasien'], 'ttl', 'value');
			            $jasa_racikan = 0;
			            $jasa_obat = 0;
			            //if($this->M_apotek->cek_biaya_racikan($row['id_re_kunjungan'])['jumlah_racikan'] >= '1'){
			            	if($row['id_type_pasien'] == '1'){
			            		$get_resep = $this->M_apotek->cek_biaya_racikan($row['id_re_kunjungan']);
		                     	$cek_jumlah_racikan = $get_resep['jumlah_racikan'];
		                      	$cek_total_obat = $get_resep['total_obat'];
		                      	$biaya_racikan_obat = $this->Content_m->get_value('siak_re_data','id','17','value');
		                      	$biaya_satuan_obat = $this->Content_m->get_value('siak_re_data','id','18','value'); 
		                      	$jasa_racikan = $cek_jumlah_racikan*$biaya_racikan_obat;
		                      	$jumlah_obat_selain_racikan = $cek_total_obat - $cek_jumlah_racikan;
		                      	if($jumlah_obat_selain_racikan>3){$jumlah_obat_selain_racikan=3;}
		                      	$jasa_obat = $jumlah_obat_selain_racikan*$biaya_satuan_obat;

			            		$ttl_obat = $ttl_obat + ($jasa_racikan+$jasa_obat);

			            		//$grand = $cek_jumlah_racikan;
			            	}
			            //}
			            $tagihan['grand_ttl'] = $tagihan['grand_ttl'] + $ttl_obat;

			            if($row['status_resep'] != '3'){
			            	$silahkan_bayar = '0';
			            }
					}
					

					

					$grand = $tagihan['grand_ttl'];

					if($row['status_perawatan'] == '4'){
						$silahkan_bayar = '1';
					}

					if($row['sudah_dibayar'] == '1'){
						$baa = 'Bayar';
						$edit = '<a onclick="return alert(\'Sudah dibayar\')"  data-toggle="tooltip" class="btn-xs btn-info" style="background:silver" data-placement="top" title="Bayar Kunjungan" href="javascript:;" ><i class="fa fa-check"></i></a>';   
						//$piutang = "<span style='color:green; font-weight:bold'> LUNAS</span> <a style='color:blue; text-decoration:line-through' target='_Blank' href=".site_url('front_office/reg_dir/kunjungan/detail_tindakan/'.$row['id_re_kunjungan']).'/'.$row['id_type_pasien'].">".$this->Content_m->currency($tagihan['grand_ttl'])."</a>";
						$piutang = "<span style='color:green; font-weight:bold'> LUNAS</span>";
					}else if($silahkan_bayar == '0'){
						$edit = '<a onclick="return alert(\'Resep belum selesai di apotek\')"  data-toggle="tooltip" class="btn-xs btn-info" style="background:silver" data-placement="top" title="Bayar Kunjungan" href="javascript:;" ><i class="fa fa-medkit"></i></a>';   
						$piutang = "<span style='color:green; font-weight:bold'></span> <a style='color:blue; text-decoration:line-through' target='_Blank' href=".site_url('front_office/reg_dir/kunjungan/detail_tindakan/'.$row['id_re_kunjungan']).'/'.$row['id_type_pasien'].">".$this->Content_m->currency($tagihan['grand_ttl'])."</a>";
					
					}else if($silahkan_bayar == '1'){



						$text= 'Bayar?';

						$edit = '<a onclick="return confirm(\''.$text.'\')"  data-toggle="tooltip" class="btn-xs btn-info" style="background:green" data-placement="top" title="Bayar Kunjungan" href="'.site_url('front_office/reg_dir/kunjungan/receive_money_new/'.$row['id_re_kunjungan']).'"><i class="fa fa-check"></i></a>';  
						$piutang = "<a style='color:blue; text-decoration:underline' target='_Blank' href=".site_url('front_office/reg_dir/kunjungan/detail_tindakan/'.$row['id_re_kunjungan']).'/'.$row['id_type_pasien'].">".$this->Content_m->currency($grand)."</a>"; 
					}
				}


				

				//$tagihan = 0;
				if($row['is_free'] == '2'){
					$data[$key]['rujukan_dari'] = "<span style='color:purple'><b>FREE PEGAWAI</b></span>";
				}else{
					$data[$key]['rujukan_dari'] = $piutang;
				}
				
			}else
			{
				$edit = '<a data-toggle="tooltip" class="btn-xs btn-info" data-placement="top" title="Ubah Data Kunjungan" href="'.site_url('front_office/reg_dir/kunjungan/edit_kunjungan/'.$row['id_re_kunjungan']).'"><i class="fa fa-edit"></i></a>';    
			}

			                 
			$print_asuransi ="";
			$print_asuransi2 ="";
			$delete ="";

			if($row['batal_periksa'] == '1'){
				$delete = '<a data-toggle="tooltip" class="btn-xs btn-danger data-placement="top" title="Batal Berkunjungan" onclick="viewHapusData(\''.$row['id_re_kunjungan'].'\',\''.$row['tgl_kunjungan'].' | '.$row['nama_pasien'].'\')"> <i class="fa fa-times"></i></a>';
			}

			if($row['sudah_dibayar'] == '1'){
				//$print_asuransi = '<a data-toggle="tooltip" target="_Blank" style="background:orange; color:#fff" class="btn-xs data-placement="top" title="Print Internal" href="'.site_url('front_office/reg_dir/kunjungan/print_asuransi/'.$row['id_re_kunjungan'].'/'.$row['id_type_pasien']).'"> <i class="fa fa-print"></i></a>';
				$print_asuransi2 = $print_asuransi.' <a data-toggle="tooltip" target="_Blank" style="background:green; color:#fff" class="btn-xs data-placement="top" title="Print Pasien" href="'.site_url('front_office/reg_dir/kunjungan/print_asuransi/'.$row['id_re_kunjungan'].'/'.$row['id_type_pasien']).'/pasien"> <i class="fa fa-print"></i></a>';
			}

			$data[$key]['aksi'] = $edit.' '.$print_asuransi2.' '.$delete;			

			$i++;
		}

		$response = array(
			"draw" => intval($draw),
			"recordsTotal" => $recordsTotal,
			"recordsFiltered" => $recordsFiltered,
			"data" => $data
		);
		echo json_encode($response);
	}

	public function tambah_kunjungan($id_re_pasien=null) 
	{
		$data['judul'] = 'Tambah Data Kunjungan';
		$data['desc_judul'] = 'Menambahkan Data Kunjungan Pasien';
		$data['page'] = 'registrasi/kunjungan/add';
		$data['id_re_pasien'] = $id_re_pasien;

		$data['breadcumb'][] = array('icon'=>'paste',
									 'link'=>site_url('front_office/reg_dir/registrasi'),
									 'judul'=>'Registrasi');

		$data['breadcumb'][] = array('icon'=>'sign-in',
									 'link'=>site_url('front_office/reg_dir/kunjungan'),
									 'judul'=>'Kunjungan');

		$data['breadcumb'][] = array('icon'=>'plus',
									   'link'=>'',
									   'judul'=>$data['judul']);	

		$this->load->view('template',$data);
	}

	public function tambah_data_proses_standard()
	{
		extract($_POST);
		//echo @$is_free?$is_free:'0';
		if($this->validasi())
		{
			$this->db->trans_start();

			$p='';
			if(isset($pemeriksaan_khusus)){
				if(count($pemeriksaan_khusus) == '2'){
					$p = $pemeriksaan_khusus[0].",".$pemeriksaan_khusus[1];
				}else{
					$p = $pemeriksaan_khusus[0];
				}
			}
			// $id_re_kunjungan = $this->db->query("select UUID() as id")->row()->id;
			date_default_timezone_set('Asia/Makassar');
			$data_kunjungan = array(
				'id_re_pasien' => $id_re_pasien,
				'id_kunjungan' => $no_kunjungan,
				'tgl_kunjungan' => $tgl_kunjungan." ".date('H:i:s'),
				'rujuk_dari' => $rujuk_dari,
				'nama_dokter_atau_rumah_sakit_rujukan'=>$nama_dokter_atau_rumah_sakit_rujukan,
				'no_surat' => $surat_no,
				'type_perawatan' => $type_rawat,
				'id_sub_spesialis'=>$id_sub_spesialis,
				'id_tipe_kamar'=>$id_tipe_kamar,
				'status_perawatan' => $status_rawat,
				'pemeriksaan_khusus' => $p,
				'keluhan' => $keluhan,
				'nama_penanggung' => $nama_penanggung,
				'alamat_penanggung' => $alamat_penanggung,
				'tlp_penanggung' => $tlp_penanggung,
				'hub_dgn_pasien' => $hub_dgn_pasien,
				'is_free'=>@$is_free?$is_free:'0',
				'date_added' => date("Y-m-d H:i:s"),
				'added_by' => $this->session->userdata('userid'),
				'deleted' => 0
			);
			$this->db->insert('bbmc_tr_kunjungan', $data_kunjungan);
			//echo $this->db->;
			$id_re_kunjungan = $this->db->insert_id();

			if($status_rawat == '1'){
				foreach ($tindakan_umum as $key ) {
					if($key != ''){
						$data_tindakan = array(
							'id_re_kunjungan' => $id_re_kunjungan,
							'tindakan'=>$key,
							'added_by'=>$this->session->userdata('userid'),
						);
						$this->db->insert('bbmc_tr_tindakan', $data_tindakan);
					}
					
				}
			}else{
				foreach ($tindakan_lainnya as $key ) {
					if($key != ''){
						$data_tindakan = array(
							'id_re_kunjungan' => $id_re_kunjungan,
							'tindakan'=>$key,
							'added_by'=>$this->session->userdata('userid'),
						);
						$this->db->insert('bbmc_tr_tindakan', $data_tindakan);
					}
					
				}
			}
			


			
			if($this->db->trans_complete()){ 
				echo json_encode(array("status"=>"1", "title"=>"Sukses!", "message"=>"Data Berhasil Ditambahkan"));
			}else{
				echo json_encode(array("status"=>"3","title"=>"Terjadi Kesalahan!","message"=>"Data Gagal Ditambah"));
			}
			
		} 
	}

	public function edit_kunjungan($id)
	{
		$data['re_kunjungan'] = $this->db->get_where('bbmc_tr_kunjungan', array('id_re_kunjungan'=>$id))->row();

		$data['get_kunjungan'] = $data['re_kunjungan'];
		if($data['re_kunjungan'])
		{			
			$data['judul'] = 'Edit Data Kunjungan';
			$data['desc_judul'] = 'Mengubah Data Kunjungan Pasien';
			$data['page'] = 'registrasi/kunjungan/edit';
			$data['edit'] = true;
			$data['breadcumb'][]=array('icon'=>'paste',
																'link'=>site_url('front_office/reg_dir/registrasi'),
																'judul'=>'Registrasi');

			$data['breadcumb'][] = array('icon'=>'sign-in',
																	'link'=>site_url('front_office/reg_dir/kunjungan'),
																	'judul'=>'Kunjungan');

			$data['breadcumb'][]=array('icon'=>'edit',
																'link'=>'',
																'judul'=>$data['judul']);			
																
			$data['re_pasien'] = $this->db->get_where('bbmc_tr_pasien', array('id_re_pasien' => $data['re_kunjungan']->id_re_pasien))->row();
			$data['id_kunjungan'] = $id;
			$data['id_re_kunjungan'] = $id;
			$this->load->view('template',$data);	
		} else
		show_404();						
	}

	public function edit_data_proses()
	{
		extract($_POST);
		if($this->validasi())
		{
			$this->db->trans_start();

			// $id_re_kunjungan = $this->db->query("select UUID() as id")->row()->id;
			date_default_timezone_set('Asia/Makassar');		
			if(!@$id_tipe_kamar){
				$id_tipe_kamar="";
			}
			if(!@$id_tipe_kamar){
				$id_tipe_kamar="";
			}

			

			$data_kunjungan = array(
				'id_kunjungan' => $no_kunjungan,
				'tgl_kunjungan' => $tgl_kunjungan,
				'rujuk_dari' => $rujuk_dari,
				'nama_dokter_atau_rumah_sakit_rujukan'=>$nama_dokter_atau_rumah_sakit_rujukan,
				'no_surat' => $surat_no,
				'type_perawatan' => $type_rawat,
				'id_sub_spesialis' =>$id_sub_spesialis,
				'id_tipe_kamar'=> $id_tipe_kamar,
				'status_perawatan' => $status_rawat,
				'pemeriksaan_khusus' => $pemeriksaan_khusus,
				'keluhan' => $keluhan,
				'nama_penanggung' => $nama_penanggung,
				'alamat_penanggung' => $alamat_penanggung,
				'tlp_penanggung' => $tlp_penanggung,
				'hub_dgn_pasien' => $hub_dgn_pasien,
				'date_modified' => date("Y-m-d H:i:s"),
				'modified_by' => $this->session->userdata('userid'),
				'deleted' => 0
			);

			$this->db->update('bbmc_tr_kunjungan', $data_kunjungan, array("id_re_kunjungan" => $id_re_kunjungan));
			

			//$data_update['deleted'] = '1';
			//$this->db->where('id_re_kunjungan',$id_re_kunjungan);
			//$this->db->update('bbmc_tr_tindakan',$data_update);

			foreach ($tindakan as $key ) {
				if($key != ''){
					$data_tindakan = array(
						'id_re_kunjungan' => $id_re_kunjungan,
						'tindakan'=>$key,
						'added_by'=>$this->session->userdata('userid'),
					);
					$this->db->insert('bbmc_tr_tindakan', $data_tindakan);
				}
			}


			$aa='Data Berhasil Diperbarui';
			//echo count($id_obat)."<br>";
			$i=1;
			$a=0;

			if($status_resep == '0'){
				$data_status_resep['status_resep'] = '1';
				$this->db->where('id_re_kunjungan', $id_re_kunjungan);
				$this->db->update('bbmc_tr_kunjungan',$data_status_resep);
			}

			$this->db->where('id_re_kunjungan',$id_re_kunjungan);
			$this->db->delete('bbmc_tr_pengobatan');

			$this->db->where('id_re_kunjungan',$id_re_kunjungan);
			$this->db->delete('bbmc_tr_pengobatan_pasien');

			$e1 = 1;
			
			
			foreach ($id_obat as $key ) {
				$cek=0;
				//echo $i.". ";
				$e2 = 0;
				foreach ($key as $keyp ) {
					if($keyp != ''){
						if($cek == 0){
							
							//$data_pengobatan['id_re_kunjungan'] = $id_re_kunjungan;
							//$this->db->insert('bbmc_tr_pengobatan',$data_pengobatan);

							$data_pengobatan['id_re_kunjungan'] = $id_re_kunjungan;
							$data_pengobatan['added_by'] = $this->session->userdata('userid');
							$data_pengobatan['date_added'] = date('Y-m-d h:i:s');
							$this->db->insert('bbmc_tr_pengobatan',$data_pengobatan);
							$id_pengobatan = $this->db->insert_id();
							$cek=1;
						}
						//echo $keyp.",";

						
						//echo "insert pengobatan pasien";
						$data_pengobatan_pasien['id_pengobatan'] = $id_pengobatan;
						$data_pengobatan_pasien['id_re_kunjungan'] = $id_re_kunjungan;
						$data_pengobatan_pasien['id_obat'] = $keyp;
						$data_pengobatan_pasien['qty'] = $qty[$e1][$e2];
						$data_pengobatan_pasien['cara_pemakaian'] = $cara_pemakaian[$e1][$e2];
						$this->db->insert('bbmc_tr_pengobatan_pasien',$data_pengobatan_pasien);
						
					}
					$e2++;
				}
				//echo "<br>";
				$i++;
				$a++;
				$e1++;
			}
			


			if($this->db->trans_complete()){ 
				//echo $this->db->last_query();
				echo json_encode(array("status"=>"1", "title"=>"Sukses!", "message"=>"Data Berhasil Diubah"));
			}else{
				echo json_encode(array("status"=>"3","title"=>"Terjadi Kesalahan!","message"=>"Data Gagal Diubah"));
			}
		}
	}


	public function hapus_data_proses()
	{
		$id_re_kunjungan = $this->input->post('id');

		date_default_timezone_set('Asia/Makassar');
		if($this->db->update('bbmc_tr_kunjungan', array("deleted"=>"1", "date_modified"=>date("Y-m-d H:i:s"), "modified_by"=>$this->session->userdata('userid')), array("id_re_kunjungan"=>$id_re_kunjungan)))
		{
			//$this->log_book->hapus(array("nm_tabel"=>"pddikti_tr_mahasiswa", "id_tabel"=>$id_pd, "tanggal"=>date("Y-m-d H:i:s")));
			echo json_encode(array("status"=>"1","title"=>"Sukses!","message"=>"Data Berhasil Dihapus"));
		}
		else
		{
			echo json_encode(array("status"=>"3","title"=>"Terjadi Kesalahan!","message"=>"Data Gagal Dihapus"));
		}
	}	


	public function per_pasien()
	{
		//echo $this->session->userdata('id_cabang');
		//echo $this->session->userdata('userid');
		$data['judul'] ='Kunjungan Pasien ';
		$data['page']  ='registrasi/kunjungan/per_pasien';
		$data['desc_judul'] ='Menampilkan Data Kunjungan Pasien dengan Nama <strong> I Ketut Kardika </strong>';

		$data['breadcumb'][] = array('icon'=>'user',
																 'link'=>site_url('front_office/pasien_dir/pasien'),
																 'judul'=>'Pasien');

		$data['breadcumb'][] = array('icon'=>'sign-in',
																 'link'=>'',
																 'judul'=>'Kunjungan');

		//echo $this->session->userdata('userid');
		$this->load->view('template',$data);
	}	

	public function validasi()
	{
		// Form Tambah 
		$this->form_validation->set_rules('id_re_pasien', 'No Rekam Medis / Nama Pasien', 'trim|required');
		$this->form_validation->set_rules('rujuk_dari', 'Rujukan Dari', 'trim|required');
		//$this->form_validation->set_rules('surat_no', 'Nomor Surat', 'trim|required');
		$this->form_validation->set_rules('type_rawat', 'Type Perawatan', 'trim|required');
		$this->form_validation->set_rules('status_rawat', 'Status Perawatan', 'trim|required');
		//$this->form_validation->set_rules('keluhan', 'Keluhan', 'trim|required');

		$this->form_validation->set_message('required', '{field} harus diisi!');
		$this->form_validation->set_message('numeric', '{field} harus berupa angka!');
		$this->form_validation->set_message('max_length', '{field} tidak boleh lebih dari {param} karakter!');
		$this->form_validation->set_message('min_length', '{field} tidak boleh kurang dari {param} karakter!');
		$this->form_validation->set_message('valid_email', '{field} tidak valid!');

		$valid = $this->form_validation->run();
		if($valid)
		{
			return $valid;
		}
		else
		{
			echo json_encode(array("status"=>"2", "title"=>"Error!", "message"=>"Terjadi kesalahan, silahkan periksa kembali data yang diinputkan", "detail_error"=>$this->form_validation->field_data()));
		}
	}		


	public function receive_money_new($id_re_kunjungan=null)
	{
		if($id_re_kunjungan == null){redirect('front_office/reg_dir/kunjungan/kasir');}
		$get_id_cust = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'id_re_pasien');
		if($get_id_cust == ''){redirect('front_office/reg_dir/kunjungan/kasir');}
		
		$cek_bayar = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'sudah_dibayar');
		$is_free = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'is_free');
		
		if($cek_bayar == '1'){
			redirect('front_office/reg_dir/kunjungan/kasir');
		}


		$get_tgl = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'tgl_kunjungan');
		$get_addr = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'alamat');
		$get_nama_pasien = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'nama_pasien');
		$get_nrm = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'nrm');
		$get_id_type_pasien = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'id_type_pasien');
		$nama_asuransi = $this->Content_m->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$get_id_type_pasien,'tipe_pasien');
      	$kode_coa_asuransi = $this->Content_m->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$get_id_type_pasien,'kode_akun_coa');
      	$biaya_racikan_obat = $this->Content_m->get_value('siak_re_data','id','17','value');
		$total_price = 0;
		//make new invoice 
		//siak_tr_invoice
		$data_insert['id'] = $this->Content_m->get_ai('siak_tr_invoice');
		$data_insert['tgl'] = date('Y-m-d h:i:s');
		$data_insert['no_inv'] = 'INV'.sprintf("%03d",$data_insert['id']).'/A/BBMC/'.$this->Content_m->roman(date("n")).'/'.date("Y");
		$data_insert['id_cust'] = $get_id_cust;
		$data_insert['addr'] = $get_addr;
		$data_insert['desc'] = "Kunjungan #$id_re_kunjungan - $nama_asuransi";
		$data_insert['id_kunjungan'] = $id_re_kunjungan;
		$data_insert['untuk'] = '1';
		$data_insert['note'] = 'Transaksi ini otomatis dibuat oleh sistem';
		$data_insert['added_by'] = $this->session->userdata('userid');
		$data_insert['date_added'] = date('Y-m-d h:i:s');
		$data_insert['modified_by'] = $this->session->userdata('userid');
		$data_insert['date_modified'] = date('Y-m-d h:i:s');
		$data_insert['id_thn_ajaran'] = date('Y');
		$data_insert['id_cabang'] = '1';
		//siak_tr_inv_item
		$this->db->trans_start();
      	if($this->Content_m->insert_db('siak_tr_invoice', $data_insert)==true)
      	{ 
      		$this->Content_m->insert_db('siak_tr_insurance_invoice', $data_insert);
      		

      		//get biaya normal dikurangi tanggungan asuransi yang nilainya != 0 itu artinya harus bayar di item itu
      		//buatkan invoice pada semua biaya tersebut yang ada biayanya, yang gak ada biaya atau ditanggung asuransi buatkan tetep subnya tapi nilainya 0
      		//receipt entry
      		$array_item = array();
      		$array_item_insurance = array();

      		$q = $this->Content_m->get_tindakan_pasien_new($id_re_kunjungan);
      		foreach ($q->result() as $key ) {
      			$cek_harga = 0;
      			if($get_id_type_pasien == 1){
              		$cek_harga = $key->price_umum;
              		$harga_insurance = $key->price_umum;
              	}
              	else if($get_id_type_pasien == 2){
              		$cek_harga = $key->price_umum-$key->price_bpjs;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_bpjs;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 3){
              		$cek_harga = $key->price_umum-$key->price_shhc;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_shhc;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 4){
              		$cek_harga = $key->price_umum-$key->price_inhealth;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_inhealth;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 5){
              		$cek_harga = $key->price_umum-$key->price_mega;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_mega;
              		//$harga_insurance = 0;
              	}

              	if($is_free == '2'){
              		$cek_harga = 0;
              		$harga_insurance = 0;
              	}
              	//invoice pasien
      			if($cek_harga > 0){
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = $cek_harga;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_pengobatan_pasien'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + $cek_harga;
		      		array_push($array_item , $data_insert_detail);
      			}else{
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = '0';
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_pengobatan_pasien'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		array_push($array_item , $data_insert_detail);
      			}

      			//invoice asuransi
      			$data_insert_detail_insurance['id_inv'] = $data_insert['id'];
	      		$data_insert_detail_insurance['acc'] = $key->id;
	      		$data_insert_detail_insurance['desc'] = $key->name;
	      		$data_insert_detail_insurance['qty'] = '1';
	      		$data_insert_detail_insurance['price'] = $harga_insurance;
	      		$data_insert_detail_insurance['disc'] = '0';
	      		$data_insert_detail_insurance['tax'] = '0';
	      		$data_insert_detail_insurance['id_bln'] = '0';
	      		$data_insert_detail_insurance['id_obat'] = '0';
	      		$data_insert_detail_insurance['id_pengobatan_pasien'] = '0';
	      		$data_insert_detail_insurance['id_asuransi'] = $get_id_type_pasien;
	      		$data_insert_detail_insurance['added_by'] = $data_insert['added_by'];
	      		$data_insert_detail_insurance['modified_by'] = $data_insert['modified_by'];
	      		array_push($array_item_insurance , $data_insert_detail_insurance);

      		}


      		$q = $this->Content_m->get_tindakan_laboratorium_new($id_re_kunjungan);
      		foreach ($q->result() as $key ) {
      			$cek_harga = 0;
      			if($get_id_type_pasien == 1){
              		$cek_harga = $key->price_umum;
              		$harga_insurance = $key->price_umum;
              	}
              	else if($get_id_type_pasien == 2){
              		$cek_harga = $key->price_umum-$key->price_bpjs;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_bpjs;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 3){
              		$cek_harga = $key->price_umum-$key->price_shhc;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_shhc;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 4){
              		$cek_harga = $key->price_umum-$key->price_inhealth;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_inhealth;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 5){
              		$cek_harga = $key->price_umum-$key->price_mega;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_mega;
              		//$harga_insurance = 0;
              	}
              	if($is_free == '2'){
              		$cek_harga = 0;
              		$harga_insurance = 0;
              	}


              	//invoice pasien
      			if($cek_harga > 0){
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = $cek_harga;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_pengobatan_pasien'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + $cek_harga;
		      		array_push($array_item , $data_insert_detail);
      			}else{
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = '0';
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_pengobatan_pasien'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		array_push($array_item , $data_insert_detail);
      			}

      			//invoice asuransi
      			$data_insert_detail_insurance['id_inv'] = $data_insert['id'];
	      		$data_insert_detail_insurance['acc'] = $key->id;
	      		$data_insert_detail_insurance['desc'] = $key->name;
	      		$data_insert_detail_insurance['qty'] = '1';
	      		$data_insert_detail_insurance['price'] = $harga_insurance;
	      		$data_insert_detail_insurance['disc'] = '0';
	      		$data_insert_detail_insurance['tax'] = '0';
	      		$data_insert_detail_insurance['id_bln'] = '0';
	      		$data_insert_detail_insurance['id_obat'] = '0';
	      		$data_insert_detail_insurance['id_pengobatan_pasien'] = '0';
	      		$data_insert_detail_insurance['id_asuransi'] = $get_id_type_pasien;
	      		$data_insert_detail_insurance['added_by'] = $data_insert['added_by'];
	      		$data_insert_detail_insurance['modified_by'] = $data_insert['modified_by'];
	      		array_push($array_item_insurance , $data_insert_detail_insurance);

      		}

      		$q = $this->Content_m->get_tindakan_radiologi_new($id_re_kunjungan);
      		foreach ($q->result() as $key ) {
      			$cek_harga = 0;
      			if($get_id_type_pasien == 1){
              		$cek_harga = $key->price_umum;
              		$harga_insurance = $key->price_umum;
              	}
              	else if($get_id_type_pasien == 2){
              		$cek_harga = $key->price_umum-$key->price_bpjs;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_bpjs;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 3){
              		$cek_harga = $key->price_umum-$key->price_shhc;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_shhc;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 4){
              		$cek_harga = $key->price_umum-$key->price_inhealth;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_inhealth;
              		//$harga_insurance = 0;
              	}
              	else if($get_id_type_pasien == 5){
              		$cek_harga = $key->price_umum-$key->price_mega;
              		//$cek_harga = 0;
              		$harga_insurance = $key->price_mega;
              		//$harga_insurance = 0;
              	}

              	if($is_free == '2'){
              		$cek_harga = 0;
              		$harga_insurance = 0;
              	}

              	//invoice pasien
      			if($cek_harga > 0){
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = $cek_harga;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_pengobatan_pasien'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + $cek_harga;
		      		array_push($array_item , $data_insert_detail);
      			}else{
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = '0';
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_pengobatan_pasien'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		array_push($array_item , $data_insert_detail);
      			}

      			//invoice asuransi
      			$data_insert_detail_insurance['id_inv'] = $data_insert['id'];
	      		$data_insert_detail_insurance['acc'] = $key->id;
	      		$data_insert_detail_insurance['desc'] = $key->name;
	      		$data_insert_detail_insurance['qty'] = '1';
	      		$data_insert_detail_insurance['price'] = $harga_insurance;
	      		$data_insert_detail_insurance['disc'] = '0';
	      		$data_insert_detail_insurance['tax'] = '0';
	      		$data_insert_detail_insurance['id_bln'] = '0';
	      		$data_insert_detail_insurance['id_obat'] = '0';
	      		$data_insert_detail_insurance['id_pengobatan_pasien'] = '0';
	      		$data_insert_detail_insurance['id_asuransi'] = $get_id_type_pasien;
	      		$data_insert_detail_insurance['added_by'] = $data_insert['added_by'];
	      		$data_insert_detail_insurance['modified_by'] = $data_insert['modified_by'];
	      		array_push($array_item_insurance , $data_insert_detail_insurance);

      		}

      		//get resep obat pasien
      		$q_pengobatan_pasien = $this->M_apotek->get_pengobatan_pasien($id_re_kunjungan);
      		foreach ($q_pengobatan_pasien->result() as $key ) {
              	//echo $key->id_obat." ".$key->qty." ".$key->harga_jual."<br>";
      			$cek_harga = 0;
      			if($get_id_type_pasien == 1){
              		$cek_harga = $key->harga_jual;
              		$harga_insurance = $key->harga_jual;
              	}
              	else if($get_id_type_pasien == 2){
              		//$cek_harga = $key->harga_jual-$key->harga_bpjs;
              		$cek_harga = 0;
              		//$harga_insurance = $key->harga_bpjs;
              		$harga_insurance = $key->harga_jual;
              	}
              	else if($get_id_type_pasien == 3){
              		//$cek_harga = $key->harga_jual-$key->harga_shhc;
              		$cek_harga = 0;
              		//$harga_insurance = $key->harga_shhc;
              		$harga_insurance = $key->harga_jual;
              	}
              	else if($get_id_type_pasien == 4){
              		//$cek_harga = $key->harga_jual-$key->harga_inhealth;
              		$cek_harga = 0;
              		//$harga_insurance = $key->harga_inhealth;
              		$harga_insurance = $key->harga_jual;
              	}
              	else if($get_id_type_pasien == 5){
              		//$cek_harga = $key->harga_jual-$key->harga_mega;
              		$cek_harga = 0;
              		//$harga_insurance = $key->harga_mega;
              		$harga_insurance = $key->harga_jual;
              	}
              	if($is_free == '2'){
              		$cek_harga = 0;
              		$harga_insurance = 0;
              	}




              	//invoice pasien
              	$nama_obat = $this->Content_m->get_value('bbmc_re_apotik_obat','id_re_apotik_obat',$key->id_obat,'nama_obat');
      			if($cek_harga > 0){
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = '5502';
		      		$data_insert_detail['desc'] = $nama_obat;
		      		$data_insert_detail['qty'] = $key->qty;
		      		$data_insert_detail['price'] = $cek_harga;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = $key->id_obat;
		      		$data_insert_detail['id_pengobatan_pasien'] = $key->id_pengobatan;
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + ($cek_harga*$key->qty);
		      		array_push($array_item , $data_insert_detail);
      			}else{
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = '5502';
		      		$data_insert_detail['desc'] = $nama_obat;
		      		$data_insert_detail['qty'] = $key->qty;
		      		$data_insert_detail['price'] = '0';
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = $key->id_obat;
		      		$data_insert_detail['id_pengobatan_pasien'] = $key->id_pengobatan;
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		array_push($array_item , $data_insert_detail);
      			}

      			
      			//invoice asuransi
      			$data_insert_detail_insurance['id_inv'] = $data_insert['id'];
	      		$data_insert_detail_insurance['acc'] = '5502';
	      		$data_insert_detail_insurance['desc'] = '';
	      		$data_insert_detail_insurance['qty'] = $key->qty;
	      		$data_insert_detail_insurance['price'] = $harga_insurance;
	      		$data_insert_detail_insurance['disc'] = '0';
	      		$data_insert_detail_insurance['tax'] = '0';
	      		$data_insert_detail_insurance['id_bln'] = '0';
	      		$data_insert_detail_insurance['id_obat'] = $key->id_obat;
	      		$data_insert_detail_insurance['id_pengobatan_pasien'] = $key->id_pengobatan;
	      		$data_insert_detail_insurance['id_asuransi'] = $get_id_type_pasien;
	      		$data_insert_detail_insurance['added_by'] = $data_insert['added_by'];
	      		$data_insert_detail_insurance['modified_by'] = $data_insert['modified_by'];
	      		array_push($array_item_insurance , $data_insert_detail_insurance);


	      		//$get_qty_obat_sebelumnya = $this->M_general->get_value('bbmc_re_apotik_obat','id_re_apotuk_obat',$key->id_obat,'stok_display');
	      		$query  = "UPDATE bbmc_re_apotik_obat SET stok_display = stok_display - $key->qty where id_re_apotik_obat = $key->id_obat";
	      		$this->db->query($query);
            }

            //get biaya racikan pasien
            
            $q_racikan_obat = $this->M_apotek->cek_biaya_racikan($id_re_kunjungan);
            if($q_racikan_obat['jumlah_racikan'] >= '1' || $q_racikan_obat['total_obat'] >= '1'){

            	$get_resep = $q_racikan_obat;
             	$cek_jumlah_racikan = $get_resep['jumlah_racikan'];
              	$cek_total_obat = $get_resep['total_obat'];
              	$biaya_racikan_obat = $this->Content_m->get_value('siak_re_data','id','17','value');
              	$biaya_satuan_obat = $this->Content_m->get_value('siak_re_data','id','18','value'); 
              	$jasa_racikan = $cek_jumlah_racikan*$biaya_racikan_obat;
              	$jumlah_obat_selain_racikan = $cek_total_obat - $cek_jumlah_racikan;
              	if($jumlah_obat_selain_racikan>3){$jumlah_obat_selain_racikan=3;}
              	$jasa_obat = $jumlah_obat_selain_racikan*$biaya_satuan_obat;

              	if($get_id_type_pasien == '1'){
              		$total_biaya_jasa = $jasa_racikan+$jasa_obat;
              	}else{
              		$total_biaya_jasa = 0;
              	}

              	if($is_free == '2'){
              		$total_biaya_jasa = 0;
              		$jasa_racikan = 0;
              		$jasa_obat = 0;
              		$total_price = 0;
              	}

              	//invoice
            	$data_insert_detail['id_inv'] = $data_insert['id'];
	      		$data_insert_detail['acc'] = '5569';
	      		$data_insert_detail['desc'] = "Racikan Kunjungan #$id_re_kunjungan - $nama_asuransi";
	      		$data_insert_detail['qty'] = '1';
	      		$data_insert_detail['price'] = $total_biaya_jasa;
	      		$data_insert_detail['disc'] = '0';
	      		$data_insert_detail['tax'] = '0';
	      		$data_insert_detail['id_bln'] = '0';
	      		$data_insert_detail['id_obat'] = '0';
	      		$data_insert_detail['id_pengobatan_pasien'] = '0';
	      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
	      		$data_insert_detail['added_by'] = $data_insert['added_by'];
	      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
	      		$total_price = $total_price + $total_biaya_jasa;
	      		array_push($array_item , $data_insert_detail);

	      		//invoice asuransi
	      		$data_insert_detail_insurance['id_inv'] = $data_insert['id'];
	      		$data_insert_detail_insurance['acc'] = '5569';
	      		$data_insert_detail_insurance['desc'] = "Racikan Kunjungan #$id_re_kunjungan - $nama_asuransi";
	      		$data_insert_detail_insurance['qty'] = '1';
	      		$data_insert_detail_insurance['price'] = $jasa_racikan+$jasa_obat;
	      		$data_insert_detail_insurance['disc'] = '0';
	      		$data_insert_detail_insurance['tax'] = '0';
	      		$data_insert_detail_insurance['id_bln'] = '0';
	      		$data_insert_detail_insurance['id_obat'] = '0';
	      		$data_insert_detail_insurance['id_pengobatan_pasien'] = '0';
	      		$data_insert_detail_insurance['id_asuransi'] = $get_id_type_pasien;
	      		$data_insert_detail_insurance['added_by'] = $data_insert['added_by'];
	      		$data_insert_detail_insurance['modified_by'] = $data_insert['modified_by'];
	      		array_push($array_item_insurance , $data_insert_detail_insurance);
            }



      		$this->db->insert_batch('siak_tr_inv_item',$array_item);
      		$this->db->insert_batch('siak_tr_insurance_inv_item',$array_item_insurance);


      		//make new receipt entry
      		$data_insert_receipt['id'] = $this->Content_m->get_ai('siak_tr_receipt');
      		$data_insert_receipt['tgl'] = $data_insert['tgl'];
      		$data_insert_receipt['ref'] = 'RM'.sprintf("%03d",$data_insert_receipt['id']).'/A/BBMC/'.$this->Content_m->roman(date("n")).'/'.date("Y");
      		$data_insert_receipt['acc_id'] = '1';
      		$data_insert_receipt['payer'] = "BBMC-$get_nrm - $get_nama_pasien";
      		$data_insert_receipt['desc'] = "Kunjungan #$id_re_kunjungan - Pelunasan Tagihan Biaya Perawatan";
      		$data_insert_receipt['receive'] = '1'; 
      		$data_insert_receipt['id_kunjungan'] = $id_re_kunjungan;
      		$data_insert_receipt['notes'] = 'Transaksi ini otomatis dibuat oleh sistem';
      		$data_insert_receipt['added_by'] = $data_insert['added_by'];
      		$data_insert_receipt['date_added'] = $data_insert['date_added'];
      		$data_insert_receipt['modified_by'] = $data_insert['modified_by'];
      		$data_insert_receipt['date_modified'] = $data_insert['date_modified'];
      		$data_insert_receipt['id_thn_ajaran'] = $data_insert['id_thn_ajaran'];
      		$data_insert_receipt['id_cabang'] = $data_insert['id_cabang'];
      		$data_insert_receipt['untuk_pembayaran'] = '0';  
      		if($this->Content_m->insert_db('siak_tr_receipt', $data_insert_receipt)==true)
      		{
      			$array_receipt_item = array();
      			$data_insert_receipt_item['id_receipt'] = $data_insert_receipt['id'];
      			$data_insert_receipt_item['acc'] = '22';
      			$data_insert_receipt_item['desc'] = "Kunjungan #$id_re_kunjungan Pelunasan Tagihan $nama_asuransi";
      			$data_insert_receipt_item['qty'] = '1';
      			$data_insert_receipt_item['price'] = $total_price;
      			$data_insert_receipt_item['tax'] = '0';
      			$data_insert_receipt_item['disc'] = '0';
      			$data_insert_receipt_item['id_cust'] = $data_insert['id_cust'];
      			$data_insert_receipt_item['id_inv'] = $data_insert['id'];
      			$data_insert_receipt_item['id_purchase'] = '0';
      			$data_insert_receipt_item['id_asuransi'] = $get_id_type_pasien;
      			$data_insert_receipt_item['dari'] = 'receive money';
      			$data_insert_receipt_item['added_by'] = $data_insert['added_by'];
      			$data_insert_receipt_item['date_added'] = $data_insert['date_added'];
      			$data_insert_receipt_item['modified_by'] = $data_insert['modified_by'];
      			$data_insert_receipt_item['date_modified'] = $data_insert['date_modified'];
      			array_push($array_receipt_item , $data_insert_receipt_item);
		      	$this->db->insert_batch('siak_tr_receipt_item',$array_receipt_item);
      		} 

      		
      			//$this->db->trans_complete();
	  		if($this->db->trans_complete()){ 
	  			$this->Content_m->msg_box('success','data sudah dibayar'); 
	  			$data_update['sudah_dibayar'] = '1';
	  			$data_update['modified_by'] = $this->session->userdata('userid');
	  			$data_update['date_modified'] = date('Y-m-d h:i:s');
	  			$this->db->where('id_re_kunjungan',$id_re_kunjungan);
	  			$this->db->update('bbmc_tr_kunjungan',$data_update);
	  		}
			else { 
				$this->Content_m->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi Admin IT...'); 
			}
			if(isset($_SERVER['HTTP_REFERER'])) {
				redirect($_SERVER['HTTP_REFERER'],'refresh');
			}

      	}

      

	}

	public function receive_money($id_re_kunjungan=null)
	{

		if($id_re_kunjungan == null){redirect('front_office/reg_dir/kunjungan/kasir');}
		//echo "id_kunjungan: $id_re_kunjungan<br>";
		//echo "item kunjungan: <br>";
		$get_id_cust = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'id_re_pasien');
		if($get_id_cust == ''){redirect('front_office/reg_dir/kunjungan/kasir');}
		$get_tgl = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'tgl_kunjungan');
		$get_addr = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'alamat');
		$get_nama_pasien = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'nama_pasien');
		$get_nrm = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'nrm');
		$get_id_type_pasien = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$get_id_cust,'id_type_pasien');
		$nama_asuransi = $this->Content_m->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$get_id_type_pasien,'tipe_pasien');
      	$kode_coa_asuransi = $this->Content_m->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$get_id_type_pasien,'kode_akun_coa');
      	$biaya_racikan_obat = $this->Content_m->get_value('siak_re_data','id','17','value');
		$total_price = 0;
		//make new invoice 
		//siak_tr_invoice
		$data_insert['id'] = $this->Content_m->get_ai('siak_tr_invoice');
		$data_insert['tgl'] = date('Y-m-d h:i:s');
		$data_insert['no_inv'] = 'INV'.sprintf("%03d",$data_insert['id']).'/A/BBMC/'.$this->Content_m->roman(date("n")).'/'.date("Y");
		$data_insert['id_cust'] = $get_id_cust;
		$data_insert['addr'] = $get_addr;
		$data_insert['desc'] = "Kunjungan #$id_re_kunjungan - $nama_asuransi";
		
		$data_insert['untuk'] = '1';
		$data_insert['note'] = 'Transaksi ini otomatis dibuat oleh sistem';
		$data_insert['added_by'] = $this->session->userdata('userid');
		$data_insert['date_added'] = date('Y-m-d h:i:s');
		$data_insert['modified_by'] = $this->session->userdata('userid');
		$data_insert['date_modified'] = date('Y-m-d h:i:s');
		$data_insert['id_thn_ajaran'] = date('Y');
		$data_insert['id_cabang'] = '1';
		//siak_tr_inv_item
		$this->db->trans_start();
      	if($this->Content_m->insert_db('siak_tr_invoice', $data_insert)==true)
      	{ 
      		//insert items
      		//get semua tindakan pasien
      		$q = $this->Content_m->get_account_tindakan($id_re_kunjungan);
      		$array_item = array();
      		foreach ($q->result() as $key ) {
      			if($key->price_umum > 0){
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = $key->id;
		      		$data_insert_detail['desc'] = $key->name;
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = $key->price_umum;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + $key->price_umum;
		      		array_push($array_item , $data_insert_detail);
      			}	
      		}
      		//get resep obat pasien
      		$q_pengobatan_pasien = $this->M_apotek->get_pengobatan_pasien($id_re_kunjungan);
      		foreach ($q_pengobatan_pasien->result() as $key ) {
              	//echo $key->id_obat." ".$key->qty." ".$key->harga_jual."<br>";
      			if($key->harga_jual > 0){
      				$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = '5502';
		      		$data_insert_detail['desc'] = '';
		      		$data_insert_detail['qty'] = $key->qty;
		      		$data_insert_detail['price'] = $key->harga_jual;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = $key->id_obat;
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + ($key->harga_jual*$key->qty);
		      		array_push($array_item , $data_insert_detail);
      			
      			}
            }
            //get biaya racikan pasien
            if($get_id_type_pasien == '1')
      		{
	            $q_racikan_obat = $this->M_apotek->cek_biaya_racikan($id_re_kunjungan);
	            if($q_racikan_obat['jumlah_racikan'] >= '1' || $q_racikan_obat['total_obat'] >= '1'){

	            	$get_resep = $q_racikan_obat;
	             	$cek_jumlah_racikan = $get_resep['jumlah_racikan'];
	              	$cek_total_obat = $get_resep['total_obat'];
	              	$biaya_racikan_obat = $this->Content_m->get_value('siak_re_data','id','17','value');
	              	$biaya_satuan_obat = $this->Content_m->get_value('siak_re_data','id','18','value'); 
	              	$jasa_racikan = $cek_jumlah_racikan*$biaya_racikan_obat;
	              	$jumlah_obat_selain_racikan = $cek_total_obat - $cek_jumlah_racikan;
	              	if($jumlah_obat_selain_racikan>3){$jumlah_obat_selain_racikan=3;}
	              	$jasa_obat = $jumlah_obat_selain_racikan*$biaya_satuan_obat;

	              	$total_biaya_jasa = $jasa_racikan+$jasa_obat;

	            	$data_insert_detail['id_inv'] = $data_insert['id'];
		      		$data_insert_detail['acc'] = '5569';
		      		$data_insert_detail['desc'] = "Racikan Kunjungan #$id_re_kunjungan - $nama_asuransi";
		      		$data_insert_detail['qty'] = '1';
		      		$data_insert_detail['price'] = $total_biaya_jasa;
		      		$data_insert_detail['disc'] = '0';
		      		$data_insert_detail['tax'] = '0';
		      		$data_insert_detail['id_bln'] = '0';
		      		$data_insert_detail['id_obat'] = '0';
		      		$data_insert_detail['id_asuransi'] = $get_id_type_pasien;
		      		$data_insert_detail['added_by'] = $data_insert['added_by'];
		      		$data_insert_detail['modified_by'] = $data_insert['modified_by'];
		      		$total_price = $total_price + $total_biaya_jasa;
		      		array_push($array_item , $data_insert_detail);
	            }
        	}


      		$this->db->insert_batch('siak_tr_inv_item',$array_item);
      		


      		//make new journal entry
      		//jika asuransi bpjs
      		if($get_id_type_pasien != '1')
      		{

      			$data_insert_journal['id_journal_entry'] = $this->Content_m->get_ai('siak_tr_journal_entry');
	      		$data_insert_journal['date'] = $data_insert['tgl'];
	      		$data_insert_journal['no_inv'] = 'JU'.sprintf("%03d",$data_insert_journal['id_journal_entry']).'/A/BBMC/'.$this->Content_m->roman(date("n")).'/'.date("Y");
	      		$data_insert_journal['naration'] = "Kunjungan #$id_re_kunjungan - Jurnal Hutang $nama_asuransi - BBMC-$get_nrm - $get_nama_pasien";
	      		$data_insert_journal['added_by'] = $data_insert['added_by'];
	      		$data_insert_journal['date_added'] = $data_insert['date_added'];
	      		$data_insert_journal['modified_by'] = $data_insert['modified_by'];
	      		$data_insert_journal['date_modified'] = $data_insert['date_modified'];
	      		$data_insert_journal['id_thn_ajaran'] = $data_insert['id_thn_ajaran'];
	      		$data_insert_journal['id_cabang'] = $data_insert['id_cabang'];

	      		if($this->Content_m->insert_db('siak_tr_journal_entry', $data_insert_journal)==true)
	      		{ 
	      			$array_journal_item = array();
	      			$debit = 0;
	      			//get semua tindakan pasien
		      		foreach ($q->result() as $key ) {

		      			if($get_id_type_pasien == '2'){
		      				$price_asuransi = $key->price_bpjs;
		      			}else if($get_id_type_pasien == '3'){
		      				$price_asuransi = $key->price_shhc;
		      			}else if($get_id_type_pasien == '4'){
		      				$price_asuransi = $key->price_inhealth;
		      			}else if($get_id_type_pasien == '5'){
		      				$price_asuransi = $key->price_mega;
		      			}

		      			if($price_asuransi > 0){
		      				$data_insert_journal_detail['id_journal_entry'] = $data_insert_journal['id_journal_entry'];
		      				$data_insert_journal_detail['acc'] = '22';
		      				$data_insert_journal_detail['id_cust'] = $data_insert['id_cust'];
		      				$data_insert_journal_detail['id_inv'] = $data_insert['id'];
		      				$data_insert_journal_detail['id_asuransi'] = $get_id_type_pasien;
		      				$data_insert_journal_detail['desc'] = "$nama_asuransi - $key->name";
		      				$data_insert_journal_detail['debit'] = '0';
		      				$data_insert_journal_detail['credit'] = $price_asuransi;
		      				$debit = $debit+$price_asuransi;
				      		array_push($array_journal_item , $data_insert_journal_detail);
		      			}	
		      		}

		      		//get resep obat pasien
		      		foreach ($q_pengobatan_pasien->result() as $key ) {
		              	//echo $key->id_obat." ".$key->qty." ".$key->harga_jual."<br>";
		              	$harga_pcs_obat = $key->harga_jual;
		      			if($get_id_type_pasien == '2'){
		      				$price_asuransi_obat = $key->harga_bpjs*$key->qty;
		      				$harga_pcs_obat = $key->harga_bpjs;
		      			}else if($get_id_type_pasien == '3'){
		      				$price_asuransi_obat = $key->harga_shhc*$key->qty;
		      				$harga_pcs_obat = $key->harga_shhc;
		      			}else if($get_id_type_pasien == '4'){
		      				$price_asuransi_obat = $key->harga_inhealth*$key->qty;
		      				$harga_pcs_obat = $key->harga_inhealth;
		      			}else if($get_id_type_pasien == '5'){
		      				$price_asuransi_obat = $key->harga_mega*$key->qty;
		      				$harga_pcs_obat = $key->harga_mega;
		      			}

		      			if($price_asuransi_obat > 0){
		      				
				      		$data_insert_journal_detail['id_journal_entry'] = $data_insert_journal['id_journal_entry'];
		      				$data_insert_journal_detail['acc'] = '22';
		      				$data_insert_journal_detail['id_cust'] = $data_insert['id_cust'];
		      				$data_insert_journal_detail['id_inv'] = $data_insert['id'];
		      				$data_insert_journal_detail['id_asuransi'] = $get_id_type_pasien;
		      				$data_insert_journal_detail['desc'] = "$nama_asuransi - $key->nama_obat $key->sediaan $key->jenis_obat @".$this->Content_m->currency_edit2($harga_pcs_obat)." x ".$key->qty."pcs";
		      				$data_insert_journal_detail['debit'] = '0';
		      				$data_insert_journal_detail['credit'] = $price_asuransi_obat;
		      				$debit = $debit+$price_asuransi_obat;
				      		array_push($array_journal_item , $data_insert_journal_detail);
		      			
		      			}
		            }

		            
		            //get biaya racikan pasien
		            if($get_id_type_pasien == '1')
      				{
			            if($q_racikan_obat['jumlah_racikan'] >= '1' || $q_racikan_obat['total_obat'] >= '1'){
			            	$data_insert_journal_detail['id_journal_entry'] = $data_insert_journal['id_journal_entry'];
		      				$data_insert_journal_detail['acc'] = '22';
		      				$data_insert_journal_detail['id_cust'] = $data_insert['id_cust'];
		      				$data_insert_journal_detail['id_inv'] = $data_insert['id'];
		      				$data_insert_journal_detail['id_asuransi'] = $get_id_type_pasien;
		      				$data_insert_journal_detail['desc'] = "$nama_asuransi - Racikan Kunjungan #$id_re_kunjungan ";
		      				$data_insert_journal_detail['debit'] = '0';
		      				$data_insert_journal_detail['credit'] = $biaya_racikan_obat;
		      				$debit = $debit+$biaya_racikan_obat;
				      		array_push($array_journal_item , $data_insert_journal_detail);
			            }
		        	}
		            





		      		$data_insert_journal_detail['id_journal_entry'] = $data_insert_journal['id_journal_entry'];
	  				$data_insert_journal_detail['acc'] = $kode_coa_asuransi; 
	  				$data_insert_journal_detail['id_cust'] = '';
	  				$data_insert_journal_detail['id_inv'] = '0';
	  				$data_insert_journal_detail['id_asuransi'] = $get_id_type_pasien;
	  				$data_insert_journal_detail['desc'] = $data_insert_journal['naration'];
	  				$data_insert_journal_detail['debit'] = $debit;
	  				$data_insert_journal_detail['credit'] = '0';
		      		array_push($array_journal_item , $data_insert_journal_detail);
		      		$this->db->insert_batch('siak_tr_journal_entry_item',$array_journal_item);

	      		}

	      		$total_price = $total_price - $debit;
      		}
      		
      		
      		//make new receipt entry
      		$data_insert_receipt['id'] = $this->Content_m->get_ai('siak_tr_receipt');
      		$data_insert_receipt['tgl'] = $data_insert['tgl'];
      		$data_insert_receipt['ref'] = 'RM'.sprintf("%03d",$data_insert_receipt['id']).'/A/BBMC/'.$this->Content_m->roman(date("n")).'/'.date("Y");
      		$data_insert_receipt['acc_id'] = '1';
      		$data_insert_receipt['payer'] = "BBMC-$get_nrm - $get_nama_pasien";
      		$data_insert_receipt['desc'] = "Kunjungan #$id_re_kunjungan - Pelunasan Tagihan Biaya Perawatan";
      		$data_insert_receipt['receive'] = '1'; 
      		$data_insert_receipt['notes'] = 'Transaksi ini otomatis dibuat oleh sistem';
      		$data_insert_receipt['added_by'] = $data_insert['added_by'];
      		$data_insert_receipt['date_added'] = $data_insert['date_added'];
      		$data_insert_receipt['modified_by'] = $data_insert['modified_by'];
      		$data_insert_receipt['date_modified'] = $data_insert['date_modified'];
      		$data_insert_receipt['id_thn_ajaran'] = $data_insert['id_thn_ajaran'];
      		$data_insert_receipt['id_cabang'] = $data_insert['id_cabang'];
      		$data_insert_receipt['untuk_pembayaran'] = '0';  
      		if($this->Content_m->insert_db('siak_tr_receipt', $data_insert_receipt)==true)
      		{
      			$array_receipt_item = array();
      			$data_insert_receipt_item['id_receipt'] = $data_insert_receipt['id'];
      			$data_insert_receipt_item['acc'] = '22';
      			$data_insert_receipt_item['desc'] = "Kunjungan #$id_re_kunjungan Pelunasan Tagihan $nama_asuransi";
      			$data_insert_receipt_item['qty'] = '1';
      			$data_insert_receipt_item['price'] = $total_price;
      			$data_insert_receipt_item['tax'] = '0';
      			$data_insert_receipt_item['disc'] = '0';
      			$data_insert_receipt_item['id_cust'] = $data_insert['id_cust'];
      			$data_insert_receipt_item['id_inv'] = $data_insert['id'];
      			$data_insert_receipt_item['id_purchase'] = '0';
      			$data_insert_receipt_item['id_asuransi'] = $get_id_type_pasien;
      			$data_insert_receipt_item['dari'] = 'receive money';
      			$data_insert_receipt_item['added_by'] = $data_insert['added_by'];
      			$data_insert_receipt_item['date_added'] = $data_insert['date_added'];
      			$data_insert_receipt_item['modified_by'] = $data_insert['modified_by'];
      			$data_insert_receipt_item['date_modified'] = $data_insert['date_modified'];
      			array_push($array_receipt_item , $data_insert_receipt_item);
		      	$this->db->insert_batch('siak_tr_receipt_item',$array_receipt_item);

		      	
      		} 

      		
      		//$this->db->trans_complete();
      		if($this->db->trans_complete()){ 
      			$this->Content_m->msg_box('success','data sudah dibayar'); 
      			$data_update['sudah_dibayar'] = '1';
      			$data_update['modified_by'] = $this->session->userdata('userid');
      			$data_update['date_modified'] = date('Y-m-d h:i:s');
      			$this->db->where('id_re_kunjungan',$id_re_kunjungan);
      			$this->db->update('bbmc_tr_kunjungan',$data_update);
      		}
			else { 
				$this->Content_m->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi Admin IT...'); 
			}
			if(isset($_SERVER['HTTP_REFERER'])) {
				redirect($_SERVER['HTTP_REFERER'],'refresh');
			}
      		//set kunjungan sudah dibayar
      	}






		

		//if asuransi jurnal entry
			//$this->Siak->new_jurnal_entry($id_cust,$id_cabang,$tgl_approve,$thn_ajaran,$biaya,'Pendaftaran');

	}
  
}