1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Persons.php");
class Customers extends Persons
{
private $_list_id;
public function __construct()
{
parent::__construct('customers');
$this->load->library('mailchimp_lib');
$CI =& get_instance();
$this->_list_id = $CI->encryption->decrypt($CI->Appconfig->get('mailchimp_list_id'));
}
public function index()
{
$data['table_headers'] = $this->xss_clean(get_customer_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)
{
$stats = $this->Customer->get_stats($person->person_id);
if(empty($stats))
{
$stats = new stdClass;
$stats->total = 0;
$stats->min = 0;
$stats->max = 0;
$stats->average = 0;
$stats->avg_discount = 0;
$stats->quantity = 0;
}
$data_rows[] = get_customer_data_row($person, $stats, $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)
{
$customer_sales_tax_support = $this->config->item('customer_sales_tax_support');
$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;
$data['sales_tax_code_label'] = $info->sales_tax_code . ' ' . $this->Tax->get_info($info->sales_tax_code)->tax_code_name;
$packages = array('' => $this->lang->line('items_none'));
foreach($this->Customer_rewards->get_all()->result_array() as $row)
{
$packages[$this->xss_clean($row['package_id'])] = $this->xss_clean($row['package_name']);
}
$data['packages'] = $packages;
$data['selected_package'] = $info->package_id;
if ($customer_sales_tax_support == '1')
{
$data['customer_sales_tax_enabled'] = TRUE;
}
else
{
$data['customer_sales_tax_enabled'] = FALSE;
}
$stats = $this->Customer->get_stats($customer_id);
if(!empty($stats))
{
foreach(get_object_vars($stats) as $property => $value)
{
$info->$property = $this->xss_clean($value);
}
$data['stats'] = $stats;
}
if(!empty($info->email))
{
if(($mailchimp_info = $this->mailchimp_lib->getMemberInfo($this->_list_id, $info->email)) !== FALSE)
{
$data['mailchimp_info'] = $this->xss_clean($mailchimp_info);
if(($activities = $this->mailchimp_lib->getMemberActivity($this->_list_id, $info->email)) !== FALSE)
{
if(array_key_exists('activity', $activities))
{
$open = 0;
$unopen = 0;
$click = 0;
$total = 0;
$lastopen = '';
foreach($activities['activity'] as $activity)
{
if($activity['action'] == 'sent')
{
++$unopen;
}
elseif($activity['action'] == 'open')
{
if(empty($lastopen))
{
$lastopen = substr($activity['timestamp'], 0, 10);
}
++$open;
}
elseif($activity['action'] == 'click')
{
if(empty($lastopen))
{
$lastopen = substr($activity['timestamp'], 0, 10);
}
++$click;
}
++$total;
}
$data['mailchimp_activity']['total'] = $total;
$data['mailchimp_activity']['open'] = $open;
$data['mailchimp_activity']['unopen'] = $unopen;
$data['mailchimp_activity']['click'] = $click;
$data['mailchimp_activity']['lastopen'] = $lastopen;
}
}
}
}
$this->load->view("customers/form", $data);
}
public function save($customer_id = -1)
{
$first_name = $this->xss_clean($this->input->post('first_name'));
$last_name = $this->xss_clean($this->input->post('last_name'));
$email = $this->xss_clean(strtolower($this->input->post('email')));
$first_name = $this->nameize($first_name);
$last_name = $this->nameize($last_name);
$person_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'gender' => $this->input->post('gender'),
'email' => $email,
'phone_number' => $this->input->post('phone_number'),
'address_1' => $this->input->post('address_1'),
'address_2' => $this->input->post('address_2'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zip' => $this->input->post('zip'),
'country' => $this->input->post('country'),
'comments' => $this->input->post('comments')
);
$customer_data = array(
'account_number' => $this->input->post('account_number') == '' ? NULL : $this->input->post('account_number'),
'company_name' => $this->input->post('company_name') == '' ? NULL : $this->input->post('company_name'),
'discount_percent' => $this->input->post('discount_percent') == '' ? 0.00 : $this->input->post('discount_percent'),
'package_id' => $this->input->post('package_id') == '' ? NULL : $this->input->post('package_id'),
'taxable' => $this->input->post('taxable') != NULL
);
$tax_code = $this->input->post('sales_tax_code');
if(!isset($tax_code))
{
$customer_data['sales_tax_code'] = '';
}
else
{
$customer_data['sales_tax_code'] = $tax_code;
}
if($this->Customer->save_customer($person_data, $customer_data, $customer_id))
{
$this->mailchimp_lib->addOrUpdateMember($this->_list_id, $email, $first_name, $last_name, $this->input->post('mailchimp_status'), array('vip' => $this->input->post('mailchimp_vip') != NULL));
if($customer_id == -1)
{
echo json_encode(array('success' => TRUE,
'message' => $this->lang->line('customers_successful_adding') . ' ' . $first_name . ' ' . $last_name,
'id' => $this->xss_clean($customer_data['person_id'])));
}
else
{
echo json_encode(array('success' => TRUE,
'message' => $this->lang->line('customers_successful_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => $customer_id));
}
}
else
{
echo json_encode(array('success' => FALSE,
'message' => $this->lang->line('customers_error_adding_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => -1));
}
}
public function ajax_check_email()
{
$exists = $this->Customer->check_email_exists(strtolower($this->input->post('email')), $this->input->post('person_id'));
echo !$exists ? 'true' : 'false';
}
public function ajax_check_account_number()
{
$exists = $this->Customer->check_account_number_exists($this->input->post('account_number'), $this->input->post('person_id'));
echo !$exists ? 'true' : 'false';
}
public function delete()
{
$customers_to_delete = $this->input->post('ids');
$customers_info = $this->Customer->get_multiple_info($customers_to_delete);
if($this->Customer->delete_list($customers_to_delete))
{
foreach($customers_info->result() as $info)
{
$this->mailchimp_lib->removeMember($this->_list_id, $info->email);
}
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)
{
$email = strtolower($data[3]);
$person_data = array(
'first_name' => $data[0],
'last_name' => $data[1],
'gender' => $data[2],
'email' => $email,
'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(
'company_name' => $data[12],
'discount_percent' => $data[14],
'taxable' => $data[15] == '' ? 0 : 1
);
$account_number = $data[13];
$invalidated = $this->Customer->check_email_exists($email);
if($account_number != '')
{
$customer_data['account_number'] = $account_number;
$invalidated &= $this->Customer->check_account_number_exists($account_number);
}
}
else
{
$invalidated = TRUE;
}
if($invalidated)
{
$failCodes[] = $i;
}
elseif($this->Customer->save_customer($person_data, $customer_data))
{
$this->mailchimp_lib->addOrUpdateMember($this->_list_id, $person_data['email'], $person_data['first_name'], '', $person_data['last_name']);
}
else
{
$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')));
}
}
}
}
?>