<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Repository\MemberRepository;
use Eccube\Repository\CustomerRepository;
use Eccube\Security\Core\Encoder\PasswordEncoder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception as HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Customize\Service\MailService;
use Customize\Service\CashbackService;
use Customize\Service\UsageFeeService;
use Customize\Repository\ChainStoreRepository;
use Plugin\Coupon4\Repository\CouponRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class CronJobController extends AbstractController
{
private $key = "e564c9a69921be4a9268be4df8bc3fa271437c28";
/**
* @var ChainStoreRepository
*/
protected $chainstoreRepository;
/**
* @var MemberRepository
*/
protected $memberRepository;
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* @var CouponRepository
*/
protected $couponRepository;
/**
* @var MailService
*/
protected $mailService;
/**
* @var CashbackService
*/
protected $cashbackService;
/**
* @var UsageFeeService
*/
protected $usageFeeService;
/**
* @var PasswordEncoder
*/
protected $passwordEncoder;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $entityManager;
/**
* BankController constructor.
*/
public function __construct(
ChainStoreRepository $chainstoreRepository,
MemberRepository $memberRepository,
CustomerRepository $customerRepository,
CouponRepository $couponRepository,
MailService $mailService,
CashbackService $cashbackService,
UsageFeeService $usageFeeService,
PasswordEncoder $passwordEncoder,
EntityManagerInterface $entityManager)
{
$this->chainstoreRepository = $chainstoreRepository;
$this->memberRepository = $memberRepository;
$this->customerRepository = $customerRepository;
$this->couponRepository = $couponRepository;
$this->mailService = $mailService;
$this->cashbackService = $cashbackService;
$this->usageFeeService = $usageFeeService;
$this->passwordEncoder = $passwordEncoder;
$this->entityManager = $entityManager;
}
/**
* check chainstore mail.
*
* @Route("/cronjob/check_chainstore_mail/{key}", name="check_chainstore_mail", methods={"GET"})
*
* @param Request $request
*
* @return JsonResponse
*/
public function checkChainStoreMail(Request $request, $key)
{
$result = [];
try {
if($this->key != $key){
$result[] = "Incorrect registry key for Access!!";
}else{
// タイムアウトを無効にする.
set_time_limit(0);
$CustomerList = $this->customerRepository->findBy(["Status" => 2]);
$MemberList = $this->memberRepository->findBy(["Work" => 1]);
//$MemberList = $this->memberRepository->findBy(["id" => 2]);
$DealerCodeList = [];
$StockNumberList = [];
$CouponCodeList = [];
foreach($CustomerList as $Customer){
$ChainStore = $Customer->getChainStore();
if(is_object($ChainStore)){
$ContractType = $ChainStore->getContractType();
$DealerCode = $ChainStore->getDealerCode();
if(!isset($DealerCode) || strlen($DealerCode) <= 1){
$DealerCodeList[] = $Customer;
}
$StockNumber = $ChainStore->getStockNumber();
if(!isset($StockNumber) || strlen($StockNumber) <= 1){
$StockNumberList[] = $Customer;
}
if($ContractType->getId() == 1 || $ContractType->getId() == 2){
$CouponList = $this->couponRepository->findBy(["ChainStore" => $ChainStore]);
if(!$CouponList){
$CouponCodeList[] = $Customer;
}
}
}
}
if(count($DealerCodeList) > 0 || count($CouponCodeList) > 0){
foreach($MemberList as $Member){
// チェック販売店会員メール送信
$this->mailService->sendCheckChainStoreMail($Member, $DealerCodeList, $StockNumberList, $CouponCodeList);
$result[] = $Member->getId();
}
}
}
} catch (\Exception $e) {
log_error('予期しないエラーです', [$e->getMessage()]);
return $this->json(['status' => $e->getMessage()], 500);
}
return $this->json(array_merge(['status' => 'OK', 'data' => $result], []));
}
/**
* Cashback 計算画面.
*
* @Route("/cronjob/calc_cashback/{key}", name="calc_cashback", methods={"GET", "POST"})
* @Template("Cashback/calcCashback.twig")
*/
public function calcCashback(Request $request, $key)
{
$YM = date('Y-m');
return $this->calcCashbackYM($request, $YM, $key);
}
/**
* Cashback 計算画面(指定年月).
*
* @Route("/cronjob/calc_cashback/{YM}/{key}", name="calc_cashback_ym", methods={"GET", "POST"})
* @Template("Cashback/calcCashback.twig")
*/
public function calcCashbackYM(Request $request, $YM, $key)
{
if($this->key != $key){
$error = "Incorrect registry key for Access!!";
return [
'Error' => $error
];
}else{
$CashbackResult = $this->cashbackService->calcCashback($YM);
return [
'CashbackResult' => $CashbackResult,
'Error' => null
];
}
}
/**
* 購入数による掛け率変動の対応を実装する。 計算画面.
*
* @Route("/cronjob/calc_reback_amount/{key}", name="calc_reback_amount", methods={"GET", "POST"})
* @Template("Cashback/calcRebackAmount.twig")
*/
public function calcRebackAmount(Request $request, $key)
{
$YM = date('Y-m');
return $this->calcRebackAmountYM($request, $YM, $key);
}
/**
* 購入数による掛け率変動の対応を実装する。 計算画面(指定年月).
*
* @Route("/cronjob/calc_reback_amount/{YM}/{key}", name="calc_reback_amount_ym", methods={"GET", "POST"})
* @Template("Cashback/calcRebackAmount.twig")
*/
public function calcRebackAmountYM(Request $request, $YM, $key)
{
if($this->key != $key){
$error = "Incorrect registry key for Access!!";
return [
'Error' => $error
];
}else{
$CashbackResult = $this->cashbackService->calcRebackAmount($YM);
return [
'CashbackResult' => $CashbackResult,
'Error' => null
];
}
}
/**
* 小売販売契約に対するブランド使用料は毎月定額15,000円となっている。(指定年月).
*
* @Route("/cronjob/calc_retail_usage_fee/{YM}/{key}", name="calc_retail_usage_fee_month", methods={"GET", "POST"})
* @Template("UsageFee/calcUsageFee.twig")
*/
public function calcRetailUsageFeeMonth(Request $request, $YM, $key)
{
if($this->key != $key){
$error = "Incorrect registry key for Access!!";
return [
'Error' => $error
];
}else{
// タイムアウトを無効にする.
set_time_limit(0);
$usageFeeResult = $this->usageFeeService->calcUsageFee($request, $this->formFactory, $YM);
$MemberList = $this->memberRepository->findBy(["Work" => 1]);
if($usageFeeResult["Error"] == null){
$RetailUsageList = $usageFeeResult["RetailUsageList"];
$hasError = $usageFeeResult["HasError"];
$RetailUsageErrList = [];
$RetailUsageGMOErrList = [];
$BankTransferCount = 0;
$GMOPaymentCount = 0;
foreach($RetailUsageList as $RetailUsage){
$Payment = $RetailUsage->getChainStore()->getAutoUsageFeePayment();
if($RetailUsage->getResult() != "OK"){
$RetailUsageErrList[] = $RetailUsage;
}else{
if($RetailUsage->getAuthorResult() != "OK" && $RetailUsage->getAuthorResult() != "" && $RetailUsage->getAuthorResult() != null){
$RetailUsageGMOErrList[] = $RetailUsage;
}else{
if($Payment->getId() == 3){
$BankTransferCount = $BankTransferCount + 1;
}else{
$GMOPaymentCount = $GMOPaymentCount + 1;
}
}
}
}
//if($hasError){
//if(count($RetailUsageErrList) > 0){
foreach($MemberList as $Member){
$YM = str_replace("-", "年",$YM)."月";
// ブランド使用料メール送信
$this->mailService->sendChainStoreRetailUsageFeeMail($Member, $YM, $usageFeeResult, $RetailUsageErrList, $RetailUsageGMOErrList, $BankTransferCount, $GMOPaymentCount);
}
//}
//}
}
return [
'UsageFeeResult' => $usageFeeResult,
'Error' => null
];
}
}
/**
* 小売販売契約に対するブランド使用料は毎月定額15,000円となっている。(当月).
*
* @Route("/cronjob/calc_retail_usage_fee/{key}", name="calc_retail_usage_fee", methods={"GET", "POST"})
* @Template("UsageFee/calcUsageFee.twig")
*/
public function calcRetailUsageFee(Request $request, $key)
{
$YM = date('Y-m');
return $this->calcRetailUsageFeeMonth($request, $YM, $key);
}
/**
* change customer and member password.
*
* @Route("/cronjob/change_pwd/{key}", name="change_pwd", methods={"GET"})
*
* @param Request $request
*
* @return JsonResponse
*/
public function change_pwd(Request $request, $key)
{
$result = [];
try {
// タイムアウトを無効にする.
set_time_limit(0);
$MemberList = $this->memberRepository->findAll(); //->findOneBy(["id" => 2]);
foreach($MemberList as $Member){
if(strlen($Member->getPassword()) <= 60){
$newPassword = $this->passwordEncoder->encodePassword($Member->getPassword(), $Member->getSalt());
$Member->setPassword($newPassword);
$this->memberRepository->save($Member);
$this->entityManager->flush();
}
}
$CustomerList = $this->customerRepository->findAll(); //->findOneBy(["id" => 401]);
foreach($CustomerList as $Customer){
if(strlen($Customer->getPassword()) <= 60){
$newPassword = $this->passwordEncoder->encodePassword($Customer->getPassword(), $Customer->getSalt());
$Customer->setPassword($newPassword);
$this->customerRepository->save($Customer);
$this->entityManager->flush();
}
}
} catch (\Exception $e) {
log_error('予期しないエラーです', [$e->getMessage()]);
return $this->json(['status' => $e->getMessage()], 500);
}
return $this->json(array_merge(['status' => 'OK', 'data' => $result], []));
}
/**
* test chainstore register to admin mail.
*
* @Route("/test_admin_mail", name="test_admin_mail", methods={"GET"})
*
* @param Request $request
*
* @return JsonResponse
*/
public function test_admin_mail(Request $request)
{
$result = [];
try {
// タイムアウトを無効にする.
set_time_limit(0);
$ChainStore = $this->chainstoreRepository->findOneBy(["id" => 68]);
$Member = $this->memberRepository->findOneBy(["id" => 2]);
$Customer = $this->customerRepository->findOneBy(["id" => 108]);
// 販売店会員メール送信
$this->mailService->sendChainStoreConfirmAdminMail($Member, $Customer, $ChainStore, $ChainStore->getContractType());
} catch (\Exception $e) {
log_error('予期しないエラーです', [$e->getMessage()]);
return $this->json(['status' => $e->getMessage()], 500);
}
return $this->json(array_merge(['status' => 'OK', 'data' => $result], []));
}
/**
* test chainstore register to admin mail.
*
* @Route("/test_kari_mail/{id}/{key}", name="test_kari_mail", methods={"GET"})
*
* @param Request $request
*
* @return JsonResponse
*/
public function test_kari_mail(Request $request, $id, $key)
{
$result = [];
try {
if($this->key != $key){
$error = "Incorrect registry key for Access!!";
return [
'Error' => $error
];
}else{
// タイムアウトを無効にする.
set_time_limit(0);
$ChainstoreType = null;
$Customer = $this->customerRepository->findOneBy(["id" => $id]);
$ChainStore = $Customer->getChainStore();
if(is_object($ChainStore)){
$ChainstoreType = $ChainStore->getContractType();
}
$activateUrl = $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
// 販売店会員メール送信
$this->mailService->sendChainStoreConfirmMailFullReg($Customer, $activateUrl, $ChainstoreType);
}
} catch (\Exception $e) {
log_error('予期しないエラーです', [$e->getMessage()]);
return $this->json(['status' => $e->getMessage()], 500);
}
return $this->json(array_merge(['status' => 'OK', 'data' => $result], []));
}
}