|
Server : Apache/2.4.18 (Ubuntu) System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64 User : oppastar ( 1041) PHP Version : 7.0.33-0ubuntu0.16.04.15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, Directory : /var/www/laciasmara.com/public_html/shop/application/controllers/admin/ |
Upload File : |
<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Special_price extends Admin_Controller
{
//this property is used for validating existing special_price title on call back edit special_price
private $special_price_current_id = null;
function __construct()
{
parent::__construct();
$this->load->model('special_price_m');
$this->load->helper('rajaongkir');
}
//this is to list all special_price
public function index()
{
/*----FILTER SEARCH special_price--*/
if (isset($_POST['search_special_price'])) {
//get product name from form
$this->data['keyword'] = $this->security->xss_clean(
$this->input->post('resellers_special_price')
);
//get all customers
$this->db->select('*');
$this->db->from('resellers_special_price');
$this->db->like('resellers_special_price_name', $this->data['keyword']);
$this->db->or_like('special_price_code', $this->data['keyword']);
$this->db->order_by('created_date', 'DESC');
$this->data['resellers_special_price'] = $this->db->get()->result();
} else {
//pagination in action. 300 results per page
$this->load->library('pagination');
$config['base_url'] = base_url() . 'admin/special_price/index';
$config['total_rows'] = $this->special_price_m->record_count();
$config['per_page'] = 300;
$config["uri_segment"] = 4;
$config['num_tag_open'] =
'<span style="padding-left:10px; padding-right:10px">';
$config['num_tag_close'] = '</span>';
$this->pagination->initialize($config);
$this->data['resellers_special_price'] = $this->special_price_m->get_all_special_price(
$config["per_page"],
$this->uri->segment(4)
);
$this->data['use_pagination'] = 'yes';
}
//load view
$this->data['subview'] = 'admin/special_price/index';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//to add a new special_price
public function add()
{
$this->data['products'] = $this->db
->distinct()
->select('title,id_products')
->from('products')
->where('product_status', 1)
->order_by('title')
->get()
->result();
$this->data['customers'] = $this->db
->distinct()
->select('id_customers,name,email')
->from('customers')
->where('status', 1)
->order_by('name')
->get()
->result();
$this->data['resellers_special_price'] = $this->special_price_m->get_new();
//get parent product categories
$this->load->model('category_m');
$this->data['parent_categories'] = $this->category_m->get_parent_categories();
//get brands
$this->db
->select('id_brands, brand')
->from('brands')
->where('status', '1')
->order_by('brand', 'ASC');
$this->data['brands'] = $this->db->get()->result();
//validation in action
//validation check in action
$config = $this->special_price_m->rules;
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters(
'<div class="error">',
'</div>'
);
if ($this->form_validation->run($this) == true) {
$created_date = date('Y-m-d H:i:s');
if ($this->input->post('expired_date')) {
$expired_date = explode('-', $this->input->post('expired_date'));
$new_expired_date =
$expired_date[2] . '-' . $expired_date[1] . '-' . $expired_date[0];
} else {
$new_expired_date = null;
}
$data = $this->table_data_processing(
$this->input->post('special_price_name'),
$this->input->post('discount_value'),
$created_date,
$new_expired_date,
$this->input->post('special_price_type'),
$this->input->post('promostart'),
$this->input->post('promoend')
);
$this->input->post('special_price_type') == 'brand promo'
? ($data['brandpromo'] = implode(',', $this->input->post('brand_id')))
: ($data['brandpromo'] = null);
$this->input->post('special_price_type') == 'product promo'
? ($data['productpromo'] = implode(
',',
$this->input->post('product_id')
))
: ($data['productpromo'] = null);
$this->input->post('special_price_type') == 'customer promo'
? ($data['customerpromo'] = implode(
',',
$this->input->post('customer_id')
))
: ($data['customerpromo'] = null);
$this->special_price_m->add_special_price($data);
$this->session->set_flashdata(
'success',
'<br><p style="background:green; color:white; padding:5px; font-weight:bold;">special_price Add Successful</p>'
);
redirect('admin/special_price');
}
//get all provinces data from provinces table
$this->db
->select('rajaongkir_province_id, province')
->from('indonesia_provinces')
->order_by('rajaongkir_province_id', 'ASC');
$this->data['provinces'] = $this->db->get()->result();
//load view
$this->data['subview'] = 'admin/special_price/edit';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//to edit special_price in admin
public function edit($id = null)
{
//check if id exist. If not exist, show 404.
$count = $this->special_price_m->count_exist($id);
if ($count == 0) {
//page not exist
show_404();
}
$this->load->model('category_m');
$this->data['parent_categories'] = $this->category_m->get_parent_categories();
//get brands
$this->db
->select('id_brands, brand')
->from('brands')
->where('status', '1')
->order_by('brand', 'ASC');
$this->data['brands'] = $this->db->get()->result();
$this->data['products'] = $this->db
->distinct()
->select('title,id_products')
->from('products')
->where('product_status', 1)
->order_by('title')
->get()
->result();
$this->data['customers'] = $this->db
->distinct()
->select('id_customers,name,email')
->from('customers')
->where('status', 1)
->order_by('name')
->get()
->result();
$this->data['resellers_special_price'] = $this->special_price_m->get($id);
//get all chosen (active) categories
$active_categories = $this->data['resellers_special_price']->categorypromo;
if ($active_categories != null) {
$this->data['chosen_categories'] = array_filter(
explode(',', $active_categories)
); //array_filter to remove empty array element
}
//get all chosen (active) brands
$active_brands = $this->data['resellers_special_price']->brandpromo;
if ($active_brands != null) {
$this->data['chosen_brands'] = array_filter(explode(',', $active_brands)); //array_filter to remove empty array element
}
//get all chosen (active) brands
$active_product = $this->data['resellers_special_price']->productpromo;
if ($active_product != null) {
$this->data['chosen_products'] = array_filter(
explode(',', $active_product)
); //array_filter to remove empty array element
}
//get all chosen (active) brands
$active_customer = $this->data['resellers_special_price']->customerpromo;
if ($active_customer != null) {
$this->data['chosen_customers'] = array_filter(
explode(',', $active_customer)
); //array_filter to remove empty array element
}
$this->special_price_current_id = (int) $id;
//validation check in action
$config = $this->special_price_m->rules;
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters(
'<div class="error">',
'</div>'
);
if ($this->form_validation->run($this) == true) {
//get current created date from table
$this->db
->select('created_date')
->from('resellers_special_price')
->where('id_special_price', (int) $id);
$created_date = $this->db->get()->row()->created_date;
if ($this->input->post('expired_date')) {
$expired_date = explode('-', $this->input->post('expired_date'));
$new_expired_date =
$expired_date[2] . '-' . $expired_date[1] . '-' . $expired_date[0];
} else {
$new_expired_date = null;
}
$data = $this->table_data_processing(
$this->input->post('resellers_special_price_name'),
$this->input->post('discount_value'),
$created_date,
$new_expired_date,
$this->input->post('special_price_type'),
$this->input->post('promostart'),
$this->input->post('promoend')
);
($data['productpromo'] = implode(
',',
$this->input->post('product_id')
));
($data['customerpromo'] = implode(
',',
$this->input->post('customer_id')
));
$this->input->post('special_price_type') == 'first purchase promo'
? ($data['customerpromo'] = implode(
',',
$this->input->post('customer_id')
))
: ($data['customerpromo'] = null);
$this->input->post('special_price_type') == 'first purchase promo'
? ($data['productpromo'] = implode(
',',
$this->input->post('product_id')
))
: ($data['productpromo'] = null);
$this->input->post('special_price_type') == 'timed promo'
? ($data['customerpromo'] = implode(
',',
$this->input->post('customer_id')
))
: ($data['customerpromo'] = null);
$this->special_price_m->edit_special_price($id, $data);
$this->session->set_flashdata(
'success',
'<br><p style="background:green; color:white; padding:5px; font-weight:bold;">special_price Edit Successful</p>'
);
redirect('admin/special_price/edit/' . $id);
}
//get all provinces data from provinces table
$this->db
->select('rajaongkir_province_id, province')
->from('indonesia_provinces')
->order_by('rajaongkir_province_id', 'ASC');
$this->data['provinces'] = $this->db->get()->result();
//load view
$this->data['subview'] = 'admin/special_price/edit';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//to delete a special_price
public function delete($id)
{
//check if id exist. If not exist, show 404.
$count = $this->special_price_m->count_exist($id);
if ($count == 0) {
//page not exist
show_404();
}
//delete special_price
$this->special_price_m->delete($id);
$this->session->set_flashdata(
'success',
'<br><p style="background:green; color:white; padding:5px; font-weight:bold;">special_price Delete Successful</p>'
);
redirect('admin/special_price');
}
private function table_data_processing(
$special_price_name,
$discount_value,
$created_date,
$new_expired_date,
$special_price_type,
$promostart,
$promoend
) {
$data = [
'resellers_special_price_name' => $this->security->xss_clean($special_price_name),
'discount_value' => (int) $this->security->xss_clean($discount_value),
'created_date' => $created_date,
'expired_date' => $new_expired_date,
'resellers_special_price_type' => $special_price_type,
'promostart' => $promostart,
'promoend' => $promoend
];
//Promo by Product Category
//get category_id from view,
$categories_id = $this->input->post('category_id');
//check id there is content inside category array
if (is_array($categories_id)) {
$categories_id_count = count($categories_id);
} else {
$categories_id_count = 0;
}
if ($categories_id_count > 0) {
$data['categorypromo'] = implode(',', $categories_id);
} else {
$data['categorypromo'] = null;
}
//Promo by Brand
//get brand_id from view,
$brands_id = $this->input->post('brand_id');
//check id there is content inside brand array
if (is_array($brands_id)) {
$brands_id_count = count($brands_id);
} else {
$brands_id_count = 0;
}
if ($brands_id_count > 0) {
$data['brandpromo'] = implode(',', $brands_id);
} else {
$data['brandpromo'] = null;
}
return $data;
}
//callback function validation add new special_price
//make it private by adding _
public function _cek_existing_special_price_code($str)
{
$num_rows = $this->special_price_m->cek_existing_special_price_code(
$str,
$this->special_price_current_id
);
if ($num_rows != 0) {
$this->form_validation->set_message(
'_cek_existing_special_price_code',
'special_price code already exist !'
);
return false;
} else {
return true;
}
}
public function upload_special_prices()
{
//upload products data
if (!isset($_POST['upload_csv'])) {
show_404();
}
//check if the uploaded file is csv format
$mimes = ['application/vnd.ms-excel', 'text/plain', 'text/csv', 'text/tsv'];
if (in_array($_FILES['userfile']['type'], $mimes)) {
ini_set('auto_detect_line_endings', true); //to detect line ending
//continue import operation
//open the csv file and put into variable
($file = fopen($_FILES['userfile']['tmp_name'], 'r')) or
die('cannot open file');
//initialize array
$csv_data = [];
//if not reach end of file...
while (!feof($file)) {
//put the csv into array..
$csv_data[] = fgetcsv($file, 10000, '~'); //file, length, separator..
}
fclose($file); //close the file
/* echo '<pre>';
print_r($csv_data);
echo '</pre>';
exit(); */
$row_number = 0;
foreach ($csv_data as $item) {
$row_number = $row_number + 1;
//skip the 1st row...
if ($row_number != 1) {
if (empty($item)) {
continue;
} //if array is empty, then continue to next iteration and bypass below script..
//check if product code already exist, if already exist, update the row, else, insert new
$this->db
->select('special_price_code')
->from('resellers_special_price')
->where('special_price_code', $item[0]);
$count_code = $this->db->get()->num_rows();
if ($count_code > 0) {
//special_price code already exist. update only..
} else {
//insert new special_price
//add to special_price table
$data = [
'special_price_code' => $item[0],
'special_price_name' => $item[1],
'special_price_type' => $item[2],
'discount_type' => $item[7],
'discount_value' => $item[8],
];
if (empty($item[6])) {
$data['min_order'] = null;
} else {
$data['min_order'] = $item[6];
}
if (empty($item[5])) {
$data['maxqty_per_person'] = null;
} else {
$data['maxqty_per_person'] = $item[5];
}
if (empty($item[4])) {
$data['qty_ready'] = null;
} else {
$data['qty_ready'] = $item[4];
}
if (empty($item[9])) {
$data['expired_date'] = null;
} else {
$data['expired_date'] = $item[9];
}
switch ($item[2]) {
case 'normal promo':
# code...
break;
case 'birthday promo':
$data['birthmonth'] = $item[3];
break;
case 'time promo':
if (strpos($item[3], '|') !== false) {
//must have | for start and end time
$timepromo_array = explode('|', $item[3]);
$data['promostart'] = trim($timepromo_array[0]);
$data['promoend'] = trim($timepromo_array[1]);
}
break;
case 'quantity promo':
$data['quantitypromo'] = $item[3];
break;
case 'province promo':
$this->db
->select('rajaongkir_province_id')
->from('indonesia_provinces')
->where('province', $item[3]);
$province_id = $this->db->get()->row();
if (count($province_id) > 0) {
$data['provincepromo'] = $province_id->rajaongkir_province_id;
}
break;
case 'category promo':
if (strpos($item[3], '|') !== false) {
//has more than 1 category
$category_array = explode('|', $item[3]);
$category_id_array = [];
foreach ($category_array as $category_name) {
//check if category exist
$this->db
->select('id_categories')
->from('categories')
->where('category', trim($category_name));
$category_id = $this->db->get()->row();
if (count($category_id) > 0) {
$category_id_array[] = $category_id->id_categories;
}
}
$data['categorypromo'] = implode(',', $category_id_array);
} else {
//has only 1 category
//check if category exist
$this->db
->select('id_categories')
->from('categories')
->where('category', trim($item[3]));
$category_id = $this->db->get()->row();
if (count($category_id) > 0) {
$data['categorypromo'] = $category_id->id_categories;
}
}
break;
case 'brand promo':
if (strpos($item[3], '|') !== false) {
//has more than 1 brand
$brand_array = explode('|', $item[3]);
$brand_id_array = [];
foreach ($brand_array as $brand_name) {
//check if category exist
$this->db
->select('id_brands')
->from('brands')
->where('brand', trim($brand_name));
$brand_id = $this->db->get()->row();
if (count($brand_id) > 0) {
$brand_id_array[] = $brand_id->id_brands;
}
}
$data['brandpromo'] = implode(',', $brand_id_array);
} else {
//has only 1 brand
//check if brand exist
$this->db
->select('id_brands')
->from('brands')
->where('brand', trim($item[3]));
$brand_id = $this->db->get()->row();
if (count($brand_id) > 0) {
$data['brandpromo'] = $brand_id->id_brands;
}
}
break;
}
//insert into special_price table
$this->db->insert('resellers_special_price', $data);
}
} /*---end if($row_number != 1) --*/
} /*---end foreach ($csv_data as $item) ---*/
$this->session->set_flashdata(
'success',
'<br><p style="background:green; color:white; padding:5px; font-weight:bold;">special_price CSV Imported.</p>'
);
redirect('admin/special_price');
} else {
//not a csv file. Not allowed.
die('Sorry, file type not allowed. Please upload only CSV file.');
}
}
}