fitness-web/frontend/models/ReceptionForm.php

140 lines
3.4 KiB
PHP

<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
use common\models\Card;
use common\models\Customer;
use common\models\Ticket;
use common\models\Account;
use common\models\CardSearch;
use common\models\AccountState;
/**
* ContactForm is the model behind the contact form.
*/
class ReceptionForm extends Model
{
public $number;
public $card;
public $customer;
public $tickets;
public $defaultAccount;
public $cardSearchModel;
public $lastCassaState;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['number'], 'required'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
public function readCard(){
$this->number = str_replace("ö", "0", $this->number);
$this->card = Card::find()->andWhere(['or', ['and',[ 'in','number' , [$this->number]],"trim(coalesce(number, '')) <>'' " ], ['and', ['in','rfid_key' ,[ $this->number] ],"trim(coalesce(rfid_key, '')) <>'' "]])->one();
if ( $this->card != null ){
$this->customer = $this->card->customer;
$this->readValidTickets();
}
$defaultAccount = Account::readDefault();
if ( isset($defaultAccount)){
$this->defaultAccount = Account::findOne($defaultAccount);
}
$this->readLastCassaState();
$this->cardSearchModel = new CardSearch();
}
public function readLastCassaState(){
$a = Account::readDefault();
if ( isset($a)){
$this->lastCassaState = AccountState::find()->andWhere(['account_state.id_account' => $a])
->andWhere(['account_state.id_user' => \Yii::$app->user->id] )
->orderBy(['account_state.created_at' => SORT_DESC])
->limit(1)
->one();
}
}
public function hasCassa(){
return isset($this->lastCassaState) ;
}
public function isCassaOpen(){
return ( isset($this->lastCassaState) && $this->lastCassaState->isTypeOpen());
}
public function isCassaClose(){
return ( isset($this->lastCassaState) && $this->lastCassaState->isTypeClose());
}
public function getDefaultAccountName(){
$result = "";
if ( $this->defaultAccount ){
$result = $this->defaultAccount->name;
}
return $result;
}
public function readValidTickets(){
$this->tickets = Ticket::readActive($this->card);
}
/**
* @return true , if card found without customer
* */
public function isFreeCard(){
$result = isset($this->card) && !isset($this->customer);
return $result;
}
/**
* @return true , if card found with customer
* */
public function isCardWithCustomer(){
$result = isset($this->card) && isset($this->customer);
return $result;
}
/**
* @return true , if card and customer found with at least one valid tickets
* */
public function isCustomerWithTicket(){
$result = false;
if ( isset($this->card) && isset($this->customer) ){
if ( isset($this->tickets) && count($this->tickets) > 0 ){
$result = true;
}
}
return $result;
}
/**
* @return true, if no card and customer found
* */
public function isInvalidNumber(){
$result = !isset($this->card) && !isset($this->customer);
return $result;
}
}