<?php

class RewardsCustomersRepository extends EntityRepository {
    
    private $table = 'rewards_customers';
    private $options = array (
        'phone' => null,
        'name'=>null,
        'last_name' => null,
        'date_of_birth' => null,
        'email' => null,
        'register_store'=>null,
        'rewards_total' => 0,       
        'rewards' => 0,
        'acumulated_amount'=>0,
        'creado_fecha'=>null,       
     );
    
    public function setOptions($data){
        foreach ($this->options as $option => $value){
            if(isset($data[$option])){
              $this->options[$option] = $data[$option];
            }
        }
    }
  
    public function getOptions(){
        return $this->options;
    }    

    public function save(array $data, $table = null) {   
        return parent::save($data, $this->table);
    }

    public function update($id, $data, $table = null) {
        return parent::update($id, $data, $this->table);
    }

    public function getById($id, $table = null,$selectAux = null) {
        $select = "SELECT * "
                 . "FROM $this->table WHERE id = '$id'";
        $result = $this->query($select);

        if ($result->num_rows>0) {
            $set = $this->resultToArray($result);
            return $set[0];
        }
        return false;
    }
    
    public function delete($id, $table = null) {
        return parent::delete($id, $this->table);
    }

    public function isUsedInRecord($id, array $buscarEn = null,$andWhere= null) {
        return parent::isUsedInRecord($id, array('rewards_records'=>'reward_customer_id'));   
    }
    
    public function getListaSelectClientes(){
        $query = "SELECT id,name,last_name,phone "
                . "FROM $this->table "
                . "ORDER BY nombre ASC";
        $result = $this->query($query);
        
        if($result){
            $array = array();
            $tools = new Tools();
            while($row = $result->fetch_object()){
                $array[$row->id] = $row->name.' - '.$tools->applyMask($row->phone, 'phone');
            }            
            return $array;
        }
                
        return null;
    }
    
     public function getListaClientes(){
        $query = "SELECT * FROM $this->table ORDER BY name ASC";
        $result = $this->query($query);
        
        if($result){
            return $this->resultToArray($result);
        }
        
        return null;
    }
    
    public function addRewardsToCustomer($customer_id,$rewards){
        $rewards_due_date = null;        
        if(isset($rewards['rewards_due_date'])){$rewards_due_date = ",rewards_due_date = '{$rewards['rewards_due_date']}' ";}
        
        $query = "UPDATE $this->table "
                . "SET "
                . "rewards_total = (rewards_total + ".(float)$rewards['points']."),"
                . "rewards = (rewards + ".(float)$rewards['points']."), "
                . "acumulated_amount = round(acumulated_amount + ".(float)$rewards['amount'].",2)"
                . "$rewards_due_date "
                . "WHERE id = '$customer_id' ";
        
        $this->query($query);
    }
    
    public function descontarRewardsToCustomer($customer_id,$rewards){
        $query = "UPDATE $this->table "
                . "SET "
                . "rewards = (rewards - ".(float)$rewards['points']."), "
                . "acumulated_amount = round(acumulated_amount - ".(float)$rewards['amount'].",2) "
                . "WHERE id = '$customer_id' ";
        
        $this->query($query);
    }
    
    
    
    public function createFilterFecha($options,$campoFecha = null ){
        $fechaInicio = $options['fechaInicio'];
        $fechaFin = $options['fechaFin'];
        $fecha = null;
        $tools = new Tools();
        if($fechaInicio!=null){
            $fechaInicio = $tools->setFormatDateToDB($fechaInicio);
            if($fechaFin!=null){
                $fechaFin = $tools->setFormatDateToDB($fechaFin);
                $fecha .=" AND $campoFecha BETWEEN '{$fechaInicio}' AND '{$fechaFin}' ";
            }else{
                $fecha .=" AND $campoFecha BETWEEN '{$fechaInicio}' AND '{$fechaInicio}' ";
            }
        }elseif($fechaFin!=null){
            $fecha .=" AND $campoFecha BETWEEN '{$fechaFin}' AND '{$fechaFin}' ";
        }
        
        return $fecha;
    }
}