File: /home/safarimaris/home/safarimaris/common/models/Order.php
<?php
namespace common\models;
use common\components\OrderStatus;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
/**
* This is the model class for table "order".
*
* @property integer $id
* @property integer $entityId
* @property integer $tourId
* @property integer $status
* @property double $totalPrice
* @property string $user
* @property string $email
* @property string $phone
* @property integer $cntPeople
* @property string $startDate
* @property string $duration
* @property integer $created_at
* @property integer $updated_at
* @property integer $currency
* @property string $comments
*
* @property Tour $tour
* @property Entity $entity
*/
class Order extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'order';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
// если вместо метки времени UNIX используется datetime:
'value' => new Expression('NOW()'),
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user', 'email', 'phone', 'cntPeople'], 'required'],
[['entityId', 'tourId', 'status', 'cntPeople', 'created_at', 'updated_at', 'duration'], 'integer'],
[['totalPrice'], 'number'],
[['startDate'], 'safe'],
[['comments'], 'string'],
['email', 'email'],
[['user', 'phone', 'currency'], 'string', 'max' => 255],
[['tourId'], 'exist', 'skipOnError' => true, 'targetClass' => Tour::className(), 'targetAttribute' => ['tourId' => 'id']],
[['entityId'], 'exist', 'skipOnError' => true, 'targetClass' => Entity::className(), 'targetAttribute' => ['entityId' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('model', 'ID'),
'entityId' => Yii::t('model', 'Entity ID'),
'tourId' => Yii::t('model', 'Tour ID'),
'status' => Yii::t('model', 'Status'),
'totalPrice' => Yii::t('model', 'Total Price'),
'user' => Yii::t('model', 'User'),
'email' => Yii::t('model', 'Email'),
'phone' => Yii::t('model', 'Phone'),
'cntPeople' => Yii::t('model', 'Cnt People'),
'startDate' => Yii::t('model', 'Start Date'),
'duration' => Yii::t('model', 'Duration'),
'created_at' => Yii::t('model', 'Created At'),
'updated_at' => Yii::t('model', 'Updated At'),
'currency' => Yii::t('model', 'Currency'),
'comments' => Yii::t('model', 'Comments'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTour()
{
return $this->hasOne(Tour::className(), ['id' => 'tourId']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEntity()
{
return $this->hasOne(Entity::className(), ['id' => 'entityId']);
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
$this->status = OrderStatus::STATUS_NEW;
if ($this->tour) {
$this->entityId = $this->tour->entityId;
$this->totalPrice = Yii::$app->currency->convert($this->tour->price, false);
$this->startDate = $this->tour->startDate;
$this->duration = $this->tour->duration;
}
$this->currency = Yii::$app->currency->get();
}
return true;
}
return false;
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
\Yii::$app->mailer
->compose('order', ['model' => $this])
->setTo(Yii::$app->params['orderEmail'])
->setSubject(Yii::t('app', 'New order #{n} on {boat}', ['n' => $this->id, 'boat' => $this->entity->name]))
->send();
}
}