<?php namespace App\Models;

use CodeIgniter\Model;

class mBillDetails extends Model {
    protected $table = 'budget_billdetails';
    protected $primaryKey = 'id';

    protected $useAutoIncrement = true;

    protected $returnType = 'object';
    protected $useSoftDeletes = false;

    protected $allowedFields = ['bill_id','comment','account_id','subamount','lkz'];

    protected $useTimestamps = false;

//     public function getEntries($id){
//         $this->select('mw_budget_bookingdetails.*,mw_budget_categories.description');
//         $this->join('mw_budget_categories', 'mw_budget_categories.id = mw_budget_bookingdetails.account_id', 'left');
//         $result = $this->orderBy('id','asc')->where('bill_id',$id)->findAll();
//         foreach ($result as $key => $value) {
            
// //            $value->datum =  date( "d.m.Y", strtotime( $value->datum ));
//     //            $value->amount = number_format( $value->amount , 2, ',', '.' );
//         }
//         return $result;
//     }

    public function getDetails($ids){
        $quelle = model('App\Models\mAccounts');
        $rows = $this->whereIn('bill_id', $ids)->findAll();
        $result = array();
        foreach($rows as $row){
            $row->description = $quelle->getDropDownEntry($row->account_id);
            $result[$row->bill_id][] = $row;
        }
        return $result;
    }
    public function getEntriesForForm($billId, &$result, $accounts){
        $this->select('budget_billdetails.*,budget_accounts.description');
        $this->join('budget_accounts', 'budget_accounts.id = budget_billdetails.account_id', 'left');
        $data = $this->orderBy('id','asc')->where('bill_id',$billId)->findAll();
        $amount = 0.0;
        foreach ($data as $key => $row){
            $result['sid'][$key]= $row->id;
            $result['category'][$key]= $row->account_id;
            $result['category_parent'][$key]= $accounts->getParent($result['category'][$key]);
            $result['input'][$key]= ($row->subamount >= 0.0)?number_format( abs( $row->subamount ), 2, ',', '' ):'';
            $result['output'][$key]= ($row->subamount < 0.0)?number_format( abs( $row->subamount ), 2, ',', '' ):'';
            $result['comment'][$key]= $row->comment;
            $amount += $row->subamount;
//            $value->datum =  date( "d.m.Y", strtotime( $value->datum ));
//            $value->amount = number_format( $value->amount , 2, ',', '.' );
        }
        if (count($data) > 1){
            $result['multi'] = 'on';
            $result['total_in']= ($amount >= 0.0)?number_format( abs( $amount ), 2, ',', '' ):'';
            $result['total_out']= ($amount < 0.0)?number_format( abs( $amount ), 2, ',', '' ):'';
        }
        else if ($accounts->checkType($result['category'][0],'account'))
            $result['transfer'] = true;
        return $result;
    }

    public function saveEntries($data, $billId){
        for ($i=0;$i<10;$i++){
            if ($data['category'][$i] > 0){
                $this->set('subamount', floatval($data['input'][$i])-floatval($data['output'][$i])); //floatval($data['total_in'])-floatval($data['total_out']));
                $this->set('comment', $data['comment'][$i]);
                $this->set('account_id', $data['category'][$i]);
                $this->set('bill_id', $billId); 
                if ( 0 == $data['sid'][$i] ){
                    $this->insert();
                }
                else{
                    $this->update($data['sid'][$i]);
                }
            }
            else if (0 < $data['sid'][$i]) {
                $this->delete(['id' => $data['sid'][$i]]);
            }
        }
     }
};
?>