|
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/angkasapuraretail.com/public_html/application/controllers/admin/ |
Upload File : |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Vouchers extends Admin_Controller {
//this property is used for validating existing voucher title on call back edit voucher
private $voucher_current_id = NULL;
function __construct() {
parent::__construct();
$this->load->model('voucher_m');
$this->load->helper('rajaongkir');
if (!in_array('vouchers', $this->data['allowed_module'])) {
$this->data['allowed'] = false;
} else {
$this->data['allowed'] = true;
}
}
//this is to list all vouchers
public function index() {
/*----FILTER SEARCH VOUCHER--*/
if (isset($_POST['search_voucher'])) {
//get product name from form
$this->data['keyword'] = $this->security->xss_clean($this->input->post('voucher'));
//get all customers
$this->db->select('*');
$this->db->from('vouchers');
$this->db->like('voucher_name', $this->data['keyword']);
$this->db->or_like('voucher_code', $this->data['keyword']);
$this->db->order_by('created_date', 'DESC');
$this->data['vouchers'] = $this->db->get()->result();
} else {
//pagination in action. 300 results per page
$this->load->library('pagination');
$config['base_url'] = base_url() . 'admin/vouchers/index';
$config['total_rows'] = $this->voucher_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['vouchers'] = $this->voucher_m->get_all_vouchers(
$config["per_page"],
$this->uri->segment(4)
);
$this->data['use_pagination'] = 'yes';
}
//get birthday promo setting
$this->data['birthdaypromo'] = $this->db->select('birthdaypromo_active, birthdaypromo_productids')->from('configuration')->where('id_configuration', 1)->get()->row();
//load view
$this->data['subview'] = 'admin/vouchers/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 voucher
public function add()
{
if ($this->data['allowed'] == false) {
redirect('admin/dashboard');
}
$this->data['vouchers'] = $this->voucher_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->voucher_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($created_date, $new_expired_date);
$this->voucher_m->add_voucher($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Voucher Add Successful</p>');
redirect('admin/vouchers');
}
//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();
//get all sales agents data from sales_list table
$this->db->select('id, employee_id, sales_name')->from('sales_list')->order_by('sales_name', 'ASC');
$this->data['salesagents'] = $this->db->get()->result();
//load view
$this->data['subview'] = 'admin/vouchers/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 voucher in admin
public function edit($id = NULL)
{
//check if id exist. If not exist, show 404.
$count = $this->voucher_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['vouchers'] = $this->voucher_m->get($id);
//get all chosen (active) categories
$active_categories = $this->data['vouchers']->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['vouchers']->brandpromo;
if ($active_brands != NULL) {
$this->data['chosen_brands'] = array_filter(explode(',', $active_brands)); //array_filter to remove empty array element
}
//get chosen promo voucher for poduct and its category
$this->data['product_category_id'] = $this->data['vouchers']->product_category_id;
$this->data['product_id'] = $this->data['vouchers']->product_id;
//get products
$this->data['products_id'] = $this->db->select('id_product')->from('category_product')->where('id_category', $this->data['product_category_id'])->get()->result();
$this->voucher_current_id = (int) $id;
//validation check in action
$config = $this->voucher_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('vouchers')->where('id_vouchers', (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($created_date, $new_expired_date);
$this->voucher_m->edit_voucher($id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Voucher Edit Successful</p>');
redirect('admin/vouchers/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();
//get all sales agents data from sales_list table
$this->db->select('id, employee_id, sales_name')->from('sales_list')->order_by('sales_name', 'ASC');
$this->data['salesagents'] = $this->db->get()->result();
//load view
$this->data['subview'] = 'admin/vouchers/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 voucher
public function delete($id)
{
if ($this->data['allowed'] == false || $this->data['role'] == 'admin') {
redirect('admin/dashboard');
}
//check if id exist. If not exist, show 404.
$count = $this->voucher_m->count_exist($id);
if ($count == 0) {
//page not exist
show_404();
}
//delete voucher
$this->voucher_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Voucher Delete Successful</p>');
redirect('admin/vouchers');
}
public function delete_voucher_by_name()
{
//get product name from form
$this->data['name'] = $this->security->xss_clean($this->input->post('voucher_name'));
$this->db->where('voucher_name', $this->data['name']);
$this->db->delete('vouchers');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Voucher Delete Successful</p>');
redirect('admin/vouchers');
}
private function table_data_processing($created_date, $new_expired_date)
{
$data = array(
'voucher_name' => $this->security->xss_clean($this->input->post('voucher_name')),
'voucher_code' => $this->security->xss_clean($this->input->post('voucher_code')),
'discount_type' => $this->input->post('discount_type'),
'discount_value' => (int) $this->security->xss_clean($this->input->post('discount_value')),
'created_date' => $created_date,
'expired_date' => $new_expired_date,
'voucher_type' => $this->input->post('voucher_type'),
'birthmonth' => $this->input->post('birthmonth'),
'gender' => $this->input->post('gender'),
'promostart' => $this->input->post('promostart'),
'promoend' => $this->input->post('promoend'),
'provincepromo' => $this->input->post('provincepromo'),
'quantitypromo' => $this->input->post('quantitypromo'),
'product_category_id' => $this->input->post('product_category_id'),
'product_id' => $this->input->post('product_id'),
'sales_agent_id' => $this->input->post('sales_agent_id'),
'weekend' => $this->input->post('weekend')
);
if ($this->input->post('qty_ready') == '') {
$data['qty_ready'] = NULL;
} else {
$data['qty_ready'] = (int) $this->security->xss_clean($this->input->post('qty_ready'));
}
if ($this->input->post('maxqty_per_person') == '') {
$data['maxqty_per_person'] = NULL;
} else {
$data['maxqty_per_person'] = (int) $this->security->xss_clean($this->input->post('maxqty_per_person'));
}
if ($this->input->post('min_order') == '') {
$data['min_order'] = NULL;
} else {
$data['min_order'] = (int) $this->security->xss_clean($this->input->post('min_order'));
}
if ($this->input->post('product_category_id') == '') {
$data['product_category_id'] = NULL;
} else {
$data['product_category_id'] = (int) $this->security->xss_clean($this->input->post('product_category_id'));
}
if ($this->input->post('product_id') == '') {
$data['product_id'] = NULL;
} else {
$data['product_id'] = (int) $this->security->xss_clean($this->input->post('product_id'));
}
if ($this->input->post('sales_agent_id') == '') {
$data['sales_agent_id'] = NULL;
} else {
$data['sales_agent_id'] = (int) $this->security->xss_clean($this->input->post('sales_agent_id'));
}
//Promo by Product Category
//get category_id from view,
$categories_id = $this->input->post('category_id');
//check id there is content inside category array
$categories_id_count = count($categories_id);
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
$brands_id_count = count($brands_id);
if ($brands_id_count > 0) {
$data['brandpromo'] = implode(',', $brands_id);
} else {
$data['brandpromo'] = NULL;
}
return $data;
}
//callback function validation add new voucher
//make it private by adding _
public function _cek_existing_voucher_code($str)
{
$num_rows = $this->voucher_m->cek_existing_voucher_code($str, $this->voucher_current_id);
if ($num_rows != 0) {
$this->form_validation->set_message('_cek_existing_voucher_code', 'voucher code already exist !');
return FALSE;
} else {
return TRUE;
}
}
public function upload_vouchers()
{
//upload products data
if (!isset($_POST['upload_csv'])) {
show_404();
}
//check if the uploaded file is csv format
$mimes = array('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 = array();
//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('voucher_code')->from('vouchers')->where('voucher_code', $item[0]);
$count_code = $this->db->get()->num_rows();
if ($count_code > 0) {
//voucher code already exist. update only..
} else {
//insert new vouchers
//add to vouchers table
$data = array(
'voucher_code' => $item[0],
'voucher_name' => $item[1],
'voucher_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 = 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 = 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 vouchers table
$this->db->insert('vouchers', $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;">Vouchers CSV Imported.</p>');
redirect('admin/vouchers');
} else {
//not a csv file. Not allowed.
die('Sorry, file type not allowed. Please upload only CSV file.');
}
}
function upload_vouchers_xls() {
if(!$_POST) {
redirect('admin/vouchers');
}
$config['upload_path'] = 'uploads/excel/';
$config['allowed_types'] = 'xlsx|xls';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')){
$this->session->set_flashdata('error', '<p style="background:red; color:white; padding:5px; font-weight:bold;">'.strip_tags($this->upload->display_errors()).'</p>');
redirect('admin/vouchers');
}
else{
require_once APPPATH . 'third_party/PHPExcel/IOFactory.php';
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data(); //Mengambil detail data yang di upload
$filename = $upload_data['file_name'];//Nama File
//ini_set('memory_limit', '-1');
$inputFileName = 'uploads/excel/'.$filename;
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
}
catch(Exception $e) {
die('Error loading file :' . $e->getMessage());
}
$worksheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$numRows = count($worksheet);
for ($i=1; $i < $numRows+1 ; $i++) {
if ($i > 500) {
break;
}
if($i != 1){
$voucher_code = $worksheet[$i]["A"];
$voucher_name = $worksheet[$i]["B"];
$voucher_type = $worksheet[$i]["C"];
$voucher_params = $worksheet[$i]["D"];
$qty_ready = $worksheet[$i]["E"];
$max_qty_person = $worksheet[$i]["F"];
$min_total_order = $worksheet[$i]["G"];
$discount_type = $worksheet[$i]["H"];
$discount_amount = $worksheet[$i]["I"];
$expired_date = $worksheet[$i]["J"];
$data = array(
'voucher_name' => $voucher_name,
'voucher_code' => $voucher_code,
'voucher_type' => $voucher_type,
'discount_type' => $discount_type,
'discount_value' => (int)$discount_amount,
'expired_date' => $expired_date,
'qty_ready' => $qty_ready,
'maxqty_per_person' => $max_qty_person,
'min_order' => $min_total_order,
);
switch (strtolower($voucher_type)) {
case 'normal promo':
break;
case 'birthday promo':
$data['birthmonth'] = $voucher_params;
break;
case 'time promo':
if (strpos($voucher_params, '|') !== false) {
//must have | for start and end time
$timepromo_array = explode('|', $voucher_params);
$data['promostart'] = trim($timepromo_array[0]);
$data['promoend'] = trim($timepromo_array[1]);
}
break;
case 'quantity promo':
$data['quantitypromo'] = $voucher_params;
break;
case 'province promo':
$this->db->select('rajaongkir_province_id')->from('indonesia_provinces')->where('province', $voucher_params);
$province_id = $this->db->get()->row();
if(count($province_id) > 0) {
$data['provincepromo'] = $province_id->rajaongkir_province_id;
}
break;
case 'category promo':
if (strpos($voucher_params, '|') !== false) {
//has more than 1 category
$category_array = explode('|', $voucher_params);
$category_id_array = 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($voucher_params));
$category_id = $this->db->get()->row();
if(count($category_id) > 0) {
$data['categorypromo'] = $category_id->id_categories;
}
}
break;
case 'product promo':
//count if this product exist
$count_product = $this->db->select('id_products')->from('products')->where('sku', trim($voucher_params))->get()->num_rows();
if($count_product > 0) {
//product exit, continue...
//get product id
$data['product_id'] = $this->db->select('id_products')->from('products')->where('sku', trim($voucher_params))->get()->row()->id_products;
//get top level category
$data['product_category_id'] = $this->db->select('id_category')->from('category_product')->where('id_product', $data['product_id'])->get()->row()->id_category;
}
break;
case 'brand promo':
if (strpos($voucher_params, '|') !== false) {
//has more than 1 brand
$brand_array = explode('|', $voucher_params);
$brand_id_array = 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($voucher_params));
$brand_id = $this->db->get()->row();
if(count($brand_id) > 0) {
$data['brandpromo'] = $brand_id->id_brands;
}
}
break;
}
$this->db->insert('vouchers', $data);
$id_vouchers = $this->db->insert_id();
}
}
unlink('uploads/excel/'.$filename);
$this->session->set_flashdata('success', '<p style="background:green; color:white; padding:5px; font-weight:bold;">Vouchers XLSX/XLS Imported.</p>');
redirect('admin/vouchers');
}
}
public function ajax_get_products()
{
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$category_id = (int) $this->input->post('category_id');
//get products
$data['products_id'] = $this->db->select('id_product')->from('category_product')->where('id_category', $category_id)->get()->result();
$this->load->view('admin/vouchers/ajax_get_products', $data);
}
public function update_birthday_promo() {
$data = array(
'birthdaypromo_active' => $this->input->post('birthdaypromo_active'),
'birthdaypromo_productids' => $this->input->post('productids')
);
$this->db->where('id_configuration', 1);
$this->db->update('configuration', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Birthday Promo Edit Successful</p>');
redirect('admin/vouchers');
}
}