src/Controller/ApiController.php line 178

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Catalog;
  4. use App\Entity\StanMag;
  5. use App\Entity\StanMagStatus;
  6. use App\Entity\SystemConfig;
  7. use App\Integration\Marketplace\Allegro\AllegroRestApi;
  8. use Doctrine\ORM\EntityManager;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. class ApiController extends ApiBaseController
  18. {
  19.     /**
  20.      * @Route("/api/stanmagReactivate/{id}", name="api_stanmag_reactivate", methods={"GET"})
  21.      * @param string $id
  22.      * @return JsonResponse
  23.      */
  24.     public function getApiStanmagReactivate(string $id ''): Response
  25.     {
  26.         $output = [];
  27.         /** @var StanMag $item */
  28.         $id preg_replace("/-/","/",$id);
  29.         /** @var StanMag $item */
  30.         $item $this->em->getRepository(StanMag::class)->findOneBy(['nrEtykiety'=> $id]);
  31.         if (!$item) {
  32.             return $this->respondWithErrors("No item found");
  33.         }
  34.         // if already active - return with error
  35.         if ($item->getStanMagStatus()->getAllegroActive() > 0) {
  36.             return $this->respondWithErrors("Item already published");
  37.         }
  38. #        if ($item->getStanMagStatus()->getIsEligible() > 0) {
  39. #            return $this->respondWithErrors("Item already active");
  40. #            $item->makeAvailable();
  41. #        }
  42.         if (!$item->makeAvailable()) {
  43.             return $this->respondWithErrors("Item activation failed");
  44.         }
  45.         $this->em->persist($item);
  46.         $this->em->flush();
  47.         // if sold - make previous auctions as ignore
  48.         // update sold dates
  49.         // update active status
  50.         // update ilosc = 1
  51.         // udpate usun = 0
  52.         return $this->respondWithText("Produkt ".$item->getNrEtykiety()." może być ponownie wystawiony na Allegro");
  53.         return $this->respondWithSuccess($output);
  54.     }
  55.     /**
  56.      * @Route("/api/stanmagDeactivate/{id}", name="api_stanmag_deactivate", methods={"GET"})
  57.      * @param string $id
  58.      * @return JsonResponse
  59.      */
  60.     public function getApiStanmagDeactivate(string $id ''): JsonResponse
  61.     {
  62.         $output = [];
  63.         /** @var StanMag $item */
  64.         $id preg_replace("/-/","/",$id);
  65.         /** @var StanMag $item */
  66.         $item $this->em->getRepository(StanMag::class)->findOneBy(['nrEtykiety'=> $id]);
  67.         if (!$item) {
  68.             return $this->respondWithErrors("No item found");
  69.         }
  70.         // if already active - return with error
  71.         if ($item->getStanMagStatus()->getAllegroActive() > 0) {
  72.             return $this->respondWithErrors("Item already published");
  73.         }
  74.         if ($item->getStanMagStatus()->getIsEligible() == 0) {
  75.             return $this->respondWithErrors("Item already inactive");
  76.         }
  77.         if (!$item->makeUnavailable()) {
  78.             return $this->respondWithErrors("Item deactivation failed");
  79.         }
  80.         $this->em->persist($item);
  81.         $this->em->flush();
  82.         // update ilosc = 0
  83.         // udpate usun = 98
  84.         return $this->respondWithSuccess($output);
  85.     }
  86.     /**
  87.      * @Route("/api/stanmagUnpublish/{id}", name="api_stanmag_unpublish", methods={"GET"})
  88.      * @param string $id
  89.      * @return JsonResponse
  90.      */
  91.     public function getApiStanmagUnpublish(string $id ''): Response
  92.     {
  93.         $output = [];
  94.         /** @var StanMag $item */
  95.         $id preg_replace("/-/","/",$id);
  96.         /** @var StanMag $item */
  97.         $item $this->em->getRepository(StanMag::class)->findOneBy(['nrEtykiety'=> $id]);
  98.         if (!$item) {
  99.             return $this->respondWithErrors("ERROR: UNABLE TO UNPUBLISH THIS ITEM No item found");
  100.         }
  101.         if (!$item->getAllegroOffers()) {
  102.             return $this->respondWithErrors("ERROR: UNABLE TO UNPUBLISH THIS ITEM No Allegro offers found");
  103.         } else {
  104.             $output['log'][] = "Found ".count($item->getAllegroOffers())." Allegro offers ";
  105.         }
  106.         $activeOffer null;
  107.         foreach($item->getAllegroOffers() as $offer) {
  108.             if (strtoupper($offer->getStatus()) == 'ACTIVE' || strtoupper($offer->getStatus()) == 'PLACEHOLDER') {
  109.                 $activeOffer $offer;
  110.                 $output['log'][] = "Found active offer ".$offer->getId()." ";
  111.             }
  112.         }
  113.         if (null === $activeOffer) {
  114.             return $this->respondWithErrors("ERROR: UNABLE TO UNPUBLISH THIS ITEM No Active Allegro offer found");
  115.         }
  116.         $source = ($item->getKodLokalizacji() == 12) ? "waw" "gru";
  117.         if ($item->getKodLokalizacji() == 99) {
  118.             $source "aga";
  119.         }
  120.         $allegroClientId $this->em->getRepository(SystemConfig::class)->getParameter('allegroClientId','allegro');
  121.         $allegroClientSecret $this->em->getRepository(SystemConfig::class)->getParameter('allegroClientSecret','allegro');
  122.         $allegroAccessToken $this->em->getRepository(SystemConfig::class)->getParameter('allegroAccessToken.'.$source,'allegro');
  123.         $api = new AllegroRestApi();
  124.         $api->setAccessToken($allegroAccessToken)
  125.             ->setClientId($allegroClientId)
  126.             ->setClientSecret($allegroClientSecret);
  127.         $output['log'][] = "Active offer removed - ".$activeOffer->getId()." ";
  128.         $res $api->removeOfferFromAllegro($activeOffer->getId());
  129.         $output['response'] = $res;
  130.         $activeOffer->setStatus("ENDED");
  131.         $this->em->persist($activeOffer);
  132.         $this->em->flush();
  133.         $output['log'][] = "Active offer status set to ENDED - ".$activeOffer->getId()." ";
  134.         //return $this->respondWithText("PRODUKT BYL WYSTAWIONY I JEST TERAZ USUNIETY");
  135.         return $this->respondWithSuccess($output);
  136.     }
  137.     /**
  138.      * @Route("/api/stanmagStatus/{location}", name="api_stanmag_status", methods={"GET"})
  139.      * @param string $location
  140.      * @return JsonResponse
  141.      */
  142.     public function getApiStanmagStatus(string $location '00'): JsonResponse
  143.     {
  144.         $output = [];
  145.         $alias "s";
  146.         $prefix $alias.'.';
  147.         $start 0;
  148.         $limit 10000;
  149.         $countQuery $this->em->createQueryBuilder();
  150.         $countQuery->select('count(s.nrEtykiety)')->from(StanMagStatus::class, $alias);
  151.         $countQuery->where('1=1');
  152.         $countQuery->andWhere(" s.kodLokalizacji = '".$location."' ");
  153.         $countRecords $countQuery->getQuery()->getSingleScalarResult();
  154.         //$output['log'][] = "Total records: ".$countRecords." ";
  155.         if ($countRecords 0) {
  156.             while ($start $countRecords) {
  157.                 //$output['log'][] = "Batch start [".$start."] limit [".$limit."]  ";
  158.                 $query $this->em->createQueryBuilder();
  159. //        $query->select('s','st')->from(StanMagStatus::class, $alias)->leftJoin($alias.'.stanMag', 'st');
  160.                 $query->select('s')->from(StanMagStatus::class, $alias);
  161.                 $query->where('1=1');
  162.                 $query->andWhere(" s.kodLokalizacji = '".$location."' ");
  163.                 $items $query->getQuery()
  164.                     ->setFirstResult($start)
  165.                     ->setMaxResults($limit)
  166.                     ->getArrayResult();
  167.                     //->getResult();
  168.                 if (is_array($items) && count($items) > 0) {
  169.             foreach($items as $item) {
  170.                 $row = [
  171.                     'nr_etykiety'    => $item['nrEtykiety'],
  172.                     'location'       => $item['kodLokalizacji'],
  173.                     'allegro_active' => $item['allegroActive'],
  174.                     'allegro_sold'   => $item['allegroSold'],
  175.                     'status'         => $item['isAvailable'],
  176.                     'is_eligible'    => $item['isEligible'],
  177.                     'status_allegro' => $this->getStatusAllegro($item),
  178.                 ];
  179.                 $output[] = $row;
  180.             }
  181.                 } else {
  182.                     return $this->respondWithErrors("No data to render");
  183.                 }
  184.                 $start += $limit;
  185.             }
  186.         } else {
  187.             // no count records received
  188.             return $this->respondWithErrors("No data received");
  189.         }
  190. //
  191. //
  192. //        //$items = $this->em->getRepository(StanMagStatus::class)->findBy(['kodLokalizacji'=> $location]);
  193. //        if (is_array($items) && count($items) > 0) {
  194. //
  195. ////            /** @var StanMagStatus $item */
  196. ////            foreach($items as $item) {
  197. ////                $row = [
  198. ////                    'nr_etykiety'    => $item->getNrEtykiety(),
  199. ////                    'location'       => $item->getKodLokalizacji(),
  200. ////                    'allegro_active' => $item->getAllegroActive(),
  201. ////                    'allegro_sold'   => $item->getAllegroSold(),
  202. ////                    'status'         => $item->getIsAvailable(),
  203. ////                    'is_eligible'    => $item->getIsEligible(),
  204. ////                    'status_allegro' => $item->getStatusAllegro(),
  205. ////                ];
  206. ////                $output[] = $row;
  207. ////            }
  208. //
  209. //            foreach($items as $item) {
  210. //                $row = [
  211. //                    'nr_etykiety'    => $item['nrEtykiety'],
  212. //                    'location'       => $item['kodLokalizacji'],
  213. //                    'allegro_active' => $item['allegroActive'],
  214. //                    'allegro_sold'   => $item['allegroSold'],
  215. //                    'status'         => $item['isAvailable'],
  216. //                    'is_eligible'    => $item['isEligible'],
  217. //                    'status_allegro' => $this->getStatusAllegro($item),
  218. //                ];
  219. //                $output[] = $row;
  220. ////                print_r($item);
  221. ////                print_r($row);
  222. ////                die();
  223. //
  224. //            }
  225. //
  226. //        } else {
  227. //            return $this->respondWithErrors("No data to render");
  228. //        }
  229.         return $this->respondWithSuccess($output);
  230.     }
  231.     
  232.     private function getStatusAllegro(array $item) : ?string
  233.     {
  234.         if ($item['allegroActive'] > 0) return "wystawione";
  235.         if ($item['allegroSold'] > 0) return "sprzedane";
  236.         if ($item['isAvailable'] < 1) return "sprzedane sisd";
  237.         if ($item['hasPhotoPrimary'] < || $item['hasPhotos'] < 1) return "brak zdjec";
  238.         if($item['isEligible'] == 1) return "gotowe do wystawiania";
  239.         return null;
  240.     }
  241. //
  242. //    /**
  243. //     * @Route("/api/login_check", name="login-check", methods={"POST"})
  244. //     * @return JsonResponse
  245. //     */
  246. //    public function getTokenUser(): JsonResponse
  247. //    {
  248. //        return new JsonResponse(['token' => 'TOKEN']);
  249. //    }
  250. }