<?php
class Enquiry extends AppModel {

    var $name = 'Enquiry';

    var $validate = array(
	    'user_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'User ID must be selected',
			    'on' => 'create'
	    ),
	    'customer_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must choose a Customer for this Enquiry',
			    'on' => 'create'
	    ),

	    'contact_user_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must choose a Contact for this Enquiry',
	    //'on' => 'create'
	    ),

	    'customer_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must choose a Customer for this Enquiry',
			    'on' => 'create'
	    ),

	    'state_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must choose a State for this Enquiry',
			    'on' => 'create'
	    ),

	    'country_id' => array(
			    'numeric' => array(
					    'rule' => 'numeric',
					    'required' => true,
					    'message' => 'Must choose a Country for this Enquiry',
					    'on' => 'create'
			    ),
			    'checkStates' => array(
					    'rule' => array('checkStates'),
					    'message' => 'Australian Enquiries must be assigned to Australian States. Overseas Enquiries must be assigned to other Countries',
					    'on' => 'create')),

	    'principle_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must choose a Principle for this Enquiry',
			    'on' => 'create'
	    ),

	    'status_id' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must choose a Status for this Enquiry'
	    ),
	    'gst' => array(
			    'rule' => 'numeric',
			    'required' => true,
			    'message' => 'Must select whether GST is applicable for this Enquiry',
			    'on' => 'create'
	    )
    );

    /*Custom Validation Rule to Check that Enquiries with Australian States are assigned to Australia.
	 * and that Overseas enquiries are NOT assigned to Australia.
	 * Not Portable at all. This code is only for CMC usage
	 * Should probably change this to use RegExes.
    */

    function checkStates($data) {
	if($this->data['Enquiry']['country_id'] != 1) { //This Enquiry is not for Australia. State must be set to overseas.
	    if($this->data['Enquiry']['state_id'] != 9) { //The State isn't set to Overseas. Return false
		return FALSE;
	    }
	    else {
		return TRUE;
	    }
	}
	if($this->data['Enquiry']['country_id'] == 1) { //This enquiry is for Australia. State must be for an Australian State
	    if($this->data['Enquiry']['state_id'] == 9) {
		return FALSE;
	    }
	    else {
		return TRUE;
	    }
	}
    }


    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
	    'User' => array('className' => 'User',
			    'foreignKey' => 'user_id',
			    'conditions' => array('User.type'=>'user'),
			    'fields' => '',
			    'order' => ''
	    ),
	    'Customer' => array('className' => 'Customer',
			    'foreignKey' => 'customer_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),
	    'Contact' => array('className' => 'User',
			    'foreignKey' => 'contact_user_id',
			    'conditions' => array('Contact.type'=>'contact'),
			    'fields' => '',
			    'order' => ''
	    ),

	    'State' => array('className' => 'State',
			    'foreignKey' => 'state_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),
	    'Country' => array('className' => 'Country',
			    'foreignKey' => 'country_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),
	    'Principle' => array('className' => 'Principle',
			    'foreignKey' => 'principle_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),
	    'Status' => array('className' => 'Status',
			    'foreignKey' => 'status_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),
	    'BillingAddress' => array('className' => 'Address',
			    'foreignKey' => 'billing_address_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),
	    'ShippingAddress' => array('className' => 'Address',
			    'foreignKey' => 'shipping_address_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),


    );

    var $hasMany = array(
	    'Quote' => array('className' => 'Quote',
			    'foreignKey' => 'enquiry_id',
			    'dependent' => false,
			    'conditions' => '',
			    'fields' => '',
			    'order' => '',
			    'limit' => '',
			    'offset' => '',
			    'exclusive' => '',
			    'finderQuery' => '',
			    'counterQuery' => ''
	    ),

	    'Invoice' => array('className' => 'Invoice',
			    'foreignKey'=>'enquiry_id'),

	    'Job' => array('className' => 'Job',
			    'foreignKey' =>'enquiry_id'),

	    'OrderAcknowledgement' => array('className'=>'OrderAcknowledgement', 'foreignKey'=>'enquiry_id')



    );



    var $hasAndBelongsToMany = array (
	    'Email' => array(
			    'className' => 'Email',
			    'joinTable' => 'emails_enquiries',
	    )
    );

    function formatAddress($id, $type) {

	if($type == 'BillingAddress' || $type == 'ShippingAddress') {
	    $address = $this->$type->find('first', array('conditions'=>array("$type.id"=>$id),'recursive'=>1));
	}
	else {
	    return;
	}

	if($address[$type]['country_id'] == 1) { //Australian Address
	    $formatted = <<<ENDADDRESS
		    {$address[$type]['address']}<br/>
		    {$address[$type]['city']}<br/>
		    {$address['State']['shortform']} {$address[$type]['postcode']}<br/>
ENDADDRESS;
	}
	else {
	    $formatted = <<<ENDADDRESS
		    {$address[$type]['address']}<br/>
		    {$address[$type]['city']}<br/>
		    {$address[$type]['postcode']}<br/>
		    {$address['Country']['name']}<br/>
ENDADDRESS;
	}

	return $formatted;
    }

}
?>
