guild/common/models/Document.php

249 lines
9.0 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\ActiveQuery;
2022-11-10 16:00:43 +03:00
use yii\db\Expression;
use yii\db\StaleObjectException;
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
* @property DocumentFieldValue[] $documentFieldsValue
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';
const SCENARIO_PARTICIPANTS_OF_THE_TRANSACTION = "contract_participants";
private $notRequiredFieldsSignature = [
'company_id' => '${company}',
'contractor_company_id' => '${contractor_company}',
'manager_id' => '${manager}',
'contractor_manager_id' => '${contractor_manager}',
];
private $fieldPattern = '/\${(№?\s*\w*|(\w*\s?)*)}/';
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()'),
],
];
}
/**
* @throws StaleObjectException
* @throws \Throwable
*/
public function beforeDelete()
{
$documentFieldsValue = $this->getDocumentFieldValues()->all();
if ($documentFieldsValue) {
foreach ($documentFieldsValue as $fieldValue){
$fieldValue->delete();
}
}
return parent::beforeDelete();
}
public function beforeSave($insert): bool
{
if (parent::beforeSave($insert)) {
$upAttributes = $this->getDirtyAttributes(['company_id', 'contractor_company_id', 'manager_id', 'contractor_manager_id', 'title']);
$oldAttributes = $this->oldAttributes;
//update dirty attributes in document body
if ($oldAttributes && $upAttributes) {
foreach ($upAttributes as $key => $value) {
if ($value != $oldAttributes[$key]) {
if ($key == 'company_id' || $key == 'contractor_company_id') {
$newValue = Company::getTitle($value);
$oldValue = $oldAttributes[$key] ? Company::getTitle($oldAttributes[$key]) : $key;
} elseif ($key == 'manager_id' || $key == 'contractor_manager_id') {
$newValue = CompanyManager::getName($value);
$oldValue = $oldAttributes[$key] ? CompanyManager::getName($oldAttributes[$key]) : $key;
} else {
$newValue = $this->title;
$oldValue = $oldAttributes[$key] ?? $key;
}
$this->body = str_replace($oldValue, $newValue, $this->body);
}
}
}
//deleting fields value
if ($this->isAttributeChanged('template_id', false)) {
$documentFieldsValue = $this->getDocumentFieldValues()->all();
foreach ($documentFieldsValue as $fieldValue){
$fieldValue->delete();
}
}
return true;
} else {
return false;
}
}
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'],
[['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],
['body', 'checkBlankFields', 'on' => self::SCENARIO_DOWNLOAD_DOCUMENT ],
[['company_id', 'contractor_company_id', 'manager_id', 'contractor_manager_id'], 'checkContractParticipants', 'skipOnEmpty'=> false, 'on' => self::SCENARIO_PARTICIPANTS_OF_THE_TRANSACTION],
[['company_id', 'contractor_company_id'], 'checkCompanies', 'skipOnEmpty'=> false, 'on' => self::SCENARIO_PARTICIPANTS_OF_THE_TRANSACTION],
[['contractor_manager_id', 'manager_id'], 'checkManagers', 'skipOnEmpty'=> false, 'on' => self::SCENARIO_PARTICIPANTS_OF_THE_TRANSACTION],
2022-11-08 18:01:42 +03:00
];
}
public function checkCompanies()
{
if ($this->company_id === $this->contractor_company_id && $this->company_id) {
$this->addError('company_id', 'Компании и компания контрагент должны быть различны ');
$this->addError('contractor_company_id', 'Компании и компания контрагент должны быть различны ');
}
}
public function checkManagers()
{
if ($this->manager_id && $this->manager->userCard->id == $this->contractorManager->userCard->id) {
$this->addError('manager_id', 'Менеджер и менеджер контрагент должны быть различными лицами');
$this->addError('contractor_manager_id', 'Менеджер и менеджер контрагент должны быть различны лицами');
}
}
public function checkBlankFields($attribute)
{
preg_match_all($this->fieldPattern, $this->$attribute,$out);
if (!empty($out[0])) {
$this->addError('body', 'В теле документа все переменные должны быть заменены!');
}
}
public function checkContractParticipants($attribute)
{
if (str_contains($this->body, $this->notRequiredFieldsSignature[$attribute])) {
$this->addError($attribute, 'Шаблоном требуется заполнить это поле');
}
}
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 ActiveQuery
2022-11-10 16:00:43 +03:00
*/
public function getTemplate()
{
return $this->hasOne(DocumentTemplate::className(), ['id' => 'template_id']);
}
2022-11-08 18:01:42 +03:00
/**
* @return ActiveQuery
2022-11-08 18:01:42 +03:00
*/
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
/**
* @return ActiveQuery
2022-11-08 18:01:42 +03:00
*/
public function getContractorCompany()
{
return $this->hasOne(Company::className(), ['id' => 'contractor_company_id']);
}
/**
* @return ActiveQuery
2022-11-08 18:01:42 +03:00
*/
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 ActiveQuery
2022-11-08 18:01:42 +03:00
*/
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
}
/**
* @return ActiveQuery
*/
public function getDocumentFieldValues(): ActiveQuery
{
return $this->hasMany(DocumentFieldValue::className(), ['document_id' => 'id']);
}
public function getBlankFields()
{
preg_match_all($this->fieldPattern, $this->body,$out);
return $out[0];
}
2022-11-08 18:01:42 +03:00
}