<?php
class compte
{
	private $error   = true; 
	
	function __construct($number="")
	{
        
        global $sgbd;
        $this->db = $sgbd;
        $this->account_number = $number;
        if(!empty($number))  $this->load();
       
	}
    function load()
    {
        if(empty($this->account_number))  return false; 
       
        //calcul de la diponibilité du solde
        $day = date('Y-m-d');
        $query = "select * from movements where available = 0 and account_number = '".$this->account_number."' and transaction_type = 'C' and (available_date<='".$day."' or available_date is NULL)";

        $adds = $this->db->query($query)->result_array();
        
        foreach($adds as $add)
        {
           $date = date('Y-m-d');

           $query = "update customers_accounts set balance_available=balance_available + ".$add['amount']." where account_number = '".$this->account_number."'";
           
           $this->db->query($query); 
           $query = "update movements set available=1  where id= '".$add['id']."'";
           $this->db->query($query);
        }
       
        $reals = $this->db->query("select transaction_type,sum(amount) as total from movements where account_number = '".$this->account_number."' group by transaction_type")->result_array();
        
        foreach($reals as $real)
        {
            if($real['transaction_type']=="C") $balance +=$real['total'];
            elseif($real['transaction_type']=="D") $balance -=$real['total'];
        }
       
        $query = "update customers_accounts set balance = '".$balance."' where account_number = '".$this->account_number."'";
        
        $this->db->query($query);
        $available = $this->db->query("select transaction_type,sum(amount) as total from movements where available = 1 and account_number = '".$this->account_number."' group by transaction_type")->row_array();
        
        
        $solde = $this->db->query("select * from customers_accounts where account_number ='".$this->account_number."'")->row_array();
       
        foreach($solde as $key=>$val)
        {
            $this->$key = $val;
        } 
        
       
    }
    function available($service)
    {
        $parts = explode('-',$this->account_number);
       
        $query = "select * from customers_services where country = '".strtoupper($this->country)."' and agent_id = '".$this->agent_id."' and service_code='".$service."' ";
        $indiv = $this->db->query($query)->row_array();
        if(empty($indiv['commission_in']) or empty($indiv['commission_out']))
        {
           
            $query = "select * from customers_services where country = '".strtoupper($this->country)."' and service_code='".$service."' ";
            $cindiv = $this->db->query($query)->row_array();
           
            if(!empty($cindiv['commission_in']))
            {
                $indiv['commission_in'] =  $cindiv['commission_in'];
                $indiv['commission_available_in'] =  $cindiv['commission_available_in'];
                $indiv['commission_type_in'] =  $cindiv['commission_type_in'];
            }

            if(!empty($cindiv['commission_out']))
            {
                $indiv['commission_out'] =  $cindiv['commission_out'];
                $indiv['commission_available_out'] =  $cindiv['commission_available_out'];
                $indiv['commission_type_out'] =  $cindiv['commission_type_out'];
            }
            
        }
        $this->Commission = $indiv;
        
    }

	
}
?>