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

require_once APPPATH . 'interfaces/Validator_interface.php';

class TransSaveValidator implements Validator_interface
{
	private $params;

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

	public function is_valid(&$data)
	{
		if (!canSave())
		{
			throw new Exception("Can't Save.");
			return false;
		}

		if (sal_mod_active(EINVOICE) && !empty($data['id']) && in_array($data['tType'], array(SALINV,CRNOTE,DBNOTE)))
		{
			$ci =& get_instance();
			$tmp = $ci->db->select('isLocked')->from('trans')->where(array(
				'cId' => $this->params->cId,
				'id' => $data['id'],
				'isLocked !=' => ''
			))->get()->row_array();

			if ($tmp)
			{
				throw new Exception(lang('EINVOICE_LINKED_ERROR'));
				return false;
			}
		}

		if (empty($data['conTaxNo']) && !empty($data['classCode']) && ($data['classCode'] == 'SEZWP' || $data['classCode'] == 'SEZWOP'))
		{
			throw new Exception(lang('REQUIRED_CONTACT_TAXNO'));
			return false;
		}

		return true;
	}

}
