<?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();
					$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($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);						
					}
					if($_GET['type'] == "RECEIPT")
					{
						$this->load->view('pages/voucher/payment_voucher_form', $record);
						// $this->load->view('pages/voucher/receipt_voucher_form', $record);
					}
				
				}
				elseif( $_GET['action'] == 'update')
				{
					$id 		= $_GET['id'];
	                $vm_id 		= encrypt_decrypt("decrypt", $id, SECRET_KEY);
	                // echo "<pre>"; print_r($_GET); exit();
	                if (!empty($vm_id)) 
	                {
	                	$record = $this->vouchermdl->get_single_update_voucher($vm_id);	                	
	                	// echo "<pre>"; print_r($record); exit;
	                	if($_GET['type'] == "PAYMENT")
						{
							
							$this->load->view('pages/voucher/payment_voucher_form', $record);
						}
						if($_GET['type'] == "RECEIPT")
						{
							$this->load->view('pages/voucher/payment_voucher_form', $record);
						}

	                } 
	                else 
	                {
						$this->load->view('errors/error');
	                }										
				}
				elseif( $_GET['action'] == 'print')
				{
					$id 		= $_GET['id'];
	                $vm_id 		= encrypt_decrypt("decrypt", $id, SECRET_KEY);
	                // echo "<pre>"; print_r($_GET); exit();
	                if (!empty($vm_id)) 
	                {
	                	$record = $this->vouchermdl->get_single_update_voucher($vm_id);	                	
	                	// echo "<pre>"; print_r($record); exit;
	                	if($_GET['type'] == "PAYMENT")
						{
							
							$this->load->view('pdfs/voucher/payment_pdf', $record);
						}
						if($_GET['type'] == "RECEIPT")
						{
							$this->load->view('pdfs/voucher/receipt_pdf', $record);
						}

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

			if(sessionExist()) 
			{				
				echo json_encode($this->vouchermdl->get_client_payment_status($client_id));
				
			} 
			else 
			{
					redirect('loginctrl/logout');
			}
		}
		public function get_karigar_payment_status($karigar_id)
      	{	

			if(sessionExist()) 
			{				
				echo json_encode($this->vouchermdl->get_karigar_payment_status($karigar_id));
				
			} 
			else 
			{
					redirect('loginctrl/logout');
			}
		}
		public function get_party_purchase_details($acc_id)
      	{	

			if(sessionExist()) 
			{				
				$record = $this->vouchermdl->get_party_purchase_details($acc_id);
				// echo"<pre>";print_r($record);exit;
				echo json_encode($record);
				
			} 
			else 
			{
					redirect('loginctrl/logout');
			}
		}
		public function payment_voucher_insert_update($vm_id)
		{
			if(sessionExist()) {
				$form_data 	= $this->input->post();
			 	// echo"<pre>"; print_r($form_data); exit();
			 	$master_data 					= [];
				$master_data['vm_entry_no']		= $form_data['vm_entry_no'];
				$master_data['vm_entry_date']	= date('Y-m-d', strtotime($form_data['vm_entry_date']));
				$master_data['vm_cash_acc_id']	= $form_data['vm_cash_acc_id'];
				$master_data['vm_voucher_type']	= $form_data['vm_voucher_type'];
				$master_data['vm_grp_id']		= $form_data['vm_grp_id'];
				$master_data['vm_party_id']		= $form_data['vm_party_id'];
				$master_data['vm_amount']		= $form_data['vm_amount'];
				$master_data['vm_round_off']	= $form_data['vm_round_off'];
				$master_data['vm_cheque_no']	= $form_data['vm_cash_acc_id'] == 'BANK' ? $form_data['vm_cheque_no'] : '';
				$master_data['vm_cheque_date']	= $form_data['vm_cash_acc_id'] == 'BANK' ? date('Y-m-d', strtotime($form_data['vm_cheque_date'])) : '';
				$master_data['vm_notes']		= $form_data['vm_notes'];				
				$master_data['vm_trans_type']	= $form_data['vm_trans_type'];				
				$master_data['vm_prepared_id']	= $form_data['vm_prepared_id'];				
				$master_data['vm_checked_id']	= $form_data['vm_checked_id'];				
				
				if($vm_id == 0) 
				{
					$this->db->trans_begin();
					$cnt = $this->db_operations->get_cnt('voucher_master', ['vm_entry_no' => $master_data['vm_entry_no']]);
					if($cnt != 0)
					{
						$master_data['vm_entry_no'] = $this->db_operations->get_fin_year_max_id_custom('voucher_master', 'vm_entry_no', 'vm_fin_year', $_SESSION['fin_year']);
					}
					
					$master_data['vm_fin_year'] 		= $_SESSION['fin_year'];
					$master_data['vm_branch_id'] 		= $_SESSION['user_branch_id'];
					$master_data['vm_user_id'] 			= $_SESSION['user_id'];
					$master_data['vm_date_create'] 		= date("Y-m-d H:i:s");					
					$master_data['vm_update_id']		= $_SESSION['user_id'];
					$master_data['vm_date_update'] 		= date("Y-m-d H:i:s");	

					// echo "<pre>"; print_r($master_data); exit;

					$vm_id = $this->db_operations->data_insert('voucher_master', $master_data);
					if(!empty($vm_id))
					{	
						if($this->update_party_balance($form_data) > 0)
						{
							if(isset($form_data['vt_id']))
							{
								if($this->insert_update_voucher_trans($form_data, $vm_id) > 0)
								{
									if($this->update_master_table($form_data, $vm_id) > 0)
									{									
										if ($this->db->trans_status() === FALSE) 
										{
										    $this->db->trans_rollback();
										    $response['resp'] = 0;
										} 
										else 
										{
										    $this->db->trans_commit();
										    $response['resp'] = 1;
										}
									}
									else
									{
										$this->db->trans_rollback();
									    $response['resp'] = 0;
									}								
								}
								else
								{
									$this->db->trans_rollback();
								    $response['resp'] = 0;
								}	
							}
							else
							{
								if ($this->db->trans_status() === FALSE) 
								{
								    $this->db->trans_rollback();
								    $response['resp'] = 0;
								} 
								else 
								{
								    $this->db->trans_commit();
								    $response['resp'] = 1;
								}
							}
						}
						else
						{
							$this->db->trans_rollback();
						    $response['resp'] = 0;
						}							
					}
					else
					{
						$this->db->trans_rollback();
					    $response['resp'] = 0;
					}
				} 
				else
				{
					$this->db->trans_begin();
					$master_data['vm_update_id']		= $_SESSION['user_id'];
					$master_data['vm_date_update'] 		= date("Y-m-d H:i:s");	

					// echo "<pre>"; print_r($master_data); exit;
					$db_voudher_data = $this->db_operations->get_record('voucher_master', ['vm_id' => $vm_id]);
					if($this->db_operations->data_update('voucher_master', $master_data, 'vm_id', $vm_id) > 0)
					{	
						if($this->update_party_balance($form_data, $db_voudher_data) > 0)
						{
							if(isset($form_data['vt_id']))
							{
								if($this->insert_update_voucher_trans($form_data, $vm_id) > 0)
								{
									if($this->update_master_table($form_data, $vm_id) > 0)
									{									
										if ($this->db->trans_status() === FALSE) 
										{
										    $this->db->trans_rollback();
										    $response['resp'] = 0;
										} 
										else 
										{
										    $this->db->trans_commit();
										    $response['resp'] = 1;
										}
									}
									else
									{
										$this->db->trans_rollback();
									    $response['resp'] = 0;
									}								
								}
								else
								{
									$this->db->trans_rollback();
								    $response['resp'] = 0;
								}	
							}
							else
							{
								if ($this->db->trans_status() === FALSE) 
								{
								    $this->db->trans_rollback();
								    $response['resp'] = 0;
								} 
								else 
								{
								    $this->db->trans_commit();
								    $response['resp'] = 1;
								}
							}
						}
						else
						{
							$this->db->trans_rollback();
						    $response['resp'] = 0;
						}							
					}
					else
					{
						$this->db->trans_rollback();
					    $response['resp'] = 0;
					}
				}
				echo json_encode($response);
			} 
			else 
			{
				redirect('loginctrl/logout');		
			}
		}
		public function insert_update_voucher_trans($form_data, $vm_id)
		{
			foreach ($form_data['vt_id'] as $key => $value) 
			{
				$trans_data = array();
				$trans_data['vt_vm_id'] 			= $vm_id;
				if($form_data['vm_grp_id'] == 5 )
				{
					$trans_data['vt_om_id'] 			= $form_data['_id'][$key];
				}
				else if($form_data['vm_grp_id'] == 23 )
				{
					$trans_data['vt_hm_id'] 			= $form_data['_id'][$key];
				}
				$trans_data['vt_entry_no'] 			= $form_data['entry_no'][$key];
				$trans_data['vt_entry_date']		= date('Y-m-d', strtotime($form_data['entry_date'][$key]));
				$trans_data['vt_total_qty']			= $form_data['total_qty'][$key];
				$trans_data['vt_total_amt']			= $form_data['total_amt'][$key];
				$trans_data['vt_adjust_amt']		= $form_data['adjust_amt'][$key];
				$trans_data['vt_adjust_round_off']	= $form_data['adjust_round_off'][$key];

				if ($value == 0) 
				{
					/*sale transaction insert process*/
					// echo "<pre>"; print_r($trans_data); exit();
					if($this->db_operations->data_insert('voucher_trans', $trans_data) < 1)
					{
						return 0;
					}
				}
				else
				{
					if($this->db_operations->data_update('voucher_trans', $trans_data, 'vt_id', $value) < 1 )
					{
						return 0;	
					}
				}	
			}

			return 1;
		}
		public function update_master_table($form_data, $vm_id)
		{
			foreach ($form_data['_id'] as $key => $value) 
			{
				$master_data 			= array();
				$allocated_amt 			= $form_data['allocated_amt'][$key];
				$adjust_round_off_old 	= $form_data['adjust_round_off_old'][$key];
				$adjust_amt 			= $form_data['adjust_amt'][$key];
				$adjust_round_off 		= $form_data['adjust_round_off'][$key];
				$total_allocated_amt 	= $allocated_amt + $adjust_amt;
				$total_adjust_round_off = $adjust_round_off_old + $adjust_round_off;
				if($form_data['vm_grp_id'] == 5 )
				{
					$master_data['om_vm_id'] 			= $vm_id;
					$master_data['om_allocated_amt']	= $total_allocated_amt;
					$master_data['om_adjust_amt'] 		= $adjust_amt;
					$master_data['om_adjust_round_off'] = $total_adjust_round_off;					

					if($this->db_operations->data_update('order_master',$master_data,'om_id',$value) < 1)
					{
						return 0;
					}
				}
				else if($form_data['vm_grp_id'] == 23 )
				{
					$master_data['hm_vm_id'] 			= $vm_id;
					$master_data['hm_allocated_amt']	= $total_allocated_amt;
					$master_data['hm_adjust_amt'] 		= $adjust_amt;
					$master_data['hm_adjust_round_off'] = $total_adjust_round_off;					

					if($this->db_operations->data_update('hisab_master',$master_data,'hm_id',$value) < 1)
					{
						return 0;
					}
				}
			}

			return 1;	
		}
		public function update_party_balance($form_data, $data = array())
		{
			if($form_data['vm_grp_id'] == 5 )
			{
				$party_data = $this->db_operations->get_record('client_master', ['client_id' => $form_data['vm_party_id']]);
			}
			else if($form_data['vm_grp_id'] == 23 )
			{
				$party_data = $this->db_operations->get_record('karigar_master', ['km_id' => $form_data['vm_party_id']]);
			}
			
			// echo "<pre>"; print_r($party_data);
			// echo "<pre>"; print_r($data);exit();
			if(empty($data))
			{
				if(!empty($party_data))
				{
					if($form_data['vm_grp_id'] == 5) // client
					{
						$db_balance = $party_data[0]['client_bal'];
						$form_amt 	= $form_data['vm_amount'] + $form_data['vm_round_off'];
						if($form_data['vm_voucher_type'] == 1) // payment (rarely happen)
						{						
							$total_bal 	= $db_balance + $form_amt;							
						}
						else if($form_data['vm_voucher_type'] == 2) // receipt
						{							
							$total_bal 	= $db_balance - $form_amt;								
						}


						if($this->db_operations->data_update('client_master', ['client_bal' => $total_bal], 'client_id', $party_data[0]['client_id']) > 0)
						{
							return 1;
						}
						else
						{
							return 0;
						}	
					}
					else if($form_data['vm_grp_id'] == 23) // karigar
					{
						$db_balance = $party_data[0]['km_bal'];
						$form_amt 	= $form_data['vm_amount'] + $form_data['vm_round_off'];
						if($form_data['vm_voucher_type'] == 1) // payment 
						{						
							$total_bal 	= $db_balance - $form_amt;							
						}
						else if($form_data['vm_voucher_type'] == 2) // receipt (rarely happen)
						{							
							$total_bal 	= $db_balance + $form_amt;								
						}

						
						if($this->db_operations->data_update('karigar_master', ['km_bal' => $total_bal], 'km_id', $party_data[0]['km_id']) > 0)
						{
							return 1;
						}
						else
						{
							return 0;
						}	
					}		
					
				}
				else
				{
					return 0;
				}	
			}
			else
			{
				$prev_vm_amt = ($data[0]['vm_amount'] + $data[0]['vm_round_off']); // 180
				if(!empty($party_data))
				{
					if($form_data['vm_grp_id'] == 5) // client
					{
						$db_balance = $party_data[0]['client_bal'];
						$form_amt 	= $form_data['vm_amount'] + $form_data['vm_round_off'];
						if($form_data['vm_voucher_type'] == 1) // payment (rarely happen)
						{						
							$total_bal 	= ($db_balance - $prev_vm_amt) + $form_amt;							
						}
						else if($form_data['vm_voucher_type'] == 2) // receipt
						{							
							$total_bal 	= ($db_balance - $prev_vm_amt) - $form_amt;								
						}


						if($this->db_operations->data_update('client_master', ['client_bal' => $total_bal], 'client_id', $party_data[0]['client_id']) > 0)
						{
							return 1;
						}
						else
						{
							return 0;
						}	
					}
					else if($form_data['vm_grp_id'] == 23) // karigar
					{
						$db_balance = $party_data[0]['km_bal'];
						$form_amt 	= $form_data['vm_amount'] + $form_data['vm_round_off'];
						if($form_data['vm_voucher_type'] == 1) // payment 
						{						
							$total_bal 	= ($db_balance - $prev_vm_amt) - $form_amt;							
						}
						else if($form_data['vm_voucher_type'] == 2) // receipt (rarely happen)
						{							
							$total_bal 	= ($db_balance - $prev_vm_amt) + $form_amt;								
						}

						
						if($this->db_operations->data_update('karigar_master', ['km_bal' => $total_bal], 'km_id', $party_data[0]['km_id']) > 0)
						{
							return 1;
						}
						else
						{
							return 0;
						}	
					}
				}
				else
				{
					return 0;
				}	
			}
		}
		public function payment_voucher_trans_insert($voucher_trans, $vc_id)
		{
			// echo "<pre>"; print_r($voucher_trans); exit;
			$flag = 0;
			foreach ($voucher_trans['vt_pm_id'] as $key => $value) 
			{
				if($voucher_trans['vt_adjust_amt'][$key] > 0)
				{
					$voucher_trans_data = array();
					$voucher_trans_data['vt_vc_id']             = $vc_id;
					$voucher_trans_data['vt_pm_id']             = $voucher_trans['vt_pm_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_bill_no']           = $voucher_trans['vt_bill_no'][$key];
					$voucher_trans_data['vt_bill_date']         = date("Y-m-d", strtotime($voucher_trans['vt_bill_date'][$key]));
					$voucher_trans_data['vt_total_qty']         = $voucher_trans['vt_total_qty'][$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];

					$result = $this->db_operations->data_insert('voucher_trans', $voucher_trans_data);

					if(!empty($result))
					{
						$flag = 1;
					}
				}
				else
				{
					$flag = 1;
				}
			}

			return $flag;
		}
		public function update_party_bal($form_data){
			// echo "<pre>"; print_r($form_data); exit;
			$party_data = $this->db_operations->get_record('karigar_master', ['km_id' => $form_data['vm_party_id']]);
			if($form_data['vm_grp_id'] == 5 ){
				$party_data = $this->db_operations->get_record('client_master', ['client_id' => $form_data['vm_party_id']]);
			}
			if(empty($party_data)) return 0;
			
			if($form_data['vm_grp_id'] == 5){ // client
				$db_balance = $party_data[0]['client_bal'];
				$form_amt 	= $form_data['vm_amount'] + $form_data['vm_round_off'];
				if($form_data['vm_voucher_type'] == 1){ // payment (rarely happen)
					$total_bal 	= $db_balance + $form_amt;							
				}
				else if($form_data['vm_voucher_type'] == 2){ // receipt	
					$total_bal 	= $db_balance - $form_amt;								
				}
				if($this->db_operations->data_update('client_master', ['client_bal' => $total_bal], 'client_id', $party_data[0]['client_id']) < 1) return 0;
			}
			else if($form_data['vm_grp_id'] == 23){ // karigar
				$db_balance = $party_data[0]['km_bal'];
				$form_amt 	= $form_data['vm_amount'] + $form_data['vm_round_off'];
				if($form_data['vm_voucher_type'] == 1){ // payment 
					$total_bal 	= $db_balance + $form_amt;							
				}
				else if($form_data['vm_voucher_type'] == 2){ // receipt (rarely happen)
					$total_bal 	= $db_balance - $form_amt;								
				}

				
				if($this->db_operations->data_update('karigar_master', ['km_bal' => $total_bal], 'km_id', $party_data[0]['km_id']) < 1) return 0;
			}

			return 1;
		}
		public function update_master($vm_id){
			$form_data = $this->db_operations->get_record('voucher_trans', ['vt_vm_id' =>$vm_id]);
			if(empty($form_data)) return 1;
			foreach ($form_data as $key => $value){
				if($value['vt_om_id'] != 0 ){
					$data = $this->db_operations->get_record('order_master', ['om_id' => $value['vt_om_id']]);
					if(!empty($data)){
						$master_data['om_allocated_amt'] = $data[0]['om_allocated_amt'] - $value['vt_adjust_amt'];
						if($this->db_operations->data_update('order_master',$master_data,'om_id',$value['vt_om_id']) < 1) return 0;
					}
				}
				else if($value['vt_hm_id'] != 0){
					$data = $this->db_operations->get_record('hisab_master', ['hm_id' => $value['vt_hm_id']]);
					if(!empty($data)){
						$master_data['hm_allocated_amt'] = $data[0]['hm_allocated_amt'] - $value['vt_adjust_amt'];
						if($this->db_operations->data_update('hisab_master',$master_data,'hm_id',$value['vt_hm_id']) < 1) return 0;
					}
				}
			}
			return 1;	
		}
		public function voucher_delete($id){
			if(!sessionExist()){
				echo json_encode($this->session_expired);
				return;
			}
			$data = $this->db_operations->get_record('voucher_master', ['vm_id' => $id]);
			// echo "<pre>"; print_r($data); exit;
			if(empty($data)){
				echo json_encode(['status' => true, 'flag' => 2, 'data' => [], 'msg' => 'Not Found']);
				return;	
			}
			$this->db->trans_begin();
			if($this->update_party_bal($data[0]) < 1){
				$this->db->trans_rollback();
				echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Transaction not deleted']);
				return;
			}
			if($this->update_master($id) < 1){
				$this->db->trans_rollback();
				echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Reference not updated']);
				return;
			}
			if($this->db_operations->delete_record('voucher_trans', ['vt_vm_id' => $id]) < 1){
				$this->db->trans_rollback();
				echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Transaction not deleted']);
				return;
			}
			if($this->db_operations->delete_record('voucher_master', ['vm_id' => $id]) < 1){
				$this->db->trans_rollback();
				echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Master not deleted']);
				return;
			}
			if ($this->db->trans_status() === FALSE){
			    $this->db->trans_rollback();
			    echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Rollback']);
				return;
		    }
		    $this->db->trans_commit();
			echo json_encode(['status' => true, 'flag' => 1, 'data' => [], 'msg' => 'Deleted successfully']);
        } 
        public function payment_voucher_edit_insert($vc_id)
		{

			$user_id = $this->session->userdata('user_id');
			
			if(!empty($user_id))
			{
				$form_data = $this->input->post();
				// echo"<pre>";print_r($form_data);exit;

				$purchase_data = array();
				$voucher_trans = array();

				
				$purchase_data['pm_id'] = $form_data['pm_id'];
				$purchase_data['pm_allocated_amt'] = $form_data['pm_allocated_amt'];
				$purchase_data['pm_adjust_round_off_old'] = $form_data['pm_adjust_round_off_old'];
				$purchase_data['pm_adjust_amt'] = $form_data['pm_adjust_amt'];
				$purchase_data['pm_adjust_round_off'] = $form_data['pm_adjust_round_off'];

				
				

				$voucher_trans['vt_pm_id']             = $form_data['pm_id'];
				$voucher_trans['vt_entry_no']          = $form_data['pm_entry_no'];
				$voucher_trans['vt_entry_date']        = $form_data['pm_entry_date'];
				$voucher_trans['vt_bill_no']           = $form_data['pm_bill_no'];
				$voucher_trans['vt_bill_date']         = $form_data['pm_bill_date'];
				$voucher_trans['vt_total_qty']         = $form_data['pm_total_qty'];
				$voucher_trans['vt_final_total']       = $form_data['pm_final_amt'];
				$voucher_trans['vt_adjust_amt']        = $form_data['pm_adjust_amt'];
				$voucher_trans['vt_adjust_round_off']  = $form_data['pm_adjust_round_off'];

				unset($form_data['pm_id']);
				unset($form_data['pm_entry_no']);
				unset($form_data['pm_entry_date']);
				unset($form_data['pm_bill_no']);
				unset($form_data['pm_bill_date']);
				unset($form_data['pm_total_qty']);
				unset($form_data['pm_final_amt']);
				unset($form_data['pm_allocated_amt']);
				unset($form_data['pm_adjust_round_off_old']);
				unset($form_data['pm_adjust_amt']);
				unset($form_data['pm_adjust_round_off']);
				
				$this->payment_voucher_trans_insert($voucher_trans, $vc_id);
						
				echo $this->purchase_update_from_payment_voucher_update($purchase_data, $vc_id);
					
			}
			else
				$this->load->view('pages/login');		
		}
		function purchase_update_from_payment_voucher_update($purchase_data , $vc_id)
		{
			$flag = 0;
			foreach ($purchase_data['pm_id'] as $key => $value) 
			{
				$pm_allocated_amt = $purchase_data['pm_allocated_amt'][$key];
				$pm_adjust_round_off_old = $purchase_data['pm_adjust_round_off_old'][$key];
				$pm_adjust_amt = $purchase_data['pm_adjust_amt'][$key];
				$pm_adjust_round_off = $purchase_data['pm_adjust_round_off'][$key];

				$total_allocated_amt = $pm_allocated_amt + $pm_adjust_amt;
				$total_adjust_round_off = $pm_adjust_round_off_old + $pm_adjust_round_off;

				$purchase_update = $this->db_operations->data_update('purchase_master',array('pm_allocated_amt'=>$total_allocated_amt,'pm_adjust_amt'=>$pm_adjust_amt,'pm_adjust_round_off'=>$total_adjust_round_off),'pm_id',$value);

				$flag = ($purchase_update == 1) ? 1 : 0;
			}

			return $flag;

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

				$invoice_data = array();
				$voucher_trans = array();

				
				if(isset($form_data['im_id']))
				{
					$invoice_data['im_id'] 						= $form_data['im_id'];
					$invoice_data['im_allocated_amt'] 			= $form_data['im_allocated_amt'];
					$invoice_data['im_adjust_round_off_old'] 	= $form_data['im_adjust_round_off_old'];
					$invoice_data['im_adjust_amt'] 				= $form_data['im_adjust_amt'];
					$invoice_data['im_adjust_round_off'] 		= $form_data['im_adjust_round_off'];

					unset($form_data['im_id']);
					unset($form_data['im_allocated_amt']);
					unset($form_data['im_adjust_round_off_old']);
					unset($form_data['im_adjust_amt']);
					unset($form_data['im_adjust_round_off']);

					$voucher_trans['vt_im_id']             = $invoice_data['im_id'];
					$voucher_trans['vt_bill_no']           = $form_data['im_bill_no'];
					$voucher_trans['vt_bill_date']         = $form_data['im_bill_date'];
					$voucher_trans['vt_total_qty']         = $form_data['im_total_qty'];
					$voucher_trans['vt_final_total']       = $form_data['im_final_amt'];
					$voucher_trans['vt_adjust_amt']        = $invoice_data['im_adjust_amt'];
					$voucher_trans['vt_adjust_round_off']  = $invoice_data['im_adjust_round_off'];

			
					unset($form_data['im_bill_no']);
					unset($form_data['im_bill_date']);
					unset($form_data['im_total_qty']);
					unset($form_data['im_final_amt']);
				
				}

				$form_data['vc_cash_acc_id'] = explode("-", $form_data['vc_cash_acc_id'])[0];
				$form_data['vc_grp_id'] = explode("-", $form_data['vc_grp_id'])[0];
				
				$cash_acc_id = $form_data['vc_cash_acc_id'];
				$vc_party_id = $form_data['vc_party_id'];

				$form_data['vc_entry_no'] = $form_data['vc_entry_no'];
				$form_data['vc_entry_date'] = date("Y-m-d", strtotime($form_data['vc_entry_date']));

				$form_data['vc_cash_acc_id'] = $cash_acc_id;
				$form_data['vc_party_id'] = $vc_party_id;

				if($form_data['vc_voucher_type'] == 1)
				{
					$form_data['vc_trans_type']  = 	"DR";
					$new_trans_type = "CR";
				}
				else if($form_data['vc_voucher_type'] == 2)
				{
					$form_data['vc_trans_type'] = 	"CR";	
					$new_trans_type = "DR";
				}
				else
				{
					if($form_data['op_trans_type'] == "DR") 
					{
						$form_data['vc_trans_type'] = 	"DR";	
						$new_trans_type = "CR";
					}
					else
					{
						$form_data['vc_trans_type'] = 	"CR";	
						$new_trans_type = "DR";	
					}
				}

				unset($form_data['op_trans_type']);

				// $this->db->trans_start();

				$form_data['vc_user_id'] = $_SESSION['user_id'];
				$form_data['vc_update_id'] = $_SESSION['user_id'];
				$form_data['vc_date_create'] = date("Y-m-d H:i:s");
				$form_data['vc_cheque_date'] = date('Y-m-d', strtotime($form_data['vc_cheque_date']));

				// echo "<pre>"; print_r($form_data); exit;
				
				$result = $this->db_operations->data_insert('voucher_master', $form_data);
				// echo"111";exit;

				// echo "<pre>"; print_r($result); exit;

				if(!empty($result))
				{
					if(isset($form_data['vc_id']))
						unset($form_data['vc_id']);
					
					$form_data['vc_cash_acc_id'] = 	$vc_party_id;
					$form_data['vc_party_id'] 	 = 	$cash_acc_id;
					$form_data['vc_refer_id'] 	 = 	$result;
					$form_data['vc_trans_type']  = 	$new_trans_type;

					$result_new = $this->db_operations->data_insert('voucher_master', $form_data);

					if(!empty($result_new))
					{
						if(!empty($voucher_trans))
						{
							$this->receipt_voucher_trans_insert($voucher_trans, $result);
							echo $this->invoice_update_from_receipt_voucher($invoice_data, $result);
						}
						else
						{
							echo 1;
						}
						
					}
					else
					{
						echo 0;
					}			
				}
				else
				{
					echo 0;
				}
			}
			else 
			{
				redirect('loginctrl/logout');
			}
					
		}
		function receipt_voucher_trans_insert($voucher_trans, $vc_id)
		{
			
			$flag = 0;
			foreach ($voucher_trans['vt_im_id'] as $key => $value) 
			{
				if($voucher_trans['vt_adjust_amt'][$key] > 0)
				{
					$voucher_trans_data = array();
					$voucher_trans_data['vt_vc_id']             = $vc_id;
					$voucher_trans_data['vt_im_id']             = $voucher_trans['vt_im_id'][$key];
					$voucher_trans_data['vt_bill_no']           = $voucher_trans['vt_bill_no'][$key];
					$voucher_trans_data['vt_bill_date']         = date("Y-m-d", strtotime($voucher_trans['vt_bill_date'][$key]));
					$voucher_trans_data['vt_total_qty']         = $voucher_trans['vt_total_qty'][$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];

					$result = $this->db_operations->data_insert('voucher_trans', $voucher_trans_data);

					if(!empty($result))
					{
						$flag = 1;
					}
				}
				else
				{
					$flag = 1;
				}
			}

			return $flag;
		}
		function invoice_update_from_receipt_voucher($invoice_data , $vc_id)
		{
			$flag = 0;
			foreach ($invoice_data['im_id'] as $key => $value) 
			{
				$im_allocated_amt = $invoice_data['im_allocated_amt'][$key];
				$im_adjust_round_off_old = $invoice_data['im_adjust_round_off_old'][$key];
				$im_adjust_amt = $invoice_data['im_adjust_amt'][$key];
				$im_adjust_round_off = $invoice_data['im_adjust_round_off'][$key];

				$total_allocated_amt = $im_allocated_amt + $im_adjust_amt;
				$total_adjust_round_off = $im_adjust_round_off_old + $im_adjust_round_off;

				$invoice_update = $this->db_operations->data_update('invoice_master',array('im_allocated_amt'=>$total_allocated_amt,'im_adjust_amt'=>$im_adjust_amt,'im_adjust_round_off'=>$total_adjust_round_off),'im_id',$value);

				$flag = ($invoice_update == 1) ? 1 : 0;
			}

			return $flag;

		}
		public function receipt_voucher_edit_insert($vc_id)
		{

			$user_id = $this->session->userdata('user_id');
			
			if(!empty($user_id))
			{
				$form_data = $this->input->post();
				// echo"<pre>";print_r($form_data);exit;

				$invoice_data = array();
				$voucher_trans = array();

				
				$invoice_data['im_id'] = $form_data['im_id'];
				$invoice_data['im_allocated_amt'] = $form_data['im_allocated_amt'];
				$invoice_data['im_adjust_round_off_old'] = $form_data['im_adjust_round_off_old'];
				$invoice_data['im_adjust_amt'] = $form_data['im_adjust_amt'];
				$invoice_data['im_adjust_round_off'] = $form_data['im_adjust_round_off'];

				
				

				$voucher_trans['vt_im_id']             = $form_data['im_id'];
				$voucher_trans['vt_bill_no']           = $form_data['im_bill_no'];
				$voucher_trans['vt_bill_date']         = $form_data['im_bill_date'];
				$voucher_trans['vt_total_qty']         = $form_data['im_total_qty'];
				$voucher_trans['vt_final_total']       = $form_data['im_final_total'];
				$voucher_trans['vt_adjust_amt']        = $form_data['im_adjust_amt'];
				$voucher_trans['vt_adjust_round_off']  = $form_data['im_adjust_round_off'];

				unset($form_data['im_id']);
				unset($form_data['im_bill_no']);
				unset($form_data['im_bill_date']);
				unset($form_data['im_total_qty']);
				unset($form_data['im_final_amt']);
				unset($form_data['im_allocated_amt']);
				unset($form_data['im_adjust_round_off_old']);
				unset($form_data['im_adjust_amt']);
				unset($form_data['im_adjust_round_off']);
				
				$this->receipt_voucher_trans_insert($voucher_trans, $vc_id);
						
				echo $this->sale_update_from_receipt_voucher_update($invoice_data, $vc_id);
					
			}
			else
				$this->load->view('pages/login');		
		}

		function sale_update_from_receipt_voucher_update($invoice_data , $vc_id)
		{
			$flag = 0;
			foreach ($invoice_data['im_id'] as $key => $value) 
			{
				$im_allocated_amt 			= $invoice_data['im_allocated_amt'][$key];
				$im_adjust_round_off_old 	= $invoice_data['im_adjust_round_off_old'][$key];
				$im_adjust_amt			 	= $invoice_data['im_adjust_amt'][$key];
				$im_adjust_round_off 		= $invoice_data['im_adjust_round_off'][$key];

				$total_allocated_amt = $im_allocated_amt + $im_adjust_amt;
				$total_adjust_round_off = $im_adjust_round_off_old + $im_adjust_round_off;

				$sale_update = $this->db_operations->data_update('invoice_master',array('im_allocated_amt'=>$total_allocated_amt,'im_adjust_amt'=>$im_adjust_amt,'im_adjust_round_off'=>$total_adjust_round_off),'im_id',$value);

				$flag = ($sale_update == 1) ? 1 : 0;
			}

			return $flag;

		}
// ********************* VOUCHER MASTER  END ***************************************

	}

?>