<?php

namespace KupShop\PohodaBundle\Tests\Invoices\Import;

use KupShop\PohodaBundle\Tests\TestUtilHelper;
use KupShop\PohodaBundle\Utils\Importers\InvoicesDownloader;
use KupShop\PohodaBundle\Utils\Importers\InvoicesImporter;
use KupShop\PohodaBundle\Utils\Tools\TransformHelper;
use Query\Operator;

class ImportInvoiceTest extends \DatabaseTestCase
{
    use TestUtilHelper;

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

    /**
     * @var TransformHelper
     */
    protected $transformHelper;

    /**
     * @var InvoicesImporter
     */
    protected $invoicesImporter;

    protected function setEnvironment()
    {
        $this->transformHelper = $this->get(TransformHelper::class);
        $this->prepareMServer();
    }

    public function testTransformInvoicesIn()
    {
        $this->setEnvironment();
        $xmlResponse = file_get_contents(__DIR__.'/invoicesInResponse.xml');
        $transformedXML = $this->transformHelper->baseTransform($xmlResponse, $this->transformHelper->loadXslt('invoicesIn.xsl'));
        $this->assertXmlStringEqualsXmlFile(__DIR__.'/invoicesInResult.xml', $transformedXML);
    }

    public function testImportInsertInvoice()
    {
        $this->setEnvironment();

        $downloader = $this->getMockBuilder(InvoicesDownloader::class)
            ->setMethods(['checkInvoicePrefix'])
            ->getMock();
        $downloader->method('checkInvoicePrefix')->willReturn(3128);

        // check number of orders
        $this->assertEquals(1, sqlQueryBuilder()
            ->select('COUNT(id)')
            ->from('orders')
            ->execute()
            ->fetchColumn());

        $this->set(InvoicesDownloader::class, $downloader);
        $this->invoicesImporter = $this->get(InvoicesImporter::class);
        $xml = file_get_contents(__DIR__.'/invoicesInResponse.xml');
        $this->invoicesImporter->processData($xml, true);

        // ORDER - issuedCorrectiveTax
        $qb = sqlQueryBuilder()
            ->select('*')
            ->from('orders_pohoda_invoices')
            ->andWhere(Operator::equals(['number_requested' => '201501736']))
            ->execute()
            ->fetch();

        $this->assertEquals('201501736', $qb['number_requested']);
        $this->assertEquals('2015', $qb['invoice_prefix']);
        $this->assertEquals('IC227192', $qb['id_pohoda_invoice']);

        $qb = sqlQueryBuilder()
            ->select('*')
            ->from('orders')
            ->andWhere(Operator::equals(['id' => $qb['id_order']]))
            ->execute()
            ->fetch();

        $note_admin = json_decode($qb['note_admin'], true);
        $this->assertEquals('web128453', $note_admin['ODD_ORDER_NO']);
        $this->assertEquals('774696', $qb['id_user']);

        // ORDER - issuedInvoice
        $qb = sqlQueryBuilder()
            ->select('*')
            ->from('orders_pohoda_invoices')
            ->andWhere(Operator::equals(['number_requested' => '210507247']))
            ->execute()
            ->fetch();

        $this->assertEquals('210507247', $qb['number_requested']);
        $this->assertEquals('2105', $qb['invoice_prefix']);
        $this->assertEquals('241497', $qb['id_pohoda_invoice']);

        // check number of orders
        $this->assertEquals(2, sqlQueryBuilder()
            ->select('COUNT(id)')
            ->from('orders')
            ->execute()
            ->fetchColumn());
    }
}
