<?php
class Article extends AppModel {
	var $name = 'Article';
	var $displayField = 'formated_display';
	var $order = array('Article.name ASC');
	var $virtualFields = array(
    'formated_display' => "CONCAT(IFNULL(Article.barcode,''),' ',Article.display)"
);
	var $validate = array(
		'tax_group_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'name' => array(
			'notempty' => array('rule' => array('notempty')),
			'unique' => array('rule' => array('unique')),
		),
		'amount' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'unit' => array(
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'price_sell' => array(
			'numeric' => array(
				'required' => true,
				'rule' => array('money', 'right'),
			),
		),
		'price_buy' => array(
			'numeric' => array(
				'required' => true,
				'rule' => array('money', 'right'),
			),
		),
		'feedback_compensation' => array(
			'numeric' => array(
				'rule' => array('money', 'right'),
				'allowEmpty' => true,
			),
		),
		'dozvoljeni_kalo' => array(
			'numeric' => array(
				'rule' => array('money', 'right'),
				'allowEmpty' => true,
			),
		),
		'isService' => array(
			'allowEmpty' => true,
		),
		/*
		'prikaz_group_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		*/
		
	);

	var $belongsTo = array(
		'TaxGroup' => array(
			'className' => 'TaxGroup',
			'foreignKey' => 'tax_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'KeubarticleGroup' => array(
			'className' => 'KeubarticleGroup',
			'foreignKey' => 'keubarticle_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'PrikazGroup' => array(
			'className' => 'PrikazGroup',
			'foreignKey' => 'prikaz_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'ArticleGroup' => array(
			'className' => 'ArticleGroup',
			'foreignKey' => 'article_group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);

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

	function  _formatDisplayField($row) {
		return "{$row['Article']['name']} ({$row['Article']['unit']})";
	}
	
	/*public function afterFind($results, $primary = false) {
    foreach ($results as $key => $val) {
         {
     if (  $results[$key]['Article']['barcode'] !=null)    $results[$key]['Article']['display'] = $results[$key]['Article']['barcode']." ".$results[$key]['Article']['display'];
        }
    }
    return $results;
}*/

}
