<commit_before><?php

use App\Test\TestCase\AppCakeTestCase;
use App\Test\TestCase\Traits\AppIntegrationTestTrait;
use App\Test\TestCase\Traits\LoginTrait;
use Cake\Core\Configure;

/**
 * FoodCoopShop - The open source software for your foodcoop
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @since         FoodCoopShop 3.2.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 * @author        <NAME> <<EMAIL>>
 * @copyright     Copyright (c) <NAME>, https://www.rothauer-it.com
 * @link          https://www.foodcoopshop.com
 */
class InvoicesTableTest extends AppCakeTestCase
{

    use AppIntegrationTestTrait;
    use LoginTrait;

    public $Invoice;

    public function setUp(): void
    {
        parent::setUp();
        $this->Invoice = $this->getTableLocator()->get('Invoices');
    }

    public function testGetNextInvoiceNumberForCustomerInvoicesDoNotExist()
    {
        $result = $this->Invoice->getNextInvoiceNumberForCustomer('2020', []);
        $this->assertEquals($result, '2020-000001');
    }

    public function testGetNextInvoiceNumberForCustomerDifferentYearAndInvoiceAlreadyExists()
    {
        $invoice = $this->Invoice->newEmptyEntity();
        $invoice->invoice_number = '2020-000001';
        $result = $this->Invoice->getNextInvoiceNumberForCustomer('2021', $invoice);
        $this->assertEquals($result, '2021-000001');
    }

    public function testGetNextInvoiceNumberForCustomerSameYearAndInvoiceAlreadyExists()
    {
        $invoice = $this->Invoice->newEmptyEntity();
        $invoice->invoice_number = '2020-000001';
        $result = $this->Invoice->getNextInvoiceNumberForCustomer('2020', $invoice);
        $this->assertEquals($result, '2020-000002');
    }

    public function testGetNextInvoiceNumberForManufacturerInvoicesDoNotExist()
    {
        $result = $this->Invoice->getNextInvoiceNumberForManufacturer([]);
        $this->assertEquals($result, '0001');
    }

    public function testGetNextInvoiceNumberForManufacturerInvoicesAlreadyExist()
    {
        $invoice = $this->Invoice->newEmptyEntity();
        $invoice->invoice_number = '0001';
        $result = $this->Invoice->getNextInvoiceNumberForManufacturer([$invoice]);
        $this->assertEquals($result, '0002');
    }

    public function testGetPreparedTaxRatesForSumTable()
    {

        $this->changeConfiguration('FCS_SEND_INVOICES_TO_CUSTOMERS', 1);

        $this->loginAsSuperadmin();
        $customerId = Configure::read('test.superadminId');
        $paidInCash = 1;

        $this->get('/admin/customers/generateInvoice.pdf?customerId='.$customerId.'&paidInCash='.$paidInCash.'&currentDay=2018-02-02');

        $invoices = $this->Invoice->find('all', [
            'conditions' => [
                'Invoices.id_customer' => $customerId,
            ],
            'contain' => [
                'InvoiceTaxes',
            ]
        ])->toArray();

        $result = $this->Invoice->getPreparedTaxRatesForSumTable($invoices);

        $expected = [
            'taxRates' => [
                'cashless' => [],
                'cash' => [
                    '0' => [
                        'sum_price_excl' => 4.54,
                        'sum_tax' => 0,
                        'sum_price_incl' => 4.54,
                    ],
                    '10' => [
                        'sum_price_excl' => 1.65,
                        'sum_tax' => 0.17,
                        'sum_price_incl' => 1.82,
                    ],<commit_msg>fixed test<commit_after><?php

use App\Test\TestCase\AppCakeTestCase;
use App\Test\TestCase\Traits\AppIntegrationTestTrait;
use App\Test\TestCase\Traits\LoginTrait;
use App\Test\TestCase\Traits\PrepareInvoiceDataTrait;
use Cake\Core\Configure;

/**
 * FoodCoopShop - The open source software for your foodcoop
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @since         FoodCoopShop 3.2.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 * @author        <NAME> <<EMAIL>>
 * @copyright     Copyright (c) <NAME>, https://www.rothauer-it.com
 * @link          https://www.foodcoopshop.com
 */
class InvoicesTableTest extends AppCakeTestCase
{

    use AppIntegrationTestTrait;
    use LoginTrait;
    use PrepareInvoiceDataTrait;

    public $Invoice;

    public function setUp(): void
    {
        parent::setUp();
        $this->Invoice = $this->getTableLocator()->get('Invoices');
    }

    public function testGetNextInvoiceNumberForCustomerInvoicesDoNotExist()
    {
        $result = $this->Invoice->getNextInvoiceNumberForCustomer('2020', []);
        $this->assertEquals($result, '2020-000001');
    }

    public function testGetNextInvoiceNumberForCustomerDifferentYearAndInvoiceAlreadyExists()
    {
        $invoice = $this->Invoice->newEmptyEntity();
        $invoice->invoice_number = '2020-000001';
        $result = $this->Invoice->getNextInvoiceNumberForCustomer('2021', $invoice);
        $this->assertEquals($result, '2021-000001');
    }

    public function testGetNextInvoiceNumberForCustomerSameYearAndInvoiceAlreadyExists()
    {
        $invoice = $this->Invoice->newEmptyEntity();
        $invoice->invoice_number = '2020-000001';
        $result = $this->Invoice->getNextInvoiceNumberForCustomer('2020', $invoice);
        $this->assertEquals($result, '2020-000002');
    }

    public function testGetNextInvoiceNumberForManufacturerInvoicesDoNotExist()
    {
        $result = $this->Invoice->getNextInvoiceNumberForManufacturer([]);
        $this->assertEquals($result, '0001');
    }

    public function testGetNextInvoiceNumberForManufacturerInvoicesAlreadyExist()
    {
        $invoice = $this->Invoice->newEmptyEntity();
        $invoice->invoice_number = '0001';
        $result = $this->Invoice->getNextInvoiceNumberForManufacturer([$invoice]);
        $this->assertEquals($result, '0002');
    }

    public function testGetPreparedTaxRatesForSumTable()
    {

        $this->changeConfiguration('FCS_SEND_INVOICES_TO_CUSTOMERS', 1);

        $this->loginAsSuperadmin();
        $customerId = Configure::read('test.superadminId');
        $paidInCash = 1;
        $this->generateInvoice($customerId, $paidInCash);

        $invoices = $this->Invoice->find('all', [
            'conditions' => [
                'Invoices.id_customer' => $customerId,
            ],
            'contain' => [
                'InvoiceTaxes',
            ]
        ])->toArray();

        $result = $this->Invoice->getPreparedTaxRatesForSumTable($invoices);

        $expected = [
            'taxRates' => [
                'cashless' => [],
                'cash' => [
                    '0' => [
                        'sum_price_excl' => 4.54,
                        'sum_tax' => 0,
                        'sum_price_incl' => 4.54,
                    ],
                    '10' => [
                        'sum_price_excl' => 1.65,
                        'sum_tax' => 0.17,
                        'sum_price_incl' => 1.82,
                    ],
{'fraction_non_alphanumeric': 0.1314959550253668, 'fraction_numerical': 0.027972027972027972, 'mean_word_length': 2.476644423260248, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 109, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2015102', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2128, 'n_words': 332}