<?php

include_once './config.php';
include_once '../env.php';
include_once './log.php';

use \App\Models\Entity\ItemsOrderDb;

function stockStatus()
{
  global $entityManager;

  $itemsOrderRepository = $entityManager->getRepository('App\Models\Entity\ItemsOrderDb');
  $itemsOrder = $itemsOrderRepository->findAll();

  $stockRepository = $entityManager->getRepository('App\Models\Entity\StockDb');
  $stock = $stockRepository->findAll();

  $ordersRepository = $entityManager->getRepository('App\Models\Entity\DataOrderSellerDb');
  $ord = $ordersRepository->findAll();

  // lista os itens sem status de estoque
  foreach ($itemsOrder as $i) {
    if ($i->getStatusStock() == null) {
      $skuVtex[] = $i->getSkuVtex();
      $orderId[] = $i->getOrderId();
      $idItemsOrder[] = $i->getId();
    }
  }

  if (!empty($skuVtex)) {
    // compara as duas tabelas de produtos
    foreach ($skuVtex as $sku) {
      foreach ($stock as $s) {
        if ($sku == $s->getSkuVtex()) {
          $id[] = $s->getId();
        }
      }
    }
    //var_dump($id);
    //var_dump($skuVtex);


    if (!empty($id)) {
      //consulta por id
      foreach ($id as $key => $i) {
        $idItem[] = $stockRepository->find($i);
      }


      //compara as tabelas
      foreach ($orderId as $o) {
        foreach ($ord as $od) {
          if ($o == $od->getOrderId()) {
            $idOrder[] = $od->getId();
          }
        }
      }

      //var_dump($idOrder);


      if (!empty($idOrder)) {
        //consulta nome da doca
        foreach ($idOrder as $key => $iO) {
          $idOrd[] = $ordersRepository->find($iO);
        }

        foreach ($idOrd as $key => $i) {
          switch ($i->getWarehouseId()) {
            case 'acreSergio':
              $stockDock = $idItem[$key]->getAcreSergio();
              //var_dump($stockDock);
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'outletCvto':
              $stockDock = $idItem[$key]->getOutletCvto();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'forMen':
              $stockDock = $idItem[$key]->getForMen();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'maiusculo':
              $stockDock = $idItem[$key]->getMaiusculo();
              //var_dump($stockDock);
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'life':
              $stockDock = $idItem[$key]->getLife();
              //var_dump($stockDock);
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'vestylle':
              $stockDock = $idItem[$key]->getVestylle();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'haut':
              $stockDock = $idItem[$key]->getHaut();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'dimazzo':
              $stockDock = $idItem[$key]->getDimazzo();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;
          
          case 'dimazzoArapongas':
              $stockDock = $idItem[$key]->getDimazzoArapongas();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'bhPadilha':
              $stockDock = $idItem[$key]->getBhPadilha();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'origem':
              $stockDock = $idItem[$key]->getOrigem();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'emporio':
              $stockDock = $idItem[$key]->getEmporio();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'abusiva':
              $stockDock = $idItem[$key]->getAbusiva();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'signoriOtto':
              $stockDock = $idItem[$key]->getSignoriOtto();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'gianottoModas':
              $stockDock = $idItem[$key]->getGianottoModas();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            case 'Etq_Cmaleao':
              $stockDock = $idItem[$key]->getOutletCvto();
              $statusStock = $idItemsOrder[$key];
              $newStatus = $itemsOrderRepository->find($statusStock);

              updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock);

              break;

            default:
              exit;
              break;
          }
        }
      }
    }
  }
  //var_dump($skuVtex);

}

function updateStock($stockDock, $statusStock, $newStatus, $entityManager, $stockRepository, $stock)
{
  foreach ($stock as $s) {
    if ($s->getSkuVtex() == $newStatus->getSkuVtex()) {
      $idStock[] = $s->getId();
    }
  }
  $newStock = $newStatus->getQuantity();

  $newStatus->setStatusStock($stockDock);
  $entityManager->persist($newStatus);
  $entityManager->flush();

  if (!empty($idStock)) {
    foreach ($idStock as $key => $id) {
      $getStock[] = $stockRepository->find($id);
    }
  } else {
    $getStock = null;
  }

  $countArray = count($getStock);

  for ($i = 0; $i < $countArray; $i++) {
    if ($getStock[$i]->getOutletCvto() == null) {
      $getStock[$i]->setOutletCvto(-$newStock);
      $entityManager->persist($getStock[$i]);
      $entityManager->flush();
    } else {
      $valueStock = $getStock[$i]->getOutletCvto() - $newStock;
      $getStock[$i]->setOutletCvto($valueStock);
      $entityManager->persist($getStock[$i]);
      $entityManager->flush();
    }
  }
}
