| Server IP : 188.114.96.2 / Your IP : 104.23.197.230 Web Server : Apache/2.4.59 (Debian) System : Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64 User : edlftp ( 1002) PHP Version : 7.4.33 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_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/nicolasj/www/sms.formationlangues.be/application/models/ |
Upload File : |
<?php
if (! defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Fidelity_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->helper('generator');
$this->load->database();
}
public function updateCart()
{
$signature = $this->get_signature();
$cart_array = $this->db->get_where('shop_session', array('Signature' => $signature,'State' => ''))->result_array();
if(!empty($cart_array)) {
$cart = array();
if($cart_array[0]['Cart'] != null) {
$cart = json_decode($cart_array[0]['Cart'], true);
}
//resume cart data
$cart_qty = 0;
$cart_total = 0;
$cart_vat = 0;
$pro = 0;
$pro_vat = 0;
$ob = 0;
$ob_vat = 0;
foreach($cart as $key => $product) {
$data = json_decode($product['data'], true);
$price_original = $data[0]['Course_Price'];
//var_dump($data);
$price = $price_original;
$target_vat = 0;
$target_qty = $product['qty'];
$selector = ($data[0]);
//var_dump($selector);
if($selector['Price_TVA_Promo'] == 0) {
$price = $selector['Price_TVA'];
} else {
$price = $selector['Price_TVA_Promo'];
//echo "promo";
}
// update cart data
$cart_qty = $cart_qty + $target_qty;
$cart_vat = 0;//$cart_vat + ($target_vat * $target_qty);
if($this->crud_model->get_course_society_id($selector['Course_ID']) == '2') {
$pro = $pro + ($price * $target_qty);
} else {
$ob = $ob + ($price * $target_qty);
}
$cart_total = round($cart_total + ($price * $target_qty), 2);
}
//promotion code
$cart_total_promo;
//check if promo
$voucher = $cart_array[0]['Promotion'];
$voucher_amount = intval($voucher);
if($voucher != '') {
if(strpos($voucher, "€") > -1) {
//euro
$cart_total_promo = $cart_total - $voucher_amount;
} elseif (strpos($voucher, "%") > -1) {
//echo "debug";
//percent
$cart_total_promo = $cart_total - ($cart_total * $voucher_amount / 100);
} else {
//nothing
}
}
$data = array(
"Cart_Qty" => $cart_qty,
"Cart_Total" => $cart_total,
"Cart_Total_Promo" => round($cart_total_promo, 2),
"Cart_Vat" => $cart_vat,
"Pro_Total" => $pro,
"Pro_Total_Vat" => $pro_vat,
"OB_Total" => $ob,
"OB_Total_Vat" => $ob_vat,
"Cart" => json_encode($cart),
);
}
}
public function check_rules($id = 0)
{
if($id == 0) {
$user = $this->session->userdata('userID');
} else {
$user = $id;
}
$today = date('Y-m-d');
//echo $code;
$this->db->where('Date_From <=', $today);
$this->db->where('Date_To >=', $today);
$rules = $this->db->get('fidelity')->result_array();
//var_dump($rules);
if(count($rules) > 0) {
foreach ($rules as $key => $r) {
$target = $r['User'];
if($target != '#') {
if(!isset($user)) {
echo "please login first";
return;
}
if($user == $target) {
//only for the target
if(1) {
if($this->check_promo_min($r)) {
//insert history
//==> check promo society rule
$total = $this->get_cart_amount($r);
$point = round(($total / $r['Point']), 2);
$sql = array(
'User' => $user,
'Point' => $point,
'Amount' => $total,
'FidelityID' => $r['ID']
);
$id = $this->db->insert('fidelity_point_collected', $sql);
echo "true";
} else {
echo "you doesnt reach the minimal amount";
}
} else {
echo "you reached the maximum of use for the promotional code";
}
} else {
echo "Debug : not for the user :$user";
echo "Invalid promotional code";
return;
}
} else {
//for everyone
if(1) {
if($this->check_promo_min($r)) {
$total = $this->get_cart_amount($r);
//echo $total;
$point = round(($total / $r['Point']), 2);
$sql = array(
'User' => $user,
'Point' => $point,
'Amount' => $total,
'FidelityID' => $r['ID']
);
$id = $this->db->insert('fidelity_point_collected', $sql);
echo "true";
} else {
echo "you doesnt reach the minimal amount";
}
} else {
echo "you reached the maximum of use for the promotional code";
}
}
}
} else {
echo "Invalid or Expired promotional code";
return;
}
}
public function check_promo_min($rule)
{
$this->db->order_by('ID', 'DESC');
$sessionCart = $this->db->get_where('shop_session', array("Signature" => $this->get_signature(),"State" => ''))->result_array();
$amount = $sessionCart[0]['Cart_Total'];
//var_dump($amount);
if($rule['Minimum'] < $amount) {
return true;
} else {
return false;
}
}
public function get_cart_amount($rule)
{
$this->db->order_by('ID', 'DESC');
$sessionCart = $this->db->get_where('shop_session', array("Signature" => $this->get_signature(),"State" => ''))->result_array();
$s = $rule['Society'];
//sepcify society amount
if($s == '#') {
return $sessionCart[0]['Cart_Total'];
} elseif($s == 'ob') {
return $sessionCart[0]['OB_Total'];
} elseif($s == 'pro') {
return $sessionCart[0]['Pro_Total'];
} else {
return null;
}
}
public function get_point_collected($total)
{
$point = $this->db->get_where('fidelity_point', ["ID" => '1'])->result_array()[0];
return round(($total / $point['Value']), 2);
}
public function get_signature()
{
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$signature = "";
$browsers = [
["IE", "Microsoft Internet Explorer"],
["Chrome", "Google Chrome"],
["Firefox", "Mozzila Firefox"],
["Opera", "Opera"],
["Safari", "Apple Safari"]
];
foreach($browsers as $browser) {
if (strpos($userAgent, $browser[0]) !== false) {
$signature = $signature.",".$browser[1];
}
}
return !empty($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR'];
}
}