app/Customize/Service/CustomerService.php line 393

Open in your IDE?
  1. <?php
  2. /**
  3.  * @version EC=CUBE4
  4.  * @copyright 株式会社 翔 kakeru.co.jp
  5.  * @author
  6.  * 2021年12月20日作成
  7.  *
  8.  * app\Customize\Service\CustomerService.php
  9.  *
  10.  * LandMark 用
  11.  *
  12.  *
  13.  *                               C= C= C= ┌(;・_・)┘トコトコ
  14.  ******************************************************/
  15. namespace Customize\Service;
  16. use Doctrine\ORM\EntityManager;
  17. use Doctrine\ORM\QueryBuilder;
  18. use Eccube\Entity\Customer;
  19. use Eccube\Entity\CustomerAddress;
  20. use Eccube\Entity\Master\CustomerStatus;
  21. use Eccube\Entity\Master\Pref;
  22. use Eccube\Entity\Shipping;
  23. use Eccube\Repository\CustomerRepository;
  24. use Lm\Entity\CustomerShippingAddress;
  25. use Symfony\Component\DependencyInjection\ContainerInterface;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  28. use Lm\Service\Db\SqlService;
  29. use Customize\Service\CommonService;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. class CustomerService
  32. {
  33.     const LM_Customer_Email 'LM_Customer_Email_';
  34.     const MYPAGE_UPDATE_SESSON 'MYPAGE_UPDATE_SESSON';
  35.     const MYPAGE_UPDATE_SESSON_MESSAGE 'MYPAGE_UPDATE_SESSON_MESSAGE';
  36.     const FIRSTLOGINFLG 'FIRSTLOGINFLG';
  37.     const AMAZONPAY_CUSTOMER_LOGIN_FLG 'AMAZONPAY_CUSTOMER_LOGIN_FLG';
  38.     const LM_CUSTOMER_MAdA = ['address3''fax''melmaga'];
  39.     const AmazonProfileKey 'amazon_pay_v2.profile';
  40.     protected $CustomerRepository;
  41.     protected $Container;
  42.     protected $Request;
  43.     protected $CommonService;
  44.     protected $Session;
  45.     protected $encoderFactory;
  46.     public function __construct(CustomerRepository $CustomerRepository
  47.         ContainerInterface                       $Container
  48.         RequestStack                             $RequestStack
  49.         SessionInterface                         $session
  50.         CommonService                            $CommonService
  51.         EncoderFactoryInterface                  $encoderFactory
  52.     )
  53.     {
  54.         $this->CustomerRepository $CustomerRepository;
  55.         $this->Container $Container;
  56.         $this->Request $RequestStack->getCurrentRequest();
  57.         $this->CommonService $CommonService;
  58.         $this->Session $session;
  59.         $this->encoderFactory $encoderFactory;
  60.     }
  61.     /**
  62.      * 2021/12/16 kakeru
  63.      * ランドマーク customerの呼び出し
  64.      *
  65.      * @param $Email username
  66.      */
  67.     public function GetLandMarkCustomer($Email$Pass null)
  68.     {
  69.         $Sql = new SqlService();
  70.         $Pass $Pass ?? $this->Request->get('login_pass');
  71.         $Customer $Sql->Table('customer_table')
  72.             ->Set('customer_mail'$Email)
  73.             ->Set('customer_ddate'null)
  74.             ->Where('customer_password = SHA1( :Pass )')
  75.             ->Param(':Pass'$Pass)
  76.             ->Find();
  77.         return $this->SetLmCustomerTel($Customer);
  78.     }
  79.     /**
  80.      * 2021/12/16 kakeru
  81.      * ランドマーク customerの呼び出し
  82.      *
  83.      * @param $Email username
  84.      */
  85.     public function GetLandMarkCustomerEmail($Email)
  86.     {
  87.         $Sql = new SqlService();
  88.         $Customer $Sql->Table('customer_table')
  89.             ->Set('customer_mail'$Email)
  90.             ->Set('customer_ddate'null)
  91.             ->Find();
  92.         return $this->SetLmCustomerTel($Customer);;
  93.     }
  94.     /**
  95.      * 2021/12/23 kakeru
  96.      * 新規会員登録で ランドマーク側の Email チェック
  97.      *
  98.      * @param $Email string
  99.      */
  100.     public function CheckLanEmail($Email)
  101.     {
  102.         $em $this->Container->get('doctrine.orm.entity_manager');
  103.         $Qb $em->createQueryBuilder();
  104.         $Customer $Qb
  105.             ->select(['C'])
  106.             ->from("Eccube\\Entity\\Customer"'C')
  107.             ->where('C.email= :Email')
  108.             ->setParameter('Email'$Email)
  109.             ->getQuery()
  110.             ->getResult();
  111.         if ($Customer[0] ?? null) {
  112.             return null;
  113.         }
  114.         return $this->GetLandMarkCustomerEmail($Email);
  115.     }
  116.     /**
  117.      * 2021/12/23 kakeru
  118.      * ランドマーク customerの呼び出し
  119.      *
  120.      * @param $Email username
  121.      */
  122.     public function GetLandMarkCustomerId($Id$Flg false)
  123.     {
  124.         $Sql = new SqlService();
  125.         $Customer $Sql->Table('customer_table')
  126.             ->Set('customer_id'$Id)
  127.             //                       ->Set('customer_ddate',null)
  128.             ->Find();
  129.         if ($Flg) {
  130.             return $Customer;
  131.         }
  132.         return $this->SetLmCustomerTel($Customer);
  133.     }
  134.     /**
  135.      * 2021/12/16 kakeru
  136.      * dtb_customer の登録
  137.      *
  138.      * @param $Emai string ,
  139.      * @param $LmCustomer array()
  140.      */
  141.     public function SetCustomer($Customer$LmCustomer$Flg true)
  142.     {
  143.         $AddFlg false;
  144.         if (null === $Customer) {
  145.             // FIXME: Revert Me!!
  146.             try {
  147.                 throw new \Exception('[LM]CustomerService::SetCustomer()');
  148.             } catch (\Exception $e) {
  149.                 log_warning($e->getMessage(), [
  150.                     '$Customer' => $Customer,
  151.                     '$LmCustomer' => $LmCustomer,
  152.                     '$Flg' => $Flg,
  153.                     '$_SERVER' => $_SERVER,
  154.                     '$_POST' => $_POST,
  155.                     '$_GET' => $_GET,
  156.                     'trace' => explode(PHP_EOL$e->getTraceAsString()),
  157.                 ]);
  158.             }
  159.             $Customer = new Customer();
  160.             $AddFlg true;
  161.         }
  162.         $em $this->Container->get('doctrine.orm.entity_manager');
  163.         $CustomerStatus $em->find(CustomerStatus::class, CustomerStatus::REGULAR);
  164.         $Pref $em->find(Pref::class, $LmCustomer['customer_prefecture']);
  165.         $Customer->setStatus($CustomerStatus)
  166.             ->setName01($LmCustomer['customer_user_name'] ?? '')
  167.             ->setName02('')
  168.             ->setKana01($LmCustomer['customer_user_name_kana'] ?? '')
  169.             ->setKana02(null)
  170.             ->setCompanyName($LmCustomer['customer_company'])
  171.             ->setPostalCode($LmCustomer['customer_postcode'])
  172.             ->setAddr01($LmCustomer['customer_address1'])
  173.             ->setAddr02($LmCustomer['customer_address2'])
  174.             ->setEmail($LmCustomer['customer_mail'])
  175.             ->setPhoneNumber($LmCustomer['customer_tel'])
  176.             ->setCompanyKana($LmCustomer['customer_company_kana'])
  177.             ->setLastNameKana($LmCustomer['customer_last_name_kana'])
  178.             ->setMobileNumber($LmCustomer['customer_mobile_number'])
  179.             ->setUniformUsage($LmCustomer['customer_uniform_usage'])
  180.             ->setUniformUsageOther($LmCustomer['customer_uniform_usage_other'])
  181.             ->setUniformReplacementFrequency($LmCustomer['customer_uniform_replacement_frequency'])
  182.             ->setJobCategory($LmCustomer['customer_job_category'])
  183.             ->setJobSubcategory($LmCustomer['customer_job_subcategory'])
  184.             ->setCustomerType($LmCustomer['customer_type'])
  185.             ->setBirth(null)
  186.             ->setPassword('')
  187.             ->setSalt(null)
  188.             ->setSecretKey('')
  189. #                 ->setFirstBuyDate(null)
  190. #                 ->setLastBuyDate(null)
  191. #                 ->setBuyTimes(0)
  192. #                 ->setBuyTotal(0.00)
  193.             ->setNote($LmCustomer['customer_memo'])
  194.             #セキュリティ情報 パスワードリセットを行う
  195.             ->setSecretKey($this->CustomerRepository->getUniqueSecretKey())
  196.             ->setCreateDate($this->CommonService->SetEcTime($LmCustomer['customer_cdate']))
  197.             ->setUpdateDate($this->CommonService->SetEcTime($LmCustomer['customer_udate']))
  198.             ->setSex(null)
  199.             ->setJob(null)
  200.             ->setCountry(null)
  201.             ->setPref($Pref)
  202.             ->setPoint(0)
  203.             ->setLmCustomerId($LmCustomer['customer_id']);
  204.         #LMでログイン済の場合は リセットキーを発行する
  205.         if ($Flg) {
  206.             $RetKey $this->CustomerRepository->getUniqueResetKey();
  207.             $Customer->setResetKey($RetKey)
  208.                 ->setResetExpire(new \DateTime('+' $this->CommonService->GetConfig('eccube_customer_reset_expire') . ' min'));
  209.         }
  210.         $em->persist($Customer);
  211.         $em->flush($Customer);
  212.         #新規登録の時だけ 配送先情報を追加する
  213.         if ($AddFlg) {
  214.             $this->SetCustomerAdds($Customer$LmCustomer['customer_id']);
  215.         }
  216.         #LMでログイン済の場合は sessionに メールアドレスを覚えさせる
  217.         if ($Flg) {
  218.             $this->Session->set(self::LM_Customer_Email$Customer->getEmail());
  219.         }
  220.         return [$Customer, ($RetKey ?? null)];
  221.     }
  222.     /**
  223.      * 2021/12/16 kakeru
  224.      * 配送先の設定
  225.      *
  226.      * @param int $lmCustomerId
  227.      */
  228.     protected function SetCustomerAdds($Customer$lmCustomerId)
  229.     {
  230.         if (!($lmCustomer = new \Lm\Entity\Customer($lmCustomerId))) {
  231.             //
  232.             return;
  233.         }
  234.         /**
  235.          * @var EntityManager $em
  236.          */
  237.         $em $this->Container->get('doctrine.orm.entity_manager');
  238.         //
  239.         foreach ($lmCustomer->getCustomerShippingAddressList() as $lmCustomerShippingAddress) {
  240.             //
  241.             $CustomerAddress = (new CustomerAddress())
  242.                 ->setName01($lmCustomerShippingAddress->getCsaUserName() ?? '')
  243.                 ->setName02('')
  244.                 ->setCompanyName($lmCustomerShippingAddress->getCsaCompany())
  245.                 ->setPostalCode($lmCustomerShippingAddress->getCsaPostcode())
  246.                 ->setAddr01($lmCustomerShippingAddress->getCsaAddress1())
  247.                 ->setAddr02($lmCustomerShippingAddress->getCsaAddress2())
  248.                 ->setPhoneNumber($lmCustomerShippingAddress->getCsaTel())
  249.                 ->setCreateDate($this->CommonService->SetEcTime($lmCustomerShippingAddress->getAddDatetime()))
  250.                 ->setUpdateDate($this->CommonService->SetEcTime($lmCustomerShippingAddress->getUpdDatetime()))
  251.                 ->setCustomer($Customer)
  252.                 ->setLmCsaId($lmCustomerShippingAddress->getCsaId())
  253.                 ->setPref($em->find(Pref::class, $lmCustomerShippingAddress->getCsaPrefecture()))
  254.             ;
  255.             //
  256.             $em->persist($CustomerAddress);
  257.             $em->flush($CustomerAddress);
  258.             //
  259.             $lmCustomerShippingAddress
  260.                 ->setEcCustomerAddressId($CustomerAddress->getId())
  261.                 ->save()
  262.             ;
  263.         }
  264.     }
  265.     public function GetLmAddressId($CsaId)
  266.     {
  267.         $Sql = new SqlService();
  268.         $Add $Sql->Table('customer_shipping_address')
  269.             ->Set('csa_id'$CsaId)
  270.             ->Find();
  271.         return $this->SetLMCustomerAddressTel($Add);
  272.     }
  273.     /**
  274.      * LM電話番号からハイフンを取る
  275.      * @param array Customer
  276.      *
  277.      * @return $Customer
  278.      */
  279.     protected function SetLmCustomerTel($Customer)
  280.     {
  281.         if (!$Customer) {
  282.             return $Customer;
  283.         }
  284.         $Customer['customer_tel'] = str_replace('-'''$Customer['customer_tel']);
  285.         return $Customer;
  286.     }
  287.     /**
  288.      * LM電話番号からハイフンを取る
  289.      * @param array Customer
  290.      *
  291.      * @return $Customer
  292.      */
  293.     protected function SetLMCustomerAddressTel($Add)
  294.     {
  295.         if (!$Add) {
  296.             return $Add;
  297.         }
  298.         $Add['csa_tel'] = str_replace('-'''$Add['csa_tel']);
  299.         return $Add;
  300.     }
  301.     /**
  302.      * 2022/02/16
  303.      * 初回ログインの フラッグをセットする
  304.      * 0 or null 通常
  305.      * 1 パスワードnull
  306.      * 2 初回ログイン
  307.      *
  308.      * `@param int $Flg
  309.      *
  310.      */
  311.     public function SetFirstLoginFlg($Flg)
  312.     {
  313.         $SeveFlg $this->GetFirstLoginFlg() ?? -1;
  314.         if ($SeveFlg >= $Flg) {
  315.             return;
  316.         }
  317.         $this->Session->set(self::FIRSTLOGINFLG$Flg);
  318.         return;
  319.     }
  320.     /**
  321.      * 2022/02/16
  322.      * 初回ログインの フラッグを取得する
  323.      *
  324.      * `@return  int Flg
  325.      *
  326.      */
  327.     public function GetFirstLoginFlg()
  328.     {
  329.         return $this->Session->get(self::FIRSTLOGINFLG);
  330.     }
  331.     /**
  332.      * 2022/02/18
  333.      * アマゾン ペイ CUSTOMER Statuses
  334.      * 0  すでにログイン  している
  335.      * 1  アマゾンからの 戻り値ののメールアドレス で顧客を決定
  336.      * 2  アマゾンIDでの取得 でも取得
  337.      * 3  LM DATAでの取得
  338.      * 4  新規会員登録
  339.      *
  340.      *
  341.      *
  342.      * `@param int $Flg
  343.      *
  344.      */
  345.     public function SetAmazonLoginFlg($Flg)
  346.     {
  347.         $this->Session->set(self::AMAZONPAY_CUSTOMER_LOGIN_FLG$Flg);
  348.         return;
  349.     }
  350.     /**
  351.      * 2022/02/16
  352.      * 初回ログインの フラッグを取得する
  353.      *
  354.      * `@return  int Flg
  355.      *
  356.      */
  357.     public function GetAmazonLoginFlg()
  358.     {
  359.         //  return 3;
  360.         return $this->Session->get(self::AMAZONPAY_CUSTOMER_LOGIN_FLG);
  361.     }
  362.     public function GetAmazonAmazonProfileKey()
  363.     {
  364.         return unserialize($this->Session->get(self::AmazonProfileKey));
  365.     }
  366.     /**
  367.      * $LmCustomerId で EC-CUBEのCustomer を取得 及び例外処理
  368.      * @param int $LmCustomerId
  369.      *
  370.      * @return obje $Customer
  371.      */
  372.     public function GetCustomer($LmCustomerId)
  373.     {
  374.         if (!$Customer $this->CustomerRepository->findOneBy(['LmCustomerId' => $LmCustomerId'Status' => CustomerStatus::REGULAR])) {
  375.             $LmCustomer $this->GetLandMarkCustomerId($LmCustomerId);
  376.             list($Customer$key) = $this->SetCustomer($Customer$LmCustomerfalse);
  377.         }
  378.         return $Customer;
  379.     }
  380.     /**
  381.      *  パスワードの変更をする場合の必要DATAの取得
  382.      *
  383.      * @param obje $Customer
  384.      * @param str $password
  385.      *
  386.      * @return array
  387.      *  1 str $Password
  388.      *  2 str $salt
  389.      *  3 str $secretKey
  390.      */
  391.     public function SetPassword($Customer$Password)
  392.     {
  393.         $encoder $this->encoderFactory->getEncoder($Customer);
  394.         $salt $encoder->createSalt();
  395.         $Password $encoder->encodePassword($Password$salt);
  396.         $secretKey $this->CustomerRepository->getUniqueSecretKey();
  397.         return [$Password$salt$secretKey];
  398.     }
  399.     /**
  400.      * @param $customerAddressId
  401.      * @return array|null
  402.      * @throws \Doctrine\ORM\NoResultException
  403.      * @throws \Doctrine\ORM\NonUniqueResultException
  404.      */
  405.     public function getCustomerShippingAddressByCustomerAddressId($customerAddressId)
  406.     {
  407.         //
  408.         $customerShippingAddress null;
  409.         //
  410.         if ($CA $this->getCustomerAddressById($customerAddressId)) {
  411.             if ($csaId $CA->getLmCsaId()) {
  412.                 //
  413.                 $customerShippingAddress self::getCustomerShippingAddressById($csaId);
  414.             }
  415.         }
  416.         //
  417.         return $customerShippingAddress;
  418.     }
  419.     /**
  420.      * @param Shipping $shipping
  421.      * @param $fallbackCustomerWhenNotFound
  422.      * @return string|void
  423.      * @throws \Exception
  424.      */
  425.     public function getShippingAddr03(Shipping $shipping$fallbackCustomerWhenNotFound true)
  426.     {
  427.         // TODO: マジックナンバー “12”.
  428.         if (($payment $shipping->getOrder()->getPayment()) && $payment->getId() === 12) {
  429.             // Amazon Payの場合、Amazon Payから渡ってきた送付先情報のみを使用
  430.             return null;
  431.         } else if ($ecCustomerAddressId $shipping->getCustomerAddressId()) {
  432.             // 別送先の指定あり
  433.             if ($csa CustomerShippingAddress::getByEcCustomerAddressId($ecCustomerAddressId)) {
  434.                 //
  435.                 return $csa->getCsaAddress3();
  436.             }
  437.         } else if ($fallbackCustomerWhenNotFound) {
  438.             // 別送先の指定なし
  439.             if ($order $shipping->getOrder()) {
  440.                 if ($customer $order->getCustomer()) {
  441.                     if ($lmCustomer $customer->getLmCustomer()) {
  442.                         if ($address3 $lmCustomer->getCustomerAddress3()) {
  443.                             //
  444.                             return $address3;
  445.                         }
  446.                     }
  447.                 }
  448.             }
  449.         }
  450.     }
  451.     /**
  452.      * @param $customerAddressId
  453.      * @return CustomerAddress
  454.      * @throws \Doctrine\ORM\NoResultException
  455.      * @throws \Doctrine\ORM\NonUniqueResultException
  456.      */
  457.     public function getCustomerAddressById($customerAddressId)
  458.     {
  459.         //
  460.         $em $this->Container->get('doctrine.orm.entity_manager');
  461.         /**
  462.          * @var QueryBuilder $Qb
  463.          * @var CustomerAddress $CA
  464.          */
  465.         $Qb $em->createQueryBuilder();
  466.         $CA  $Qb
  467.             ->select('CA')
  468.             ->from("Eccube\\Entity\\CustomerAddress"'CA')
  469.             ->andWhere('CA.id = :id')
  470.             ->setParameters([
  471.                 'id' => $customerAddressId,
  472.             ])
  473.             ->getQuery()
  474.             ->getOneOrNullResult();
  475.         //
  476.         return $CA;
  477.     }
  478.     /**
  479.      * @param $csaId
  480.      * @return array|null
  481.      */
  482.     public static function getCustomerShippingAddressById($csaId)
  483.     {
  484.         return (new SqlService())
  485.             ->Table('customer_shipping_address')
  486.             ->Set('csa_id'$csaId)
  487.             ->Find()
  488.             ;
  489.     }
  490.     /**
  491.      * Get customer's company name filter
  492.      */
  493.     public function getNotAllowedCompanies()
  494.     {
  495.         $notAllowedCompanies = (new SqlService())
  496.             ->Sql('SELECT filter_customer_name FROM customer_name_filter_table')
  497.             ->Fetch();
  498.         if (empty($notAllowedCompanies['filter_customer_name'])) {
  499.             return [];
  500.         }
  501.         $notAllowedCompanies explode("\r\n"$notAllowedCompanies['filter_customer_name']);
  502.         $notAllowedCompanies array_map(function ($notAllowedCompany) {
  503.             return trim($notAllowedCompany);
  504.         }, $notAllowedCompanies);
  505.         $notAllowedCompanies array_filter($notAllowedCompanies, function ($notAllowedCompany) {
  506.             return !empty($notAllowedCompany);
  507.         });
  508.         return $notAllowedCompanies;
  509.     }
  510.     /**
  511.      * @param $value
  512.      * @param \Eccube\Entity\Customer|\Lm\Entity\Customer|null $user
  513.      * @return bool
  514.      * @throws \Exception
  515.      */
  516.     public function isValidCustomerBangou($value$user null)
  517.     {
  518.         //
  519.         if ($user === null) {
  520.             //
  521.             $user $this->CommonService->getUser();
  522.         }
  523.         //
  524.         if ($user instanceof \Eccube\Entity\Customer) {
  525.             //
  526.             $customer $user->getLmCustomer();
  527.         } else if ($user instanceof \Lm\Entity\Customer) {
  528.             //
  529.             $customer $user;
  530.         } else {
  531.             //
  532.             return false;
  533.         }
  534.         //
  535.         return (((int)$value) === $customer->getCustomerBangou());
  536.     }
  537. }