<?php
class Customer extends AppModel {

    var $name = 'Customer';

    var $displayField = 'name';

    var $validate = array(
	    'name' => array(
			    'rule' => 'isUnique',
			    'message' => 'This Customer name is already taken or has been left blank',
			    'required' => true,
			    'allowEmpty' => false),



	    'abn' => array(
			    'rule' => 'checkABN',
			    'required' => false,
			    'message' => 'ABN must contain 11 digits',
			    'allowEmpty' => true,
	    )
    );


    /* Custom Validation for ABN */

    function checkABN($data) {
	$numbers = preg_replace("/\s*\D*/", "", $data['abn']);
	$length = strlen($numbers);
	return $length == 11;
	//ABNs must be exactly 11 digits long.
    }


    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasMany = array(
	    'Address' => array('className' => 'Address',
			    'foreignKey' => 'customer_id',
			    'dependent' => false,
			    'conditions' => '',
			    'fields' => '',
			    'order' => '',
			    'limit' => '',
			    'offset' => '',
			    'exclusive' => '',
			    'finderQuery' => '',
			    'counterQuery' => ''
	    ),
	    'Contact' => array('className' => 'User',
			    'foreignKey' => 'customer_id',
			    'dependent' => false,
			    'conditions' => '',
			    'fields' => '',
			    'order' => '',
			    'limit' => '',
			    'offset' => '',
			    'exclusive' => '',
			    'finderQuery' => '',
			    'counterQuery' => ''
	    ),
	    'Enquiry' => array('className' => 'Enquiry',
			    'foreignKey' => 'customer_id',
			    'dependent' => false,
			    'conditions' => '',
			    'fields' => '',
			    'order' => '',
			    'limit' => '',
			    'offset' => '',
			    'exclusive' => '',
			    'finderQuery' => '',
			    'counterQuery' => ''
	    ),
	    'Invoice' => array('className'=>'Invoice',
			    'foreignKey' => 'customer_id'),
	    'Job' => array('className'=>'Job',
			    'foreignKey' => 'customer_id')
    );



    var $belongsTo = array('CustomerCategory'=>
	    array('className' => 'CustomerCategory','foreignKey' => 'customer_category_id'),
	    'Country' =>
	    array('className' =>'Country', 'foreignKey' => 'country_id')
    );



    var $hasAndBelongsToMany = array (
	    'Industry' => array(
			    'className' => 'Industry',
			    'joinTable' => 'industries_customers',
			    'foreignKey' => 'customer_id',
			    'associationForeignKey' => 'industry_id'
	    )
    );
}
?>