<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Will Kill You Do Not Worry..!!');

class Merchants extends MY_Controller
{

	var $model ='Merchants_model';
	
	public function __construct(){
		parent::__construct();
		if($this->session->userdata('isLogin') == FALSE ){ redirect(base_url('admin/login/login_form'));}	
		$this->load->model('admin/'.$this->model); 
		
		$config['upload_path']          = 'uploads/merchants/';
		$config['allowed_types']        = 'gif|jpg|png|jpeg';
		$config['max_size'] 			= 12000;
        $config['max_width'] 			= 13500;
        $config['max_height'] 			= 13500;
		$this->upload->initialize($config);
	}
	
	public function index(){
			
	}

/*##########################################################*/
/*################ Merchant GROUPS ####################*/
/*##########################################################*/

	
	
	public function groups(){
			$data['pagetitle']='Merchants Groups';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Merchants_model->get_merchant_group($data['status']);
			$data['updatelink']=base_url('admin/merchants/grouppublish');
			$data['deletelink']=base_url('admin/merchants/groupdelete');
			$this->adminview('merchants/groups',$data);
	}
	
	public function grouppublish($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'title',
											'label' => 'Group Title',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['iconold'] != $_FILES['icon']['name'] && $_FILES['icon']['name'] !=''){
					if (!$this->upload->do_upload('icon')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['icon']=$imgdata;		
					}
				}
				
				
				
