<?php
class ProductsController extends AppController {

	var $name = 'Products';
	var $uses = array('Product', 'ProductGroup', 'TaxGroup');

    var $components = array(
	    'Filter' => array(
	        'index' => array(
	            'product_group_id' => array(
        	        'form_options' => array('label' => 'ProductGroup', 'options' => 'ProductGroups'),
	            	'filter_options' => array(),
	            ),
				'name' => array(
        	        'form_options' => array('label' => 'Name'),
	            	'filter_options' => array('fields' => array('Product.name'), 'operator' => 'like^'),
				),
	        )
        ),
	);

	function  beforeFilter() {
		parent::beforeFilter();
		$this->Auth->mapActions(array(
			'group_add' => 'create',
			'group_edit' => 'update',
			'group_delete' => 'delete',
			'tax_group_add' => 'create',
			'tax_group_edit' => 'update',
			'tax_group_delete' => 'delete',
		));
	}

	function index() {
		if (!empty($this->filterParams['product_group_id'])) {
			$this->set('productGroup', $this->ProductGroup->read(null, $this->filterParams['product_group_id']));
		}

		$this->Product->recursive = 0;
		$this->set('items', $this->paginate());
	}

	function view($id = null) {
		$this->Product->contain('ProductGroup', 'TaxGroup', 'ProductItem.Article','Printer','KeubGroup');
		if (!$id  || !($item = $this->Product->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Product', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('item', $item);
	}

	function add() {
		if (!empty($this->data)) {
			$this->Product->create();

			//XTR: should be set in model, but we don't have currency info there
			$xRate = $this->get_item('select value from settings where name = "general.euro_exchange_rate" limit 1 ');
			$this->data['Product']['price_currency'] = $this->data['Product']['price'] * $xRate;

			if ($this->Product->saveAll($this->data)) {
				$this->Session->setFlash(__('The product has been saved', true), 'default', array('class' => 'success'));
				
				if(isset($_POST['return-hash'])){
					$l_add = intval(str_replace('#back-', '', $_POST['return-hash']));
					if($l_add > 0){
						$l_add = '/page:'.$l_add;
					}else{
						$l_add = '';
					}
				}else{
					$l_add = '';
				}
				
				$this->redirect(array('action' => 'index'.$l_add));
			} else {
				$this->Session->setFlash(__('The product could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$productGroups = $this->Product->ProductGroup->find('list');
		$taxGroups = $this->Product->TaxGroup->find('list');
		$noteGroups = $this->Product->NoteGroup->find('list');
		$extraGroups = $this->Product->ExtraGroup->find('list');
		$printers = $this->Product->Printer->find('list');
		$keubGroups = $this->Product->KeubGroup->find('list');
		$articles = $this->Product->ProductItem->Article->find('list', array('order' => array('name' => 'asc'))); //getGrouped(null, 'id', 'display');

		$this->set(compact('productGroups', 'taxGroups', 'noteGroups', 'extraGroups', 'articles','printers','keubGroups'));
	}

	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Product->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Product', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
//			$this->Product->data = null;	// unset model data so modified field can be updated automagically

			//XTR: should be set in model, but we don't have currency info there
			$xRate = $this->get_item('select value from settings where name = "general.euro_exchange_rate" limit 1 ');
			$this->data['Product']['price_currency'] = $this->data['Product']['price'] * $xRate;

			if ($this->Product->saveAll($this->data)) {
				$this->Session->setFlash(__('The product has been saved', true), 'default', array('class' => 'success'));
				
				if(isset($_POST['return-hash'])){
					$l_add = intval(str_replace('#back-', '', $_POST['return-hash']));
					if($l_add > 0){
						$l_add = '/page:'.$l_add;
					}else{
						$l_add = '';
					}
				}else{
					$l_add = '';
				}
				
				$this->redirect(array('action' => 'index'.$l_add));
			} else {
				$this->Session->setFlash(__('The product could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
		$productGroups = $this->Product->ProductGroup->find('list');
		$taxGroups = $this->Product->TaxGroup->find('list');
		$noteGroups = $this->Product->NoteGroup->find('list');
		$extraGroups = $this->Product->ExtraGroup->find('list');
		$printers = $this->Product->Printer->find('list');
		$keubGroups = $this->Product->KeubGroup->find('list');
		
		$articles = $this->Product->ProductItem->Article->find('list', array('order' => array('name' => 'asc'))); //getGrouped(null, 'id', 'display');

		$this->set(compact('productGroups', 'taxGroups', 'noteGroups', 'extraGroups', 'articles','printers','keubGroups'));
	}

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


// PRODUCT GROUPS
	function group_add() {
		if (!$this->me['is_super_admin']) {
			$this->cakeError('error404');
		}

		if (!empty($this->data)) {
			$this->ProductGroup->create();
			if ($this->ProductGroup->save($this->data)) {
				$this->Session->setFlash(__('The product group has been saved', true), 'default', array('class' => 'success'));
                if ($this->RequestHandler->isAjax()) {
					$this->nyro = array(
						'close' => true, 'reload' => false, 'hide_content' => true,
						'js_callback' => 'after_add_product_group',
						'js_callback_param' => $this->ProductGroup->read(null, $this->ProductGroup->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The product group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}

	function group_edit($id = null) {
		if (!$this->me['is_super_admin']) {
			$this->cakeError('error404');
		}

		if ((!$id && empty($this->data)) || ($id && !($item = $this->ProductGroup->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Product Group', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->ProductGroup->data = null;	// unset model data so modified field can be updated automagically
			if ($this->ProductGroup->save($this->data)) {
				$this->Session->setFlash(__('The product group has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The product group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
	}

	function group_delete($id = null) {
		if (!$this->me['is_super_admin']) {
			$this->cakeError('error404');
		}

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

	function _faProductGroups() {
		return $this->ProductGroup->find('list');
	}


// TAX GROUPS
	function tax_group_add() {
		if (!$this->me['is_super_admin']) {
			$this->cakeError('error404');
		}

		if (!empty($this->data)) {
			$this->TaxGroup->create();
			if ($this->TaxGroup->save($this->data)) {
				$this->Session->setFlash(__('The tax group has been saved', true), 'default', array('class' => 'success'));
                if ($this->RequestHandler->isAjax()) {
					$this->nyro = array(
						'close' => true, 'reload' => false, 'hide_content' => true,
						'js_callback' => 'after_add_tax_group',
						'js_callback_param' => $this->TaxGroup->read(null, $this->TaxGroup->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The tax group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}

	function tax_group_edit($id = null) {
		if (!$this->me['is_super_admin']) {
			$this->cakeError('error404');
		}

		if ((!$id && empty($this->data)) || ($id && !($item = $this->TaxGroup->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Tax Group', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->TaxGroup->data = null;	// unset model data so modified field can be updated automagically
			if ($this->TaxGroup->save($this->data)) {
				$this->Session->setFlash(__('The tax group has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tax group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
	}

	function tax_group_delete($id = null) {
		if (!$this->me['is_super_admin']) {
			$this->cakeError('error404');
		}

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

	function _faTaxGroups() {
		return $this->TaxGroup->find('list');
	}

}
?>