<?php
class  SettingsRepository extends EntityRepository {

    private $table = 'settings';
    private $options = array();
    
    public function __construct(){
        $this->setOptions($this->getListSettings());
    }
    
    public function setOptions($options){
        $this->options = $options;
    }
    
    public function _get($setting){
        if(isset($this->options[$setting])){
            return $this->options[$setting];
        }
        return null;
    }
    
    public function _getDefaultLocation(){        
        return $this->_get('deafult_location_when_receiving');
    }
    
    public function _getNexPurchaseId(){
        $lastPurchseId = $this->_get('last_purchase_id');

        $lastPurchseId++;
        return $lastPurchseId;      
    }
    
    public function setLastPurchaseId($lasPurchaseId){
        $this->_setSettings('last_purchase_id',$lasPurchaseId);
    }
    
    public function _getNextInvoiceNumber(){
        $lastInvoiceId = $this->_get('last_invoice_id');

        $lastInvoiceId++;
        return $lastInvoiceId;   
    }
    
    public function setLastInvoiceId($lasPurchaseId){
        $this->_setSettings('last_invoice_id',$lasPurchaseId);
    }

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

    public function update($id, $data, $table = null) {
    }

    public function getById($id, $table = null,$selectAux = null) {
        return parent::getById($id, $this->table,$selectAux);
    }
    
    public function _setSettings($setting,$value){
        $query = "UPDATE $this->table SET value = '$value' WHERE `key` = '$setting' ";
        $this->query($query);
    }
    
    public function getListSettings(){
        $query = "SELECT * FROM settings";
        $result = $this->query($query);
        
        if($result->num_rows > 0){
            $result = $this->resultToArray($result);
            $settings = array();
            foreach($result as $row){
                $settings[$row['key']] = $row['value'];
            }
            
            return $settings;
        }
    }
}