<?php

namespace KupShop\OrderingBundle\Tests;

use KupShop\AdminBundle\Util\Stats\StoreDataLoader;
use Query\Operator;

class StockInTest extends \DatabaseTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->updateStockInDates(1);
    }

    public function testStoreValue()
    {
        $this->removeModule(\Modules::WAREHOUSE);
        $storeDataLoader = $this->get(StoreDataLoader::class);
        $yearNum = date('Y');
        $qb = $storeDataLoader->getCurrentStoreQb("{$yearNum}-07-07 05:00:00");
        $qb->select('p.id as id_product, pv.id as id_variation, p.title, s.in_store, r.average_price')
            ->orderBy('p.id, pv.id')
            ->having('average_price IS NOT NULL');

        $items = $qb->execute()->fetchAllAssociative();

        $this->assertEquals([
            [
                'id_product' => '4',
                'id_variation' => '1',
                'title' => 'Columbia Lay D Down',
                'average_price' => '60.00000000', // 50 nákup + 10 další náklady
                'in_store' => '5',
            ],
        ], $items);
    }

    public function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }

    private function updateStockInDates(int $stockInId): void
    {
        $now = new \DateTime();

        sqlQueryBuilder()
            ->update('stock_in')
            ->directValues([
                'date_created' => "{$now->format('Y')}-01-01 00:00:00",
                'date_stock_in' => $now->format('Y-01-01 H:i:s'),
            ])
            ->where(Operator::equals(['id' => $stockInId]))
            ->execute();
    }
}
