<?php defined('BASEPATH') OR exit('No direct script access allowed');

require_once APPPATH . 'interfaces/Validator_interface.php';

class TransReferedValidator implements Validator_interface
{
	private $params;

	public function __construct(&$params=null)
	{
        $this->params =& $params;
	}

	public function is_valid(&$data)
	{
		if (empty($data['id']))
		{
			return true;
		}

		$ci =& get_instance();

		$ci->db->select('group_concat(concat(tNo ," (",t.tType,")")) AS gc', false);
		$ci->db->from('trans_ref AS tr');
		$ci->db->join('trans as t','t.id=tr.transId');
		$ci->db->where(array('tr.cId' => $this->params->cId, 'tr.refTransId' => $data['id']));

		// ignore rep commision journals
		$ci->db->where_not_in('t.tType', array(JOURNAL_VOUCHER, JOURNAL_CONTRA));

		$reft = $ci->db->get()->row_array();

		if (!empty($reft['gc']))
		{
			throw new Exception(sprintf(lang('TRANSACTION_REFERED'), $reft['gc']));
			return false;
		}

		return true;
	}
}
