<?php
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

require_once __DIR__ . "/../required.php";

use Medoo\Medoo;

redirectIfNotLoggedIn();

$registers = $database->select('registers', ['registerid (id)', 'registername (name)']);
?>

<div class="btn-toolbar">
    <a href="app.php?page=editregister" class="btn btn-success"><i class="fas fa-plus"></i> <?php lang("add register"); ?></a>
</div>

<table id="registertable" class="table table-bordered table-hover table-sm">
    <thead>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php lang('actions'); ?></th>
            <th data-priority="1"><i class="fas fa-fw fa-font d-none d-md-inline"></i> <?php lang('name'); ?></th>
            <th data-priority="2"><i class="fas fa-fw fa-balance-scale d-none d-md-inline"></i> <?php lang('balance'); ?></th>
            <th data-priority="3"><i class="fas fa-fw fa-play d-none d-md-inline"></i> <?php lang('last opened'); ?></th>
            <th data-priority="3"><i class="fas fa-fw fa-stop d-none d-md-inline"></i> <?php lang('closed'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($registers as $r) {
            $cashwhere = [
                'registerid' => $r['id'],
                'ORDER' => ['close' => "DESC"]
            ];
            if ($database->has('cash_drawer', ['AND' => ['registerid' => $r['id'], 'close' => null]])) {
                $cashwhere = ['AND' => ['registerid' => $r['id'], 'close' => null]];
            }
            $cash = $database->get('cash_drawer', [
                'cashid', 'open', 'close', 'start_amount', 'end_amount'
                    ], $cashwhere);
            $balance = 0.0;
            $open = "";
            $close = "";
            if ($cash === false) {
                $open = lang("never", false);
                $close = lang("never", false);
            } else {
                if (!is_null($cash['end_amount']) && !is_null($cash['close'])) {
                    $balance = (float) $cash['end_amount'];
                } else {
                    $balance = (float) $cash['start_amount'];
                    $rows = $database->select("payments", [
                        "[>]transactions" => ['txid' => 'txid']
                            ], 'amount', [
                        'AND' => [
                            'transactions.cashid' => $cash['cashid'],
                            'payments.type' => 1
                        ]
                    ]);
                    foreach ($rows as $row) {
                        $balance += $row;
                    }
                }
                $open = date(DATETIME_FORMAT, strtotime($cash['open']));
                $close = is_null($cash['close']) ? lang("still open", false) : date(DATETIME_FORMAT, strtotime($cash['close']));
            }
            ?>
            <tr>
                <td></td>
                <td>
                    <a class="btn btn-primary btn-sm" href="app.php?page=editregister&id=<?php echo $r['id']; ?>"><i class="fas fa-edit"></i> <?php lang("edit"); ?></a>
                    <?php
                    if (is_null($cash['close']) && !is_null($cash['open'])) {
                        ?>
                        <form action="action.php" method="POST" class="d-inline">
                            <input type="hidden" name="action" value="closecash" />
                            <input type="hidden" name="source" value="registers" />
                            <input type="hidden" name="register" value="<?php echo $r['id']; ?>" />
                            <button class="btn btn-danger btn-sm" type="submit"><i class="fas fa-stop"></i> <?php lang("close"); ?></button>
                        </form>
                        <?php
                    } else {
                        ?>
                        <button class="btn btn-success btn-sm btn-opencash" data-register="<?php echo $r['id']; ?>"><i class="fas fa-play"></i> <?php lang("open"); ?></button>
                        <?php
                    }
                    ?>
                </td>
                <td><?php echo $r['name']; ?></td>
                <td>$<?php echo number_format($balance, 2); ?></td>
                <td><?php echo $open; ?></td>
                <td><?php echo $close; ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php lang('actions'); ?></th>
            <th data-priority="1"><i class="fas fa-fw fa-font d-none d-md-inline"></i> <?php lang('name'); ?></th>
            <th data-priority="2"><i class="fas fa-fw fa-balance-scale d-none d-md-inline"></i> <?php lang('balance'); ?></th>
            <th data-priority="3"><i class="fas fa-fw fa-play d-none d-md-inline"></i> <?php lang('last opened'); ?></th>
            <th data-priority="3"><i class="fas fa-fw fa-stop d-none d-md-inline"></i> <?php lang('closed'); ?></th>
        </tr>
    </tfoot>
</table>

<form action="action.php" method="POST" class="d-none" id="opencashform">
    <input type="hidden" name="action" value="opencash" />
    <input type="hidden" name="source" value="registers" />
    <input type="hidden" name="startamount" value="" id="opencashstart" />
    <input type="hidden" name="register" value="" id="opencashregid" />
</form>