<?php
class InventoryArticlesController extends AppController {

	var $name = 'Inventory_articles';
    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->InventoryArticle->recursive = 0;
		$this->set('items', $this->paginate());
	}

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

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