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

class Voucherctrl extends CI_Controller {
	public $excel;
	public $Vouchermdl;
	public $pagination;
	public function __construct()
	{
		parent::__construct();

		$this->load->model('Vouchermdl');
		$this->load->library('session');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->config->load('extra');
		$this->load->library('db_operations');
	}

	public function index()
	{
		$user_id = $this->session->userdata('user_id'); 
		if(!empty($user_id)) 
		{
			$this->load->view('pages/master/index');
		} 
		else 
		{
			redirect('loginctrl/logout');	
		}
	}
	
	public function get_select2_party($func) //(party_data)
    {
    	$json = [];
		$data = $this->Vouchermdl->$func();
		foreach ($data as $key => $value){
			$json[] = ['id'=>$value['id'], 'text'=>$value['name']];
		}
		echo json_encode($json);
    }
    
    public function get_select2_billno($func) //(order_data)
    {
    	$json = [];
		$data = $this->Vouchermdl->$func();
		foreach ($data as $key => $value){
			$json[] = ['id'=>$value['id'], 'text'=>$value['name']];
		}
		echo json_encode($json);
    }

	public function send_sms_for_order($mob,$msg)
    {
		$xml_data ='<?xml version="1.0"?>
		<parent> 
		<child>
		<user>STYLO FASHION</user>
		<key>dacec36fb2XX</key>
		<mobile>'.$mob.'</mobile>
		<message>'.$msg.'</message>
		<accusage>1</accusage>
		<senderid>STYLO FASHION</senderid>
		</child>
		</parent>';
		// print_r($xml_data);exit;
		$URL = "http://sms.interlinkconsultant.com/submitsms.jsp?"; 

		$ch = curl_init($URL);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
		curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);
		// print_r($output);
    }


	/* babygolucky voucher is used */
	public function voucher()
	{
		$user_id = $this->session->userdata('user_id');
		if(!empty($user_id)) 
		{
			if($_GET['ac'] == "view") 
			{
				$config = array();
				$this->config->load('extra');
				$config = $this->config->item('pagination');
				$config['per_page'] = 20;
				// print_r($_GET);exit;
				if (isset($_GET['type']) && $_GET['type'] == 'client') 
				{
					$config['total_rows'] = $this->Vouchermdl->get_voucher_data_count($_SESSION['fin_year']);
				}
				elseif (isset($_GET['type']) && $_GET['type'] == 'supplier')  
				{
					$config['total_rows'] = $this->Vouchermdl->get_voucher_data_supplier_count($_SESSION['fin_year']);
				}
				elseif (isset($_GET['type']) && $_GET['type'] == 'karigar') 
				{
					$config['total_rows'] = $this->Vouchermdl->get_voucher_data_karigar_count($_SESSION['fin_year']);
				}else{
					$config['total_rows'] = $this->Vouchermdl->get_voucher_data_general_count($_SESSION['fin_year']);
				}

				$config['base_url']   = base_url('voucherctrl/voucher?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);

		       	if (isset($_GET['type']) && $_GET['type'] == 'client') 
				{
					$record = $this->Vouchermdl->get_all_voucher_data($_SESSION['fin_year'],$config['per_page'],$offset);
				}
				elseif (isset($_GET['type']) && $_GET['type'] == 'supplier')  
				{
					$record = $this->Vouchermdl->get_all_voucher_data_supplier($_SESSION['fin_year'],$config['per_page'],$offset);
				}
				elseif (isset($_GET['type']) && $_GET['type'] == 'karigar') 
				{
					$record = $this->Vouchermdl->get_all_voucher_data_karigar($_SESSION['fin_year'],$config['per_page'],$offset);
				}else{
					$record = $this->Vouchermdl->get_all_voucher_data_general($_SESSION['fin_year'],$config['per_page'],$offset);
				}

		        
		        $record['count'] 		= $offset;
				$record['total_rows'] 	= $config['total_rows'];

				if (isset($_GET['type']) && $_GET['type'] == 'client') 
				{
					$record['search_data'] 	= $this->config->item('voucher_search');
					$this->load->view('pages/voucher/voucher_master',$record);
				}
				elseif (isset($_GET['type']) && $_GET['type'] == 'supplier')  
				{
					$record['search_data'] 	= $this->config->item('voucher_search_supplier');
					$this->load->view('pages/voucher/voucher_master_supplier',$record);
				}
				elseif (isset($_GET['type']) && $_GET['type'] == 'karigar') 
				{
					$record['search_data'] 	= $this->config->item('voucher_search_karigar');
					$this->load->view('pages/voucher/voucher_master_karigar',$record);
				}else{
					$record['search_data'] 	= $this->config->item('voucher_search_general');
					// echo "<pre>";print_r($record);exit();
					$this->load->view('pages/voucher/voucher_master_general',$record);
				}

			}
			else if ($_GET['ac'] == "add") 
			{
				$record = $this->Vouchermdl->get_single_voucher($_SESSION['fin_year']);
				// echo "<pre>";print_r($record);exit();
				$record['voucher_type'] = $_GET['type'];
				$this->load->view('pages/voucher/voucher_form', $record);

			}
			else if ($_GET['ac'] == "update") 
			{
				$id 		= $_GET['id'];
                $vc_id 		= encrypt_decrypt("decrypt", $id, SECRET_KEY); 
                if (!empty($vc_id)) {
                	$record = $this->Vouchermdl->get_single_update_voucher($vc_id);
                	$record['voucher_type'] = $_GET['type'];
                	// echo "<pre>"; print_r($record);die;
					$this->load->view('pages/voucher/voucher_form', $record);
					
                } else {
					$this->load->view('errors/error');
                }
			}
			else if ($_GET['action'] == "print") 
			{
				$id 		= $_GET['id'];
                $vc_id 		= encrypt_decrypt("decrypt", $id, SECRET_KEY);

                if (!empty($sm_id)) {

                	// $record = $this->Vouchermdl->get_single_sale_data_for_print($vc_id);

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

					// $this->load->view('pdfs/sales_print',$record);
                } else {
					$this->load->view('errors/error');
                }
			}
		} 
		else 
		{
			redirect('loginctrl/logout');	
		}	
	}


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

	public function get_party_balance_status($acc_id,$grp_id) 
	{
		$user_id = $this->session->userdata('user_id');
		if(!empty($user_id))
		{
			$record = $this->Vouchermdl->get_party_acc_status($acc_id,$grp_id);

			echo json_encode($record);
		}
		else
		{
			redirect('loginctrl/logout');
		}	
	}

	public function get_party_balance_status_with_billno($bill_no)
	{
		$user_id = $this->session->userdata('user_id');
		if(!empty($user_id))
		{
			$record = $this->Vouchermdl->get_party_status_with_billno($bill_no);

			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 get_party_order_details_with_billno($bill_no)
	{
		$user_id = $this->session->userdata('user_id');
		if(!empty($user_id))
		{
			$record = $this->Vouchermdl->get_party_order_details_with_billno($bill_no);
			echo json_encode($record);
		}
		else
		{
			redirect('loginctrl/logout');
		}	
	}

	public function voucher_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;

			$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_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['sale_id']))
			{
				$order_data['sale_id'] 					= $form_data['sale_id'];
				$order_data['sale_allocated_amt'] 		= $form_data['sale_allocated_amt'];
				$order_data['sale_adjust_amt'] 			= $form_data['sale_adjust_amt'];
				$order_data['sale_round_off'] 			= $form_data['sale_round_off'];
				$order_data['sale_adjust_round_off'] 		= $form_data['sale_adjust_round_off'];

				$voucher_trans['vt_id']             	= $form_data['vt_id'];
				$voucher_trans['vt_sale_id']            = $form_data['sale_id'];
				$voucher_trans['vt_bill_no']           	= $form_data['sale_entry_no'];
				$voucher_trans['vt_bill_date']         	= $form_data['sale_entry_date'];
				$voucher_trans['vt_final_total']       	= $form_data['sale_total_amt'];
				$voucher_trans['vt_adjust_amt']        	= $form_data['sale_adjust_amt'];
				$voucher_trans['vt_adjust_round_off']  	= $form_data['sale_adjust_round_off'];

				$grp = 5;

			}

			/*supplier voucher*/
			if(isset($form_data['per_id']))
			{
				$order_data['per_id'] 					= $form_data['per_id'];
				$order_data['per_allocated_amt'] 		= $form_data['per_allocated_amt'];
				$order_data['per_adjust_amt'] 			= $form_data['per_adjust_amt'];
				$order_data['per_round_off'] 			= $form_data['per_round_off'];
				$order_data['per_adjust_round_off'] 	= $form_data['per_adjust_round_off'];

				$voucher_trans['vt_id']             	= $form_data['vt_id'];
				$voucher_trans['vt_per_id']             = $form_data['per_id'];
				$voucher_trans['vt_bill_no']           	= $form_data['per_bill_no'];
				$voucher_trans['vt_bill_date']         	= $form_data['per_bill_date'];
				$voucher_trans['vt_final_total']       	= $form_data['per_total_amt'];
				$voucher_trans['vt_adjust_amt']        	= $form_data['per_adjust_amt'];
				$voucher_trans['vt_adjust_round_off']  	= $form_data['per_adjust_round_off'];

				$grp = 6;

			}

			/*karigar voucher */

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

				$voucher_trans['vt_id']             	= $form_data['vt_id'];
				$voucher_trans['vt_hm_id']             	= $form_data['hm_id'];
				
				$voucher_trans['vt_bill_no']           	= $form_data['vt_bill_no'];
				$voucher_trans['vt_bill_date']         	= $form_data['vt_bill_date'];

				$voucher_trans['vt_final_total']       	= $form_data['hm_total'];
				$voucher_trans['vt_adjust_amt']        	= $form_data['hm_adjust_amt'];
				$voucher_trans['vt_adjust_round_off']  	= $form_data['hm_adjust_round_off'];

				$grp = 7;
			}

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

				$voucher_data['vc_entry_no'] = $this->Vouchermdl->get_fin_year_wise_max_id('voucher_master','vc_entry_no','vc_fin_year',$_SESSION['fin_year']);
				$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 ($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))
					{
						// echo "<pre>";print_r($order_data);exit();
						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;
					}
				}
			}
		}
		else
		{
			redirect('loginctrl/logout');
		}	
	}

	public function voucher_trans_insert_update($voucher_trans,$vc_id)  
	{
		// $voucher_trans_data = $this->db_operations->get_record('voucher_trans',array('vt_vc_id' =>$vc_id)); 
		// if(!empty($voucher_trans_data))
		// {
		// 	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_sale_id'])) 
				{
					$voucher_trans_data['vt_sale_id']    	= $voucher_trans['vt_sale_id'][$key];
				}
				elseif (isset($voucher_trans['vt_per_id'])) 
				{
					$voucher_trans_data['vt_per_id']    	= $voucher_trans['vt_per_id'][$key];
				}
				else
				{
					$voucher_trans_data['vt_hm_id']   	= $voucher_trans['vt_hm_id'][$key];
				}

				$voucher_trans_data['vt_bill_no']      	= $voucher_trans['vt_bill_no'][$key];
				$voucher_trans_data['vt_bill_date']     = date("Y-m-d");
				$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['sale_id'] as $key => $value) 
			{
				$sale_allocated_amt 			= $order_data['sale_allocated_amt'][$key];
				$sale_adjust_amt 				= $order_data['sale_adjust_amt'][$key];
				$sale_allocated_amt        	= floatval($sale_allocated_amt);
				$sale_adjust_amt 			= floatval($sale_adjust_amt);
				$total_allocated_amt 		= $sale_allocated_amt + $sale_adjust_amt;

				$sale_round_off_amt 			= $order_data['sale_round_off'][$key];
				$sale_adjust_round_off_amt 	= $order_data['sale_adjust_round_off'][$key];

				$sale_round_off_amt        	= floatval($sale_round_off_amt);
				$sale_adjust_round_off_amt 	= floatval($sale_adjust_round_off_amt);
				$total_round_off_amt 		= $sale_round_off_amt + $sale_adjust_round_off_amt;

				$order_update = $this->db_operations->data_update('sales_master',array('sale_allocated_amt'=> $total_allocated_amt , 'sale_round_off' => $total_round_off_amt),'sale_id',$value);
			}
		}
		elseif ($grp == 6) 
		{
			foreach ($order_data['per_id'] as $key => $value) 
			{
				$per_allocated_amt 			= $order_data['per_allocated_amt'][$key];
				$per_adjust_amt 			= $order_data['per_adjust_amt'][$key];

				$per_allocated_amt        	= floatval($per_allocated_amt);
				$per_adjust_amt 			= floatval($per_adjust_amt);
				$total_allocated_amt 		= $per_allocated_amt + $per_adjust_amt;

				$per_round_off_amt 			= $order_data['per_round_off'][$key];
				$per_adjust_round_off_amt 	= $order_data['per_adjust_round_off'][$key];

				$per_round_off_amt        	= floatval($per_round_off_amt);
				$per_adjust_round_off_amt 	= floatval($per_adjust_round_off_amt);
				$total_round_off_amt 		= $per_round_off_amt + $per_adjust_round_off_amt;

				$order_update = $this->db_operations->data_update('purchase_master',array('per_allocated_amt'=> $total_allocated_amt , 'per_round_off' => $total_round_off_amt),'per_id',$value);
			}
		}
		else
		{
			// print_r($order_data);exit();
			foreach ($order_data['hm_id'] as $key => $value)  
			{
				$hm_allocated_amt 			= $order_data['hm_allocated_amt'][$key];
				$hm_adjust_amt 				= $order_data['hm_adjust_amt'][$key];
				$total_allocated_amt 		= $hm_allocated_amt + $hm_adjust_amt;

				$hm_round_off_amt 			= $order_data['hm_round_off'][$key];
				$hm_adjust_round_off_amt 	= $order_data['hm_adjust_round_off'][$key];
				$total_round_off_amt 		= $hm_round_off_amt + $hm_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_adjust_amt' => $hm_adjust_amt,'hm_adjust_round_off' => $hm_adjust_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_sale_id'] > 0)
				{
					$vt_sale_id 				= $value['vt_sale_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("sales_master", array('sale_id' => $value['vt_sale_id']));
					
					$sale_allocated_amt 		= $order_data[0]['sale_allocated_amt'];
					$sale_round_off 			= $order_data[0]['sale_round_off'];

					$total_allocated_amt 	= $sale_allocated_amt - $vt_adjust_amt;
					$total_roundoff_amt 	= $sale_round_off - $vt_adjust_round_off;

					$order_update = $this->db_operations->data_update('sales_master',array('sale_allocated_amt' => $total_allocated_amt,'sale_round_off' => $total_roundoff_amt),'sale_id',$value['vt_sale_id']);
				}
			}
		}
		elseif ($type == 6) 
		{
			foreach ($record['voucher_trans_data'] as $key => $value) 
			{
				if($value['vt_per_id'] > 0)
				{
					$vt_per_id 				= $value['vt_per_id'];
					$vt_adjust_amt 			= $value['vt_adjust_amt'];

					$vt_adjust_round_off 	= $value['vt_adjust_round_off'];

					$inward_data = array();
					$inward_data = $this->db_operations->get_record("purchase_master", array('per_id' => $value['vt_per_id']));
					
					$per_allocated_amt 		= $inward_data[0]['per_allocated_amt'];
					$per_roundoff 			= $inward_data[0]['per_round_off'];

					$total_allocated_amt 	= $per_allocated_amt - $vt_adjust_amt;
					$total_roundoff_amt 	= $per_roundoff - $vt_adjust_round_off;

					$order_update = $this->db_operations->data_update('purchase_master',array('per_allocated_amt' => $total_allocated_amt,'per_round_off' => $total_roundoff_amt),'per_id',$value['vt_per_id']);
				}
			}
		}
		else
		{	
			foreach ($record['voucher_trans_data'] as $key => $value) 
			{
				if($value['vt_hm_id'] > 0)
				{
					$vt_hm_id 					= $value['vt_hm_id'];
					$vt_hm_adjust_amt 			= $value['vt_adjust_amt'];
					$vt_hm_adjust_round_off 	= $value['vt_adjust_round_off'];

					$hisab_data = array();
					$hisab_data = $this->db_operations->get_record("hisab_master", array('hm_id' => $value['vt_hm_id']));

					$hm_allocated_amt 		= $hisab_data[0]['hm_allocated_amt'];
					$hm_round_off 			= $hisab_data[0]['hm_round_off'];

					$hm_total_allocated_amt 	= $hm_allocated_amt - $vt_hm_adjust_amt;
					$hm_total_roundoff_amt 		= $hm_round_off - $vt_hm_adjust_round_off;

					$hisab_update = $this->db_operations->data_update('hisab_master',array('hm_allocated_amt' => $hm_total_allocated_amt,'hm_round_off' => $hm_total_roundoff_amt),'hm_id',$value['vt_hm_id']);
				}
			}
		}

		$this->db_operations->delete_record("voucher_master", array('vc_id' => $vc_id));
		$this->db_operations->delete_record("voucher_trans", array('vt_vc_id' => $vc_id));
	}


	public function receipt_voucher_update($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;

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

			$voucher_data['vc_entry_date'] 		= date('Y-m-d',strtotime($form_data['vc_entry_date']));
			$voucher_data['vc_notes']         	= $form_data['vc_notes'];
			$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");

			if(isset($form_data['sale_id']))
			{
				$order_data['sale_id'] 					= $form_data['sale_id'];
				$order_data['sale_allocated_amt'] 			= $form_data['sale_allocated_amt'];
				$order_data['sale_adjust_amt'] 			= $form_data['sale_adjust_amt'];

				$voucher_trans['vt_sale_id']             = $form_data['sale_id'];
				$voucher_trans['vt_bill_no']           = $form_data['sale_entry_no'];
				$voucher_trans['vt_bill_date']         = $form_data['sale_entry_date'];
				$voucher_trans['vt_final_total']       = $form_data['sale_total_amt'];
				$voucher_trans['vt_adjust_amt']        = $form_data['sale_adjust_amt'];
				$voucher_trans['vt_adjust_round_off']  = $form_data['sale_round_off'];

			}


			$this->db->trans_begin();

			// 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 (isset($form_data['sale_id'])) 
				{
					$this->receipt_voucher_trans_insert($voucher_trans, $vc_id);
					$this->order_update_from_receipt_voucher($order_data, $vc_id);
				}

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

		}
		else
		{
			redirect('loginctrl/logout');
		}	
	}

/**/
}
?>
