<?php

use app\models\Settings;
use app\models\Product;

?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Счёт №<?= $data['order']->_key ?></title>
</head>

<body>
    <table style="height: 100vh;" cols="13" rows="50">
        <tr>
            <td style="text-align: center; outline: 2px solid;" colspan="13" rowspan="3" valign="center"><b>ВНИМАНИЕ! Уважаемые покупатели, просим вас указывать в назначении платежа: Аванс за запчасти по договору №<?= $buyer['id'] ?></b></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>
        <tr>
            <td colspan="13"></td>
        </tr>
        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td style="text-align: center;" colspan="13"><b>Образец заполнения платежного поручения</b></td>
        </tr>

        <tr>
            <td style="border: solid;" colspan="6" rowspan="2" valign="top">АО "ТИНЬКОФФ БАНК" г. Москва</td>
            <td style="border: solid;" valign="top">БИК</td>
            <td style="border: solid;" colspan="6" valign="top">044525974</td>
        </tr>

        <tr>
            <td style="border: solid;" rowspan="2" valign="top">Сч. №</td>
            <td style="border: solid;" colspan="6" rowspan="2" valign="top">30101810145250000974</td>
        </tr>

        <tr>
            <td style="border: solid;" colspan="6" valign="top">Банк получателя</td>
        </tr>

        <tr>
            <td style="border: solid;" valign="top">ИНН</td>
            <td style="text-align: left; border: solid;" colspan="2" valign="top">2724241607</td>
            <td style="border: solid;" valign="top">КПП</td>
            <td style="text-align: left; border: solid;" colspan="2" valign="top">272401001</td>
            <td style="border: solid;" valign="top">Сч. №</td>
            <td style="text-align: left; border: solid;" colspan="6" valign="top">40702810610000696279</td>
        </tr>

        <tr>
            <td style="border: solid;" colspan="6" rowspan="3" valign="top">ООО "СтандартМашинери"</td>
            <td style="border: solid;" valign="top">Вид оп.</td>
            <td style="text-align: left; border: solid;" valign="top">01</td>
            <td style="border: solid;" colspan="2" valign="top">Срок плат.</td>
            <td style="text-align: left; border: solid;" colspan="3" valign="top"></td>
        </tr>

        <tr>
            <td style="border: solid;" valign="top">Наз. пл.</td>
            <td style="border: solid;"></td>
            <td style="border: solid;" colspan="2" valign="top">Очер. плат.</td>
            <td style="text-align: left; border: solid;" colspan="3" valign="top">5</td>
        </tr>

        <tr>
            <td style="border: solid;" valign="top">Код</td>
            <td style="border: solid;"></td>
            <td style="border: solid;" colspan="2" valign="top">Рез. поле</td>
            <td style="text-align: left; border: solid;" colspan="3" valign="top"></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td style="text-indent: 2px; text-align: left; border-bottom: medium; font-size: 18rem;" colspan="13" rowspan="3" valign="center">
                <?php
                // Инициализация часового пояса

                preg_match_all('/UTC([\+\-0-9:]*)/', $account->zone ?? Settings::searchActive()['timezone_default'] ?? 'UTC+3', $timezone);
                $timezone = $timezone[1][0];
                ?>
                <b>Счет на оплату №<?= $data['order']->_key ?> от <?= (new DateTime())->setTimestamp($date)->setTimezone(new DateTimeZone($timezone))->format('d.m.Y') ?></b>
            </td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td colspan="2" rowspan="2" valign="center">Поставщик</td>
            <td style="word-wrap: break-word;" colspan="11" rowspan="2" valign="top"><b>ООО "СтандартМашинери", ИНН 2724241607, КПП 272401001, 680014, Хабаровский край, Хабаровск г, Промышленная ул, дом 3, офис 105, тел.: 89242128879</b></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td colspan="2" rowspan="2" valign="center">Покупатель</td>
            <td style="word-wrap: break-word;" colspan="11" rowspan="2" valign="top"><b><?= $buyer['info'] ?></b></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <tr>
            <td style="text-align: center; border: solid; border-top: thick; border-left: thick;" colspan="1" rowspan="2" valign="center"><b>№</b></td>
            <td style="text-align: center; border: solid; border-top: thick;" colspan="6" rowspan="2" valign="center"><b>Товар</b></td>
            <td style="text-align: center; border: solid; border-top: thick;" colspan="2" rowspan="2" valign="center"><b>Количество</b></td>
            <td style="text-align: center; border: solid; border-top: thick;" colspan="2" rowspan="2" valign="center"><b>Цена</b></td>
            <td style="text-align: center; border: solid; border-top: thick; border-right: thick;" colspan="2" rowspan="2" valign="center"><b>Сумма</b></td>
        </tr>

        <tr>
            <td colspan="13"></td>
        </tr>

        <?php
        // Инициализация счётчика строк
        $row = 1;

        // Инициализация итоговой цены
        $cost = 0;
        ?>

        <?php foreach ($data['supplies'] as $prod => $supplies) : ?>
            <?php foreach ($supplies as $catn => $deliveries) : ?>
                <?php foreach ($deliveries as $delivery => $supply) : ?>
                    <?php
                        // Инициализация названия
                        $name = Product::searchByCatn($catn)->name ?? 'Без названия';
                    ?>
                    <tr>
                        <td style="text-align: center; border: solid; border-left: thick;" colspan="1" valign="center"><?= $row++ ?></td>
                        <td style="text-align: left; border: solid;" colspan="6" valign="center"><?= $prod ?> <?= $catn ?> <?= $name ?></td>
                        <td style="text-align: center; border: solid;" colspan="2" valign="center"><?= $supply['amount'] ?></td>
                        <td style="text-align: center; border: solid;" valign="center"><?= $supply['cost'] * $supply['amount'] ?></td>
                        <td style="text-align: center; border: solid;" valign="center"><?= $supply['currency'] ?></td>
                        <td style="text-align: center; border: solid; border-right: thick;" colspan="2" valign="center"><?= $cost += $supply['cost'] * $supply['amount'] ?></td>
                    </tr>
                <?php endforeach ?>
            <?php endforeach ?>
        <?php endforeach ?>

        <tr>
            <td style="border-top: thick;" colspan="13"></td>
        </tr>

        <tr>
            <td colspan="9"></td>
            <td style="text-align: right;" colspan="2" valign="center"><b>Итого:</b></td>
            <td colspan="2" valign="center"><b><?= $cost - $tax = $cost * 0.2 ?></b></td>
        </tr>

        <tr>
            <td colspan="9"></td>
            <td style="text-align: right;" colspan="2" valign="center"><b>В т.ч. НДС (20%):</b></td>
            <td colspan="2" valign="center"><b><?= $tax ?></b></td>
        </tr>

        <tr>
            <td colspan="9"></td>
            <td style="text-align: right;" colspan="2" valign="center"><b>Итого с НДС:</b></td>
            <td colspan="2" valign="center"><b><?= $cost ?></b></td>
        </tr>

        <tr>
            <td colspan="13" valign="center">Всего наименований <?= $row - 1 ?>, на сумму <?= $cost ?> руб.</td>
        </tr>

        <tr>
            <td colspan="13" valign="center"><b><?= mb_strtoupper(mb_substr($text = mb_strtolower($text = (new NumberFormatter("ru", NumberFormatter::SPELLOUT))->format($cost), 'UTF-8'), 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($text, 1, null, 'UTF-8') ?> рублей</b></td>
        </tr>

        <tr>
            <td style="width: 8pt;" colspan="13"><img src="<?= YII_PATH_PUBLIC . '/img/invoices/signature.png' ?>" /></td>
        </tr>
    </table>
</body>

</html>
