restore flag_out logic, toggleKey logic

This commit is contained in:
2016-12-29 19:17:31 +01:00
parent c673c5f73d
commit 6de07dac93
8 changed files with 100 additions and 67 deletions

View File

@@ -4,19 +4,22 @@ namespace frontend\models;
use common\models\DoorLog;
use common\models\Log;
use Yii;
use yii\base\Model;
use /** @noinspection PhpUndefinedClassInspection */
yii\base\Model;
use common\models\CardKeyAssignment;
use common\models\Key;
use yii\helpers\ArrayHelper;
use common\components\Helper;
use common\models\Card;
/** @noinspection PhpUndefinedClassInspection */
/**
* ContactForm is the model behind the contact form.
*
* @property \common\models\Card $card
* @property \common\models\Key $key
* @property \common\models\Key $keyModel
* @property \common\models\Card $keyCard
*
*/
class KeyToggleForm extends Model
@@ -48,42 +51,57 @@ class KeyToggleForm extends Model
return [
];
}
/**
*
*/
public function toggleKey(){
$query= Key::find();
$this->key = Helper::fixAsciiChars($this->key);
// add condition rfid key or number
$query->andWhere(['or',
['and',[ 'in','key.number' , [$this->key]],"trim(coalesce(key.number, '')) <>'' " ],
['and', ['in','key.rfid_key' ,[ $this->key ] ],"trim(coalesce(key.rfid_key, '')) <>'' "],
]);
$this->keyModel = $query->one();
if ( isset($this->keyModel) ){
//ha van ilyen kulcs
if ( isset($this->keyModel) ){
//find card - key assignments
/** @var /common/models/Card keyCard */
$this->keyCard = Card::find()->innerJoin('card_key_assignment','card.id_card = card_key_assignment.id_card')->andWhere('card_key_assignment.id_key = ' .$this->keyModel->id_key)->one();
$assignments = CardKeyAssignment::find()->andWhere(['id_key' => $this->keyModel->id_key])->all();
//if assignment found - we will unassign it
if ( count($assignments) > 0){
if ( isset( $this->keyCard ) ){
$this->unassign();
$this->action = 'unassign';
Log::log([
'type' =>Log::$TYPE_KEY_ASSIGN,
'message' => 'Kulcs visszaadás - Kártya/Kulcs/Vendég:' .$this->keyCard->number ."/" .$this->keyModel->number . "/".$this->keyCard->customer->name,
'id_key' => $this->card->id_card,
'id_customer' => $this->keyCard->customer->id_customer
]);
DoorLog::mkDoorLog(-1,$this->keyCard,$this->keyCard->customer,$this->keyModel );
CardKeyAssignment::deleteAll(['id_key' => $this->keyModel->id_key]);
$this->keyCard->setFlagsHasKey(false);
$this->keyCard->save(false);
/** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'success', 'Kulcs visszaadva!' );
$this->action = 'unassign';
Log::log([
'type' =>Log::$TYPE_KEY_ASSIGN,
'message' => 'Kulcs visszaadás - Kártya/Kulcs/Vendég:' .$this->keyCard->number ."/" .$this->keyModel->number . "/".$this->keyCard->customer->name,
'id_key' => $this->card->id_card,
'id_customer' => $this->keyCard->customer->id_customer
]);
DoorLog::mkDoorLog(-1,$this->keyCard,$this->keyCard->customer,$this->keyModel );
}else{
/** @noinspection PhpUndefinedClassInspection */
\Yii::error("Key for assignment not found");
/** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'danger', 'Kulcs visszaadás hiba: kulcs nem található!' );
}
}else{
// if there is no assignment - assign it to the customer
$this->assign();
}
}else{
\Yii::$app->session->setFlash ( 'danger', 'Kulcs nem található!' );
/** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'danger', 'Kulcs nem található!' );
}
}
@@ -91,14 +109,17 @@ class KeyToggleForm extends Model
if ( isset($this->card) && isset($this->customer) ){
$assignments = CardKeyAssignment::find()->andWhere(['id_card' => $this->card->id_card])->all();
if ( count($assignments) > 0 ){
\Yii::$app->session->setFlash ( 'danger', 'A vendégnél egyszerre csak egy kulcs lehet' );
/** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'danger', 'A vendégnél egyszerre csak egy kulcs lehet' );
}else{
$assignment = new CardKeyAssignment();
$assignment->id_card = $this->card->id_card;
$assignment->id_key = $this->keyModel->id_key;
$assignment->id_user = \Yii::$app->user->id;
/** @noinspection PhpUndefinedClassInspection */
$assignment->id_user = \Yii::$app->user->id;
$assignment->save(false);
\Yii::$app->session->setFlash ( 'success', 'Kulcs kiadva!' );
/** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'success', 'Kulcs kiadva!' );
$this->card->setFlagsHasKey(true);
$this->card->save(false);
@@ -113,15 +134,10 @@ class KeyToggleForm extends Model
DoorLog::mkDoorLog(-2,$this->card,$this->customer,$this->keyModel );
}
}else{
\Yii::$app->session->setFlash ( 'danger', 'Nincs vendég kiválasztva vagy érvénytelen kártya!' );
/** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'danger', 'Nincs vendég kiválasztva vagy érvénytelen kártya!' );
}
}
public function unassign(){
CardKeyAssignment::deleteAll(['id_key' => $this->keyModel->id_key]);
$this->card->setFlagsHasKey(false);
$this->card->save(false);
\Yii::$app->session->setFlash ( 'success', 'Kulcs visszaadva!' );
}
}