<?php
class ReceivingsController extends AppController {


	
	
	var $name = 'Receivings';
    var $components = array(
	    'Filter' => array('indexKeub,indexMedjus,indexZapisnikPromjenaCijenaPopusti,indexKnjigaPopisa,indexZapisnikPromjenaCijeneArtikla,indexZapisnikPromjenaCijeneArtiklaList,indexAnnulments,viewAnnulments,viewInventura,addAnnulments,editAnnulments,indexReturns,viewReturns,addReturns,editReturns,indexTransfers,indexInventura,viewTransfers,addTransfers,editTransfers',
	        'index' => array(
	            'supplier_id' => array(
        	        'form_options' => array('label' => 'Supplier', 'options' => 'Suppliers'),
	            	'filter_options' => array(),
	            ),
				'canceled' => array(
        	        'form_options' => array('label' => 'Status', 'options' => 'Filter.Receiving_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->allow('indexKeub','indexMedjus','indexZapisnikPromjenaCijenaPopusti','indexKnjigaPopisa','indexZapisnikPromjenaCijeneArtikla','indexZapisnikPromjenaCijeneArtiklaList','indexAnnulments','viewMedjus','viewZapisnikPromjenaCijenaPopusti','viewKnjigaPopisa','viewZapisnikPromjenaCijeneArtikla','viewZapisnikPromjenaCijeneArtiklaList','viewAnnulments','viewInventura','addAnnulments','editAnnulments','indexReturns','viewReturns','addReturns','editReturns','indexTransfers','indexInventura','viewTransfers','addTransfers','editTransfers','indexSkladiste','viewSkladiste','addSkladiste','editSkladiste' );		
		$this->Auth->mapActions(array('cancel' => 'delete'));
	}

	function index() {
		$this->Receiving->recursive = 0;
		$this->set('items', $this->paginate('Receiving',  array('Receiving.type = '=> 0 )));
	}
	
	function indexKeub(){
    $this->Receiving->recursive = 0;
	}
	
	function indexMedjus(){
    $this->Receiving->recursive = 0;
	}
	
	function indexKnjigaPopisa(){
	}
	
	function indexZapisnikPromjenaCijenaPopusti(){
    $this->Receiving->recursive = 0;
	}
	
	function indexZapisnikPromjenaCijeneArtikla(){
    $this->Receiving->recursive = 0;
	}
	
	function indexZapisnikPromjenaCijeneArtiklaList(){
    $this->Receiving->recursive = 0;
	}
	
	function indexAnnulments(){
	    $this->Receiving->recursive = 0;
		$this->set('items', $this->paginate('Receiving',  array('Receiving.type = '=> 1 )));
	
	}
	
	function indexReturns(){
	    $this->Receiving->recursive = 0;
		$this->set('items', $this->paginate('Receiving',  array('Receiving.type = '=> 2 )));
	
	}
	
	function indexInventura(){
	}
	
	function indexTransfers(){
	    $this->Receiving->recursive = 0;
		$this->set('items', $this->paginate('Receiving',  array('Receiving.type = '=> 3 )));
	
	}
	
	function indexSkladiste(){
	    $this->Receiving->recursive = 0;
		$this->set('items', $this->paginate('Receiving',  array('Receiving.type = '=> 6 )));
	
	}

	function view($id = null) {
		$this->Receiving->contain('Supplier', 'ReceivingItem.Article');
		if (!$id  || !($item = $this->Receiving->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Receiving', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('item', $item);
	}
	
	function viewInventura($id = null){
	}
	
	function viewMedjus($id = null){
	}
	
	function viewKnjigaPopisa($id = null){
	}
	
	function viewZapisnikPromjenaCijenaPopusti($id = null){
	}
	
	function viewZapisnikPromjenaCijeneArtikla($id = null){
	}
	
	function viewZapisnikPromjenaCijeneArtiklaList($id = null){
	}
	
	function viewAnnulments($id = null){
	$this->Receiving->contain('ReceivingItem.Article');
		if (!$id  || !($item = $this->Receiving->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Annulment', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexAnnulments'));
		}
		$this->set('item', $item);
	}
	
	function viewReturns($id = null){
	$this->Receiving->contain('Supplier', 'ReceivingItem.Article');
		if (!$id  || !($item = $this->Receiving->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Return', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexReturns'));
		}
		$this->set('item', $item);
	}
	
	function viewTransfers($id = null){
	$this->Receiving->contain('Supplier', 'ReceivingItem.Article');
		if (!$id  || !($item = $this->Receiving->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Transfer', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexTransfers'));
		}
		$this->set('item', $item);
	}
	
	function viewSkladiste($id = null){
	$this->Receiving->contain('ReceivingItem.Article');
		if (!$id  || !($item = $this->Receiving->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Skladiste', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexSkladiste'));
		}
		$this->set('item', $item);
	}

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

			// calculate price field
			
			$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
				foreach($this->data['ReceivingItem'] as &$ri) {
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;

			$this->Receiving->create();
			if ($this->Receiving->saveAll($this->data)) {
				$this->Session->setFlash(__('The receiving has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The receiving could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		
		$suppliers = $this->Receiving->Supplier->find('list');
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');
		
		
	
		
		$this->Receiving->order = 'Receiving.created DESC';
		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		//for (int $i=0;$i<3000;$i++) $articlesData[$i]=$tmp[$i];
		unset($tmp);

		$this->set(compact('suppliers', 'articles', 'articlesData'));
	}
	
	function addAnnulments(){
	if (!empty($this->data)) {

			// calculate price field
			$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
			
				foreach($this->data['ReceivingItem'] as &$ri) {
				$ri['amount'] = - $ri['amount'];
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 1;

			$this->Receiving->create();
			if ($this->Receiving->saveAll($this->data)) {
				$this->Session->setFlash(__('The annulment has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexAnnulments'));
			} else {
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
				$this->Session->setFlash(__('The annulment could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');
		$this->Receiving->order = 'Receiving.created DESC';
		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('articles', 'articlesData'));
	}
	
	function addReturns(){
	if (!empty($this->data)) {

			// calculate price field
			$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
			
				foreach($this->data['ReceivingItem'] as &$ri) {
				$ri['amount'] = - $ri['amount'];
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 2;

			$this->Receiving->create();
			if ($this->Receiving->saveAll($this->data)) {
				$this->Session->setFlash(__('The return has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexReturns'));
			} else {
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
				$this->Session->setFlash(__('The return could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$suppliers = $this->Receiving->Supplier->find('list');
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');
		$this->Receiving->order = 'Receiving.created DESC';
		$tmp = $this->Receiving->ReceivingItem->Article->find('all');
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('suppliers', 'articles', 'articlesData'));
	}
	
	function addTransfers(){
	if (!empty($this->data)) {

			// calculate price field
			$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
			
				foreach($this->data['ReceivingItem'] as &$ri) {
				$ri['amount'] = - $ri['amount'];
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 3;

			$this->Receiving->create();
			if ($this->Receiving->saveAll($this->data)) {
				$this->Session->setFlash(__('The transfer has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexTransfers'));
			} else {
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
				$this->Session->setFlash(__('The transfer could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$suppliers = $this->Receiving->Supplier->find('list');
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');
		$this->Receiving->order = 'Receiving.created DESC';
		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('suppliers', 'articles', 'articlesData'));
	}
	
	function addSkladiste(){
	if (!empty($this->data)) {

			// calculate price field - NE KORISTI abs(), amount moze biti + ili -
			$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
			
				foreach($this->data['ReceivingItem'] as &$ri) {
					// amount ostaje kakav je unesen (+ ili -)
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 6;

			$this->Receiving->create();
			if ($this->Receiving->saveAll($this->data)) {
				$this->Session->setFlash(__('The skladiste has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexSkladiste'));
			} else {
				$this->Session->setFlash(__('The skladiste could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0')));
		$this->Receiving->order = 'Receiving.created DESC';
		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('articles', 'articlesData'));
	}

	function cancel($id = null) {
		$S = array(0 => "Receiving", 1 => "Annulment", 2 => "Return", 3 => "Transfer", 6 => "Skladiste");
		$Si = array(0 => "index", 1 => "indexAnnulments", 2 => "indexReturns", 3 => "indexTransfers", 6 => "indexSkladiste");
		$i= $this->Receiving->find('first', array('conditions' => array('Receiving.id' => $id)));
		
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Receiving->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid '.$S[$i['Receiving']['type']], true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => $Si[$i['Receiving']['type']]));
		}

		if ($this->Receiving->cancelReceiving($id)) {
			$this->Session->setFlash(__($S[$i['Receiving']['type']].' canceled', true), 'default', array('class' => 'success'));
		} 
		else {
			$this->Session->setFlash(__($S[$i['Receiving']['type']].' could not be canceled', true), 'default', array('class' => 'error'));	
		}
		$this->redirect(array('action' => $Si[$i['Receiving']['type']]));
	}


	function edit($id = null) {
			
	
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Receiving->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Receiving', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->Receiving->data = null;	// unset model data so modified field can be updated automagically
			
				$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
				foreach($this->data['ReceivingItem'] as &$ri) {
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				//	$ri['receiving_id']=$id;
					//$this->Receiving->ReceivingItem->save($this->data['ReceivingItem']);
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			
			
			if ($this->Receiving->saveAll($this->data)) {
			$this->Receiving->cancelReceiving($id);
				$this->Session->setFlash(__('The receiving has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The receiving could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
		
		$suppliers = $this->Receiving->Supplier->find('list');
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');

		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('suppliers', 'articles', 'articlesData'));
		
	}
	
	function editAnnulments($id = null){
			
	
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Receiving->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Annulment', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexAnnulments'));
		}
		if (!empty($this->data)) {
			$this->Receiving->data = null;	// unset model data so modified field can be updated automagically
			
				$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
				foreach($this->data['ReceivingItem'] as &$ri) {
				$ri['amount'] = - $ri['amount'];
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				//	$ri['receiving_id']=$id;
					//$this->Receiving->ReceivingItem->save($this->data['ReceivingItem']);
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 1;
			
			if ($this->Receiving->saveAll($this->data)) {
			$this->Receiving->cancelReceiving($id);
				$this->Session->setFlash(__('The annulment has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexAnnulments'));
			} else {
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
				$this->Session->setFlash(__('The annulment could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
		}
		
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');

		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact( 'articles', 'articlesData'));
	}
	
	function editReturns($id = null){
			
	
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Receiving->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Return', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexReturns'));
		}
		if (!empty($this->data)) {
			$this->Receiving->data = null;	// unset model data so modified field can be updated automagically
			
				$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
				foreach($this->data['ReceivingItem'] as &$ri) {
				$ri['amount'] = - $ri['amount'];
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				//	$ri['receiving_id']=$id;
					//$this->Receiving->ReceivingItem->save($this->data['ReceivingItem']);
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 2;
			
			if ($this->Receiving->saveAll($this->data)) {
			$this->Receiving->cancelReceiving($id);
				$this->Session->setFlash(__('The return has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexReturns'));
			} else {
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
				$this->Session->setFlash(__('The return could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
		}
		
		$suppliers = $this->Receiving->Supplier->find('list');
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');

		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('suppliers', 'articles', 'articlesData'));
	}
	
	function editTransfers($id = null){
			
	
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Receiving->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Transfer', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexTransfers'));
		}
		if (!empty($this->data)) {
			$this->Receiving->data = null;	// unset model data so modified field can be updated automagically
			
				$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
				foreach($this->data['ReceivingItem'] as &$ri) {
				$ri['amount'] = - $ri['amount'];
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				//	$ri['receiving_id']=$id;
					//$this->Receiving->ReceivingItem->save($this->data['ReceivingItem']);
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 3;
			
			if ($this->Receiving->saveAll($this->data)) {
			$this->Receiving->cancelReceiving($id);
				$this->Session->setFlash(__('The transfer has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexTransfers'));
			} else {
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
				$this->Session->setFlash(__('The transfer could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
			foreach($this->data['ReceivingItem'] as &$ri) {
			$ri['amount'] = - $ri['amount'];
			$ri['price'] = - $ri['price'];
			}
		}
		
		$suppliers = $this->Receiving->Supplier->find('list');
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');

		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		//print_r($tmp);
		//die();
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('suppliers', 'articles', 'articlesData'));
	}
	
	function editSkladiste($id = null){
			
	
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Receiving->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Skladiste', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'indexSkladiste'));
		}
		if (!empty($this->data)) {
			$this->Receiving->data = null;	// unset model data so modified field can be updated automagically
			
				$price_buy = $price_sell = 0;
			if (!empty($this->data['ReceivingItem'])) {
				foreach($this->data['ReceivingItem'] as &$ri) {
				// amount ostaje kakav je unesen (+ ili -)
					$ri['price'] = $ri['amount'] * $ri['price_buy'] * (1 - ($ri['discount'] / 100));
					$price_buy += $ri['price'];
					$price_sell += $ri['price_sell']* $ri['amount'];
				}
			}
			$this->data['Receiving']['price_buy'] = $price_buy;
			$this->data['Receiving']['price_sell'] = $price_sell;
			$this->data['Receiving']['type'] = 6;
			
			if ($this->Receiving->saveAll($this->data)) {
			$this->Receiving->cancelReceiving($id);
				$this->Session->setFlash(__('The skladiste has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'indexSkladiste'));
			} else {
				$this->Session->setFlash(__('The skladiste could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
		
		$articles = $this->Receiving->ReceivingItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0')));

		$tmp = $this->Receiving->ReceivingItem->Article->find('all',array('fields' => array('DISTINCT Article.id','Article.price_buy','Article.price_sell' )));
		$articlesData = array();
		foreach($tmp as &$t) $articlesData[$t['Article']['id']] = $t;
		unset($tmp);

		$this->set(compact('articles', 'articlesData'));
	}

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

}
