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/Ajax.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Ajax extends Public_Controller {

	public function __construct() { 
		parent::__construct(); 
	}

	//ajax product page select size
	public function ajax_select_size() { 

		//test if ajax call to prevent direct access
		if (!$this->input->is_ajax_request()) {
   			exit('No direct script access allowed');
		}
		$quantity = (int) $this->input->post('quantity');
		$id_product = (int) $this->input->post('id_product');
        //get product base price
        $this->db->select('price')->from('products')->where('id_products', $id_product);
        $base_price = $this->db->get()->row()->price;
		//check if the id_product has quantity discount
		$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);
		$count_quantity_discount = $this->db->get()->num_rows();
		if ($count_quantity_discount > 0) {
			//get discount for chosen quantity, choosing the closest quantity
			$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");
			$row = $query->row();
			if (count($row) > 0) {
				$data['quantity_discounted_price'] = $base_price - ($base_price * $row->discount_percentage / 100);
				$data['quantity_discount_percentage'] = $row->discount_percentage;
			} else {
				//quantity is less than minimum discount rule
				//check if have base normal discount
				$this->db->select('discount_price')->from('products')->where('id_products', $id_product);
    			$discount_price = $this->db->get()->row()->discount_price;
				if ($discount_price != 0) {
					$data['discounted_price'] = $base_price - ($base_price * $discount_price / 100);  
					$data['discount_percentage'] = $discount_price;
				}
			}
		} else {
			//no quantity discount
			//check if have base normal discount
			$this->db->select('discount_price')->from('products')->where('id_products', $id_product);
			$discount_price = $this->db->get()->row()->discount_price;
			if ($discount_price != 0) {
				$data['discounted_price'] = $base_price - ($base_price * $discount_price / 100); 
				$data['discount_percentage'] = $discount_price; 
			}
		}
		$data['price'] = $base_price;	
		$data['id_product'] = $id_product;
		$this->load->view('ajax/ajax_select_size', $data);  
	} 

	public function login_another_platform(){
		$this->load->model('customer_m');
		$customer_type = $this->customer_m->login();
		$to_page = '';
		if($this->customer_m->loggedin() == TRUE) {
			if($this->session->userdata('from_shipping_page') == 'yes') {	
				$to_page = base_url('shipping');
			} else {
				$to_page = base_url();	
			}
		}  
		echo json_encode(array(
			'to_page'=>$to_page
		));
	}
	public function google_authenticated(){
		// echo 'tes';
		$userData = json_decode($this->input->post('userData')); 
		if(!empty($userData)){
		    // The user's profile info
		    $oauth_provider = $this->input->post('oauth_provider');
		    $oauth_uid  = !empty($userData->id)?$userData->id:'';
		    $first_name = !empty($userData->given_name)?$userData->given_name:'';
		    $last_name  = !empty($userData->family_name)?$userData->family_name:'';
		    $email      = !empty($userData->email)?$userData->email:'';
		    $gender     = !empty($userData->gender)?$userData->gender:'';
		    $locale     = !empty($userData->locale)?$userData->locale:'';
		    $picture    = !empty($userData->picture)?$userData->picture:'';
		    $link       = !empty($userData->link)?$userData->link:'';

		    echo $oauth_provider."<br>";
			echo $oauth_uid."<br>";
			echo $first_name."<br>";
			echo $last_name."<br>";
			echo $email."<br>";
			echo $gender."<br>";
			echo $locale."<br>";
			echo $picture."<br>";
			echo $link."<br>";
		    
		     
		    
		    // return true;
		}
	}

	public function check_email(){
		$t_case = $this->security->xss_clean($this->input->post('t_case'));
		$register_email = $this->security->xss_clean($this->input->post('register_email'));

		$data_where = array(
			'email'=>$register_email,
			'oauth_provider'=>$t_case,
		); 

		$count_email = $this->db->get_where('customers',$data_where)->num_rows(); 

		echo json_encode(array(
			'count'=>$count_email,
		));
	}

	//ajax product page add product review
	public function ajax_addproductreview() { 

		//test if ajax call to prevent direct access
		if (!$this->input->is_ajax_request()) {
   			exit('No direct script access allowed');
		}
		//CPATCHA VALIDATION
		// First, delete old captchas
		$expiration = time() - 7200; // Two hour limit
		$this->db->where('captcha_time < ', $expiration)
				->delete('captcha');
		// Then see if a captcha exists and match
		$sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
		$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();
		if ($row->count == 0) {
			echo '<p style="background-color:red; color:white; padding:5px;">Mohon masukan kode yang benar.</p>';
			exit();
		}		
		$product_id = (int) $this->input->post('product_id');
		$rating = $this->input->post('rating');
		$review = $this->security->xss_clean($this->input->post('review'));
		if($this->input->post('customer_id')) {
			//if customer act as a registered during product review 
			$customer_id = (int) $this->input->post('customer_id');
			//get customer name and email
			$this->db->select('name, email')->from('customers')->where('id_customers', $customer_id);
			$customer_data = $this->db->get()->row();
			$data = array(
				'product_id' => $product_id,
				'review_date' => date('j M Y'),
				'is_registered' => 'yes',
				'customer_id' => $customer_id,
				'name'	=> $customer_data->name,
				'email'	=> $customer_data->email,
				'rating' => $rating,
				'review' => $review
			);
		} else {
			//customer act as a guest during product review
			//get value from serialize form data ajax
			$name = $this->security->xss_clean($this->input->post('name'));
			$email = $this->security->xss_clean($this->input->post('email'));
			$data = array(
				'product_id' => $product_id,
				'review_date' => date('j M Y'),
				'is_registered' => 'no',
				'name'	=> $name,
				'email'	=> $email,
				'rating' => $rating,
				'review' => $review
			);
		}
		$this->db->insert('product_review', $data);
		//get all product reviews
		$this->db->select('*')->from('product_review')->where('product_id', $product_id)->order_by('review_date', 'DESC');
		$data['product_reviews'] = $this->db->get()->result();
		$data['product_id'] = $product_id;
		$this->load->view('ajax/ajax_addproductreview', $data);
	}



	//ajax get price

	public function ajax_get_price() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$id_product_detail = (int) $this->input->post('id_product_details');

		$id_product = (int) $this->input->post('id_product');



		//check if $id_product_detail is exist

		$this->db->select('id_product_details')->from('product_details')->where('id_product_details', $id_product_detail);

		$count_id_product_detail = $this->db->get()->num_rows();



		$this->load->helper('category_discount');

		$category_discount_percentage = category_discount($id_product);



		if(isset($this->session->userdata('customer')['customer_id'])) {



			//customer is logged in

			//check if customer is a reseller. if reseller use reseller min quantity

			$this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);

			$reseller_id = $this->db->get()->row()->reseller_id;



			//check if reseller min quantity already available (already input by admin). If not, display 1 as minimum quantity

			$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

			$count_reseller_price = $this->db->get()->num_rows();



			if($reseller_id != NULL && $count_reseller_price > 0) {

				

				//customer is reseller, and data already inputted by admin. so use reseller price

				$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['price'] = $this->db->get()->row()->price;

				$data['discounted_price'] = 0;



			} elseif($reseller_id != NULL && $id_product_detail == 0) {



				//customer is a reseller. id_product_detail is 0 because he choose no option with 0 id product details

				//get product detail id (for 1st detail only)

				$this->db->select('id_product_details')->from('product_details')->where('product_id', $id_product)->limit(1);

				$id_product_detail = $this->db->get()->row()->id_product_details;



				$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['price'] = $this->db->get()->row()->price;

				$data['discounted_price'] = 0;



			} elseif($reseller_id == NULL || $count_reseller_price == 0) {

				

				//customer is not a reseller or data not inputted by admin, so use normal price with 0 id product details

				if($category_discount_percentage != NULL) {

				//category discount is active

					if($count_id_product_detail > 0) {

						

						$this->db->select('price, sku, attributes')->from('product_details')->where('id_product_details', $id_product_detail);

						$prices = $this->db->get()->row(); 

						$data['price'] = $prices->price;

						$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);



					} else {

						//id_product_details is not available, because customer choose option with 0 id product details

						$this->db->select('price, sku, attributes')->from('product_details')->where('product_id', $id_product)->order_by('id_product_details', 'ASC')->limit(1);

						$prices = $this->db->get()->row(); 

						$data['price'] = $prices->price;

						$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

					}



				} else {

					//category discount not active

					if($count_id_product_detail > 0) {



						//get the initial product price from product_details table

						$this->db->select('price, discounted_price')->from('product_details')->where('id_product_details', $id_product_detail)->order_by('id_product_details', 'ASC')->limit(1);

						$prices = $this->db->get()->row(); 

						$data['price'] = $prices->price;

						$data['discounted_price'] = $prices->discounted_price;



					} else {



						//id_product_details is not available, because customer choose option with 0 id product details

						$this->db->select('price, discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

						$prices = $this->db->get()->row(); 

						$data['price'] = $prices->price;

						$data['discounted_price'] = $prices->discounted_price; 

					}

				}	

			}



		} else {

			//if customer is not logged in

			if($category_discount_percentage != NULL) {

				//category discount is active

				if($count_id_product_detail > 0) {

					

					$this->db->select('price, sku, attributes')->from('product_details')->where('id_product_details', $id_product_detail);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);



				} else {

					//id_product_details is not available, because customer choose option with 0 id product details

					$this->db->select('price, sku, attributes')->from('product_details')->where('product_id', $id_product)->order_by('id_product_details', 'ASC')->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

				}



			} else {

				//category discount not active

				if($count_id_product_detail > 0) {



					//get the initial product price from product_details table

					$this->db->select('price, discounted_price')->from('product_details')->where('id_product_details', $id_product_detail)->order_by('id_product_details', 'ASC')->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->discounted_price;



				} else {



					//id_product_details is not available, because customer choose option with 0 id product details

					$this->db->select('price, discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->discounted_price; 

				}

			}	

		}

		

		$this->load->view('ajax/ajax_get_price', $data);

	}	



	//ajax get sku. stock, weight

	public function ajax_get_productdetails() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$id_product_detail = (int) $this->input->post('id_product_details');

		$id_product = (int) $this->input->post('id_product');



		//check if $id_product_detail is exist

		$this->db->select('id_product_details')->from('product_details')->where('id_product_details', $id_product_detail);

		$count_id_product_detail = $this->db->get()->num_rows();



		if($count_id_product_detail > 0) {

			

			$this->db->select('sku, weight, stock')->from('product_details')->where('id_product_details', $id_product_detail);

			$product_details = $this->db->get()->row();

			$data['sku'] = $product_details->sku;

			$data['weight'] = $product_details->weight;

			$data['stock'] = $product_details->stock;



		} else {



			//id_product_details is not available, because customer choose option with 0 id product details

			$this->db->select('sku, weight, stock')->from('product_details')->where('product_id', $id_product)->order_by('id_product_details', 'ASC')->limit(1);	

			$product_details = $this->db->get()->row();

			$data['sku'] = $product_details->sku;

			$data['weight'] = $product_details->weight;

			$data['stock'] = $product_details->stock;

		}



		//get product code (SKU), weight, and stock display status

		$this->db->select('show_product_sku, show_product_weight, show_product_stock')->from('configuration')->where('id_configuration', 1);

		$display_status = $this->db->get()->row();

		$data['display_sku'] = $display_status->show_product_sku;

		$data['display_weight'] = $display_status->show_product_weight;

		$data['display_stock'] = $display_status->show_product_stock;



		$this->load->view('ajax/ajax_get_productdetails', $data);

	}	



	//ajax get quantity discount

	public function ajax_get_quantity_discount() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$id_product_detail = (int) $this->input->post('id_product_details');

		$id_product = (int) $this->input->post('id_product');



		//GET THE PRICE

		//get initial min quantity

		if(isset($this->session->userdata('customer')['customer_id'])) {



			//customer is logged in

			//check if customer is a reseller. if reseller use reseller min quantity

			$this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);

			$reseller_id = $this->db->get()->row()->reseller_id;



			//check if reseller min quantity already available (already input by admin). If not, display 1 as minimum quantity

			$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

			$count_reseller_price = $this->db->get()->num_rows();



			if($reseller_id != NULL && $count_reseller_price > 0) {

				

				//customer is reseller, and data already inputted by admin. so use reseller price

				$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['price'] = $this->db->get()->row()->price;

				$data['discounted_price'] = 0;



			} elseif($reseller_id != NULL && $id_product_detail == 0) {



				//customer is a reseller. id_product_detail is 0 because he choose no option with 0 id product details

				//get product detail id (for 1st detail only)

				$this->db->select('id_product_details')->from('product_details')->where('product_id', $id_product)->limit(1);

				$id_product_detail = $this->db->get()->row()->id_product_details;



				$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['price'] = $this->db->get()->row()->price;

				$data['discounted_price'] = 0;



			} elseif($reseller_id == NULL || $count_reseller_price == 0) {

				

				//customer is not a reseller or data already inputted by admin, so use normal price with 0 id product details

				//check if $id_product_detail is exist

				$this->db->select('id_product_details')->from('product_details')->where('id_product_details', $id_product_detail);

				$count_id_products = $this->db->get()->num_rows();



				if($count_id_products > 0) {

					//get the initial product price from product_details table

					$this->db->select('price, discounted_price')->from('product_details')->where('id_product_details', $id_product_detail)->order_by('id_product_details', 'ASC')->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->discounted_price;

				} else {

					//id_product_details is not available, because customer choose option with 0 id product details

					$this->db->select('price, discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->discounted_price; 

				}

			}



		} else {

			//if customer is not logged in

			//check if $id_product_detail is exist

			$this->db->select('id_product_details')->from('product_details')->where('id_product_details', $id_product_detail);

			$count_id_products = $this->db->get()->num_rows();



			if($count_id_products > 0) {

				//get the initial product price from product_details table

				$this->db->select('price, discounted_price')->from('product_details')->where('id_product_details', $id_product_detail)->order_by('id_product_details', 'ASC')->limit(1);

				$prices = $this->db->get()->row(); 

				$data['price'] = $prices->price;

				$data['discounted_price'] = $prices->discounted_price;

			} else {

				//id_product_details is not available, because customer choose option with 0 id product details

				$this->db->select('price, discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

				$prices = $this->db->get()->row(); 

				$data['price'] = $prices->price;

				$data['discounted_price'] = $prices->discounted_price; 

			}

		}



		//GET THE QUANTITY 

		//check whether quantity_discount_active is no, retail only, reseller only, or both

		$this->db->select('quantity_discount_active')->from('products')->where('id_products', $id_product);

		$quantity_discount_active = $this->db->get()->row()->quantity_discount_active;

		

		//check quantity discount if exist

		$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);

		$count_quantity_discount = $this->db->get()->num_rows();



		if(isset($this->session->userdata('customer')['customer_id'])) {

			//customer is loggedin

			//check if customer is a reseller

			$this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);

			$reseller_id = $this->db->get()->row()->reseller_id;



			if($reseller_id != NULL) {

				//this is a reseller

				//display quantity discount

				if($quantity_discount_active == 'reseller' || $quantity_discount_active == 'retail-reseller') {

					if($count_quantity_discount > 0) {

						//quantity discount exist. get quantity discount

						$this->db->select('*')->from('quantity_discount')->where('product_id', $id_product)->order_by('min_quantity', 'ASC');

						$data['quantity_discount'] = $this->db->get()->result();

					}

				}

			} else {

				//this is a regular customer

				//display quantity discount

				if($quantity_discount_active == 'retail' || $quantity_discount_active == 'retail-reseller') {

					if($count_quantity_discount > 0) {

						//quantity discount exist. get quantity discount

						$this->db->select('*')->from('quantity_discount')->where('product_id', $id_product)->order_by('min_quantity', 'ASC');

						$data['quantity_discount'] = $this->db->get()->result();

					}

				}

			}

		} else {

			//customer is not loggedin

			//display quantity discount

			if($quantity_discount_active == 'retail' || $quantity_discount_active == 'retail-reseller') {

				if($count_quantity_discount > 0) {

					//quantity discount exist. get quantity discount

					$this->db->select('*')->from('quantity_discount')->where('product_id', $id_product)->order_by('min_quantity', 'ASC');

					$data['quantity_discount'] = $this->db->get()->result();

				}

			}

		}



		$this->load->view('ajax/ajax_get_quantity_discount', $data);

	

	}



	//ajax ajax_get_quantity_discount_price

	public function ajax_get_quantity_discount_price() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$id_product_detail = (int) $this->input->post('id_product_details');

		$id_product = (int) $this->input->post('id_product');

		$quantity = (int) $this->input->post('product_quantity');



		$this->load->helper('category_discount');

		$category_discount_percentage = category_discount($id_product);

		

		if(isset($this->session->userdata('customer')['customer_id'])) {



			//customer is logged in

			//check if customer is a reseller. if reseller use reseller discounted price

			$this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);

			$reseller_id = $this->db->get()->row()->reseller_id;



			//check if reseller min quantity already available (already input by admin)

			$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

			$count_reseller_price = $this->db->get()->num_rows();



			//check if the id_product has quantity discount

    		$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);

    		$count_quantity_discount = $this->db->get()->num_rows();

				

			if($reseller_id != NULL && $count_reseller_price > 0) {

			

				//customer is reseller, and data already inputted by admin. so use reseller price

				$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['price'] = $this->db->get()->row()->price;



				if ($count_quantity_discount > 0) {



					//count if min_quantity <= '$quantity' is exist

					$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

					$count_discount_percentage = $query->num_rows();



					if($count_discount_percentage > 0) {

						

						$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

						$discount_percentage = $query->row()->discount_percentage;



						$data['discounted_price'] = $data['price'] - ($data['price'] * $discount_percentage / 100);



					} else {

						$data['discounted_price'] = 0;

					}



				} else {

					$data['discounted_price'] = 0;;

				}



			} elseif($reseller_id != NULL && $id_product_detail == 0) {



				//customer is a reseller. id_product_detail is 0 because he choose no option with 0 id product details

				//get product detail id (for 1st detail only)

				$this->db->select('id_product_details')->from('product_details')->where('product_id', $id_product)->limit(1);

				$id_product_detail = $this->db->get()->row()->id_product_details;



				$this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['price'] = $this->db->get()->row()->price;



				if ($count_quantity_discount > 0) {



					//count if min_quantity <= '$quantity' is exist

					$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

					$count_discount_percentage = $query->num_rows();



					if($count_discount_percentage > 0) {

						

						$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

						$discount_percentage = $query->row()->discount_percentage;



						$data['discounted_price'] = $data['price'] - ($data['price'] * $discount_percentage / 100);



					} else {

						$data['discounted_price'] = 0;

					}



				} else {

					$data['discounted_price'] = 0;;

				}



			} elseif($reseller_id == NULL || $count_reseller_price == 0) {

				

				//customer is not a reseller or data not yet inputted by admin, so use normal price with 0 id product details

				if($id_product_detail != 0) {

					//product detail is not 0, means customer did choose an option

					//get the initial product price from product_details table

					$this->db->select('price')->from('product_details')->where('id_product_details', $id_product_detail)->order_by('id_product_details', 'ASC')->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;



					//check if the id_product has quantity discount

					$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);

					$count_quantity_discount = $this->db->get()->num_rows();



					if ($count_quantity_discount > 0) {



						//count if min_quantity <= '$quantity' is exist

						$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

						$count_discount_percentage = $query->num_rows();



						if($count_discount_percentage > 0) {

							

							$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

							$discount_percentage = $query->row()->discount_percentage;



							$data['discounted_price'] = $data['price'] - ($data['price'] * $discount_percentage / 100);



						} else {

							if($category_discount_percentage != NULL) {

								//category discount is active

								$this->db->select('price, discounted_price')->from('product_details')->where('id_product_details', $id_product_detail);

								$prices = $this->db->get()->row(); 

								$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

							} else {

								//category discount is not active

								$this->db->select('discounted_price')->from('product_details')->where('id_product_details', $id_product_detail);

								$data['discounted_price'] = $this->db->get()->row()->discounted_price;

							}

						}



					} else {

						//no quantity discount

						$this->db->select('discounted_price')->from('product_details')->where('id_product_details', $id_product_detail);

						$data['discounted_price'] = $this->db->get()->row()->discounted_price;

					}



				} else {

					//id_product_details is not available, because customer choose option with 0 id product details

					$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;



					//check if the id_product has quantity discount

					$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);

					$count_quantity_discount = $this->db->get()->num_rows();



					if ($count_quantity_discount > 0) {



						//count if min_quantity <= '$quantity' is exist

						$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

						$count_discount_percentage = $query->num_rows();



						if($count_discount_percentage > 0) {

							

							$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

							$discount_percentage = $query->row()->discount_percentage;



							$data['discounted_price'] = $data['price'] - ($data['price'] * $discount_percentage / 100);



						} else {

							if($category_discount_percentage != NULL) {

								//category discount is active

								$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

								$prices = $this->db->get()->row(); 

								$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

							} else {

								//category discount is not active

								$this->db->select('discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

								$data['discounted_price'] = $this->db->get()->row()->discounted_price;

							}	

						}



					} else {

						//no quantity discount

						if($category_discount_percentage != NULL) {

							//category discount is active

							$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

							$prices = $this->db->get()->row(); 

							$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

						} else {

							//category discount is not active

							$this->db->select('discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

							$data['discounted_price'] = $this->db->get()->row()->discounted_price;

						}	

					}

				}

				

			}



		} else {



			//if customer is not logged in

			if($id_product_detail != 0) {

				//product detail is not 0, means customer did choose an option

				if($category_discount_percentage != NULL) {

					//category discount is active

					$this->db->select('price')->from('product_details')->where('id_product_details', $id_product_detail);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

				} else {

					//category discount is not active

					//get the initial product price from product_details table

					$this->db->select('price')->from('product_details')->where('id_product_details', $id_product_detail);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

				}

				

				//check if the id_product has quantity discount

				$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);

				$count_quantity_discount = $this->db->get()->num_rows();



				if ($count_quantity_discount > 0) {



					//count if min_quantity <= '$quantity' is exist

					$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

					$count_discount_percentage = $query->num_rows();



					if($count_discount_percentage > 0) {

						

						$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

						$discount_percentage = $query->row()->discount_percentage;



						$data['discounted_price'] = $data['price'] - ($data['price'] * $discount_percentage / 100);



					} else {

						if($category_discount_percentage != NULL) {

							//category discount is active

							$this->db->select('price')->from('product_details')->where('id_product_details', $id_product_detail);

							$prices = $this->db->get()->row(); 

							$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

						} else {

							//category discount is not active

							$this->db->select('discounted_price')->from('product_details')->where('id_product_details', $id_product_detail);

							$data['discounted_price'] = $this->db->get()->row()->discounted_price;

						}

					}



				} else {

					//no quantity discount

					$this->db->select('discounted_price')->from('product_details')->where('id_product_details', $id_product_detail);

					$data['discounted_price'] = $this->db->get()->row()->discounted_price;

				}



			} else {

				//id_product_details is not available, because customer choose option with 0 id product details

				if($category_discount_percentage != NULL) {

					//category discount is active

					$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

					$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

				} else {

					//category discount is not active

					$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

					$prices = $this->db->get()->row(); 

					$data['price'] = $prices->price;

				}



				//check if the id_product has quantity discount

				$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);

				$count_quantity_discount = $this->db->get()->num_rows();



				if ($count_quantity_discount > 0) {



					//count if min_quantity <= '$quantity' is exist

					$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

					$count_discount_percentage = $query->num_rows();



					if($count_discount_percentage > 0) {

						

						$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE product_id = '$id_product' AND  min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");

						$discount_percentage = $query->row()->discount_percentage;



						$data['discounted_price'] = $data['price'] - ($data['price'] * $discount_percentage / 100);



					} else {

						if($category_discount_percentage != NULL) {

							//category discount is active

							$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

							$prices = $this->db->get()->row(); 

							$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

						} else {

							//category discount is not active

							$this->db->select('discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

							$data['discounted_price'] = $this->db->get()->row()->discounted_price;

						}	

					}



				} else {

					//no quantity discount

					if($category_discount_percentage != NULL) {

						//category discount is active

						$this->db->select('price')->from('product_details')->where('product_id', $id_product)->limit(1);

						$prices = $this->db->get()->row(); 

						$data['discounted_price'] = $prices->price - ($prices->price * $category_discount_percentage/100);

					} else {

						//category discount is not active

						$this->db->select('discounted_price')->from('product_details')->where('product_id', $id_product)->limit(1);

						$data['discounted_price'] = $this->db->get()->row()->discounted_price;

					}	

				}

			}

		}

		

		$this->load->view('ajax/ajax_get_price', $data);



	}



	//ajax get product purchase min quantity

	public function ajax_get_min_quantity() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$id_product_detail = (int) $this->input->post('id_product_details');

		$id_product = (int) $this->input->post('id_product');



		//get initial min quantity

		if(isset($this->session->userdata('customer')['customer_id'])) {



			//customer is logged in

			//check if customer is a reseller. if reseller use reseller min quantity

			$this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);

			$reseller_id = $this->db->get()->row()->reseller_id;



			//check if reseller min quantity already available (already input by admin). If not, display 1 as minimum quantity

			$this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

			$count_reseller = $this->db->get()->num_rows();



			if($reseller_id != NULL && $count_reseller > 0) {

				

				//customer is reseller, and data already inputtedby admin. so use reseller min quantity

				$this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['reseller_min_quantity'] = $this->db->get()->row()->min_quantity;



			} elseif($reseller_id == NULL) {

				

				$data['reseller_min_quantity'] = 1;



			} elseif($reseller_id != NULL && $count_reseller == 0) {



				//customer is a reseller, but data not input yet, or customer choose empty option..

				//then give default reseller min quantity

				//get id_product_details

				$this->db->select('id_product_details')->from('product_details')->where('product_id', $id_product)->order_by('id_product_details', 'ASC')->limit(1);

				$id_default_product_detail = $this->db->get()->row()->id_product_details;



				//get default reseller min quantity

				$this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_default_product_detail);

				$data['reseller_min_quantity'] = $this->db->get()->row()->min_quantity;

			}



		} else {

			//if customer is not logged in

			//set min quantity as 1

			$data['reseller_min_quantity'] = 1;

		}

		

		echo $data['reseller_min_quantity'];

	}	



	//ajax get product purchase min quantity

	public function ajax_get_quantity_options() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$id_product_detail = (int) $this->input->post('id_product_details');

		$id_product = (int) $this->input->post('id_product');



		//get min quantity

		if(isset($this->session->userdata('customer')['customer_id'])) {



			//customer is logged in

			//check if customer is a reseller. if reseller use reseller min quantity

			$this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']);

			$reseller_id = $this->db->get()->row()->reseller_id;



			//check if reseller min quantity already available (already input by admin). If not, display 1 as minimum quantity

			$this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

			$count_reseller = $this->db->get()->num_rows();



			if($reseller_id != NULL && $count_reseller > 0) {

				

				//customer is reseller, and data already inputtedby admin. so use reseller min quantity

				$this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail);

				$data['reseller_min_quantity'] = $this->db->get()->row()->min_quantity;



			} elseif($reseller_id == NULL) {

				

				$data['reseller_min_quantity'] = 1;



			} elseif($reseller_id != NULL && $count_reseller == 0) {



				//customer is a reseller, but data not input yet, or customer choose empty option..

				//then give default reseller min quantity

				//get id_product_details

				$this->db->select('id_product_details')->from('product_details')->where('product_id', $id_product)->order_by('id_product_details', 'ASC')->limit(1);

				$id_default_product_detail = $this->db->get()->row()->id_product_details;



				//get default reseller min quantity

				$this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_default_product_detail);

				$data['reseller_min_quantity'] = $this->db->get()->row()->min_quantity;

			}



		} else {

			//if customer is not logged in

			//set min quantity as 1

			$data['reseller_min_quantity'] = 1; 

		}

		

		$this->load->view('ajax/ajax_get_quantity_options', $data);

	}	

	public function ajax_get_cart() { 

		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {
   			exit('No direct script access allowed');
		}

		$json_data = array();

		$this->load->helper('cart');
		$json_data['cart_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_cart_popup",'', true);	 
		$json_data['cart_count'] = count_cart_content(); 

		echo json_encode($json_data);

	} 

	//ajax product page add to cart
	public function ajax_add_to_cart() { 

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

		$this->load->library('cart');

		$product_id =  $this->input->post('product_id'); 
		$attribute_detail_ids = $this->input->post('attribute_detail_ids'); // get all chosen attribute detail ids
		
		//get product details id from product_combination
		$correct_product_detail_id = NULL;
		$this->db->distinct(); //only get 1 unique value of product_details_id
		$this->db->select('product_details_id')->from('product_combination')->where('product_id', $product_id)->where_in('attribute_detail_id', $attribute_detail_ids);
		$product_details_ids = $this->db->get()->result();

		foreach ($product_details_ids as $product_details_id) {
			
			$this->db->select('attribute_detail_id')->from('product_combination')->where('product_details_id', $product_details_id->product_details_id);
			$attribute_detail_check_ids = $this->db->get()->result();
			
			$result_check = array();
			
			foreach ($attribute_detail_check_ids as $id) {
				$result_check[] = $id->attribute_detail_id;
			}
			
			$array_match = array_intersect($attribute_detail_ids, $result_check);
			
			if(count($array_match) == count($attribute_detail_ids)) {
				$correct_product_detail_id = $product_details_id->product_details_id;
			} 
		}

		if($correct_product_detail_id != NULL) {

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

			$data['qty'] = (int) $this->input->post('qty'); 

			//get product price
			$product_detail_data = $this->db->select('price, discounted_price, is_indent, sku')->from('product_details')->where('id', $correct_product_detail_id)->get()->row();

			$product_flashsale = $this->db->select('flashsale_products.*, flashsale.status')
			->from('flashsale_products')
			->join('flashsale','flashsale_products.flashsale_id=flashsale.id')
			->where('flashsale_products.product_id', $product_id)
			->where('flashsale_products.product_details_id', $correct_product_detail_id)
			->get();

			$is_indent = $product_detail_data->is_indent;
			$price = $product_detail_data->price;
			$discounted_price = $product_detail_data->discounted_price;
			$sku = $product_detail_data->sku;

			if($discounted_price > 0) {
				$data['price'] = $discounted_price;
			} else {
				$data['price'] = $price;
			}

			$reseller_id = $this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id'])->get()->row()->reseller_id;

			

			if($product_flashsale->num_rows()>0){

				if($product_flashsale->row()->status = 'active'){
					$data['price'] = $product_flashsale->row()->discounted_price; 
				}
			}else{
				if($reseller_id != NULL){
					$reseller_price = $this->db->select('price')
					->from('resellers_price')
					->where('product_detail_id', $correct_product_detail_id)
					->where('reseller_id', $reseller_id)
					->get()->row()->price;

					$data['price'] = $reseller_price;
				}
			}

			//check all stock
			$total_stock = $this->db->select_sum('stock')->from('stock')->where('id_product', $product_id)->where('id_product_detail', $correct_product_detail_id)->get()->row()->stock;

			if($total_stock > 0) {

				//get current cart qty for this product detail
				$current_qty = 0;
			    foreach($this->cart->contents() as $item) {
			    	if($item['id'] == $correct_product_detail_id) {
			    		$current_qty = $current_qty + $item['qty'];
			    	}
			    }

				//check if total stock is less or equal to purchase qty
				if($total_stock >= ($data['qty'] + $current_qty)) {
					//stock is enough	
					$data['options']['stock_condition'] = 'In Stock';
				} else {
					//stock is not enough
					$not_enough_stock = 'yes';
				}

			} else {
				//stock is 0. check if indent is allow
				$is_indent = $this->db->select('is_indent')->from('product_details')->where('id', $correct_product_detail_id)->where('product_id', $product_id)->get()->row()->is_indent;

				if($is_indent == 'yes') { 

					$data['options']['stock_condition'] = 'Indent';

					if($discounted_price > 0) {
						$data['options']['downpayment_price'] = $data['price'] * ($product_data->indent_dp / 100);
					} else {
						$data['options']['downpayment_price'] = $data['price'] * ($product_data->indent_dp / 100);
					}

					if($product_flashsale->num_rows()>0){ 
						if($product_flashsale->row()->status = 'active'){
							$data['options']['downpayment_price'] = $product_flashsale->row()->discounted_price * ($product_data->indent_dp / 100); 
						}
					}else{
						if($reseller_id != NULL){
							$reseller_price = $this->db->select('price')
							->from('resellers_price')
							->where('product_detail_id', $correct_product_detail_id)
							->where('reseller_id', $reseller_id)
							->get()->row()->price;

							$data['price'] = $reseller_price;
						}
					}
					
					$data['options']['dp_percentage'] = $product_data->indent_dp;

					$data['options']['indent_message'] = '<span style="font-size:12px">DP IDR ' . number_format($data['options']['downpayment_price'],0,',','.') . ' (' . $product_data->indent_dp . '%)</span>';
					

				} else {

					//stock is not enough
					$not_enough_stock = 'yes';
				}
			}
		} 

		if(isset($not_enough_stock)) {

			echo 'no stock';

		} else {

			$data['id'] = $correct_product_detail_id;
			$data['name'] = $product_data->title;
			$data['options']['attribute_detail_ids'] = $attribute_detail_ids;
			$data['options']['sku'] = $sku;
			$this->cart->product_name_rules = '[:print:]'; //this is to eliminate cart product name restriction on special characters
			$this->cart->insert($data); 
			$this->load->helper('cart');
			$json_data['cart_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_cart_popup",'', true);	
			$json_data['cart_count'] = count_cart_content(); 

			echo json_encode($json_data);  
		}
	} 

	public function ajax_remove_cart() { 

		if (!$this->input->is_ajax_request()) {
   			exit('No direct script access allowed');
		}

		$rowid = $this->input->post('rowid');

		if ($rowid == 'all') {
			
			$this->cart->destroy();

		} else {

			$data = array(
				'rowid'   => $rowid,
				'qty'     => 0
			);
			$this->cart->update($data);
		}

		$json_data = array();

		$this->load->helper('cart');
		$json_data['cart_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_cart_popup",'', true);	 
		$json_data['cart_count'] = count_cart_content(); 

		echo json_encode($json_data);
	}



	//callback function validation cek stock available when add to cart

	public function cek_stock() {



		$id_product_details = (int) $this->input->post('product_size'); 

		$chosen_quantity = (int) $this->input->post('qty'); ; 

		

		//get current stock froms product_details table

		$this->db->select('stock');

		$this->db->from('product_details');

		$this->db->where('id_product_details', $id_product_details);

		$query = $this->db->get(); 

		$current_stock = (int)  $query->row()->stock;



		//check if quantity is less or equal to current stock

		if ($chosen_quantity > $current_stock) {

			return FALSE;

		} else {

			return TRUE;  

		}	

	}



	public function ajax_get_district() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$province_id = (int) $this->input->post('id_province'); 



		//check districts table if province_id already available

		$this->db->select('id_indonesia_districts')->from('indonesia_districts')->where('indonesia_id_province', $province_id);

		$count_districts = $this->db->get()->num_rows();



		if($count_districts > 0) {



			//districts already available, get the districts

			$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $province_id);

			$data['districts'] = $this->db->get()->result();



		} else {



			//districts not available yet..then get rajaongkir data and store into districts table

			$this->load->helper('rajaongkir');

			//get list of districts from RajaOngkir.com API

			$districts = get_rajaongkir_data('city?province=' . $province_id); //get from helper file

			

			foreach($districts['rajaongkir']['results'] as $district) {



				//check first if rajaongkir district_id already exist..

				$this->db->select('rajaongkir_id_district')->from('indonesia_districts')->where('rajaongkir_id_district', $district['city_id']);

				$count_districts = $this->db->get()->num_rows();



				if($count_districts == 0) {

					//can input new data, because still empty

					//insert into districts database

					$data = array(

						'rajaongkir_id_district' => $district['city_id'],

						'district' => $district['city_name'],

						'indonesia_id_province' => $province_id

					);

					$this->db->insert('indonesia_districts', $data);

				}	

			}



			//districts should be available now, get the districts

			$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $province_id);

			$data['districts'] = $this->db->get()->result();

		}



		$this->load->view('ajax/ajax_get_district', $data);

	}



	public function ajax_get_shipping_district() {

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

		$shipping_province_id = (int) $this->input->post('id_shipping_province');

		//check districts table if province_id already available
		$this->db->select('id_indonesia_districts')->from('indonesia_districts')->where('indonesia_id_province', $shipping_province_id);
		$count_districts = $this->db->get()->num_rows();

		if($count_districts > 0) {
			
			//districts already available, get the districts
			$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $shipping_province_id);
			$data['shipping_districts'] = $this->db->get()->result();

		} else {

			//districts not available yet..then get rajaongkir data and store into districts table
			$this->load->helper('rajaongkir');
			//get list of districts from RajaOngkir.com API
			$districts = get_rajaongkir_data('city?province=' . $shipping_province_id); //get from helper file

			foreach($districts['rajaongkir']['results'] as $district) {

				//check first if rajaongkir district_id already exist..
				$this->db->select('rajaongkir_id_district')->from('indonesia_districts')->where('rajaongkir_id_district', $district['city_id']);
				$count_districts = $this->db->get()->num_rows();

				if($count_districts == 0) {
					//can input new data, because still empty
					//insert into districts database
					$data = array(
						'rajaongkir_id_district' => $district['city_id'],
						'district' => $district['city_name'],
						'indonesia_id_province' => $shipping_province_id
					);
					$this->db->insert('indonesia_districts', $data);
				}	
			}

			//districts should be available now, get the districts
			$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $shipping_province_id);
			$data['shipping_districts'] = $this->db->get()->result();
		}

		$this->load->view('ajax/ajax_get_shipping_district', $data);
	}



	public function ajax_get_subdistrict() {



		//if(!$_POST) { show_404(); } 



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$district_id = (int) $this->input->post('id_district'); 



		//check subdistricts table if district_id already available

		$this->db->select('id_indonesia_subdistricts')->from('indonesia_subdistricts')->where('indonesia_id_district', $district_id);

		$count_subdistricts = $this->db->get()->num_rows();



		if($count_subdistricts > 0) {



			//subdistricts already available, get the subdistricts

			$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $district_id);

			$data['subdistricts'] = $this->db->get()->result();



		} else {

			//subdistricts not available yet..then get rajaongkir data and store into subdistricts table

			$this->load->helper('rajaongkir');

			//get list of subdistricts from RajaOngkir.com API

			$subdistricts = get_rajaongkir_data('subdistrict?city=' . $district_id); //get from helper file



			foreach($subdistricts['rajaongkir']['results'] as $subdistrict) {



				//check first if rajaongkir subdistrict_id already exist..

				$this->db->select('rajaongkir_id_subdistrict')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', $subdistrict['subdistrict_id']);

				$count_subdistricts = $this->db->get()->num_rows();



				if($count_subdistricts == 0) {

					//can input new data, because still empty

					//insert into subdistricts database

					$data = array(

						'rajaongkir_id_subdistrict' => $subdistrict['subdistrict_id'],

						'subdistrict' => $subdistrict['subdistrict_name'],

						'indonesia_id_district' => $district_id

					);

					$this->db->insert('indonesia_subdistricts', $data);

				}	

			}



			//subdistricts should be available now, get the subdistricts

			$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $district_id);

			$data['subdistricts'] = $this->db->get()->result();

		}



		$this->load->view('ajax/ajax_get_subdistrict', $data);

	}



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

		$shipping_district_id = (int) $this->input->post('id_shipping_district'); 
		//check subdistricts table if district_id already available
		$this->db->select('id_indonesia_subdistricts')->from('indonesia_subdistricts')->where('indonesia_id_district', $shipping_district_id);
		$count_subdistricts = $this->db->get()->num_rows();

		if($count_subdistricts > 0) {
			//subdistricts already available, get the subdistricts
			$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $shipping_district_id);
			$data['shipping_subdistricts'] = $this->db->get()->result();

		} else {

			//subdistricts not available yet..then get rajaongkir data and store into subdistricts table
			$this->load->helper('rajaongkir');
			//get list of subdistricts from RajaOngkir.com API
			$subdistricts = get_rajaongkir_data('subdistrict?city=' . $shipping_district_id); //get from helper file

			foreach($subdistricts['rajaongkir']['results'] as $subdistrict) {
				//check first if rajaongkir subdistrict_id already exist..
				$this->db->select('rajaongkir_id_subdistrict')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', $subdistrict['subdistrict_id']);
				$count_subdistricts = $this->db->get()->num_rows();

				if($count_subdistricts == 0) {
					//can input new data, because still empty
					//insert into subdistricts database
					$data = array(
						'rajaongkir_id_subdistrict' => $subdistrict['subdistrict_id'],
						'subdistrict' => $subdistrict['subdistrict_name'],
						'indonesia_id_district' => $shipping_district_id
					);
					$this->db->insert('indonesia_subdistricts', $data);
				}	
			}

			//subdistricts should be available now, get the subdistricts
			$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $shipping_district_id);
			$data['shipping_subdistricts'] = $this->db->get()->result();
		}

		$this->load->view('ajax/ajax_get_shipping_subdistrict', $data);
	}



	public function send_sms_code() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$phone = $this->security->xss_clean($this->input->post('handphone_number'));



		//create 4 random number SMS Code

		$random1 = rand(1,9); 

		$random2 = rand(1,9); 

		$random3 = rand(1,9); 

		$random4 = rand(1,9); 

		$sms_code = $random1 . $random2 . $random3 . $random4;



		//check if the phone number already exist in sms_code table

		$this->db->select('id_sms_code')->from('sms_code')->where('phone', $phone);

		$count_handphone_number = $this->db->get()->num_rows();



		if($count_handphone_number == 0) {

			//handphone number not exist yet...then add new record

			$data = array(

				'phone' => $phone,

				'sms_code' => $sms_code

			);

			$this->db->insert('sms_code', $data);

		

		} else {

			//handphone number already exist...then update record

			$data = array(

				'sms_code' => $sms_code

			);

			$this->db->where('phone', $phone);

			$this->db->update('sms_code', $data);

		}



		//send sms code to user's phone by sms gateway..

		$url = 'http://gateway.siskomdigital.com:12010/cgi-bin/sendsms';

		$params = array( 'gw-username' => 'oky18003', 'gw-password' => '1qa2ws4r', 'gw-to' => '62' . $phone, 'gw-from' => 'Farmaku.com', 'gw-text' => 'Farmaku.com OTP Anda adalah ' . $sms_code,

		'gw-coding' => '1', 'gw-dlr-url' => base_url() . 'sms_receiver',

		'gw-dlr-mask' => '1'

		);

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);

		curl_setopt($ch, CURLOPT_POST, true);

		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

		$response = curl_exec($ch);

		curl_close ($ch);

		echo $response; //contoh: status=0&msgid=0028_alpha0219164522660005.0001;



		//update status and msgid into sms_code table

		$response_array = explode('&', $response);

		$status_array = explode('=', $response_array[0]);

		$status = $status_array[1];



		$msgid_array = explode('=', $response_array[1]);

		$msgid = $msgid_array[1];



		//update record

		$data = array(

			'status' => $status,

			'msgid' => $msgid

		);

		$this->db->where('phone', $phone);

		$this->db->update('sms_code', $data);



		echo $response;



	}



	public function ajax_check_stock() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$qty = (int) $this->security->xss_clean($this->input->post('qty'));

		$product_id = (int) $this->security->xss_clean($this->input->post('product_id'));

		$cart_row_id = $this->security->xss_clean($this->input->post('row_id'));

		$subtotal = $this->security->xss_clean($this->input->post('subtotal'));



		//update qty to cart item

		$data = array(

	        'rowid' => $cart_row_id,

	        'qty'   => $qty,

	        'subtotal' => $subtotal

		);

		$this->cart->update($data);



		//get backorder status

		$this->db->select('is_backorder')->from('products')->where('id_products', $product_id);

		$is_backorder = $this->db->get()->row()->is_backorder;



		//get total stok from warehouse

		$this->db->select_sum('stock')->from('stock')->where('id_product', $product_id);

		$total_stock = $this->db->get()->row()->stock;

		

		if($total_stock < $qty) {

			if($is_backorder == 'no') {

				echo 'stok tidak cukup';

			} 

		} else {

			//stock cukup..

			echo '&nbsp;';

		}

	}



	public function ajax_set_subtotal() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$qty 			= (int) $this->security->xss_clean($this->input->post('qty'));

		$product_id 	= (int) $this->security->xss_clean($this->input->post('product_id'));

		$cart_row_id 	= $this->security->xss_clean($this->input->post('row_id'));

		$price 			= $this->security->xss_clean($this->input->post('price'));

		$subtotal 		= $qty*$price;



		$data = array(

	        'rowid' => $cart_row_id,

	        'qty'   => $qty,

	        'subtotal' => $subtotal

		);

		$this->cart->update($data);



		$cart = $this->cart->contents();

		$grand_total = 0;

		foreach ($cart as $item) {

			$count_grand_total = $count_grand_total + $item['subtotal'];

		}

		if($count_grand_total > 0){

			$grand_total = $count_grand_total;

		}



		$data['subtotal'] 	= 'IDR ' .  number_format($subtotal);

		$data['grand_total']= 'TOTAL:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDR '. number_format($grand_total);

		echo json_encode($data);

	}



	public function ajax_get_2hourdelivery() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		//get current subdistricts for 2hour and 1day delivery

		$this->db->select('twohour_subdistrict_id')->from('shipment_method_express')->where('warehouse_id', $this->input->post('id_warehouse'));

		$data['current_2hour_subdistrict_id'] = $this->db->get()->result();



		$this->db->select('*')->from('indonesia_subdistricts');

        $this->db->join('indonesia_districts', 'indonesia_districts.rajaongkir_id_district = indonesia_subdistricts.indonesia_id_district');

        $this->db->where('indonesia_districts.indonesia_id_province', $this->input->post('id_province'));

        $data['subdistricts'] = $this->db->get()->result(); 



        $this->load->view('ajax/ajax_2hourdelivery', $data);

	}



	public function ajax_get_1dayservice() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$this->db->select('oneday_subdistrict_id')->from('shipment_method_express')->where('warehouse_id', $this->input->post('id_warehouse'));

		$data['current_1day_subdistrict_id'] = $this->db->get()->result();



		$this->db->select('*')->from('indonesia_subdistricts');

        $this->db->join('indonesia_districts', 'indonesia_districts.rajaongkir_id_district = indonesia_subdistricts.indonesia_id_district');

        $this->db->where('indonesia_districts.indonesia_id_province', $this->input->post('id_province'));

        $data['subdistricts'] = $this->db->get()->result(); 



        $this->load->view('ajax/ajax_1dayservice', $data);

	}

	public function ajax_check_stock_shipping() {

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

		$qty = (int) $this->security->xss_clean($this->input->post('qty'));
		$item_id = (int) $this->security->xss_clean($this->input->post('item_id'));
		$product_id = (int) $this->security->xss_clean($this->input->post('product_id'));
		$warehouse_id = $this->security->xss_clean($this->input->post('warehouse_id'));
		$is_backorder = $this->security->xss_clean($this->input->post('is_backorder'));

		//get total stok from warehouse
		error_reporting(0);
		$warehouse_stock = $this->db->select('stock')->from('stock')->where('id_product', $product_id)->where('id_product_detail', $item_id)->where('warehouse_id', $warehouse_id)->get()->row()->stock;
		
		if($warehouse_stock < $qty) {
			if($is_backorder == 'no') {
				echo 'Not Enough Stock';		
			}
		} else {
			echo '&nbsp;';
		}
	}

	public function ajax_change_shipping_fee() {

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

		$this->load->helper('shipping');
		$this->load->helper('rajaongkir');
	    $data['qty'] = $this->input->post('qty');
	    $data['id_province'] = $this->input->post('province');
	    
	    $data['price'] = $this->input->post('price');
	    $data['rowid'] = $this->input->post('rowid');
	    $data['warehouse_id'] = $this->input->post('warehouse_id');
	    $data['shipping_method_ids'] = $this->input->post('shipping_method_ids');
	    $data['shipping_id_subdistrict'] = $this->input->post('shipping_id_subdistrict');
	    $data['product_id'] = $this->input->post('product_id');
	    $data['id'] = $this->input->post('item_id');
	    $data['selected_shipping_method_id'] = $this->input->post('selected_shipping_method_id');

	    //get shipping fee
	    $shipping_info = calculate_shipping_fee($data['selected_shipping_method_id'], $data['warehouse_id'], $data['product_id'], $data['id'], $data['qty'], $data['shipping_id_subdistrict']);  

		$total_shipping_fee = $shipping_info['total_shipping_fee'];

		$subtotal = $data['qty'] * $data['price'];

	    //add new info to shipping cart session

	    $shipping_cart =  $this->session->userdata('shipping_cart');
		$shipping_cart[$data['rowid']]['qty'] = $this->input->post('qty');
		$shipping_cart[$data['rowid']]['subtotal'] = $subtotal;
		$shipping_cart[$data['rowid']]['shipping_fee'] = $total_shipping_fee;
		$this->session->set_userdata('shipping_cart', $shipping_cart); 

		if($this->session->userdata('site_lang') == 'english') {
			$this->lang->load('shipping', 'english');
		} else {
			$this->lang->load('shipping', 'indonesian'); 
		}
        $this->load->view('ajax/ajax_change_shipping_fee', $data); 
	}

	public function ajax_get_subtotal() {

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

		$this->load->helper('shipping');
		$this->load->helper('rajaongkir');

	    $data['qty'] = $this->input->post('qty');
	    $data['price'] = $this->input->post('price');
	    $data['rowid'] = $this->input->post('rowid');
	    $data['warehouse_id'] = $this->input->post('warehouse_id');
	    $data['shipping_id_subdistrict'] = $this->input->post('shipping_id_subdistrict');
	    $data['id'] = $this->input->post('item_id');
	    $data['product_id'] = $this->input->post('product_id');
	    $data['selected_shipping_method_id'] = $this->input->post('selected_shipping_method_id');

	    if($data['selected_shipping_method_id'] == 2) {
	    	//get self delivery fee/gosend from configuration table
			$this->db->select('shopdelivery_fee')->from('configuration')->where('id_configuration', 1);
			$shopdelivery_fee = $this->db->get()->row()->shopdelivery_fee;
			$shipping_name = '2 Hour Delivery';
			$total_shipping_fee = $shopdelivery_fee;

	    } else {
	    	$shipping_info = calculate_shipping_fee($data['selected_shipping_method_id'], $data['warehouse_id'], $data['product_id'], $data['id'], $data['qty'], $data['shipping_id_subdistrict']); 
			$total_shipping_fee = $shipping_info['total_shipping_fee'];
	    }

		$subtotal = $data['qty'] * $data['price'];
		//add new info to shipping cart session
	    $shipping_cart =  $this->session->userdata('shipping_cart');
		$shipping_cart[$data['rowid']]['qty'] = $this->input->post('qty');
		$shipping_cart[$data['rowid']]['subtotal'] = $subtotal;
		$shipping_cart[$data['rowid']]['chosen_shipping_id'] = $data['selected_shipping_method_id'];
		$shipping_cart[$data['rowid']]['shipping_fee'] = $total_shipping_fee;
		$this->session->set_userdata('shipping_cart', $shipping_cart);  

		echo number_format($subtotal);   
	}

	public function ajax_get_grandtotal() {

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

		//get grand total for total products, total shipping fee, grand total

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

		$total_item_amount = 0;
		/*$final_total_shipping_fee = 0;*/
		foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {
			$total_item_amount = $total_item_amount + $item['subtotal'];
		}

		/*new get final total shipping fee*/
		$this->load->helper('rajaongkir');
		$shipping_id_subdistrict = $this->input->post('subdistrict');

		//get shipping_id_district & shipping_id_province
		$shipping_id_district = $this->db->select('indonesia_id_district')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', $shipping_id_subdistrict)->get()->row()->indonesia_id_district;

		$shipping_id_province = $this->db->select('indonesia_id_province')->from('indonesia_districts')->where('rajaongkir_id_district', $shipping_id_district)->get()->row()->indonesia_id_province;
		
		$final_total_shipping_fee = $this->calculate_total_shipping_fee($shipping_id_subdistrict); //from My_controller
		
		$free_shipping_fee = $this->calculate_free_shipping_fee($shipping_id_province, $final_total_shipping_fee); //from My_controller

		$finalshippingfee = 0;
		$calculate_finalshippingfee = $final_total_shipping_fee - $free_shipping_fee;
		if($calculate_finalshippingfee > 0){
			$finalshippingfee = $calculate_finalshippingfee;
		}

		//GET THE VALUE OF INDENT REMAINING (only for indent item)
		$indent_remaining = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {

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

				$indent_remaining = $indent_remaining + (($shipping_cart_item['price'] - $shipping_cart_item['dp_price']) * $shipping_cart_item['qty']);
			}	
		}

		//GET THE VALUE OF INDENT SHIPPING FEE (only for indent item)
		$indent_shipping_fee = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {

			if($shipping_cart_item['is_backorder'] == 'yes') {
				$indent_shipping_fee = $indent_shipping_fee + $shipping_cart_item['shipping_fee'];
			}	
		}

		$final_grand_total = 0;
		$grand_total = $total_item_amount + $finalshippingfee - $indent_remaining - $indent_shipping_fee;
		if($grand_total > 0){
			$final_grand_total = $grand_total;
		}

		/*$grand_total = $total_item_amount + $final_total_shipping_fee;*/

		$data_total = array(
			'total_item_amount' => number_format($total_item_amount),
			'total_shipping_fee' => number_format($final_total_shipping_fee),
			'total_free_shipping_fee' => number_format($free_shipping_fee),
			'finalshippingfee' => number_format($finalshippingfee),
			'grand_total' => number_format($final_grand_total),
			'indent_remaining' => '-' . number_format($indent_remaining),
			'indent_shipping_fee' => '-' . number_format($indent_shipping_fee)
		);
		// $this->db->select('shopdelivery_fee')->from('configuration')->where('id_configuration', 1);
		// $shopdelivery_fee = $this->db->get()->row()->shopdelivery_fee;
		// if($shopdelivery_fee == 0){
		// 	$data_total['total_shipping_fee']=$shopdelivery_fee;
		// 	$data_total['finalshippingfee']=$shopdelivery_fee;
		// }

		$id_province = $this->input->post('province'); 

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

		$condition_freeshipping = false;

		if($free_shipping_type == 'region') {
			$selected_region_province = 
			$this->db->select('*')
			->from('free_shipping_region')
			->where('configuration_id',1)
			->where('province_id',$id_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;
				}  
			}
		}
		 

		if($condition_freeshipping == true){ 
			$data_total['grand_total'] = number_format($total_item_amount - $indent_remaining - $indent_shipping_fee);

			$data_total['total_shipping_fee'] = number_format(0); 
			$data_total['finalshippingfee'] = number_format(0);

			// $data_total['total_item_amount'] = number_format($total_item_amount); 

			// $data_total['total_free_shipping_fee'] = number_format($free_shipping_fee);

			// $data_total['indent_remaining'] = '-' . number_format($indent_remaining);

			// $data_total['indent_shipping_fee'] = '-' . number_format($indent_shipping_fee);

		}
		$data_total['condition_freeshipping'] = $condition_freeshipping; 
		echo json_encode($data_total);
	}

	public function update_shipping_address() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		if(!empty($this->input->post('shipping_address'))) {

			$data = array(

			'shipping_address' => $this->input->post('shipping_address'),

			);

			$this->db->where('id_customers', $this->input->post('customer_id'));

			$this->db->update('customers', $data);

		}



		echo 'success';

		

	}



	public function update_shipping_postcode() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		if(!empty($this->input->post('shipping_postcode'))) {

			$data = array(

			'shipping_postcode' => $this->input->post('shipping_postcode'),

			);

			$this->db->where('id_customers', $this->input->post('customer_id'));

			$this->db->update('customers', $data);

		}



		echo 'success';

		

	}



	public function ajax_get_suggest_product() {

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

		//test if ajax call to prevent direct access
		if (!$this->input->is_ajax_request()) {
   			exit('No direct script access allowed');
		}
		$search_data = $this->security->xss_clean($this->input->post('search_data'));
		//search by area firstly..
		$this->db->select('*');
		$this->db->from('products');
    	$this->db->like('title', $search_data);
    	$this->db->where('product_status', '1');
    	$this->db->order_by('rand()');
		$this->db->limit(5);
		$data['result_products'] = $this->db->get()->result();
		if ($data['result_products'] != null) {	
			$this->load->view('ajax/ajax_get_suggest_product', $data);
		} 
		else {
			echo "<li>Product Not Available...</li>";
		}
	}


