<?php
/**
 * Created by PhpStorm.
 * User: Olyanren
 * Date: 22.10.2016
 * Time: 12:36
 */

namespace AppBundle\Controller;


use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\NoResultException;
use JMS\Serializer\SerializerBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * ReportController controller.
 *
 * @Route("/7073a00e724bbda643cb7e2a85110640/home/report")
 */
class ReportController extends Controller
{
    /**
     * @Route("/")
     */
    public function index()
    {
        return $this->redirect('/home/product-move');
    }

    /**
     * Finds and displays a Product entity.
     *
     * @Route("/product-move", name="product_move")
     * @Method({"GET", "POST"})
     */
    public function productMove(Request $request)
    {
        $ownId = 0;
        $parentId = 0;
        $parentParentId = 0;
        $deliveryHistories = new ArrayCollection();
        $productHistories = new ArrayCollection();
        if ($request->isMethod('POST')) {
            $locationId = $this->get("app.locationutil")->getLocationId($request);
            if ($locationId != null)
                list($ownId, $type, $parentId, $parentParentId) =
                    $this->get("app.locationutil")->getIdsFromLocationId($locationId);

            $txtSearchDelivery = $request->request->get("txtSearchDelivery");
            if (empty($txtSearchDelivery)) {
                $this->addFlash("error", "Arama kısmı boş olamaz");
            } else {
                $txtStartDate = $request->request->get("txtStartDate");
                $txtEndDate = $request->request->get("txtEndDate");
                $companyId = $request->request->get("company");
                $sevkVeFirma = $request->request->get("sevkVeFirma");
                $product = $this->get("app.doctrineutil")->getProductWithInvoiceAndDeliveries($sevkVeFirma, $txtSearchDelivery,
                    $txtStartDate, $txtEndDate, $companyId, $locationId);
                if ($product != null) {
                    $isSevk = in_array("1", $sevkVeFirma);
                    $isFatura = in_array("2", $sevkVeFirma);
                    if ($isSevk)
                        $deliveryHistories = $product->getDeliveryHistories();
                    if ($isFatura)
                        $productHistories = $product->getProductHistories();
                }
            }

        }
        return $this->render('report/productMove.html.twig', array(
            'type' => 'ekip',
            'deliveryHistories' => $deliveryHistories,
            'productHistories' => $productHistories,
            'parent_id' => $parentId,
            'own_id' => $ownId,
            'parent_parent_id' => $parentParentId,
            'token' => $this->get('app.crsfutil')->getToken()
        ));
    }

    /**
     * Finds and displays a Product entity.
     *
     * @Route("/product-envanter", name="product_envanter")
     * @Method({"GET", "POST"})
     */
    public function productEnvanteri(Request $request)
    {
        $ownId = 0;
        $parentId = 0;
        $parentParentId = 0;
        $totalInputCount = 0;
        $totalOutputCount = 0;
        $totalCount = 0;
        $totalPrice = 0;
        $productList = new ArrayCollection();
        if ($request->isMethod('POST')) {
            $locationId = $this->get("app.locationutil")->getLocationId($request);
            if ($locationId != null)
                list($ownId, $type, $parentId, $parentParentId) =
                    $this->get("app.locationutil")->getIdsFromLocationId($locationId);

            $productgroup = $request->request->get("productgroup");
            $kdv = $request->request->get("kdv");
            $measure = $request->request->get("measure");
            $minBakiye = $request->request->get("minBakiye");
            $productList = $this->get("app.doctrineutil")->getProducts($locationId, $productgroup,
                $kdv, $measure, $minBakiye);
            $totalInputCount = $this->calculateTotalInputAmount($productList);
            $totalOutputCount = $this->calculateTotalOutputAmount($productList);
            $totalCount = $this->calculateTotalAmount($productList);
            $totalPrice = $this->calculateTotalPriceAmount($productList);
        }
        return $this->render('report/productEnvanteri.html.twig', array(
            'productList' => $productList,
            'parent_id' => $parentId,
            'type' => 'ekip',
            'own_id' => $ownId,
            'totalInputCount' => $totalInputCount,
            'totalOutputCount' => $totalOutputCount,
            'totalCount' => $totalCount,
            'totalPrice' => $totalPrice,
            'parent_parent_id' => $parentParentId,
            'token' => $this->get('app.crsfutil')->getToken()
        ));
    }

    private function calculateTotalInputAmount($productList)
    {
        $result=0;
        foreach ($productList as $product){
            $result+=$product->getInputAmount();
        }
        return $result;
    }

    private function calculateTotalOutputAmount($productList)
    {
        $result=0;
        foreach ($productList as $product){
            $result+=$product->getOutputAmount();
        }
        return $result;
    }

    private function calculateTotalAmount($productList)
    {
        $result=0;
        foreach ($productList as $product){
            $result+=$product->getAmount();
        }
        return $result;
    }

    private function calculateTotalPriceAmount($productList)
    {
        $result=0;
        foreach ($productList as $product){
            $result+=$product->getPrice();
        }
        return $result;
    }


}