<?php
class ExpenseConfigRepository extends EntityRepository {

    private $table = 'expenses_fixed_config';
    private $options = array(
       
    );
    
    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 _saveConfig(array $data) {
        $query = "SELECT id,CONCAT(store_id,'_',expense_fixed_id)as `key` FROM $this->table ";
        $result = $this->query($query);
        
        $currentData = array();
        if($result->num_rows > 0){
            $result = $this->resultToArray($result);            
            
            foreach($result as $row){
                $currentData[$row['key']] = $row['id'];
            }         
        }
        
        foreach($data as $key => $config ){
            $newConfig = array('cost'=>$config['cost'],'due_date'=>$config['duedate']);
            if(key_exists($key, $currentData)){
                $id = $currentData[$key];
                $this->update($id,$newConfig);
            }else{
                $key = explode('_', $key);
                $newConfig['store_id'] = $key[0];
                $newConfig['expense_fixed_id'] = $key[1];
                $this->save($newConfig);
            }
        }
        
        return true;
    }    
    
    public function _saveFixedExpense(array $data,$year,$month) {
        $query = "SELECT id,CONCAT(store_id,'_',expense_fixed_id)as `key` FROM expenses_fixed_history ";
        $result = $this->query($query);
        
        $currentData = array();
        if($result->num_rows > 0){
            $result = $this->resultToArray($result);            
            
            foreach($result as $row){
                $currentData[$row['key']] = $row['id'];
            }         
        }
        
        foreach($data as $key => $config ){
            $newConfig = array(
                'year' =>$year,
                'month' =>$month,
                'cost' => $config['cost'] ?? 0,
                'due_date' => ($config['duedate'] ?? '') !== '' ? $config['duedate'] : 0
            );

            if(key_exists($key, $currentData)){
                $id = $currentData[$key];
                parent::update($id,$newConfig,'expenses_fixed_history');
            }else{
                $key = explode('_', $key);
                $newConfig['store_id'] = $key[0];
                $newConfig['expense_fixed_id'] = $key[1];               
                parent::save($newConfig,'expenses_fixed_history');
            }
        }
        
        return true;
    }
    
    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) {        
        return parent::update($id, $data, $this->table);
    }

    public function getById($id, $table = null,$selectAux = null) {
        return parent::getById($id, $this->table,$selectAux);
    }
    
    public function getFixedExpensesList(){
        $categoryRepo = new CategoryRepository(array());
        $fixedExpenseList = $categoryRepo->getListSelectCategories('fixed expense','1');
        
        $storeRepository = new StoreRepository();
        $storeList = $storeRepository->getListSelectStores(null,'1');
        
        $query = "SELECT * FROM $this->table ";
        $result = $this->query($query);
        
        $data = array();
        if($result->num_rows > 0){
            $result = $this->resultToArray($result);            
            
            foreach($result as $row){
                $data[$row['store_id']][$row['expense_fixed_id']] = array('cost'=>$row['cost'],'due_date'=>$row['due_date']);
            }           
        }
        
        return array(
            'fixed_expense_list'=>$fixedExpenseList,
            'store_list'=>$storeList,
            'data_config'=>$data
        );      
    }
}