|
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/vajra.id/public_html/application/controllers/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Summary extends Customer_Controller {
function __construct() {
parent::__construct();
$this->load->model('cart_model');
$this->load->model('product_m');
$this->load->model('customer_m');
$this->load->library('cart');
$this->load->helper('rajaongkir');
$this->load->library('form_validation');
}
public function index() {
if (!$this->cart->contents()) {
$data['message'] = '<p style="background:#564E52; color:white; padding:10px; margin-bottom:100px;">Your cart is empty</p>';
} else {
$data['message'] = $this->session->flashdata('message');
$id_customer = (int) $this->session->userdata('customer')['customer_id'];
//get customer shipping address detail
$data['shipping'] = $this->customer_m->get_shipping($id_customer);
//get toc
$this->db->select('toc')->from('toc')->where('id_toc', 1);
$data['toc'] = $this->db->get()->row();
//check if Tax is active
$this->db->select('ppn_active')->from('configuration')->where('id_configuration', 1);
$data['tax_active'] = $this->db->get()->row()->ppn_active;
//get veritrans fee
$this->db->select('veritrans_fee, veritrans_admin, virtualaccount_admin')->from('configuration')->where('id_configuration', 1);
$veritrans = $this->db->get()->row();
$veritrans_fee = $veritrans->veritrans_fee;
$veritrans_admin = $veritrans->veritrans_admin;
$virtualaccount_admin = $veritrans->virtualaccount_admin;
$this->session->set_userdata('veritrans_transaction_fee', $veritrans_fee);
$this->session->set_userdata('veritrans_admin_fee', $veritrans_admin);
$this->session->set_userdata('virtualaccount_admin_fee', $virtualaccount_admin);
//if session not exist yet, then initialize new session on bank transfer
if (!$this->session->userdata('chosen_payment_type')) {
$this->session->set_userdata('chosen_payment_type', 'bank_transfer');
}
//check if customer is Dropshipper
$this->db->select('dropship')->from('customers')->where('id_customers', $id_customer);
$dropship_status = $this->db->get()->row()->dropship;
if($dropship_status == 'yes') {
//customer is a dropshipper
//if session not exist yet, then initialize new session on
if (!$this->session->userdata('choose_dropship_status')) {
$this->session->set_userdata('choose_dropship_status', 'yes');
}
}
//calculate shipping fee
//get total weight
$total_weight_gram = 0;
$data['product_grand_total'] = 0;
$cart = $this->cart->contents();
foreach ($cart as $item) {
$item_id = (int) $item['id'];
//get product weight
$this->db->select('weight')->from('product_details')->where('product_id', $item_id)->where('sku', $item['options']['sku']);
$item_weight = $this->db->get()->row()->weight * $item['qty'];
$total_weight_gram = $total_weight_gram + $item_weight;
$data['product_grand_total'] = $data['product_grand_total'] + ($item['price'] * $item['qty']);
}
//get active carrier from configuration table..
$this->db->select('jne_reg, jne_yes, jne_oke, tiki_eco, tiki_reg, tiki_ons, pos_regular, pos_express, rpx_regular, rpx_retail, shop_delivery, shopdelivery_duration, shopdelivery_fee, gosend, gosend_fee, gosend_minimum_fee')->from('configuration')->where('id_configuration', 1);
$data['active_carrier'] = $this->db->get()->row();
$data['shopdelivery_duration'] = $data['active_carrier']->shopdelivery_duration;
//get shop ID from configuration table
$this->db->select('shop_district_id, shop_subdistrict_id')->from('configuration')->where('id_configuration', 1);
$shop_ids = $this->db->get()->row();
$shop_district_id = $shop_ids->shop_district_id;
$shop_subdistrict_id = $shop_ids->shop_subdistrict_id;
//get destination country
//shipping_id_country 0 is indonesia, else other countries
$destination_country_id = (int) $data['shipping']->shipping_id_country;
if($destination_country_id == 0) {
//destination country is Indonesia
//get subdistrict destination ID
$destination_subdistrict_id = (int) $data['shipping']->shipping_id_subdistrict;
//get all shipping cost for display at summary page
if($data['active_carrier']->jne_reg == 1) {
$data['jne_regular_fee'] = $this->get_carrier_cost('jne', 'JNE REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG', 'CTC');
}
if($data['active_carrier']->jne_yes == 1) {
$data['jne_yes_fee'] = $this->get_carrier_cost('jne', 'JNE YES', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'YES', 'CTCYES');
}
if($data['active_carrier']->jne_oke == 1) {
$data['jne_oke_fee'] = $this->get_carrier_cost('jne', 'JNE OKE', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'OKE');
}
if($data['active_carrier']->tiki_reg == 1) {
$data['tiki_reg_fee'] = $this->get_carrier_cost('tiki', 'TIKI REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG');
}
if($data['active_carrier']->tiki_ons == 1) {
$data['tiki_ons_fee'] = $this->get_carrier_cost('tiki', 'TIKI ONS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ONS');
}
if($data['active_carrier']->tiki_eco == 1) {
$data['tiki_eco_fee'] = $this->get_carrier_cost('tiki', 'TIKI ECO', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ECO');
}
if($data['active_carrier']->pos_regular == 1) {
$data['pos_regular_fee'] = $this->get_carrier_cost('pos', 'POS KILAT KHUSUS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'Surat Kilat Khusus');
}
if($data['active_carrier']->pos_express == 1) {
$data['pos_express_fee'] = $this->get_carrier_cost('pos', 'POS EXPRESS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'Express Next Day');
}
if($data['active_carrier']->rpx_regular == 1) {
$data['rpx_regular_fee'] = $this->get_carrier_cost('rpx', 'RPX REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'RGP');
}
if($data['active_carrier']->rpx_retail == 1) {
$data['rpx_retail_fee'] = $this->get_carrier_cost('rpx', 'RPX RETAIL', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REP');
}
if($data['active_carrier']->shop_delivery == 1) {
$data['shop_delivery_fee'] = $data['active_carrier']->shopdelivery_fee;
}
//set shipping cost
if($this->session->userdata('carrier_name') != NULL) {
//carrier already exist
switch($this->session->userdata('carrier_name')) {
case 'JNE REGULAR':
$data['total_shipping_fee'] = $data['jne_regular_fee'];
break;
case 'JNE YES':
$data['total_shipping_fee'] = $data['jne_yes_fee'];
break;
case 'JNE OKE':
$data['total_shipping_fee'] = $data['jne_oke_fee'];
break;
case 'TIKI REGULAR':
$data['total_shipping_fee'] = $data['tiki_reg_fee'];
break;
case 'TIKI ONS':
$data['total_shipping_fee'] = $data['tiki_ons_fee'];
break;
case 'TIKI ECO':
$data['total_shipping_fee'] = $data['tiki_eco_fee'];
break;
case 'POS KILAT KHUSUS':
$data['total_shipping_fee'] = $data['pos_regular_fee'];
break;
case 'POS EXPRESS':
$data['total_shipping_fee'] = $data['pos_express_fee'];
break;
case 'RPX REGULAR':
$data['total_shipping_fee'] = $data['rpx_regular_fee'];
break;
case 'RPX RETAIL':
$data['total_shipping_fee'] = $data['rpx_retail_fee'];
break;
case 'SHOP DELIVERY':
$data['total_shipping_fee'] = $data['shop_delivery_fee'];
break;
case 'GO-SEND':
if($this->session->userdata('destination_latitude') && $this->session->userdata('destination_longitude')) {
//get shop latitude and longitude, and gosend detail
$this->db->select('shop_latitude, shop_longitude, gosend_fee, gosend_minimum_fee')->from('configuration')->where('id_configuration', 1);
$shop_location = $this->db->get()->row();
$shop_latitude = $shop_location->shop_latitude;
$shop_longitude = $shop_location->shop_longitude;
$gosend_fee = $shop_location->gosend_fee; //fee per km
$gosend_minimum_fee = $shop_location->gosend_minimum_fee;
$destination_latitude = $this->session->userdata('destination_latitude');
$destination_longitude = $this->session->userdata('destination_longitude');
//get distance from google map api
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$shop_latitude.",".$shop_longitude."&destinations=".$destination_latitude.",".$destination_longitude."&mode=driving";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$distance = $response_a['rows'][0]['elements'][0]['distance']['text'];
//$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
$distance = $response_a['rows'][0]['elements'][0]['distance']['value'] /1000; //distance in km
$gosend_fee = $distance * $gosend_fee;
if($gosend_fee >= $gosend_minimum_fee) {
//$data['total_shipping_fee'] = round($gosend_fee + 4000, -3); //add buffer 4rb parkir
$data['total_shipping_fee'] = round($gosend_fee, -3);
} else {
$data['total_shipping_fee'] = $gosend_minimum_fee;
}
}
break;
}
} else {
//carrier not exist yet, so set initial carrier
//get all shipping cost for display at summary page
if($data['active_carrier']->jne_reg == 1) {
$data['total_shipping_fee'] = $data['jne_regular_fee'];
$this->session->set_userdata('carrier', 'jne');
$this->session->set_userdata('carrier_name', 'JNE REGULAR');
} else {
if($data['active_carrier']->tiki_reg == 1) {
$data['total_shipping_fee'] = $data['tiki_reg_fee'];
$this->session->set_userdata('carrier', 'tiki');
$this->session->set_userdata('carrier_name', 'TIKI REGULAR');
} else {
if($data['active_carrier']->pos_regular == 1) {
$data['total_shipping_fee'] = $data['pos_regular_fee'];
$this->session->set_userdata('carrier', 'pos');
$this->session->set_userdata('carrier_name', 'POS KILAT KHUSUS');
} else {
if($data['active_carrier']->rpx_regular == 1) {
$data['total_shipping_fee'] = $data['rpx_regular_fee'];
$this->session->set_userdata('carrier', 'rpx');
$this->session->set_userdata('carrier_name', 'RPX REGULAR');
} else {
if($data['active_carrier']->shop_delivery == 1) {
$data['shop_delivery_fee'] = $data['active_carrier']->shopdelivery_fee;
$this->session->set_userdata('carrier', 'shop_delivery');
$this->session->set_userdata('carrier_name', 'SHOP DELIVERY');
}
}
}
}
}
}
} else {
//shipping to outside indonesia
$this->session->set_userdata('carrier', 'Tiki International');
$this->session->set_userdata('carrier_name', 'Tiki International');
$rajaongkir_cost = get_rajaongkir_ongkos_overseas($shop_district_id, $destination_country_id, $total_weight_gram);
/* echo '<pre>';
print_r($rajaongkir_cost);
echo '</pre>'; */
//check if weight is zero. If zero, then rajaongkir cannot proceed..
if($total_weight_gram > 0) {
if(count($rajaongkir_cost['rajaongkir']['results'][0]['costs']) != 0) {
//cost is available because have cost array...
foreach($rajaongkir_cost['rajaongkir']['results'][0]['costs'] as $key => $result) {
if($result['service'] == 'Paket') {
//means paket is available. so use paket price..
$data['total_shipping_fee'] = $result['cost'];
break;
} else {
//means paket is not available, so use dokumen price
$data['total_shipping_fee'] = $result['cost'];
}
}
} else {
//cost is not available, because cost array is empty..
$data['total_shipping_fee'] = NULL; //service is not available
}
} else {
//total weight gram is zero
$data['total_shipping_fee'] = NULL; //service is not available
}
}
//get free shipping min purchase amount from configuration table
$this->db->select('free_shipping')->from('configuration')->where('id_configuration', 1);
$data['free_shipping'] = $this->db->get()->row()->free_shipping;
if ($data['free_shipping'] != NULL) {
if ($data['product_grand_total'] >= $data['free_shipping']) {
$data['total_shipping_fee'] = 0;
}
}
/* //check if voucher type is free shipping
if($this->session->userdata('chosen_voucher_type') == 'free shipping') {
$data['total_shipping_fee'] = 0;
} */
//CHECK IF DELIVERY ADDRESS IS THE SAME WITH SHOP ADDRESS, FREE SHIPPING FOR SAME AREA;
$this->db->select('free_shipping_shop_area')->from('configuration')->where('id_configuration', 1);
$free_shipping_shop_area = $this->db->get()->row()->free_shipping_shop_area;
//get shipping province id
if($free_shipping_shop_area == 'yes') {
//check if customer destination district is the same with shop district
//$shop_district_id
if($shop_district_id == 151 || $shop_district_id == 152 || $shop_district_id == 153 || $shop_district_id == 154 || $shop_district_id == 155) {
//shop is in jakarta area
if($data['shipping']->shipping_id_district == 151 || $data['shipping']->shipping_id_district == 152 || $data['shipping']->shipping_id_district == 153 || $data['shipping']->shipping_id_district == 154 || $data['shipping']->shipping_id_district == 155) {
$data['total_shipping_fee'] = 0;
$data['free_shipping_shop_area'] = 'Jakarta';
}
} else {
//shop is outside jakarta area
if($shop_district_id == $data['shipping']->shipping_id_district) {
$data['total_shipping_fee'] = 0;
$data['free_shipping_shop_area'] = $data['shipping']->shipping_district;
}
}
}
//SET GRAND TOTAL
if($data['tax_active'] == 'yes') { //add tax 10%
$data['grand_total'] = $data['total_shipping_fee'] + $data['product_grand_total'] + (10/100*$data['product_grand_total']) - $this->session->userdata('chosen_point_discount') ;
} else {
$data['grand_total'] = $data['total_shipping_fee'] + $data['product_grand_total'] - $this->session->userdata('chosen_point_discount');
}
//MASALAH DISINI....SESSION BRAND TIDAK TERDETEKSI
if($this->session->userdata('chosen_voucher_type') == 'amount') {
$data['grand_total'] = $data['grand_total'] - $this->session->userdata('chosen_voucher_discount');
$this->session->set_userdata('redeemed_voucher_amount', $this->session->userdata('chosen_voucher_discount'));
} elseif($this->session->userdata('chosen_voucher_type') == 'percentage') {
if($this->session->userdata('total_categoryproduct_promo')) {
//product category promo exist
$data['grand_total'] = $data['grand_total'] - $this->session->userdata('total_categoryproduct_promo');
$this->session->set_userdata('redeemed_voucher_amount', $this->session->userdata('total_categoryproduct_promo'));
} elseif($this->session->userdata('total_brandproduct_promo')) {
//product brand promo exist
$data['grand_total'] = $data['grand_total'] - $this->session->userdata('total_brandproduct_promo');
$this->session->set_userdata('redeemed_voucher_amount', $this->session->userdata('total_brandproduct_promo'));
} else {
$data['grand_total'] = $data['grand_total'] - ($data['product_grand_total'] * $this->session->userdata('chosen_voucher_discount')/100);
$this->session->set_userdata('redeemed_voucher_amount', $data['product_grand_total'] * $this->session->userdata('chosen_voucher_discount')/100);
}
} else {
//voucher type is free shipping
//do nothing...
}
//get point rewards setting
$this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1);
$data['point_rewards'] = $this->db->get()->row();
//get customer current point reward
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $id_customer);
$data['current_point'] = $this->db->get()->row()->current_pointreward;
//get payment displaysetting
$this->db->select('bank_transfer, credit_card, virtual_account, cod')->from('configuration')->where('id_configuration', 1);
$data['payment_display'] = $this->db->get()->row();
} /*---end if(!$this->cart->contents() --*/
//get SEO
$this->db->select('website_name, meta_keywords')->from('configuration')->where('id_configuration', 1);
$website_name = $this->db->get()->row();
$this->data_header['browser_title'] = ucwords($website_name->website_name) . ' - My Summary';
$this->data_header['meta_description'] = ucwords($website_name->website_name) . ' - My Summary';
$this->data_header['meta_keywords'] = $website_name->meta_keywords;
$this->load->view('template/header', $this->data_header);
$this->load->view('summary', $data);
$this->load->view('template/footer', $this->data_footer);
}
private function get_carrier_cost($carrier, $carrier_name, $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, $service_code1, $service_code2 = NULL) {
$rajaongkir_cost = get_rajaongkir_ongkos($shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, $carrier);
/* echo '<pre>';
print_r($rajaongkir_cost);
echo '</pre>'; */
//check if weight is zero. If zero, then rajaongkir cannot proceed..
if($total_weight_gram > 0) {
//check which key has carrier name
if($service_code2 != NULL) {
if(count($rajaongkir_cost['rajaongkir']['results'][0]['costs']) != 0) {
foreach($rajaongkir_cost['rajaongkir']['results'][0]['costs'] as $key => $result) {
if($result['service'] == $service_code1 || $result['service'] == $service_code2) {
$total_shipping_fee = $result['cost'][0]['value'];
break;
} else {
$total_shipping_fee = NULL; //service is not available
}
}
} else {
$total_shipping_fee = NULL; //service is not available
}
} else {
if(count($rajaongkir_cost['rajaongkir']['results'][0]['costs']) != 0) {
foreach($rajaongkir_cost['rajaongkir']['results'][0]['costs'] as $key => $result) {
if($result['service'] == $service_code1) {
$total_shipping_fee = $result['cost'][0]['value'];
break;
} else {
$total_shipping_fee = NULL; //service is not available
}
}
} else {
$total_shipping_fee = NULL; //service is not available
}
}
} else {
//total weight gram is zero
$total_shipping_fee = NULL; //service is not available
}
return $total_shipping_fee;
}
public function remove($rowid) {
if ($rowid=='all'){
$this->cart->destroy();
} else {
$data = array(
'rowid' => $rowid,
'qty' => 0
);
$this->cart->update($data);
}
$this->session->unset_userdata('chosen_voucher_code');
$this->session->unset_userdata('chosen_voucher_type');
$this->session->unset_userdata('chosen_voucher_discount');
$this->session->unset_userdata('total_categoryproduct_promo');
$this->session->unset_userdata('total_brandproduct_promo');
$this->session->unset_userdata('redeemed_voucher_amount');
redirect('summary');
}
public function get_vouchers() {
if(!isset($_POST['submit_voucher_code'])) { redirect('summary'); }
//unset this userdata if user enter other voucher code than category or brand product code
$this->session->unset_userdata('total_categoryproduct_promo');
$this->session->unset_userdata('total_brandproduct_promo');
//validation in action
$config = array(
array(
'field' => 'voucher_code',
'label' => 'voucher code',
'rules' => 'trim|required|alpha_numeric|callback__check_voucher_code'
),
);
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$voucher_code = $this->security->xss_clean($this->input->post('voucher_code'));
//get discount type and amount
$this->db->select('*')->from('vouchers')->where('voucher_code', $voucher_code);
$voucher = $this->db->get()->row();
$this->session->set_userdata('chosen_voucher_code', $voucher->voucher_code);
$this->session->set_userdata('chosen_voucher_type', $voucher->discount_type);
$this->session->set_userdata('chosen_voucher_discount', (int) $voucher->discount_value);
if($voucher->voucher_type == 'category promo') {
if($voucher->discount_type == 'percentage') {
//discount type by percentage..here need to calculate discount for specific products whose categories are matched only..
$discount_rate = $voucher->discount_value;
//get categories id from voucher code
$this->db->select('categorypromo')->from('vouchers')->where('voucher_code', $voucher_code);
$categories = $this->db->get()->row()->categorypromo;
$categories_array = explode(',', $categories);
//get current cart content
$cart = $this->cart->contents();
$total_amount_promoted_categories = 0;
foreach ($cart as $item) {
//check if this item has category which is match with $categories_array
$this->db->select('id_category')->from('category_product')->where('id_product', $item['id']);
$categories_id = $this->db->get()->result();
$count_category_id = 0;
foreach($categories_id as $category_id) {
if(in_array($category_id->id_category, $categories_array)) {
$count_category_id = $count_category_id + 1;
}
}
if($count_category_id > 0) {
//this $item has category which is match with $categories_array, so we can add to percentage discounts
$total_amount_promoted_categories = $total_amount_promoted_categories + ($item['price'] * $item['qty'] * $discount_rate / 100);
}
}
$this->session->set_userdata('total_categoryproduct_promo', (int) $total_amount_promoted_categories);
}
}
if($voucher->voucher_type == 'brand promo') {
if($voucher->discount_type == 'percentage') {
//discount type by percentage..here need to calculate discount for specific products whose brands are matched only..
$discount_rate = $voucher->discount_value;
//get brands id from voucher code
$this->db->select('brandpromo')->from('vouchers')->where('voucher_code', $voucher_code);
$brands = $this->db->get()->row()->brandpromo;
$brands_array = explode(',', $brands);
//get current cart content
$cart = $this->cart->contents();
$total_amount_promoted_brands = 0;
foreach ($cart as $item) {
//check if this item has brand which is match with $brands_array
$this->db->select('brand_id')->from('products')->where('id_products', $item['id']);
$brand_id = $this->db->get()->row()->brand_id;
if(in_array($brand_id, $brands_array)) {
//this $item has brand which is match with $brands_array, so we can add to percentage discounts
$total_amount_promoted_brands = $total_amount_promoted_brands + ($item['price'] * $item['qty'] * $discount_rate / 100);
}
}
$this->session->set_userdata('total_brandproduct_promo', (int) $total_amount_promoted_brands);
}
}
$this->session->set_flashdata('summary_message', 'VOUCHER CODE REDEEMED');
$this->index();
} else {
//form validation not pass..
$this->session->unset_userdata('chosen_voucher_code');
$this->session->unset_userdata('chosen_voucher_type');
$this->session->unset_userdata('chosen_voucher_discount');
$this->session->unset_userdata('total_categoryproduct_promo');
$this->session->unset_userdata('total_brandproduct_promo');
$this->session->unset_userdata('redeemed_voucher_amount');
$this->index();
}
}
public function _check_voucher_code($str) {
//check if input voucher code is really exist
$this->db->select('id_vouchers')->from('vouchers')->where('voucher_code', $str);
$count_voucher = $this->db->get()->num_rows();
if ($count_voucher == 0) {
$this->form_validation->set_message('_check_voucher_code', 'Voucher Code Not Exist!');
return FALSE;
}
//check if the voucher quantity already empty
$this->db->select('qty_ready')->from('vouchers')->where('voucher_code', $str);
$qty_ready = $this->db->get()->row()->qty_ready;
if ($qty_ready == 0 && $qty_ready != NULL) {
$this->form_validation->set_message('_check_voucher_code', 'Voucher Code Used Up!');
return FALSE;
}
//check if the voucher usage already exceed max customer usage
//get max quantity
$this->db->select('maxqty_per_person')->from('vouchers')->where('voucher_code', $str);
$maxqty_per_person = $this->db->get()->row()->maxqty_per_person;
if ($maxqty_per_person != NULL) {
//get customer current voucher usage
$customer_id = (int) $this->session->userdata('customer')['customer_id'];
//get voucher id
$this->db->select('id_vouchers')->from('vouchers')->where('voucher_code', $str);
$voucher_id = (int) $this->db->get()->row()->id_vouchers;
//check on customer voucher_user table, if exist
$this->db->select('*')->from('voucher_users')->where('voucher_id', $voucher_id)->where('customer_id', $customer_id);
$count_user = $this->db->get()->num_rows();
if ($count_user > 0) {
//get current voucher usage
$this->db->select('voucher_used')->from('voucher_users')->where('voucher_id', $voucher_id)->where('customer_id', $customer_id);
$voucher_used = (int) $this->db->get()->row()->voucher_used;
//if the user voucher already exceed max quota
if ($voucher_used >= $maxqty_per_person) {
$this->form_validation->set_message('_check_voucher_code', 'You have used max allowed no. of vouchers / customer');
return FALSE;
}
}
}
//check for minimum order
$grand_total_without_shipping = (int) $this->session->userdata('grand_total_without_shipping'); //this value is taken from summary page
//get minimum order from voucher table
$this->db->select('min_order')->from('vouchers')->where('voucher_code', $str);
$min_order = $this->db->get()->row()->min_order;
if ($min_order != NULL) {
if ($grand_total_without_shipping < (int) $min_order) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry Your order amount is not enough');
return FALSE;
}
}
//Check for expired date
//get expired date for this voucher
$this->db->select('expired_date')->from('vouchers')->where('voucher_code', $str);
$expired_date = $this->db->get()->row()->expired_date;
if ($expired_date != NULL) {
$expired_date_numbers = strtotime($expired_date);
$current_date_numbers = strtotime(date('Y-m-d H:i:s'));
if ($current_date_numbers > $expired_date_numbers) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry Your Voucher Code Already Expired');
return FALSE;
}
}
//get voucher type
$this->db->select('voucher_type')->from('vouchers')->where('voucher_code', $str);
$voucher_type = $this->db->get()->row()->voucher_type;
$customer_id = (int) $this->session->userdata('customer')['customer_id'];
switch ($voucher_type) {
case 'normal promo':
# do nothing...
break;
case 'birthday promo':
//get birthmonth
$this->db->select('birthmonth')->from('vouchers')->where('voucher_code', $str);
$voucher_birthmonth = (int) $this->db->get()->row()->birthmonth;
//get customer birthmonth
$this->db->select('birthday')->from('customers')->where('id_customers', $customer_id);
$birthday = $this->db->get()->row()->birthday;
$birthday_array = explode('-',$birthday);
$customer_birthmonth = (int) $birthday_array[1];
if ($voucher_birthmonth != $customer_birthmonth) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry It is not Your Birth Month');
return FALSE;
}
break;
case 'gender promo':
//get gender
$this->db->select('gender')->from('vouchers')->where('voucher_code', $str);
$voucher_gender = $this->db->get()->row()->gender;
//get customer gender
$this->db->select('title')->from('customers')->where('id_customers', $customer_id);
$customer_title = $this->db->get()->row()->title;
if ($customer_title == 'mr') {
$customer_gender = 'male';
} else {
$customer_gender = 'female';
}
if ($voucher_gender != $customer_gender) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry It is not Your Gender');
return FALSE;
}
break;
case 'time promo':
//strtotime means convert date string d-m-Y to time froom 1970 unix time
//get start promo time
$this->db->select('promostart')->from('vouchers')->where('voucher_code', $str);
$promostart = strtotime($this->db->get()->row()->promostart);
//get end promo time
$this->db->select('promoend')->from('vouchers')->where('voucher_code', $str);
$promoend = strtotime($this->db->get()->row()->promoend);
//get current date and time
$currentdatetime = strtotime(date('Y-m-d H:i:s'));
if ($currentdatetime > $promostart && $currentdatetime < $promoend) {
//time range is correct, promo is valid
//do nothing..
} else {
//time range is false, so promo is not valid
$this->form_validation->set_message('_check_voucher_code', 'Sorry Promo Time expired');
return FALSE;
}
break;
case 'province promo':
//get province_id
$this->db->select('provincepromo')->from('vouchers')->where('voucher_code', $str);
$voucher_province_id = (int) $this->db->get()->row()->provincepromo;
//get customer province_id
$this->db->select('id_province')->from('customers')->where('id_customers', $customer_id);
$customer_province_id = $this->db->get()->row()->id_province;
if ($voucher_province_id != $customer_province_id) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry It is not Your Province');
return FALSE;
}
break;
case 'quantity promo':
//get min quantity at checkout
$this->db->select('quantitypromo')->from('vouchers')->where('voucher_code', $str);
$voucher_quantitypromo = (int) $this->db->get()->row()->quantitypromo;
//get current combined product quantity at cart
$cart = $this->cart->contents();
$total_cart_quantity = 0;
foreach ($cart as $item) {
$total_cart_quantity = $total_cart_quantity + $item['qty'];
}
if ($total_cart_quantity < $voucher_quantitypromo) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry Your Total Cart Quantity not enough');
return FALSE;
}
break;
case 'category promo':
//get categories id from voucher code
$this->db->select('categorypromo')->from('vouchers')->where('voucher_code', $str);
$categories = $this->db->get()->row()->categorypromo;
$categories_array = explode(',', $categories);
//get current cart content
$cart = $this->cart->contents();
$count_category_exist = 0;
foreach ($cart as $item) {
//get category_id from each item
$this->db->select('id_category')->from('category_product')->where('id_product', $item['id']);
$products_category = $this->db->get()->result();
foreach($products_category as $category_item) {
if(in_array($category_item->id_category, $categories_array)) {
$count_category_exist = $count_category_exist + 1;
}
}
}
if($count_category_exist == 0) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry You did not choose Products with Promoted Category');
return FALSE;
}
break;
case 'brand promo':
//get brands id from voucher code
$this->db->select('brandpromo')->from('vouchers')->where('voucher_code', $str);
$brands = $this->db->get()->row()->brandpromo;
$brands_array = explode(',', $brands);
//get current cart content
$cart = $this->cart->contents();
$count_brand_exist = 0;
foreach ($cart as $item) {
//get brand_id from each item
$this->db->select('brand_id')->from('products')->where('id_products', $item['id']);
$product_brand_id = $this->db->get()->row()->brand_id;
if(in_array($product_brand_id, $brands_array)) {
$count_brand_exist = $count_brand_exist + 1;
}
}
if($count_brand_exist == 0) {
$this->form_validation->set_message('_check_voucher_code', 'Sorry You did not choose Products with Promoted Brand');
return FALSE;
}
break;
}
return true;
}
public function choose_payment() {
if(!isset($_POST['payment_type'])) { redirect('summary'); }
//unset first
$this->session->unset_userdata('veritrans_transaction_fee');
$this->session->unset_userdata('veritrans_admin_fee');
$this->session->unset_userdata('virtualaccount_admin_fee');
$payment_type = $this->input->post('payment_type');
if ($payment_type == 'veritrans') {
//get veritrans fee
$this->db->select('veritrans_fee, veritrans_admin')->from('configuration')->where('id_configuration', 1);
$veritrans = $this->db->get()->row();
$veritrans_fee = $veritrans->veritrans_fee;
$veritrans_admin = $veritrans->veritrans_admin;
$this->session->set_userdata('veritrans_transaction_fee', $veritrans_fee);
$this->session->set_userdata('veritrans_admin_fee', $veritrans_admin);
//set initial payment type to session
$this->session->set_userdata('chosen_payment_type', 'veritrans');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE CREDIT CARD PAYMENT');
} elseif($payment_type == 'virtualaccount') {
//get virtualaccount fee
$this->db->select('virtualaccount_admin')->from('configuration')->where('id_configuration', 1);
$virtualaccount_admin = $this->db->get()->row()->virtualaccount_admin;
$this->session->set_userdata('virtualaccount_admin_fee', $virtualaccount_admin);
//set initial payment type to session
$this->session->set_userdata('chosen_payment_type', 'virtualaccount');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE VIRTUAL ACCOUNT PAYMENT');
} elseif($payment_type == 'cod') {
//set initial payment type to session
$this->session->set_userdata('chosen_payment_type', 'cod');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE COD PAYMENT');
} elseif($payment_type == 'bank_transfer') {
//set initial payment type to session
$this->session->set_userdata('chosen_payment_type', 'bank_transfer');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE BANK TRANSFER');
}
$this->index();
}
public function choose_dropship_status() {
if(!isset($_POST['send_as_dropship'])) { redirect('summary'); }
$dropship_status = $this->input->post('send_as_dropship');
if ($dropship_status == 'yes') {
//set dropship status to session
$this->session->set_userdata('choose_dropship_status', 'yes');
$this->session->set_flashdata('summary_message', 'YOU SEND AS DROPSHIPPER');
} elseif($dropship_status == 'no') {
//set dropship status to session
$this->session->set_userdata('choose_dropship_status', 'no');
$this->session->set_flashdata('summary_message', 'YOU SEND AS NON DROPSHIPPER');
}
$this->index();
}
public function choose_carrier($carrier = 'jne_regular') {
//set shipping
switch ($carrier) {
case 'jne_regular':
$this->session->set_userdata('carrier', 'jne');
$this->session->set_userdata('carrier_name', 'JNE REGULAR');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE JNE REGULAR');
break;
case 'jne_yes':
$this->session->set_userdata('carrier', 'jne');
$this->session->set_userdata('carrier_name', 'JNE YES');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE JNE YES');
break;
case 'jne_oke':
$this->session->set_userdata('carrier', 'jne');
$this->session->set_userdata('carrier_name', 'JNE OKE');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE JNE OKE');
break;
case 'tiki_reg':
$this->session->set_userdata('carrier', 'tiki');
$this->session->set_userdata('carrier_name', 'TIKI REGULAR');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE TIKI REGULAR');
break;
case 'tiki_ons':
$this->session->set_userdata('carrier', 'tiki');
$this->session->set_userdata('carrier_name', 'TIKI ONS');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE TIKI ONS');
break;
case 'tiki_eco':
$this->session->set_userdata('carrier', 'tiki');
$this->session->set_userdata('carrier_name', 'TIKI ECO');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE TIKI ECO');
break;
case 'pos_regular':
$this->session->set_userdata('carrier', 'pos');
$this->session->set_userdata('carrier_name', 'POS KILAT KHUSUS');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE POS KILAT KHUSUS');
break;
case 'pos_express':
$this->session->set_userdata('carrier', 'pos');
$this->session->set_userdata('carrier_name', 'POS EXPRESS');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE POS EXPRESS');
break;
case 'rpx_regular':
$this->session->set_userdata('carrier', 'rpx');
$this->session->set_userdata('carrier_name', 'RPX REGULAR');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE RPX REGULAR');
break;
case 'rpx_retail':
$this->session->set_userdata('carrier', 'rpx');
$this->session->set_userdata('carrier_name', 'RPX RETAIL');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE RPX RETAIL');
break;
case 'shop_delivery':
$this->session->set_userdata('carrier', 'shop_delivery');
$this->session->set_userdata('carrier_name', 'SHOP DELIVERY');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE SHOP DELIVERY');
break;
case 'gosend':
$this->session->set_userdata('carrier', 'gosend');
$this->session->set_userdata('carrier_name', 'GO-SEND');
$this->session->set_flashdata('summary_message', 'YOU CHOOSE GO-SEND');
if($this->input->post('destination_latitude') && $this->input->post('destination_longitude')) {
$this->session->set_userdata('destination_latitude', $this->input->post('destination_latitude'));
$this->session->set_userdata('destination_longitude', $this->input->post('destination_longitude'));
}
break;
}
$this->index();
}
public function get_pointrewards() {
if(!isset($_POST['submit_point_rewards'])) {
redirect('summary');
}
//validation in action
$config = array(
array(
'field' => 'point_rewards',
'label' => 'point rewards',
'rules' => 'trim|required|numeric|callback__check_point_rewards'
),
);
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$point = (int) $this->security->xss_clean($this->input->post('point_rewards'));
//get point rewards setting
$this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1);
$point_rewards = $this->db->get()->row();
$point_discount = $point * (int) $point_rewards->conversion;
$this->session->set_userdata('chosen_point', $point);
$this->session->set_userdata('chosen_point_discount', $point_discount);
$this->session->set_flashdata('summary_message', 'POINT REWARDS REDEEMED');
$this->index();
} else {
//form validation not pass..
$this->session->unset_userdata('chosen_point');
$this->session->unset_userdata('chosen_point_discount');
$this->index();
}
}
public function _check_point_rewards($str) {
if ($str == 0) {
$this->form_validation->set_message('_check_point_rewards', 'Point cannot be empty, 0 or non numbers!');
return FALSE;
}
//get customer current point reward
$this->db->select('current_pointreward')->from('customers')->where('id_customers', (int) $this->session->userdata('customer')['customer_id']);
$current_point = $this->db->get()->row()->current_pointreward;
if((int) $str > (int) $current_point) {
$this->form_validation->set_message('_check_point_rewards', "Point cannot bigger than {$current_point}");
return FALSE;
}
return TRUE;
}
}