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

class Utilityctrl extends CI_Controller
{
	public $Mastermdl;
	public $excel;
	public $pagination;
	public function __construct()
	{
		parent::__construct();

		$this->load->model('master/Mastermdl');
		// $this->load->model('master/Dhiaanmdl');
		$this->load->library('session');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->config->load('extra');
		$this->load->library('db_operations');
	}

	public function index()
	{
		if(sessionExist())
		{
			$this->load->view('pages/customer_import');
		}
		else
		{
			redirect('loginctrl/logout');
		}
	}

	// ===================================
	// 	Customer Import
	public function import_dhiangems_customers() // Supplier
	{
		if(sessionExist())
		{
			$form_data = $this->input->post();
			$file_data = $_FILES;
			if($file_data['customer_import']['error'] == 0)
			{
				unset($config);
				$config = array();
				$config['upload_path']   = 'public/uploads/customers/';
				$config['allowed_types'] = 'xlsx|xls';
				$config['detect_mime']   = 'TRUE';
				$config['remove_spaces'] = 'TRUE';

				$file_name 	= trim($file_data['customer_import']['name']);
				$ext 		= strtolower(substr($file_name, strrpos($file_name, '.') + 1));
				$filename 	= time().'.'.$ext;

				$config['file_name'] = $filename;

				$this->upload->initialize($config);

				if(!$this->upload->do_upload('customer_import'))
				{
					$errors = $this->upload->display_errors();
					$record_array['error'] = $errors;
				}
				else
				{
					$inputFileName 	= "./public/uploads/customers/".$filename;
					$inputFileType 	= PHPExcel_IOFactory::identify($inputFileName);
					$objReader 		= PHPExcel_IOFactory::createReader($inputFileType);
                	$objPHPExcel 	= $objReader->load($inputFileName);
                	$allDataInSheet = $objPHPExcel->getActiveSheet();
    				$arrayCount 	= count($allDataInSheet);
    				$data_array 	= [];
    				foreach ($allDataInSheet->getRowIterator() as $row)
					{

						if($row->getRowIndex()> 1)
						{
							$cellIterator = $row->getCellIterator();
							$cellIterator->setIterateOnlyExistingCells(false);
							foreach ($cellIterator as $cell)
							{
								$data_array[$cell->getRow()][$cell->getColumn()] = trim($cell->getFormattedValue(), " ");
							}
						}
					}
					if(isset($data_array) && is_array($data_array) && !empty($data_array))
					{
						$duplicate_record = "";
						$arr = array();
						
						foreach ($data_array as $key => $current_array) 
						{
							$cnt = $this->Mastermdl->get_cnt('account_master', array('acc_name' => $current_array['C']));
							if($cnt == 0)
							{
								$data_array[$key]['flag'] = "";
								$data_array[$key]['supp_name'] = $current_array['B'];
								$data_array[$key]['name'] = $current_array['C'];
								if($current_array['M']=='CUSTOMER')
								{
									$data_array[$key]['group'] = $current_array['M'];
									$data_array[$key]['group_id'] = 5;	
								}
								else if($current_array['M']=="SUPPLIER")
								{
									$data_array[$key]['group'] = $current_array['M'];
									$data_array[$key]['group_id'] = 6;
								}
								else if($current_array['M']=="BROKER")
								{
									$data_array[$key]['group'] = $current_array['M'];
									$data_array[$key]['group_id'] = 23;
								}
								
								$data_array[$key]['cityId'] = '';
								$data_array[$key]['ref1'] = '';
								$data_array[$key]['ref2'] = '';
								$data_array[$key]['address'] = $current_array['D'];
								$data_array[$key]['tel1'] = $current_array['E'];
								$data_array[$key]['mobile'] = $current_array['F'];
								$data_array[$key]['email'] = $current_array['G'];
								$data_array[$key]['gst'] = $current_array['P'];
								$data_array[$key]['pan'] = $current_array['K'];
								$data_array[$key]['ref_name'] = $current_array['H']; // for website
								
								if($current_array['Q']=='WITHIN MAHARASHTRA')
								{
									$data_array[$key]['ref1'] = $current_array['Q'];
									$data_array[$key]['ref2'] = 1;	
								}
								else if($current_array['Q']=="OUTSIDE MAHARASHTRA")
								{
									$data_array[$key]['ref1'] = $current_array['Q'];
									$data_array[$key]['ref2'] = 2;
								}
							}
							else
							{
								$data_array[$key]['flag'] = "red";
								$data_array[$key]['supp_name'] = $current_array['B'];
								$data_array[$key]['name'] = $current_array['C'];
								if($current_array['M']=='CUSTOMER')
								{
									$data_array[$key]['group'] = $current_array['M'];
									$data_array[$key]['group_id'] = 5;	
								}
								else if($current_array['M']=="SUPPLIER")
								{
									$data_array[$key]['group'] = $current_array['M'];
									$data_array[$key]['group_id'] = 6;
								}
								else if($current_array['M']=="BROKER")
								{
									$data_array[$key]['group'] = $current_array['M'];
									$data_array[$key]['group_id'] = 23;
								}
								$data_array[$key]['cityId'] = '';
								$data_array[$key]['ref1'] = '';
								$data_array[$key]['ref2'] = '';
								$data_array[$key]['address'] = $current_array['D'];
								$data_array[$key]['tel1'] = $current_array['E'];
								$data_array[$key]['mobile'] = $current_array['F'];
								$data_array[$key]['email'] = $current_array['G'];
								$data_array[$key]['gst'] = $current_array['P'];
								$data_array[$key]['pan'] = $current_array['K'];
								$data_array[$key]['ref_name'] = $current_array['H']; // for website
								
								if($current_array['Q']=='WITHIN MAHARASHTRA')
								{
									$data_array[$key]['ref1'] = $current_array['Q'];
									$data_array[$key]['ref2'] = 1;	
								}
								else if($current_array['Q']=="OUTSIDE MAHARASHTRA")
								{
									$data_array[$key]['ref1'] = $current_array['Q'];
									$data_array[$key]['ref2'] = 2;
								}
							}
						}

						if($duplicate_record == "")
						{
							$result['imp_customers'] = $data_array;
						}
						else
						{
							$result['error'] = "duplicate found for customers".$duplicate_record."";
						}

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

	// ===================================
	// 	Supplier Import
// 	public function import_dhiangems_supplier()
// 	{
// 		if(sessionExist())
// 		{
// 			$form_data = $this->input->post();
// 			$file_data = $_FILES;
// 			if($file_data['customer_import']['error'] == 0)
// 			{
// 				unset($config);
// 				$config = array();
// 				$config['upload_path']   = 'public/uploads/customers/';
// 				$config['allowed_types'] = 'xlsx|xls';
// 				$config['detect_mime']   = 'TRUE';
// 				$config['remove_spaces'] = 'TRUE';

// 				$file_name 	= trim($file_data['customer_import']['name']);
// 				$ext 		= strtolower(substr($file_name, strrpos($file_name, '.') + 1));
// 				$filename 	= time().'.'.$ext;

// 				$config['file_name'] = $filename;

// 				$this->upload->initialize($config);

// 				if(!$this->upload->do_upload('customer_import'))
// 				{
// 					$errors = $this->upload->display_errors();
// 					$record_array['error'] = $errors;
// 				}
// 				else
// 				{
// 					$inputFileName 	= "./public/uploads/customers/".$filename;
// 					$inputFileType 	= PHPExcel_IOFactory::identify($inputFileName);
// 					$objReader 		= PHPExcel_IOFactory::createReader($inputFileType);
//                 	$objPHPExcel 	= $objReader->load($inputFileName);
//                 	$allDataInSheet = $objPHPExcel->getActiveSheet();
//     				$arrayCount 	= count($allDataInSheet);
//     				$data_array 	= [];
//     				foreach ($allDataInSheet->getRowIterator() as $row)
// 					{

// 						if($row->getRowIndex()> 1)
// 						{
// 							$cellIterator = $row->getCellIterator();
// 							$cellIterator->setIterateOnlyExistingCells(false);
// 							foreach ($cellIterator as $cell)
// 							{
// 								$data_array[$cell->getRow()][$cell->getColumn()] = trim($cell->getFormattedValue(), " ");
// 							}
// 						}
// 					}
// 					if(isset($data_array) && is_array($data_array) && !empty($data_array))
// 					{
// 						$duplicate_record = "";
// 						$arr = array();
						
// 						foreach ($data_array as $key => $current_array) 
// 						{
// 							$cnt = $this->Mastermdl->get_cnt('account_master', array('acc_name' => $current_array['C']));
// 							if($cnt == 0)
// 							{
// 								$data_array[$key]['flag'] = "";
// 								$data_array[$key]['name'] = $current_array['C'];
// 								if($current_array['Q']=='CUSTOMER')
// 								{
// 									$data_array[$key]['group'] = $current_array['Q'];
// 									$data_array[$key]['group_id'] = 5;	
// 								}
// 								else if($current_array['Q']=="SUPPLIER")
// 								{
// 									$data_array[$key]['group'] = $current_array['Q'];
// 									$data_array[$key]['group_id'] = 6;
// 								}
// 								else if($current_array['Q']=="BROKER")
// 								{
// 									$data_array[$key]['group'] = $current_array['Q'];
// 									$data_array[$key]['group_id'] = 23;
// 								}
// 								// $data_array[$key]['group'] = $current_array['C'];
// 								$data_array[$key]['cityId'] = $current_array['E'];
// 								$data_array[$key]['address'] = $current_array['G'];
// 								$data_array[$key]['tel1'] = $current_array['H'];
// 								// $data_array[$key]['tel2'] = $current_array['G'];
// 								$data_array[$key]['mobile'] = $current_array['K'];
// 								$data_array[$key]['email'] = $current_array['L'];
// 								$data_array[$key]['gst'] = $current_array['N'];
// 								$data_array[$key]['pan'] = $current_array['P'];
// 								$data_array[$key]['ref_name'] = $current_array['R'];
// 								$data_array[$key]['ref1'] = $current_array['AB'];
// 								$data_array[$key]['ref2'] = $current_array['AG'];
// 							}
// 							else
// 							{
// 								$data_array[$key]['flag'] = "red";
// 								$data_array[$key]['name'] = $current_array['C'];
// 								if($current_array['Q']=='CUSTOMER')
// 								{
// 									$data_array[$key]['group'] = $current_array['Q'];
// 									$data_array[$key]['group_id'] = 5;	
// 								}
// 								else if($current_array['Q']=="SUPPLIER")
// 								{
// 									$data_array[$key]['group'] = $current_array['Q'];
// 									$data_array[$key]['group_id'] = 6;
// 								}
// 								else if($current_array['Q']=="BROKER")
// 								{
// 									$data_array[$key]['group'] = $current_array['Q'];
// 									$data_array[$key]['group_id'] = 23;
// 								}
// 								// $data_array[$key]['group'] = $current_array['C'];
// 								$data_array[$key]['cityId'] = $current_array['E'];
// 								$data_array[$key]['address'] = $current_array['G'];
// 								$data_array[$key]['tel1'] = $current_array['H'];
// 								// $data_array[$key]['tel2'] = $current_array['G'];
// 								$data_array[$key]['mobile'] = $current_array['K'];
// 								$data_array[$key]['email'] = $current_array['L'];
// 								$data_array[$key]['gst'] = $current_array['N'];
// 								$data_array[$key]['pan'] = $current_array['P'];
// 								$data_array[$key]['ref_name'] = $current_array['R'];
// 								$data_array[$key]['ref1'] = $current_array['AB'];
// 								$data_array[$key]['ref2'] = $current_array['AG'];
// 							}
// 						}

// 						if($duplicate_record == "")
// 						{
// 							$result['imp_customers'] = $data_array;
// 						}
// 						else
// 						{
// 							$result['error'] = "duplicate found for customers".$duplicate_record."";
// 						}

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

	// ===================================
	
	public function insert_customer_records()
	{
		$flag = 1;
	  	$formdata = $this->input->post();
	  		// print_r($formdata);
	  		// exit;
	  	if(!empty($formdata['acc_id']))
	  	{
	  		foreach ($formdata['acc_id'] as $key => $value)
	  		{
	  			$data['acc_grp_id'] = trim($formdata['group_id'][$key]);
	  			$data['acc_name'] = trim($formdata['supp_name'][$key]);
	  			$data['acc_owner'] = trim($formdata['cust_name'][$key]);
	  			$data['acc_mobile'] = trim($formdata['mobile'][$key]);
	  			$data['acc_email'] = trim($formdata['email'][$key]);
	  			$data['acc_city_id'] = trim($formdata['cityId'][$key]);
	  			$data['acc_address'] = trim($formdata['address'][$key]);
	  			$data['acc_tel_1'] = trim($formdata['tel1'][$key]);
	  			$data['acc_gst'] = trim($formdata['gst'][$key]);
	  			$data['acc_pan'] = trim($formdata['pan'][$key]);
	  			$data['acc_website'] = trim($formdata['ref_name'][$key]);
	  			$data['acc_type'] = trim($formdata['ref2'][$key]);

	  			$result = $this->db_operations->data_insert('account_master', $data);

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

	  		if($flag == 1){ echo 1; }else{ echo 0; }
	  	}
	}

// 	public function insert_customer_records()
// 	{
// 		$flag = 1;
// 	  	$formdata = $this->input->post();
// 	  		// print_r($formdata);
// 	  		// exit;
// 	  	if(!empty($formdata['acc_id']))
// 	  	{
// 	  		foreach ($formdata['acc_id'] as $key => $value)
// 	  		{
// 	  			$data['acc_grp_id'] = trim($formdata['group_id'][$key]);
// 	  			$data['acc_name'] = trim($formdata['cust_name'][$key]);
// 	  			$data['acc_mobile'] = trim($formdata['mobile'][$key]);
// 	  			$data['acc_email'] = trim($formdata['email'][$key]);
// 	  			$data['acc_city_id'] = trim($formdata['cityId'][$key]);
// 	  			$data['acc_address'] = trim($formdata['address'][$key]);
// 	  			$data['acc_tel_1'] = trim($formdata['tel1'][$key]);
// 	  			$data['acc_gst'] = trim($formdata['gst'][$key]);
// 	  			$data['acc_pan'] = trim($formdata['pan'][$key]);
// 	  			$data['acc_ref'] = trim($formdata['ref_name'][$key]);
// 	  			$data['acc_party1'] = trim($formdata['ref1'][$key]);
// 	  			$data['acc_party2'] = trim($formdata['ref2'][$key]);

// 	  			$result = $this->db_operations->data_insert('account_master', $data);

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

// 	  		if($flag == 1){ echo 1; }else{ echo 0; }
// 	  	}
// 	}

	// ===================================

}
?>