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

	class Invoicectrl extends CI_Controller {
		public $invoicemdl;
		public $excel;
		public $pagination;
		public function __construct()
		{
			parent::__construct();
			$this->load->model('invoice/Invoicemdl','invoicemdl');
			$this->load->library('session');
			$this->load->library('db_operations');
			$this->config->load('extra');
			$this->load->library('pagination');
		}

		public function invoice_master()
		{
			if(sessionExist()) 
			{
				if( $_GET['action'] == 'view' )
				{
					$config 				= array();
					$config 				= $this->config->item('pagination');
					$config['total_rows'] 	= $this->invoicemdl->get_all_invoice_data_cnt();
					$config['base_url'] 	= base_url("invoice/invoicectrl/invoice_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->invoicemdl->get_all_invoice_data($config['per_page'],$offset);

					$this->load->view('pages/invoice/invoice_master', $record);
				}
				else if( $_GET['action'] == 'add' )
				{
					$data = $this->invoicemdl->get_invoice_form_assets();
					// echo "<pre>"; print_r($data);die;
					$this->load->view('pages/invoice/invoice_form', $data);
				}
				else if( $_GET['action'] == 'edit' )
				{
					$inv_id = encrypt_decrypt("decrypt", $_GET['id'], SECRET_KEY);
					$data = $this->invoicemdl->get_single_invoice_data_for_edit($inv_id);
					$this->load->view('pages/invoice/invoice_form', $data);
				}
				else if( $_GET['action'] == 'print' )
				{
					$inv_id = encrypt_decrypt("decrypt", $_GET['id'], SECRET_KEY);
					// $inv_id = $_GET['id'];
					$data = $this->invoicemdl->get_single_invoice_data_for_print($inv_id);
					
					$this->load->view('pdfs/invoice/invoice_pdf', $data);
				}
			}
			else 
			{
				redirect('loginctrl/logout');	
			}
		}

		public function get_product_data($pro_id,$cust_id)
		{
			if(sessionExist())
			{
				// $record = $this->db_operations->get_record('product_master',array("pro_id"=>$pro_id));
				$record = $this->invoicemdl->get_product_data($pro_id,$cust_id);
				echo json_encode($record);
			}
			else
			{
				$this->load->view('pages/login');
			}
		}

		// ============================
		// 		Invoice Insert
		// ============================

		public function invoice_insert($id)
		{
			if(sessionExist()) 
			{
				$formdata = $this->input->post();

				$inv_data['inv_entry_no'] = $formdata['inv_no'];
				$inv_data['inv_entry_date'] = date('Y-m-d', strtotime($formdata['inv_entry_date']));
				$inv_data['inv_invoice_date'] = date('Y-m-d', strtotime($formdata['inv_invoice_date']));
				$inv_data['inv_customer_id'] = $formdata['cust_id'];
				$inv_data['inv_dispatch_by'] = $formdata['inv_dispatch_by'];
				$inv_data['inv_terms']      = $formdata['inv_terms'];
				$inv_data['inv_addon']      = $formdata['inv_addon'];
				$inv_data['inv_acc_type'] = $formdata['acc_type'];
				$inv_data['inv_total_cgst'] = $formdata['total_inv_cgst'];
				$inv_data['inv_total_sgst'] = $formdata['total_inv_sgst'];
				$inv_data['inv_total_igst'] = $formdata['total_inv_igst'];
				$inv_data['inv_total_qty'] = $formdata['total_qty'];
				$inv_data['inv_total_rate'] = $formdata['total_rate'];
				$inv_data['inv_sub_total_amt'] = $formdata['sub_total'];
				$inv_data['inv_total_amt'] = $formdata['final_total'];
				$inv_data['inv_addon_for_self'] = $formdata['inv_addon_for_self'];

				$this->db->trans_begin();
				if($id == 0)
				{
					$inv_data['inv_create_userid'] = $_SESSION['user_id'];
					$inv_data['inv_create_date'] = date("Y-m-d H:i:s");
					$inv_data['inv_fin_year'] = $_SESSION['fin_year'];

					$result = $this->db_operations->data_insert('invoice_master', $inv_data);

					if(!empty($result))
					{
						$trans_result 	= $this->insert_update_invoice_trans($formdata, $result);
						if(!empty($trans_result))
						{
							if ($this->db->trans_status() === FALSE)
							{
							    $this->db->trans_rollback();
							    $response['flag'] = 0;
								echo json_encode($response);
							}
							else
							{
							    $this->db->trans_commit();
							   	$response['flag'] = 1;
							 	$response['last_id'] = encrypt_decrypt("encrypt", $result, SECRET_KEY);
								echo json_encode($response);
							}
						}
					}
					else
					{
						$this->db->trans_commit();
						$response['flag'] = 0;
						echo json_encode($response);
					}
				}
				else
				{
					$inv_data['inv_update_userid'] = $_SESSION['user_id'];
					$inv_data['inv_update_date'] = date("Y-m-d H:i:s");
					
					$result = $this->db_operations->data_update('invoice_master', $inv_data, 'inv_id',$id);

					if(!empty($result))
					{
						$trans_result 	= $this->insert_update_invoice_trans($formdata, $id);
						if(!empty($trans_result))
						{
							if ($this->db->trans_status() === FALSE)
							{
							    $this->db->trans_rollback();
							    $response['flag'] = 0;
								echo json_encode($response);
							}
							else
							{
							    $this->db->trans_commit();
							   	$response['flag'] = 1;
							 	$response['last_id'] = encrypt_decrypt("encrypt", $id, SECRET_KEY);
								echo json_encode($response);
							}
						}
					}
					else
					{
						$this->db->trans_commit();
						$response['flag'] = 0;
						echo json_encode($response);
					}
				}

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

		// ====

		public function insert_update_invoice_trans($formdata, $inv_id)
		{
			$flag = 1;
			$inv_trans_data = $this->db_operations->get_record('invoice_trans',array('invt_inv_id' =>$inv_id));
			foreach($inv_trans_data as $key => $value)
    		{
      			if(!in_array( $value['invt_id'], $formdata['inv_id'] ) )
      			{
        			$this->db_operations->delete_record('invoice_trans',array('invt_id' =>$value['invt_id']));
     			}
    		}
			foreach ($formdata['inv_id'] as $key => $value)
			{
				$inv_trans['invt_inv_id'] 	= $inv_id;
				$inv_trans['invt_pro_id'] 	= $formdata['pro_id'][$key];
				$inv_trans['invt_hsn_code'] = $formdata['hsn_code'][$key];
				$inv_trans['invt_qty'] 		= $formdata['inv_qty'][$key];
				$inv_trans['invt_rate'] 	= $formdata['inv_rate'][$key];
				$inv_trans['invt_per_cgst'] = $formdata['inv_cgst'][$key];
				$inv_trans['invt_amt_cgst'] = $formdata['inv_cgst_total'][$key];
				$inv_trans['invt_per_sgst'] = $formdata['inv_sgst'][$key];
				$inv_trans['invt_amt_sgst'] = $formdata['inv_sgst_total'][$key];
				$inv_trans['invt_per_igst'] = $formdata['inv_igst'][$key];
				$inv_trans['invt_amt_igst'] = $formdata['inv_igst_total'][$key];			
				$inv_trans['invt_sub_total_amt']= $formdata['inv_sub_total'][$key];
				$inv_trans['invt_total_amt']= $formdata['inv_total'][$key];

				if($value == 0)
				{
					$inv_trans_result = $this->db_operations->data_insert('invoice_trans', $inv_trans);

					if(!empty($inv_trans_result))
					{
						$flag = 1;
					}
				}else
				{
					$inv_trans_result = $this->db_operations->data_update('invoice_trans', $inv_trans, 'invt_id', $formdata['inv_id'][$key]);

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

			if($flag == 1)
			{
				return 1;
			}else
			{
				return 0;
			}
		}
		
		public function delete_invoice($id)
		{
		    if(sessionExist())
			{
				$this->db_operations->delete_record('invoice_master', array('inv_id' => $id));
				$this->db_operations->delete_record('invoice_trans', array('invt_inv_id' => $id));
			}
			else
			{
				$this->load->view('pages/login');
			}
		}

		// ================================
	}

?>
