<?php
class Gizi extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('dashboard_model');
        $this->load->model('live_model');
        $this->load->helper('content');

        if ($this->user_model->is_logged_in() == FALSE)
            redirect(base_url(), 'refresh');
    }

/* persediaan */
    function persediaan() {
        $view['content'] = 'chart/gizi_persediaan';
        $view['data'] = $this->live_model->persediaan_list()->result();
        $this->load->view('admin/index', $view);
    }

    function persediaan_input() {
        $this->db->where('Bulan', date('n'));
        $this->db->where('Tahun', date('Y'));
        $this->db->delete('gizi_persediaan');
        
        $data = $this->dashboard_model->persediaan_input();
        //print_r($data);

        foreach ($data as $row) {
            $this->db->set('Bulan', $row->Bulan);
            $this->db->set('Tahun', $row->Tahun);
            $this->db->set('kelompok', $row->kelompok);
            $this->db->set('SaldoAwal', $row->SaldoAwal); 
            $this->db->set('MutasiMasuk', $row->MutasiMasuk);
            $this->db->set('MutasiKeluar', $row->MutasiKeluar);
            $this->db->set('Adjustment', $row->Adjustment);
            $this->db->set('SaldoAkhir', $row->SaldoAkhir);
            $this->db->insert('gizi_persediaan');
        }

    }

/* penerimaan */
    function penerimaan() {
        $view['content'] = 'chart/gizi_penerimaan';
        $view['data'] = $this->live_model->penerimaan_list()->result();
        $this->load->view('admin/index', $view);
    }

    function penerimaan_input() {
        $this->db->where('Bulan', date('n'));
        $this->db->where('Tahun', date('Y'));
        $this->db->delete('gizi_penerimaan');
        
        $data = $this->dashboard_model->penerimaan_input();
        //print_r($data);
        foreach ($data as $row) {
            $this->db->set('bulan', $row->bulan);
            $this->db->set('tahun', $row->tahun);
            $this->db->set('kodekelompok', $row->kodekelompok);
            $this->db->set('namakelompok', $row->namakelompok); 
            $this->db->set('QtyPenerimaan', $row->QtyPenerimaan);
            $this->db->set('TotalHargaBeli', $row->TotalHargaBeli);
            $this->db->set('TotalPenerimaan', $row->TotalPenerimaan);
            $this->db->insert('gizi_penerimaan');
        }
    }

/* distribusi */   
    function distribusi() {
        $view['content'] = 'chart/gizi_distribusi';
        $view['data'] = $this->live_model->distribusi_list()->result();
        $this->load->view('admin/index', $view);
    }

    function distribusi_input() {
        $this->db->where('Bulan', date('n'));
        $this->db->where('Tahun', date('Y'));
        $this->db->delete('gizi_distribusi');
        
        $data = $this->dashboard_model->distribusi_input();
        //print_r($data);
        foreach ($data as $row) {
            $this->db->set('bulan', $row->bulan);
            $this->db->set('tahun', $row->tahun);
            $this->db->set('kodekelompok', $row->kodekelompok);
            $this->db->set('namakelompok', $row->namakelompok); 
            $this->db->set('QtyDistribusi', $row->QtyDistribusi);
            $this->db->set('TotalDistribusi', $row->TotalDistribusi);
            $this->db->insert('gizi_distribusi');
        }
    }
}