guild/common/models/Document.php

139 lines
4.5 KiB
PHP
Raw Normal View History

2022-11-08 18:01:42 +03:00
<?php
namespace common\models;
2022-11-10 16:00:43 +03:00
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
2022-11-08 18:01:42 +03:00
/**
* This is the model class for table "document".
*
* @property int $id
* @property int $company_id
* @property int $contractor_company_id
* @property int $manager_id
* @property int $contractor_manager_id
2022-11-10 16:00:43 +03:00
* @property int $template_id
* @property string $title
* @property string $body
* @property string $created_at
* @property string $updated_at
2022-11-08 18:01:42 +03:00
*
* @property Company $company
* @property Company $contractorCompany
2022-12-01 14:11:29 +03:00
* @property CompanyManager $contractorManager
2022-11-10 16:00:43 +03:00
* @property DocumentTemplate $template
2022-12-01 14:11:29 +03:00
* @property CompanyManager $manager
2022-11-08 18:01:42 +03:00
*/
class Document extends \yii\db\ActiveRecord
{
2022-11-10 16:00:43 +03:00
const SCENARIO_UPDATE_DOCUMENT_BODY = 'update_document_body';
2022-11-16 14:24:52 +03:00
const SCENARIO_DOWNLOAD_DOCUMENT = 'download_document';
2022-11-10 16:00:43 +03:00
2022-11-08 18:01:42 +03:00
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'document';
}
2022-11-10 16:00:43 +03:00
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
2022-11-08 18:01:42 +03:00
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2022-12-01 14:11:29 +03:00
[['title', 'template_id'], 'required'],
2022-11-10 16:00:43 +03:00
[['company_id', 'contractor_company_id', 'manager_id', 'contractor_manager_id', 'template_id'], 'integer'],
[['body'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['title'], 'string', 'max' => 255],
2022-11-08 18:01:42 +03:00
[['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']],
[['contractor_company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['contractor_company_id' => 'id']],
2022-12-01 14:11:29 +03:00
[['contractor_manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => CompanyManager::className(), 'targetAttribute' => ['contractor_manager_id' => 'id']],
2022-11-10 16:00:43 +03:00
[['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentTemplate::className(), 'targetAttribute' => ['template_id' => 'id']],
2022-12-01 14:11:29 +03:00
[['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => CompanyManager::className(), 'targetAttribute' => ['manager_id' => 'id']],
2022-11-10 16:00:43 +03:00
['body', 'required', 'on' => self::SCENARIO_UPDATE_DOCUMENT_BODY],
2022-11-16 14:24:52 +03:00
['body', function ($attribute, $params) {
2022-12-01 14:11:29 +03:00
preg_match_all('/\${(\w+|№|№+w)}/', $this->$attribute,$out);
2022-11-16 14:24:52 +03:00
if (!empty($out[0])) {
$this->addError('body', 'В теле документа все переменные должны быть заменены!');
2022-11-16 14:24:52 +03:00
}
}, 'on' => self::SCENARIO_DOWNLOAD_DOCUMENT
],
2022-11-08 18:01:42 +03:00
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
2022-11-10 16:00:43 +03:00
'company_id' => 'Компания',
'contractor_company_id' => 'Компания контрагент',
'manager_id' => 'Менеджер',
'contractor_manager_id' => 'Менеджер контрагент',
'template_id' => 'Шаблон документа',
'title' => 'Название',
'body' => 'Тело документа',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
2022-11-08 18:01:42 +03:00
];
}
2022-11-10 16:00:43 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getTemplate()
{
return $this->hasOne(DocumentTemplate::className(), ['id' => 'template_id']);
}
2022-11-08 18:01:42 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getContractorCompany()
{
return $this->hasOne(Company::className(), ['id' => 'contractor_company_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getContractorManager()
{
2022-12-01 14:11:29 +03:00
return $this->hasOne(CompanyManager::className(), ['id' => 'contractor_manager_id']);
2022-11-08 18:01:42 +03:00
}
/**
* @return \yii\db\ActiveQuery
*/
public function getManager()
{
2022-12-01 14:11:29 +03:00
return $this->hasOne(CompanyManager::className(), ['id' => 'manager_id']);
2022-11-08 18:01:42 +03:00
}
}