| Server IP : 188.114.97.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
/*********************
*********************
User Registration Model
Creation 02 may 2019
Aernout Guillaume
http://Codes.Solutions
(Most features could be betters,
Google map api should be use)
*********************
*********************/
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Customer_model extends CI_Model
{
public function __construct()
{
parent::__construct(array('no_cache' => 1));
$this->load->model('crud_model');
$this->load->model('wallet_model');
$this->load->model('course_model');
$this->load->model('notification_model');
$this->load->model('product_transaction_model');
$this->load->database();
}
public function get_entry(int $id)
{
if ($id <= 0) {
return null;
}
$result = $this->db->get_where('customer', ['ID' => $id])->result();
return isset($result[0]) ? $result[0] : null;
}
public function update_entry(int $id, array $fields): bool
{
if ($id <= 0 || empty($fields)) {
return false;
}
return $this->db->update('customer', $fields, ['ID' => $id]);
}
public function transaction_log($stripe, $id, $address, $cart, $tot, $state, $info)
{
$sql = array(
'Stripe_ID' => "$stripe",
'User_ID' => "$id",
'Address_ID' => "$address",
'Products' => json_encode($cart),
'Payment_Amount' => "$tot",
'Payment_State' => "$state",
'Payment_Info' => $info,
'Session' => implode(',', $_SESSION)
);
$this->db->insert('shop_transaction', $this->crud_model->cleanArray($sql));
$id = $this->db->insert_id();
return $id;
}
public function get_cart()
{
$id = $this->session->userdata('userID');
$this->db->order_by("ID", "desc");
$query = $this->db->get_where('shop_transaction', array('User_ID' => $id));
$cart = $query->result_array();
$n = 0;
while ($cart[$n]['Products'] == "null" && $n < count($cart) + 1) {
$n++;
}
$cart = json_decode($cart[$n]['Products'], true);
return $cart['Cart'];
}
public function get_paid_cart()
{
$id = (int) $this->session->userdata('userID');
if (empty($id)) {
return [];
}
$this->db->order_by("ID", "desc");
$query = $this->db->get_where('shop_transaction', [
'User_ID' => $id,
'Payment_Info' => 'done'
]);
$cart = $query->result_array();
$n = 0;
while ($cart[$n]['Products'] == "null" && $n < count($cart) + 1) {
$n++;
}
$cart = json_decode($cart[$n]['Products'], true);
return $cart['Cart'];
}
public function get_transaction()
{
$id = $this->session->userdata('userID');
$this->db->order_by("ID", "desc");
$query = $this->db->get_where('shop_transaction', array('User_ID' => $id,'Products !=' => 'null'));
$cart = $query->result_array();
return $cart[0];
}
public function get_customer($id)
{
$data = $this->db->get_where('customer', array('ID' => $id))->result_array();
return $data[0];
}
public function get_customer_connexe($ip)
{
$data = $this->db->get_where('customer', array('IP' => $ip))->result_array();
return $data;
}
public function get_customer_connexe_bis($ip)
{
$this->db->order_by('ID', 'DESC');
$data = $this->db->get_where('customer', array('IP' => $ip,'Name !=' => '','Surname !=' => '','Mail !=' => ''))->result_array();
return $data[0];
}
public function get_customer_by_stripe(string $stripe_client_secret): ?stdClass
{
$this->db->order_by('ID', 'DESC');
$customer = $this->db->get_where('customer', ['stripe_client_secret' => $stripe_client_secret])->result();
return isset($customer[0]) ? $customer[0] : null;
}
public function get_customer_payment_unfinish(int $id): ?array
{
$sql = "SELECT * FROM `customer`
WHERE ID = $id AND (Payment_State IS NULL OR Payment_State != 'succeeded')";
$customer = $this->db->query($sql)->result_array();
return isset($customer[0]) ? $customer[0] : null;
}
public function get_customers($m = 999)
{
$lim_date = date('Y-m-d', strtotime("-" . (int) $m . " months"));
$sql = "SELECT t.*, u.first_name, u.last_name, u.role_id, u.email
FROM shop_transaction as t
JOIN user as u ON t.User_ID = u.user_id
WHERE u.first_name != '' AND u.last_name != ''
AND t.Payment_Date >= ' . $lim_date . '
ORDER BY t.User_ID DESC";
$transactions = $this->db->query($sql)->result_array();
$query = "SELECT c.ID, c.CourseName FROM `course_new` c";
$courses_name = [];
foreach ($this->db->query($query)->result() as $course) {
$courses_name[(int) $course->ID] = $course->CourseName;
}
$customers = array();
foreach ($transactions as $customer) {
$customer['flagged'] = false;
$customer['fullname'] = $customer['first_name'].' '.$customer['last_name'];
$customer['role'] = $customer['role_id'];
// Remove photos from results (Refactoring all this method, too heavy)
$products = json_decode(str_replace("'", "", $customer['Products']), true);
$cart = json_decode($products['Cart'], true);
foreach ($cart as &$c) {
$c['data'] = is_array($c['data']) ? $c['data'] : json_decode($c['data'], true);
if (empty($c['data']['Course_ID'])) {
$c['data'] = $c['data'][0];
}
$c['data']['course_name'] = $courses_name[(int) $c['data']['Course_ID']];
unset($c['data']['Course_Photo']);
}
unset($customer['Products']);
$products['Cart'] = $cart;
$customer['product'] = $products;
if ($customer['fullname'] != ' ') {
if ($customer['role_id'] == '3') {
$childs = $this->crud_model->get_parent_child($customer['User_ID']);
foreach ($this->notification_model->get_user_notifications($customer['User_ID']) as $n) {
if ($n['Notification_Trigger'] == "Profile_Invited_Pending") {
$customer['notify_invited'] = true;
}
if ($n['Notification_Trigger'] == "Shop_Aborted_Cart") {
$customer['notify_aborted'] = true;
}
}
$customer['childs'] = array();
$wallet_global = 0;
if (count($childs) > 0) {
foreach ($childs as $child) {
$customer['childs'][$child]['student_id'] = $child;
$customer['childs'][$child]['student_name'] = $this->crud_model->get_student_name($child);
$customer['childs'][$child]['balance'] = $this->wallet_model->wallet_balance($child);
$customer['childs'][$child]['courses'] = $this->crud_model->get_student_courses_bis($child);
$wallet_global = $wallet_global + $this->wallet_model->payment_last_amount($child);
}
if (json_decode($customer['Products'])->Cart_Total != $wallet_global) {
$customer['flagged'] = true;
}
} else {
$customer['student_id'] = $customer['User_ID'];
$customer['student_name'] = $customer['fullname'];
$customer['balance'] = $this->wallet_model->wallet_balance($customer['User_ID']);
$customer['adult_courses'] = $this->crud_model->get_student_courses_bis($customer['User_ID']);
}
$customers[] = $customer;
} else {
$customer['student_id'] = $customer['User_ID'];
$customer['student_name'] = $customer['fullname'];
$customer['adult_courses'] = $this->crud_model->get_student_courses_bis($customer['User_ID']);
$customer['balance'] = $this->wallet_model->wallet_balance($customer['User_ID']);
$customers[] = $customer;
}
}
}
return $customers;
}
public function get_paid_unattribued_courses(int $customer_id): array
{
$products_transactions = $this->product_transaction_model->get_all_unassigned((int) $customer_id);
$courses_id = array_map(function ($pt) { return (int) $pt->course_id; }, $products_transactions);
$missing_courses = $this->course_model->get_entries_with_products($courses_id);
return $missing_courses;
}
public function get_sessions($m = 999)
{
$lim_date = date('Y-m-d', strtotime("-$m months"));
$this->db->order_by("ID", "desc");
$this->db->where('Creation >=', $lim_date);
$this->db->from('shop_session');
$query = $this->db->get();
$query = $query->result_array();
foreach ($query as $key => $r) {
if ($r['Linked_User'] != "") {
$query[$key]['Linked_User_Data'] = $this->get_customer($r['Linked_User']);
}
}
return $query;
}
public function check_if_logged($id)
{
$query = $this->db->get_where('customer', array('ID' => $id));//array('Logged_User' => $id, 'Logged_In' => '1'));
$query = $query->result_array();
if ($query[0]['Logged_User'] != null) {
return $query[0]['Logged_User'];
} else {
return (bool) false;
}
}
public function update_user_state($param, $state, $id)
{
$sql = array(
$param => $state
);
$this->db->set($sql);
$this->db->where('user_id', $id);
$this->db->update('user');
}
public function update_user_transaction($param, $state, $id)
{
$sql = array(
$param => $state
);
$this->db->set($sql);
$this->db->where('User_ID', $id);
$this->db->update('shop_transaction');
}
public function update_session($signature, $id)
{
$sql = array(
'State' => 'done',
'Linked_User' => $id
);
$this->db->set($sql);
$this->db->where('Signature', $signature);
$this->db->update('shop_session');
}
public function update_session_manualy($id)
{
$sql = array(
'State' => 'manualy validated'
);
$this->db->order_by('ID', 'DESC');
$this->db->set($sql);
$this->db->where('Linked_User', $id);
$this->db->limit(1);
$this->db->update('shop_session');
//return $this->db->last_query();
}
public function update_balance_new($student_id, $courses, $repartition)
{
$vat = 21;
$trans = $this->get_transaction();
$promo = 0;
$check = json_decode($trans['Products'])->Cart_Total_Promo;
if ($check != 0 && $check != null) {
$promo = (json_decode($trans['Products'])->Cart_Total) - (json_decode($trans['Products'])->Cart_Total_Promo);
}
$courses = is_array($courses) ? $courses : json_decode($courses);
$total = 0;
$c_list = '';
foreach ($courses as $key => $c) {
$c_list = $c_list.";".$c;
$c_price = $this->crud_model->course_shop_price($c);
if ($this->wallet_model->is_vat_course($c)) {
$vatPrice = (($c_price / 100) * $vat);
$c_price = $vatPrice + $c_price;
}
$c_price = round($c_price, 2);
$total = $total + $c_price;
if ($promo > 0 && is_numeric($promo)) {
$total -= ($promo / $repartition);
}
}
$c_list = substr($c_list, 1);
if ($trans['Payment_State'] == "succeeded" && $total > 0) {
$sql = array(
'Payment_ID' => $trans['ID'],
'Student_ID' => $student_id,
'Payment_amount' => $total,
'Method_ID' => '9',
'Status' => '1',
'Date' => date("Y-m-d"),
'Comment' => 'shop payment from user'.$this->session->userdata('userID') .'[course:'.$c_list.' ]',
'Courses_id' => json_encode($courses),
'Payment_expiration' => ''
);
$last_wallet = $this->wallet_model->get_last_wallet();
if ($last_wallet['Courses_id'] != $sql['Courses_id'] || $last_wallet['Student_ID'] != $sql['Student_ID'] || $last_wallet['Payment_amount'] != $sql['Payment_amount']) {
$this->db->insert('wallet', $this->crud_model->cleanArray($sql));
$sql_id = $this->db->insert_id();
}
}
if ($promo > 0 && is_numeric($promo)) {
//insert promo code
$sql = array(
'Student_ID' => $student_id,
'Discount_amount' => $promo / $repartition,
'Comment' => "Transaction linked: ".$sql_id."[promo repartition :".$repartition."]"
);
$this->db->insert('discount', $this->crud_model->cleanArray($sql));
$sql2_id = $this->db->insert_id();
}
}
public function update_balance_student(int $student_id, int $transaction_id, int $course_id, int $repartition = 1): bool
{
$vat = 21.0;
$trans = $this->wallet_model->get_transaction_by_id($transaction_id);
if (empty($trans)) {
return false;
}
$promo = 0;
$cart = json_decode($trans->Products);
$check = $cart->Cart_Total_Promo;
if ($check != 0 && $check != null) {
$promo = (int) $cart->Cart_Total - (int) $cart->Cart_Total_Promo;
}
$c_list .= ";" . $course_id;
$c_price = $this->crud_model->course_shop_price($course_id);
if ($this->wallet_model->is_vat_course($course_id)) {
$vatPrice = (($c_price / 100) * $vat);
$c_price = $vatPrice + $c_price;
}
$c_price = round($c_price, 2);
$total = $c_price;
if ($promo > 0 && is_numeric($promo)) {
$total = $total - ($promo / $repartition);
}
$c_list = substr($c_list, 1);
if ($trans->Payment_State == "succeeded" && $total > 0) {
$sql = array(
'Payment_ID' => $trans->ID,
'Student_ID' => $student_id,
'Payment_amount' => $total,
'Method_ID' => '9',
'Status' => '1',
'Date' => date("Y-m-d"),
'Comment' => 'shop payment from user'.$this->session->userdata('userID') .'[course:'.$c_list.' ]',
'Courses_id' => json_encode($courses),
'Payment_expiration' => ''
);
$last_wallet = $this->wallet_model->get_last_wallet();
if ($last_wallet['Courses_id'] != $sql['Courses_id'] || $last_wallet['Student_ID'] != $sql['Student_ID'] || $last_wallet['Payment_amount'] != $sql['Payment_amount']) {
$this->db->insert('wallet', $this->crud_model->cleanArray($sql));
$sql_id = $this->db->insert_id();
}
}
if ($promo > 0 && is_numeric($promo)) {
//insert promo code
$sql = array(
'Student_ID' => $student_id,
'Discount_amount' => $promo / $repartition,
'Comment' => "Transaction linked: ".$sql_id."[promo repartition :".$repartition."]"
);
$this->db->insert('discount', $this->crud_model->cleanArray($sql));
$sql2_id = $this->db->insert_id();
}
return true;
}
public function update_balance($id, $repartition)
{
$trans = $this->get_transaction();
$courses = array();
$transObject = json_decode($trans['Products'], true);
//return $transObject;
foreach ($transObject['cart'] as $key => $object) {
$course_id = $object['data'][0]['Course_ID'];
if ($course_id != null && $course_id != '') {
$courses[$course_id] = $this->crud_model->get_course_name($course_id);
}
}
//return json_encode($courses);
if ($trans['Payment_State'] == "succeeded") {
$sql = array(
'Payment_ID' => $trans['ID'],
'Student_ID' => $id,
'Payment_amount' => ($trans['Payment_Amount'] / $repartition),
'Method_ID' => '9',
'Status' => '1',
'Date' => date("Y-m-d"),
'Comment' => 'shop payment from user'.$this->session->userdata('userID') .' [repartition :'.$repartition.']',
'Courses_id' => json_encode($courses),
'Payment_expiration' => ''
);
// var_dump($sql);
$this->db->insert('wallet', $this->crud_model->cleanArray($sql));
$id = $this->db->insert_id();
}
}
public function set_preference($user, $course)
{
$sql = array(
'CourseID' => $course,
'StudentID' => $user
);
$this->db->insert('shop_preference', $this->crud_model->cleanArray($sql));
$id = $this->db->insert_id();
}
public function reset_preference($user, $course)
{
$sql = array(
'CourseID' => $course,
'StudentID' => $user
);
// $this->db->where($sql);
$this->db->delete('shop_preference', $sql);
//return $this->db->last_query();
}
public function get_preference($user)
{
$query = $this->db->get_where('shop_preference', array('StudentID' => $user));
$array = $query->result_array();
return $array;
}
}