<?php
class Product extends AppModel {
	var $name = 'Product';
	var $order = array('Product.name ASC');
	var $displayField = 'name';
	var $validate = array(
		'product_group_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'tax_group_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'printer_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),


		'tezina' => array(
			'numeric' => array('rule' => array('numeric'), 'allowEmpty' => true),
			'Format decimalnog broja 0.00' => array('rule' => array('comparison', '>=', 0)),
		),
		'keub_group_id' => array(
			'numeric' => array('rule' => array('numeric'), 'allowEmpty' => true),
		),


		'note_group_id' => array(
			'numeric' => array('rule' => array('numeric'), 'allowEmpty' => true),
		),
		'extra_group_id' => array(
			'numeric' => array('rule' => array('numeric'), 'allowEmpty' => true),
		),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
			),
			'unique' => array(
				'rule' => array('unique'),
			),
		),
		'name_long' => array(
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'price' => array(
			'numeric' => array('rule' => array('numeric')),
			'g0' => array('rule' => array('comparison', '>=', 0)),
		),
		'price_1' => array(
			'numeric' => array('rule' => array('numeric')),
			'g0' => array('rule' => array('comparison', '>=', 0)),
		),
		'price_2' => array(
			'numeric' => array('rule' => array('numeric')),
			'g0' => array('rule' => array('comparison', '>=', 0)),
		),
		'price_3' => array(
			'numeric' => array('rule' => array('numeric')),
			'g0' => array('rule' => array('comparison', '>=', 0)),
		),
		'price_4' => array(
			'numeric' => array('rule' => array('numeric')),
			'g0' => array('rule' => array('comparison', '>=', 0)),
		),
		'sort_order' => array(
			'numeric' => array('rule' => array('numeric')),
			'g0' => array('rule' => array('comparison', '>', 0)),
		),
	);

	var $belongsTo = array(
		'ProductGroup' => array(
			'className' => 'ProductGroup',
			'foreignKey' => 'product_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'TaxGroup' => array(
			'className' => 'TaxGroup',
			'foreignKey' => 'tax_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'KeubGroup' => array(
			'className' => 'KeubGroup',
			'foreignKey' => 'keub_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Printer' => array(
			'className' => 'Printer',
			'foreignKey' => 'printer_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		
		'NoteGroup' => array(
			'className' => 'NoteGroup',
			'foreignKey' => 'note_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'ExtraGroup' => array(
			'className' => 'ExtraGroup',
			'foreignKey' => 'extra_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);

	var $hasMany = array(
		'ProductItem' => array(
			'className' => 'ProductItem',
			'foreignKey' => 'product_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'SaleItem' => array(
			'className' => 'SaleItem',
			'foreignKey' => 'product_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	function  beforeSave($options) {
		parent::beforeSave($options);

		if (!empty($this->data['Product']['id'])) {
			$this->ProductItem->deleteAll(array('product_id' => $this->data['Product']['id']), false, false);
		}

		if (empty($this->data['Product']['price_2'])) $this->data['Product']['price_2'] = $this->data['Product']['price_1'];
		if (empty($this->data['Product']['price_3'])) $this->data['Product']['price_3'] = $this->data['Product']['price_1'];
		if (empty($this->data['Product']['price_4'])) $this->data['Product']['price_4'] = $this->data['Product']['price_1'];

		// set current price to price_1
		//$this->data['Product']['price'] = $this->data['Product']['price_1'];

		return true;
	}

}
