<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	class Voucherctrl extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();

			$this->load->model('voucher/vouchermdl');
			$this->load->library('session');
			$this->load->library('upload');
			$this->load->library('pagination');
			$this->load->library('db_operations');
			$this->config->load('extra');
		}
// ********************* VOUCHER MASTER START ***************************************
		public function voucher_master()
		{
			// echo "<pre>"; print_r($_GET); exit;
			if(sessionExist()) 
			{
				if( $_GET['action'] == 'view' )
				{
					$config 				= array();
					$config 				= $this->config->item('pagination');
					$config['total_rows'] 	= $this->vouchermdl->get_all_voucher_data_cnt($_SESSION['fin_year']);
					$config['base_url'] 	= base_url("voucher/voucherctrl/voucher_master?search=true");

					foreach ($_GET as $key => $value) 
					{
						if($key != 'search' && $key != 'offset')
						{
							$config['base_url'] .= "&" . $key . "=" .$value;
						}
					}

					$offset = (!empty($_GET['offset'])) ? $_GET['offset'] : 0;
					$this->pagination->initialize($config);

					

					$record['count'] 		= $offset;
					$record['total_rows'] 	= $config['total_rows'];
					$record['role'] 		= $this->config->item('role');
					$record['data']			= $this->vouchermdl->get_all_voucher_data($_SESSION['fin_year'],$config['per_page'],$offset);
					// echo "<pre>"; print_r($record); exit;
					$this->load->view('pages/voucher/voucher_master',$record);				

				}
				elseif( $_GET['action'] == 'add' )
				{
					$record = $this->vouchermdl->get_voucher_data();
					// echo"<pre>"; print_r($record); exit;
					if($_GET['type'] == "PAYMENT")
					{
						$this->load->view('pages/voucher/payment_voucher_form', $record);						
					}
					elseif($_GET['type'] == "CLIENT")
					{
						$this->load->view('pages/voucher/receipt_voucher_form', $record);
					}
					else 
	                {
						$this->load->view('errors/error');
	                }
				
				}
				elseif( $_GET['action'] == 'edit')
				{
					$id 		= $_GET['id'];
	                $vc_id 		= encrypt_decrypt("decrypt", $id, SECRET_KEY);
	                // echo "<pre>"; print_r($_GET); exit();
	                if (!empty($vc_id)) 
	                {
	                	$record = $this->vouchermdl->get_single_update_voucher($vc_id);	                	
	                	// echo "<pre>"; print_r($record); exit;
	                	if($_GET['type'] == "PAYMENT")
						{
							$this->load->view('pages/voucher/payment_voucher_form', $record);						
						}
						elseif($_GET['type'] == "RECEIPT")
						{
							$this->load->view('pages/voucher/receipt_voucher_form', $record);
						}
						else 
		                {
							$this->load->view('errors/error');
		                }

	                } 
	                else 
	                {
						$this->load->view('errors/error');
	                }										
				}
				else 
                {
					$this->load->view('errors/error');
                }
			} 
			else 
			{
				redirect('loginctrl/logout');	
			}
		}
      	
      	public function get_party_list($grp_id)
      	{	
			if(sessionExist()) {				
				
				$record = $this->vouchermdl->get_party_list_data($grp_id);
				echo json_encode($record);
			
			} else {
				redirect('loginctrl/logout');
			}
		}


		public function get_client_payment_status()
      	{	

			if(sessionExist()) {				
				$acc_id = $_GET['acc_id'];
				$grp_id = $_GET['grp_id'];
				$record = $this->vouchermdl->get_party_acc_status($acc_id, $grp_id);
				// echo"<pre>";print_r($result);exit;
				echo json_encode($record);
			} else {
				redirect('loginctrl/logout');
			}
		}

		public function get_party_payment_status()
      	{	
			if(sessionExist()) {				
				$acc_id = $_GET['acc_id'];
				$grp_id = $_GET['grp_id'];
				$record = $this->vouchermdl->get_party_payment_status($acc_id, $grp_id);
				// echo"<pre>";print_r($result);exit;
				echo json_encode($record);
				
			} else {
				redirect('loginctrl/logout');
			}
		}

		public function get_party_order_details($acc_id,$grp_id)
		{
			$user_id = $this->session->userdata('user_id');
			if(!empty($user_id))
			{
				$record = $this->vouchermdl->get_party_order_details($acc_id,$grp_id);
				echo json_encode($record);
			}
			else
			{
				redirect('loginctrl/logout');
			}	
		}
		
		

		public function receipt_voucher_insert($vc_id)
		{
			if (sessionExist()) 
			{			
			
				$form_data = $this->input->post();
			
				// echo"<pre>";print_r($form_data);exit;

				$voucher_data 	= array();
				$order_data 	= array();
				$voucher_trans 	= array();

				$voucher_data['vc_entry_no']   		= $form_data['vc_entry_no'];
				$voucher_data['vc_entry_date'] 		= date('Y-m-d',strtotime($form_data['vc_entry_date']));
				$voucher_data['vc_voucher_type']    = $form_data['vc_voucher_type'];
				$voucher_data['vc_trans_type']     	= $form_data['vc_trans_type'];
				$voucher_data['vc_cash_acc_id']    	= $form_data['vc_cash_acc_id'];
				$voucher_data['vc_grp_id']         	= $form_data['vc_grp_id'];
				$voucher_data['vc_party_id']        = $form_data['vc_party_id'];
				$voucher_data['vc_amount']         	= $form_data['vc_amount'];
				$voucher_data['vc_round_off']       = $form_data['vc_round_off'];
				// $voucher_data['vc_payment_type']    = $form_data['vc_payment_type'];

				if (isset($form_data['vc_bank_name'])) 
				{
					$voucher_data['vc_bank_name']         	= $form_data['vc_bank_name'];
				}

				if (isset($form_data['vc_cheq_no'])) 
				{
					$voucher_data['vc_cheq_no']         	= $form_data['vc_cheq_no'];
				}

				if (isset($form_data['vc_cheq_date'])) 
				{
					$voucher_data['vc_cheq_date']         	= $form_data['vc_cheq_date'];
				}

				if (isset($form_data['vc_card_type'])) 
				{
					$voucher_data['vc_card_type']         	= $form_data['vc_card_type'];
				}

				if (isset($form_data['vc_card_no'])) 
				{
					$voucher_data['vc_card_no']         	= $form_data['vc_card_no'];
				}

				if (isset($form_data['vc_expiry_date'])) 
				{
					$voucher_data['vc_expiry_date']         = $form_data['vc_expiry_date'];
				}


				$voucher_data['vc_notes']         	= $form_data['vc_notes'];
				$voucher_data['vc_branch_id'] 	 	= $_SESSION['user_branch_id'];
				$voucher_data['vc_user_id'] 	 	= $_SESSION['user_id'];
				$voucher_data['vc_fin_year'] 	 	= $_SESSION['fin_year'];
				$voucher_data['vc_update_id'] 	 	= $_SESSION['user_id'];
				$voucher_data['vc_date_create'] 	= date("Y-m-d H:i:s");

				/*order voucher*/
				if(isset($form_data['om_id']))
				{
					$order_data['om_id'] 					= $form_data['om_id'];
					$order_data['om_allocated_amt'] 		= $form_data['om_allocated_amt'];
					$order_data['om_adjust_amt'] 			= $form_data['om_adjust_amt'];
					$order_data['om_voucher_round_off_amt'] = $form_data['om_voucher_round_off_amt'];
					$order_data['om_round_off'] 			= $form_data['om_round_off'];
					$order_data['om_adjust_round_off'] 		= $form_data['om_adjust_round_off'];

					$voucher_trans['vt_id']             	= $form_data['vt_id'];
					$voucher_trans['vt_om_id']             	= $form_data['om_id'];
					$voucher_trans['vt_entry_no']           = $form_data['om_entry_no'];
					$voucher_trans['vt_entry_date']         = $form_data['om_entry_date'];
					$voucher_trans['vt_final_total']       	= $form_data['om_final_amt'];
					$voucher_trans['vt_adjust_amt']        	= $form_data['om_adjust_amt'];
					$voucher_trans['vt_adjust_round_off']  	= $form_data['om_adjust_round_off'];

					$grp = 5;

				}

				/*karigar voucher */

				if(isset($form_data['hm_id']))
				{
					$order_data['hm_id'] 					= $form_data['hm_id'];
					$order_data['hm_allocated_amt'] 		= $form_data['om_allocated_amt'];
					$order_data['hm_adjust_amt'] 			= $form_data['om_adjust_amt'];
					$order_data['hm_round_off'] 			= $form_data['om_round_off'];
					$order_data['hm_adjust_round_off'] 		= $form_data['om_adjust_round_off'];

					$voucher_trans['vt_id']             	= $form_data['vt_id'];
					$voucher_trans['vt_hm_id']             	= $form_data['hm_id'];
					$voucher_trans['vt_entry_no']           = $form_data['om_no'];
					$voucher_trans['vt_entry_date']         = $form_data['om_date'];
					$voucher_trans['vt_final_total']       	= $form_data['om_final_total'];
					$voucher_trans['vt_adjust_amt']        	= $form_data['om_adjust_amt'];
					$voucher_trans['vt_adjust_round_off']  	= $form_data['om_adjust_round_off'];

					$grp = 7;
				}


				if ($vc_id == 0) 
				{
					$this->db->trans_begin();

					$cnt = $this->vouchermdl->check_duplicate_voucher_entry($form_data['vc_entry_no'],$_SESSION['fin_year']);

					if ($cnt != 0) 
					{
						echo 2;
					}
					else
					{
						// echo "<pre>";print_r($form_data);exit();
						$result = $this->db_operations->data_insert('voucher_master', $voucher_data);

						if(!empty($result))
						{
							if(!empty($voucher_trans))
							{
								$this->voucher_trans_insert_update($voucher_trans, $result);
								$this->order_update_master_data($order_data, $result, $grp);
							}

							// $mob = $form_data['vc_mob_no'];
							// $amt = $form_data['vc_amount'];
							
							// if ($grp == 5) 
							// {
							// 	$msg = "Thank you for your Payment. We acknowledge your payment of Rs. $amt against your order. We appreciate your time, Visit again.";
							// 	$this->send_sms_for_order($mob,$msg);
							// }
							// else
							// {
							// 	// $msg = "Your payment of Rs. $amt is successful.";
							// 	// $this->send_sms_for_order($mob,$msg);
							// }

							if ($this->db->trans_status() === FALSE) {
							    $this->db->trans_rollback();
							    echo 0;
							} else {
							    $this->db->trans_commit();
								echo 1;
							}	
						}
						else
						{
							echo 0;
						}
					}
				}
				else
				{
					$this->db->trans_begin();

					$edit_cnt = $this->vouchermdl->check_edit_duplicate_voucher_entry($form_data['vc_entry_no'],$_SESSION['fin_year'],$vc_id);

					if ($edit_cnt != 0) 
					{
						echo 2;
					}
					else
					{
						// echo "<pre>";print_r($form_data);exit();
						$result = $this->db_operations->data_update('voucher_master', $voucher_data, 'vc_id', $vc_id);

						if(!empty($result))
						{
							if(!empty($voucher_trans))
							{
								$this->voucher_trans_insert_update($voucher_trans, $vc_id);
								$this->order_update_master_data($order_data, $result, $grp);
							}

							if ($this->db->trans_status() === FALSE) {
							    $this->db->trans_rollback();
							    echo 0;
							} else {
							    $this->db->trans_commit();
								echo 1;
							}	
						}
						else
						{
							echo 0;
						}
					}
				}


			// echo "<pre>";print_r($voucher_data);
			} 
			else 
			{
				redirect('loginctrl/logout');
			}
					
		}

		public function voucher_trans_insert_update($voucher_trans,$vc_id)
		{
			// 0 = not avialable
			// 1 = avialable
			$voucher_trans_data = $this->db_operations->get_record('voucher_trans',array('vt_vc_id' =>$vc_id)); // get all db record first and all form data and match who is missing and those data delete by system  

			foreach ($voucher_trans_data as $key => $value) 
			{
				if (!in_array($value['vt_id'], $voucher_trans['vt_id'])) 
				{	
					/*delete transactions*/
					$del_res = $this->db_operations->delete_record('voucher_trans',array('vt_id' => $value['vt_id']));
				}
			}

			foreach ($voucher_trans['vt_id'] as $key => $value) 
			{
				if(($voucher_trans['vt_adjust_amt'][$key] > 0) || ($voucher_trans['vt_adjust_round_off'][$key] > 0))
				{
					$voucher_trans_data = array();
					$voucher_trans_data['vt_vc_id']        	= $vc_id;
					
					if (isset($voucher_trans['vt_om_id'])) 
					{
						$voucher_trans_data['vt_om_id']    	= $voucher_trans['vt_om_id'][$key];
					}
					else
					{
						$voucher_trans_data['vt_hm_id']   	= $voucher_trans['vt_hm_id'][$key];
					}

					$voucher_trans_data['vt_entry_no']      	= $voucher_trans['vt_entry_no'][$key];
					$voucher_trans_data['vt_entry_date']     = date("Y-m-d", strtotime($voucher_trans['vt_entry_date'][$key]));
					$voucher_trans_data['vt_final_total']  	= $voucher_trans['vt_final_total'][$key];
					$voucher_trans_data['vt_adjust_amt']   	= $voucher_trans['vt_adjust_amt'][$key];
					$voucher_trans_data['vt_adjust_round_off']  = $voucher_trans['vt_adjust_round_off'][$key];

					if ($value == 0) 
					{
						/*voucher transaction insert process*/
						$voucher_trans_result = $this->db_operations->data_insert('voucher_trans', $voucher_trans_data);
					} 
					else 
					{
						/*voucher transaction update process*/
						$voucher_trans_result = $this->db_operations->data_update('voucher_trans',$voucher_trans_data,'vt_id',$value);
					}
				}

			}
		}

		public function order_update_master_data($order_data,$vc_id, $grp)
		{
			if ($grp == 5) 
			{
				foreach ($order_data['om_id'] as $key => $value) 
				{
					$om_allocated_amt 			= $order_data['om_allocated_amt'][$key];
					$om_adjust_amt 				= $order_data['om_adjust_amt'][$key];
					$total_allocated_amt 		= $om_allocated_amt + $om_adjust_amt;

					$om_round_off_amt 			= $order_data['om_voucher_round_off_amt'][$key];
					$om_adjust_round_off_amt 	= $order_data['om_adjust_round_off'][$key];
					$total_round_off_amt 		= $om_round_off_amt + $om_adjust_round_off_amt;

					$order_update = $this->db_operations->data_update('order_master',array('om_allocated_amt'=> $total_allocated_amt , 'om_voucher_round_off_amt' => $total_round_off_amt),'om_id',$value);
				}
			}
			else
			{
				foreach ($order_data['hm_id'] as $key => $value) 
				{
					$om_allocated_amt 			= $order_data['hm_allocated_amt'][$key];
					$om_adjust_amt 				= $order_data['hm_adjust_amt'][$key];
					$total_allocated_amt 		= $om_allocated_amt + $om_adjust_amt;

					$om_round_off_amt 			= $order_data['hm_round_off'][$key];
					$om_adjust_round_off_amt 	= $order_data['hm_adjust_round_off'][$key];
					$total_round_off_amt 		= $om_round_off_amt + $om_adjust_round_off_amt;

					$order_update = $this->db_operations->data_update('hisab_master',array('hm_allocated_amt'=> $total_allocated_amt , 'hm_round_off' => $total_round_off_amt),'hm_id',$value);
				}
			}

		}


		public function voucher_delete($vc_id,$type)
		{
			$record['voucher_trans_data'] = $this->db_operations->get_record("voucher_trans", array('vt_vc_id' => $vc_id));
			// echo"<pre>";print_r($record);exit;

			if ($type == 5) 
			{
				foreach ($record['voucher_trans_data'] as $key => $value) 
				{
					if($value['vt_om_id'] > 0)
					{
						$vt_om_id 				= $value['vt_om_id'];
						$vt_adjust_amt 			= $value['vt_adjust_amt'];

						$vt_adjust_round_off 	= $value['vt_adjust_round_off'];

						$order_data = array();
						$order_data = $this->db_operations->get_record("order_master", array('om_id' => $value['vt_om_id']));
						
						$om_allocated_amt 		= $order_data[0]['om_allocated_amt'];
						$om_roundoff 			= $order_data[0]['om_voucher_round_off_amt'];

						$total_allocated_amt 	= $om_allocated_amt - $vt_adjust_amt;
						$total_roundoff_amt 	= $om_roundoff - $vt_adjust_round_off;

						$order_update = $this->db_operations->data_update('order_master',array('om_allocated_amt' => $total_allocated_amt,'om_voucher_round_off_amt' => $total_roundoff_amt),'om_id',$value['vt_om_id']);
					}
				}
			}
			

			$this->db_operations->delete_record("voucher_master", array('vc_id' => $vc_id));
			echo $this->db_operations->delete_record("voucher_trans", array('vt_vc_id' => $vc_id));
		
		}
        
		
// ********************* VOUCHER MASTER  END ***************************************

	}

?>