<?php
class ArticlesController extends AppController {

	var $name = 'Articles';
	//var $uses = array('Article', 'TaxGroup', 'PrikazGroup', 'ArticleGroup');
	var $uses = array('Article', 'TaxGroup', 'PrikazGroup', 'ArticleGroup');

    var $components = array(
	    'Filter' => array(
	        'index' => array(
	            'tax_group_id' => array(
        	        'form_options' => array('label' => 'TaxGroup', 'options' => 'TaxGroups'),
	            	'filter_options' => array(),
	            ),
				'name' => array(
        	        'form_options' => array('label' => 'Name'),
	            	'filter_options' => array('fields' => array('Article.name'), 'operator' => 'like^'),
				),
	        )
        ),
	);


	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('group_add','group_index');
	}

	function index() {
		if (!empty($this->filterParams['tax_group_id'])) {
			$this->set('taxGroup', $this->TaxGroup->read(null, $this->filterParams['tax_group_id']));
		}

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

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

	
	
	
	function group_add(){
		if (!empty($this->data)) {
			
			//print_r($this->ArticleGroup);
			//exit;
			
			$this->ArticleGroup->create();
			if ($this->ArticleGroup->save($this->data)) {
				$this->Session->setFlash(__('The Articles Group has been saved', true), 'default', array('class' => 'success'));
                if ($this->RequestHandler->isAjax()) {
					$this->nyro = array(
						'close' => true, 'reload' => false, 'hide_content' => true,
					);
				} else {
					$this->redirect(array('action'=>'group_index'));
				}
			} else {
				$this->Session->setFlash(__('The Article Group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}
	
	function group_index(){
		$x = $this->get_data('select * from article_groups order by id ', 1);
		
		$y = array();
		for($i = 0; $i < sizeof($x['id']); $i++){
			$z = array();
			foreach($x as $key => $value){
				$z[$key] = $value[$i];
			}
			$y[$i] = array('ArticleGroup' => $z);
		}
		
		$this->set('items', $y);
		//$this->set('items', $this->paginate());
	}
	
	
	
	
	
	function add() {
		if (!empty($this->data)) {
			$this->Article->create();
			$this->data['Article']['price_sell_currency'] = round($this->data['Article']['price_sell'] * 7.5345, 2);
			if ($this->Article->save($this->data, true, array('tax_group_id', 'name', 'unit', 'isService', 'display', 'barcode', 'price_sell', 'price_sell_currency', 'price_buy', 'feedback_compensation', 'dozvoljeni_kalo', 'keub', 'keubarticle_group_id', 'prikaz_group_id', 'article_group_id', 'is_visible'))) {
				$this->Session->setFlash(__('The article 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_article',
						'js_callback_param' => $this->Article->read(null, $this->Article->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$taxGroups = $this->Article->TaxGroup->find('list');
		$keubarticleGroups = $this->Article->KeubarticleGroup->find('list');
		$prikazGroups = $this->Article->PrikazGroup->find('list');
		$articleGroups = $this->Article->ArticleGroup->find('list');
		
		//$this->set(compact('taxGroups','keubarticleGroups', 'prikazGroups'));
		$this->set(compact('taxGroups','keubarticleGroups', 'prikazGroups', 'articleGroups'));
	}

	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Article->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Article', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->Article->data = null;	// unset model data so modified field can be updated automagically
			$this->data['Article']['price_sell_currency'] = round($this->data['Article']['price_sell'] * 7.5345, 2);
			if ($this->Article->save($this->data, true, array('tax_group_id', 'name', 'amount', 'unit', 'isService', 'display', 'barcode', 'price_sell', 'price_sell_currency', 'price_buy', 'feedback_compensation', 'dozvoljeni_kalo', 'keub', 'keubarticle_group_id', 'prikaz_group_id', 'article_group_id', 'is_visible'))) {
				$this->Session->setFlash(__('The article has been saved', true), 'default', array('class' => 'success'));
				
				
				//print_r('insert into articles_rucno ("article_id", "amount_new") values('.$id.', 5)');
				//exit;
				
				$x = $item['Article'];
				$y = $this->data['Article'];
				
				/*
				print_r('aa');
				print_r($x);
				print_r('bb');
				print_r($y);
				print_r('cc');
				
				print_r('insert into articles_rucno values('.
					'null, "'.$id.'", "'.$x['name'].'", "'.$x['amount'].'", "'.$y['amount'].'", "'.$x['price_buy'].'",'.
					' "'.$y['price_buy'].'", "'.$x['price_sell'].'", "'.$y['price_sell'].'", "'.$this->Session->read('Auth.User.id').'", now())');
				print_r($this->data);
				print_r($item);
				exit;
				*/
				
				
				$rez = mysql_query('insert into articles_rucno values('.
					'null, "'.$id.'", "'.$x['name'].'", "'.$x['amount'].'", "'.$y['amount'].'", "'.$x['price_buy'].'",'.
					' "'.$y['price_buy'].'", "'.$x['price_sell'].'", "'.$y['price_sell'].'", "'.$this->Session->read('Auth.User.id').'", now())');
				
				
				
				
				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
		$taxGroups = $this->Article->TaxGroup->find('list');
		$keubarticleGroups = $this->Article->KeubarticleGroup->find('list');
		$prikazGroups = $this->Article->PrikazGroup->find('list');
		$articleGroups = $this->Article->ArticleGroup->find('list');
		
		//$this->set(compact('taxGroups','keubarticleGroups', 'prikazGroups'));
		$this->set(compact('taxGroups','keubarticleGroups', 'prikazGroups', 'articleGroups'));
	}

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

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

}
?>