<?php
class echoSignM extends CFormModel 
{	
	public $sign;
	
	// ibagreement / trader_agreement
	//public $email;
	public $name;
	public $nameTwo;
	public $comName;
	
	// withdrawal
	public $account;
	public $reason;
	public $amount;
	public $currency;
	public $printName;
		
		public $beneficiary;
		public $bankName;
		public $bankAdress;
		public $bankAccount;
		public $address;
		public $swift;
		public $reference;
		public $intermBankName;
		
		public $purseNumer;
		public $accName;
		
		public $accNameWeb;
		public $accEmail;
		
		public $email;		
	
	public $es_img;
	public $traderId;
	public $date;
	
	public $phone;
	public $repName;
	public $repEmail;
	public $repAddress;
	public $nationaly;
	public $closeTrAcc;
	
	public $docFullNames; 
	public $sendViaMail; 
	public $finishTexts; 
	
	public function rules(){
		$model=new Registration('registration');
   
		return array(
			array('sign', 'required', 'except'=>'partners', 'message'=> Yii::t("main","36_cannot_be_blank")),
		
			// ibagreement
			array('name, comName', 'required', 'on'=>'ibagreement', 'message'=> Yii::t("main","36_cannot_be_blank")),
			//array('email' , 'email', 'on'=>'ibagreement','message' => Yii::t("main","22_this_email_address_is_not_a_valid")),
			array('name, comName', 'match','pattern' => '/^[a-z0-9`~!"@№%#\$;:\?\/\\&\*\+-_\(\)=\{\}\[\]\'|\<\>\s]*$/i','message' => Yii::t("main","34_only_latin_characters_allowed_for"), 'on'=>'ibagreement'),
			array('name', 'match','pattern' => '/^[a-z0-9`"-_\(\)\'\s]*$/i','message' => Yii::t("main","34_only_latin_characters_allowed_for"), 'on'=>'handWriteCheck'),
		
			array('comName','match','pattern' => '/^\D*$/','message' => Yii::t("main","33_numbers_are_not_allowed_in"), 'on'=>'ibagreement'),		
			
			// trader_agreement
			array('name', 'required', 'on'=>'trader_agreement', 'message'=> Yii::t("main","36_cannot_be_blank")),
			//array('email' , 'email', 'on'=>'trader_agreement', 'message' => Yii::t("main","22_this_email_address_is_not_a_valid")),
			array('name', 'match', 'pattern' => '/^[a-z0-9`~!"@№%#\$;:\?\/\\&\*\+-_\(\)=\{\}\[\]\'|\<\>\s]*$/i','message' => Yii::t("main","34_only_latin_characters_allowed_for"), 'on'=>'trader_agreement'),
			
			// withdrawal
			array('account, name, reason, amount, currency, printName, email', 'required', 'on'=>'withdrawal, bank_withdrawal, skrill_withdrawal, webmoney_withdrawal', 'message'=>Yii::t("main","36_cannot_be_blank")), 
			array('account, name, reason, amount, currency, printName, email, beneficiary, bankName, bankAdress, bankAccount, address, swift, reference, purseNumer, accName, accEmail, accNameWeb, intermBankName', 'match', 'pattern' => '/^[a-z0-9`~!"@№%#\$;:\?\/\\&\*\+-_\(\)=\{\}\[\]\'|\<\>\s]*$/i','message' => Yii::t("main","34_only_latin_characters_allowed_for"), 'on'=>'withdrawal, bank_withdrawal, skrill_withdrawal, webmoney_withdrawal'), 
			
			array('account', 'numerical', 'integerOnly'=>true, 'on'=>'withdrawal, bank_withdrawal, skrill_withdrawal, webmoney_withdrawal','message' => Yii::t("main","371_numbers_allowed")),			
			array('amount', 'numerical', 'integerOnly'=>false, 'on'=>'withdrawal, bank_withdrawal, skrill_withdrawal, webmoney_withdrawal','message' => Yii::t("main","371_numbers_allowed")),			
			array('amount, account', 'checkAmount', 'on'=>'withdrawal, bank_withdrawal, skrill_withdrawal, webmoney_withdrawal'),						
			
				// bank withdrawal
				array('beneficiary, bankName, bankAdress, bankAccount, address, swift, reference', 'required', 'on'=>'bank_withdrawal, withdrawal', 'message'=>Yii::t("main","36_cannot_be_blank")),				
				
				// skrill withdrawal
				array('purseNumer, accName', 'required', 'on'=>'skrill_withdrawal, withdrawal', 'message'=>Yii::t("main","36_cannot_be_blank")), 
				
				// webmoney
				array('accEmail, accNameWeb', 'required', 'on'=>'webmoney_withdrawal, withdrawal', 'message'=>Yii::t("main","36_cannot_be_blank")), 	
				array('accEmail' , 'email', 'on'=>'webmoney_withdrawal, withdrawal', 'message' => Yii::t("main","22_this_email_address_is_not_a_valid")),
				
			array('name', 'match','pattern' => '/^[a-z0-9`"-_\(\)\'\s]*$/i','message' => Yii::t("main","34_only_latin_characters_allowed_for"), 'on'=>'partners'),	
			//array('date, name', 'required', 'on'=>'partners', 'message'=> Yii::t("main","36_cannot_be_blank")),
			
			//eu_withdrawal
			array('account, name, reason, amount, currency, printName, beneficiary, bankName, bankAdress, bankAccount, intermBankName, address, swift, reference, repAddress, nationaly, traderId', 'required', 'on'=>'eu_withdrawal, eu_withdrawal_corp', 'message'=>Yii::t("main","36_cannot_be_blank")), 
			array('closeTrAcc', 'required', 'on'=>'eu_withdrawal', 'message'=>Yii::t("main","959_choose_the_answer")),
			array('repName', 'required', 'on'=>'eu_withdrawal_corp', 'message'=>Yii::t("main","36_cannot_be_blank")), 
			array('closeTrAcc', 'in', 'on'=>'eu_withdrawal, eu_withdrawal_corp', 'range' => array('Yes', 'No')),
			array('nationaly', 'in', 'on'=>'eu_withdrawal, eu_withdrawal_corp', 'range' => $model->GetCountry()),
			array('account', 'numerical', 'integerOnly'=>true, 'on'=>'eu_withdrawal, eu_withdrawal_corp','message' => Yii::t("main","371_numbers_allowed")),			
			array('amount', 'numerical', 'integerOnly'=>false, 'on'=>'eu_withdrawal, eu_withdrawal_corp','message' => Yii::t("main","371_numbers_allowed")),			
			array('amount, account', 'checkAmount', 'on'=>'eu_withdrawal, eu_withdrawal_corp'),	
			
			
			//noor ibagreement
			array('name, nameTwo, email, phone, bankName, swift, beneficiary, intermBankName, bankAccount, repName, repEmail', 'required', 'on'=>'noor_ibagreement', 'message'=> Yii::t("main","36_cannot_be_blank")),
			array('name, nameTwo, email, phone, bankName, swift, beneficiary, intermBankName, bankAccount, repName, repEmail', 'match', 'pattern' => '/^[a-z0-9`~!"@№%#\$;:\?\/\\&\*\+-_\(\)=\{\}\[\]\'|\<\>\s]*$/i','message' => Yii::t("main","34_only_latin_characters_allowed_for"), 'on'=>'noor_ibagreement'),
			array('email, repEmail' , 'email', 'on'=>'noor_ibagreement', 'message' => Yii::t("main","22_this_email_address_is_not_a_valid")),
			
			//noor withdrawal
			array('account, name, reason, amount, currency, bankAccount, bankName, bankAdress, swift, intermBankName, printName, email', 'required', 'on'=>'noor_withdrawal', 'message'=>Yii::t("main","36_cannot_be_blank")), 
		);
	}
	
