<?php
class InventoryBooksController extends AppController {


	var $name = 'Inventory_books';
	var $components = array(
	    'Filter' => array(
	        /*'index' => array(
	            'supplier_id' => array(
        	        'form_options' => array('label' => 'Supplier', 'options' => 'Suppliers'),
	            	'filter_options' => array(),
	            ),
				'canceled' => array(
        	        'form_options' => array('label' => 'Status', 'options' => 'Filter.ReceivingOrder_cancel'),
	            	'filter_options' => array(),
				),*/
//				'date' => array(
//        	        'form_options' => array('label' => 'Date', 'type' => 'date'),
//	            	'filter_options' => array('fields' => array('DATE(date)')),
//				)
	        
        ),
	);

	function  beforeFilter() {
		parent::beforeFilter();
		$this->Auth->mapActions(array('cancel' => 'delete'));
	}

	function index() {
		$this->InventoryBook->recursive = 0;
		$this->set('items', $this->paginate());
	}

	function view($id = null) {
		//$this->InventoryArticle->contain('Supplier', 'ReceivingOrderItem.Article');
		if (!$id  || !($item = $this->InventoryBook->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Inventory Book', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		} 
		$this->set('item', $item);
	}
	
	function add() {
		if (!empty($this->data)) {
		    $this->InventoryBook->create();
			if ($this->InventoryBook->save($this->data)) {
				$this->Session->setFlash(__('The Inventory Book has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Inventory Book could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}
	
	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->InventoryBook->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Inventory book', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['InventoryBook']['id'] = $id;
			$this->InventoryBook->data = null;	// unset model data so modified field can be updated automagically	
			if ($this->InventoryBook->saveall($this->data)) {
				$this->Session->setFlash(__('The inventory book has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The inventory book could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
	}
	
	function delete($id = null) {

	    if (!$id || !$this->InventoryBook->read(null, $id)) {
			$this->Session->setFlash(__('Invalid Inventory Book', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->InventoryBook->delete($id)) {
			$this->Session->setFlash(__('Inventory Book deleted', true), 'default', array('class' => 'success'));
		} else {
			$this->Session->setFlash(__('Inventory Book delete failed. Please, try again.', true), 'default', array('class' => 'error'));
		}

		$this->redirect(array('action' => 'index'));
	}

	

}
