<?php
App::uses('AppModel', 'Model');
/**
 * Credit Model
 *
 * @property User $User
 */
class Credit extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'id';

/**
 *Virtual fields 
 *
 * @var array 
 */
	public $virtualFields = array(
		'total' => '(Credit.value * Credit.amount)',
	);

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'card' => array(
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'credit_type_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'value' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'range' => array(
				'rule' => array('range', 0, 201),
				'message' => 'Please enter a value between 1 and 200.'
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'CreditType' => array(
			'className' => 'CreditType',
			'foreignKey' => 'credit_type_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	public function summarize($userId)
	{
		$total = $this->find('first', array(
			'conditions' => array(
				'user_id' => $userId,
				'not' => array(
					'refunded' => 1
				),
			),
			'group' => array(
				'user_id',
			),
			'fields' => array(
				'SUM(Credit.value) AS total'
			),
		));

		if (empty($total))
		{
			return 0;
		}

		return $total[0]['total'];
	}

	public function afterSave($created, $options = array())
	{
		$user = $this->find('first', array(
			'conditions' => array(
				'Credit.id' => $this->id,
			),
			'fields' => array(
				'user_id',
			),
			'recursive' => -1,
		));

		$summary = $this->summarize($user['Credit']['user_id']);
		$this->User->id = $user['Credit']['user_id'];
		$this->User->saveField('credits', $summary);
	}
}