				if($_POST['id'] ==''){
					if(checkunique('title',array('title'=>$_POST['title']),'merchant_group')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Merchant Group Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					$data['updatedon']=date('Y-m-d H:i:s');
					$id = $this->Merchants_model->set_merchant_group($data,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Merchant Group Updated Successfully...</div>');
					redirect(base_url('admin/merchants/groups'));
				}else{
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Merchants_model->get_single_merchant_group($para);
					$data['id']=$response[0]->merchantgroupId;
					$data['title']=$response[0]->title;
					$data['description']=$response[0]->description;
					$data['icon']=$response[0]->icon;
					$data['status']=$response[0]->status;
			}
			$data['pagetitle']='Update Merchants Group';	
			$this->adminview('merchants/grouppublish',$data);
	}

	public function groupdelete($para = null){
		$this->Merchants_model->delete_merchant_group($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/merchants/groups'));
	}
/*#######################################################################*/

	
/*##########################################################*/
/*################ Merchants ####################*/
/*##########################################################*/	

public function merchants(){
			$data['pagetitle']='Merchants';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Merchants_model->get_merchant($data['status']);
			$data['updatelink']=base_url('admin/merchants/merchantspublish');
			$data['creditlink']=base_url('admin/merchants/merchantscredits');
			$data['deletelink']=base_url('admin/merchants/merchantsdelete');
			$this->adminview('merchants/merchants',$data);
	}
	
	public function merchantspublish($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'accountId',
											'label' => 'Merchant Account Id',
											'rules' => 'required'
									),
									array(
											'field' => 'storename',
											'label' => 'Merchant Store Name',
											'rules' => 'required'
									),
									array(
											'field' => 'username',
											'label' => 'Merchant User Name',
											'rules' => 'required'
									),
									array(
											'field' => 'password',
											'label' => 'Merchant Password',
											'rules' => 'required'
									),
									array(
											'field' => 'name',
											'label' => 'Merchant Name',
											'rules' => 'required'
									)
									
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['iconold'] != $_FILES['icon']['name'] && $_FILES['icon']['name'] !=''){
					if (!$this->upload->do_upload('icon')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['icon']=$imgdata;		
					}
				}
				
				
				
				if($_POST['id'] ==''){
					if(checkunique('accountId',array('accountId'=>$_POST['accountId']),'merchant')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Merchant Account Id Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['accountId']=$_POST['accountId'];
					$data['groupId']=$_POST['groupId'];
					$data['storename']=$_POST['storename'];
					$data['name']=$_POST['name'];
					$data['username']=$_POST['username'];
					$data['password']=$_POST['password'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['whatsappno']=$_POST['whatsappno'];
					$data['description']=$_POST['description'];
					
					if($_POST['dailylimit']>0){$data['dailylimit']=$_POST['dailylimit'];}
					else{$data['dailylimit']=0;}
					
					
					if($_POST['headerkeyRML'] !=null && $_POST['headerkeyRML'] !="")
					{$data['headerkeyRML'] =$_POST['headerkeyRML'];}
					else{ $data['headerkeyRML'] ='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYnV5bnh0NiIsInVzZXJuYW1lIjoiYnV5bnh0NiIsImV4cCI6MTkzNjk2MDY1OCwiZW1haWwiOiJ1ZGF5QGJ1eW54dC5pbiIsIm9yaWdfaWF0IjoxNjIxNjAwNjU4LCJjdXN0b21lcl9pZCI6IkZCeVNXZzlvT2ZoayJ9.O4CFD6oepQP9qRig04MHZE0z1keJYb7-7YtaLUr9Pmg';}
					
					if($_POST['headerkeyINFOBIP'] !=null && $_POST['headerkeyINFOBIP'] !="")
					{$data['headerkeyINFOBIP'] =$_POST['headerkeyINFOBIP'];}
					else{ $data['headerkeyINFOBIP'] ='App 2e99c35d9d74cd2245c4b3faf8170b09-5d183eae-74cc-48f0-9038-c27eeb79cf9d';}
					
					
					if($_POST['scenariokeyINFOBIP'] !=null && $_POST['scenariokeyINFOBIP'] !="")
					{$data['scenariokeyINFOBIP'] =$_POST['scenariokeyINFOBIP'];}
					else{ $data['scenariokeyINFOBIP'] ='77E3760DB647A79B03F245C55FE7D3A2';}
					
					
					$data['authlink'] = md5($data['username']."_".$data['password']."_".$data['accountId']);
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					$data['updatedon']=date('Y-m-d H:i:s');
					$id = $this->Merchants_model->set_merchant($data,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Merchant Updated Successfully...</div>');
					redirect(base_url('admin/merchants/merchants'));
				}else{
					$data['accountId']=$_POST['accountId'];
					$data['groupId']=$_POST['groupId'];
					$data['storename']=$_POST['storename'];
					$data['username']=$_POST['username'];
					$data['password']=$_POST['password'];
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['whatsappno']=$_POST['whatsappno'];
					$data['description']=$_POST['description'];
					$data['dailylimit']=$_POST['dailylimit'];
					$data['authlink'] = md5($data['username']."_".$data['password']."_".$data['accountId']);
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
					$data['headerkeyRML'] =$_POST['headerkeyRML'];
					$data['headerkeyINFOBIP'] =$_POST['headerkeyINFOBIP'];
					$data['scenariokeyINFOBIP'] =$_POST['scenariokeyINFOBIP'];
				}
			
			}
			
			if($para >0){
					
					$response = $this->Merchants_model->get_single_merchant($para);
					$data['id']=$response[0]->merchantId;
					$data['accountId']=$response[0]->accountId;
					$data['groupId']=$response[0]->groupId;
					$data['storename']=$response[0]->storename;
					$data['name']=$response[0]->name;
					$data['username']=$response[0]->username;
					$data['password']=$response[0]->password;
					$data['email']=$response[0]->email;
					$data['contact']=$response[0]->contact;
					$data['whatsappno']=$response[0]->whatsappno;
					$data['description']=$response[0]->description;
					$data['dailylimit']=$response[0]->dailylimit;
					$data['icon']=$response[0]->icon;
					$data['status']=$response[0]->status;
					$data['authlink'] = $response[0]->authlink;
					$data['headerkeyRML'] =$response[0]->headerkeyRML;
					$data['headerkeyINFOBIP'] =$response[0]->headerkeyINFOBIP;
					$data['scenariokeyINFOBIP'] =$response[0]->scenariokeyINFOBIP;
			}
			$data['pagetitle']='Update Merchants';
			$data['group'] = $this->Merchants_model->get_merchant_group(1);	
			$this->adminview('merchants/merchantspublish',$data);
	}

	public function merchantsdelete($para = null){
		$this->Merchants_model->delete_merchant($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/merchants/merchants'));
	}
	
	
	public function merchantscredits($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['addcredits']))
			{
				$rules = array(
									array(
											'field' => 'credits',
											'label' => 'Merchant Credits',
											'rules' => 'required'
									)
									
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['credits']<=0){
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> Credit Cannot Be Zero...</div>');
				}else{
					$formerror = 0; 
				}
				if(!$formerror)
				{
					
					$id = $_POST['id'];
					$data=array();
					
					$data['credits']=$_POST['credits'];

					$data['mode']='ADMIN';
					$data['txnId']='';
					$data['message']='ADD BY ADMIN';
					$data['type']='CREDIT';
					$data['amount']='0';
					$data['createdon']=date('Y-m-d H:i:s');
					$data['updatedon']=date('Y-m-d H:i:s');
					
					$id = $this->Merchants_model->add_credit_account($id,$data);					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Merchant Credits Updated Successfuly...</div>');
					redirect(base_url('admin/merchants/merchants'));
				}
			
			}
			
			if(isset($_POST['removecredits']))
			{
				$rules = array(
									array(
											'field' => 'credits',
											'label' => 'Merchant Credits',
											'rules' => 'required'
									)
									
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['credits']<=0){
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> Credit Cannot Be Zero...</div>');
				}else{
					$formerror = 0; 
				}
				if(!$formerror)
				{
					
					$id = $_POST['id'];
					$data=array();
					
					$data['credits']=$_POST['credits'];

					$data['mode']='ADMIN';
					$data['txnId']='';
					$data['message']='REDUCED BY ADMIN';
					$data['type']='DEBIT';
					$data['amount']='0';
					$data['createdon']=date('Y-m-d H:i:s');
					$data['updatedon']=date('Y-m-d H:i:s');
					
					$id = $this->Merchants_model->remove_credit_account($id,$data);					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Merchant Credits Reduced Successfuly...</div>');
					redirect(base_url('admin/merchants/merchants'));
				}
			}
			if($para >0){
					
					$response = $this->Merchants_model->get_merchant_credits($para);
					$data['creditdetails']=$response;
					$response1 = $this->Merchants_model->get_merchant_credits_history($para);
					$data['creditdetailslog']=$response1;
					$data['id']=$para;
			}
			$data['pagetitle']='Update Merchants Credits';
			$this->adminview('merchants/merchantscredits',$data);
	}


}	

?>