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


class Import extends CI_Controller
{
	public $addr_regex		= '/^[0-9a-zA-Z\s\-\)\(\,\/\&\#\@\.\+]+$/';
	public $addr_regex_msg	= 'can only contains alphanumeric characters with special characters (- / : @ # & ,).';
	public $login_regex		= '/^[0-9a-zA-Z\@\.]+$/';
	public $login_regex_msg	= 'can only contains alphanumeric characters with special characters (@ .).';
	public $person_name_rgx	= '/^[0-9a-zA-Z\.\s\-\)\(\&]+$/';
	public $person_name_rgx_msg	= 'can only contains alphanumeric characters with special characters (- /)';
	public $alpha			= '/^[a-zA-Z]+$/i';
	public $alpha_msg		= 'Contain only alphabets';
	public $alphanumeric	= '/^[a-zA-Z0-9]+$/i';
	public $numeric			= '/^[0-9]+$/i';
	public $numeric_msg		= 'Contain only Numeric Value';
	public $decimal			= '/^(\d*\.)?\d+$/i';
	public $decimal_msg		= 'Contain only decimal value';
	public $date            = '/^\d{2}-\d{2}-\d{4}$/';
	public $date_msg        = 'Invalid Date Format';
	public $datetime        = '\d{2}-\d{2}-\d{4}\s\d{2}:\d{2}:\d{2}';
	public $datetime_msg    = 'Invalid Datetime Format';
	public $radiovalarr		= array('YES', 'NO');
	public $emptyvalarr		= array('', 'N/A', 'NULL', 'Not Applicable', 'NONE');
	public $commonMasters	= array(
		'MASTER_FEE_TYPE',
		'MASTER_TAX_TYPE',
		'MASTER_PAYMENT_MODE',
		'MASTER_QUALIFICATION',
		'MASTER_RELATION',
		'MASTER_DOCUMENT_TYPE',
		'MASTER_REFERENCE_TYPE',
		'MASTER_FORM',
	);
	function __construct()
	{
		parent::__construct();
		error_reporting(0);
		$this->fx->validateCORS();

		$this->load->model('Import_Model');
		$this->convertHeaderToPost($this->input->request_headers());
		// $this->fx->validate();
		// fx::pr('dsdf',1);
	}



	private function convertHeaderToPost($headerData)
	{
		foreach ($headerData as $key => $value) {
			if (strtolower($key) == 'token' || strtolower($key) == 'loginsession' || strtolower($key) == 'isloginwithmaster') {
				$_POST[strtolower($key)] = $value;
				// break;
			}
		}
	}

	public function response($success, $message, $status_code = 200, $data = array())
	{
		$response = array(
			'success'		=> $success,
			'message'		=> $message,
			'result'		=> (count($data) < 1) ? new stdClass() : $data,
			'status_code'	=> $status_code,
		);
		echo json_encode($response);
		exit;
	}



