app/Plugin/CustomPrice/Controller/CustomPriceApiController.php line 36

Open in your IDE?
  1. <?php
  2. namespace Plugin\CustomPrice\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Eccube\Controller\AbstractController;
  6. use Eccube\Repository\ProductRepository;
  7. use Plugin\CustomPrice\Repository\CustomPriceRepository;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Plugin\CustomPrice\Common\CommonPrice;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Eccube\Service\TaxRuleService;
  12. class CustomPriceApiController extends AbstractController
  13. {
  14.     protected $productRepository;
  15.     protected $customPriceRepository;
  16.     protected $entityManager;
  17.     protected $taxRuleService;
  18.     public function __construct(
  19.         ProductRepository $productRepository,
  20.         CustomPriceRepository $customPriceRepository,
  21.         EntityManagerInterface $entityManager,
  22.         TaxRuleService $taxRuleService
  23.     ) {
  24.         $this->productRepository $productRepository;
  25.         $this->customPriceRepository $customPriceRepository;
  26.         $this->entityManager $entityManager;
  27.         $this->taxRuleService $taxRuleService;
  28.     }
  29.     /**
  30.      * @Route("/api/custom_price/product/{id}", name="api_custom_price", methods={"GET"}, requirements={"id" = "\d+"})
  31.      */
  32.     public function custom_price(Request $request$id): JsonResponse
  33.     {
  34.         $product $this->productRepository->find($id);
  35.         $width $request->query->get('custom_width'null);
  36.         $height $request->query->get('custom_height'null);
  37.         $type $request->query->get('type'null);
  38.         $quantity $request->query->get('quantity'1);
  39.         $totalSize null;
  40.         if (!empty($width)) {
  41.             $totalSize = (int)$width;
  42.         }
  43.         if (!empty($height)) {
  44.             $totalSize += (int)$height;
  45.         }
  46.         if (!$quantity) {
  47.             $quantity 1;
  48.         }
  49.         if (!$product) {
  50.             return new JsonResponse([
  51.                 'error' => '製品が存在しません'
  52.             ], JsonResponse::HTTP_NOT_FOUND);
  53.         }
  54.         if ($type == 1) {
  55.             $price floor($product->getPrice02Max() * CommonPrice::getProfit($type));
  56.             return new JsonResponse([
  57.                 'main_price' => number_format($this->taxRuleService->getPriceIncTax($price $quantity$product)),
  58.                 'price' => number_format($this->taxRuleService->getPriceIncTax($price$product)),
  59.             ]);
  60.         }
  61.         if (empty($product->getCustomProductPrice()->getPrices())) {
  62.             return new JsonResponse([
  63.                 'error' => '製品のオプション金額がありません'
  64.             ], JsonResponse::HTTP_NOT_FOUND);
  65.         }
  66.         $productConfigId $product->getCustomProductPrice()->getCustomPriceConfig()->getId();
  67.         $customPrice $this->customPriceRepository->findPriceByTotal($productConfigId$totalSize);
  68.         if (!$customPrice) {
  69.             return new JsonResponse(['error' => '製作可能サイズを超えております'], JsonResponse::HTTP_NOT_FOUND);
  70.         }
  71.         $chop_price 1;
  72.         if ($type == && is_numeric($product->getChopPrice())) {
  73.             $chop_price $product->getChopPrice();
  74.         }
  75.         if ($type == 2) {
  76.             $price floor($chop_price * ($width $height) * CommonPrice::getProfit($type));
  77.         } else {
  78.             $price floor($chop_price $customPrice->getPrice() * CommonPrice::getProfit($type));
  79.         }
  80.         return new JsonResponse([
  81.             'main_price' => number_format($this->taxRuleService->getPriceIncTax($price $quantity$product)),
  82.             'price' => number_format($this->taxRuleService->getPriceIncTax($price$product)),
  83.         ]);
  84.     }
  85. }