<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class dailyLog extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $all_accounts = array();
        $dailyLog = $this->db->query("SELECT * FROM `dailyLog`")->result();
        foreach( $dailyLog as $log ){
            $id = $log->id;
            $Logitems = $this->db->query("SELECT * FROM `dailyLog_items` WHERE dailyLogId = '$id' ")->result();
            $log->sumCredit = 0;
            $log->sumDebit = 0;
            $log->sumTotal = 0;
            if(!empty($Logitems)){
                foreach( $Logitems as $item ){
                    if($item->total > 0 ){
                        $log->sumCredit += $item->total;
                    }
                    if($item->total < 0 ){
                        $log->sumDebit += $item->total;
                    }
                }
                $log->sumDebit = $log->sumDebit * -1;
                $log->sumTotal = $log->sumCredit - $log->sumDebit;
            }
        }

        $daily = reindex_by_key($this->db->query("SELECT * FROM `dailyLog` WHERE id = '$id'")->result(), 'id');
        foreach( $dailyLog as $log ){
            $id = $log->id;
            $Logitems = $this->db->query("SELECT * FROM `dailyLog_items` WHERE dailyLogId = '$id' ")->result();
            $log->sumCredit = 0;
            $log->sumDebit = 0;
            $log->sumTotal = 0;
            if(!empty($Logitems)){
                foreach( $Logitems as $item ){
                    if($item->total > 0 ){
                        $log->sumCredit += $item->total;
                    }
                    if($item->total < 0 ){
                        $log->sumDebit += $item->total;
                    }
                }
                $log->sumDebit = $log->sumDebit * -1;
                $log->sumTotal = $log->sumCredit - $log->sumDebit;
            }
        }
        
        $all_accounts['suppliers']  = $this->db->query("SELECT `supplier_id` as `id`, `name` FROM `suppliers`")->result();
        $all_accounts['users']      = $this->db->query("SELECT `userId` as `id`, CONCAT(`firstname`, ' ', `lastname` ) as name FROM `users`")->result();
        $all_accounts['accounts']   = $this->db->query("SELECT `id`, `name` FROM `accounts`")->result();
        
        $this->template
            ->set('view', 'admin/dailyLog/index')
            ->set('dailyLog', $dailyLog)
            ->set('daily', $daily)
            ->set('Logitems', $Logitems)
            ->set('all_accounts', $all_accounts)
            ->render();
    }

    public function createLog(){
        
        $id             = $this->input->post('id');
        $total             = $this->input->post('total');
        $note             = $this->input->post('note');
        $userId             = $this->input->post('userId');

        $data = array(
            // 'total'        => $total,
            // 'note'          => $note,
            'userId'        => $userId,
            'dateCreation'  => date('Y-m-d H:i:s'),
            'lastUpdate'    => date('Y-m-d H:i:s')
        );
        $log = $this->general_model->insertRow('dailyLog', $data);



        $data = $_POST;
         
        foreach( $_POST['total_credit'] as $key => $value){
            $dlog = array(
                'dailyLogId'    => $log,
                'total'         => !empty($_POST['total_credit'][$key]) ? $_POST['total_credit'][$key] : $_POST['total_debit'][$key] * -1,
                'notes'         => !empty($log['notes'][$key]) ? $log['notes'][$key] : '',
                'userId'        => getUserId(),
                'accountId'     => 0,
                'dateCreation'  => date('Y-m-d H:i:s'),
                'lastUpdate'    => date('Y-m-d H:i:s')
            );
            $log_item = $this->general_model->insertRow('dailyLog_items', $dlog);
        }
        redirect('admin/dailyLog/index', 'refresh');
    }
    
    public function updateLog(){
        $id = $this->input->post('logId');
        foreach( $_POST['total_credit'] as $key => $value){
            $check_item = $this->db->query("SELECT * FROM `dailyLog_items` WHERE `dailyLogId` = '$id' AND `id` = '$key'")->row();
            if(empty($check_item)){
                $dlog = array(
                    'dailyLogId'    => $id,
                    'total'         => !empty($_POST['total_credit'][$key]) ? $_POST['total_credit'][$key] : $_POST['total_debit'][$key] * -1,
                    'notes'         => !empty($log['notes'][$key]) ? $log['notes'][$key] : '',
                    'userId'        => getUserId(),
                    'accountId'     => 0,
                    'dateCreation'  => date('Y-m-d H:i:s'),
                    'lastUpdate'    => date('Y-m-d H:i:s')
                );
                $log_item = $this->general_model->insertRow('dailyLog_items', $dlog);
            }else{
                $dlog = array(
                    'total'         => !empty($_POST['total_credit'][$key]) ? $_POST['total_credit'][$key] : $_POST['total_debit'][$key] * -1,
                    'notes'         => !empty($log['notes'][$key]) ? $log['notes'][$key] : '',
                    'userId'        => getUserId(),
                    'accountId'     => 0,
                    'dateCreation'  => date('Y-m-d H:i:s'),
                    'lastUpdate'    => date('Y-m-d H:i:s')
                );
                $log_item = $this->general_model->updateRowById('dailyLog_items', 'id', $check_item->id, $dlog);
            }
        }
        redirect('admin/dailyLog/index', 'refresh');
    }
    
    public function getPaymentsData(){
        $daily_log_id = $this->input->post('id');
        if(!empty($daily_log_id) && $daily_log_id > 0){
            $payments = $this->db->query("SELECT `id`, `accountId`, `total`, `notes`, `dateCreation` FROM `dailyLog_items` WHERE `dailyLogId` = '$daily_log_id'")->result();
            foreach( $payments as $payment ){
                $payment->dateCreation = date('d/m/Y', strtotime($payment->dateCreation));
            }
            if(!empty($payments)){
                echo json_encode(array("result" => true, "data" => $payments));
            }else{
                echo json_encode(array("result" => false));
            }
        }else{
            echo json_encode(array("result" => false));
        }
    }
    
}