	public function init()
	{
		$account = new Account();
		$info = $account->GetInfo();
		
		$this->date = time();
		
		
		$this->docFullNames = array('ibagreement'=>Yii::t("main","759_ibagreement"), 'trader_agreement'=>Yii::t("main","760_trader_agreement"), 'withdrawal'=>Yii::t("main","761_trader_agreement")); 
		$this->sendViaMail= array('ibagreement'=>true, 'trader_agreement'=>true, 'withdrawal'=>false); 
		$this->finishTexts = array('ibagreement'=>Yii::t("main","763_ibagreement_finish_text")); 
		
		if($this->getScenario() == 'ibagreement'){
			if($info->Type==4 && $info->GroupType==2){
				$Registration = new RegistrationCompany();
				$Registration->initCompany();
				$this->name = $this->comName = $Registration->company_name;
			}else{
				$Registration = new Registration();
				$Registration->initIndv();
				//$this->email = $Registration->email;
				$this->name = $this->comName = $Registration->firstName.' '.$Registration->lastName;				
			}
		}elseif($this->getScenario() == 'noor_ibagreement'){
			$Registration = new Registration();
			$Registration->initIndv();
			if($info->Type==4 && $info->GroupType==2){
				$RegistrationCom = new RegistrationCompany();
				$RegistrationCom->initCompany();
				$this->email = $this->repEmail = $Registration->email;
				$this->phone = $Registration->phone;
				$this->name = $this->nameTwo = $this->repName = $RegistrationCom->company_name;
			}else{
				$this->email = $this->repEmail = $Registration->email;
				$this->phone = $Registration->phone;
				$this->name = $this->nameTwo = $this->repName = $Registration->firstName.' '.$Registration->lastName;					
			}
		}elseif($this->getScenario() == 'trader_agreement'){
			$Registration = new Registration();
			$Registration->initIndv();
			//$this->email = $Registration->email;
			$this->name =  $Registration->firstName.' '.$Registration->lastName;
		}elseif($this->getScenario() == 'withdrawal' || $this->getScenario() == 'bank_withdrawal' || $this->getScenario() == 'skrill_withdrawal' || $this->getScenario() == 'webmoney_withdrawal'){
			$Registration = new Registration();
			$Registration->initIndv();
			$this->email = $Registration->email;
			if($info->Type==4 && $info->GroupType==2){
				$Registration = new RegistrationCompany();
				$Registration->initCompany();
				$this->name = $this->printName = $this->beneficiary = $Registration->company_name;
			}else{
				$this->name = $this->printName = $this->beneficiary = $Registration->firstName.' '.$Registration->lastName;
			} 
		}elseif($this->getScenario() == 'eu_withdrawal'){
			$Registration = new Registration();
			$Registration->initIndv();
			
			if(($info->Type==4 && $info->GroupType==2)){
				$Registration = new RegistrationCompany();
				$Registration->initCompany();
				$this->name = $this->printName = $this->beneficiary = $Registration->company_name;
			}elseif($info->Type==6 && $info->GroupType==2){
				$Registration = new RegistrationTrader('Corporate');
				$Registration->initTrader();
				$this->name = $this->printName = $this->beneficiary = $Registration->firstName;
			}else{
				$this->name = $this->printName = $this->beneficiary = $Registration->firstName.' '.$Registration->lastName;
				
				$this->nationaly = $Registration->country;
				$this->traderId = $Registration->idNum;
				$this->repAddress = $Registration->address;
			} 
		}elseif($this->getScenario() == 'noor_withdrawal'){
			$Registration = new Registration();
			$Registration->initIndv();
			$this->email = $Registration->email;
			
			if(($info->Type==4 && $info->GroupType==2)){
				$Registration = new RegistrationCompany();
				$Registration->initCompany();
				$this->name = $this->printName =  $Registration->company_name;
			}elseif($info->Type==6 && $info->GroupType==2){
				$Registration = new RegistrationTrader('Corporate');
				$Registration->initTrader();
				$this->name = $this->printName = $Registration->firstName;
			}else{
				$this->name = $this->printName = $Registration->firstName.' '.$Registration->lastName;
			} 			
		}
	}
	
