HEX
Server: LiteSpeed
System: Linux premium260.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: aliazzsr (627)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //home/aliazzsr/api.crm.vqode.com/models/core/CompanyContact.php
<?php
namespace app\models\core;

use app\components\BaseModel;
use app\models\enums\CompanyContactRelType;
use yii\behaviors\AttributeTypecastBehavior;
use yii\db\Expression;
use yii\db\Query;

/**
 * Class CompanyContact
 * @package app\models\core
 * @property integer $reltype_id
 */
class CompanyContact extends BaseModel
{
    public $countClosedProjects;
    public $countOpenProjects;

    public static function tableName()
    {
        return 'company_contact';
    }

    public function rules()
    {
        return [
            [['name', 'surname', 'job_title', 'company_id',], 'required'],
            [['name', 'surname', 'job_title', 'salutation', 'mobile', 'phone', 'notes',], 'string', 'max' => 255],
            [['email', ], 'email'],
            [['company_id',], 'integer'],
            [['company_id'], 'exist', 'targetClass' => 'app\models\core\Company', 'targetAttribute' => 'id'],
            [['influencer_id'], 'exist', 'targetClass' => 'app\models\core\Influencer', 'targetAttribute' => 'id'],
            [['reltype_id'], 'default', 'value' => CompanyContactRelType::RELATED],
            [['reltype_id'], 'in', 'range' => CompanyContactRelType::getConstantsByName()],
            [
                [
                    'name', 'surname', 'job_title', 'salutation',
                    'email', 'mobile', 'phone', 'company_id', 'notes',
                    'influencer_id', 'linkedin',
                ],
                'safe'
            ],
        ];
    }

    public static $extraFields = [
        'company',
        'influencer',
    ];

    public static function findWithJoin()
    {
        return parent::find()
            ->joinWith([
                'company as company',
                'influencer as influencer'
            ]);
    }

    public function attributeLabels()
    {
        return [
            'company_id' => 'Company',
            'influencer_id' => 'Influencer',
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = [
            'linkedinTypecast' => [
                'class' => AttributeTypecastBehavior::className(),
                'attributeTypes' => [
                    'linkedin' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                    'countOpenProjects' => AttributeTypecastBehavior::TYPE_INTEGER,
                    'countClosedProjects' => AttributeTypecastBehavior::TYPE_INTEGER,
                ],
                'typecastAfterValidate' => true,
                'typecastBeforeSave' => true,
                'typecastAfterFind' => true,
            ],
        ];

        return array_merge(parent::behaviors(), $behaviors);
    }

    public static function getAdditionalFields()
    {
        return [
            'countClosedProjects' => self::getCountClosedProjects(),
            'countOpenProjects' => self::getCountOpenProjects(),
        ];
    }

    protected static function getCountClosedProjects()
    {
        $subQuery = new Query();

        $subQuery->select('count(*)')
            ->from(['cp' => ProjectContact::tableName()])
            ->innerJoin(Project::tableName() . ' as p', 'p.id = cp.project_id')
            ->innerJoin(Stage::tableName() . ' as s', 's.id = p.stage_id')
            ->where(['cp.contact_id' => new Expression(self::tableName() . '.id')])
            ->andWhere(['s.active' => false]);

        return $subQuery;
    }

    protected static function getCountOpenProjects()
    {
        $subQuery = new Query();

        $subQuery->select('count(*)')
            ->from(['cp' => ProjectContact::tableName()])
            ->innerJoin(Project::tableName() . ' as p', 'p.id = cp.project_id')
            ->innerJoin(Stage::tableName() . ' as s', 's.id = p.stage_id')
            ->where(['cp.contact_id' => new Expression(self::tableName() . '.id')])
            ->andWhere(['s.active' => true]);

        return $subQuery;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCompany()
    {
        return $this->hasOne(Company::className(), ['id' => 'company_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getInfluencer()
    {
        return $this->hasOne(Influencer::className(), ['id' => 'influencer_id']);
    }

    public function beforeDelete()
    {
        if (!parent::beforeDelete()) {
            return false;
        }

        $isValid = true;

        try {
            ProjectContact::deleteAll(['contact_id' => $this->id]);
        } catch (\Throwable $e) {
            $isValid = false;
        }

        return $isValid;
    }

    /**
     * @inheritdoc
     */
    public function beforeSave($insert)
    {
        if ($this->reltype_id === CompanyContactRelType::PRIMARY) {
            $primaryModel = static::getPrimaryCompanyContact($this->company_id);
            if ($primaryModel && $primaryModel->id !== $this->id) {
                $primaryModel->reltype_id = CompanyContactRelType::RELATED;
                $primaryModel->save();
            }
        }

        return parent::beforeSave($insert);
    }

    /**
     * Returns a primary contact for the company
     * @param integer $companyId
     * @return null|static
     */
    protected static function getPrimaryCompanyContact($companyId)
    {
        return static::findOne([
            'company_id' => $companyId,
            'reltype_id' => CompanyContactRelType::PRIMARY,
        ]);
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_DELETE,
        ];
    }
}
/**
 * @SWG\Definition(
 *     definition="CompanyContact",
 *     type="object",
 *     description="CompanyContact model",
 *     allOf={
 *       @SWG\Schema(ref="#/definitions/CompanyContact"),
 *       @SWG\Schema(
 *           required={"name","surname","job_title","company_id"},
 *           @SWG\Property(property="id", type="integer", example=7),
 *           @SWG\Property(property="company_id", type="integer", example=31),
 *           @SWG\Property(property="name", type="string", example="Kevin"),
 *           @SWG\Property(property="surname", type="string", example="Smith"),
 *           @SWG\Property(property="salutation", type="string", example="Mr Smith"),
 *           @SWG\Property(property="job_title", type="string", example="Operations Director"),
 *           @SWG\Property(property="email", type="string"),
 *           @SWG\Property(property="mobile", type="string"),
 *           @SWG\Property(property="phone", type="string"),
 *           @SWG\Property(property="influencer_id", type="integer", example=2, description="Influencer identifier"),
 *           @SWG\Property(property="linkedin", type="boolean", example=true, description="LinkedIn"),

 *       )
 *     }
 * )
 */