<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: capp
 * Date: 29/05/2018
 * Time: 15:49
 */


class Nano_Credit_model extends CI_Model
{

	// Nano credit
	public function insertCreditLimit($limit_min, $limit_max, $has_nano_credit, $id_network)
	{
		$sql = "UPDATE `configWallet` SET `has_nano_credit` = ?, `limite_credit_min` = ? , `limite_credit_max` = ? WHERE (`id_network` = ? );";
		return $this->db->query($sql, array($has_nano_credit, $limit_min, $limit_max, $id_network));
	}

	public function insertNanoCreditSharedRates($taux_ag, $taux_sup, $taux_hyp, $id_network)
	{
		$sql = "UPDATE `configWallet` SET `taux_com_ag_nano_credit` = ?, `taux_com_sup_nano_credit` = ? , `taux_com_hyp_nano_credit` = ? WHERE (`id_network` = ? );";
		return $this->db->query($sql, array($taux_ag, $taux_sup, $taux_hyp, $id_network));
	}

	public function getAllIlinkWorldNetworks()
	{
		$query = $this->db->query("SELECT networks.name AS network,networks.status AS status,networks.id,cc.name AS country,networks.country_id , configWallet.id_network , configWallet.type,cc.currency_code,
 			 configWallet.limite_credit_min , configWallet.limite_credit_max , configWallet.has_nano_credit  FROM `networks`
 												INNER JOIN countries_currencies cc ON networks.country_id=cc.id LEFT JOIN configWallet ON configWallet.id_network = networks.id WHERE status = 1 AND configWallet.type = 'ilink'");
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getUsersGroups($id_network)
	{
		$query = $this->db->query("SELECT * FROM infos_users_groups WHERE id_network = '" . $id_network . "'");
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getNanoCreditRates($idConfig, $type = 'nano_credit')
	{
		$sql = "SELECT *  FROM `paliersConfigNanoCredit` WHERE (`idConfig` = ?  AND `type` = ? );";
		$query = $this->db->query($sql, array($idConfig, $type));
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function deleteNanoCreditRates($idConfig, $type = 'nano_credit')
	{
		$sql = "DELETE FROM `paliersConfigNanoCredit` WHERE (`idConfig` = ?  AND `type` = ? );";
		$query = $this->db->query($sql, array($idConfig, $type));
		return $query;
	}

	public function addNanoCreditRates($idConfig, $duree, $valeur, $type = 'nano_credit')
	{
		$sql = "INSERT INTO `paliersConfigNanoCredit` (`duree_mois`, `taux`, `idConfig` ,`type`) VALUES (?,?,?,?);";
		$query = $this->db->query($sql, array($duree, $valeur, $idConfig, $type));
		return $query;
	}

	public function getUserDemandesCredit($debut, $fin, $id_network)
	{
		$chain = $debut ? " AND date_creation BETWEEN '" . $debut . "' AND '" . $fin . "'" : "";
		$query = $this->db->query("SELECT *
                                    FROM infos_users_demandes_credits
                                    WHERE `id_network`='" . $id_network . "'" . $chain);
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getUserDemandesCreditForSuper($debut, $fin, $codeParrain)
	{
		$chain = $debut ? " AND date_creation BETWEEN '" . $debut . "' AND '" . $fin . "'" : "";
		$query = $this->db->query("SELECT *
                                    FROM infos_users_demandes_credits
                                    WHERE `codeParrain`='" . $codeParrain . "'" . $chain);
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getUsersSavings($debut, $fin, $id_network)
	{
		$chain = $debut ? " AND date_creation BETWEEN '" . $debut . "' AND '" . $fin . "'" : "";
		$query = $this->db->query("SELECT *
                                    FROM infos_users_epargnes
                                    WHERE `id_network`='" . $id_network . "'" . $chain);
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getGroupMembers($group_code){
		$query = $this->db->query("SELECT
									CASE 
									WHEN u.id = ug.id_createur THEN 'creator'
									WHEN u.id = id_sponsor1 or u.id = id_sponsor2 or u.id = id_sponsor3  THEN 'sponsor'
									ELSE 'member'
								   END as role,
								    u.* ,  wu.balance , ug.* , u.id as id_user FROM users u inner JOIN users_groups ug on u.group_id = ug.id INNER JOIN wallets_users wu ON wu.idUser = u.id where ug.code_groupe = '" . $group_code . "'");
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getGroup($group_code){
		$query = $this->db->query("SELECT * FROM infos_users_groups WHERE code_groupe = '" . $group_code . "'");
		if ($query->num_rows() > 0) {
			return $query->first_row();
		} else {
			return false;
		}
	}

	// Somme des credits de groupes pour un utilisateur donné
	public function sumGroupCredit($id_user){
		$query = $this->db->query("SELECT (SUM(montant + montant_rembourse +interet + taxe)) AS credit FROM users_demandes_credits WHERE etat='VALIDE' AND type_caution='groupe' AND id_user = '" . $id_user . "'");
		if ($query->num_rows() > 0) {
			return $query->first_row();
		} else {
			return false;
		}
	}
}
