|
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/ptbos.biz/public_html/application/controllers/ |
Upload File : |
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Ajax extends Public_Controller {
public function __construct() {
parent::__construct();
}
public function get_ajax_project() {
$this->load->view('ajax/ajax_project');
}
//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);
}
//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);
}
//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('form_validation');
$this->load->library('cart');
//validation check in action
$config = array(
array(
'field' => 'product_size',
'label' => 'product Size',
'rules' => 'trim|required|callback_cek_stock'
)
);
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$id_product_details = (int) $this->input->post('product_size');
//get product details
$this->db->select('*')->from('product_details')->where('id_product_details', $id_product_details);
$product_details = $this->db->get()->row();
$data['id'] = (int) $this->input->post('product_id');
$data['name'] = '<strong>' . $this->input->post('product_name') . '</strong>' . '<br>Option: ' . $product_details->attributes . '<br>Product Code: ' . $product_details->sku;
$data['qty'] = (int) $this->input->post('qty');
$data['price'] = (int) $this->input->post('price');
$data['options']['size'] = $product_details->attributes;
$data['options']['sku'] = $product_details->sku;
$this->cart->product_name_rules = '[:print:]'; //this is to eliminate cart product name restriction on special characters
$this->cart->insert($data);
echo count($this->cart->contents());
} else {
echo 0; //means not enough stock
}
}
//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() {
//if(!$_POST) { show_404(); }
//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() {
//if(!$_POST) { show_404(); }
//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() {
//if(!$_POST) { show_404(); }
//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);
}
}