	public function attributeLabels()
	{
		$scenarionLabels = array();
		$commonLabels = array(
			'sign'=>Yii::t("funds","531_echo_sign"),
		);
		 switch($this->getScenario())
		{
			case 'ibagreement':
				$scenarionLabels = array(				
					'email' => Yii::t("funds",'530_echo_email'),
					'name' => Yii::t("funds","533_name_company"),
					'comName' => Yii::t("funds","533_name_company"),
				);
			break;
			
			case 'trader_agreement':
				$scenarionLabels = array(				
					'email' => Yii::t("funds",'530_echo_email'),
					'name' => Yii::t("funds","533_name_company"),
				);
			break;
			
			case 'withdrawal':
			case 'bank_withdrawal':
			case 'skrill_withdrawal':
			case 'webmoney_withdrawal':
				$scenarionLabels = array(				
					'account' => Yii::t("funds",'535_wth_echo_cust_acc'),
					'name' => Yii::t("funds","534_wth_echo_cust_name"),
					'reason' => Yii::t("funds","537_wth_echo_reason"),
					'amount' => Yii::t("funds","536_wth_echo_amount"),
					'currency' => Yii::t("funds","538_wth_echo_curr"),
					'printName' => Yii::t("funds","551_wth_echo_print_name"),
					'email' => Yii::t("funds","530_echo_email"),
					
					'beneficiary' => Yii::t("funds","539_wth_echo_benific"),
					'bankName' => Yii::t("funds","540_wth_echo_bank_name"),
					'bankAdress' => Yii::t("funds","541_wth_echo_bank_adr"),
					'bankAccount' => Yii::t("funds","542_wth_echo_bank_acc"),
					'address' => Yii::t("funds","543_wth_echo_oter_adr"),
					'swift' => Yii::t("funds","545_wth_echo_swift"),
					'reference' => Yii::t("funds","546_wth_echo_ref"),
					'intermBankName' => Yii::t("funds","544_wth_echo_name_intrm_bank"),
					
					'purseNumer' => Yii::t("funds","549_wth_echo_webmoney_purse"),
					'accName' => Yii::t("funds","550_wth_echo_webmoney_acc_name"),
					
					'accNameWeb' => Yii::t("funds","550_wth_echo_webmoney_acc_name"),
					'accEmail' => Yii::t("funds","547_wth_echo_skril_email"),
					'traderId'=>Yii::t("main","211_passport"),
					'repAddress'=>Yii::t("main","133_address"),
					'nationaly'=>Yii::t("main","958_nationaly"),
					'repName'=>Yii::t("main","957_rep_name"),
				);
			break;
			
			case 'noor_withdrawal':
				$scenarionLabels = array(				
					'account' => Yii::t("main",'403_account_number'),
					'name' => Yii::t("funds","582_acc_name"),
					'reason' => Yii::t("funds","583_the_sum"),
					'amount' => Yii::t("funds","536_wth_echo_amount"),
					'currency' => Yii::t("funds","538_wth_echo_curr"),
					'printName' => Yii::t("funds","551_wth_echo_print_name"),
					'email' => Yii::t("funds","530_echo_email"),
					
					'bankAccount' => Yii::t("funds","17_account_"),
					'bankName' => Yii::t("funds","12_bank_name"),
					'bankAdress' => Yii::t("funds","541_wth_echo_bank_adr"),
					'swift' => Yii::t("funds","117_swift_code"),
					'intermBankName' => Yii::t("funds","581_iban"),
				);
			break;
		}
		
		return array_merge($commonLabels, $scenarionLabels);
	}
	
