fitness-web/common/models/Card.php

140 lines
3.2 KiB
PHP

<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "card".
*
* @property integer $id_card
* @property string $number
* @property integer $status
* @property integer $type
* @property string $created_at
* @property string $updated_at
*/
class Card extends \common\models\BaseFitnessActiveRecord
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const TYPE_RFID = 10;
const TYPE_QRCODE = 20;
const TYPE_BARCODE = 30;
const TYPE_OLD = 40;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'card';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['number', 'type','status'], 'required'],
[['status', 'type'], 'integer'],
[['number'], 'string', 'max' => 20],
[['number'], 'unique' ]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_card' => Yii::t('common/card', 'Id Card'),
'number' => Yii::t('common/card', 'Number'),
'status' => Yii::t('common/card', 'Status'),
'type' => Yii::t('common/card', 'Type'),
'created_at' => Yii::t('common/card', 'Created At'),
'updated_at' => Yii::t('common/card', 'Updated At'),
];
}
static function statuses() {
return [
self::STATUS_ACTIVE => Yii::t('common/card', 'Active'),
self::STATUS_DELETED => Yii::t('common/card', 'Inactive'),
];
}
public function getStatusHuman(){
$result = null;
$s = self::statuses($this->status);
if ( array_key_exists($this->status, $s)){
$result = $s[$this->status];
}
return $result;
}
static function types() {
return [
self::TYPE_RFID => Yii::t('common/card', 'RFID'),
self::TYPE_QRCODE => Yii::t('common/card', 'QRCODE'),
self::TYPE_BARCODE => Yii::t('common/card', 'BARCODE'),
self::TYPE_OLD => Yii::t('common/card', 'OLD'),
];
}
public function getTypeHuman(){
$result = null;
$s = self::types($this->type);
if ( array_key_exists($this->type, $s)){
$result = $s[$this->type];
}
return $result;
}
public function isInactive(){
return $this->status == self::STATUS_DELETED;
}
public static function readCard($number,$free = null){
$card = null;
$query = Card::find()
->leftJoin(Customer::tableName(), 'card.id_card = customer.id_customer_card ' )
->andWhere(['number'=>$number ]);
if ( isset($free) ){
if ( $free == true){
$query->andWhere('customer.id_customer is null');
}else{
$query->andWhere('customer.id_customer is not null');
}
}
$cards = $query->all();
if ( count($cards) == 1){
$card = $cards[0];
}
return $card;
}
public function getCustomer(){
return $this->hasOne(Customer::className(), ['id_customer_card' => 'id_card']);
}
public function getCustomerName(){
$name = null;
$customer = $this->customer;
if ( $this->customer != null){
$name = $this->customer->name;
}
return $name;
}
}