<?php
class StoreDetailsTempRepository extends EntityRepository {

    private $table = 'store_details_';
    
    public function __construct(){
        $login = new Login();
        $this->table = $this->table.$login->getId();
    }
    // Guarda en temporal
    public function save(array $data, $table = null) {
        if(isset($data['idDetailTemp']) && trim($data['idDetailTemp'])!= ''){
            $result = $this->getById($data['idDetailTemp']);
            if($result){
                $id = $data['idDetailTemp'];
                unset($data['idDetailTemp']);    
                return parent::update($id, $data,$this->table);
            }
         }
        
        unset($data['idDetailTemp']); 
        /*
        $query = "SELECT id,min,max FROM ".$this->table.
                " WHERE id_product = '".$data['id_product']."'"
               . "AND days = '{$data['days']}'";
        
        $result = $this->query($query);      
         
        if($result->num_rows > 0){
            $result = $result->fetch_object();
            $data['min'] = $data['min'] + $result->min;
            $data['min'] = $data['min'] + $result->min;
            $id = $result->id;
            return parent::update($id, $data,$this->table);
        }else{*/
            return parent::save($data, $this->table);
        #}
    }
    
    // Guarda en tabla comprasdetalles
    public function saveDetalles($idCompra, $tokenForm){
        $query = "SELECT * FROM $this->table WHERE token_form = '$tokenForm'";
        $result = $this->query($query);
        if($result->num_rows > 0){
           
            $result = $this->resultToArray($result);
            
            foreach($result as $data){
                unset($data['id'],$data['id_detail'],$data['token_form']); 
                $data['id_store'] = $idCompra;
                
                parent::save($data, 'store_details');
            }            
        }

        return true;
    } 
    
    public function updateDetalles($idStore, $tokenForm){
        $query = "SELECT id FROM store_details WHERE id_store = '$idStore'";
        $result = $this->query($query);
        $detallesOrigin = null;    
        if($result->num_rows > 0){
            $detallesOrigin = $this->resultToArray($result);
        }
        
        $query = "SELECT * FROM $this->table WHERE token_form = '$tokenForm'";
        $result = $this->query($query);
        
        if($result->num_rows > 0){
            $idsDetalles = array();
        
            $result = $this->resultToArray($result);
            foreach($result as $data){
                $idDetalle = $data['id_detail'];
                unset($data['id'],$data['id_detail'],$data['token_form']);
                $data['id_store'] = $idStore;
              
                if(!$idDetalle){
                    parent::save($data, 'store_details');
                }else{
                    #contiene los idDetalles de la requisicion original que se mantendran, lo que no esten aqui se eliminaran.
                    $idsDetalles[] = $idDetalle;
                    parent::update($idDetalle,$data, 'store_details');         
                }  
            }
        }

        #Se eliminan ids de tabla requisition_details que ya no estan en $idsDetalles (este array contiene los ids que permanececieron en la requisicion despues del update)                
        if($detallesOrigin){
            foreach ($detallesOrigin as $detalle){
                if(!in_array($detalle['id'], $idsDetalles)){
                    parent::delete($detalle['id'], 'store_details');
                }
            }
        }  
        return true;
    }
    
    public function delete($id, $table = null) {
        return parent::delete($id, $this->table);
    }
    
    public function truncate($tokenForm){
        $query = "DELETE FROM ".$this->table." WHERE token_form = '$tokenForm'";
        $result = $this->query($query);
        
        if($result){
            return true;
        }
        
        return null;
    }
    
    public function setStoreDetailsById($idStore, $tokenForm){
        $query = "INSERT INTO ".$this->table." (id_detail,token_form,id_store,id_product,days,min,max)
                    SELECT id,'$tokenForm',id_store,id_product,days,min,max
                    FROM store_details WHERE id_store = '$idStore'";
        
        $result = $this->query($query);
        if($result){
            return true;
        }
        
        return null;
    }

    public function update($id, $data, $table = null) {
        if(isset($data['id_store'])){
            if($data['id_store']=='' || !$data['id_store']){
                unset($data['id_store']);
            }
        }

        return parent::update($id, $data, $this->table);
    }

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