	public function checkAmount($attribute,$params){
		if(!empty($this->account) && !empty($this->amount)){
			$Account = new Account();
			$info = $Account->getInfo();
			$accounts = $Account->GetAccount(array('live'));
			if($info->Type == 4){
				$sysAccounts = $Account->GetSysAccount();
				$accounts = array_merge($sysAccounts, $accounts);
			}
			foreach($accounts as $acc){
				if(abs($this->account) == abs($acc->AccountNum)){
					if($this->currency == $acc->Currency){
						$curAmount = $this->amount;
					}else{
						$curAmount = stFunctions::currConv($this->currency, $acc->Currency, $this->amount);
					}
					if((float)$acc->Balance < (float)$curAmount){
						$this->addError('amount', Yii::t("funds","572_you_dont_have_enough_funds_for_with"));
					}
				}
			}
		}
		
		if($attribute == 'amount' && $this->amount == 0){
			$this->addError('amount', Yii::t("funds","567_AmountMustBeGreaterThanZero"));
		}
	}
	
	public function sendEchoRequest($fileType, $json, $url){
		//var_dump(Yii::app()->params['json_server'].'/API/Registration/SaveEchoSignRequest?guid='.Yii::app()->user->id.'&fileType='.$fileType.'&json='.$json.'&url='.$url);
		$Info = Yii::app()->curl->get(Yii::app()->params['json_server'].'/API/Registration/SaveEchoSignRequest?guid='.Yii::app()->user->id.'&fileType='.$fileType.'&json='.$json.'&url='.$url);			
		//var_dump($Info);
		return $Info;
	}

}