merge app api
This commit is contained in:
@@ -8,7 +8,7 @@ use common\models\Card;
|
||||
use common\models\Customer;
|
||||
use common\models\Product;
|
||||
use common\models\Transfer;
|
||||
use yii\base\Object;
|
||||
use yii\base\BaseObject;
|
||||
use common\models\Account;
|
||||
use common\models\Discount;
|
||||
use common\models\Currency;
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace frontend\models;
|
||||
|
||||
use common\models\DoorLog;
|
||||
use common\models\Log;
|
||||
use common\models\VirtualKey;
|
||||
use /** @noinspection PhpUndefinedClassInspection */
|
||||
yii\base\Model;
|
||||
use common\models\CardKeyAssignment;
|
||||
@@ -31,8 +32,8 @@ class KeyToggleForm extends Model
|
||||
public $action;
|
||||
|
||||
public $keyCard;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@@ -52,20 +53,24 @@ class KeyToggleForm extends Model
|
||||
];
|
||||
}
|
||||
|
||||
public function readKey($keyNumber){
|
||||
$query= Key::find();
|
||||
$fixedKeyNumber = Helper::fixAsciiChars($keyNumber);
|
||||
// add condition rfid key or number
|
||||
$query->andWhere(['or',
|
||||
['and',[ 'in','key.number' , [$fixedKeyNumber]],"trim(coalesce(key.number, '')) <>'' " ],
|
||||
['and', ['in','key.rfid_key' ,[ $fixedKeyNumber ] ],"trim(coalesce(key.rfid_key, '')) <>'' "],
|
||||
|
||||
]);
|
||||
return $query->one();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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();
|
||||
$this->keyModel = $this->readKey($this->key);
|
||||
//ha van ilyen kulcs
|
||||
if ( isset($this->keyModel) ){
|
||||
//find card - key assignments
|
||||
@@ -74,27 +79,7 @@ class KeyToggleForm extends Model
|
||||
$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 ) ){
|
||||
|
||||
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->keyModel->id_key,
|
||||
'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ó!' );
|
||||
}
|
||||
$this->revoke();
|
||||
}else{
|
||||
// if there is no assignment - assign it to the customer
|
||||
$this->assign();
|
||||
@@ -105,6 +90,27 @@ class KeyToggleForm extends Model
|
||||
}
|
||||
}
|
||||
|
||||
public function revoke(){
|
||||
if ( isset( $this->keyCard ) ){
|
||||
|
||||
CardKeyAssignment::deleteAll(['id_key' => $this->keyModel->id_key]);
|
||||
$this->keyCard->setFlagsHasKey(false);
|
||||
$this->keyCard->save(false);
|
||||
\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->keyModel->id_key,
|
||||
'id_customer' => $this->keyCard->customer->id_customer
|
||||
]);
|
||||
DoorLog::mkDoorLog(-1,$this->keyCard,$this->keyCard->customer,$this->keyModel );
|
||||
}else{
|
||||
\Yii::error("Key for assignment not found");
|
||||
\Yii::$app->session->setFlash ( 'danger', 'Kulcs visszaadás hiba: kulcs nem található!' );
|
||||
}
|
||||
}
|
||||
|
||||
public function assign(){
|
||||
if ( isset($this->card) && isset($this->customer) ){
|
||||
$assignments = CardKeyAssignment::find()->andWhere(['id_card' => $this->card->id_card])->all();
|
||||
@@ -117,6 +123,7 @@ class KeyToggleForm extends Model
|
||||
$assignment->id_key = $this->keyModel->id_key;
|
||||
/** @noinspection PhpUndefinedClassInspection */
|
||||
$assignment->id_user = \Yii::$app->user->id;
|
||||
$assignment->virtual_key = uniqid(null,true);
|
||||
$assignment->save(false);
|
||||
/** @noinspection PhpUndefinedClassInspection */
|
||||
\Yii::$app->session->setFlash ( 'success', 'Kulcs kiadva!' );
|
||||
@@ -132,12 +139,23 @@ class KeyToggleForm extends Model
|
||||
]);
|
||||
|
||||
DoorLog::mkDoorLog(-2,$this->card,$this->customer,$this->keyModel );
|
||||
|
||||
// delete all virtual key for this card
|
||||
VirtualKey::deleteAll(['id_card' => $this->card->id_card]);
|
||||
|
||||
// create a new virtual key
|
||||
$virtualKey = new VirtualKey();
|
||||
$virtualKey->id_card = $this->card->id_card;
|
||||
$virtualKey->id_key = $this->keyModel->id_key;
|
||||
$virtualKey->number = uniqid(null,true);
|
||||
|
||||
$virtualKey->save(false);
|
||||
}
|
||||
}else{
|
||||
/** @noinspection PhpUndefinedClassInspection */
|
||||
\Yii::$app->session->setFlash ( 'danger', 'Nincs vendég kiválasztva vagy érvénytelen kártya!' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
89
frontend/models/MobileDeviceSearch.php
Normal file
89
frontend/models/MobileDeviceSearch.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\MobileDevice;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\data\ActiveDataProvider;
|
||||
use yii\db\Query;
|
||||
|
||||
/**
|
||||
* EventSearch represents the model behind the search form about `common\models\Event`.
|
||||
*/
|
||||
class MobileDeviceSearch extends MobileDevice
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function scenarios()
|
||||
{
|
||||
// bypass scenarios() implementation in the parent class
|
||||
return Model::scenarios();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates data provider instance with search query applied
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return ActiveDataProvider
|
||||
*/
|
||||
public function search($params)
|
||||
{
|
||||
$query = new Query();
|
||||
|
||||
$query->select([
|
||||
'card.id_card as card_id',
|
||||
'card.number as card_number',
|
||||
'mobile_device.id as device_id',
|
||||
'mobile_device.device_name as device_name',
|
||||
'mobile_device.status as device_status',
|
||||
'mobile_device.created_at as device_created_at',
|
||||
]);
|
||||
|
||||
$query->from("mobile_device");
|
||||
$query->innerJoin('customer', 'customer.id_customer_card = mobile_device.id_card');
|
||||
$query->innerJoin('card', 'card.id_card = mobile_device.id_card');
|
||||
$query->andWhere(['card.id_card' => $this->id_card]);
|
||||
|
||||
$dataProvider = new ActiveDataProvider([
|
||||
'query' => $query,
|
||||
'sort' => [
|
||||
'defaultOrder' => [
|
||||
'device_created_at' => SORT_DESC
|
||||
],
|
||||
'attributes' => Helper::mkYiiSortItems([
|
||||
['device_created_at'],
|
||||
['device_status'],
|
||||
]),
|
||||
]
|
||||
]);
|
||||
|
||||
$this->load($params);
|
||||
|
||||
if (!$this->validate()) {
|
||||
// uncomment the following line if you do not want to return any records when validation fails
|
||||
// $query->where('0=1');
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
// $query->andFilterWhere([
|
||||
// 'event.id' => $this->id,
|
||||
// ]);
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
}
|
||||
96
frontend/models/MobileStatusForm.php
Normal file
96
frontend/models/MobileStatusForm.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\Card;
|
||||
use common\models\Customer;
|
||||
use common\models\Log;
|
||||
use common\models\MobileDevice;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* ContactForm is the model behind the contact form.
|
||||
*/
|
||||
class MobileStatusForm extends Model
|
||||
{
|
||||
public $id_device;
|
||||
public $status;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['id_device', 'status'], 'required'],
|
||||
];
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$tx = null;
|
||||
try {
|
||||
$tx = \Yii::$app->db->beginTransaction();
|
||||
|
||||
$device = MobileDevice::findOne(['id' => $this->id_device]);
|
||||
|
||||
if ($device === null) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$card = Card::findOne(['id_card' => $device->id_card]);
|
||||
|
||||
if ($card === null) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
\Yii::info("MobileStatusForm: changing status from $device->status to $this->status");
|
||||
|
||||
$origStatus = $device->status;
|
||||
$device->status = $this->status;
|
||||
|
||||
if ( $this->status === MobileDevice::STATUS_ACTIVE ){
|
||||
// only one card can be active at the same time
|
||||
MobileDevice::updateAll([
|
||||
'activated_at' => null,
|
||||
'status' => MobileDevice::STATUS_DELETED_SYSTEM
|
||||
],
|
||||
[
|
||||
'id_card' => $device->id_card
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->status === MobileDevice::STATUS_ACTIVE) {
|
||||
if (!isset($device->activated_at)) {
|
||||
$device->activated_at = Helper::getDateTimeString();
|
||||
}
|
||||
} else {
|
||||
$device->activated_at = null;
|
||||
}
|
||||
|
||||
$device->save(false);
|
||||
|
||||
$customer = Customer::find()->andWhere(['id_customer_card' => $device->id_card])->one();
|
||||
|
||||
Log::log([
|
||||
'type' => Log::$TYPE_MOBILE_DEVICE_STATUS,
|
||||
'message' => "Eszkösz $device->id; Kártya: $card->number; előző státusz: $origStatus; új státusz: $this->status",
|
||||
'id_customer' => $customer->id_customer
|
||||
]);
|
||||
$tx->commit();
|
||||
}catch ( \Exception $e){
|
||||
\Yii::error("Failed to save status for device $this->id_device :".$e->getMessage());
|
||||
if ( $tx != null ){
|
||||
$tx->rollBack();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use common\models\Card;
|
||||
use common\models\Customer;
|
||||
use common\models\Product;
|
||||
use common\models\Transfer;
|
||||
use yii\base\Object;
|
||||
use yii\base\BaseObject;
|
||||
use common\models\Account;
|
||||
use common\models\Discount;
|
||||
use common\models\Currency;
|
||||
|
||||
@@ -6,7 +6,7 @@ use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\data\ActiveDataProvider;
|
||||
use common\models\Transfer;
|
||||
use yii\base\Object;
|
||||
use yii\base\BaseObject;
|
||||
use yii\db\Query;
|
||||
use yii\db\Expression;
|
||||
use common\models\Account;
|
||||
|
||||
Reference in New Issue
Block a user