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

class Expense extends CI_Controller {
	protected $table;

	public function __construct() {
        parent::__construct();
        $this->table = "expense";
        $this->load->helper("array");
        $this->load->helper("az_core");
        az_check_login();
    }

	public function index(){
		$this->load->library('AZApp');
		$azapp = $this->azapp;

		$this->load->helper("az_azkasir");

		$crud = $azapp->add_crud();

		$column = array('#', 'Jenis', azlang('Date'), 'Keterangan', 'Jumlah', azlang('Action'));
		$crud->set_column($column);
		$crud->set_id($this->table);
		$crud->set_default_url(true);
		$crud->set_form('form');
		
		$datetime = $azapp->add_datetime();
		$datetime->set_id("expense_date");
		$datetime->set_name("expense_date");
		$datetime->add_class('x-hidden');
		$datetime->set_value(Date("d-m-Y H:i:s"));
		$data_modal['expense_date'] = $datetime->render();

		$v_modal = $this->load->view('expense/v_expense', $data_modal, true);
		$crud->set_modal($v_modal);
		$crud->set_modal_title('Pemasukan');

		$v_view = $crud->render();
		$v_modal = $crud->generate_modal();
		$v_view .= $v_modal;
		$azapp->add_content($v_view);

		$data_header['title'] = azlang('TRANSACTION');
		$data_header['subtitle'] = 'PENGELUARAN';
		
		$azapp->set_data_header($data_header);

		echo $azapp->render();
	}

	public function get() {
		$this->load->library("AZAppCRUD");
		$crud = $this->azappcrud;

    	$crud->set_select("idexpense, expense_type, expense_date, description, total");
    	$crud->set_select_align(",,,right");
    	$crud->set_select_number("3");
    	$crud->set_filter('description');
    	$crud->set_table($this->table);
    	$crud->set_sorting('expense_type, expense_date, description, total');
    	$crud->set_id($this->table);
    	$crud->set_order_by('expense_date desc');

		echo $crud->get_table();
	}


	public function edit() {
		$id = $this->input->post("id");
		$this->db->select("id".$this->table.", DATE_FORMAT(expense_date, '%d-%m-%Y %H:%i:%s') as expense_date, total, expense_type, description");
		$this->db->where("id".$this->table, $id);

		$rdata = $this->db->get($this->table)->result_array();
		echo json_encode($rdata);
	}

	public function save($type = ""){
		$data = array();
		$data["sMessage"] = "";
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('', '');

		$this->form_validation->set_rules('expense_date', azlang('Product Name'), 'required|trim');
		$this->form_validation->set_rules('total', azlang('Detail'), 'required|trim');
		$this->form_validation->set_rules('expense_type', "Jenis", 'required|trim');
		$this->form_validation->set_rules('description', azlang('Stock Date'), 'required|trim');

		$data_post = $this->input->post();
		$err_code = "";
		$err_message = "";

		$total_stock = azarr($data_post, "total");
		$total_stock = str_replace(".", "", $total_stock);

		
		if($this->form_validation->run() == FALSE){
			$err_code++;
			$err_message = validation_errors();
		}		

		if ($err_code == 0) {
			$idpost = $data_post['id'.$this->table];
			$this->load->helper('az_crud');

			$data_save = array(
				'iduser' => $this->session->userdata("iduser"),
				'expense_date' => Date('Y-m-d H:i:s', strtotime($this->input->post("expense_date"))),
				'total' => az_crud_number($this->input->post('total')),
				'description' => $this->input->post('description'),
				'expense_type' => $this->input->post('expense_type')
			);

			if($idpost == ""){
				$data_save["created"] = Date("Y-m-d H:i:s");
				$data_save["createdby"] = $this->session->userdata("username");
				if(!$this->db->insert($this->table, $data_save)){
					$err = $this->db->error();
					$err_code = $err["code"];
					$err_message = $err["message"];
				}
			}
			else {
				$this->db->where("id".$this->table, $idpost);
				if (!$this->db->update($this->table, $data_save)) {
					$err = $this->db->error();
					$err_code = $err["code"];
					$err_message = $err["message"];
				}
			}
		}

		$data["sMessage"] = $err_message;
		echo json_encode($data);
	}

	public function delete() {
		$id = $this->input->post("id");

		if (is_array($id)) {
			$this->db->where_in("id".$this->table, $id);
		}
		else {
			$this->db->where("id".$this->table, $id);
		}

		$this->db->delete($this->table);

		echo json_encode(array("SUCCESS"));
	}
}