97 lines
2.6 KiB
PHP
97 lines
2.6 KiB
PHP
<?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;
|
|
}
|
|
}
|
|
|
|
}
|