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

require_once(str_replace("\\","/",APPPATH).'helpers/Secure_area_adm.php');
class Send_request_note extends Secure_area_adm {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('M_general');


		$data['jenis_page'] = "front_office/";
		$data['controller_dir'] = 'request_note/';
		$data['controller'] = 'rekam_medis';
		$data['parent_controller'] = 'rekam_medis';

        $this->load->vars($data);
	}
	
	public function index()
	{
		$data['active']='pengajuan';
		$data['judul']='Pengeluaran Bahan Habis Pakai';
		$data['title'] = 'Amprahan';
		$data['desc_judul']='';
		$data['page'] = 'request_note/send_req_index';
		$data['tgl'] = isset($_POST['tgl']) ? $_POST['tgl'] : date('Y-m-d');
		$data['breadcumb'][]=array('icon'=>'bookmark',
        									'link'=>'',
        									'judul'=>'Amprahan');
	
		$data['dyn_form'] = TRUE;
		$this->load->view('template',$data);
	}


	public function input()
	{
		$data['active']='pengajuan';
		$data['judul']='Kirim Pengajuan Amprahan';
		$data['title'] = 'Pengajuan Amprahan';
		$data['desc_judul']='';
		$data['page'] = 'request_note/send_req_new';
		$data['tgl'] = isset($_POST['tgl']) ? $_POST['tgl'] : date('Y-m-d');
		$data['breadcumb'][]=array('icon'=>'send',
        									'link'=>'',
        									'judul'=>'Transaksi');
		
		$data['dyn_form'] = TRUE;
		$this->load->view('template',$data);
	}

	public function edit($id)
	{
		$data['active']='pengajuan';
		$data['judul']='Kirim Pengajuan Anggaran';
		$data['title'] = 'Pengajuan Anggaran';
		$data['desc_judul']='Pengajuan biaya dari sub unit';
		$data['page'] = 'request_note/send_req_edit';
		$data['tgl'] = isset($_POST['tgl']) ? $_POST['tgl'] : date('Y-m-d');
		$data['breadcumb'][]=array('icon'=>'send',
        									'link'=>'',
        									'judul'=>'Transaksi');
		
		$data['get'] = @$this->M_general->get_row_custom('bbmc_tr_amprahan_request_new','id_amprahan_request_new',$id);
		$data['id'] = $id;
		$data['dyn_form'] = TRUE;
		$this->load->view('template',$data);
	}


	/*
	public function save()
	{
		extract($_POST);
		
		
		$data=array(
		  'tanggal'=>$tgl,
		  'nama'=>$nama_anggaran,
		  'biaya'=>str_replace(",", "", $biaya),
		  'deskripsi'=>$deskripsi_anggaran,
		  'added_by'=>$this->session->userdata('userid'),
		  'date_added'=>date('Y-m-d h:i:s'),
		  'id_sinkron'=>'0'
		);
		
		if($this->M_general->insert_db('siak_tr_request_note', $data)==true)
		{
			$this->M_general->msg_box('success','New supplier added');			
		}
		else {
			$this->M_general->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi admin...');
		}
		
		redirect('send_request_note');
	}
	*/

	/*
	public function save()
	{
		extract($_POST);
		
		$data=array(
		  'tanggal'=>$tanggal,
		  'id_unit'=>$id_unit,
		  'added_by'=>$this->session->userdata('userid'),
		  'date_added'=>date('Y-m-d h:i:s'),
		  'id_sinkron'=>'0'
		);
		
		if($this->M_general->insert_db('siak_tr_request_note', $data)==true)
		{
			$last_id = $this->db->insert_id();

			$count = count($kebutuhan);
			for ($i=0; $i < $count; $i++) { 
				if($kebutuhan[$i] != ''){
					$data=array(
					  'id_request_note'=>$last_id,
					  'kebutuhan'=>$kebutuhan[$i],
					  'qty'=>$qty[$i],
					  'satuan'=>$satuan[$i],
					  'biaya'=>str_replace(",", "", $biaya[$i]),
					  'added_by'=>$this->session->userdata('userid'),
					  'date_added'=>date('Y-m-d h:i:s'),
					);
					$this->M_general->insert_db('siak_tr_request_note_detail', $data);
				}
				
			}
			


			$this->M_general->msg_box('success','Proses pengajuan sudah dibuat');			
		}
		else {
			$this->M_general->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi admin...');
		}
		
		redirect('send_request_note');
	}
	*/

	/*
	public function save()
	{
		extract($_POST);

		$this->db->trans_start();

		$data = array(
			'tanggal'=>$tanggal,
			'added_by'=>$this->session->userdata('userid')
		);
		$this->db->insert('bbmc_tr_amprahan_request_new',$data);
		$id_amprahan_request_new = $this->db->insert_id();

		$arr = array();

		foreach ($id_bhp as $key =>$value) {
			if($value != ''){

				$data2 = array(
					'id_amprahan_request_new'=>$id_amprahan_request_new,
					'id_re_bahan_habis_pakai'=>$value,
					'qty'=>$qty[$key]
				);
				array_push($arr, $data2);
			}
		}

		if(@$arr){
			$this->db->insert_batch('bbmc_tr_amprahan_request_new_detail',$arr);
		}



		
		if($this->db->trans_complete()){ 
			$this->M_general->msg_box('success','<B>Delete Entry Success</B>'); 
		}else{
			$this->M_general->msg_box('error','<B>Delete Entry Failed, Please Contact IT Support</B>'); 
		}

		
		//redirect($_SERVER['HTTP_REFERER'],'refresh');	
		redirect('request_note');
	}
	*/

	public function save()
	{
		extract($_POST);

		$this->db->trans_start();

		
		$arr = array();

		foreach ($id_bhp as $key =>$value) {
			if($value != ''){

				$data = array(
					'tanggal'=>$tanggal,
					'id_unit'=>$id_unit,
					'added_by'=>$this->session->userdata('userid')
				);
				$this->db->insert('bbmc_tr_amprahan_request_new',$data);
				$id_amprahan_request_new = $this->db->insert_id();


				$data2 = array(
					'id_amprahan_request_new'=>$id_amprahan_request_new,
					'id_re_bahan_habis_pakai'=>$value,
					'qty'=>$qty[$key]
				);
				array_push($arr, $data2);
			}
		}

		if(@$arr){
			$this->db->insert_batch('bbmc_tr_amprahan_request_new_detail',$arr);
		}



		
		if($this->db->trans_complete()){ 
			$this->M_general->msg_box('success','<B>Delete Entry Success</B>'); 
		}else{
			$this->M_general->msg_box('error','<B>Delete Entry Failed, Please Contact IT Support</B>'); 
		}

		
		//redirect($_SERVER['HTTP_REFERER'],'refresh');	
		redirect('request_note');
	}

	public function update()
	{
		extract($_POST);

		$this->db->trans_start();
		$cek_approve = $this->M_general->get_value('bbmc_tr_amprahan_request_new','id_amprahan_request_new',$id_amprahan_request_new,'approve');
		if($cek_approve == '0'){
			$data = array(
				'tanggal'=>$tanggal,
				'added_by'=>$this->session->userdata('userid')
			);
			$this->db->where('id_amprahan_request_new',$id_amprahan_request_new);
			$this->db->update('bbmc_tr_amprahan_request_new',$data);
			
			$arr = array();
			$this->db->where('id_amprahan_request_new',$id_amprahan_request_new);
			$this->db->delete('bbmc_tr_amprahan_request_new_detail');

			foreach ($id_bhp as $key =>$value) {
				if($value != ''){
					$data2 = array(
						'id_amprahan_request_new'=>$id_amprahan_request_new,
						'id_re_bahan_habis_pakai'=>$value,
						'qty'=>$qty[$key]
					);
					array_push($arr, $data2);
				}
			}
			if(@$arr){
				$this->db->insert_batch('bbmc_tr_amprahan_request_new_detail',$arr);
			}
		}
		
		if($this->db->trans_complete()){ 
			$this->M_general->msg_box('success','<B>Delete Entry Success</B>'); 
		}else{
			$this->M_general->msg_box('error','<B>Delete Entry Failed, Please Contact IT Support</B>'); 
		}
		
		//redirect($_SERVER['HTTP_REFERER'],'refresh');	
		redirect('request_note');
	}

	/*
	public function update()
	{
		extract($_POST);
		
		
		$data=array(
		  'tanggal'=>$tgl,
		  'nama'=>$nama_anggaran,
		  'biaya'=>str_replace(",", "", $biaya),
		  'status'=>'4',
		  'deskripsi'=>$deskripsi_anggaran,
		  'modified_by'=>$this->session->userdata('userid'),
		  'date_modified'=>date('Y-m-d h:i:s'),
		  'id_sinkron'=>'0'
		);
		
		if($this->M_general->update_db_custom('siak_tr_request_note', 'id_request_note',$id_request_note, $data)==true)
		{
			$this->M_general->msg_box('success','New supplier added');			
		}
		else {
			$this->M_general->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi admin...');
		}
		
		redirect('send_request_note');
	}

	*/


	public function delete($id)
	{
		$data=array(
		  	'deleted'=>'1'
		);
		
		if($this->M_general->update_db_custom('bbmc_tr_amprahan_request_new_detail', 'id_amprahan_request_new_detail',$id, $data)==true)
		{
			$this->M_general->msg_box('success','Data sudah di hapus');			
		}
		else {
			$this->M_general->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi admin...');
		}

		redirect($_SERVER['HTTP_REFERER'],'refresh');	
	}

	public function delete_request($id)
	{
		$data=array(
		  	'deleted'=>'1'
		);
		
		if($this->M_general->update_db_custom('bbmc_tr_amprahan_request_new', 'id_amprahan_request_new',$id, $data)==true)
		{
			$this->M_general->msg_box('success','Data sudah di hapus');			
		}
		else {
			$this->M_general->msg_box('error','Ooops... Terjadi Kesalahan... Silahkan hubungi admin...');
		}

		redirect($_SERVER['HTTP_REFERER'],'refresh');	
	}

	public function approve()
	{
		$data  = array(
			'validate_by' => $this->session->userdata('userid'),
			'date_validate'=> date('Y-m-d h:i:s'),
			'status'=> '3',  
			'modified_by'=>$this->session->userdata('userid'),
			'date_modified'=>date('Y-m-d h:i:s'),
			'id_sinkron'=>'0'
		);

		$get_verifikator_bawahan = array();

		$this->db->where('id_sdm',$this->session->userdata('userid'));
		foreach ($this->db->get('siak_tr_validator')->result() as $key ) {
			array_push($get_verifikator_bawahan, $key->id_sdm_verifikator);
		}
		


        $this->db->where_in('verified_by',$get_verifikator_bawahan);
		$this->M_general->update_db_custom('siak_tr_purchase_item','status','2', $data);

		$this->db->where_in('verified_by',$get_verifikator_bawahan);
		$this->M_general->update_db_custom('siak_tr_receipt_item','status','2', $data);

		$this->db->where_in('verified_by',$get_verifikator_bawahan);
		$this->M_general->update_db_custom('siak_tr_inv_item','status','2', $data);
		
		$this->db->where_in('verified_by',$get_verifikator_bawahan);
		$this->M_general->update_db_custom('siak_tr_journal_entry_item','status','2', $data);
		
		redirect('validasi');

	}


	
}
