https://t.me/RX1948
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/rabbithabit.com/public_html/application/controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //var/www/rabbithabit.com/public_html/application/controllers/Shipping.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shipping extends Public_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() {

		// echo '<pre>';
		// print_r($this->cart->contents());
		// echo '</pre>';
		// exit(); 

		if(count($this->cart->contents()) == 0) {
			redirect('cart');
		}

		//check if the current total stocks are enough.
		$have_stock = true; 

		foreach ($this->cart->contents() as $item) {

			$product_id = get_product_id($item['id']);

			//get product name and backorder
			$product = $this->db->select('title')->from('products')->where('id_products', $product_id)->get()->row();
			$product_name = $product->title;

			//get is_indent status
			$is_indent = $this->db->select('is_indent')->from('product_details')->where('id', $item['id'])->get()->row()->is_indent;
			
			//get total stok from warehouse
			$total_stock = $this->db->select_sum('stock')->from('stock')->where('id_product', $product_id)->where('id_product_detail', $item['id'])->get()->row()->stock;
			
			if($total_stock < $item['qty']) {

				if($is_indent == 'no') {
					$this->session->set_flashdata('no_stock' . $item['id'], 'Not Enough Stock');
					$have_stock = false;
				}	
			} 
		}

		if($have_stock == false) {
			redirect('cart');	
		}

		$view_data = $this->create_shipping_carts(); 

		//get SEO
		if($this->session->userdata('site_lang') == 'english') {
			$this->lang->load('shipping', 'english');
		} else {
			$this->lang->load('shipping', 'indonesian'); 
		}

		$website_name = $this->db->select('website_name')->from('configuration')->where('id_configuration', 1)->get()->row();
		$this->data_header['browser_title'] =  ucwords($website_name->website_name) . ' - Shipping'; 
		$this->data_header['meta_description'] = ucwords($website_name->website_name) . ' - Shipping';

		// echo "<pre>";
		// var_dump($view_data);
		// exit();
		
		$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); 

		$this->session->set_userdata('shipping_to_payment', 'yes');
	}

	public function ajax_reload_shipping_result() {

		//test if ajax call to prevent direct access
		if (!$this->input->is_ajax_request()) {
   			exit('No direct script access allowed'); 
		} 
		$condition_freeshipping = false; 

		$free_shipping_type = 
		$this->db->select('free_shipping_type')
		->from('configuration')
		->where('id_configuration',1)->get()->row()
		->free_shipping_type;

		$get_grand_total = 0;
		foreach ($this->session->userdata('shipping_cart') as $item){
			$get_grand_total += $item['subtotal'];
		}

		$q_config_cond = $this->db->select('type_cond_prov_free_shipping, cond_more_prov_free_shipping, cond_less_prov_free_shipping')->from('configuration')->where('id_configuration',1)->get()->row();
 

		if($free_shipping_type == 'region') {
			$selected_region_province = 
			$this->db->select('*')
			->from('free_shipping_region')
			->where('configuration_id',1)
			->where('province_id',$this->input->post('id_shipping_province'))
			->get();

			if($selected_region_province->num_rows() > 0){   
				switch ($q_config_cond->type_cond_prov_free_shipping) {
					case 'more_than':
						if ($get_grand_total >= $q_config_cond->cond_more_prov_free_shipping) {
							$condition_freeshipping = true;
						}else{
							$condition_freeshipping = false;
						}
						break;
					case 'less_than':
						if ($get_grand_total <= $q_config_cond->cond_less_prov_free_shipping) {
							$condition_freeshipping = true;
						}else{
							$condition_freeshipping = false;
						}
						break;
					
					default: 
						$condition_freeshipping = true;
						break;
				}
			} 

			// echo $this->input->post('id_shipping_province'); exit();
		}

		$shipping_array = array(
			'shipping_id_province' => 
			$this->input->post('id_shipping_province'),

			'condition_freeshipping' => 
			$condition_freeshipping,

			'shipping_id_district' => 
			$this->input->post('id_shipping_district'),

			'shipping_id_subdistrict' => 
			$this->input->post('id_shipping_subdistrict'),
		);

		$this->session->set_userdata('guest_shipping_id', $shipping_array);

		$this->session->set_userdata('condition_freeshipping', $condition_freeshipping);

		if($this->session->userdata('site_lang') == 'english') {
			$this->lang->load('shipping', 'english');
		} else {
			$this->lang->load('shipping', 'indonesian'); 
		}
		
		$view_data = $this->create_shipping_carts();

		$this->load->view('ajax/ajax_reload_shipping_result', $view_data);
	}

	private function create_shipping_carts() {

		//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');
		$this->session->unset_userdata('shipping_cart');

		//get all provinces data from provinces table...
		$view_data['shipping_provinces'] = $this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC')->get()->result();

		if($this->session->userdata('guest_shipping_id')) {

			$shipping_id_province = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
			$shipping_id_subdistrict = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];

		} else {

			if($this->session->userdata('customer')['customer_id'] != NULL) { 

				//if customer id already exist, means in loggedin state....
				$id_customer = (int) $this->session->userdata('customer')['customer_id'];

				//get customer shipping id
				$shipping_ids = $this->db->select('shipping_id_province, shipping_id_subdistrict')->from('customers')->where('id_customers', $id_customer)->get()->row();
				$shipping_id_province = $shipping_ids->shipping_id_province;
				$shipping_id_subdistrict = $shipping_ids->shipping_id_subdistrict;

			} else {

				$view_data['guest_checkout'] = true;

				//set default province and subdistrict from main warehouse
				$default_destination = $this->db->select('id_province, id_subdistrict')->from('warehouse')->where('warehouse_type', 'main')->get()->row();
				$shipping_id_province = $default_destination->id_province;
				$shipping_id_subdistrict = $default_destination->id_subdistrict;
			}
		}

		//check if warehouse1 is exist for the shipping destination province id
		$count_warehouse1 = $this->db->select('id')->from('warehouse')->where('id_province', $shipping_id_province)->get()->num_rows();

		if($count_warehouse1 > 0) {
			//warehouse is exist.
			$warehouse1 = $this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('id_province', $shipping_id_province)->order_by('priority', 'ASC')->limit(1)->get()->row_array();
		} else {
			//Warehouse is not exist. Destination address is not covered by warehouse location, so use main warehouse
			$warehouse1 = $this->db->select('id, priority, name, warehouse_type')->from('warehouse')->where('warehouse_type', 'main')->order_by('priority', 'ASC')->limit(1)->get()->row_array();
		}

		$this->last_tracking_warehouse = $warehouse1;

		//count total warehouse
		$count_warehouses = $this->db->select('id')->from('warehouse')->get()->num_rows();

		//get cart contents	
		foreach ($this->cart->contents() as $item) {

			//get product id
			$product_id = get_product_id($item['id']);

			$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;
			$fs_cek_product = $this->db->select('id')->from('flashsale_products')->where('product_id', $product_id)->where('flashsale_id',$this->session->userdata('flashsale_id_active'))->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('product_details')->where('id', $item['id'])->where('product_id', $product_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_detail', $item['id'])->where('id_product', $product_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($product_id, $item['id'], $warehouse1['id'], $warehouse1['name'], $item['qty'], $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale, $item['options']['attribute_detail_ids'], $item['options']['sku']); 

					if($remaining_qty > 0) {
						if($i == $count_warehouses) {
							//this is the last warehouse
							$this->evaluate_backorder($product_id, $item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict, $item['options']['attribute_detail_ids'], $item['options']['sku'], $item['options']['downpayment_price'], $item['options']['dp_percentage'], $item['options']['indent_message']);
							$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($product_id, $item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale, $item['options']['attribute_detail_ids'], $item['options']['sku']);

					if($remaining_qty > 0) {
						if($i == $count_warehouses) {
							//this is the last warehouse
							$this->evaluate_backorder($product_id, $item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict, $item['options']['attribute_detail_ids'], $item['options']['sku'], $item['options']['downpayment_price'], $item['options']['dp_percentage'], $item['options']['indent_message']);
							$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($product_id, $item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale, $item['options']['attribute_detail_ids'], $item['options']['sku']);

					if($remaining_qty > 0) {
						if($i == $count_warehouses) {
							//this is the last warehouse
							$this->evaluate_backorder($product_id, $item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict, $item['options']['attribute_detail_ids'], $item['options']['sku'], $item['options']['downpayment_price'], $item['options']['dp_percentage'], $item['options']['indent_message']);
							$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();

						if(count($warehouse) == 0) {
							//if no warehouse is found
							$this->db->select('id, priority, name')->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($product_id, $item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale, $item['options']['attribute_detail_ids'], $item['options']['sku']); 

					if($remaining_qty > 0) {
						if($i == $count_warehouses) {
							//this is the last warehouse
							$this->evaluate_backorder($product_id, $item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict, $item['options']['attribute_detail_ids'], $item['options']['sku'], $item['options']['downpayment_price'], $item['options']['dp_percentage'], $item['options']['indent_message']);
							$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($product_id, $item['id'], $warehouse['id'], $warehouse['name'], $remaining_qty, $shipping_id_subdistrict, $item['price'], $item['name'], $is_flashsale, $is_sale, $item['options']['attribute_detail_ids'], $item['options']['sku']); 

					if($remaining_qty > 0) {
						if($i == $count_warehouses) {
							//this is the last warehouse
							$this->evaluate_backorder($product_id, $item['id'], $item['name'], $item['qty'], $item['price'], $total_stock_all_warehouses, $shipping_id_subdistrict, $item['options']['attribute_detail_ids'], $item['options']['sku'], $item['options']['downpayment_price'], $item['options']['dp_percentage'], $item['options']['indent_message']);
							$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', $this->session->userdata('customer')['customer_id']);
		$view_data['current_point'] = $this->db->get()->row()->current_pointreward;

		/* echo '<pre>';
		print_r($this->session->userdata('shipping_cart'));
		echo '</pre>'; */

		//SHIPPING PROVINCE, DISTRICT, SUBDISTRICT
		//get customer shipping address detail
		//SHIPPING PROVINCE, DISTRICT, SUBDISTRICT
		//get customer shipping address detail

		//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();

		if($this->session->userdata('customer')['customer_id'] != NULL) { 

			$view_data['shipping'] = $this->customer_m->get_shipping($this->session->userdata('customer')['customer_id']); 

			if($this->session->userdata('guest_shipping_id')) {

				$view_data['shipping']->shipping_id_province = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$view_data['shipping']->shipping_id_district = $this->session->userdata('guest_shipping_id')['shipping_id_district'];
				$view_data['shipping']->shipping_id_subdistrict = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];
				$current_shipping_province_id = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$current_shipping_district_id = $this->session->userdata('guest_shipping_id')['shipping_id_district'];

			} else {

				$view_data['shipping'] = $this->customer_m->get_shipping($this->session->userdata('customer')['customer_id']);  
				$current_shipping_province_id = $view_data['shipping']->shipping_id_province;
			} 

		} else {

			if($this->session->userdata('guest_shipping_id')) {

				$view_data['shipping'] = new stdClass;

				$view_data['shipping']->shipping_id_province = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$view_data['shipping']->shipping_id_district = $this->session->userdata('guest_shipping_id')['shipping_id_district'];
				$view_data['shipping']->shipping_id_subdistrict = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];
				$current_shipping_province_id = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$current_shipping_district_id = $this->session->userdata('guest_shipping_id')['shipping_id_district'];

			} else {

				$this->db->select('id_province, id_district, id_subdistrict')->from('warehouse')->where('warehouse_type', 'main');
				$default_destination = $this->db->get()->row();
				$shipping_id_province = $default_destination->id_province;
				$shipping_id_district = $default_destination->id_district;
				$shipping_id_subdistrict = $default_destination->id_subdistrict;

				$view_data['shipping'] = new stdClass;
				$view_data['shipping']->shipping_id_province = $shipping_id_province;
				$view_data['shipping']->shipping_id_district = $shipping_id_district;
				$view_data['shipping']->shipping_id_subdistrict = $shipping_id_subdistrict;
				$current_shipping_province_id = $shipping_id_province;
				$current_shipping_district_id = $shipping_id_district;

				$shipping_data = array(
					'shipping_id_province' => $shipping_id_province,
					'shipping_id_district' => $shipping_id_district,
					'shipping_id_subdistrict' => $shipping_id_subdistrict
				);
				$this->session->set_userdata('guest_shipping_id', $shipping_data);
			}

		}

		//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();

		/*cek shipping per product untuk harga diatas Rp.0 untuk menentukan kembali choosen shipping id*/
		// echo "<pre>";
		// var_dump($this->session->userdata('shipping_cart')); exit();
		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['product_id'], $cek_product_shipping['id'], $cek_product_shipping['qty'], $shipping_id_subdistrict); 
			$product_shipping_fee = $cek_fee['total_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['product_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;
			$new_shipping_cart[$rowid]['id_product'] = $cek_product_shipping['product_id'];

			$this->session->set_userdata('shipping_cart', $new_shipping_cart);
		}
		/*cek shipping per product untuk harga diatas Rp.0 untuk menentukan kembali choosen shipping id*/

		//calculate initial total shipping fee
		$view_data['final_total_shipping_fee'] = $this->calculate_total_shipping_fee($shipping_id_subdistrict); //function in MY_Controller

		$view_data['free_shipping_fee'] = $this->calculate_free_shipping_fee($shipping_id_province, $view_data['final_total_shipping_fee']); //function in MY_Controller

		//GET THE VALUE OF DOWNPAYMENT (only for indent item) and other values
		$view_data['total_indent_amount'] = 0;
		$view_data['total_downpayment'] = 0;
		$view_data['no_of_indent'] = 0;
		$view_data['no_of_instock'] = 0;
		$view_data['total_indent_shipping_fee'] = 0;

		foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {

			if($item['is_backorder'] == 'yes') {

				$view_data['total_indent_amount'] += $item['subtotal'];
				$view_data['total_downpayment'] += ($item['dp_price'] * $item['qty']);
				$view_data['no_of_indent']++;
				$view_data['total_indent_shipping_fee'] += $item['shipping_fee'];
			
			} else {
				$view_data['no_of_instock']++;
			}	
			$view_data['id_product'] = $item['id_product'];
		}

		//CHECK IF THESE SHIPPING CARTS CONTAIN AT LEAST 1 INDENT ORDER
		$view_data['contain_indent_product'] = 'no'; 
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {
			if($shipping_cart_item['is_backorder'] == 'yes') {
				$view_data['contain_indent_product'] = 'yes';
				break;	
			}	
		}

		return $view_data;
	}

	/* private function get_remaining_qty($i, $count_warehouses, $total_stock_all_warehouses, $product_id, $item_id, $warehouse_id, $warehouse_name, $qty, $shipping_id_subdistrict, $price, $name, $is_flashsale, $is_sale, $attribute_detail_ids) {

		$remaining_qty = $this->evaluate_warehouse($product_id, $item_id, $warehouse_id, $warehouse_name, $qty, $shipping_id_subdistrict, $price, $name, $is_flashsale, $is_sale, $attribute_detail_ids); 

		if($remaining_qty > 0) {
			if($i == $count_warehouses) {
				//this is the last warehouse
				$this->evaluate_backorder($product_id, $item_id, $name, $qty, $price, $total_stock_all_warehouses, $shipping_id_subdistrict, $attribute_detail_ids);
				$remaining_qty = 0;
			}	
		}

		return $remaining_qty;
	} */

	private function count_warehouse_stock($product_id, $item_id, $warehouse_id) {

		//check if stock data is exist for warehouse 
		$this->db->select('stock')->from('stock')->where('id_product', $product_id)->where('id_product_detail', $item_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($product_id, $item_id, $name, $qty, $price, $subtotal, $warehouse_name, $warehouse_id, $shipping_method_ids, $shipping_fee, $is_backorder, $is_flashsale = NULL, $is_sale = FALSE, $attribute_detail_ids = NULL, $sku = NULL, $dp_price = NULL, $dp_percentage = NULL, $indent_message = NULL) {

		$shipping_cart = $this->session->userdata('shipping_cart');

		$shipping_cart[uniqid()] = array(
			'id'					=> $item_id,
			'product_id'			=> $product_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,
			'attribute_detail_ids'  => $attribute_detail_ids,
			'sku'					=> $sku,
			'dp_price'				=> $dp_price,
			'dp_percentage'			=> $dp_percentage,
			'indent_message'		=> $indent_message
		);

		$this->session->set_userdata('shipping_cart', $shipping_cart);

		/* echo '<pre>';
		print_r($this->session->userdata('shipping_cart'));
		echo '</pre>';	
		exit(); */
	}

	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)->order_by('shipment_method_id', 'ASC');
		$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) {

						//get product_id
						$product_id = get_product_id($item['id']);
						
						//check if this item has category which is match with $categories_array
						$this->db->select('id_category')->from('category_product')->where('id_product', $product_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) {

						//get product_id
						$product_id = get_product_id($item['id']);

						//check if this item has brand which is match with $brands_array
						$this->db->select('brand_id')->from('products')->where('id_products', $product_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 product_id
					$product_id = get_product_id($item['id']);

					//get category_id from each item
					$this->db->select('id_category')->from('category_product')->where('id_product', $product_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 product_id
					$product_id = get_product_id($item['id']);
					
					//get brand_id from each item
					$this->db->select('brand_id')->from('products')->where('id_products', $product_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) {

		if($this->session->userdata('customer')['customer_id'] != NULL) { 

			$view_data['shipping'] = $this->customer_m->get_shipping($this->session->userdata('customer')['customer_id']); 

			if($this->session->userdata('guest_shipping_id')) {

				$view_data['shipping']->shipping_id_province = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$view_data['shipping']->shipping_id_district = $this->session->userdata('guest_shipping_id')['shipping_id_district'];
				$view_data['shipping']->shipping_id_subdistrict = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];
				$current_shipping_province_id = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$current_shipping_district_id = $this->session->userdata('guest_shipping_id')['shipping_id_district'];
				$current_shipping_subdistrict_id = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];

			} else {

				$view_data['shipping'] = $this->customer_m->get_shipping($this->session->userdata('customer')['customer_id']);  
				$current_shipping_province_id = $view_data['shipping']->shipping_id_province;
			} 

		} else {

			if($this->session->userdata('guest_shipping_id')) {

				$view_data['shipping'] = new stdClass;

				$view_data['shipping']->shipping_id_province = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$view_data['shipping']->shipping_id_district = $this->session->userdata('guest_shipping_id')['shipping_id_district'];
				$view_data['shipping']->shipping_id_subdistrict = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];
				$current_shipping_province_id = $this->session->userdata('guest_shipping_id')['shipping_id_province'];
				$current_shipping_district_id = $this->session->userdata('guest_shipping_id')['shipping_id_district'];
				$current_shipping_subdistrict_id = $this->session->userdata('guest_shipping_id')['shipping_id_subdistrict'];

			} else {

				$this->db->select('id_province, id_district, id_subdistrict')->from('warehouse')->where('warehouse_type', 'main');
				$default_destination = $this->db->get()->row();
				$shipping_id_province = $default_destination->id_province;
				$shipping_id_district = $default_destination->id_district;
				$shipping_id_subdistrict = $default_destination->id_subdistrict;

				$view_data['shipping'] = new stdClass;
				$view_data['shipping']->shipping_id_province = $shipping_id_province;
				$view_data['shipping']->shipping_id_district = $shipping_id_district;
				$view_data['shipping']->shipping_id_subdistrict = $shipping_id_subdistrict;
				$current_shipping_province_id = $shipping_id_province;
				$current_shipping_district_id = $shipping_id_district;
				$current_shipping_subdistrict_id = $shipping_id_subdistrict;

				$shipping_data = array(
					'shipping_id_province' => $shipping_id_province,
					'shipping_id_district' => $shipping_id_district,
					'shipping_id_subdistrict' => $shipping_id_subdistrict
				);
				$this->session->set_userdata('guest_shipping_id', $shipping_data);
			}
		}

		//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);

		//get all provinces data from provinces table
		$view_data['shipping_provinces'] = $this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC')->get()->result();

		//get all initial shipping districts based on stored province
		$view_data['shipping_district_lists'] = $this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $current_shipping_province_id)->get()->result();  

		//get all initial shipping subdistricts lists
		$view_data['shipping_subdistrict_lists'] = $this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $current_shipping_district_id)->get()->result();  

		//get point rewards setting
		$view_data['point_rewards'] = $this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1)->get()->row();

		if($this->session->userdata('customer')['customer_id']) {
			//get customer current point reward
			$view_data['current_point'] = $this->db->select('current_pointreward')->from('customers')->where('id_customers', $id_customer)->get()->row()->current_pointreward;
		} else {
			$view_data['current_point'] = 0;
		}

		//calculate initial total shipping fee
		$final_total_shipping_fee = $this->calculate_total_shipping_fee($view_data['shipping']->shipping_id_subdistrict); //function in MY_Controller

		$view_data['final_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;

		$get_grand_total = 0;
		foreach ($this->session->userdata('shipping_cart') as $item){
			$get_grand_total += $item['subtotal'];
		}

		$q_config_cond = $this->db->select('type_cond_prov_free_shipping, cond_more_prov_free_shipping, cond_less_prov_free_shipping')->from('configuration')->where('id_configuration',1)->get()->row();

		if($free_shipping_type == 'region') {

			$selected_region_province = $this->db->select('province_id')->from('free_shipping_region')->where('configuration_id',1)->get()->result();

			$cond_freeshipping = true;
			switch ($q_config_cond->type_cond_prov_free_shipping) {
				case 'more_than':
					if ($get_grand_total >= $q_config_cond->cond_more_prov_free_shipping) {
						$cond_freeshipping = false;
					} 
					break;
				case 'less_than':
					if ($get_grand_total <= $q_config_cond->cond_less_prov_free_shipping) {
						$cond_freeshipping = false;
					} 
					break;
				
				default: 
					$cond_freeshipping = false;
					break;
			}

			foreach ($selected_region_province as $region_province) {

				if($region_province->province_id == $shipping_id_province) {

					if ($cond_freeshipping == false) {
						$view_data['free_shipping_fee'] = 0;
					}else{
						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 THE VALUE OF DOWNPAYMENT (only for indent item) and other values
		$view_data['total_indent_amount'] = 0;
		$view_data['total_downpayment'] = 0;
		$view_data['no_of_indent'] = 0;
		$view_data['no_of_instock'] = 0;
		$view_data['total_indent_shipping_fee'] = 0;

		foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {

			if($item['is_backorder'] == 'yes') {

				$view_data['total_indent_amount'] += $item['subtotal'];
				$view_data['total_downpayment'] += ($item['dp_price'] * $item['qty']);
				$view_data['no_of_indent']++;
				$view_data['total_indent_shipping_fee'] += $item['shipping_fee'];
			
			} else {
				$view_data['no_of_instock']++;
			}	
		}

		//CHECK IF THESE SHIPPING CARTS CONTAIN AT LEAST 1 INDENT ORDER
		$view_data['contain_indent_product'] = 'no'; 
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {
			if($shipping_cart_item['is_backorder'] == 'yes') {
				$view_data['contain_indent_product'] = 'yes';
				break;	
			}	
		}
		
		//destroy cart if shipping cart is empty
		if(count($this->session->userdata('shipping_cart')) == 0) {
			$this->cart->destroy();
			redirect('cart');
		}

		//get SEO
		$this->db->select('website_name')->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->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 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
		$final_total_shipping_fee = $this->calculate_total_shipping_fee($shipping_id_subdistrict); //function in MY_Controller

		$view_data['final_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*/
		
		$q_config_cond = $this->db->select('type_cond_prov_free_shipping, cond_more_prov_free_shipping, cond_less_prov_free_shipping')->from('configuration')->where('id_configuration',1)->get()->row(); 

		$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();

			$cond_freeshipping = true;
			switch ($q_config_cond->type_cond_prov_free_shipping) {
				case 'more_than':
					if ($get_grand_total >= $q_config_cond->cond_more_prov_free_shipping) {
						$cond_freeshipping = false;
					} 
					break;
				case 'less_than':
					if ($get_grand_total <= $q_config_cond->cond_less_prov_free_shipping) {
						$cond_freeshipping = false;
					} 
					break;
				
				default: 
					$cond_freeshipping = false;
					break;
			} 
			foreach ($selected_region_province as $region_province) {
				if($region_province->province_id == $shipping_id_province) {

					if ($cond_freeshipping == false) {
						$view_data['free_shipping_fee'] = 0;
					}else{
						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')->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->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); 
	}

	private function evaluate_warehouse($product_id, $item_id, $warehouse_id, $warehouse_name, $qty, $shipping_id_subdistrict, $item_price, $item_name, $is_flashsale, $is_sale, $attribute_detail_ids, $sku) {

		$warehouse_stock = $this->count_warehouse_stock($product_id, $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($product_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, $product_id, $item_id, $warehouse_stock, $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($product_id, $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, $attribute_detail_ids, $sku);
			}

		} else {

			//stock for warehouse is enough..
			//get shippimg methods id
			$eligible_shipping_method_id = $this->get_shipping_method_ids($product_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, $product_id, $item_id, $qty, $shipping_id_subdistrict); 

			$total_shipping_fee = $shipping_info['total_shipping_fee'];
			
			$subtotal = $qty * $item_price;

			$this->create_new_cart_item($product_id, $item_id, $item_name, $qty, $item_price, $subtotal, $warehouse_name, $warehouse_id, $eligible_shipping_method_id, $total_shipping_fee, 'no', $is_flashsale, $is_sale, $attribute_detail_ids, $sku);

			$remaining_qty = 0;

		}	

		return $remaining_qty;
	}

	private function evaluate_backorder($product_id, $item_id, $item_name, $item_qty, $item_price, $total_stock_all_warehouses, $shipping_id_subdistrict, $attribute_detail_ids, $sku, $dp_price, $dp_percentage, $indent_message) {

		//last warehouse but still have remaining quantity
		//this is for the last warehouse, check for backorder status
		$this->db->select('is_indent')->from('product_details')->where('id', $item_id)->where('product_id', $product_id);
		$is_indent = $this->db->get()->row()->is_indent;

		if($is_indent == '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, $product_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($product_id, $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', $is_flashsale, $is_sale, $attribute_detail_ids, $sku, $dp_price, $dp_percentage, $indent_message);
		}		
	}	
}

https://t.me/RX1948 - 2025