custom fields have been added to the documents
This commit is contained in:
@ -2,7 +2,6 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\Expression;
|
||||
|
||||
@ -104,4 +103,9 @@ class Company extends \yii\db\ActiveRecord
|
||||
{
|
||||
return $this->hasOne(Project::class, ['company_id' => 'id']);
|
||||
}
|
||||
|
||||
public static function getTitle($id)
|
||||
{
|
||||
return self::findOne($id)->name;
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,6 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "company_manager".
|
||||
*
|
||||
@ -71,4 +68,9 @@ class CompanyManager extends \yii\db\ActiveRecord
|
||||
{
|
||||
return self::find()->where(['company_id' => $company_id])->all();
|
||||
}
|
||||
|
||||
public static function getName($id)
|
||||
{
|
||||
return self::findOne($id)->userCard->fio;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,9 @@
|
||||
namespace common\models;
|
||||
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Expression;
|
||||
use yii\db\StaleObjectException;
|
||||
|
||||
/**
|
||||
* This is the model class for table "document".
|
||||
@ -24,11 +26,21 @@ use yii\db\Expression;
|
||||
* @property CompanyManager $contractorManager
|
||||
* @property DocumentTemplate $template
|
||||
* @property CompanyManager $manager
|
||||
* @property DocumentFieldValue[] $documentFieldsValue
|
||||
*/
|
||||
class Document extends \yii\db\ActiveRecord
|
||||
{
|
||||
const SCENARIO_UPDATE_DOCUMENT_BODY = 'update_document_body';
|
||||
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?)*)}/';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@ -50,6 +62,63 @@ class Document extends \yii\db\ActiveRecord
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -58,7 +127,6 @@ class Document extends \yii\db\ActiveRecord
|
||||
return [
|
||||
[['title', 'template_id'], 'required'],
|
||||
[['company_id', 'contractor_company_id', 'manager_id', 'contractor_manager_id', 'template_id'], 'integer'],
|
||||
[['body'], 'string'],
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
[['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']],
|
||||
@ -67,16 +135,44 @@ class Document extends \yii\db\ActiveRecord
|
||||
[['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentTemplate::className(), 'targetAttribute' => ['template_id' => 'id']],
|
||||
[['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => CompanyManager::className(), 'targetAttribute' => ['manager_id' => 'id']],
|
||||
['body', 'required', 'on' => self::SCENARIO_UPDATE_DOCUMENT_BODY],
|
||||
['body', function ($attribute, $params) {
|
||||
preg_match_all('/\${(\w+|№|№+w)}/', $this->$attribute,$out);
|
||||
if (!empty($out[0])) {
|
||||
$this->addError('body', 'В теле документа все переменные должны быть заменены!');
|
||||
}
|
||||
}, 'on' => self::SCENARIO_DOWNLOAD_DOCUMENT
|
||||
],
|
||||
['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],
|
||||
];
|
||||
}
|
||||
|
||||
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, 'Шаблоном требуется заполнить это поле');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -97,7 +193,7 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getTemplate()
|
||||
{
|
||||
@ -105,7 +201,7 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getCompany()
|
||||
{
|
||||
@ -113,7 +209,7 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getContractorCompany()
|
||||
{
|
||||
@ -121,7 +217,7 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getContractorManager()
|
||||
{
|
||||
@ -129,10 +225,24 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getManager()
|
||||
{
|
||||
return $this->hasOne(CompanyManager::className(), ['id' => 'manager_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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];
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
@ -29,6 +28,7 @@ class DocumentField extends \yii\db\ActiveRecord
|
||||
{
|
||||
return [
|
||||
[['title', 'field_template'], 'string', 'max' => 255],
|
||||
['field_template', 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
|
69
common/models/DocumentFieldValue.php
Normal file
69
common/models/DocumentFieldValue.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
/**
|
||||
* This is the model class for table "document_field_value".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $document_field_id
|
||||
* @property int $document_id
|
||||
* @property string $value
|
||||
*
|
||||
* @property Document $document
|
||||
* @property DocumentField $documentField
|
||||
*/
|
||||
class DocumentFieldValue extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'document_field_value';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['document_field_id', 'document_id', 'value'], 'required'],
|
||||
[['document_field_id', 'document_id'], 'integer'],
|
||||
[['value'], 'string', 'max' => 255],
|
||||
['value', 'unique', 'targetAttribute' => ['document_id', 'value'], 'message'=>'Значение каждого поля должно быть уникально в пределах документа'], //
|
||||
[['document_id'], 'exist', 'skipOnError' => true, 'targetClass' => Document::className(), 'targetAttribute' => ['document_id' => 'id']],
|
||||
[['document_field_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentField::className(), 'targetAttribute' => ['document_field_id' => 'id']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'document_field_id' => 'Поле',
|
||||
'document_id' => 'Документ',
|
||||
'value' => 'Значение',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getDocument()
|
||||
{
|
||||
return $this->hasOne(Document::className(), ['id' => 'document_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getDocumentField()
|
||||
{
|
||||
return $this->hasOne(DocumentField::className(), ['id' => 'document_field_id']);
|
||||
}
|
||||
}
|
@ -18,6 +18,8 @@ use yii\db\Expression;
|
||||
*/
|
||||
class DocumentTemplate extends \yii\db\ActiveRecord
|
||||
{
|
||||
private $fieldPattern = '/\${(№?\s*\w*|(\w*\s?)*)}/';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -66,4 +68,10 @@ class DocumentTemplate extends \yii\db\ActiveRecord
|
||||
'updated_at' => 'Updated At',
|
||||
];
|
||||
}
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
preg_match_all($this->fieldPattern, $this->template_body,$out);
|
||||
return $out[0];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user