	public function doUpload($file_name, $folder)
	{
		$config['upload_path'] = "$folder";
		$config['allowed_types'] = 'csv';
		$config['max_size'] = 5000;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload($file_name)) {
			$error = $this->upload->display_errors();
			return array('success' => false, 'error' => $error);
		} else {
			$data = $this->upload->data();
			return array('success' => true, 'file_name' => $data['file_name']);
		}
	}
	public function get_loginsession($session_id)
	{
		$this->loginsession = $this->decode($session_id);
	}

	private function encode($string)
	{
		return $this->fx->encrypt_decrypt('encrypt', $string);
	}

	private function decode($string)
	{
		return $this->fx->encrypt_decrypt('decrypt', $string);
	}

	function setData($token, $default_cont = false)
	{
		$array = explode("-", $this->decode($token));
		if (empty($array[0])) {
			return;
		}


		$query = $this->db->get_where('tms_user_master', array('user_id' => $this->db->escape_str($array[0])));
		if ($query->num_rows() > 0) {
			$row = $query->row();
			if ($row->full_control != 1) {
				$query1 = $this->db->get_where('tms_user_type', array('id' => $this->db->escape_str($array[3])));
				$row1 = $query1->row();
				if ($row1->status == 0) {
					$this->api_response(['success' => false, 'status_code' => 402, 'message' => 'User Type is In Active'], 402);
				}
				if ($row->status == 0) {
					$this->api_response(['success' => false, 'status_code' => 402, 'message' => 'In Active User'], 402);
				}
			}

			if ($row->is_login == 1 && (strtolower($this->input->post('isloginwithmaster')) !== "false" ||  ($this->loginsession == $row->login_session || $default_cont))) {
			} else {

				$this->api_response(['success' => false, 'status_code' => 402, 'message' => 'Token Expired', 'loginsession' => $row->login_session], 402);
			}
		} else {
			$this->api_response(['success' => false, 'status_code' => 402, 'message' => 'Token Expired'], 402);
		}
		$this->user_id = $this->db->escape_str($array[0]);
		$this->user_email = $this->db->escape_str($array[1]);
		$this->user_mobile = $this->db->escape_str($array[2]);
		$this->user_type = $this->db->escape_str($array[3]);
		$this->full_control = $this->db->escape_str($array[4]);
		$this->queue_perm_id = $this->db->escape_str($array[5]);
		$this->maskEmail = $this->db->escape_str($array[6]);
		$this->maskPhone = $this->db->escape_str($array[7]);
		$this->manager_id = $this->db->escape_str($array[10]);
		$this->allTicket = $this->db->escape_str($array[11]);
		$this->priority_change_flag = $this->db->escape_str($array[12]);
		$this->open_to_queue = $this->db->escape_str($array[13]);
		$this->name = $this->db->escape_str($array[14]);


		return true;
	}
	function index()
	{

		try {


			$token = $this->input->post('token');
			$this->get_loginsession($this->input->post('loginsession'));
			if ($this->setData($token) == false)
				return;


			$fcpath = str_replace("\\", "/", FCPATH);
			$uploadFolder = $fcpath . "uploads/documents/";

			if (!file_exists($uploadFolder)) {
				mkdir($uploadFolder, 0777, true);
			}
			$post = (array) json_decode(file_get_contents("php://input"));


			if (empty($post['data_file'])) {
				$this->response(FALSE, 'Please upload a file', 400);
				return;
			}
			if ($post['size'] == 0) {
				$this->response(FALSE, 'Please upload some file', 400);
				return;
			}
			// $docdata = $this->doUpload('data_file', $uploadFolder);

			// if ($docdata['success'] == FALSE) {
			// 	$this->response(FALSE, $docdata['error'], 400);
			// 	return;
			// }
			$this->load->library('upload');

			if (!empty($post['fname'])) {
				$config['upload_path'] =  "uploads/documents";
				$config['allowed_types'] = 'csv';
				$config['max_size'] = 2097152;
				$folderPath = $uploadFolder;
				$this->upload->initialize($config);
				$attachmentSize = $post['size'];

				if ($attachmentSize > $config['max_size']) {
					throw new Exception("Total size of attachments cannot be greater than 2MB");
				} else {
					$this->upload->do_upload($post['file']);
					$image_parts = explode(";base64,", $post['file']);
					$image_base64 = base64_decode($image_parts[1]);
					$file = $folderPath . $post['fname'];
					$dbData['con_type_img'] = $post['fname'];

					if (file_put_contents($file, $image_base64)) {
					} else {
						throw new Exception('File Not Uploaded');
					}
				}
			} else {
				if ($post['removeFlag'] == 1) {
					$dbData['con_type_img'] = '';
				} else {
					unset($dbData['con_type_img']);
				}
			}

			$data = array(
				'import_type'	=> $post['import_type'],
				'document_path'	=> str_replace($fcpath, '', $uploadFolder),
				'doc_name'		=> $post['fname'],
				'status'		=> 1,
				'queue_id' => isset($post['queue_id']) ? $post['queue_id'] : 0,
			);
			$log_id = $this->Import_Model->addNewData($data);
			if ($log_id <= 0) {
				$this->response(FALSE, 'Oops! something went wrong', 400);
				return;
			}


			$fcpath = str_replace("\\", "/", FCPATH);
			// fx::pr($data,1);
			if ($data['import_type'] == 'getCategoryList') {
				$this->getCategoryList($log_id,	$this->user_id,	$post['queue_id']);
				die;
				$this->fx->runPhpCommand($fcpath . "index.php import getCategoryList " . $log_id . " " . $this->user_id . " " . $post['queue_id']);
			} else if ($data['import_type'] == 'MASTER_STATES') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterState " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_VISA_TYPE') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterVisaType " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_BRANCH') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterBranch " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_PROCESS_CHECKLIST') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterChecklist " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_ROLES') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterUserRoles " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_USER') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterUsers " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_COLLEGE') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterCollege " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_AFTER_VISA_CHECKLIST') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterAfterVisaChecklist " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_COURSE') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterCourse " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_BANK') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterBank " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_INVOICE_TYPE') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterInvoiceType " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_CURRENCY') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterCurrency " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_OTHER_CHARGES') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterOtherCharges " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_CLIENT_TYPE') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterClientType " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_CLIENT') {
				// $this->importMasterClient($log_id, $this->fx->userId);
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterClient " . $log_id . " " . $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_TAX') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterTax " . $log_id . " " . $this->fx->userId . " " . $data['import_type']);
			} else if ($data['import_type'] == 'CASES_MASTER') {
				$this->fx->runPhpCommand($fcpath . "index.php import importCases " . $log_id . " " . $this->fx->userId . " " . $data['import_type']);
			} else if ($data['import_type'] == 'LEAD_MASTER') {
				$this->fx->runPhpCommand($fcpath . "index.php import importLead " . $log_id . " " . $this->fx->userId . " " . $data['import_type']);
				// $this->importLead($log_id, $this->fx->userId);
			} else if ($data['import_type'] == 'MASTER_FILE_STATUS') {
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterFileStatus " . $log_id . " " . $this->fx->userId . " " . $data['import_type']);
			} else if ($data['import_type'] == 'MASTER_FEE_STRUCTURE_ITEMS') {
				$fee_structure_id = $this->input->post('id');
				$this->fx->runPhpCommand($fcpath . "index.php import importMasterFeeStructure " . $log_id . " " . $this->fx->userId . " " . $data['import_type'] . " " . $fee_structure_id);
			} else if (in_array($data['import_type'], $this->commonMasters)) {
				$this->fx->runPhpCommand($fcpath . "index.php import importMaster " . $log_id . " " . $this->fx->userId . " " . $data['import_type']);
			}
			$this->response(TRUE, 'File Uploaded', 200);
		} catch (Exception $e) {
			$this->response(false,  $e->getMessage(), 400);
			// Handle the exception here, you can log it or return an error response.

		}
	}

	function addImportStartLog($log_id)
	{
		$this->Import_Model->addNewData(
			array(
				'processing_start_time' => date('Y-m-d H:i:s'),
				'process_status' => 2
			),
			array('log_id' => $log_id)
		);
	}

	function addImportEndLog($log_id, $dataLogInst, $masterName, $csvResultedColumns, $failedDataArray)
	{
		$errorFileName = '';
		if (count($failedDataArray) > 0) {
			$fcpath = str_replace("\\", "/", FCPATH);
			$uploadFolder	= $fcpath . "uploads/documents";
			$errorFileName	= strtolower($masterName) . "_import_$log_id.csv";
			$errorFilePath	= $uploadFolder . "/" . $errorFileName;
			$fp = fopen($errorFilePath, 'w');
			fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
			foreach ($failedDataArray as $key => $value) {
				fputcsv($fp, $value);
			}
			fclose($fp);
		}
		$dataLogInst = array_merge(
			$dataLogInst,
			array(
				'processing_end_time' => date('Y-m-d H:i:s'),
				'process_status' => 3,
				'error_file' => $errorFileName
			)
		);

		$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
	}

	function getSampleFileColumns($masterName)
	{
		$importtype	= strtolower($masterName);
		$sampleFileName	= $importtype . '.csv';
		$sampleFilePath	= FCPATH . 'assets/download/import_master/' . $sampleFileName;
		$sampleFile		= fopen($sampleFilePath, "r");
		$csvResultedColumns = array();

		$data = fgetcsv($sampleFile, 100, ",");
		foreach ($data as $key => $val) {
			if (empty($val) || trim($val) == '') break;
			$csvResultedColumns[] = $val;
		}
		return $csvResultedColumns;
	}

	function importLead($log_id, $user_id)
	{
		// echo 'her';die;
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);

			$usersArray = array();
			$usersDBArray = $this->Import_Model->getUsersList();
			foreach ($usersDBArray as $key => $value) {
				$usersArray[strtolower($value['name'])] = $value['id'];
			}

			$stateArray = array();
			$stateDBArray = $this->Import_Model->getAllStateList();
			foreach ($stateDBArray as $key => $value) {
				$stateArray[strtolower($value['state_name'])] = $value['state_code'];
			}

			$branchArray = array();
			$branchDBArray = $this->Import_Model->getBranchList();
			foreach ($branchDBArray as $key => $value) {
				$branchArray[strtolower($value['branch_name'])] = $value['branch_id'];
			}

			$visaTypeArray = array();
			$visaTypeDBArray = $this->Import_Model->getVisaTypeList();
			foreach ($visaTypeDBArray as $key => $value) {
				$visaTypeArray[strtolower($value['title'])] = $value['id'];
			}

			$countryArray		= array();
			$countryDBArray	= $this->Import_Model->getAllCountryList();
			foreach ($countryDBArray as $key => $value) {
				$countryArray[strtolower($value['country_name'])] = $value['country_id'];
			}

			$leadCategoryArray		= array();
			$leadCategoryDBArray	= $this->Import_Model->getLeadCategoryList();
			foreach ($leadCategoryDBArray as $key => $value) {
				$leadCategoryArray[strtolower($value['lead_category_name'])] = $value['lead_category_id'];
			}

			$leadTypeArray		= array();
			$leadTypeDBArray	= $this->Import_Model->getLeadTypeList();
			foreach ($leadTypeDBArray as $key => $value) {
				$leadTypeArray[strtolower($value['lead_type_name'])] = $value['lead_type_id'];
			}

			$leadSourceArray		= array();
			$leadSourceDBArray	= $this->Import_Model->getLeadSourceList();
			foreach ($leadSourceDBArray as $key => $value) {
				$leadSourceArray[strtolower($value['lead_source_name'])] = $value['lead_source_id'];
			}

			$leadStatusArray		= array();
			$leadStatusDBArray	= $this->Import_Model->getLeadStatusList();
			foreach ($leadStatusDBArray as $key => $value) {
				$leadStatusArray[strtolower($value['lead_status_name'])] = $value['lead_status_id'];
			}


			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();

			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Branch', 'Lead Date', 'Client Name', 'Select Account', 'Contact Person', 'Mobile', 'Email', 'State', 'Address', 'Lead Category', 'Lead Type', 'Lead Source', 'Lead Status', 'Lead Tags', 'Reference', 'Valid From', 'Valid To', 'Client Notification', 'Close Date', 'Visa Type', 'Country', 'Remarks', 'Status');


			$i = 0;
			$last = 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;

				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}


				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) { // Branch
					$message = "Branch Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[0])), $branchArray)) {
					$message = "Invalid Branch Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) { // lead_date
					$message = "Lead Date is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} elseif (!preg_match($this->date, trim($data[1]))) {
					$message = "Invalid Lead Date Format";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) { //client name
					$message = "Client Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[2]))) {
					$message = "Client Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[2]) > 100 || strlen($data[2]) < 2) {
					$message = "Client Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[3]) || in_array(trim($data[3]), $this->emptyvalarr)) { // select account
					$message = "Select Account is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[3])), $usersArray)) {
					$message = "Invalid Select Account";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[4]) || in_array(trim($data[4]), $this->emptyvalarr)) { // contact person
					$message = "Contact Person is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[4]))) {
					$message = "Contact Person " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[4]) > 100 || strlen($data[4]) < 2) {
					$message = "Contact Person must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				//MOBILE NO.
				if (empty($data[5]) || in_array(trim($data[5]), $this->emptyvalarr)) {
					$message = "Mobile No. is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[5]))) {
					$message = "Mobile No. " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[5]) != 10) {
					$message = "Mobile No must be 10 digit length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				//EMAIL ID
				if (empty($data[6]) || in_array(trim($data[6]), $this->emptyvalarr)) {
					$message = "Email Id is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (filter_var($data[6], FILTER_VALIDATE_EMAIL) === false) {
					$message = "Email Id is not valid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[6]) > 100) {
					$message = "Email Id must not be greater than 100 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[7]) || in_array(trim($data[7]), $this->emptyvalarr)) { // State
					$message = "State Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[7])), $stateArray)) {
					$message = "Invalid State Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[8]) || in_array(trim($data[8]), $this->emptyvalarr)) { // Address
					$message = "Address is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[9]) || in_array(trim($data[9]), $this->emptyvalarr)) { // lead category
					$message = "Lead Category is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[9])), $leadCategoryArray)) {
					$message = "Invalid Lead Category";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[10]) || in_array(trim($data[10]), $this->emptyvalarr)) { // lead type
					$message = "Lead Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[10])), $leadTypeArray)) {
					$message = "Invalid Lead Type";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[11]) || in_array(trim($data[11]), $this->emptyvalarr)) { // lead source
					$message = "Lead Source is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[11])), $leadSourceArray)) {
					$message = "Invalid Lead Source";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[12]) || in_array(trim($data[12]), $this->emptyvalarr)) { // lead status
					$message = "Lead Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[12])), $leadStatusArray)) {
					$message = "Invalid Lead Status";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$valid_from = strtotime(date_format(date_create($data[15]), "Y-m-d"));
				$valid_to = strtotime(date_format(date_create($data[16]), "Y-m-d"));

				if ($valid_from > $valid_to) {
					$message = "Valid from date can't be greater than valid to date";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$clientNotification = array('email', 'whatsapp', 'sms');

				if (!empty($data[17])) {
					if (!in_array(strtolower(trim($data[17])), $clientNotification)) {
						$message = "Client Notification is Invalid";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (!empty($data[18])) {
					if (!preg_match($this->date, trim($data[18]))) {
						$message = "Invalid Close Date Format";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}


				if (empty($data[19]) || in_array(trim($data[19]), $this->emptyvalarr)) { // visa_type
					$message = "Visa Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[20])), $visaTypeArray)) {
					$message = "Invalid Visa Type";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[20]) || in_array(trim($data[20]), $this->emptyvalarr)) { // country
					$message = "Country is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[20])), $countryArray)) {
					$message = "Invalid Country";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$status = ['Active', 'Inactive'];

				if (!empty($data[22])) {
					if (!in_array(ucfirst(strtolower(trim($data[22]))), $status)) {
						$message = "Status is Invalid";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}


				$dataArray	= array();
				$dataArray['branch_id']	= $branchArray[strtolower(trim($data[0]))];
				$dataArray['lead_date'] = date_format(date_create($data[1]), 'Y-m-d');
				$dataArray['acc_head']	= trim($data[2]);
				$dataArray['agent_id']	= $usersArray[strtolower(trim($data[3]))];
				$dataArray['acc_head_contact_person']	= trim($data[4]);
				$dataArray['acc_head_mobile'] = trim($data[5]);
				$dataArray['acc_head_email'] = trim($data[6]);
				$dataArray['acc_head_state_code']	= $stateArray[strtolower(trim($data[7]))];
				$dataArray['acc_head_address'] = trim($data[8]);
				$dataArray['lead_category_id'] = $leadCategoryArray[strtolower(trim($data[9]))];
				$dataArray['lead_type_id'] = $leadTypeArray[strtolower(trim($data[10]))];
				$dataArray['lead_source_id'] = $leadsSourceArray[strtolower(trim($data[11]))];
				$dataArray['lead_status_id'] = $leadStatusArray[strtolower(trim($data[12]))];
				$dataArray['tags']	= !empty(trim($data[13])) ? trim($data[13]) : NULL;
				$dataArray['refer_by']	= !empty(trim($data[14])) ? trim($data[14]) : NULL;
				$dataArray['valid_from']	= date_format(date_create($data[15]), 'Y-m-d');
				$dataArray['valid_to']	= date_format(date_create($data[16]), 'Y-m-d');
				$dataArray['notify_client_via']	= !empty(trim($data[17])) ? strtolower(trim($data[17])) : NULL;
				$dataArray['close_date']	=  !empty(trim($data[18])) ? date_format(date_create($data[18]), 'Y-m-d') : NULL;
				$dataArray['lead_visa_id']	= $visaTypeArray[strtolower(trim($data[19]))];
				$dataArray['lead_country_id']	= $countryArray[strtolower(trim($data[20]))];
				$dataArray['notes'] = ucfirst(strtolower(trim($data[21])));
				$dataArray['status'] = (strtolower(trim($data[22])) == 'active') ? 1 : 2;
				$dataArray['cr_usr'] = $user_id;
				$dataArray['cr_date'] = date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_LEAD');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Lead details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "lead_master_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importCases($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);

			$clientArray = array();
			$clientDBArray = $this->Import_Model->getAllClientList();
			foreach ($clientDBArray as $key => $value) {
				$clientArray[strtolower($value['client_name'])] = $value['client_id'];
			}

			$stateArray = array();
			$stateDBArray = $this->Import_Model->getAllStateList();
			foreach ($stateDBArray as $key => $value) {
				$stateArray[strtolower($value['state_name'])] = $value['state_code'];
			}

			$maritalArray = array();
			$maritalDBArray = $this->Import_Model->getAllMaritalStatusList();
			foreach ($maritalDBArray as $key => $value) {
				$maritalArray[strtolower($value['title'])] = $value['id'];
			}

			$branchArray = array();
			$branchDBArray = $this->Import_Model->getBranchList();
			foreach ($branchDBArray as $key => $value) {
				$branchArray[strtolower($value['branch_name'])] = $value['branch_id'];
			}

			$visaTypeArray = array();
			$visaTypeDBArray = $this->Import_Model->getVisaTypeList();
			foreach ($visaTypeDBArray as $key => $value) {
				$visaTypeArray[strtolower($value['title'])] = $value['id'];
			}

			$countryArray		= array();
			$countryDBArray	= $this->Import_Model->getAllCountryList();
			foreach ($countryDBArray as $key => $value) {
				$countryArray[strtolower($value['country_name'])] = $value['country_id'];
			}

			$fileStatusArray		= array();
			$fileStatusDBArray	= $this->Import_Model->getAllFileStatusList();
			foreach ($fileStatusDBArray as $key => $value) {
				$fileStatusArray[strtolower($value['title'])] = $value['id'];
			}


			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();

			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Registration Date', 'Branch Name', 'Case Expiry Date', 'Visa Type', 'Country', 'Have Passport(Yes/No)', 'Case Type(Fresh/Refuse)', 'File Status', 'Name', 'Bill To', 'Date of Birth', 'Gender', 'Email', 'Mobile', 'Alternate Mobile', 'Telephone', 'Address', 'State', 'Marital Status', 'Details of any Visa Held / Rejected', 'Value of Property', 'Value of FDR\'s', 'Value of Jewellery', 'Others', 'Father Name', 'Mother Name', 'Parents Phone Number');


			$i = 0;
			$last = 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;

				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}

				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) { // case_registration_date
					$message = "Case Registration Date is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} elseif (!preg_match($this->date, trim($data[0]))) {
					$message = "Invalid Case Registration Date Format";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) { // Branch
					$message = "Branch Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[1])), $branchArray)) {
					$message = "Invalid Branch Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) { // case_expiry_date
					$message = "Case Expiry Date is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} elseif (!preg_match($this->date, trim($data[2]))) {
					$message = "Invalid Case Expiry Date Format";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[3]) || in_array(trim($data[3]), $this->emptyvalarr)) { // visa_type
					$message = "Visa Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[3])), $visaTypeArray)) {
					$message = "Invalid Visa Type";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (empty($data[4]) || in_array(trim($data[4]), $this->emptyvalarr)) { // country
					$message = "Country is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[4])), $countryArray)) {
					$message = "Invalid Country";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$passport = array('Yes', 'No');

				if (empty($data[5]) || in_array(trim($data[5]), $this->emptyvalarr)) { // have_passport
					$message = "Have Passport Feild is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!in_array(ucfirst(strtolower(trim($data[5]))), $passport)) {
					$message = "Have Passport Feild is Invalid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$case_type = array('Fresh', 'Refuse');

				if (empty($data[6]) || in_array(trim($data[6]), $this->emptyvalarr)) { // case_type
					$message = "Case Type Feild is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!in_array(ucfirst(strtolower(trim($data[6]))), $case_type)) {
					$message = "Case Type is Invalid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[7]) || in_array(trim($data[7]), $this->emptyvalarr)) { // File Status
					$message = "File Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[7])), $fileStatusArray)) {
					$message = "Invalid File Status";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[8]) || in_array(trim($data[8]), $this->emptyvalarr)) {
					$message = "Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[8]))) {
					$message = "Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[8]) > 100 || strlen($data[8]) < 2) {
					$message = "Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (trim($data[9]) != '') {
					if (!array_key_exists(strtolower(trim($data[9])), $clientArray)) {
						$message = "Invalid Bill To Name";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (empty($data[10]) || in_array(trim($data[10]), $this->emptyvalarr)) {
					$message = "Date of Birth is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} elseif (!preg_match($this->date, trim($data[10]))) {
					$message = "Invalid Date of Birth Format";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dob = date('Y-m-d', strtotime(trim($data[10])));
				$gender = array('Male', 'Female');

				if (empty($data[11]) || in_array(trim($data[11]), $this->emptyvalarr)) {
					$message = "Gender is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!in_array(ucfirst(strtolower(trim($data[11]))), $gender)) {
					$message = "Gender is Invalid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!preg_match($this->alpha, trim($data[11]))) {
					$message = "Gender " . $this->alpha_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				//EMAIL ID
				if (empty($data[12]) || in_array(trim($data[12]), $this->emptyvalarr)) {
					$message = "Email Id is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (filter_var($data[12], FILTER_VALIDATE_EMAIL) === false) {
					$message = "Email Id is not valid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[12]) > 100) {
					$message = "Email Id must not be greater than 100 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				//MOBILE NO.
				if (empty($data[13]) || in_array(trim($data[13]), $this->emptyvalarr)) {
					$message = "Mobile No. is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[13]))) {
					$message = "Mobile No. " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[13]) != 10) {
					$message = "Mobile No must be 10 digit length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				// ALTERNATE MOBILE NO.
				if (trim($data[14]) != '') {
					if (!preg_match($this->numeric, trim($data[14]))) {
						$message = "Alternate Mobile No. " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[14]) != 10) {
						$message = "Alternate Mobile No. must be 10 digit length.";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				// Telephone
				if (trim($data[15]) != '') {
					if (!preg_match($this->numeric, trim($data[15]))) {
						$message = "Telephone " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					// else if (strlen($data[15]) != 10) {
					// 	$message = "Telephone must be 10 digit length.";
					// 	$failedDataArray[] = array_merge($data, array($message));
					// 	continue;
					// }
				}

				if (empty($data[16]) || in_array(trim($data[16]), $this->emptyvalarr)) { // Address
					$message = "Address is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[17]) || in_array(trim($data[17]), $this->emptyvalarr)) { // State
					$message = "State Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[17])), $stateArray)) {
					$message = "Invalid State Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[18]) || in_array(trim($data[18]), $this->emptyvalarr)) { // Marital Status
					$message = "Marital Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[18])), $maritalArray)) {
					$message = "Invalid Marital Status";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (trim($data[20]) != '' && !preg_match($this->numeric, trim($data[20]))) { // property
					$message = "Value of Property " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (trim($data[21]) != '' && !preg_match($this->numeric, trim($data[21]))) { // Value of FDR's
					$message = "Value of FDR's " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (trim($data[22]) != '' && !preg_match($this->numeric, trim($data[22]))) { // Value of Jewellery
					$message = "Value of Jewellery " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (trim($data[23]) != '' && !preg_match($this->numeric, trim($data[23]))) { // Others
					$message = "Others " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[24]) || in_array(trim($data[24]), $this->emptyvalarr)) { // Father Name
					$message = "Father Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[24]))) {
					$message = "Father Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[24]) > 100 || strlen($data[24]) < 2) {
					$message = "Father Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[25]) || in_array(trim($data[25]), $this->emptyvalarr)) { // Mother Name
					$message = "Mother Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[25]))) {
					$message = "Mother Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[25]) > 100 || strlen($data[25]) < 2) {
					$message = "Mother Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				// PARENT MOBILE NO.
				if (trim($data[26]) != '') {
					if (!preg_match($this->numeric, trim($data[26]))) {
						$message = "Parent Mobile No. " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[26]) != 10) {
						$message = "Parent Mobile No. must be 10 digit length.";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				# procedural
				$age = date_diff(date_create($dob), date_create('today'))->y;

				$dataArray	= array();
				$dataArray['case_registration_date'] = date_format(date_create($data[0]), 'Y-m-d H:i:s');
				$dataArray['case_branch_id']	= $branchArray[strtolower(trim($data[1]))];
				$dataArray['case_expiry_date']	= date_format(date_create($data[2]), 'Y-m-d H:i:s');
				$dataArray['case_visa_id']	= $visaTypeArray[strtolower(trim($data[3]))];
				$dataArray['case_country_id']	= $countryArray[strtolower(trim($data[4]))];
				$dataArray['case_have_passport'] = ucfirst(strtolower(trim($data[5])));
				$dataArray['case_type'] = ucfirst(strtolower(trim($data[6])));
				$dataArray['case_file_status'] = $fileStatusArray[strtolower(trim($data[7]))];
				$dataArray['case_client_name']	= trim($data[8]);
				$dataArray['case_bill_to']	= $clientArray[strtolower(trim($data[9]))];
				$dataArray['case_client_dob'] = date_format(date_create($dob), 'Y-m-d H:i:s');
				$dataArray['case_client_age'] = $age;
				$dataArray['case_gender'] = ucfirst(strtolower(trim($data[11])));
				$dataArray['case_client_email'] = trim($data[12]);
				$dataArray['case_client_number'] = trim($data[13]);
				$dataArray['case_client_alt_number'] = !empty(trim($data[14])) ? trim($data[14]) : NULL;
				$dataArray['case_client_telephone'] = !empty(trim($data[15])) ? trim($data[15]) : NULL;
				$dataArray['case_client_address'] = trim($data[16]);
				$dataArray['case_client_state']	= $stateArray[strtolower(trim($data[17]))];
				$dataArray['case_client_marital_status'] = $maritalArray[strtolower(trim($data[18]))];
				$dataArray['case_client_detail_of_visa'] = !empty(trim($data[19])) ? trim($data[19]) : NULL;
				$dataArray['case_client_property_value'] = trim($data[20]);
				$dataArray['case_client_fdrs_value'] = trim($data[21]);
				$dataArray['case_client_jewellery_value'] = trim($data[22]);
				$dataArray['case_client_other_value'] = trim($data[23]);
				$dataArray['case_client_father_name'] = trim($data[24]);
				$dataArray['case_client_mother_name'] = trim($data[25]);
				$dataArray['case_client_parent_number'] = !empty(trim($data[26])) ? trim($data[26]) : NULL;
				$dataArray['cr_usr'] = $user_id;
				$dataArray['cr_date'] = date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_CASES');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Case details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "case_master_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterTax($log_id, $user_id, $masterName)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->addImportStartLog($log_id);

			$taxTypeArray = array();
			$taxTypeDBArray = $this->Import_Model->getAllTaxTypeList();
			foreach ($taxTypeDBArray as $key => $value) {
				$taxTypeArray[strtolower($value['title'])] = $value['id'];
			}

			$csvResultedColumns	= $this->getSampleFileColumns($masterName);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");
			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[0] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[0]))) {
					$message = $csvResultedColumns[0] . " " . $this->addr_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 50 || strlen($data[0]) < 2) {
					$message = $csvResultedColumns[0] . " must be 2 to 50 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[1] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[1])), $taxTypeArray)) {
					$message = "Invalid " . $csvResultedColumns[1];
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[2] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->decimal, trim($data[2]))) {
					$message = $csvResultedColumns[2] . " " . $this->decimal_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[2]) >= 100 || strlen($data[2]) < 0) {
					$message = $csvResultedColumns[2] . " must lie between 0 and 100 .";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$title			= addslashes(trim($data[0]));
				$tax_type_id	= $taxTypeArray[strtolower($data[1])];
				$tax_percentage	= round(trim($data[2]), 2);


				$arrkey = array_search($title, array_column($dataInsrt, 'title'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "title='" . $title . "'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, $masterName);
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['title']			= $title;
				$dataArray['tax_type_id']	= $tax_type_id;
				$dataArray['tax_percentage'] = $tax_percentage;
				$dataArray['cr_usr']		= $user_id;
				$dataArray['cr_date'] 		= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, $masterName);
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Tax Master Details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$this->addImportEndLog($log_id, $dataLogInst, $masterName, $csvResultedColumns, $failedDataArray);
		}
	}

	function importMasterFileStatus($log_id, $user_id, $masterName)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->addImportStartLog($log_id);

			$groupArray = array();
			$groupDBArray = $this->Import_Model->getAllStatusGroupList();
			foreach ($groupDBArray as $key => $value) {
				$groupArray[strtolower($value['title'])] = $value['id'];
			}

			$csvResultedColumns	= $this->getSampleFileColumns($masterName);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");
			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[0] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[0]))) {
					$message = $csvResultedColumns[0] . " " . $this->addr_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = $csvResultedColumns[0] . " must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[1] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[1])), $groupArray)) {
					$message = "Invalid " . $csvResultedColumns[1];
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[2] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!preg_match('/^#?([0-9a-f]{6}|[0-9a-f]{3})$/i', trim($data[2]))) {
					$message = $csvResultedColumns[2] . " must be valid Hex color code";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$title	= addslashes(trim($data[0]));
				$group	= $groupArray[strtolower(trim($data[1]))];
				$color	= trim($data[2]);

				$arrkey = array_search($title, array_column($dataInsrt, 'title'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "title='" . $title . "'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, $masterName);
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['title']				= $title;
				$dataArray['status_group_id']	= $group;
				$dataArray['color_code']		= $color;
				$dataArray['cr_usr']	= $user_id;
				$dataArray['cr_date'] 	= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, $masterName);
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$this->addImportEndLog($log_id, $dataLogInst, $masterName, $csvResultedColumns, $failedDataArray);
		}
	}

	function importMasterFeeStructure($log_id, $user_id, $masterName, $fee_structure_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->addImportStartLog($log_id);

			$feeTypeArray = array();
			$feeTypeDBArray = $this->Import_Model->getAllFeeTypeList();
			foreach ($feeTypeDBArray as $key => $value) {
				$feeTypeArray[strtolower($value['title'])] = $value['id'];
			}
			$csvResultedColumns	= $this->getSampleFileColumns($masterName);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");
			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}

				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[1] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[0])), $feeTypeArray)) {
					$message = "Invalid " . $csvResultedColumns[0];
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[1] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (!preg_match($this->decimal, trim($data[1]))) {
					$message = $csvResultedColumns[1] . " " . $this->decimal_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if ((float)$data[1] < 0) {
					$message = $csvResultedColumns[1] . " can not be negative.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				for ($n = 2; $n <= 9; $n++) {
					if (empty($data[$n]) || in_array(trim($data[$n]), $this->emptyvalarr)) {
						$message = $csvResultedColumns[$n] . " is can not be empty.It must contain YES or NO";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}

					if (!in_array(strtoupper(trim($data[$n])), $this->radiovalarr)) {
						$message = $csvResultedColumns[$n] . " must contain YES or NO";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				$dataArray	= array();
				$dataArray['fee_type_id']	= $feeTypeArray[strtolower(trim($data[0]))];
				$dataArray['amount']		= round(trim($data[1]), 2);
				$dataArray['is_sds']		= (!empty($data[2]) && strtoupper(trim($data[2])) == 'YES') ? 1 : 0;
				$dataArray['is_non_sds']	= (!empty($data[3]) && strtoupper(trim($data[3])) == 'YES') ? 1 : 0;
				$dataArray['is_university_category']	= (!empty($data[4]) && strtoupper(trim($data[4])) == 'YES') ? 1 : 0;
				$dataArray['is_counsellor']		= (!empty($data[5]) && strtoupper(trim($data[5])) == 'YES') ? 1 : 0;
				$dataArray['is_processing']		= (!empty($data[6]) && strtoupper(trim($data[6])) == 'YES') ? 1 : 0;
				$dataArray['is_taxable']		= (!empty($data[7]) && strtoupper(trim($data[7])) == 'YES') ? 1 : 0;
				$dataArray['is_offer_letter']	= (!empty($data[8]) && strtoupper(trim($data[8])) == 'YES') ? 1 : 0;
				$dataArray['is_printable']		= (!empty($data[9]) && strtoupper(trim($data[9])) == 'YES') ? 1 : 0;

				$arrkey = array_search($dataArray['fee_type_id'], array_column($dataInsrt, 'fee_type_id'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "fee_type_id=" . $dataArray['fee_type_id'] . " AND fee_structure_id=" . $fee_structure_id;
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, $masterName);
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray['fee_structure_id']	= $fee_structure_id;
				$dataArray['cr_usr']	= $user_id;
				$dataArray['cr_date'] 	= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, $masterName);
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Fee Details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$this->addImportEndLog($log_id, $dataLogInst, $masterName, $csvResultedColumns, $failedDataArray);
		}
	}

	function importMaster($log_id, $user_id, $masterName)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->addImportStartLog($log_id);

			$csvResultedColumns	= $this->getSampleFileColumns($masterName);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");
			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = $csvResultedColumns[0] . " is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[0]))) {
					$message = $csvResultedColumns[0] . " " . $this->addr_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 250 || strlen($data[0]) < 2) {
					$message = $csvResultedColumns[0] . " must be 2 to 250 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$title	= addslashes(trim($data[0]));
				$arrkey = array_search($title, array_column($dataInsrt, 'title'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "title='" . $title . "'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, $masterName);
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['title']		= $title;
				$dataArray['cr_usr']	= $user_id;
				$dataArray['cr_date'] 	= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, $masterName);
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$this->addImportEndLog($log_id, $dataLogInst, $masterName, $csvResultedColumns, $failedDataArray);
		}
	}

	function importMasterCountry($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Country Name', 'Country Code');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Country Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Country Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "Country Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Country Code is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[1]))) {
					$message = "Invaild Country Code";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} elseif (strlen(trim($data[1])) > 2) {
					$message = "Country Code can't greater then 2";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$country_name	= strtoupper(trim($data[0]));
				$arrkey = array_search($country_name, array_column($dataInsrt, 'country_name'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "country_name='" . $country_name . "'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_COUNTRY');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['country_name']	= $country_name;
				$dataArray['country_code']	= '+' . trim($data[1]);
				$dataArray['cr_usr']		= $user_id;
				$dataArray['cr_date'] 		= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_COUNTRY');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Country details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_country_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterAfterVisaChecklist($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('After Visa Name');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "After Visa Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Country Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "After Visa Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$aftervisa_name	= trim($data[0]);

				$where = "after_visa_name='" . $aftervisa_name . "'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_AFTER_VISA_CHECKLIST');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['after_visa_name'] = $aftervisa_name;
				$dataArray['cr_usr']		  = $user_id;
				$dataArray['cr_date'] 		  = date('Y-m-d H:i:s');
				$dataInsrt[]                  = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_AFTER_VISA_CHECKLIST');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'After Visa details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_after_visa_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterClientType($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Client Type');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Client Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Client Type " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 50 || strlen($data[0]) < 2) {
					$message = "Client Type must be 2 to 50 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				// $where = "after_visa_name='" . $aftervisa_name . "'";
				// $dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_AFTER_VISA_CHECKLIST');
				// if (!empty($dedupeRes)) {
				// 	$message = "Duplicate Entry found in DB";
				// 	$failedDataArray[] = array_merge($data, array($message));
				// 	continue;
				// }

				$dataArray	= array();
				$dataArray['client_type']     = ucfirst(trim($data[0]));
				$dataArray['status']          = 1;
				$dataArray['cr_usr']		  = $user_id;
				$dataArray['cr_date'] 		  = date('Y-m-d H:i:s');
				$dataInsrt[]                  = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_CLIENT_TYPE');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Client Type can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_client_type_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterInvoiceType($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Invoice Type', 'Status', 'Is Default');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Invoice Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Invoice Type " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 50 || strlen($data[0]) < 2) {
					$message = "Invoice Type must be 2 to 50 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$status = ['ACTIVE', 'INACTIVE'];

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!in_array(strtoupper(trim($data[1])), $status)) {
					$message = "Invalid Status";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (!empty(trim($data[2]))) {
					if (trim($data[2]) != '0' && trim($data[2]) != '1') {
						$message = "Invalid Is Default";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				$is_default	= trim($data[2]);

				if ($is_default == '1') {
					$where = "is_default = 1";
					$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_INVOICE_TYPE');
					if (!empty($dedupeRes)) {
						$message = "Duplicate Entry found in DB";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				$dataArray	= array();
				$dataArray['invoice_type']    = strtoupper(trim($data[0]));
				$dataArray['invoice_status']  = (strtoupper(trim($data[1])) == 'ACTIVE') ? 1 : 2;
				$dataArray['is_default']      = trim($data[2]);
				$dataArray['status']          = 1;
				$dataArray['cr_usr']		  = $user_id;
				$dataArray['cr_date'] 		  = date('Y-m-d H:i:s');
				$dataInsrt[]                  = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_INVOICE_TYPE');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Invoice Type details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_invoice_type_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterCurrency($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Currency Name', 'Exchange Rate', 'Is Default', 'Status');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Currency Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Currency Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 20 || strlen($data[0]) < 2) {
					$message = "Currency Name must be 2 to 20 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Exchange Rate is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->decimal, trim($data[1]))) {
					$message = "Exchange Rate " . $this->decimal_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[1]) >= 10 || strlen($data[1]) < 0) {
					$message = "Exchange Rate must lie between 0 and 10 .";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$is_default	= trim($data[2]);

				if (!empty(trim($data[2]))) {
					if (trim($data[2]) != '0' && trim($data[2]) != '1') {
						$message = "Invalid Is Default";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				$status = ['ACTIVE', 'INACTIVE'];

				if (empty($data[3]) || in_array(trim($data[3]), $this->emptyvalarr)) {
					$message = "Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!in_array(strtoupper(trim($data[3])), $status)) {
					$message = "Invalid Status";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if ($is_default == '1') {
					$where = "is_default = 1";
					$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_CURRENCY');
					if (!empty($dedupeRes)) {
						$message = "Duplicate Entry found in DB";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				$dataArray	= array();
				$dataArray['currency']        = strtoupper(trim($data[0]));
				$dataArray['exchange_rate']   = trim($data[1]);
				$dataArray['currency_status'] = (strtoupper(trim($data[3])) == 'ACTIVE') ? 1 : 2;
				$dataArray['is_default']      = trim($data[2]);
				$dataArray['status']          = 1;
				$dataArray['cr_usr']		  = $user_id;
				$dataArray['cr_date'] 		  = date('Y-m-d H:i:s');
				$dataInsrt[]                  = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_CURRENCY');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Currency details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_currency_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterOtherCharges($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Title', 'Status');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Title is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Title " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 50 || strlen($data[0]) < 2) {
					$message = "Title must be 2 to 50 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$status = ['ACTIVE', 'INACTIVE'];

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!in_array(strtoupper(trim($data[1])), $status)) {
					$message = "Invalid Status";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$dataArray	= array();
				$dataArray['title']           = ucfirst(trim($data[0]));
				$dataArray['oth_status']      = (strtoupper(trim($data[1])) == 'ACTIVE') ? 1 : 2;
				$dataArray['status']          = 1;
				$dataArray['cr_usr']		  = $user_id;
				$dataArray['cr_date'] 		  = date('Y-m-d H:i:s');
				$dataInsrt[]                  = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_OTHER_CHARGES');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Other Charges details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_other_charges_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterChecklist($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$countryArray		= array();
			$countryDBArray	= $this->Import_Model->getAllCountryList();
			foreach ($countryDBArray as $key => $value) {
				$countryArray[strtolower($value['country_name'])] = $value['country_id'];
			}

			$visaTypeArray = array();
			$visaTypeDBArray = $this->Import_Model->getVisaTypeList();
			foreach ($visaTypeDBArray as $key => $value) {
				$visaTypeArray[strtolower($value['title'])] = $value['id'];
			}

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Visa_type', 'Country');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}

				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) { // visa_type
					$message = "Visa Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[0])), $visaTypeArray)) {
					$message = "Invalid Visa Type";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}


				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Country Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[1])), $countryArray)) {
					$message = "Invalid Country Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$dataArray	= array();
				$dataArray['checklist_visa_type_id'] = $visaTypeArray[strtolower(trim($data[0]))];
				$dataArray['checklist_country_id']	= $countryArray[strtolower(trim($data[1]))];
				$dataArray['status'] = 1;
				$dataArray['cr_usr'] = $user_id;
				$dataArray['cr_date'] = date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_PROCESS_CHECKLIST');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Process checklist details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_process_checklist_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterClient($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$clientVendorArray		= array();
			$clientVendorDBArray	= $this->Import_Model->getAllClientVendorList();
			foreach ($clientVendorDBArray as $key => $value) {
				$clientVendorArray[strtolower($value['client_type'])] = $value['client_type_id'];
			}

			$countryArray		= array();
			$countryDBArray	= $this->Import_Model->getAllCountryList();
			foreach ($countryDBArray as $key => $value) {
				$countryArray[strtolower($value['country_name'])] = $value['country_id'];
			}

			$stateArray = array();
			$stateDBArray = $this->Import_Model->getAllStateList();
			foreach ($stateDBArray as $key => $value) {
				$stateArray[strtolower($value['state_name'])] = $value['state_code'];
			}

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Client/Vendor Type', 'Client/Vendor Name', 'Email', 'Mobile No.', 'PAN No', 'GSTIN No', 'Country', 'State', 'City', 'Pincode', 'Address', 'Bank Name', 'Account No', 'IFSC Code', 'MICR Code');
			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}

				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Client/Vendor Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[0])), $clientVendorArray)) {
					$message = "Invalid Client/Vendor Type Type";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Client/Vendor Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[1]))) {
					$message = "Client/Vendor Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[1]) > 50 || strlen($data[1]) < 2) {
					$message = "Client/Vendor Name must be 2 to 50 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//EMAIL ID
				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
					$message = "Email is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!empty($data[2]) && filter_var($data[2], FILTER_VALIDATE_EMAIL) === false) {
					$message = "Email Id is not valid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!empty($data[2]) && strlen($data[2]) > 100) {
					$message = "Email Id must not be greater than 100 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//MOBILE NO.
				if (empty($data[3]) || in_array(trim($data[3]), $this->emptyvalarr)) {
					$message = "Mobile No. is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[3]))) {
					$message = "Mobile No. " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[3]) != 10) {
					$message = "Mobile No must be 10 digit length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!empty($data[4])) { // pan no
					if (!preg_match($this->alphanumeric, trim($data[4]))) {
						$message = "PAN can only be alphanumeric value";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[4]) != 10) {
						$message = "Invalid PAN";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				if (!empty($data[5])) { // gstin no
					if (!preg_match($this->alphanumeric, trim($data[5]))) {
						$message = "GSTIN can only be alphanumeric value";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[5]) != 15) {
						$message = "Invalid GSTIN";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (empty($data[6]) || in_array(trim($data[6]), $this->emptyvalarr)) {
					$message = "Country Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[6])), $countryArray)) {
					$message = "Invalid Country Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[7]) || in_array(trim($data[7]), $this->emptyvalarr)) {
					$message = "State Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[7])), $stateArray)) {
					$message = "Invalid State Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//CITY
				if (!empty($data[8])) {
					if (!preg_match($this->addr_regex, trim($data[8]))) {
						$message = "City Name " . $this->addr_regex_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[8]) > 50) {
						$message = "City Name must not be greater than 50 characters in length";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				if (!empty($data[9])) {
					if (!preg_match($this->numeric, trim($data[9]))) { // pincode
						$message = "Pincode " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[9]) != 6) {
						$message = "Invalid Pincode";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				if (!empty($data[5])) {
					if (!preg_match($this->numeric, trim($data[12]))) {
						$message = "Account No. " . $this->numeric;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[12]) > 17 || strlen($data[12]) < 5) {
						$message = "Account No. must be 5 to 17 character length.";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (empty($data[10]) || in_array(trim($data[10]), $this->emptyvalarr)) {
					$message = "Address is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[10]))) {
					$message = "Address " . $this->addr_regex;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$dataArray	= array();
				$dataArray['client_type'] = $clientVendorArray[strtolower(trim($data[0]))];
				$dataArray['client_name'] = ucfirst(trim($data[1]));
				$dataArray['client_email'] = trim($data[2]);
				$dataArray['client_phone'] = trim($data[3]);
				$dataArray['client_pan_no'] = !empty(trim($data[4])) ? trim($data[4]) : '';
				$dataArray['gstin_no'] = !empty(trim($data[5])) ? trim($data[5]) : NULL;
				$dataArray['country'] = $countryArray[strtolower(trim($data[6]))];
				$dataArray['state'] = $stateArray[strtolower(trim($data[7]))];
				$dataArray['client_city'] = !empty(trim($data[8])) ? trim($data[8]) : '';
				$dataArray['client_pincode'] = !empty(trim($data[9])) ? trim($data[9]) : '';
				$dataArray['address'] = trim($data[10]);
				$dataArray['bank_name'] = !empty(trim($data[11])) ? trim($data[11]) : '';
				$dataArray['account_no'] = !empty(trim($data[12])) ? trim($data[12]) : '';
				$dataArray['ifsc_code'] = !empty(trim($data[13])) ? trim($data[13]) : '';
				$dataArray['micr_code'] = !empty(trim($data[14])) ? trim($data[14]) : '';
				$dataArray['status'] = 1;
				$dataArray['cr_usr'] = $user_id;
				$dataArray['cr_date'] = date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);

			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_CLIENT');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Client/Vendor details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_client_vendor_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterBank($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Type', 'Bank Name', 'Account No.', 'IFSC Code', 'MICR Code', 'SWIFT Code', 'Address');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}

				$type = ['BANK', 'CASH'];
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) { // Type
					$message = "Type is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!in_array(trim(strtoupper($data[0])), $type)) {
					$message = "Invalid Type";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}


				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Bank Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[1]))) {
					$message = "Bank Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (trim(strtoupper($data[0])) == 'BANK') {
					if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
						$message = "Account No. is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					if (!preg_match($this->numeric, trim($data[2]))) {
						$message = "Account No. " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[2]) > 17 || strlen($data[2]) < 5) {
						$message = "Account No. must be 5 to 17 character length.";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}

					if (empty($data[6]) || in_array(trim($data[6]), $this->emptyvalarr)) {
						$message = "Address is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					if (!preg_match($this->addr_regex, trim($data[6]))) {
						$message = "Address " . $this->addr_regex;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}

					$dataArray	= array();
					$dataArray['type'] = (strtoupper(trim($data[0])) == 'BANK' ? 1 : 2);
					$dataArray['name']	= ucfirst(trim($data[1]));
					$dataArray['account_no'] = !empty(trim($data[2])) ? trim($data[2]) : NULL;
					$dataArray['ifsc_code'] = !empty(trim($data[3])) ? trim($data[3]) : NULL;
					$dataArray['micr_code'] = !empty(trim($data[4])) ? trim($data[4]) : NULL;
					$dataArray['swift_code'] = !empty(trim($data[5])) ? trim($data[5]) : NULL;
					$dataArray['address']	= trim($data[6]);
					$dataArray['status'] = 1;
					$dataArray['cr_usr'] = $user_id;
					$dataArray['cr_date'] = date('Y-m-d H:i:s');
					$dataInsrt[] = $dataArray;
				} else {
					if (!empty(trim($data[2])) || !empty(trim($data[3])) || !empty(trim($data[4])) || !empty(trim($data[5])) || !empty(trim($data[6]))) {
						$message = "In Cash Type only Bank Name is Required";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}

					$dataArray	= array();
					$dataArray['type'] = (strtoupper(trim($data[0])) == 'BANK' ? 1 : 2);
					$dataArray['name']	= ucfirst(trim($data[1]));
					$dataArray['status'] = 1;
					$dataArray['cr_usr'] = $user_id;
					$dataArray['cr_date'] = date('Y-m-d H:i:s');
					$dataInsrt[] = $dataArray;
				}
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_BANK');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Bank details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_bank_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}
	function dbFreeRes()
	{
		$conn = $this->db->conn_id;
		do {
			if ($result = mysqli_store_result($conn)) {
				mysqli_free_result($result);
			}
		} while (mysqli_more_results($conn) && mysqli_next_result($conn));
	}
	function getCategoryList($log_id, $user_id, $queue_id)
	{

		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2,
					'queue_id' => $queue_id
				),
				array('log_id' => $log_id, 'queue_id' => $queue_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Category', 'Status', 'Default', 'Sub Category Name', 'Message', 'Deafult sub Category');

			$i		= 0;
			$last	= 4000;
			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {

				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}

				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Category is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Category Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "Category Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Status is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!in_array(strtoupper(trim($data[1])), $this->radiovalarr)) {
					$message = "Status must be either YES or NO";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}


				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
					$message = "Default is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!in_array(strtoupper(trim($data[2])), $this->radiovalarr)) {
					$message = "Default must be either YES or NO";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}


				if (empty($data[3]) || in_array(trim($data[3]), $this->emptyvalarr)) {
					$message = "Sub Category Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else {
					$sub_category = explode(',', $data[3]);
				}

				if (empty($data[4]) || in_array(trim($data[4]), $this->emptyvalarr)) {
					$message = "Sub Category Message is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else {
					$sub_message = explode(',', $data[4]);
					if (count($sub_category) !=  count($sub_message)) {
						$message = "Number of Sub Category Message does not match the number of Sub Categories ";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (empty($data[5]) || in_array(trim($data[5]), $this->emptyvalarr)) {
					$message = "deafult sub Category is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else {
					$sub_deafult = $data[5];
				}


				$Category	= strtoupper(trim($data[0]));
				$Status	=  strtoupper(trim($data[1]));
				$default	= strtoupper(trim($data[2]));
				$is_ut		= strtoupper(trim($data[3]));
				$arrkey = array_search($Category, array_column($dataInsrt, 'Category'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Category Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$where = "(category_name='" . $Category . "')";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'tms_queue_category_' . $queue_id);

				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$this->dbFreeRes();
				$where = "`default`=1";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'tms_queue_category_' . $queue_id);

				if (!empty($dedupeRes) && $default == 'YES') {
					$message = "Already Entry found in DB of Default Category ";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$dataArray	= array();
				$dataArray['category_name']	= $Category;
				$dataArray['status']	= ($Status == 'YES') ? 1 : 0;
				$dataArray['default']	= ($default == 'YES') ? 1 : 0;
				$dataInsrt[] = $dataArray;

				$array1 = $sub_category;
				$array2 = $sub_message;
				$default = $sub_deafult;

				// Main array to store data for insertion
				$sub_category_data[$Category] = [];

				foreach ($array1 as $key => $value) {
					$isDefault = ($value === $default) ? 1 : 0;
					$sub_category_data[$Category][] = [
						'sub_category_name' => $value,
						'message' => $array2[$key],
						'default' => $isDefault,
					];
				}
			}


			$countInserted = count($dataInsrt);
			$result = $this->checkArrayValidity($dataInsrt);
			if (!($result === true)) {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Category details can not be inserted. ' . $result;
			} else {

				if (empty($failedDataArray) && $countInserted > 0) {
					foreach ($dataInsrt as $key => $value) {
						$inserted_id = $this->Import_Model->insertSingleMaster($value,  'tms_queue_category_' . $queue_id);
						$sub_data = [];
						if ($inserted_id != 0) {
							foreach ($sub_category_data[$value['category_name']] as &$row) {
								$row['category_id'] = $inserted_id;
							}
						}
						$this->dbFreeRes();
						// fx::pr($sub_category_data[$value['category_name']]);
						$this->Import_Model->insertMultipleMaster($sub_category_data[$value['category_name']], 'tms_queue_sub_category_' . $queue_id);
					}
				} else {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Category details can not be inserted.';
				}


				if ($dataLogInst['status'] != 2) {
					if (count($failedDataArray) > 0)
						$dataLogInst['status'] = 3;

					$dataLogInst['remarks'] = "\n $countInserted rows inserted";
				}
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "category_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName,
					'queue_id' => $queue_id
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id, 'queue_id' => $queue_id));
		}
	}

	function checkArrayValidity($array)
	{
		$categoryNames = [];
		$defaultCount = 0;
		
		foreach ($array as $item) {
			// Check for duplicate category_name
			if (in_array($item['category_name'], $categoryNames)) {
				return 'Error: Duplicate category name found.';
			} else {
				$categoryNames[] = $item['category_name'];
			}

			// Check default value
			if ($item['default'] == 1) {
				$defaultCount++;
			}
		}
		
		// Ensure only one default value
		if ($defaultCount != 0) {
			if ($defaultCount !== 1) {
				return 'Error: Number of default values is not exactly one.';
			}
		}

		// No errors found
		return true;
	}

	function importMasterVisaType($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Visa Type Title', 'File Id Initial');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Visa Type Title is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Visa Type Title " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "Visa Type Title must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "File Id Initial is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (strlen($data[1]) > 3) {
					$message = "File Id Initial can't greater then 3 characters or numbers";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$visa_type	= strtoupper(trim($data[0]));
				$arrkey = array_search($visa_type, array_column($dataInsrt, 'title'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$arrkey = array_search(trim($data[1]), array_column($dataInsrt, 'file_id_initial'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "title='" . $visa_type . "' or file_id_initial = '$data[1]'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_VISA_TYPE');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['title']		= $visa_type;
				$dataArray['file_id_initial'] = strtoupper(trim($data[1]));
				$dataArray['cr_usr']	= $user_id;
				$dataArray['cr_date'] 	= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_VISA_TYPE');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Visa Type details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_visa_type_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterCollege($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$countryArray		= array();
			$countryDBArray	= $this->Import_Model->getAllCountryList();
			foreach ($countryDBArray as $key => $value) {
				$countryArray[strtolower($value['country_name'])] = $value['country_id'];
			}

			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataDedupe = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('College Name', 'Country Name');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				//COLLEGE NAME
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "College Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "College Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 200 || strlen($data[0]) < 2) {
					$message = "College Name must be 2 to 200 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//COUNTRY
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Country Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[1])), $countryArray)) {
					$message = "Invalid Country Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$college_name	= strtoupper(trim($data[0]));
				$country_name	= strtolower(trim($data[1]));
				$arrkey = array_search($college_name . $countryArray[$country_name], $dataDedupe);
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "college_name='" . $college_name . "' AND college_country_id=" . $countryArray[$country_name];
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_COLLEGE');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['college_name']		= $college_name;
				$dataArray['college_country_id'] = $countryArray[$country_name];
				$dataArray['cr_usr']	= $user_id;
				$dataArray['cr_date'] 	= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;

				$dataDedupe[]	= $college_name . $countryArray[$country_name];
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_COLLEGE');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'College details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_college_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterCourse($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$collegeArray		= array();
			$collegeDBArray	= $this->Import_Model->getAllCollegeList();
			foreach ($collegeDBArray as $key => $value) {
				$collegeArray[strtolower($value['college_name'])] = $value['college_id'];
			}

			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataDedupe = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Course Name', 'College Name');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				//COLLEGE NAME
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Course Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[0]))) {
					$message = "Course Name " . $this->addr_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "Course Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//COLLEGE
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "College Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[1])), $collegeArray)) {
					$message = "Invalid College Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$course_name	= strtoupper(trim($data[0]));
				$college_name	= strtolower(trim($data[1]));
				$arrkey = array_search($course_name . $collegeArray[$college_name], $dataDedupe);
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "course_name='" . $course_name . "' AND course_college_id=" . $collegeArray[$college_name];
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_COURSE');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['course_name']		= $course_name;
				$dataArray['course_college_id']	= $collegeArray[$college_name];
				$dataArray['cr_usr']	= $user_id;
				$dataArray['cr_date'] 	= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;

				$dataDedupe[]	= $course_name . $collegeArray[$college_name];
			}

			$countInserted = count($dataInsrt);
			if ($countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_COURSE');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'Course details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_course_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterBranch($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$stateArray = array();
			$stateDBArray = $this->Import_Model->getAllStateList();
			foreach ($stateDBArray as $key => $value) {
				$stateArray[strtolower($value['state_name'])] = $value['state_code'];
			}

			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Branch Name', 'Branch Prefix', 'Contact No', 'Alt. Contact No', 'Email Id', 'GSTIN', 'PAN', 'State', 'Address', 'Is Default');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Branch Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Branch Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "Branch Name must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Branch Prefix is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (strlen(trim($data[1])) > 5 && strlen(trim($data[1])) < 1) {
					$message = "Branch Prefix must be 2 to 5 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
					$message = "Contact No is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[2]))) {
					$message = "Contact No " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[2]) > 20 || strlen($data[2]) < 5) {
					$message = "Contact No must be 5 to 20 digit length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!empty($data[3])) {
					if (!preg_match($this->numeric, trim($data[3]))) {
						$message = "Alt. Contact No. " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[3]) > 20 || strlen($data[3]) < 5) {
						$message = "Alt. Contact No. must be 5 to 20 digit length.";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				//EMAIL ID
				if (!empty($data[4]) && filter_var($data[4], FILTER_VALIDATE_EMAIL) === false) {
					$message = "Email Id is not valid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!empty($data[4]) && strlen($data[4]) > 100) {
					$message = "Email Id must not be greater than 100 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!empty($data[5])) {
					if (!preg_match($this->alphanumeric, trim($data[5]))) {
						$message = "GSTIN can only be alphanumeric value";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[5]) != 15) {
						$message = "Invalid GSTIN";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				if (!empty($data[6])) {
					if (!preg_match($this->alphanumeric, trim($data[4]))) {
						$message = "PAN can only be alphanumeric value";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[6]) != 10) {
						$message = "Invalid PAN";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				if (empty($data[7]) || in_array(trim($data[7]), $this->emptyvalarr)) {
					$message = "State Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[7])), $stateArray)) {
					$message = "Invalid State Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[8]) || in_array(trim($data[8]), $this->emptyvalarr)) {
					$message = "Address is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!empty(trim($data[9]))) {
					if (trim($data[9]) != '1' && trim($data[9] != '0')) {
						$message = "Invalid Is Default Value it must contain only 1 or 0";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				$branch_name	= strtoupper(trim($data[0]));
				$branch_prefix	= strtoupper(trim($data[1]));
				$contact_no		= trim($data[2]);
				$alt_contact_no	= trim($data[3]);
				$gstin			= trim($data[5]);
				$pan			= trim($data[6]);
				$address		= trim($data[8]);

				$arrkey 		= array_search($branch_name, array_column($dataInsrt, 'branch_name'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Branch Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$arrkey 		= array_search($branch_prefix, array_column($dataInsrt, 'branch_prefix'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Branch Prefix";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$arrkey = array_search($contact_no, array_column($dataInsrt, 'branch_phone_no'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Contact No";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (!empty($alt_contact_no)) {
					$arrkey = array_search($alt_contact_no, array_column($dataInsrt, 'branch_alt_phone_no'));
					if (!empty($arrkey)) {
						$message = "Duplicate Entry - Alt. Contact No";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (!empty($gstin)) {
					$arrkey = array_search($gstin, array_column($dataInsrt, 'branch_gstin'));
					if (!empty($arrkey)) {
						$message = "Duplicate Entry - GSTIN";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				if (!empty($pan)) {
					$arrkey = array_search($pan, array_column($dataInsrt, 'branch_pan'));
					if (!empty($arrkey)) {
						$message = "Duplicate Entry - PAN";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				$where  = "branch_name='" . $branch_name . "' OR branch_phone_no='" . $contact_no . "'";

				if (!empty($alt_contact_no)) {
					$where .= " OR branch_alt_phone_no='" . $alt_contact_no . "'";
				}
				if (!empty($gstin)) {
					$where .= " OR branch_gstin='" . $gstin . "'";
				}
				if (!empty($pan)) {
					$where .= " OR branch_pan='" . $pan . "'";
				}
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_BRANCH');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['branch_name']			= $branch_name;
				$dataArray['branch_prefix']			= $branch_prefix;
				$dataArray['branch_phone_no']		= $contact_no;
				$dataArray['branch_alt_phone_no']	= !empty($alt_contact_no) ? $alt_contact_no : NULL;
				$dataArray['branch_email_id']	    = !empty(trim($data[4])) ? $data[4] : '';
				$dataArray['branch_gstin']			= !empty($gstin) ? $gstin : NULL;
				$dataArray['branch_pan']			= !empty($pan) ? $pan : NULL;
				$dataArray['branch_state_code']		= $stateArray[strtolower(trim($data[7]))];
				$dataArray['branch_address']		= $address;
				$dataArray['is_default']		    = empty(trim($data[9])) ? 0 : 1;
				$dataArray['cr_usr']				= $user_id;
				$dataArray['cr_date'] 				= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_BRANCH');
			} else {
				$dataLogInst['status']	= 2;
				$dataLogInst['remarks'] = 'Branch details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_branch_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterUserRoles($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('Role Title');

			$i		= 0;
			$last	= 4000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "Role Title is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "Role Title " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 100 || strlen($data[0]) < 2) {
					$message = "Role Title must be 2 to 100 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Have Contact Rights";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!in_array(strtoupper(trim($data[1])), $this->radiovalarr)) {
					$message = "Have Contact Rights must contain YES or NO";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				$role_title		= strtoupper(trim($data[0]));
				$contact_rights	= strtoupper(trim($data[1]));

				$arrkey = array_search($role_title, array_column($dataInsrt, 'title'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$where = "title='" . $role_title . "'";
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_ROLES');
				if (!empty($dedupeRes)) {
					$message = "Duplicate Entry found in DB";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray	= array();
				$dataArray['title']				= $role_title;
				$dataArray['contact_rights']	= ($contact_rights == 'YES') ? 1 : 0;
				$dataArray['cr_usr']			= $user_id;
				$dataArray['cr_date'] 			= date('Y-m-d H:i:s');
				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_ROLES');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'User Roles details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_user_roles_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}

	function importMasterUsers($log_id, $user_id)
	{
		error_reporting(0);
		$dataDbLog = $this->Import_Model->getData($log_id);
		if (!empty($dataDbLog)) {
			$this->Import_Model->addNewData(
				array(
					'processing_start_time' => date('Y-m-d H:i:s'),
					'process_status' => 2
				),
				array('log_id' => $log_id)
			);
			$stateArray		= array();
			$stateDBArray	= $this->Import_Model->getAllStateList();
			foreach ($stateDBArray as $key => $value) {
				$stateArray[strtolower($value['state_name'])] = $value['state_id'];
			}

			$rolesArray		= array();
			$rolesDBArray	= $this->Import_Model->getAllRoleList();
			foreach ($rolesDBArray as $key => $value) {
				$rolesArray[strtolower($value['title'])] = $value['id'];
			}

			$filename	= "$dataDbLog->document_path/$dataDbLog->doc_name";
			$file		= fopen($filename, "r");

			$dataNotUploded		= $dataInsrt = $dataUpdt = array();
			$failedDataArray	= array();
			$countInserted		= 0;
			$dataLogInst		= array('status' => 4);
			$csvResultedColumns = array('First Name', 'Last Name', 'Mobile No', 'Email Id', 'Login Id', 'Password', 'Role', 'Address', 'City', 'State', 'Pincode', 'SMTP Email Id', 'SMTP Password', 'Email Server', 'SMTP Port', 'CC Email Address', 'Enable SSL (YES/NO)', 'Verify SSL (YES/NO)', 'Extension No', 'DID No');

			$i		= 0;
			$last	= 100000;

			while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
				$i++;
				if ($i <= 1 || $i < 0)
					continue;
				if ($i > $last) {
					$dataLogInst['status'] = 2;
					$dataLogInst['remarks'] = 'Exit Due to over limit';
					break 1;
				}
				//FIRST NAME 
				if (empty($data[0]) || in_array(trim($data[0]), $this->emptyvalarr)) {
					$message = "First Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[0]))) {
					$message = "First Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[0]) > 30 || strlen($data[0]) < 2) {
					$message = "First Name must be 2 to 30 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//LAST NAME
				if (empty($data[1]) || in_array(trim($data[1]), $this->emptyvalarr)) {
					$message = "Last Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->person_name_rgx, trim($data[1]))) {
					$message = "Last Name " . $this->person_name_rgx_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[1]) > 30 || strlen($data[1]) < 2) {
					$message = "Last Name must be 2 to 30 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//MOBILE NO.
				if (empty($data[2]) || in_array(trim($data[2]), $this->emptyvalarr)) {
					$message = "Mobile No. is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[2]))) {
					$message = "Mobile No. " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[2]) != 10) {
					$message = "Mobile No must be 10 digit length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//EMAIL ID
				if (empty($data[3]) || in_array(trim($data[3]), $this->emptyvalarr)) {
					$message = "Email Id is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (filter_var($data[3], FILTER_VALIDATE_EMAIL) === false) {
					$message = "Email Id is not valid";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[3]) > 100) {
					$message = "Email Id must not be greater than 100 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//LOGIN ID
				if (empty($data[4]) || in_array(trim($data[4]), $this->emptyvalarr)) {
					$message = "Login Id is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->login_regex, trim($data[4]))) {
					$message = "Login Id " . $this->login_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[4]) > 80) {
					$message = "Login Id must not be greater than 80 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//LOGIN PASSWORD
				if (empty($data[5])) {
					$message = "Login password is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[5]) > 16 || strlen($data[5]) < 8) {
					$message = "Login password must be 8 to 16 character length.";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//ROLE
				if (empty($data[6]) || in_array(trim($data[6]), $this->emptyvalarr)) {
					$message = "Role is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[6])), $rolesArray)) {
					$message = "Invalid Role Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//ADDRESS
				if (empty($data[7]) || in_array(trim($data[7]), $this->emptyvalarr)) {
					$message = "Address is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[7]))) {
					$message = "Address " . $this->addr_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[7]) > 250) {
					$message = "Address must not be greater than 250 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//CITY
				if (empty($data[8]) || in_array(trim($data[8]), $this->emptyvalarr)) {
					$message = "City Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->addr_regex, trim($data[8]))) {
					$message = "City Name " . $this->addr_regex_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[8]) > 100) {
					$message = "City Name must not be greater than 100 characters in length";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//STATE
				if (empty($data[9]) || in_array(trim($data[9]), $this->emptyvalarr)) {
					$message = "State Name is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (!array_key_exists(strtolower(trim($data[9])), $stateArray)) {
					$message = "Invalid State Name";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				//PINCODE
				if (empty($data[10]) || in_array(trim($data[10]), $this->emptyvalarr)) {
					$message = "Pincode is empty";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!preg_match($this->numeric, trim($data[10]))) {
					$message = "Pincode " . $this->numeric_msg;
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				} else if (strlen($data[10]) != 6) {
					$message = "Invalid Pincode";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}

				//SMTP SETUP
				if (!empty($data[11]) || !empty($data[12]) || !empty($data[13]) || !empty($data[14]) || !empty($data[15]) || !empty($data[16]) || !empty($data[17])) {
					//SMTP EMAIL ID
					if (empty($data[11])) {
						$message = "SMTP Email Id is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					if (filter_var($data[11], FILTER_VALIDATE_EMAIL) === false) {
						$message = "SMTP Email Id is not valid";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[11]) > 100) {
						$message = "SMTP Email Id must not be greater than 100 characters in length";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					//SMTP PASSWORD
					if (empty($data[12])) {
						$message = "SMTP password is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					//SMTP SERVER
					if (empty($data[13]) || in_array(trim($data[13]), $this->emptyvalarr)) {
						$message = "SMTP server name is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					//SMTP PORT
					if (empty($data[14]) || in_array(trim($data[14]), $this->emptyvalarr)) {
						$message = "SMTP Port No is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (!preg_match($this->numeric, trim($data[14]))) {
						$message = "SMTP Port No " . $this->numeric_msg;;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					//CC EMAIL ID
					if (!empty($data[15])) {
						$tmpArr		= explode(',', $data[15]);
						$invalidCC	= false;
						foreach ($tmpArr as $emailId) {
							if (filter_var($emailId, FILTER_VALIDATE_EMAIL) === false) {
								$invalidCC = true;
								break;
							}
						}
						if ($invalidCC) {
							$message = "CC Email Id is not valid";
							$failedDataArray[] = array_merge($data, array($message));
							continue;
						}
					}
					//ENABLE SSL
					if (empty($data[16])) {
						$message = "Enable SSL is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					if (!in_array(strtoupper(trim($data[16])), $this->radiovalarr)) {
						$message = "Enable SSL must contain YES or NO";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					//VERIFY SSL
					if (empty($data[17])) {
						$message = "Verify SSL is empty";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
					if (!in_array(strtoupper(trim($data[17])), $this->radiovalarr)) {
						$message = "Verify SSL must contain YES or NO";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				//EXTENSION NO.
				if (!empty($data[18])) {
					if (!preg_match($this->numeric, trim($data[18]))) {
						$message = "Extension No " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[18]) > 10) {
						$message = "Extension No must not be greater than 10 digit in length ";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				//DID NO.
				if (!empty($data[19])) {
					if (!preg_match($this->numeric, trim($data[19]))) {
						$message = "DID No " . $this->numeric_msg;
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					} else if (strlen($data[19]) > 10) {
						$message = "DID No must not be greater than 10 digit in length ";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}
				$dataArray	= array();
				$dataArray['user_first_name']		= strtoupper(trim($data[0]));
				$dataArray['user_last_name']		= strtoupper(trim($data[1]));
				$dataArray['user_phone_no']			= trim($data[2]);
				$dataArray['user_email_id']			= trim($data[3]);
				$dataArray['user_login_id']			= trim($data[4]);
				$dataArray['user_login_password']	= password_hash($data[5], PASSWORD_DEFAULT);
				$dataArray['user_role_id']			= $rolesArray[strtolower(trim($data[6]))];
				$dataArray['user_address']			= trim($data[7]);
				$dataArray['user_addr_city_name']	= trim($data[8]);
				$dataArray['user_addr_state_id']	= $stateArray[strtolower(trim($data[9]))];
				$dataArray['user_addr_pincode']		= trim($data[10]);
				$dataArray['smtp_email_id']			= !empty($data[11]) ? $data[11] : NULL;
				$dataArray['smtp_email_password']	= !empty($data[12]) ? $data[12] : NULL;
				$dataArray['smtp_email_server']		= !empty($data[13]) ? $data[13] : NULL;
				$dataArray['smtp_port']				= !empty($data[14]) ? $data[14] : NULL;
				$dataArray['cc_email_id']			= !empty($data[15]) ? $data[15] : NULL;
				$dataArray['smtp_enable_ssl']		= ($data[16] == 'YES') ? 1 : 0;
				$dataArray['smtp_verified_ssl']		= ($data[17] == 'YES') ? 1 : 0;
				$dataArray['user_extention_no']		= !empty($data[18]) ? $data[18] : NULL;
				$dataArray['user_did_no']			= !empty($data[19]) ? $data[19] : NULL;

				$arrkey = array_search($dataArray['user_phone_no'], array_column($dataInsrt, 'user_phone_no'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Mobile No";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$arrkey = array_search($dataArray['user_email_id'], array_column($dataInsrt, 'user_email_id'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Email Id";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$arrkey = array_search($dataArray['user_login_id'], array_column($dataInsrt, 'user_login_id'));
				if (!empty($arrkey)) {
					$message = "Duplicate Entry - Login Id";
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				if (!empty($smtp_email_id)) {
					$arrkey = array_search($dataArray['smtp_email_id'], array_column($dataInsrt, 'smtp_email_id'));
					if (!empty($arrkey)) {
						$message = "Duplicate Entry - SMTP Email Id";
						$failedDataArray[] = array_merge($data, array($message));
						continue;
					}
				}

				$where = "user_phone_no='" . $dataArray['user_phone_no'] . "' OR user_email_id='" . $dataArray['user_email_id'] . "' OR user_login_id='" . $dataArray['user_login_id'] . "'";
				if (!empty($dataArray['smtp_email_id'])) {
					$where .= " OR smtp_email_id='" . $dataArray['smtp_email_id'] . "'";
				}
				$dedupeRes	= $this->Import_Model->checkDedupeInMaster($where, 'MASTER_USER');
				if (!empty($dedupeRes)) {
					$message = "Duplicate entry in existing record - ";
					if ($dedupeRes[0]['user_phone_no'] == $dataArray['user_phone_no']) {
						$message .= " Mobile No";
					}
					if ($dedupeRes[0]['user_email_id'] == $dataArray['user_email_id']) {
						$message .= " Email Id";
					}
					if ($dedupeRes[0]['user_login_id'] == $dataArray['user_login_id']) {
						$message .= " Login Id";
					}
					if (!empty($dataArray['smtp_email_id'])) {
						if ($dedupeRes[0]['smtp_email_id'] == $dataArray['smtp_email_id']) {
							$message .= " SMTP Email Id";
						}
					}
					$failedDataArray[] = array_merge($data, array($message));
					continue;
				}
				$dataArray['parent_user_id']	= $user_id;
				$dataArray['cr_usr']			= $user_id;
				$dataArray['cr_date'] 			= date('Y-m-d H:i:s');

				$dataInsrt[] = $dataArray;
			}

			$countInserted = count($dataInsrt);
			if (empty($failedDataArray) && $countInserted > 0) {
				$this->Import_Model->insertMultipleMaster($dataInsrt, 'MASTER_USER');
			} else {
				$dataLogInst['status'] = 2;
				$dataLogInst['remarks'] = 'User details can not be inserted.';
			}

			if ($dataLogInst['status'] != 2) {
				if (count($failedDataArray) > 0)
					$dataLogInst['status'] = 3;

				$dataLogInst['remarks'] = "\n $countInserted rows inserted";
			}
			$errorFileName = '';
			if (count($failedDataArray) > 0) {
				$errorFileName = "master_user_import_$log_id.csv";
				$opFileName = "$dataDbLog->document_path/$errorFileName";
				$fp = fopen($opFileName, 'w');
				fputcsv($fp, (is_array($csvResultedColumns)) ? $csvResultedColumns : array());
				foreach ($failedDataArray as $key => $value) {
					fputcsv($fp, $value);
				}
				fclose($fp);
			}

			$dataLogInst = array_merge(
				$dataLogInst,
				array(
					'processing_end_time' => date('Y-m-d H:i:s'),
					'process_status' => 3,
					'error_file' => $errorFileName
				)
			);

			$this->Import_Model->addNewData($dataLogInst, array('log_id' => $log_id));
		}
	}
}
