<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\Order;
use App\Service\OrderService;
use DateTime;
use Doctrine\Persistence\ManagerRegistry;
use PHPShopify\ShopifySDK;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class OrderApiController extends AbstractController
{
/**
* @Route("/api/pending_order", name="app_pending_order_api")
*/
public function index(Request $request, ManagerRegistry $doctrine): Response
{
$entityManager = $doctrine->getManager();
$customerId = $request->query->get('id');
$customerObj = $entityManager->getRepository(Customer::class)->findOneBy(array('customerId' => $customerId));
if (empty($customerObj)){
return new JsonResponse([]);
}
$customerObjId = $customerObj->getId();
$orders = $entityManager->getRepository(Order::class)->findBy(array('customer' => $customerObjId));
$objResponse = array();
foreach ($orders as $order) {
if (empty($order->getDraftOrderLink())) {
continue;
}
$orderCreatedAt = $order->getDate();
$orderCreatedAt = date_format($orderCreatedAt, 'd/m/Y');
array_push($objResponse, [
$order->getOrderId(),
$orderCreatedAt,
$order->getPaymentStatus(),
'$'. round($order->getTotal(), 2),
'<a href="'. $order->getDraftOrderLink() .'">View Pending Checkout</a>',
]);
}
return new JsonResponse(array('data' => $objResponse));
}
/**
* @Route("/api/test_order", name="app_test_order")
*/
public function testorders(Request $request, OrderService $orderService): Response
{
$response = $orderService->pushProductsFromDraftOrder('1097635004649');
// $orderService->pushHcpProducts();
// return new JsonResponse(['test']);
return $response;
}
}