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

class Production_cash_balance extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('az_auth');
        az_check_auth('production_cash_balance');
        $this->load->helper('az_crud');
        $this->load->library('AZApp');
        $this->crud = $this->azapp->add_crud();
        $this->role_name = $this->session->userdata('role_name');
        
        // Hanya admin yang bisa akses
        if ($this->role_name != 'administrator') {
            show_404();
        }
    }

    public function index() {
        $azapp = $this->azapp;
        $data_header['title'] = 'Saldo Awal Produksi';
        $data_header['breadcrumb'] = array('production_cash', 'production_cash_balance');
        $azapp->set_data_header($data_header);

        // Get production users
        $this->db->where('idrole', 4);
        $this->db->where('status', 1);
        $data['production_users'] = $this->db->get('user');

        // Get current period
        $monday = date('Y-m-d', strtotime('monday this week'));
        $sunday = date('Y-m-d', strtotime('sunday this week'));
        $data['monday'] = $monday;
        $data['sunday'] = $sunday;
        $data['period_label'] = date('d-m-Y', strtotime($monday)) . ' s/d ' . date('d-m-Y', strtotime($sunday));

        // Get existing balances for this period
        $this->db->where('period_start', $monday);
        $this->db->where('status', 1);
        $data['balances'] = $this->db->get('production_cash_balance');

        $js = az_add_js('production_cash_balance/vjs_production_cash_balance');
        $azapp->add_js($js);

        $view = $this->load->view('production_cash_balance/v_production_cash_balance', $data, true);
        $azapp->add_content($view);

        echo $azapp->render();
    }

    function save_balance() {
        $err_code = 0;
        $err_message = '';

        $iduser = $this->input->post('iduser');
        $opening_balance = az_crud_number($this->input->post('opening_balance'));

        if (strlen($iduser) == 0) {
            $err_code++;
            $err_message = 'User wajib dipilih';
        }

        if ($err_code == 0) {
            $monday = date('Y-m-d', strtotime('monday this week'));
            $sunday = date('Y-m-d', strtotime('sunday this week'));

            // Cek apakah sudah ada
            $this->db->where('iduser', $iduser);
            $this->db->where('period_start', $monday);
            $this->db->where('status', 1);
            $check = $this->db->get('production_cash_balance');

            $data_save = array(
                'iduser' => $iduser,
                'period_start' => $monday,
                'period_end' => $sunday,
                'opening_balance' => $opening_balance,
                'updated' => date('Y-m-d H:i:s'),
                'updatedby' => $this->session->userdata('username'),
            );

            if ($check->num_rows() > 0) {
                $this->db->where('idproduction_cash_balance', $check->row()->idproduction_cash_balance);
                $this->db->update('production_cash_balance', $data_save);
            } else {
                $data_save['created'] = date('Y-m-d H:i:s');
                $data_save['createdby'] = $this->session->userdata('username');
                $this->db->insert('production_cash_balance', $data_save);
            }
        }

        echo json_encode(array(
            'err_code' => $err_code,
            'err_message' => $err_message,
        ));
    }
}