<?php

class RewardsReedemRepository extends EntityRepository {
    
    private $table = 'rewards_reedem';
    private $options = array (
        'id_product' => null,
        'rewards_reedem'=>null,
        'start_date' => null,
        'end_date' => null,
        'status'=>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) {   
        $tools = new Tools();
        $data['start_date'] = $tools->setFormatDateTimeToDB($data['start_date']);
        $data['end_date'] = $tools->setFormatDateTimeToDB($data['end_date']);
        
        return parent::save($data, $this->table);
    }

    public function update($id, $data, $table = null) {
        $tools = new Tools();
        $data['start_date'] = $tools->setFormatDateTimeToDB($data['start_date']);
        $data['end_date'] = $tools->setFormatDateTimeToDB($data['end_date']);
        
        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 getListReedems(){
        $query = "SELECT "
                . "id_product,"
                . "rewards_reedem,"
                . "DATE_FORMAT(start_date,'%m/%d/%Y %h:%i %p')as formated_start_date,"
                . "DATE_FORMAT(end_date,'%m/%d/%Y %h:%i %p')as formated_end_date,"
                . "fxGetStatusName(status,'status','RewardsReedem')as status_name "
                . " FROM $this->table ORDER BY id ASC";
        $result = $this->query($query);
        
        if($result){
            $data = $this->resultToArray($result);
            
            $productsRepo = new PosProductoRepository();
            $productList = $productsRepo->getListaProductos();
            
            if($productList){
                $productArray = array();
                foreach($productList as $product){$productArray[$product['id']] = $product['descripcion'];}
                
                /*Obtener nombre de prodctos*/              
                foreach($data as $keyData => $row){
                    $productName = '';
                    $productForRule = explode(',', $row['id_product']);
                    foreach($productForRule as $key => $productId){
                        $productName .= $productArray[$productId];
                    }       
                
                    $data[$keyData]['product_name'] = $productName;
                }
            }         
            
            return $data;
        }
        
        return null;
    }
    
     public function getActiveReedem(){
        $query = "SELECT * FROM $this->table "
                . "WHERE status = 1 "
                . "AND NOW() BETWEEN start_date AND end_date ";
        
        $result = $this->query($query);
        
         $arrayReedem = array();
        if ($result->num_rows>0) {           
            $rules = $this->resultToArray($result);
            if($rules){
                foreach($rules as $rule){
                    if(strpos(trim($rule['id_product'],','), ',') ){
                        $productIds = explode(',', $rule['id_product']);                    
                        foreach($productIds as $productId){
                             $arrayReedem[$productId] = $rule['rewards_reedem'];
                        }
                    }else{
                        $arrayReedem[$rule['id_product']] = $rule['rewards_reedem'];
                    }                
                }
            }
        }
        
       return $arrayReedem;
    }
    
     public function getListStatus(){
        $query = "SELECT * FROM status_code WHERE operation = 'RewardsReedem'";
        $result = $this->query($query);
        
        if($result->num_rows > 0){
            $array = array();
            foreach($result as $status){
                $array[$status['code']] = $status['description'];
            }
            return $array;
        }
        return null;
    }
    
    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;
    }
}