app/Customize/Repository/EstimateRepository.php line 41

Open in your IDE?
  1. <?php
  2. namespace Customize\Repository;
  3. use Customize\Entity\EstimateShipping;
  4. use Doctrine\ORM\NoResultException;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Doctrine\Persistence\ManagerRegistry as RegistryInterface;
  7. use Eccube\Doctrine\Query\Queries;
  8. use Eccube\Entity\Customer;
  9. use Customize\Entity\Master\EstimateStatus;
  10. use Eccube\Entity\Master\Sex;
  11. use Customize\Entity\Estimate;
  12. use Eccube\Entity\Payment;
  13. use Eccube\Repository\AbstractRepository;
  14. use Eccube\Util\StringUtil;
  15. /**
  16.  * EstimateRepository
  17.  *
  18.  * This class was generated by the Doctrine ORM. Add your own custom
  19.  * repository methods below.
  20.  */
  21. class EstimateRepository extends AbstractRepository
  22. {
  23.     /**
  24.      * @var Queries
  25.      */
  26.     protected $queries;
  27.     public const COLUMNS = [
  28.         'estimate' => 'e.name01''estimator' => 'e.id''estimate_shipping_id' => 's.id''purchase_product' => 'ei.product_name''quantity' => 'ei.quantity''payment_method' => 'e.payment_method''estimate_status' => 'e.EstimateStatus''purchase_price' => 'e.total''shipping_status' => 's.shipping_date''tracking_number' => 's.tracking_number''delivery' => 's.name01',
  29.     ];
  30.     /**
  31.      * EstimateRepository constructor.
  32.      *
  33.      * @param RegistryInterface $registry
  34.      * @param Queries $queries
  35.      */
  36.     public function __construct(RegistryInterface $registryQueries $queries)
  37.     {
  38.         parent::__construct($registryEstimate::class);
  39.         $this->queries $queries;
  40.     }
  41.     /**
  42.      * @param int $estimateId
  43.      * @param EstimateStatus $Status
  44.      */
  45.     public function changeStatus($estimateIdEstimateStatus $Status)
  46.     {
  47.         $Estimate $this
  48.             ->find($estimateId)
  49.             ->setEstimateStatus($Status)
  50.         ;
  51.         switch ($Status->getId()) {
  52.             case '6'// 入金済へ
  53.                 $Estimate->setPaymentDate(new \DateTime());
  54.                 break;
  55.         }
  56.         $em $this->getEntityManager();
  57.         $em->persist($Estimate);
  58.         $em->flush();
  59.     }
  60.     /**
  61.      * @param array{
  62.      *         estimate_id?:string|int,
  63.      *         estimate_no?:string,
  64.      *         estimate_id_start?:string|int,
  65.      *         estimate_id_end?:string|int,
  66.      *         multi?:string|int|null,
  67.      *         status?:EstimateStatus[]|int[],
  68.      *         company_name?:string,
  69.      *         name?:string,
  70.      *         kana?:string,
  71.      *         email?:string,
  72.      *         phone_number?:string,
  73.      *         sex?:Sex[],
  74.      *         payment?:Payment[],
  75.      *         estimate_datetime_start?:\DateTime,
  76.      *         estimate_datetime_end?:\DateTime,
  77.      *         estimate_date_start?:\DateTime,
  78.      *         estimate_date_end?:\DateTime,
  79.      *         payment_datetime_start?:\DateTime,
  80.      *         payment_datetime_end?:\DateTime,
  81.      *         payment_date_start?:\DateTime,
  82.      *         payment_date_end?:\DateTime,
  83.      *         update_datetime_start?:\DateTime,
  84.      *         update_datetime_end?:\DateTime,
  85.      *         update_date_start?:\DateTime,
  86.      *         update_date_end?:\DateTime,
  87.      *         payment_total_start?:string|int,
  88.      *         payment_total_end?:string|int,
  89.      *         payment_product_name?:string,
  90.      *         shipping_mail?:Shipping::SHIPPING_MAIL_UNSENT|Shipping::SHIPPING_MAIL_SENT,
  91.      *         tracking_number?:string,
  92.      *         shipping_delivery_datetime_start?:\DateTime,
  93.      *         shipping_delivery_datetime_end?:\DateTime,
  94.      *         shipping_delivery_date_start?:\DateTime,
  95.      *         shipping_delivery_date_end?:\DateTime,
  96.      *         sortkey?:string,
  97.      *         sorttype?:string
  98.      *     } $searchData
  99.      *
  100.      * @return QueryBuilder
  101.      */
  102.     public function getQueryBuilderBySearchDataForAdmin($searchData)
  103.     {
  104.         $qb $this->createQueryBuilder('e')
  105.             ->select('e, s')
  106.             ->addSelect('ei''pref')
  107.             ->leftJoin('e.EstimateItems''ei')
  108.             ->leftJoin('e.Pref''pref')
  109.             ->innerJoin('e.EstimateShippings''s');
  110.         // estimate_id_start
  111.         if (isset($searchData['estimate_id']) && StringUtil::isNotBlank($searchData['estimate_id'])) {
  112.             $qb
  113.                 ->andWhere('e.id = :estimate_id')
  114.                 ->setParameter('estimate_id'$searchData['estimate_id']);
  115.         }
  116.         // estimate_no
  117.         if (isset($searchData['estimate_no']) && StringUtil::isNotBlank($searchData['estimate_no'])) {
  118.             $qb
  119.                 ->andWhere('e.estimate_no = :estimate_no')
  120.                 ->setParameter('estimate_no'$searchData['estimate_no']);
  121.         }
  122.         // sticker_code
  123.         if (isset($searchData['sticker_code']) && StringUtil::isNotBlank($searchData['sticker_code'])) {
  124.             $qb
  125.                 ->andWhere('ei.sticker_code LIKE :sticker_code')
  126.                 ->setParameter('sticker_code''%'.$searchData['sticker_code'].'%');
  127.         }
  128.         // estimate_id_start
  129.         if (isset($searchData['estimate_id_start']) && StringUtil::isNotBlank($searchData['estimate_id_start'])) {
  130.             $qb
  131.                 ->andWhere('e.id >= :estimate_id_start')
  132.                 ->setParameter('estimate_id_start'$searchData['estimate_id_start']);
  133.         }
  134.         // multi
  135.         if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
  136.             // スペース除去
  137.             $clean_key_multi preg_replace('/\s+|[ ]+/u'''$searchData['multi']);
  138.             $multi preg_match('/^\d{0,10}$/'$clean_key_multi) ? $clean_key_multi null;
  139.             if ($multi && $multi '2147483647' && $this->isPostgreSQL()) {
  140.                 $multi null;
  141.             }
  142.             $qb
  143.                 ->andWhere('e.id = :multi OR CONCAT(e.name01, e.name02) LIKE :likemulti OR '.
  144.                     "CONCAT(COALESCE(e.kana01, ''), COALESCE(e.kana02, '')) LIKE :likemulti OR e.company_name LIKE :multi_company_name OR ".
  145.                     'e.estimate_no LIKE :likemulti OR e.email LIKE :likemulti OR e.phone_number LIKE :likemulti')
  146.                 ->setParameter('multi'$multi)
  147.                 ->setParameter('likemulti''%'.$clean_key_multi.'%')
  148.                 ->setParameter('multi_company_name''%'.$searchData['multi'].'%'); // 会社名はスペースを除去せず検索
  149.         }
  150.         // estimate_id_end
  151.         if (isset($searchData['estimate_id_end']) && StringUtil::isNotBlank($searchData['estimate_id_end'])) {
  152.             $qb
  153.                 ->andWhere('e.id <= :estimate_id_end')
  154.                 ->setParameter('estimate_id_end'$searchData['estimate_id_end']);
  155.         }
  156.         // status
  157.         $filterStatus false;
  158.         if (!empty($searchData['status']) && count($searchData['status'])) {
  159.             $qb
  160.                 ->andWhere($qb->expr()->in('e.EstimateStatus'':status'))
  161.                 ->setParameter('status'$searchData['status']);
  162.             $filterStatus true;
  163.         }
  164.         if (!$filterStatus) {
  165.             // 購入処理中, 決済処理中は検索対象から除外
  166.             $qb->andWhere($qb->expr()->notIn('e.EstimateStatus'':status'))
  167.                 ->setParameter('status', [EstimateStatus::APPROVAL_PENDING]);
  168.         }
  169.         // company_name
  170.         if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) {
  171.             $qb
  172.                 ->andWhere('e.company_name LIKE :company_name')
  173.                 ->setParameter('company_name''%'.$searchData['company_name'].'%');
  174.         }
  175.         // name
  176.         if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
  177.             $clean_name preg_replace('/\s+|[ ]+/u'''$searchData['name']);
  178.             $qb
  179.                 ->andWhere('CONCAT(e.name01, e.name02) LIKE :name')
  180.                 ->setParameter('name''%'.$clean_name.'%');
  181.         }
  182.         // kana
  183.         if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
  184.             $clean_kana preg_replace('/\s+|[ ]+/u'''$searchData['kana']);
  185.             $qb
  186.                 ->andWhere("CONCAT(COALESCE(e.kana01, ''), COALESCE(e.kana02, '')) LIKE :kana")
  187.                 ->setParameter('kana''%'.$clean_kana.'%');
  188.         }
  189.         // email
  190.         if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
  191.             $qb
  192.                 ->andWhere('e.email like :email')
  193.                 ->setParameter('email''%'.$searchData['email'].'%');
  194.         }
  195.         // tel
  196.         if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
  197.             $tel preg_replace('/[^0-9]/'''$searchData['phone_number']);
  198.             $qb
  199.                 ->andWhere('e.phone_number LIKE :phone_number')
  200.                 ->setParameter('phone_number''%'.$tel.'%');
  201.         }
  202.         // sex
  203.         if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
  204.             $qb
  205.                 ->andWhere($qb->expr()->in('e.Sex'':sex'))
  206.                 ->setParameter('sex'$searchData['sex']->toArray());
  207.         }
  208.         // payment
  209.         if (!empty($searchData['payment']) && count($searchData['payment'])) {
  210.             $payments = [];
  211.             foreach ($searchData['payment'] as $payment) {
  212.                 $payments[] = $payment->getId();
  213.             }
  214.             $qb
  215.                 ->leftJoin('e.Payment''p')
  216.                 ->andWhere($qb->expr()->in('p.id'':payments'))
  217.                 ->setParameter('payments'$payments);
  218.         }
  219.         // oreder_date
  220.         if (!empty($searchData['estimate_datetime_start']) && $searchData['estimate_datetime_start']) {
  221.             $date $searchData['estimate_datetime_start'];
  222.             $qb
  223.                 ->andWhere('e.estimate_date >= :estimate_date_start')
  224.                 ->setParameter('estimate_date_start'$date);
  225.         } elseif (!empty($searchData['estimate_date_start']) && $searchData['estimate_date_start']) {
  226.             $date $searchData['estimate_date_start'];
  227.             $qb
  228.                 ->andWhere('e.estimate_date >= :estimate_date_start')
  229.                 ->setParameter('estimate_date_start'$date);
  230.         }
  231.         if (!empty($searchData['estimate_datetime_end']) && $searchData['estimate_datetime_end']) {
  232.             $date $searchData['estimate_datetime_end'];
  233.             $qb
  234.                 ->andWhere('e.estimate_date < :estimate_date_end')
  235.                 ->setParameter('estimate_date_end'$date);
  236.         } elseif (!empty($searchData['estimate_date_end']) && $searchData['estimate_date_end']) {
  237.             $date = clone $searchData['estimate_date_end'];
  238.             $date $date
  239.                 ->modify('+1 days');
  240.             $qb
  241.                 ->andWhere('e.estimate_date < :estimate_date_end')
  242.                 ->setParameter('estimate_date_end'$date);
  243.         }
  244.         // payment_date
  245.         if (!empty($searchData['payment_datetime_start']) && $searchData['payment_datetime_start']) {
  246.             $date $searchData['payment_datetime_start'];
  247.             $qb
  248.                 ->andWhere('e.payment_date >= :payment_date_start')
  249.                 ->setParameter('payment_date_start'$date);
  250.         } elseif (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
  251.             $date $searchData['payment_date_start'];
  252.             $qb
  253.                 ->andWhere('e.payment_date >= :payment_date_start')
  254.                 ->setParameter('payment_date_start'$date);
  255.         }
  256.         if (!empty($searchData['payment_datetime_end']) && $searchData['payment_datetime_end']) {
  257.             $date $searchData['payment_datetime_end'];
  258.             $qb
  259.                 ->andWhere('e.payment_date < :payment_date_end')
  260.                 ->setParameter('payment_date_end'$date);
  261.         } elseif (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
  262.             $date = clone $searchData['payment_date_end'];
  263.             $date $date
  264.                 ->modify('+1 days');
  265.             $qb
  266.                 ->andWhere('e.payment_date < :payment_date_end')
  267.                 ->setParameter('payment_date_end'$date);
  268.         }
  269.         // update_date
  270.         if (!empty($searchData['update_datetime_start']) && $searchData['update_datetime_start']) {
  271.             $date $searchData['update_datetime_start'];
  272.             $qb
  273.                 ->andWhere('e.update_date >= :update_date_start')
  274.                 ->setParameter('update_date_start'$date);
  275.         } elseif (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
  276.             $date $searchData['update_date_start'];
  277.             $qb
  278.                 ->andWhere('e.update_date >= :update_date_start')
  279.                 ->setParameter('update_date_start'$date);
  280.         }
  281.         if (!empty($searchData['update_datetime_end']) && $searchData['update_datetime_end']) {
  282.             $date $searchData['update_datetime_end'];
  283.             $qb
  284.                 ->andWhere('e.update_date < :update_date_end')
  285.                 ->setParameter('update_date_end'$date);
  286.         } elseif (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
  287.             $date = clone $searchData['update_date_end'];
  288.             $date $date
  289.                 ->modify('+1 days');
  290.             $qb
  291.                 ->andWhere('e.update_date < :update_date_end')
  292.                 ->setParameter('update_date_end'$date);
  293.         }
  294.         // payment_total
  295.         if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
  296.             $qb
  297.                 ->andWhere('e.payment_total >= :payment_total_start')
  298.                 ->setParameter('payment_total_start'$searchData['payment_total_start']);
  299.         }
  300.         if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
  301.             $qb
  302.                 ->andWhere('e.payment_total <= :payment_total_end')
  303.                 ->setParameter('payment_total_end'$searchData['payment_total_end']);
  304.         }
  305.         // buy_product_name
  306.         if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
  307.             $qb
  308.                 ->andWhere('ei.product_name LIKE :buy_product_name')
  309.                 ->setParameter('buy_product_name''%'.$searchData['buy_product_name'].'%');
  310.         }
  311.         // 発送メール送信/未送信.
  312.         if (isset($searchData['shipping_mail']) && $count count($searchData['shipping_mail'])) {
  313.             // 送信済/未送信両方にチェックされている場合は検索条件に追加しない
  314.             if ($count 2) {
  315.                 $checked current($searchData['shipping_mail']);
  316.                 if ($checked == EstimateShipping::SHIPPING_MAIL_UNSENT) {
  317.                     // 未送信
  318.                     $qb
  319.                         ->andWhere('s.mail_send_date IS NULL');
  320.                 } elseif ($checked == EstimateShipping::SHIPPING_MAIL_SENT) {
  321.                     // 送信
  322.                     $qb
  323.                         ->andWhere('s.mail_send_date IS NOT NULL');
  324.                 }
  325.             }
  326.         }
  327.         // 送り状番号.
  328.         if (!empty($searchData['tracking_number'])) {
  329.             $qb
  330.                 ->andWhere('s.tracking_number = :tracking_number')
  331.                 ->setParameter('tracking_number'$searchData['tracking_number']);
  332.         }
  333.         // お届け予定日(Shipping.delivery_date)
  334.         if (!empty($searchData['shipping_delivery_datetime_start']) && $searchData['shipping_delivery_datetime_start']) {
  335.             $date $searchData['shipping_delivery_datetime_start'];
  336.             $qb
  337.                 ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
  338.                 ->setParameter('shipping_delivery_date_start'$date);
  339.         } elseif (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
  340.             $date $searchData['shipping_delivery_date_start'];
  341.             $qb
  342.                 ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
  343.                 ->setParameter('shipping_delivery_date_start'$date);
  344.         }
  345.         if (!empty($searchData['shipping_delivery_datetime_end']) && $searchData['shipping_delivery_datetime_end']) {
  346.             $date $searchData['shipping_delivery_datetime_end'];
  347.             $qb
  348.                 ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
  349.                 ->setParameter('shipping_delivery_date_end'$date);
  350.         } elseif (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
  351.             $date = clone $searchData['shipping_delivery_date_end'];
  352.             $date $date
  353.                 ->modify('+1 days');
  354.             $qb
  355.                 ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
  356.                 ->setParameter('shipping_delivery_date_end'$date);
  357.         }
  358.         // Estimate By
  359.         if (isset($searchData['sortkey']) && !empty($searchData['sortkey'])) {
  360.             $sortEstimate = (isset($searchData['sorttype']) && $searchData['sorttype'] == 'a') ? 'ASC' 'DESC';
  361.             $qb->orderBy(self::COLUMNS[$searchData['sortkey']], $sortEstimate);
  362.             $qb->addOrderBy('e.update_date''DESC');
  363.             $qb->addOrderBy('e.id''DESC');
  364.         } else {
  365.             $qb->orderBy('e.update_date''DESC');
  366.             $qb->addOrderBy('e.id''DESC');
  367.         }
  368.         return $this->queries->customize(CustomQueryKey::ESTIMATE_SEARCH_ADMIN$qb$searchData);
  369.     }
  370.     /**
  371.      * @param  \Eccube\Entity\Customer $Customer
  372.      *
  373.      * @return QueryBuilder
  374.      */
  375.     public function getQueryBuilderByCustomer(Customer $Customer)
  376.     {
  377.         $qb $this->createQueryBuilder('e')
  378.             ->where('e.Customer = :Customer')
  379.             ->setParameter('Customer'$Customer);
  380.         // Order By
  381.         $qb->addOrderBy('e.id''DESC');
  382.         return $this->queries->customize(CustomQueryKey::ESTIMATE_SEARCH_BY_CUSTOMER$qb, ['customer' => $Customer]);
  383.     }
  384.     /**
  385.      * ステータスごとの受注件数を取得する.
  386.      *
  387.      * @param integer $EstimateStatusOrId
  388.      *
  389.      * @return int
  390.      *
  391.      * @throws \Doctrine\ORM\NoResultException
  392.      * @throws \Doctrine\ORM\NonUniqueResultException
  393.      */
  394.     public function countByEstimateStatus($EstimateStatusOrId)
  395.     {
  396.         return (int) $this->createQueryBuilder('e')
  397.             ->select('COALESCE(COUNT(e.id), 0)')
  398.             ->where('e.EstimateStatus = :EstimateStatus')
  399.             ->setParameter('EstimateStatus'$EstimateStatusOrId)
  400.             ->getQuery()
  401.             ->getSingleScalarResult();
  402.     }
  403.     /**
  404.      * 会員の購入金額, 購入回数, 初回購入日, 最終購入費を更新する
  405.      *
  406.      * @param Customer $Customer
  407.      * @param array $EstimateStatuses
  408.      */
  409.     public function updateEstimateSummary(Customer $Customer, array $EstimateStatuses = [EstimateStatus::NEW, EstimateStatus::IN_PROGRESSEstimateStatus::APPROVAL_PENDINGEstimateStatus::FIXED])
  410.     {
  411.         try {
  412.             $result $this->createQueryBuilder('e')
  413.                 ->select('COUNT(e.id) AS buy_times, SUM(e.total) AS buy_total, MIN(e.id) AS first_estimate_id, MAX(e.id) AS last_estimate_id')
  414.                 ->where('e.Customer = :Customer')
  415.                 ->andWhere('e.EstimateStatus in (:EstimateStatuses)')
  416.                 ->setParameter('Customer'$Customer)
  417.                 ->setParameter('EstimateStatuses'$EstimateStatuses)
  418.                 ->groupBy('e.Customer')
  419.                 ->getQuery()
  420.                 ->getSingleResult();
  421.         } catch (NoResultException $e) {
  422.             // 受注データが存在しなければ初期化
  423.             $Customer->setFirstBuyDate(null);
  424.             $Customer->setLastBuyDate(null);
  425.             $Customer->setBuyTimes(0);
  426.             $Customer->setBuyTotal(0);
  427.             return;
  428.         }
  429.         $FirstEstimate $this->find(['id' => $result['first_estimate_id']]);
  430.         $LastEstimate $this->find(['id' => $result['last_estimate_id']]);
  431.         $Customer->setBuyTimes($result['buy_times']);
  432.         $Customer->setBuyTotal($result['buy_total']);
  433.         $Customer->setFirstBuyDate($FirstEstimate->getEstimateDate());
  434.         $Customer->setLastBuyDate($LastEstimate->getEstimateDate());
  435.     }
  436.     /**
  437.      * 指定した顧客の有効な見積を取得する為のQueryBuilderを取得
  438.      *
  439.      * @param Customer $Customer
  440.      * @return QueryBuilder
  441.      */
  442.     public function getQueryValidEstimatesByCustomer(Customer $Customer)
  443.     {
  444.         global $kernel;
  445.         $config $kernel->getContainer()->getParameter('sticker');
  446.         // ステッカーの商品IDを取得
  447.         $sticker_product_ids = [];
  448.         foreach ($config['sticker_type'] as $sticker_type => $row) {
  449.             $sticker_product_ids[] = $row['product_id'];
  450.         }
  451.         // サブクエリ
  452.         // @NOTE dtb_estimate_itemからデータ不足なものを除外する為のサブクエリ
  453.         $subQb $this->getEntityManager()->createQueryBuilder();
  454.         $subQb
  455.             ->select('IDENTITY(tmp_ei.Estimate)')
  456.             ->from('Customize\Entity\EstimateItem''tmp_ei')
  457.             ->andWhere('tmp_ei.Product IN(:sticker_product_ids)')
  458.             ->andWhere(
  459.                 $subQb->expr()->orX(
  460.                     $subQb->expr()->lte('tmp_ei.price'0),
  461.                     $subQb->expr()->lte('tmp_ei.quantity'0),
  462.                     $subQb->expr()->isNull('tmp_ei.materials'),
  463.                     $subQb->expr()->isNull('tmp_ei.options'),
  464.                 )
  465.             )
  466.             ->groupBy('tmp_ei.Estimate')
  467.         ;
  468.         $qb $this->getQueryBuilderByCustomer($Customer);
  469.         $qb
  470.             ->andWhere($qb->expr()->notIn('e.id'$subQb->getDQL()))
  471.             ->setParameter(':sticker_product_ids'$sticker_product_ids)
  472.         ;
  473.         return $qb;
  474.     }
  475.     /**
  476.      * 指定した顧客の有効な見積を取得
  477.      *
  478.      * @param Customer $Customer
  479.      * @return
  480.      */
  481.     public function getValidEstimatesByCustomer(Customer $Customer)
  482.     {
  483.         $qb $this->getQueryValidEstimatesByCustomer($Customer);
  484.         return $qb->getQuery()->getResult();
  485.     }
  486. }