Your IP : 216.73.216.220


Current Path : /var/www/html/app/code/FlipsDigital/HappyMind/Observer/
Upload File :
Current File : /var/www/html/app/code/FlipsDigital/HappyMind/Observer/CustomerChildrenRegister.php

<?php
namespace FlipsDigital\HappyMind\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\Config\ScopeConfigInterface;

class CustomerChildrenRegister implements ObserverInterface
{
    protected $request;
    private $customerRepository;
    protected $addressFactory;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManagerInterface,
        \FlipsDigital\EducationInstitution\Model\EducationInstitutionFactory $educationInstitutionFactory,
        \FlipsDigital\TextbookList\Model\TextbookListFactory $textbookListFactory,
        \FlipsDigital\textbookListStudent\Model\TextbookListStudentFactory $textbookListStudentFactory,
        \FlipsDigital\CustomerTextbookList\Model\CustomerTextbookListFactory $customerTextbookListFactory,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\AddressFactory $addressFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
    )
    {
        $this->_objectManager = $objectManagerInterface;
        $this->request = $this->_objectManager->create(Http::class);
        $this->customerRepository = $customerRepository;
		$this->_eiFactory = $educationInstitutionFactory;
		$this->_tlFactory = $textbookListFactory;
		$this->_tlsFactory = $textbookListStudentFactory;
		$this->_ctlFactory = $customerTextbookListFactory;
        $this->addressFactory = $addressFactory;
        $this->timezone = $timezone;
    }
    public function execute(Observer $observer)
    {
        $event = $observer->getEvent();
        $objCustomer = $observer->getEvent()->getCustomer();
        $customerFormData = $this->request->getPost();
        if (isset($customerFormData['agree_privacy_policy'])){
            $customer = $this->customerRepository->getById($objCustomer->getId());
            $customer->setTaxvat("agree_privacy_policy:1");
            $this->customerRepository->save($customer);
        }
        if (
            isset($customerFormData['customer_phone']) && 
            !empty($customerFormData['customer_phone'])
        ){
            $customerId = $objCustomer->getId();
            $address = $this->addressFactory->create();
            $address->setCustomerId($customerId)
            ->setFirstname($objCustomer->getFirstname())
            ->setLastname($objCustomer->getLastname())
            ->setCountryId('HK')
            ->setCity('HK')
            ->setTelephone($customerFormData['customer_phone'])
            ->setStreet('HK')
            ->setIsDefaultBilling('1')
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1');
            $address->save();
        }
        if (
            isset($customerFormData['children']) &&
            count($customerFormData['children'])
        ){
            $childrenFormData = $customerFormData['children'];
            $objectManager = $this->_objectManager;
            $objEI = $this->_eiFactory->create();
            $collectionEI = $objEI->getCollection();
            $collectionEI->addAttributeToFilter('group_id', $childrenFormData['school_code']);
            if ($collectionEI->count()){
                $arrEI = $collectionEI->getFirstItem();
                $objCustomerTextbookList = $this->_ctlFactory->create();
                $_studentClass = ($childrenFormData['student_class'])?$childrenFormData['student_class']:"";
                $_studentNo = ($childrenFormData['student_no'])?$childrenFormData['student_no']:"";
                // $_studentFirstName = ($childrenFormData['chi_name'])?$childrenFormData['chi_name']:"";
                $_studentLastName = ($childrenFormData['eng_name'])?$childrenFormData['eng_name']:"";

                $arrData = [
                    'identifier' => $objCustomer->getId().'-'.$arrEI->getId().'-'.$_studentClass.'-'.$_studentNo.'-'.((int)($objCustomerTextbookList->getCollection()->count())+1),
                    'customer_id' => $objCustomer->getId(),
                    'education_institution_id' => $arrEI->getId(),
                    'student_class' => $_studentClass,
                    'student_no' => $_studentNo,
                    // 'chi_name' => $_studentFirstName,
                    'eng_name' => $_studentLastName,
                    'booklist_id' => "",
                    'booklist_student_id' => "",
                    'mers_stock_source_g_gl_student_id' => "",
                    'mers_stock_source_g_gl_student_gl_id' => "",
                    'mers_stock_source_g_gl_student_gl_group_id' => "",
                    'mers_stock_source_g_gl_student_gl_classname' => "",
                    'status' => 1,
                ];
                $objTL = $this->_tlFactory->create();
                $objTLS = $this->_tlsFactory->create();
                $collectionTL = $objTL->getCollection();
                $collectionTL->addAttributeToSelect('*');
                $collectionTL->addAttributeToFilter('education_institution_id', $arrEI->getId());
                $collectionTL->addAttributeToFilter('mers_stock_source_g_gl_magento_start_date', ['lteq' => $this->timezone->date()->format('Y-m-d H:i:s')]);
                $collectionTL->addAttributeToFilter('mers_stock_source_g_gl_magento_end_date', ['gteq' => $this->timezone->date()->format('Y-m-d H:i:s')]);
                $collectionTL->addAttributeToFilter('status', 1);
                if ($collectionTL->count()){
                    foreach ($collectionTL as $key => $value) {
                        $collectionTLS = $objTLS->getCollection();
                        $collectionTLS->addAttributeToSelect('*');
                        $collectionTLS->addAttributeToFilter('mers_stock_source_g_gl_student_gl_id', $value->getData('mers_stock_source_g_gl_id'));
                        $collectionTLS->addAttributeToFilter('mers_stock_source_g_gl_student_gl_group_id', $value->getData('mers_stock_source_g_gl_group_id'));
                        $collectionTLS->addAttributeToFilter('mers_stock_source_g_gl_student_student_class', $_studentClass);
                        $collectionTLS->addAttributeToFilter('mers_stock_source_g_gl_student_student_no', $_studentNo);
                        if ($collectionTLS->count()){
                            $arrTLS = $collectionTLS->getFirstItem();
                            $arrData['booklist_id'] = $value->getId();
                            $arrData['booklist_student_id'] = $arrTLS->getId();
                            $arrData['mers_stock_source_g_gl_student_id'] = $arrTLS->getData('mers_stock_source_g_gl_student_id');
                            $arrData['mers_stock_source_g_gl_student_gl_id'] = $arrTLS->getData('mers_stock_source_g_gl_student_gl_id');
                            $arrData['mers_stock_source_g_gl_student_gl_group_id'] = $arrTLS->getData('mers_stock_source_g_gl_student_gl_group_id');
                            $arrData['mers_stock_source_g_gl_student_gl_classname'] = $arrTLS->getData('mers_stock_source_g_gl_student_gl_classname');
                            $scopeConfig = $objectManager->create(ScopeConfigInterface::class);

                            $customer = $this->customerRepository->getById($objCustomer->getId());
                            $customer->setGroupId($scopeConfig->getValue('happymind/create_account/default_group'));
                            $this->customerRepository->save($customer);
                            break;
                        }
                    }
                }
                $objCustomerTextbookList->setData($arrData);
                $objCustomerTextbookList->save();
            }
        }
    }
}