<?php
class ReceivingOrdersController extends AppController {

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

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

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

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

			$this->ReceivingOrder->create();
			if ($this->ReceivingOrder->saveAll($this->data)) {
				$this->Session->setFlash(__('The receiving order has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The receiving order could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$suppliers = $this->ReceivingOrder->Supplier->find('list');
		$articles = $this->ReceivingOrder->ReceivingOrderItem->Article->find('list', array('order' => array('name' => 'asc'),'conditions' => array('Article.isService' => '0'))); //getGrouped(null, 'id', 'display');
$this->ReceivingOrder->order = 'ReceivingOrder.created DESC';
		$tmp = $this->ReceivingOrder->ReceivingOrderItem->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 cancel($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->ReceivingOrder->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Receiving order', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}

		if ($this->ReceivingOrder->cancelReceivingOrder($id)) {
			$this->Session->setFlash(__('Receiving order canceled', true), 'default', array('class' => 'success'));
		} else {
			$this->Session->setFlash(__('Receiving order could not be canceled', true), 'default', array('class' => 'error'));
		}
		$this->redirect(array('action' => 'index'));
	}


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

		$tmp = $this->ReceivingOrder->ReceivingOrderItem->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 delete($id = null) {
		if (!$id || !$this->ReceivingOrder->read(null, $id)) {
			$this->Session->setFlash(__('Invalid Receiving order', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ReceivingOrder->delete($id)) {
			$this->Session->setFlash(__('Receiving order deleted', true), 'default', array('class' => 'success'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Receiving order delete failed. Please, try again.', true), 'default', array('class' => 'error'));
		$this->redirect(array('action' => 'index'));
	}


	/*function _faSuppliers() {
		return $this->Receiving_order->Supplier->find('list');
	}*/

}
