File: /home/cafsindia/allyindian_com/backend/application/controllers/Customers.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Persons.php");
class Customers extends Persons{
public function __construct(){
parent::__construct('customers');
$this->load->model('Email_model');
}
public function index(){
if(!$this->Appconfig->isAppvalid()){
redirect('config');
}
$data['table_headers'] = $this->xss_clean(get_people_manage_table_headers());
$this->load->view('people/manage', $data);
}
public function search(){
$search = $this->input->get('search');
$limit = $this->input->get('limit');
$offset = $this->input->get('offset');
$sort = $this->input->get('sort');
$order = $this->input->get('order');
$customers = $this->Customer->search($search, $limit, $offset, $sort, $order);
$total_rows = $this->Customer->get_found_rows($search);
$data_rows = array();
foreach($customers->result() as $person){
$data_rows[] = get_person_data_row($person, $this);
}
$data_rows = $this->xss_clean($data_rows);
echo json_encode(array('total' => $total_rows, 'rows' => $data_rows));
}
public function suggest(){
$suggestions = $this->xss_clean($this->Customer->get_search_suggestions($this->input->get('term'), TRUE));
echo json_encode($suggestions);
}
public function suggest_search(){
$suggestions = $this->xss_clean($this->Customer->get_search_suggestions($this->input->post('term'), FALSE));
echo json_encode($suggestions);
}
public function view($customer_id = -1){
$info = $this->Customer->get_info($customer_id);
foreach(get_object_vars($info) as $property => $value){
$info->$property = $this->xss_clean($value);
}
$data['person_info'] = $info;
$this->load->view("customers/form", $data);
}
public function save($customer_id = -1){
$doc = date("Y-m-d h:i:s"); //created date..
$password = $this->input->post('custpassword');
$fname = $this->input->post('custfname');
$lname = $this->input->post('custlname');
$customer_data = array(
'custid' => $this->input->post('custid'),
'custtitle' => $this->input->post('custtitle'),
'custfname' => $this->input->post('custfname'),
'custlname' => $this->input->post('custlname'),
'custgender' => $this->input->post('custgender'),
'custdob' => date('Y-m-d',strtotime($this->input->post('custdob'))),
'custoccu' => $this->input->post('custoccu'),
'custemail' => $this->input->post('custemail'),
'custaddress' => $this->input->post('custaddress'),
'custcity' => $this->input->post('custcity'),
'custstate' => $this->input->post('custstate'),
'custcountry' => $this->input->post('custcountry'),
'custpincode' => $this->input->post('custpincode'),
'custnational' => $this->input->post('custnational'),
'custphone' => $this->input->post('custphone'),
'custmobile' => $this->input->post('custmobile'),
'custloginid' => $this->input->post('custloginid'),
'custpassword' => base64_encode($password),
'custnewsletter' => $this->input->post('custnewsletter'),
'custmaileroffer' => $this->input->post('custmaileroffer'),
'custagentcode' => $this->input->post('custagentcode'),
'custref' => $this->input->post('custref'),
'doc' => $doc,
);
if($this->Customer->save_customer($customer_data, $customer_id)){
$customer_data = $this->xss_clean($customer_data);
if($customer_id == -1){
$this->send_email($fname,$lname,$this->input->post('custemail'),$password);
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_successful_adding').' '.
$customer_data['cust_name'], 'id' => $customer_data['cust_id']));
}else {
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_successful_updating').' '.
$customer_data['cust_name'], 'id' => $customer_id));
}
}else{
$customer_data = $this->xss_clean($customer_data);
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_error_adding_updating').' '.
$customer_data['cust_name'], 'id' => -1));
}
}
public function get_row($customer_ids){
$customer_infos = $this->Customer->get_multiple_info($customer_ids);
$result = array();
foreach($customer_infos as $item_info){
$result[$item_info->customer_id] = $this->xss_clean(get_item_data_row($item_info, $this));
}
echo json_encode($result);
}
public function delete(){
$customers_to_delete = $this->xss_clean($this->input->post('ids'));
if($this->Customer->delete_list($customers_to_delete)){
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_successful_deleted').' '.
count($customers_to_delete).' '.$this->lang->line('customers_one_or_multiple')));
}else{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_cannot_be_deleted')));
}
}
public function excel(){
$name = 'import_customers.csv';
$data = file_get_contents($name);
force_download($name, $data);
}
public function excel_import(){
$this->load->view('customers/form_excel_import', NULL);
}
public function do_excel_import(){
if($_FILES['file_path']['error'] != UPLOAD_ERR_OK){
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_excel_import_failed')));
}else{
if(($handle = fopen($_FILES['file_path']['tmp_name'], 'r')) !== FALSE){
fgetcsv($handle);
$i = 1;
$failCodes = array();
while(($data = fgetcsv($handle)) !== FALSE){
$data = $this->xss_clean($data);
if(sizeof($data) >= 15){
$person_data = array(
'first_name' => $data[0],
'last_name' => $data[1],
'gender' => $data[2],
'email' => $data[3],
'phone_number' => $data[4],
'address_1' => $data[5],
'address_2' => $data[6],
'city' => $data[7],
'state' => $data[8],
'zip' => $data[9],
'country' => $data[10],
'comments' => $data[11]
);
$customer_data = array(
'shop_id' => $this->session->userdata('shop_id'),
'company_name' => $data[12],
'discount_percent' => $data[14],
'taxable' => $data[15] == '' ? 0 : 1
);
$account_number = $data[13];
$invalidated = FALSE;
if($account_number != ''){
$customer_data['account_number'] = $account_number;
$invalidated = $this->Customer->account_number_exists($account_number);
}
}else{
$invalidated = TRUE;
}
if($invalidated || !$this->Customer->save_customer($person_data, $customer_data)){
$failCodes[] = $i;
}
++$i;
}
if(count($failCodes) > 0){
$message = $this->lang->line('customers_excel_import_partially_failed') . ' (' . count($failCodes) . '): ' . implode(', ', $failCodes);
echo json_encode(array('success' => FALSE, 'message' => $message));
}else{
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_excel_import_success')));
}
}else{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_excel_import_nodata_wrongformat')));
}
}
}
public function check_login_exist(){
$custloginid = $this->input->post('custloginid');
$result = $this->Customer->check_login_exist($custloginid);
if($result === 0){
echo json_encode(array('success' => TRUE, 'message' => 'not Exist'));
}else{
echo json_encode(array('success' => FALSE, 'message' => 'Login ID Already Exist Enter Someother'));
}
}
public function send_email($fname,$lname,$custemail,$password){
$content = "<div style='width: 750px; margin-left: auto; margin-right: auto; padding: 15px; background-color: #EEEEEE; border-radius: 3px; box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2);'><h4 align='left'>Dear $fname $lname,</h4>
<p>Your Have Registered Successfully...Your Password is $password... Thankyou.</p>
</div>";
$email_data = array(
'to_email' => $custemail,
'subject' => "SBLT Registeration",
'email_content' => $content,
'email_type' => "registeration",
);
$this->Email_model->send_phpmailer($email_data);
}
}
?>