|
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/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shipping extends Customer_Controller {
private $last_tracking_warehouse;
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->helper('shipping');
$this->load->library('form_validation');
}
public function index() {
//get all cities
$this->db->select('destination_city, id')->from('indah_cargo')->order_by('destination_city', 'ASC');
$view_data['cities'] = $this->db->get()->result();
//unset voucher, shipping fee session
$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->session->unset_userdata('total_shipping_fee');
//$this->session->unset_userdata('free_shipping');
$this->session->unset_userdata('add_point_reward');
$this->session->unset_userdata('chosen_point');
$this->session->unset_userdata('chosen_point_discount');
//check if the current total stocks are enough.
$have_stock = true;
foreach ($this->cart->contents() as $item) {
//get product name and backorder
$this->db->select('title, is_backorder')->from('products')->where('id_products', $item['id']);
$product = $this->db->get()->row();
$product_name = $product->title;
$is_backorder = $product->is_backorder;
//get total stok from warehouse
$this->db->select_sum('stock')->from('stock')->where('id_product', $item['id']);
$total_stock = $this->db->get()->row()->stock;
if($total_stock < $item['qty']) {
if($is_backorder == 'no') {
$this->session->set_flashdata('no_stock' . $item['id'], "Stok tidak cukup");
$have_stock = false;
}
}
}
if($have_stock == false) {
redirect('cart');
}
//TOTAL STOK CUKUP. LANJUT KE HALAMAN SHIPPING...
$this->session->unset_userdata('shipping_cart');
//get all provinces data from provinces table...
/* $this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC');
$view_data['shipping_provinces'] = $this->db->get()->result(); */
//get all cities of Indah Cargo
$this->db->select('destination_city, id, destination_province')->from('indah_cargo')->order_by('destination_city', 'ASC');
$view_data['indahcargo_cities'] = $this->db->get()->result();
if($this->session->userdata('customer')['customer_id'] != NULL) {
//if customer id already exist (means in loggedin state....then proceed as usual..
$id_customer = (int) $this->session->userdata('customer')['customer_id'];
//get customer shipping id
$this->db->select('indah_cargo_id')->from('customers')->where('id_customers', $id_customer);
$shipping_ids = $this->db->get()->row();
if($shipping_ids->indah_cargo_id == NULL) {
$shipping_id_province = 1;
} else {
$shipping_id_province = $shipping_ids->indah_cargo_id;
}
//$shipping_id_subdistrict = $shipping_ids->shipping_id_subdistrict;
//check if warehouse1 is exist for the shipping destination province id
$this->db->select('id')->from('warehouse')->where('id_province', $shipping_id_province);
$count_warehouse1 = $this->db->get()->num_rows();
if($count_warehouse1 > 0) {
//warehouse is exist.
$this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('id_province', $shipping_id_province)->order_by('priority', 'ASC')->limit(1);
$warehouse1 = $this->db->get()->row_array();
} else {
//Warehouse is not exist. Destination address is not covered by warehouse location, so use main warehouse
$this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('warehouse_type', 'main')->order_by('priority', 'ASC')->limit(1);
$warehouse1 = $this->db->get()->row_array();
}
$this->last_tracking_warehouse = $warehouse1;
//count total warehouse
$this->db->select('id')->from('warehouse');
$count_warehouses = $this->db->get()->num_rows();
//get cart contents
foreach ($this->cart->contents() as $item) {
$is_flashsale = NULL;
$is_sale = FALSE;
//cek flashsale
//apakah core session flashsale lagi jalan?
//kalo ya, cek apakah item $item['id'] == item di flashsale ?
//bila ya, is_flashsale = TRUE;
$this->db->select('id')->from('flashsale_products')->where('product_id',$item['id'])->where('flashsale_id',$this->session->userdata('flashsale_id_active'));
$fs_cek_product = $this->db->get()->row();
if($fs_cek_product != null){
$is_flashsale = $this->session->userdata('flashsale_id_active');
}
//cek sale
//cek di tabel produk, apakah discounted_price < 0 ? if yes, is_sale = TRUE;
$discounted_price = $this->db->select('discounted_price')->from('products')->where('id_products',$item['id'])->get()->row()->discounted_price;
if($discounted_price > 0){
$is_sale = TRUE;
}
//get total stok from all warehouses
$this->db->select_sum('stock')->from('stock')->where('id_product', $item['id']);
$total_stock_all_warehouses = $this->db->get()->row()->stock;
for ($i = 1; $i <= $count_warehouses; $i++) {
if($i == 1) {
$remaining_qty = $this->evaluate_warehouse($item['id'], $warehouse1['id'], $warehouse1['name'], $item['qty'], $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale);
if($remaining_qty > 0) {
if($i == $count_warehouses) {
//this is the last warehouse
$this->evaluate_backorder($item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict);
$remaining_qty = 0;
}
}
} elseif($i == 2) {
if($warehouse1['warehouse_type'] == 'main') {
$this->db->select('id, priority, name')->from('warehouse')->where('priority >', $warehouse1['priority'])->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
} else {
$this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('warehouse_type', 'main')->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
}
$this->last_tracking_warehouse = $warehouse;
$remaining_qty = $this->evaluate_warehouse($item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale);
if($remaining_qty > 0) {
if($i == $count_warehouses) {
//this is the last warehouse
$this->evaluate_backorder($item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict);
$remaining_qty = 0;
}
}
} elseif($i == $count_warehouses && $i != 1 && $i != 2 && $i != 3) {
if($warehouse1['warehouse_type'] == 'main') {
$this->db->select('id, priority, name')->from('warehouse')->where('priority >', $this->last_tracking_warehouse['priority'])->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
} else {
$this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('priority >', $this->last_tracking_warehouse['priority'])->where('warehouse_type != ', 'main')->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
}
$this->last_tracking_warehouse = $warehouse;
$remaining_qty = $this->evaluate_warehouse($item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale);
if($remaining_qty > 0) {
if($i == $count_warehouses) {
//this is the last warehouse
$this->evaluate_backorder($item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict);
$remaining_qty = 0;
}
}
} elseif($i == 3) {
if($warehouse1['warehouse_type'] == 'main') {
$this->db->select('id, priority, name')->from('warehouse')->where('priority >', $this->last_tracking_warehouse['priority'])->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
} else {
$this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('priority >', $warehouse1['priority'])->where('warehouse_type !=', 'main')->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
}
$this->last_tracking_warehouse = $warehouse;
$remaining_qty = $this->evaluate_warehouse($item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale);
if($remaining_qty > 0) {
if($i == $count_warehouses) {
//this is the last warehouse
$this->evaluate_backorder($item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict);
$remaining_qty = 0;
}
}
} else {
if($warehouse1['warehouse_type'] == 'main') {
$this->db->select('id, priority, name')->from('warehouse')->where('priority >', $this->last_tracking_warehouse['priority'])->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
} else {
$this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('priority >', $this->last_tracking_warehouse['priority'])->where('warehouse_type != ', 'main')->order_by('priority', 'ASC')->limit(1);
$warehouse = $this->db->get()->row_array();
}
$this->last_tracking_warehouse = $warehouse;
$remaining_qty = $this->evaluate_warehouse($item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale);
if($remaining_qty > 0) {
if($i == $count_warehouses) {
//this is the last warehouse
$this->evaluate_backorder($item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict);
$remaining_qty = 0;
}
}
}
if($remaining_qty == 0) {
//warehouse stock is enough
break;
}
}
}
//get point rewards setting
$this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1);
$view_data['point_rewards'] = $this->db->get()->row();
//get customer current point reward
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $id_customer);
$view_data['current_point'] = $this->db->get()->row()->current_pointreward;
/* echo '<pre>';
print_r($this->session->userdata('shipping_cart'));
echo '</pre>';
exit(); */
//SHIPPING PROVINCE, DISTRICT, SUBDISTRICT
//get customer shipping address detail
//SHIPPING PROVINCE, DISTRICT, SUBDISTRICT
//get customer shipping address detail
$view_data['shipping'] = $this->customer_m->get_shipping($id_customer);
//get all provinces data from provinces table
$this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC');
$view_data['shipping_provinces'] = $this->db->get()->result();
$current_shipping_province_id = $view_data['shipping']->shipping_id_province;
//get all initial shipping districts based on stored province
$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $current_shipping_province_id);
$view_data['shipping_district_lists'] = $this->db->get()->result();
$current_shipping_district_id = $view_data['shipping']->shipping_id_district;
//get all initial shipping subdistricts lists
$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $current_shipping_district_id);
$view_data['shipping_subdistrict_lists'] = $this->db->get()->result();
$final_total_shipping_fee = 0;
/*cek shipping per product untuk harga diatas Rp.0 untuk menentukan kembali choosen shipping id*/
foreach ($this->session->userdata('shipping_cart') as $rowid => $cek_product_shipping) {
$final_choosen_shipping_id = $cek_product_shipping['chosen_shipping_id'];
$final_shipping_fee_product= $cek_product_shipping['shipping_fee'];
$cek_fee = calculate_shipping_fee($cek_product_shipping['chosen_shipping_id'], $cek_product_shipping['warehouse_id'], $cek_product_shipping['id'], $cek_product_shipping['qty'], $shipping_id_subdistrict);
$product_shipping_fee = $cek_fee['total_shipping_fee'];
$final_total_shipping_fee = $final_total_shipping_fee + $product_shipping_fee;
/*jika hasil calculate fee per product Rp.0*/
if($product_shipping_fee == 0){
$count_available_method = count($cek_product_shipping['shipping_method_ids']);
for($new_sid=0;$new_sid<$count_available_method;$new_sid++){
$cek_fee1 = calculate_shipping_fee($cek_product_shipping['shipping_method_ids'][$new_sid], $cek_product_shipping['warehouse_id'], $cek_product_shipping['id'], $cek_product_shipping['qty'], $shipping_id_subdistrict);
$fee1 = $cek_fee1['total_shipping_fee'];
if($fee1 != 0){
$final_choosen_shipping_id = $cek_product_shipping['shipping_method_ids'][$new_sid];
$final_shipping_fee_product= $fee1;
break;
}
}
}
//add new info to shipping cart session
$new_shipping_cart = $this->session->userdata('shipping_cart');
$new_shipping_cart[$rowid]['chosen_shipping_id'] = $final_choosen_shipping_id;
$new_shipping_cart[$rowid]['shipping_fee'] = $final_shipping_fee_product;
$this->session->set_userdata('shipping_cart', $new_shipping_cart);
}
//calculate initial total shipping fee
$view_data['final_total_shipping_fee'] = $final_total_shipping_fee;
$this->session->set_userdata('total_shipping_fee', $final_total_shipping_fee);
/*set free shipping fee*/
$view_data['free_shipping_fee'] = 0;
/*get grand total for calculate min transaction*/
$get_grand_total = 0;
foreach ($this->session->userdata('shipping_cart') as $item){
$get_grand_total = $get_grand_total + $item['subtotal'];
}
/*get grand total for calculate min transaction*/
$free_shipping_price = $this->db->select('free_shipping_type_subsidi')->from('configuration')->where('id_configuration',1)->get()->row()->free_shipping_type_subsidi;
$free_shipping_type = $this->db->select('free_shipping_type')->from('configuration')->where('id_configuration',1)->get()->row()->free_shipping_type;
if($free_shipping_type == 'region'){
//special case for DKI Jakarta, BOGOR, DEPOK, TANGERANG, BEKASI
$exception_indah_cargo_ids = [1, 2, 3, 4, 5];
if(in_array($shipping_id_province, $exception_indah_cargo_ids)) {
//current customer indahcargo shipping id is Free shipping
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
$this->session->set_userdata('free_shipping', $final_total_shipping_fee);
} else {
$view_data['free_shipping_fee'] = 0;
$this->session->set_userdata('free_shipping', 0);
}
/* $selected_region_province = $this->db->select('province_id')->from('free_shipping_region')->where('configuration_id',1)->get()->result();
foreach ($selected_region_province as $region_province) {
if($region_province->province_id == $shipping_id_province){
if($free_shipping_price == 0){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
else{
$view_data['free_shipping_fee'] = $free_shipping_price;
}
break;
}
} */
}
elseif($free_shipping_type == 'global'){
$view_data['min_transaction'] = $this->db->select('min_transaction')->from('free_shipping_global')->where('configuration_id',1)->get()->row()->min_transaction;
if($free_shipping_price == 0){
if ($get_grand_total >= $view_data['min_transaction']){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
}
else{
if ($get_grand_total >= $view_data['min_transaction']){
$view_data['free_shipping_fee'] = $free_shipping_price;
}
}
}
elseif($free_shipping_type == 'off'){
$view_data['free_shipping_fee'] = 0;
}
$this->session->set_userdata('free_shipping', $view_data['free_shipping_fee']);
} else {
//if customer id not yet exist...this is a GUEST CHECKOUT
$view_data['guest_checkout'] = TRUE;
}
//var_dump($this->session->userdata('shipping_cart'));
//get SEO
$this->data_header['browser_title'] = ucwords($this->data_header['website_name']) . ' - Shipping';
$this->data_header['meta_description'] = ucwords($this->data_header['website_name']) . ' - Shipping';
$this->load->view("themes/$this->theme_no/header", $this->data_header);
$this->load->view('shipping', $view_data);
$this->load->view("themes/$this->theme_no/footer", $this->data_footer);
}
public function save_shipping() {
if(!isset($_POST['indah_cargo_id'])) { redirect('shipping'); }
if(isset($_POST['guest_checkout'])) {
//from guest checkout shipping
//check if email already exist, and check customer type..
$this->db->select('id_customers')->from('customers')->where('email', $this->input->post('email'));
$count_customer = $this->db->get()->num_rows();
if($count_customer > 0) {
//customer already exist
//check customer type. if customer type is regular, then update shipping information only..
$customer_data = array(
'shipping_name' => $this->security->xss_clean($this->input->post('name')),
'shipping_phone' => $this->security->xss_clean($this->input->post('phone')),
'shipping_id_province' => $this->input->post('shipping_province'),
'shipping_id_district' => $this->input->post('shipping_district'),
'shipping_id_subdistrict' => $this->input->post('shipping_subdistrict'),
'address' => $this->security->xss_clean($this->input->post('address')),
'shipping_address' => $this->security->xss_clean($this->input->post('address')),
'postcode' => $this->security->xss_clean($this->input->post('postcode')),
'shipping_postcode' => $this->security->xss_clean($this->input->post('postcode'))
);
$this->db->where('email', $this->input->post('email'));
$this->db->update('customers', $customer_data);
//get customer id
$this->db->select('id_customers, name')->from('customers')->where('email', $this->input->post('email'));
$customer_data = $this->db->get()->row();
$customer_name = $customer_data->name;
$customer_id = $customer_data->id_customers;
$customer_data = array(
'customer_name' => ucwords($customer_name),
'customer_email' => $this->security->xss_clean($this->input->post('email')),
'customer_id' => $customer_id,
'customer_loggedin' => TRUE,
'customer_type' => 'guest'
);
$this->session->set_userdata(array('customer' => $customer_data));
} else {
//customer not yet exist
//insert new customer
$customer_data = array(
'name' => $this->security->xss_clean($this->input->post('name')),
'recipient_name' => $this->security->xss_clean($this->input->post('name')),
'shipping_name' => $this->security->xss_clean($this->input->post('name')),
'email' => $this->security->xss_clean($this->input->post('email')),
'phone' => $this->security->xss_clean($this->input->post('phone')),
'shipping_phone' => $this->security->xss_clean($this->input->post('phone')),
'shipping_id_province' => $this->input->post('shipping_province'),
'shipping_id_district' => $this->input->post('shipping_district'),
'shipping_id_subdistrict' => $this->input->post('shipping_subdistrict'),
'address' => $this->security->xss_clean($this->input->post('address')),
'shipping_address' => $this->security->xss_clean($this->input->post('address')),
'postcode' => $this->security->xss_clean($this->input->post('postcode')),
'shipping_postcode' => $this->security->xss_clean($this->input->post('postcode')),
'type' => 'guest'
);
$this->db->insert('customers', $customer_data);
$customer_id = $this->db->insert_id();
$customer_data = array(
'customer_name' => 'Guest',
'customer_email' => $this->security->xss_clean($this->input->post('email')),
'customer_id' => $customer_id,
'customer_loggedin' => TRUE,
'customer_type' => 'guest'
);
$this->session->set_userdata(array('customer' => $customer_data));
}
} else {
//from regular customer shipping
//save shipping id
$data = array(
'indah_cargo_id' => $this->input->post('indah_cargo_id')
);
$this->db->where('id_customers', $this->session->userdata('customer')['customer_id']);
$this->db->update('customers', $data);
}
redirect('shipping');
}
private function count_warehouse_stock($product_id, $warehouse_id) {
//check if stock data is exist for warehouse
$this->db->select('stock')->from('stock')->where('id_product', $product_id)->where('warehouse_id', $warehouse_id);
$stock_warehouse = $this->db->get()->row();
if(count($stock_warehouse) == 0) {
//not stock data available
$stock = 0;
} else {
$stock = $stock_warehouse->stock;
}
return $stock;
}
private function create_new_cart_item($id, $name, $qty, $price, $subtotal, $warehouse_name, $warehouse_id, $shipping_method_ids, $shipping_fee, $is_backorder, $is_flashsale = NULL, $is_sale = FALSE) {
$shipping_cart = $this->session->userdata('shipping_cart');
$shipping_cart[uniqid()] = array(
'id' => $id,
'name' => $name,
'qty' => $qty,
'price' => $price,
'subtotal' => $subtotal,
'warehouse_name' => $warehouse_name,
'warehouse_id' => $warehouse_id,
'shipping_method_ids' => $shipping_method_ids,
'chosen_shipping_id' => $shipping_method_ids[0],
'shipping_fee' => $shipping_fee,
'is_backorder' => $is_backorder,
'is_flashsale' => $is_flashsale,
'is_sale' => $is_sale
);
$this->session->set_userdata('shipping_cart', $shipping_cart);
}
private function get_shipping_method_ids($product_id, $warehouse_id, $shipping_id_subdistrict) {
//get the shipping method options
$this->db->select('shipment_method_id')->from('shipment_method_product')->where('product_id', $product_id);
$shipping_method_ids = $this->db->get()->result();
$eligible_shipping_method_ids = array();
foreach ($shipping_method_ids as $shipping_method_id) {
//check if this shipping method id is eligible (specific for 2hour delivery and 1 day service )
if($shipping_method_id->shipment_method_id == 1) {
//shipment method of 1 day service
//check if this warehouse allow 1 day service
$this->db->select('oneday_subdistrict_id')->from('shipment_method_express')->where('warehouse_id', $warehouse_id)->where('oneday_subdistrict_id', $shipping_id_subdistrict);
$count_exist = $this->db->get()->num_rows();
if($count_exist > 0) {
//1 day service is included
$eligible_shipping_method_ids[] = 1;
}
} elseif($shipping_method_id->shipment_method_id == 2) {
//2 hours delivery
//check if this warehouse allow 2 hour delivery
$this->db->select('twohour_subdistrict_id')->from('shipment_method_express')->where('warehouse_id', $warehouse_id)->where('twohour_subdistrict_id', $shipping_id_subdistrict);
$count_exist = $this->db->get()->num_rows();
if($count_exist > 0) {
//2 hour delivery is included
$eligible_shipping_method_ids[] = 2;
}
} elseif ($shipping_method_id->shipment_method_id == 5) {
//indent
} else {
//regular and next day
$eligible_shipping_method_ids[] = (int) $shipping_method_id->shipment_method_id;
}
}
return $eligible_shipping_method_ids;
}
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 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 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;
}
public function remove_shipping_cart($rowid = NULL) {
$id_customer = (int) $this->session->userdata('customer')['customer_id'];
//get customer shipping id
$this->db->select('shipping_id_province, shipping_id_subdistrict')->from('customers')->where('id_customers', $id_customer);
$shipping_ids = $this->db->get()->row();
$shipping_id_province = $shipping_ids->shipping_id_province;
$shipping_id_subdistrict = $shipping_ids->shipping_id_subdistrict;
//destination data is complete. Continue with stock check per warehouse, and update cart items.
$view_data['shipping_data_complete'] = true;
//remove session of shipping_cart
$shipping_cart = $this->session->userdata('shipping_cart');
unset($shipping_cart[$rowid]);
$this->session->set_userdata('shipping_cart', $shipping_cart);
$view_data['shipping'] = $this->customer_m->get_shipping($id_customer);
//get all provinces data from provinces table
$this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC');
$view_data['shipping_provinces'] = $this->db->get()->result();
$current_shipping_province_id = $view_data['shipping']->shipping_id_province;
//get all initial shipping districts based on stored province
$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $current_shipping_province_id);
$view_data['shipping_district_lists'] = $this->db->get()->result();
$current_shipping_district_id = $view_data['shipping']->shipping_id_district;
//get all initial shipping subdistricts lists
$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $current_shipping_district_id);
$view_data['shipping_subdistrict_lists'] = $this->db->get()->result();
//get point rewards setting
$this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1);
$view_data['point_rewards'] = $this->db->get()->row();
//get customer current point reward
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $id_customer);
$view_data['current_point'] = $this->db->get()->row()->current_pointreward;
//calculate initial total shipping fee
//if 2 product has the same shipping method, then use 1 shipping fee only..
$shipping_fee_array = array();
foreach ($this->session->userdata('shipping_cart') as $item) {
$shipping_fee_array[$item['chosen_shipping_id']][$item['warehouse_id']][] = $item;
}
$final_total_shipping_fee = 0; //harga akhir
foreach ($shipping_fee_array as $warehouse_sid) {
$total_fee_shipping = 0;
foreach ($warehouse_sid as $item1) {
$total_fee_warehouse = 0;
$total_weight_wids = 0;
$count_wsid = count($item1);
for($a=0;$a<$count_wsid;$a++){
$this->db
->select('dimension_weight, dimension_length, dimension_width, dimension_height')
->from('products')
->where('id_products', $item1[$a]['id']);
$product_dimension = $this->db->get()->row();
$product_weight = $product_dimension->dimension_weight; //gram
$product_length = $product_dimension->dimension_length; //cm
$product_width = $product_dimension->dimension_width; //cm
$product_height = $product_dimension->dimension_height; //cm
//check if volume is bigger than weight
$volume_weight = $product_length * $product_width * $product_height / 6000; //kg
if(($volume_weight * 1000) >= $product_weight) {
$weight = $volume_weight * 1000;
} else {
$weight = $product_weight;
}
$total_weight_gram = ceil($weight * $item1[$a]['qty']); //gram
$total_weight_wids = $total_weight_wids + $total_weight_gram;
}
//get shipping method name
$this->db
->select('name,shipper,carrier, service_code1, service_code2')
->from('shipment_method')
->where('id', $item1[0]['chosen_shipping_id']);
$shipping_method = $this->db->get()->row();
$shipping_name = $shipping_method->name;
$shipping_carrier = $shipping_method->carrier;
$shipping_shipper = $shipping_method->shipper;
$service_code1 = $shipping_method->service_code1;
$service_code2 = $shipping_method->service_code2;
//get warehose sub district id
$this->db->select('id_subdistrict')->from('warehouse')->where('id', $item1[0]['warehouse_id']);
$warehouse_subdistrict_id = $this->db->get()->row()->id_subdistrict;
$rajaongkir_cost = get_rajaongkir_ongkos($warehouse_subdistrict_id, $shipping_id_subdistrict, $total_weight_wids, $shipping_carrier);
//check if weight is zero. If zero, then rajaongkir cannot proceed..
if($total_weight_wids > 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 = 0; //service is not available
}
}
}
else {
$total_shipping_fee = 0; //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 = 0; //service is not available
}
}
}
else {
$total_shipping_fee = 0; //service is not available
}
}
}
else {
//total weight gram is zero
$total_shipping_fee = 0; //service is not available
}
//total fee dari banyak produk dalam 1 warehouse dan 1 shipping fee
$total_fee_warehouse = $total_fee_warehouse + $total_shipping_fee;
//total fee dari banyak warehouse dalam 1 shipping fee
$total_fee_shipping = $total_fee_shipping + $total_fee_warehouse;
}
$final_total_shipping_fee = $final_total_shipping_fee + $total_fee_shipping;//penjumlahan ke harga akhir
}
$view_data['final_total_shipping_fee'] = $final_total_shipping_fee;
$this->session->set_userdata('total_shipping_fee', $final_total_shipping_fee);
/*set free shipping fee*/
$view_data['free_shipping_fee'] = 0;
/*get grand total for calculate min transaction*/
$get_grand_total = 0;
foreach ($this->session->userdata('shipping_cart') as $item){
$get_grand_total = $get_grand_total + $item['subtotal'];
}
/*get grand total for calculate min transaction*/
$free_shipping_price = $this->db->select('free_shipping_type_subsidi')->from('configuration')->where('id_configuration',1)->get()->row()->free_shipping_type_subsidi;
$free_shipping_type = $this->db->select('free_shipping_type')->from('configuration')->where('id_configuration',1)->get()->row()->free_shipping_type;
if($free_shipping_type == 'region'){
$selected_region_province = $this->db->select('province_id')->from('free_shipping_region')->where('configuration_id',1)->get()->result();
foreach ($selected_region_province as $region_province) {
if($region_province->province_id == $shipping_id_province){
if($free_shipping_price == 0){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
else{
$view_data['free_shipping_fee'] = $free_shipping_price;
}
break;
}
}
}
elseif($free_shipping_type == 'global'){
$view_data['min_transaction'] = $this->db->select('min_transaction')->from('free_shipping_global')->where('configuration_id',1)->get()->row()->min_transaction;
if($free_shipping_price == 0){
if ($get_grand_total >= $view_data['min_transaction']){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
}
else{
if ($get_grand_total >= $view_data['min_transaction']){
$view_data['free_shipping_fee'] = $free_shipping_price;
}
}
}
elseif($free_shipping_type == 'off'){
$view_data['free_shipping_fee'] = 0;
}
$this->session->set_userdata('free_shipping', $view_data['free_shipping_fee']);
//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) . ' - Shipping';
$this->data_header['meta_description'] = ucwords($website_name->website_name) . ' - Shipping';
$this->data_header['meta_keywords'] = $website_name->meta_keywords;
$this->load->view('template/header', $this->data_header);
$this->load->view('shipping', $view_data);
$this->load->view('template/footer', $this->data_footer);
}
public function no_stock() {
$id_customer = (int) $this->session->userdata('customer')['customer_id'];
//get customer shipping id
$this->db->select('shipping_id_province, shipping_id_subdistrict')->from('customers')->where('id_customers', $id_customer);
$shipping_ids = $this->db->get()->row();
$shipping_id_province = $shipping_ids->shipping_id_province;
$shipping_id_subdistrict = $shipping_ids->shipping_id_subdistrict;
//destination data is complete. Continue with stock check per warehouse, and update cart items.
$view_data['shipping_data_complete'] = true;
$view_data['shipping'] = $this->customer_m->get_shipping($id_customer);
//get all provinces data from provinces table
$this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC');
$view_data['shipping_provinces'] = $this->db->get()->result();
$current_shipping_province_id = $view_data['shipping']->shipping_id_province;
//get all initial shipping districts based on stored province
$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $current_shipping_province_id);
$view_data['shipping_district_lists'] = $this->db->get()->result();
$current_shipping_district_id = $view_data['shipping']->shipping_id_district;
//get all initial shipping subdistricts lists
$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $current_shipping_district_id);
$view_data['shipping_subdistrict_lists'] = $this->db->get()->result();
//get point rewards setting
$this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1);
$view_data['point_rewards'] = $this->db->get()->row();
//get customer current point reward
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $id_customer);
$view_data['current_point'] = $this->db->get()->row()->current_pointreward;
//calculate initial total shipping fee
//if 2 product has the same shipping method, then use 1 shipping fee only..
$shipping_fee_array = array();
foreach ($this->session->userdata('shipping_cart') as $item) {
$shipping_fee_array[$item['chosen_shipping_id']][$item['warehouse_id']][] = $item;
}
$final_total_shipping_fee = 0; //harga akhir
foreach ($shipping_fee_array as $warehouse_sid) {
$total_fee_shipping = 0;
foreach ($warehouse_sid as $item1) {
$total_fee_warehouse = 0;
$total_weight_wids = 0;
$count_wsid = count($item1);
for($a=0;$a<$count_wsid;$a++){
$this->db
->select('dimension_weight, dimension_length, dimension_width, dimension_height')
->from('products')
->where('id_products', $item1[$a]['id']);
$product_dimension = $this->db->get()->row();
$product_weight = $product_dimension->dimension_weight; //gram
$product_length = $product_dimension->dimension_length; //cm
$product_width = $product_dimension->dimension_width; //cm
$product_height = $product_dimension->dimension_height; //cm
//check if volume is bigger than weight
$volume_weight = $product_length * $product_width * $product_height / 6000; //kg
if(($volume_weight * 1000) >= $product_weight) {
$weight = $volume_weight * 1000;
} else {
$weight = $product_weight;
}
$total_weight_gram = ceil($weight * $item1[$a]['qty']); //gram
$total_weight_wids = $total_weight_wids + $total_weight_gram;
}
//get shipping method name
$this->db
->select('name,shipper,carrier, service_code1, service_code2')
->from('shipment_method')
->where('id', $item1[0]['chosen_shipping_id']);
$shipping_method = $this->db->get()->row();
$shipping_name = $shipping_method->name;
$shipping_carrier = $shipping_method->carrier;
$shipping_shipper = $shipping_method->shipper;
$service_code1 = $shipping_method->service_code1;
$service_code2 = $shipping_method->service_code2;
//get warehose sub district id
$this->db->select('id_subdistrict')->from('warehouse')->where('id', $item1[0]['warehouse_id']);
$warehouse_subdistrict_id = $this->db->get()->row()->id_subdistrict;
$rajaongkir_cost = get_rajaongkir_ongkos($warehouse_subdistrict_id, $shipping_id_subdistrict, $total_weight_wids, $shipping_carrier);
//check if weight is zero. If zero, then rajaongkir cannot proceed..
if($total_weight_wids > 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 = 0; //service is not available
}
}
}
else {
$total_shipping_fee = 0; //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 = 0; //service is not available
}
}
}
else {
$total_shipping_fee = 0; //service is not available
}
}
}
else {
//total weight gram is zero
$total_shipping_fee = 0; //service is not available
}
//total fee dari banyak produk dalam 1 warehouse dan 1 shipping fee
$total_fee_warehouse = $total_fee_warehouse + $total_shipping_fee;
//total fee dari banyak warehouse dalam 1 shipping fee
$total_fee_shipping = $total_fee_shipping + $total_fee_warehouse;
}
$final_total_shipping_fee = $final_total_shipping_fee + $total_fee_shipping;//penjumlahan ke harga akhir
}
$view_data['final_total_shipping_fee'] = $final_total_shipping_fee;
$this->session->set_userdata('total_shipping_fee', $final_total_shipping_fee);
/*set free shipping fee*/
$view_data['free_shipping_fee'] = 0;
/*get grand total for calculate min transaction*/
$get_grand_total = 0;
foreach ($this->session->userdata('shipping_cart') as $item){
$get_grand_total = $get_grand_total + $item['subtotal'];
}
/*get grand total for calculate min transaction*/
$free_shipping_price = $this->db->select('free_shipping_type_subsidi')->from('configuration')->where('id_configuration',1)->get()->row()->free_shipping_type_subsidi;
$free_shipping_type = $this->db->select('free_shipping_type')->from('configuration')->where('id_configuration',1)->get()->row()->free_shipping_type;
/* if($free_shipping_type == 'region'){
$selected_region_province = $this->db->select('province_id')->from('free_shipping_region')->where('configuration_id',1)->get()->result();
foreach ($selected_region_province as $region_province) {
if($region_province->province_id == $shipping_id_province){
if($free_shipping_price == 0){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
else{
$view_data['free_shipping_fee'] = $free_shipping_price;
}
break;
}
}
} */
if($free_shipping_type == 'region'){
$this->db->select('indah_cargo_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);
$indah_cargo_id = $this->db->get()->row();
if($indah_cargo_id == 1) {
if($free_shipping_price == 0){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
else{
$view_data['free_shipping_fee'] = $free_shipping_price;
}
}
/* $selected_region_province = $this->db->select('province_id')->from('free_shipping_region')->where('configuration_id',1)->get()->result();
foreach ($selected_region_province as $region_province) {
if($region_province->province_id == $shipping_id_province){
if($free_shipping_price == 0){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
else{
$view_data['free_shipping_fee'] = $free_shipping_price;
}
break;
}
} */
}
elseif($free_shipping_type == 'global'){
$view_data['min_transaction'] = $this->db->select('min_transaction')->from('free_shipping_global')->where('configuration_id',1)->get()->row()->min_transaction;
if($free_shipping_price == 0){
if ($get_grand_total >= $view_data['min_transaction']){
$view_data['free_shipping_fee'] = $final_total_shipping_fee;
}
}
else{
if ($get_grand_total >= $view_data['min_transaction']){
$view_data['free_shipping_fee'] = $free_shipping_price;
}
}
}
elseif($free_shipping_type == 'off'){
$view_data['free_shipping_fee'] = 0;
}
$this->session->set_userdata('free_shipping', $view_data['free_shipping_fee']);
//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) . ' - Shipping';
$this->data_header['meta_description'] = ucwords($website_name->website_name) . ' - Shipping';
$this->data_header['meta_keywords'] = $website_name->meta_keywords;
$this->load->view('template/header', $this->data_header);
$this->load->view('shipping', $view_data);
$this->load->view('template/footer', $this->data_footer);
}
private function evaluate_warehouse($item_id, $warehouse_id, $warehouse_name, $qty, $shipping_id_subdistrict, $item_price, $item_name, $is_flashsale, $is_sale) {
$warehouse_stock = $this->count_warehouse_stock($item_id, $warehouse_id);
if($warehouse_stock < $qty) {
//stock is not enough. need to get additional stok from next priority warehouse
$remaining_qty = $qty - $warehouse_stock;
//get shippimg methods id
$eligible_shipping_method_id = $this->get_shipping_method_ids($item_id, $warehouse_id, $shipping_id_subdistrict);
//get shipping fee
$selected_shipping_id = $eligible_shipping_method_id[0];
$shipping_info = calculate_shipping_fee($selected_shipping_id, $warehouse_id, $item_id, $qty, $shipping_id_subdistrict);
$total_shipping_fee = $shipping_info['total_shipping_fee'];
$subtotal = $warehouse_stock * $item_price;
if($warehouse_stock != 0) {
$this->create_new_cart_item($item_id, $item_name, $warehouse_stock, $item_price, $subtotal, $warehouse_name, $warehouse_id, $eligible_shipping_method_id, $total_shipping_fee, 'no', $is_flashsale, $is_sale);
}
} else {
//stock for warehouse is enough..
//get shippimg methods id
$eligible_shipping_method_id = $this->get_shipping_method_ids($item_id, $warehouse1_id, $shipping_id_subdistrict);
//get shipping fee
$selected_shipping_id = $eligible_shipping_method_id[0];
$shipping_info = calculate_shipping_fee($selected_shipping_id, $warehouse_id, $item_id, $qty, $shipping_id_subdistrict);
$total_shipping_fee = $shipping_info['total_shipping_fee'];
$subtotal = $qty * $item_price;
$this->create_new_cart_item($item_id, $item_name, $qty, $item_price, $subtotal, $warehouse_name, $warehouse_id, $eligible_shipping_method_id, $total_shipping_fee, 'no', $is_flashsale, $is_sale);
$remaining_qty = 0;
}
return $remaining_qty;
}
private function evaluate_backorder($item_id, $item_name, $item_qty, $item_price, $total_stock_all_warehouses, $shipping_id_subdistrict) {
//last warehouse bu still have remaining quantity
//this is for the last warehouse, check for backorder status
$this->db->select('is_backorder')->from('products')->where('id_products', $item_id);
$is_backorder = $this->db->get()->row()->is_backorder;
if($is_backorder == 'yes') {
//product with backorder
$remaining_qty_indent = $item_qty - $total_stock_all_warehouses;
//fill in remaining quantity for indent
$selected_shipping_id_indent = array(5); //id 5 is for indent
//choose warehouse with main status
$this->db->select('id, name')->from('warehouse')->where('warehouse_type', 'main')->limit(1)->order_by('id', 'ASC');
$main_warehouse = $this->db->get()->row();
$shipping_info_indent = calculate_shipping_fee(5, $main_warehouse->id, $item_id, $remaining_qty_indent, $shipping_id_subdistrict);
$total_shipping_fee_indent = $shipping_info_indent['total_shipping_fee'];
$subtotal_indent = $remaining_qty_indent * $item_price;
$this->create_new_cart_item($item_id, $item_name, $remaining_qty_indent, $item_price, $subtotal_indent, $main_warehouse->name, $main_warehouse->id, $selected_shipping_id_indent, $total_shipping_fee_indent, 'yes');
}
}
public function generate_penawaran_pdf() {
//add PDF attachment DOMPDF
$data['title'] = 'Surat Penawaran Harga';
//get customer detail
$this->db->select('*')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);
$pdf_data['customer'] = $this->db->get()->row();
//get current month ij roman
$pdf_data['current_month'] = integerToRoman(date('n'));
//get current year 2 digits
$pdf_data['current_year'] = date('y');
$subtotal = 0;
foreach($this->session->userdata('shipping_cart') as $cart) {
$subtotal = $subtotal + ($cart['qty'] * $cart['price']);
}
//insert into penawaran harga table and get the id
$data = array(
'customer_id' => $pdf_data['customer']->id_customers,
'employee_id' => $this->session->userdata('chosen_sales_id'),
'cart_array' => serialize($this->session->userdata('shipping_cart')),
'sales_id' => $this->session->userdata('chosen_sales_id'),
'total_shipping_fee' => $this->session->userdata('total_shipping_fee'),
'free_shipping' => $this->session->userdata('free_shipping'),
'sub_total' => $subtotal
);
if($this->session->userdata('redeemed_voucher_amount')) {
$data['redeemed_voucher_amount'] = $this->session->userdata('redeemed_voucher_amount');
}
if($this->session->userdata('chosen_point_discount')) {
$data['minus_reward_amount'] = $this->session->userdata('chosen_point_discount');
}
$this->db->insert('penawaran_harga', $data);
$pdf_data['penawaran_id'] = $this->db->insert_id();
//insert into penawaran harga table and get the id
$data_update = array(
'no_penawaran_harga' => 'IBU/' . $pdf_data['current_month'] . '/'. $pdf_data['current_year'] . '/ECM/' . $pdf_data['penawaran_id'],
);
$this->db->where('id', $pdf_data['penawaran_id']);
$this->db->update('penawaran_harga', $data_update);
//create session for penawaran_id
$this->session->set_userdata('penawaran_harga_id', $pdf_data['penawaran_id']);
//get sales person data
$this->db->select('*')->from('sales_list')->where('employee_id', $this->session->userdata('chosen_sales_id'));
$pdf_data['sales_data'] = $this->db->get()->row();
$this->load->library('dompdf_gen');
$html = $this->load->view('penawaran_pdf', $pdf_data ,true);
/*tambahan*/
$html = preg_replace('/>\s+</', '><', $html);
/*tambahan*/
$this->dompdf->load_html($html);
$this->dompdf->set_paper('A4', 'portrait');
$this->dompdf->render();
$this->dompdf->stream('penawaran_harga.pdf', array("Attachment" => 0));
$output = $this->dompdf->output();
$file_to_save = 'uploads/pdf/penawaran_harga.pdf';
file_put_contents($file_to_save, $output);
//get website data
$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
$website_data = $this->db->get()->row();
$data_pdf['logo'] = $website_data->logo;
$data_pdf['website_name'] = $website_data->website_name;
//----SEND EMAIL TO CUSTOMER
$this->load->library('email');
//get email setting
$config['protocol'] = 'smtp';
$config['smtp_host'] = $website_data->email_smtp_host;
$config['smtp_port'] = $website_data->email_smtp_port;
$config['smtp_user'] = $website_data->email_smtp;
$config['smtp_pass'] = $website_data->email_smtp_password;
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
$this->email->set_crlf( "\r\n" ); //must add this for hotmail
$this->email->initialize($config);
$this->email->from('ecommerce@indolok.id', $website_data->website_name);
$this->email->to($pdf_data['customer']->email);
$this->email->cc($pdf_data['sales_data']->email);
$this->email->bcc($pdf_data['sales_data']->email_manager);
$this->email->attach(base_url() . $file_to_save);
$this->email->subject('Penawaran Harga Indolok ' . 'Ref : IBU/' . $pdf_data['current_month'] . '/'. $pdf_data['current_year'] . '/ECM/' . $pdf_data['penawaran_id']);
$email = $this->load->view('email/indonesian/penawaran_harga', $data_pdf, TRUE);
$this->email->message($email);
$response = $this->email->send();
if (!$response) { var_dump($this->email->print_debugger()); exit(); }
}
}