<?php
class Supplier extends AppModel {
	var $name = 'Supplier';
	var $displayField = 'display';
	var $order = array('Supplier.name ASC');
	var $validate = array(
		'name' => array(
			'notempty' => array('rule' => array('notempty')),
			'unique' => array('rule' => array('unique'), 'message' => 'Name already exists. Please, choose another one', 'on' => 'create'),
		),
	);

	var $hasMany = array(
		'Receiving' => array(
			'className' => 'Receiving',
			'foreignKey' => 'supplier_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => 'date DESC',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	function  _formatDisplayField($row) {
		$fields = array($row['Supplier']['name']);
		if (!empty($row['Supplier']['address'])) $fields[] = $row['Supplier']['address'];
		if (!empty($row['Supplier']['city'])) $fields[] = $row['Supplier']['city'];

		return implode(', ', $fields);
	}

}
