<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Statue_books extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('statue_book_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->statue_book_model->list_all();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $actions = $this->actions();
        $data['add_btn']    = 'Create New Statue&Books';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $service_menu_id = get_statue_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '5');
        $this->render($data, 'statue_books/statue_book/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['categories'] = $this->statue_book_model->category_list();
        $data['items'] = $this->statue_book_model->item_list();
        $data['vendors'] = $this->statue_book_model->vendor_list();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'statue_books/statue_book/add');
    }
    
    public function insert() {
        
        $statues = $this->statue_book_model->get_last_code($this->input->post('cat_id'));
        $cat_name = $this->statue_book_model->get_cat_name($this->input->post('cat_id'))->name;
        
        if($statues->num_rows() > 0) {
            $last_code = $statues->row()->code;
            $num = preg_replace("/[^0-9,.]/", "", $last_code );
            $last_no = (int)$num; 
            $code = (($last_no + 1)  > 9999) ? ($last_no + 1)  : sprintf('0%03d', ($last_no + 1) );
        } else
            $code = '0001';
        
        $item_code = $cat_name[0] . $code;
        $insert = array(
            'code' => $item_code,
            'cat_id' => $this->input->post('cat_id'),
            'item_id' => $this->input->post('item_id'),
            'invoice_no' => $this->input->post('inv_no'),
            'invoice_date' => date('Y-m-d',strtotime($this->input->post('inv_date'))),
            'vendor_id' => $this->input->post('vendor_id'),
            'ship_advice_no' => $this->input->post('shipping_ad_no'),
            'received_date' => date('Y-m-d',strtotime($this->input->post('rec_date'))),
            'box_no' => $this->input->post('box_no'),
            'quantity' => $this->input->post('qty'),
            'description' => $this->input->post('desc'),
            'cost_per_item' => $this->input->post('cost_per_item'),
            'total_amount' => $this->input->post('total_amt'),           
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->statue_book_model->insert($insert);  
        if ($insert_id > 0) {
            $this->session->set_flashdata('success', 'Statue and Books details added successfully');
            
            $sb_item = $this->statue_book_model->get_sb_item_rows($this->input->post('item_id'));
            if($sb_item->num_rows() > 0) {
                $results = $sb_item->row();
                $update = array(
                    'total_quantity' => $results->total_quantity + $this->input->post('qty'),
                    'updated_by' => $this->user_data['user_id'],
                    'updated_on' => date('Y-m-d H:i:s')
                );
                $this->statue_book_model->update_sb_item($update,$results->id);
            } else {
                $insert = array (
                    'item_id' => $this->input->post('item_id'),
                    'total_quantity' => $this->input->post('qty'),
                    'created_by' => $this->user_data['user_id'],
                    'created_on' => date('Y-m-d H:i:s')
                );
                $this->statue_book_model->insert_sb_item($insert);
            }
        }
        else
            $this->session->set_flashdata('error', 'Statue and Books details not added!');
        redirect('statue_books', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['categories'] = $this->statue_book_model->category_list();
        $data['items'] = $this->statue_book_model->item_list();
        $data['vendors'] = $this->statue_book_model->vendor_list();
        $data['statue'] = $this->statue_book_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'statue_books/statue_book/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $statue = $this->statue_book_model->get_by_id($id);
        
        $update = array(
            'cat_id' => $this->input->post('cat_id'),
            'item_id' => $this->input->post('item_id'),
            'invoice_no' => $this->input->post('inv_no'),
            'invoice_date' => date('Y-m-d',strtotime($this->input->post('inv_date'))),
            'vendor_id' => $this->input->post('vendor_id'),
            'ship_advice_no' => $this->input->post('shipping_ad_no'),
            'received_date' => date('Y-m-d',strtotime($this->input->post('rec_date'))),
            'box_no' => $this->input->post('box_no'),
            'quantity' => $this->input->post('qty'),
            'description' => $this->input->post('desc'),
            'cost_per_item' => $this->input->post('cost_per_item'),
            'total_amount' => $this->input->post('total_amt'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ( $this->statue_book_model->update($update, $id) > 0) {
            $this->session->set_flashdata('success', 'Statue and Books details updated successfully');
            
            $diff_qty = $this->input->post('qty') - $statue->quantity;
            
            $sb_item = $this->statue_book_model->get_sb_item_rows($this->input->post('item_id'));
            if($sb_item->num_rows() > 0) {
                $results = $sb_item->row();
                $update = array(
                    'total_quantity' => $results->total_quantity + $diff_qty,
                    'updated_by' => $this->user_data['user_id'],
                    'updated_on' => date('Y-m-d H:i:s')
                );
                $this->statue_book_model->update_sb_item($update,$results->id);
            } else {
                $insert = array (
                    'item_id' => $this->input->post('item_id'),
                    'total_quantity' => $this->input->post('qty'),
                    'created_by' => $this->user_data['user_id'],
                    'created_on' => date('Y-m-d H:i:s')
                );
                $this->statue_book_model->insert_sb_item($insert);
            }
           
        }
        else
            $this->session->set_flashdata('error', 'Statue and Books details not updated!');    
        
        redirect('statue_books','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        $data = array();
        $data['statue'] = $this->statue_book_model->get_by_id($id);
        $this->render($data, 'statue_books/statue_book/view');
    }
    
    public function check_valid_qty() {
        $data = $this->statue_book_model->check_valid_qty($_POST['item_id']);
        echo json_encode($data);
    }
    
     public function delete($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        $statue = $this->statue_book_model->get_by_id($id);
        $sb_item = $this->statue_book_model->get_sb_item_rows($statue->item_id)->row();
        $update = array(
            'total_quantity' => $sb_item->total_quantity - $statue->quantity,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        $this->statue_book_model->update_sb_item($update,$sb_item->id);
        
        $update = array ( 
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
            
        if($this->statue_book_model->update($update,$id)> 0) {
            $this->session->set_flashdata('success', 'Statue and Books details deleted successfully');
        } else 
            $this->session->set_flashdata('error', 'Statue and Books details not deleted!');
        
        redirect( 'statue_books', 'refresh');
    }
    
    public function get_item_by_cat_id() {
        $data['items'] = $this->statue_book_model->get_item_by_cat_id($_POST['cat_id']);
        echo json_encode($data);
    }
}
?>