<?php
namespace Plugin\CustomPrice\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Controller\AbstractController;
use Eccube\Repository\ProductRepository;
use Plugin\CustomPrice\Repository\CustomPriceRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Plugin\CustomPrice\Common\CommonPrice;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Service\TaxRuleService;
class CustomPriceApiController extends AbstractController
{
protected $productRepository;
protected $customPriceRepository;
protected $entityManager;
protected $taxRuleService;
public function __construct(
ProductRepository $productRepository,
CustomPriceRepository $customPriceRepository,
EntityManagerInterface $entityManager,
TaxRuleService $taxRuleService
) {
$this->productRepository = $productRepository;
$this->customPriceRepository = $customPriceRepository;
$this->entityManager = $entityManager;
$this->taxRuleService = $taxRuleService;
}
/**
* @Route("/api/custom_price/product/{id}", name="api_custom_price", methods={"GET"}, requirements={"id" = "\d+"})
*/
public function custom_price(Request $request, $id): JsonResponse
{
$product = $this->productRepository->find($id);
$width = $request->query->get('custom_width', null);
$height = $request->query->get('custom_height', null);
$type = $request->query->get('type', null);
$quantity = $request->query->get('quantity', 1);
$totalSize = null;
if (!empty($width)) {
$totalSize = (int)$width;
}
if (!empty($height)) {
$totalSize += (int)$height;
}
if (!$quantity) {
$quantity = 1;
}
if (!$product) {
return new JsonResponse([
'error' => '製品が存在しません'
], JsonResponse::HTTP_NOT_FOUND);
}
if ($type == 1) {
$price = floor($product->getPrice02Max() * CommonPrice::getProfit($type));
return new JsonResponse([
'main_price' => number_format($this->taxRuleService->getPriceIncTax($price * $quantity, $product)),
'price' => number_format($this->taxRuleService->getPriceIncTax($price, $product)),
]);
}
if (empty($product->getCustomProductPrice()->getPrices())) {
return new JsonResponse([
'error' => '製品のオプション金額がありません'
], JsonResponse::HTTP_NOT_FOUND);
}
$productConfigId = $product->getCustomProductPrice()->getCustomPriceConfig()->getId();
$customPrice = $this->customPriceRepository->findPriceByTotal($productConfigId, $totalSize);
if (!$customPrice) {
return new JsonResponse(['error' => '製作可能サイズを超えております'], JsonResponse::HTTP_NOT_FOUND);
}
$chop_price = 1;
if ($type == 2 && is_numeric($product->getChopPrice())) {
$chop_price = $product->getChopPrice();
}
if ($type == 2) {
$price = floor($chop_price * ($width + $height) * CommonPrice::getProfit($type));
} else {
$price = floor($chop_price * $customPrice->getPrice() * CommonPrice::getProfit($type));
}
return new JsonResponse([
'main_price' => number_format($this->taxRuleService->getPriceIncTax($price * $quantity, $product)),
'price' => number_format($this->taxRuleService->getPriceIncTax($price, $product)),
]);
}
}