| Server IP : 188.114.96.2 / Your IP : 104.23.197.231 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/controllers/BO/ |
Upload File : |
<?php
/*********************
*********************
Aernout Guillaume
http://Codes.Solutions
*********************
*********************/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Fidelity extends NS_Controller {
function __construct() {
parent::__construct(array('no_cache'=>1));
$this->load->model('crud_model');
$this->load->model('shop_model');
$this->load->database();
}
////////////////////////
// Fonction controller
////////////////////////
function index() {
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
$data['page_name'] = 'fidelity';
$data['page_title'] = get_phrase('fidelity');
$data['students'] = $this->crud_model->get_student_list();
$data['list'] = $this->list();
$data['promo'] = $this->list_promo();
$data['points'] = $this->points();
$data['points_expi'] = $this->points_expi();
$data['points_collected'] = $this->points_collected();
$data['points_collected_total'] = $this->points_collected_total();
$this->load->view('backend/index', $data);
}
function push(){
$this->continueIfAllowed(array('allowed'=>array('admin')));
$p = $this->input->post();
unset($p['s2id_User']);
unset($p['s2id_Society']);
//var_dump($p);
$this->db->insert('fidelity',$this->crud_model->cleanArray($p));
}
function pull(){
$this->continueIfAllowed(array('allowed'=>array('admin')));
$id = $this->input->post('ID');
$this->db->where('ID', $id);
$this->db->delete('fidelity');
}
function list(){
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
return $this->db->get('fidelity')->result_array();
}
function points(){
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
return $this->db->get('fidelity_point')->result_array()[0];
}
function points_expi(){
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
return $this->db->get('fidelity_expi')->result_array()[0];
}
function points_collected(){
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
$role = $this->session->userdata("login_type");
if($role == "student"){
//get also parent points
$users = (array) $this->crud_model->get_child_parent($_SESSION['userID']);
array_push($users,$_SESSION['userID']);
$sql = "SELECT * FROM `fidelity_point_collected` WHERE `User` IN (".implode(',',$users).") AND `Used` = 0 ;";
$data = $this->db->query($sql)->result_array();
var_dump($this->db->last_query());
return $data;
}
else{
//get also child points
$childs = $this->crud_model->get_parent_child($_SESSION['userID']);
array_push($childs,$_SESSION['userID']);
$sql = "SELECT * FROM `fidelity_point_collected` WHERE `User` IN (".implode(',',$childs).") AND `Used` = 0 ;";
$data = $this->db->query($sql)->result_array();
var_dump($this->db->last_query());
return $data;
}
}
function points_collected_total(){
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
$points;
$role = $this->session->userdata("login_type");
if($role == "student"){
//get also parent points
$users = (array) $this->crud_model->get_child_parent($_SESSION['userID']);
array_push($users,$_SESSION['userID']);
$sql = "SELECT * FROM `fidelity_point_collected` WHERE `User` IN (".implode(',',$users).") AND `Used` = 0 ;";
$points = $this->db->query($sql)->result_array();
//echo $this->db->last_query();
}
else{
//get also child points
$childs = $this->crud_model->get_parent_child($_SESSION['userID']);
array_push($childs,$_SESSION['userID']);
$sql = "SELECT * FROM `fidelity_point_collected` WHERE `User` IN (".implode(',',$childs).") AND `Used` = 0 ;";
$points = $this->db->query($sql)->result_array();
//var_dump($this->db->last_query());
}
$total=0;
foreach ($points as $key => $p) {
$total = $total + $p['Point'];
}
var_dump($total);
return $total;
}
function modify_point(){
$this->continueIfAllowed(array('allowed'=>array('admin')));
$v = $this->input->post('v');
$this->db->set("Value",$v);
$this->db->where('ID','1');
$this->db->update('fidelity_point');
}
function modify_point_expi(){
$this->continueIfAllowed(array('allowed'=>array('admin')));
$v = $this->input->post('v');
$this->db->set("Days",$v);
$this->db->where('ID','1');
$this->db->update('fidelity_expi');
}
function check_rules(){
$user = $this->session->userdata('userID');
$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
$total = $this->get_cart_amount($r);
$sql = array(
'User'=>$user,
'Point'=>$this->get_point_collected($total),
'Amount'=>$total,
'FidelityID'=>$r['ID']
);
$id = $this->db->insert('fidelity_point_collected',$this->crud_model->cleanArray($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;
$sql = array(
'User'=>$user,
'Point'=>$this->get_point_collected($total),
'Amount'=>$total,
'FidelityID'=>$r['ID']
);
$id = $this->db->insert('fidelity_point_collected',$this->crud_model->cleanArray($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;
}
}
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;
}
}
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();
return $sessionCart[0]['Cart_Total'];
}
function get_point_collected($total){
$point = $this->db->get_where('fidelity_point',array("ID"=>'1'))->result_array()[0];
return round(($total/$point['Value']),2);
}
function list_promo(){
$this->continueIfAllowed(array('allowed'=>array('admin','parent','student')));
return $this->db->get_where('promotion',array('User'=>$_SESSION['userID']))->result_array();
}
function get_signature(){
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$signature = "";
$browsers = array(
array("IE", "Microsoft Internet Explorer"),
array("Chrome", "Google Chrome"),
array("Firefox", "Mozzila Firefox"),
array("Opera", "Opera"),
array("Safari", "Apple Safari")
);
foreach($browsers as $browser){
if (strpos($userAgent, $browser[0]) !== false){
$signature = $signature.",".$browser[1];
}
}
if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])) {
return md5($_SERVER["HTTP_CF_CONNECTING_IP"]."+".$signature);
}
return md5($_SERVER['REMOTE_ADDR']."+".$signature);
}
function convert_points(){
$points = $this->points();
$points_collected_total = $this->points_collected_total();
$points_expi = $this->points_expi()['Days'];
//var_dump($points_expi);
$user = $this->session->userdata('userID');
//var_dump($points);
$reduction = round(($points_collected_total/$points["Value"]),2);
echo 'debug';
if($reduction == 0){
return;
}
//new promo
$promo = array(
'Name'=>'Fidelity '.date('Y-m-d'),
'Description'=>'Fidelity generate on '.date('Y-m-d').' for '. $points_collected_total.' points',
'Code'=>$this->generateCode(),
'User'=>$user,
'Date_From'=>date('Y-m-d'),
'Date_To'=>date('Y-m-d', strtotime('+'.$points_expi.' days')),
'Minimum'=>'1',
'Numbers'=>'1',
'Numbers_Pers'=>'1',
'Reduction'=>$reduction,
'Method' =>'amount'
);
$this->db->insert('promotion',$this->crud_model->cleanArray($promo));
//remove collected points + childs
$role = $this->session->userdata("login_type");
$users;
if($role != "student"){
$users = $this->crud_model->get_parent_child($_SESSION['userID']);
array_push($users,$_SESSION['userID']);
}
else{
$users = (array) $this->crud_model->get_child_parent($_SESSION['userID']);
array_push($users,$_SESSION['userID']);
}
$sql = "UPDATE `fidelity_point_collected` SET `Used` = '1' WHERE `User` IN (".implode(',',$users).") ;";
$this->db->query($sql);
echo $this->db->last_query();
//var_dump($reduction);
}
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),
);
//var_dump($data);
//UPDATE
$this->db->set($this->crud_model->cleanArray($data));
$this->db->where(array('Signature'=>$signature));
$this->db->update('shop_session');
}
}
function generateCode($length = 5) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
?>