File: //var/www/html/pmw24/app/application/controllers/admin/Customer_12.07.2021.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Customer extends MY_Controller {
public function __construct() {
parent::__construct();
$this->redirect_guest();
$this->admin=$this->session->userdata('admin');
//print_r($this->admin);die;
$this->load->model('admin/mcustomer');
$this->load->library('session');
}
public function index() {
$this->_load_list_view();
}
private function _load_list_view() {
$data['admin'] = $this->admin;
$data['content'] = 'admin/customer/list';
$this->load->view('admin/layouts/index', $data);
}
public function inactive_user() {
$this->_load_list_view_inactive();
}
private function _load_list_view_inactive() {
$data['admin'] = $this->admin;
$data['content'] = 'admin/usersetting/list_inactive';
$this->load->view('admin/layouts/index', $data);
}
public function all_content_list(){
$list = $this->mcustomer->get_datatables(0);
//print_r($list);die;
$data = array();
$no = $_POST['start'];
$i=1;
if($no>1){
$i=$i*$no+1;
}
foreach ($list as $person) {
$row = array();
$row[]=$i;
$row[] = ($person->customer_type == 'I')?'Individual':'Business';
$row[] =($person->customer_type =='B')?$person->business_name:$person->first_name.' '.(!empty($person->middle_name)?$person->middle_name.' ':'').$person->last_name;
$row[] = $person->abn;
$row[] = $person->landline_no;
$row[] = $person->mobile;
$row[] = $person->email;
$row[] = 'N/A';
$row[] = $person->full_address;
$admin = $this->admin;
$CI =& get_instance();
$CI->db->select('*');
$CI->db->from('role_menu');
$CI->db->join('menu', 'menu.menu_id = role_menu.menu_id', 'left');
$CI->db->where('role_id',$admin['role_id']);
$CI->db->where('parent_id',63);
$query=$CI->db->get();
$menus = $query->result_array();
$status_result = $this->searcharray('Status', 'menu_name', $menus);
$edit_result = $this->searcharray('Edit', 'menu_name', $menus);
if(!empty($status_result))
{
$row[] = ($person->status==0?'<a class="cstm_view_status btn btn-warning approve_btn" data-href="'.base_url('admin/customer/approve_active_customer/'.$person->customer_id).'" title="Approve"><span class="glyphicon glyphicon-ok"></span></a>':'N/A');
}
if(!empty($edit_result)){
$row[] = '<a href="'.base_url('admin/customer/edit/'.$person->customer_id).'" title="Edit" class="btn btn-info"><span class="glyphicon glyphicon-pencil"></span></a>';
}
$data[] = $row;
$i++;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->mcustomer->count_all(),
"recordsFiltered" => $this->mcustomer->count_filtered(0),
"data" => $data,
);
echo json_encode($output);
}
public function all_content_list_active(){
$list = $this->mcustomer->get_datatables(1);
//print_r($list);die;
$data = array();
$no = $_POST['start'];
$i=1;
if($no>1){
$i=$i*$no+1;
}
foreach ($list as $person) {
$row = array();
$row[]=$i;
$row[] = ($person->customer_type == 'I')?'Individual':'Business';
$row[] =($person->customer_type =='B')?$person->business_name:$person->first_name.' '.(!empty($person->middle_name)?$person->middle_name.' ':'').$person->last_name;
$row[] = $person->abn;
$row[] = $person->landline_no;
$row[] = $person->mobile;
$row[] = $person->email;
$row[] = $person->org_password;
$row[] = $person->full_address;
$admin = $this->admin;
$CI =& get_instance();
$CI->db->select('*');
$CI->db->from('role_menu');
$CI->db->join('menu', 'menu.menu_id = role_menu.menu_id', 'left');
$CI->db->where('role_id',$admin['role_id']);
$CI->db->where('parent_id',63);
$query=$CI->db->get();
$menus = $query->result_array();
$status_result = $this->searcharray('Status', 'menu_name', $menus);
//print_r($status_result);die;
$edit_result = $this->searcharray('Edit', 'menu_name', $menus);
if(!empty($status_result))
{
$row[] = ($person->status==1?'<a class="cstm_view_status btn btn-success deactive_btn" data-href="'.base_url('admin/customer/inactive_customer/'.$person->customer_id).'" title="Dectivate"><span class="glyphicon glyphicon-ok"></span></a>':'N/A');
}
if(!empty($edit_result)){
$row[] = '<a href="'.base_url('admin/customer/edit/'.$person->customer_id).'" title="Edit" class="btn btn-info"><span class="glyphicon glyphicon-pencil"></span></a>';
}
//print_r($row);die;
$data[] = $row;
$i++;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->mcustomer->count_all(),
"recordsFiltered" => $this->mcustomer->count_filtered(1),
"data" => $data,
);
echo json_encode($output);
}
public function all_content_list_inactive(){
$list = $this->mcustomer->get_datatables(2);
//print_r($list);die;
$data = array();
$no = $_POST['start'];
$i=1;
if($no>1){
$i=$i*$no+1;
}
foreach ($list as $person) {
$row = array();
$row[]=$i;
$row[] = ($person->customer_type == 'I')?'Individual':'Business';
$row[] =($person->customer_type =='B')?$person->business_name:$person->first_name.' '.(!empty($person->middle_name)?$person->middle_name.' ':'').$person->last_name;
$row[] = $person->abn;
$row[] = $person->landline_no;
$row[] = $person->mobile;
$row[] = $person->email;
$row[] = $person->org_password;
$row[] = $person->full_address;
$admin = $this->admin;
$CI =& get_instance();
$CI->db->select('*');
$CI->db->from('role_menu');
$CI->db->join('menu', 'menu.menu_id = role_menu.menu_id', 'left');
$CI->db->where('role_id',$admin['role_id']);
$CI->db->where('parent_id',63);
$query=$CI->db->get();
$menus = $query->result_array();
$status_result = $this->searcharray('Status', 'menu_name', $menus);
$edit_result = $this->searcharray('Edit', 'menu_name', $menus);
if(!empty($status_result))
{
$row[] = ($person->status==2?'<a class="cstm_view_status btn btn-danger active_btn" data-href="'.base_url('admin/customer/approve_active_customer/'.$person->customer_id).'" title="Activate"><span class="glyphicon glyphicon-remove"></span></a>':'N/A');
}
if(!empty($edit_result)){
$row[] = '<a href="'.base_url('admin/customer/edit/'.$person->customer_id).'" title="Edit" class="btn btn-info"><span class="glyphicon glyphicon-pencil"></span></a>';
}
$data[] = $row;
$i++;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->mcustomer->count_all(),
"recordsFiltered" => $this->mcustomer->count_filtered(2),
"data" => $data,
);
echo json_encode($output);
}
private function searcharray($value, $key, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
return $val;
}
}
return null;
}
public function edit($customer_id){
$data['customer_data']=$this->mcustomer->getRow('customer',array('customer_id'=>$customer_id));
$data['content'] = 'admin/customer/edit';
$this->load->view('admin/layouts/index', $data);
}
public function edit_customer(){
if($this->input->post()){
$this->form_validation->set_rules('customer_type','Customer Type','required');
$this->form_validation->set_rules('first_name','First Name','required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_email',array('check_email'=>'This %s already exists.'));
$this->form_validation->set_rules('mobile','Mobile','required');
if($this->form_validation->run()==FALSE){
$this->edit($this->input->post('customer_id'));
}else{
$customer_data['customer_type']=$this->input->post('customer_type');
if($this->input->post('customer_type') == 'B'){
$customer_data['business_name']=$this->input->post('business_name');
}
$customer_data['first_name']=$this->input->post('first_name');
$customer_data['middle_name']=$this->input->post('middle_name');
$customer_data['last_name']=$this->input->post('last_name');
$customer_data['abn']=$this->input->post('abn');
$customer_data['landline_no']=$this->input->post('landline_no');
$customer_data['mobile']=$this->input->post('mobile');
$customer_data['email']=$this->input->post('email');
$customer_data['flat_no']=$this->input->post('flat_no');
$customer_data['street_no']=$this->input->post('street_no');
$customer_data['street_name']=$this->input->post('street_name');
$customer_data['suburb']=$this->input->post('suburb');
$customer_data['state']=$this->input->post('state');
$customer_data['pin']=$this->input->post('pin');
$customer_data['updated_by']= $this->admin['admin_id'];
$customer_data['updated_ts']=date('Y-m-d H:i:s');
$customer_id = $this->mcustomer->update(array('customer_id'=>$this->input->post('customer_id')),$customer_data);
$this->session->set_flashdata('success_msg','Customer updated successfully');
redirect('admin/customer');
}
}else{
$this->session->set_flashdata('error_msg','Oops!something went wrong...');
redirect('admin/customer');
}
}
public function add(){
$data['content'] = 'admin/customer/add';
$this->load->view('admin/layouts/index', $data);
}
public function add_customer(){
if($this->input->post()){
$this->form_validation->set_rules('customer_type','Customer Type','required');
$this->form_validation->set_rules('first_name','First Name','required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[customer.email]',array('check_email'=>'This %s already exists.'));
$this->form_validation->set_rules('mobile','Mobile','required');
if($this->form_validation->run()==FALSE){
$this->add();
}else{
$customer_data['customer_type']=$this->input->post('customer_type');
if($this->input->post('customer_type') == 'B'){
$customer_data['business_name']=$this->input->post('business_name');
}
$customer_data['first_name']=$this->input->post('first_name');
$customer_data['middle_name']=$this->input->post('middle_name');
$customer_data['last_name']=$this->input->post('last_name');
$password = $this->password_generate(6);
$customer_data['password']=md5($password);
$customer_data['org_password']=$password;
$customer_data['abn']=$this->input->post('abn');
$customer_data['landline_no']=$this->input->post('landline_no');
$customer_data['mobile']=$this->input->post('mobile');
$customer_data['email']=$this->input->post('email');
$customer_data['flat_no']=$this->input->post('flat_no');
$customer_data['street_no']=$this->input->post('street_no');
$customer_data['street_name']=$this->input->post('street_name');
$customer_data['suburb']=$this->input->post('suburb');
$customer_data['state']=$this->input->post('state');
$customer_data['pin']=$this->input->post('pin');
$customer_data['status']=1;
$customer_data['created_by']=$this->admin["admin_id"];
$customer_data['created_ts']=date('Y-m-d H:i:s');
$customer_id = $this->mcustomer->add($customer_data);
if($customer_id){
$full_name = $customer_data['first_name'].' '.(!empty($customer_data['middle_name'])?$customer_data['middle_name'].' ':'').$customer_data['last_name'];
$mail_temp = file_get_contents('./global/mail/password.html');
$mail_temp=str_replace("{name}",$full_name,$mail_temp);
$mail_temp=str_replace("{shop_name}","PUNJAB MOTOR WORKSHOP",$mail_temp);
$mail_temp=str_replace("{username}",$customer_data['email'],$mail_temp);
$mail_temp=str_replace("{passwd}",$password,$mail_temp);
$mail_temp=str_replace("{shop_logo}",LOGOURL,$mail_temp);
$mail_temp=str_replace("{web_url}",WEBSITE_URL,$mail_temp);
$data['to']=$customer_data['email'];
$data['name']='PUNJAB MOTOR WORKSHOP';
$data['subject']='Approved By Admin';
$data['message']=$mail_temp;
$this->sendMail($data);
}
$this->session->set_flashdata('success_msg','Customer added successfully');
redirect('admin/customer');
}
}else{
$this->session->set_flashdata('error_msg','Oops!something went wrong...');
redirect('admin/customer/add');
}
}
public function check_email() {
$email = $this->input->post('email');// get fiest name
$customer_id = $this->input->post('customer_id');// get last name
$result = $this->mcustomer->check_email_exist($email,$customer_id);
//echo $result;die;
return $result;
}
private function sendMail($data){
$config['protocol'] = 'smtp';
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://mail.fitser.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'test123@fitser.com';
$config['smtp_pass'] = 'Test123@';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'html';
$config['validation'] = TRUE;
$this->email->initialize($config);
$this->email->set_crlf( "\r\n" );
$this->email->from('info@punjabmotor.com', 'PUNJAB MOTOR WORKSHOP');
$this->email->to($data['to']);
$this->email->subject($data['subject']);
$this->email->message($data['message']);
$this->email->send();
return true;
}
public function inactive_customer($customer_id){
$this->db->set('status',2);
$this->db->set('updated_by',$this->admin['admin_id']);
$this->db->set('updated_ts',date('Y-m-d H:i:s'));
$this->db->where('customer_id',$customer_id);
$this->db->update('customer');
$this->session->set_flashdata('success_msg','Customer deactivated successfully');
redirect('admin/customer');
}
public function approve_active_customer($customer_id){
$customer_data = $this->mcustomer->getRow('customer',array('customer_id'=>$customer_id));
if($customer_data['status'] == 0){
$password = $this->password_generate(6);
$this->db->set('password',md5($password));
$this->db->set('org_password',$password);
}
$this->db->set('status',1);
$this->db->set('updated_by',$this->admin['admin_id']);
$this->db->set('updated_ts',date('Y-m-d H:i:s'));
$this->db->where('customer_id',$customer_id);
$customer_id=$this->db->update('customer');
if($customer_id && $customer_data['status'] == 0){
$full_name = $customer_data['first_name'].' '.(!empty($customer_data['middle_name'])?$customer_data['middle_name'].' ':'').$customer_data['last_name'];
$mail_temp = file_get_contents('./global/mail/password.html');
$mail_temp=str_replace("{name}",$full_name,$mail_temp);
$mail_temp=str_replace("{shop_name}","PUNJAB MOTOR WORKSHOP",$mail_temp);
$mail_temp=str_replace("{username}",$customer_data['email'],$mail_temp);
$mail_temp=str_replace("{passwd}",$password,$mail_temp);
$mail_temp=str_replace("{shop_logo}",LOGOURL,$mail_temp);
$mail_temp=str_replace("{web_url}",WEBSITE_URL,$mail_temp);
$data['to']=$customer_data['email'];
$data['name']='PUNJAB MOTOR WORKSHOP';
$data['subject']='Approved By Admin';
$data['message']=$mail_temp;
$this->sendMail($data);
}
$this->session->set_flashdata('success_msg','Customer activated successfully');
redirect('admin/customer');
}
function password_generate($chars){
$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
return substr(str_shuffle($data), 0, $chars);
}
}