public function ajax_get_sku() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		 $id = $this->security->xss_clean($this->input->post('id'));

		

		$this->db->select('*');

		$this->db->from('product_details');

		$this->db->where('product_id',$id);

  //   	$this->db->like('title', $search_data);

  //   	$this->db->order_by('title', 'ASC');

		// $this->db->limit(10);

		$data['product_detail'] = $this->db->get()->result();



		if ($data['product_detail'] != null) {	

			$this->load->view('ajax/ajax_get_sku', $data);

		} 

		else {

			echo '<p>Product Not Available...</p>';

		}

	}



	public function ajax_admin_get_product() {



		//test if ajax call to prevent direct access

		if (!$this->input->is_ajax_request()) {

   			exit('No direct script access allowed');

		}



		$search_data = $this->security->xss_clean($this->input->post('search_data'));

		

		$this->db->select('*');

		$this->db->from('products');

    	$this->db->like('title', $search_data);

    	$this->db->order_by('title', 'ASC');

		$this->db->limit(10);

		$data['result_products'] = $this->db->get()->result();



		if ($data['result_products'] != null) {	

			$this->load->view('ajax/ajax_admin_get_product', $data);

		} 

		else {

			echo '<p>Product Not Available...</p>';

		}

	}

	/*new ajax voucher and point rewards*/
	public function ajax_set_voucher() {

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

		$input_voucher		= $this->security->xss_clean($this->input->post('voucher'));
		$pointprice			= $this->security->xss_clean($this->input->post('pointprice'));
		$id_customer 		= $this->security->xss_clean($this->input->post('id_customer'));
		$voucher_price 		= 0;
		$voucher_discount 	= '';
		$alert				= '';

		if($this->session->userdata('customer')['customer_type'] == 'guest') {
			$alert = 'Silahkan Login untuk menggunakan voucher';
		}

		

		//check if the voucher quantity already empty
		$this->db->select('qty_ready')->from('vouchers')->where('voucher_code', $input_voucher);
		$qty_ready = $this->db->get()->row()->qty_ready;
		if ($qty_ready == 0 && $qty_ready != NULL) {
			$alert = 'Voucher Code Used Up!';
		}

		//check if the voucher usage already exceed max customer usage
		//get max quantity
		$this->db->select('maxqty_per_person')->from('vouchers')->where('voucher_code', $input_voucher);
		$maxqty_per_person = $this->db->get()->row()->maxqty_per_person;

		if ($maxqty_per_person != NULL) {
			
			//get voucher id
			$this->db->select('id_vouchers')->from('vouchers')->where('voucher_code', $input_voucher);
			$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', $id_customer);
			$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', $id_customer);
				$voucher_used = (int) $this->db->get()->row()->voucher_used;

				//if the user voucher already exceed max quota
				if ($voucher_used >= $maxqty_per_person) {
					$alert = 'You have used max allowed no. of vouchers / customer';
				}
			}
		}

		/*get grand total without shipping fee*/
		$grand_total_without_shipping = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {
			$grand_total_without_shipping = $grand_total_without_shipping + $item['subtotal'];
		}

		//cek jika discount value lebih besar dari total order without shipping, maka di cegat
		/*$this->db->select('discount_value')->from('vouchers')->where('voucher_code', $input_voucher);
		$cek_discount_value = $this->db->get()->row()->discount_value;
		if($cek_discount_value > $grand_total_without_shipping) {
			$alert = 'Harap menambahkan jumlah transaksi anda min. IDR '.number_format($cek_discount_value).'<br/> untuk menggunakan voucher ini';
		}*/

		//get minimum order from voucher table
		$this->db->select('min_order')->from('vouchers')->where('voucher_code', $input_voucher);
		$min_order = $this->db->get()->row()->min_order;

		if ($min_order != NULL) {
			if ($grand_total_without_shipping < (int) $min_order) {
				$alert = 'Sorry Your order amount is not enough';
			}
		}

		/*VOUCHER VALIDATION*/
		$get_voucher = $this->db->select('voucher_code')->from('vouchers')->where('voucher_code like binary '.'"'.$input_voucher.'"'.'')->get()->row();
		if (count($get_voucher) == 0) {
			$alert = 'Voucher Code Not Exist!';
		} 

		//Check for expired date
		//get expired date for this voucher
		$this->db->select('expired_date')->from('vouchers')->where('voucher_code', $input_voucher);
		$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) {
				$alert = 'Sorry Your Voucher Code Already Expired';
			}
		}

		//get voucher type
		$this->db->select('voucher_type')->from('vouchers')->where('voucher_code', $input_voucher);
		$voucher_type = $this->db->get()->row()->voucher_type;

		//id_customer
			
		switch ($voucher_type) {
			case 'normal promo':
				# do nothing...
				break;

			case 'birthday promo':
				//get birthmonth
				$this->db->select('birthmonth')->from('vouchers')->where('voucher_code', $input_voucher);
				$voucher_birthmonth = (int) $this->db->get()->row()->birthmonth;
				//get customer birthmonth
				$this->db->select('birthday')->from('customers')->where('id_customers', $id_customer);
				$birthday = $this->db->get()->row()->birthday;
				$birthday_array = explode('-',$birthday);
				$customer_birthmonth = (int) $birthday_array[1];
				if ($voucher_birthmonth != $customer_birthmonth) {
					$alert = 'Sorry It is not Your Birth Month';
				}
				break;	

			case 'gender promo':
				//get gender
				$this->db->select('gender')->from('vouchers')->where('voucher_code', $input_voucher);
				$voucher_gender = $this->db->get()->row()->gender;
				//get customer gender
				$this->db->select('sex_type')->from('customers')->where('id_customers', $id_customer);
				$customer_gender = $this->db->get()->row()->sex_type;
				/*if ($customer_title == 'mr') {
					$customer_gender = 'male';
				} else {
					$customer_gender = 'female';
				}*/
				if ($voucher_gender != $customer_gender) {
					$alert = 'Sorry It is not Your Gender';
				}	
				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', $input_voucher);
				$promostart = strtotime($this->db->get()->row()->promostart);
				//get end promo time
				$this->db->select('promoend')->from('vouchers')->where('voucher_code', $input_voucher);
				$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
					$alert = 'Sorry Promo Time expired';
				}
				break;	

			case 'province promo':
				//get province_id
				$this->db->select('provincepromo')->from('vouchers')->where('voucher_code', $input_voucher);
				$voucher_province_id = (int) $this->db->get()->row()->provincepromo;
				//get customer province_id
				$this->db->select('shipping_id_province')->from('customers')->where('id_customers', $id_customer);
				$customer_shipping_id_province = $this->db->get()->row()->shipping_id_province;
				if ($voucher_province_id != $customer_shipping_id_province) {
					$alert = 'Sorry It is not Your Province';
				}	
				break;	

			case 'quantity promo':
				//get min quantity at checkout
				$this->db->select('quantitypromo')->from('vouchers')->where('voucher_code', $input_voucher);
				$voucher_quantitypromo = (int) $this->db->get()->row()->quantitypromo;
				//get current combined product quantity at cart
				$cart = $this->session->userdata('shipping_cart');
				$total_cart_quantity = 0;
				foreach ($cart as $item) {
					$total_cart_quantity = $total_cart_quantity + $item['qty'];
				}
				if ($total_cart_quantity < $voucher_quantitypromo) {
					$alert = 'Sorry Your Total Cart Quantity not enough';
				}	
				break;	

			case 'category promo':

				//get categories id from voucher code
				$this->db->select('categorypromo')->from('vouchers')->where('voucher_code', $input_voucher);
				$categories = $this->db->get()->row()->categorypromo;
				$categories_array = explode(',', $categories);
				//get current cart content
				$cart = $this->session->userdata('shipping_cart');
				$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) {
					$alert = 'Sorry You did not choose Products with Promoted Category';
				}
				break;	

			case 'brand promo':
				//get brands id from voucher code
				$this->db->select('brandpromo')->from('vouchers')->where('voucher_code', $input_voucher);
				$brands = $this->db->get()->row()->brandpromo;
				$brands_array = explode(',', $brands);
				//get current cart content
				$cart = $this->session->userdata('shipping_cart');
				$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) {
					$alert = 'Sorry You did not choose Products with Promoted Brand';
				}
				break;		
		}
		/*VOUCHER VALIDATION*/

		/*if alert is null, is mean validation false*/
		if($alert != ''){
			$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');
		}

		/*if alert null, is mean validation true*/
		else{
			/*SET VOUCHER VALUE*/
			//get discount type and amount
			$this->db->select('*')->from('vouchers')->where('voucher_code', $input_voucher);
			$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);
			//$voucher_price = (int) $voucher->discount_value;
			
			if($voucher->voucher_type == 'category promo') {

				if($voucher->discount_type == 'percentage') {
					$voucher_discount = '('.$voucher->discount_value.'%)';

					//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', $input_voucher);
					$categories = $this->db->get()->row()->categorypromo;
					$categories_array = explode(',', $categories);

					//get current cart content
					$cart = $this->session->userdata('shipping_cart');
					
					$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);*/
							$voucher_price = $voucher_price + ($item['price'] * $item['qty'] * $discount_rate / 100);
						}
					}
					$this->session->set_userdata('total_categoryproduct_promo', (int) $voucher_price);
				}
				else{
					$voucher_discount = '';
					$voucher_price = (int) $voucher->discount_value;
				}	
			} 

			elseif($voucher->voucher_type == 'brand promo') {

				if($voucher->discount_type == 'percentage') {
					$voucher_discount = '('.$voucher->discount_value.'%)';

					//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', $input_voucher);
					$brands = $this->db->get()->row()->brandpromo;
					$brands_array = explode(',', $brands);

					//get current cart content
					$cart = $this->session->userdata('shipping_cart');
					
					$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);*/
							$voucher_price = $voucher_price + ($item['price'] * $item['qty'] * $discount_rate / 100);
						}	
					}
					$this->session->set_userdata('total_brandproduct_promo', (int) $voucher_price);
				}
				else{
					$voucher_discount = '';
					$voucher_price = (int) $voucher->discount_value;
				}	
			}
			else{
				if($voucher->discount_type == 'percentage') {
					$voucher_discount = '('.$voucher->discount_value.'%)';
					$product_grand_total = 0;
					foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {
						$product_grand_total = $product_grand_total + $item['subtotal'];
					}
					$voucher_price = ($voucher->discount_value/100) * $product_grand_total;
				}
				else{
					$voucher_discount = '';
					$voucher_price = (int) $voucher->discount_value;
				}
			}

			$this->session->set_userdata('redeemed_voucher_amount', $voucher_price);

			/*SET VOUCHER VALUE*/
		}

		/*hitung grand total include point rewards*/
		$total_item_amount = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {
			$total_item_amount = $total_item_amount + $item['subtotal'];
		}

		/*if($voucher_price > $total_item_amount) {
			$alert = 'Jumlah Voucher Yang Ditukar <br/>IDR '.number_format($voucher_price).'<br/> Melebihi Total Pembelian';
			$voucher_price	= 0;
		}*/

		/*new get final total shipping fee*/
		$this->load->helper('rajaongkir');
		$shipping_id_subdistrict = $this->input->post('subdistrict');

		//get shipping_id_district & shipping_id_province
		$shipping_id_district = $this->db->select('indonesia_id_district')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', $shipping_id_subdistrict)->get()->row()->indonesia_id_district;

		$shipping_id_province = $this->db->select('indonesia_id_province')->from('indonesia_districts')->where('rajaongkir_id_district', $shipping_id_district)->get()->row()->indonesia_id_province;
		
		$final_total_shipping_fee = $this->calculate_total_shipping_fee($shipping_id_subdistrict); //from My_controller

		$free_shipping_fee = $this->calculate_free_shipping_fee($shipping_id_province, $final_total_shipping_fee); //from My_controller

		$finalshippingfee = 0;
		$calculate_finalshippingfee = $final_total_shipping_fee - $free_shipping_fee;
		if($calculate_finalshippingfee > 0){
			$finalshippingfee = $calculate_finalshippingfee;
		}

		//GET THE VALUE OF INDENT REMAINING (only for indent item)
		$indent_remaining = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {

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

				$indent_remaining = $indent_remaining + (($shipping_cart_item['price'] - $shipping_cart_item['dp_price']) * $shipping_cart_item['qty']);
			}	
		}

		//GET THE VALUE OF INDENT SHIPPING FEE (only for indent item)
		$indent_shipping_fee = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {

			if($shipping_cart_item['is_backorder'] == 'yes') {
				$indent_shipping_fee = $indent_shipping_fee + $shipping_cart_item['shipping_fee'];
			}	
		}

		$final_grand_total = 0;
    $grand_total = ($total_item_amount - $voucherprice - $pointprice) + $finalshippingfee - $indent_remaining - $indent_shipping_fee;
  
		if($grand_total > 0){
			$final_grand_total = $grand_total;
		} else {
			//check if finalshippingfee is > 0
			if($finalshippingfee > 0) {
				$final_grand_total = $finalshippingfee;
			}
		}  

		$data_total = array(
			'total_item_amount'			=> number_format($total_item_amount),
			'voucher_discount'			=> $voucher_discount,
			'voucherprice'				=> '-' . number_format($voucher_price),
			'voucherprice_input'		=> $voucher_price,
			'alert'						=> $alert,
			'firsttotal'				=> number_format($total_item_amount - $voucher_price - $pointprice),
			'total_shipping_fee' 		=> number_format($final_total_shipping_fee),
			'total_free_shipping_fee' 	=> number_format($free_shipping_fee),
			'finalshippingfee' 			=> number_format($finalshippingfee),
			'grand_total' 				=> number_format($final_grand_total),
			'indent_remaining' 			=> '-' . number_format($indent_remaining),
			'indent_shipping_fee' 		=> '-' . number_format($indent_shipping_fee)

		);
		echo json_encode($data_total);
	}



	public function ajax_set_point_rewards() {

		$voucherprice 		= $this->security->xss_clean($this->input->post('voucherprice'));
		$point 				= $this->security->xss_clean($this->input->post('point'));
		$id_customer 		= $this->security->xss_clean($this->input->post('id_customer'));
		$finalpoint_rewards	= 0;
		$alert				= '';


		if($this->session->userdata('customer')['customer_type'] == 'guest') {
			$alert = 'Silahkan Login untuk menggunakan point reward';
		}

		if($this->session->userdata('customer')['customer_type'] == 'regular') {
			/*hitung point rewards*/
			//get customer current point reward
			$this->db->select('current_pointreward')->from('customers')->where('id_customers', $id_customer);
			$current_point = $this->db->get()->row()->current_pointreward;

			if($point > $current_point){

				$alert = 'Point cannot bigger than '.$current_point;
				$this->session->unset_userdata('chosen_point');
				$this->session->unset_userdata('chosen_point_discount');
				
			} else{

				$this->db->select('*')->from('point_rewards')->where('id_point_rewards', 1);
				$point_rewards 		= $this->db->get()->row();
				$finalpoint_rewards = $point * (int) $point_rewards->conversion;

				$this->session->set_userdata('chosen_point', $point);
				$this->session->set_userdata('chosen_point_discount', $finalpoint_rewards);
			}
			/*hitung point rewards*/
		}

		/*hitung grand total include point rewards*/
		$total_item_amount = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $item) {
			$total_item_amount = $total_item_amount + $item['subtotal'];
		}

		if($finalpoint_rewards > $total_item_amount) {
			$alert = 'Jumlah Point Reward <br/>IDR '.number_format($finalpoint_rewards).'<br/> Melebihi Total Pembelian';
			$finalpoint_rewards	= 0;
		}

		/*new get final total shipping fee*/
		$this->load->helper('rajaongkir');
		$shipping_id_subdistrict = $this->input->post('subdistrict');

		//get shipping_id_district & shipping_id_province
		$shipping_id_district = $this->db->select('indonesia_id_district')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', $shipping_id_subdistrict)->get()->row()->indonesia_id_district;

		$shipping_id_province = $this->db->select('indonesia_id_province')->from('indonesia_districts')->where('rajaongkir_id_district', $shipping_id_district)->get()->row()->indonesia_id_province;
		
		$final_total_shipping_fee = $this->calculate_total_shipping_fee($shipping_id_subdistrict); //from My_controller

		$free_shipping_fee = $this->calculate_free_shipping_fee($shipping_id_province, $final_total_shipping_fee); //from My_controller

		$finalshippingfee = 0;
		$calculate_finalshippingfee = $final_total_shipping_fee - $free_shipping_fee;
		if($calculate_finalshippingfee > 0){
			$finalshippingfee = $calculate_finalshippingfee;
		}

		//GET THE VALUE OF INDENT REMAINING (only for indent item)
		$indent_remaining = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {

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

				$indent_remaining = $indent_remaining + (($shipping_cart_item['price'] - $shipping_cart_item['dp_price']) * $shipping_cart_item['qty']);
			}	
		}

		//GET THE VALUE OF INDENT SHIPPING FEE (only for indent item)
		$indent_shipping_fee = 0;
		foreach ($this->session->userdata('shipping_cart') as $rowid => $shipping_cart_item) {

			if($shipping_cart_item['is_backorder'] == 'yes') {
				$indent_shipping_fee = $indent_shipping_fee + $shipping_cart_item['shipping_fee'];
			}	
		}

		$final_grand_total = 0;
		$grand_total = ($total_item_amount - $voucherprice - $finalpoint_rewards) + $finalshippingfee - $indent_remaining - $indent_shipping_fee;
		if($grand_total > 0 ){
			$final_grand_total = $grand_total;
		}
		/*hitung grand total include point rewards*/

		$data_total = array(
			'total_item_amount'			=> number_format($total_item_amount),
			'pointrewards'				=> '-' . number_format($finalpoint_rewards),
			'pointrewards_input'		=> $finalpoint_rewards,
			'alert'						=> $alert,
			'firsttotal'				=> number_format($total_item_amount - $voucherprice - $finalpoint_rewards),
			'total_shipping_fee' 		=> number_format($final_total_shipping_fee),
			'total_free_shipping_fee' 	=> number_format($free_shipping_fee),
			'finalshippingfee' 			=> number_format($finalshippingfee),
			'grand_total' 				=> number_format($final_grand_total),
			'indent_remaining' 			=> '-' . number_format($indent_remaining),
			'indent_shipping_fee' 		=> '-' . number_format($indent_shipping_fee)
		);
		echo json_encode($data_total);
	}

	public function ajax_cek_current_qty() {

		$product_id = $this->security->xss_clean($this->input->post('product_id'));

		$a 			= $this->cart->contents();

		$b 			= 0;

		foreach ($a as $item) {

			if($item['id'] == $product_id){

				$b = $item['qty'];

			}

			break;

		}

		echo $b;

	}



	//PRODUCT DETAIL PAGE
	public function initialProductDetail() {

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

		$this->load->helper('product'); 

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

		$product_id = $this->input->post('product_id');
		$attribute_detail_ids = $this->input->post('attribute_detail_ids'); // get all chosen attribute detail ids

		//get product details id from product_combination
		$correct_product_detail_id = NULL;

		$this->db->distinct(); //only get 1 uniqe value of product_details_id
		$this->db->select('product_details_id')->from('product_combination')->where('product_id', $product_id)->where_in('attribute_detail_id', $attribute_detail_ids);
		$product_details_ids = $this->db->get()->result();

		foreach ($product_details_ids as $product_details_id) {

			$this->db->select('attribute_detail_id')->from('product_combination')->where('product_details_id', $product_details_id->product_details_id);
			$attribute_detail_check_ids = $this->db->get()->result();

			$result_check = array();

			foreach ($attribute_detail_check_ids as $id) {
				$result_check[] = $id->attribute_detail_id;
			}

			$array_match = array_intersect($attribute_detail_ids, $result_check);

			if(count($array_match) == count($attribute_detail_ids)) {
				$correct_product_detail_id = $product_details_id->product_details_id;
			} 
		}

		$json_data = array();

		//get placeholder image
   		$this->db->select('image_not_available')->from('configuration')->where('id_configuration', 1);
   		$image_data['placeholder_image'] = $this->db->get()->row()->image_not_available;

   		//get product title
       	$this->db->select('title')->from('products')->where('id_products', $product_id);

       	$image_data['product_name'] = $this->db->get()->row()->title;

		if($correct_product_detail_id != NULL) {

			$json_data['match_found'] = 'true';
			//product detail is available..
			//get all images from confirmed product detail id
			$this->db->select('id, image')->from('product_images')->where('product_id', $product_id)->where('product_details_id', $correct_product_detail_id)->where('status', '1')->order_by('priority', 'ASC'); 
			$image_data['product_images'] = $this->db->get()->result(); 
			//get product price
			$this->db->select('*')->from('product_details')->where('id', $correct_product_detail_id);
			$prices = $this->db->get()->row();

			$price_data['price'] = $prices->price;
			$price_data['discounted_price'] = $prices->discounted_price;

			//check all stock
			$this->db->select_sum('stock')->from('stock')->where('id_product', $product_id)->where('id_product_detail', $correct_product_detail_id);
			$total_stock = $this->db->get()->row()->stock;
			
			// product price
			$price_data['sku'] = $this->db->select('sku')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->sku;
			$price_data['weight'] = $this->db->select('weight')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->weight;
			$price_data['stock'] = $total_stock;
			$price_data['primary_colortheme']=$this->data_header['primary_colortheme'];
			$price_data['product_id'] = $product_id;
			$json_data['price_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_get_product_price", $price_data, true);


			if($total_stock > 0) {
				$json_data['stock_text'] = 'In Stock';
			} else {
				//stock is 0. check if indent is allow
				$this->db->select('is_indent')->from('product_details')->where('id', $correct_product_detail_id)->where('product_id', $product_id);
				$is_indent = $this->db->get()->row()->is_indent;

				if($is_indent == 'yes') {
					$json_data['stock_text'] = 'Indent';
				} else {
					$json_data['stock_text'] = 'No Stock';
				}
			}

			//get SKU
			$json_data['sku'] = $this->db->select('sku')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->sku;
			$json_data['weight'] = $this->db->select('weight')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->weight;
			$json_data['stock'] = $total_stock;

		} else {
			$image_data['product_images'] = NULL; 
			$json_data['match_found'] = 'false';
			$json_data['stock_text'] = 'Not Available';
		}

		//get attribute details name
		$json_data['attribute_detail_names'] = '';
	
		foreach ($attribute_detail_ids as $index => $id) {
			
			//get attribute detail name
			if($this->session->userdata('site_lang') == 'english') {
				$this->db->select('attribute_detail_en as attribute_detail')->from('product_attributes_detail')->where('id', $id);
			} else {
				$this->db->select('attribute_detail')->from('product_attributes_detail')->where('id', $id);
			}	
			$attribute_detail_name = $this->db->get()->row()->attribute_detail;

			if($index == 0) {
				$json_data['attribute_detail_names'] = $json_data['attribute_detail_names'] . $attribute_detail_name;
			} else {
				$json_data['attribute_detail_names'] = $json_data['attribute_detail_names'] . '-' . $attribute_detail_name;
			}	
		}

		$json_data['image_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_get_product_images", $image_data, true);

		echo json_encode($json_data); 
	}

	public function changeProductDetail() {
		if($this->session->userdata('site_lang') == 'english') {
			$this->lang->load('product_detail', 'english');
			$this->lang->load('product_list', 'english');
		} else {
			$this->lang->load('product_list', 'indonesian');
			$this->lang->load('product_detail', 'indonesian');
		} 

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

		$product_id = $this->input->post('product_id');
		//get chosen atribute detail id
		$attribute_detail_id = $this->input->post('attribute_detail_id'); 
		// get all chosen attribute detail ids
		$attribute_detail_ids = $this->input->post('attribute_detail_ids'); 

		//get product details id from product_combination
		$correct_product_detail_id = NULL;

		$this->db->distinct(); //only get 1 uniqe value of product_details_id
		$this->db->select('product_details_id')->from('product_combination')->where('product_id', $product_id)->where_in('attribute_detail_id', $attribute_detail_ids);
		$product_details_ids = $this->db->get()->result();

		foreach ($product_details_ids as $product_details_id) {

			$this->db->select('attribute_detail_id')->from('product_combination')->where('product_details_id', $product_details_id->product_details_id);
			$attribute_detail_check_ids = $this->db->get()->result();

			$result_check = array();

			foreach ($attribute_detail_check_ids as $id) {
				$result_check[] = $id->attribute_detail_id;
			}

			$array_match = array_intersect($attribute_detail_ids, $result_check);

			if(count($array_match) == count($attribute_detail_ids)) {
				$correct_product_detail_id = $product_details_id->product_details_id;
			} 
		}

		$json_data = array();

		//get placeholder image
       	$this->db->select('image_not_available')->from('configuration')->where('id_configuration', 1);
       	$image_data['placeholder_image'] = $this->db->get()->row()->image_not_available;

   		//get product title
   		$this->db->select('title')->from('products')->where('id_products', $product_id);
   		$image_data['product_name'] = $this->db->get()->row()->title;

		if($correct_product_detail_id != NULL) {

			$json_data['match_found'] = 'true';
			//product detail is available..
			//get all images from confirmed product detail id
			$this->db->select('id, image')->from('product_images')->where('product_id', $product_id)->where('product_details_id', $correct_product_detail_id)->where('status', '1')->order_by('priority', 'ASC'); 
			$image_data['product_images'] = $this->db->get()->result(); 

			//get product price
			$this->db->select('*')->from('product_details')->where('id', $correct_product_detail_id);
			$prices = $this->db->get()->row();

			$price_data['price'] = $prices->price;
			$price_data['discounted_price'] = $prices->discounted_price;
			
			//check all stock
			$this->db->select_sum('stock')->from('stock')->where('id_product', $product_id)->where('id_product_detail', $correct_product_detail_id);
			$total_stock = $this->db->get()->row()->stock;

			// product price
			$price_data['sku'] = $this->db->select('sku')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->sku;
			$price_data['weight'] = $this->db->select('weight')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->weight;
			$price_data['stock'] = $total_stock;
			$price_data['primary_colortheme']=$this->data_header['primary_colortheme'];
			$price_data['product_details_id'] = $correct_product_detail_id;
			$price_data['product_id'] = $product_id;
			$json_data['price_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_get_product_price", $price_data, true);

			

			if($total_stock > 0) {
				$json_data['stock_text'] = 'In Stock';
			} else {
				//stock is 0. check if indent is allow
				$this->db->select('is_indent')->from('product_details')->where('id', $correct_product_detail_id)->where('product_id', $product_id);
				$is_indent = $this->db->get()->row()->is_indent;

				if($is_indent == 'yes') {
					$json_data['stock_text'] = 'Indent';
				} else {
					$json_data['stock_text'] = 'No Stock';
				}
			}

			//get SKU
			$json_data['sku'] = $this->db->select('sku')->from('product_details')->where('id', $correct_product_detail_id)->get()->row()->sku;

		} else {
			$image_data['product_images'] = NULL;
			$json_data['match_found'] = 'false';
			$json_data['stock_text'] = 'Not Available';
		}

		$json_data['image_content'] = $this->load->view("themes/$this->theme_no/ajax/ajax_get_product_images", $image_data, true); 

		//GET ATTRIBUTE DETAIL NAME
		if($this->session->userdata('site_lang') == 'english') { 
			$this->db->select('attribute_detail_en as attribute_detail')->from('product_attributes_detail')->where('id', $attribute_detail_id);
		} else { 
			$this->db->select('attribute_detail')->from('product_attributes_detail')->where('id', $attribute_detail_id); 
		}

		$attribute_name = $this->db->get()->row()->attribute_detail;
		$json_data['detail_attribute_name'] = ucwords($attribute_name);

		//get attribute details name
		$json_data['attribute_detail_names'] = '';
	
		foreach ($attribute_detail_ids as $index => $id) {
			
			//get attribute detail name
			if($this->session->userdata('site_lang') == 'english') {
				$this->db->select('attribute_detail_en as attribute_detail')->from('product_attributes_detail')->where('id', $id);
			} else {
				$this->db->select('attribute_detail')->from('product_attributes_detail')->where('id', $id);
			}	
			$attribute_detail_name = $this->db->get()->row()->attribute_detail;

			if($index == 0) {
				$json_data['attribute_detail_names'] = $json_data['attribute_detail_names'] . $attribute_detail_name;
			} else {
				$json_data['attribute_detail_names'] = $json_data['attribute_detail_names'] . '-' . $attribute_detail_name;
			}	
		}

		echo json_encode($json_data); 
	}

}

https://t.me/RX1948 - 2025