<?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__ . "/receipts.php";

class GenerateReceipt {

    const RECEIPT_TYPE_TRANSACTION = 1;
    const RECEIPT_TYPE_X = 2;
    const RECEIPT_TYPE_Z = 3;

    private static function saleReceipt($transaction) {
        global $database;
        $receipt = new Receipt();
        $tx = $database->get('transactions', ['txid', 'txdate', 'customerid', 'type', 'cashier', 'discountpercent'], ['txid' => $transaction]);
        // Info
        $txid = $tx['txid'];
        $datetime = date(DATETIME_FORMAT, strtotime($tx['txdate']));
        $type = $tx['type'];
        $cashier = getUserByID($tx['cashier'])['name'];
        $customerid = $tx['customerid'];

        // Items
        $itemlines = [];
        $items = $database->select('lines', ['amount', 'name', 'itemid', 'qty'], ['txid' => $txid]);
        $subtotal = 0.0;
        $paid = 0.0;
        foreach ($items as $i) {
            $itemlines[] = new ReceiptLine(
                    $i['name'], (float) $i['qty'] . '@' . number_format($i['amount'], 2), '$' . number_format($i['qty'] * $i['amount'] * 1.0, 2)
            );
            $subtotal += $i['qty'] * $i['amount'] * 1.0;
        }

        // Payments
        $total = $subtotal * (1.0 - ((float) $tx['discountpercent'] / 100));
        $paymentlines = [];
        $payments = $database->select('payments', [
            '[>]payment_types' => ['type' => 'typeid']
                ], [
            'amount', 'type', 'typename', 'text'
                ], [
            'txid' => $txid
        ]);
        foreach ($payments as $p) {
            if ($p['amount'] < 0) {
                continue;
            }
            $paymentlines[] = new ReceiptLine(lang($p['text'], false), "", '$' . number_format($p['amount'] * 1.0, 2));
            $paid += $p['amount'] * 1.0;
        }
        $change = $paid - $total;
        if ($change <= 0) {
            $change = 0.0;
        }

        // Totals
        $subtotalline = new ReceiptLine("Subtotal:", "", '$' . number_format($subtotal, 2));
        $paidline = new ReceiptLine("Paid:", "", '$' . number_format($paid, 2), ReceiptLine::LINEFORMAT_BOLD);
        $changeline = new ReceiptLine("Change:", "", '$' . number_format($change, 2), ReceiptLine::LINEFORMAT_BOLD);
        $totalline = new ReceiptLine("Total:", "", '$' . number_format($subtotal, 2), ReceiptLine::LINEFORMAT_BOLD);

        $receipt->appendLine(new ReceiptLine("Date: $datetime"));
        $receipt->appendLine(new ReceiptLine("Tx. ID: $txid"));
        $receipt->appendLine(new ReceiptLine("Cashier: $cashier"));
        if (!is_null($customerid) && !empty($customerid)) {
            $customer = $database->get('customers', 'name', ['customerid' => $customerid]);
            $receipt->appendLine(new ReceiptLine("Customer: $customer"));
        }
        $receipt->appendBreak();
        $receipt->appendLines($itemlines);
        $receipt->appendBreak();
        $receipt->appendLine($subtotalline);
        if ($tx['discountpercent'] > 0) {
            $receipt->appendLine(new ReceiptLine("Discount:", "", (float) $tx['discountpercent'] . '% off'));
            $totalline = new ReceiptLine("Total:", "", '$' . number_format($total, 2), ReceiptLine::LINEFORMAT_BOLD);
        }
        $receipt->appendLine($totalline);
        $receipt->appendBreak();
        $receipt->appendLines($paymentlines);
        $receipt->appendBreak();
        $receipt->appendLine($paidline);
        $receipt->appendLine($changeline);

        return $receipt;
    }

    private static function returnReceipt($transaction) {
        global $database;
        $receipt = new Receipt();
        $tx = $database->get('transactions', ['txid', 'txdate', 'customerid', 'type', 'cashier', 'discountpercent'], ['txid' => $transaction]);
        // Info
        $txid = $tx['txid'];
        $datetime = date(DATETIME_FORMAT, strtotime($tx['txdate']));
        $type = $tx['type'];
        $cashier = getUserByID($tx['cashier'])['name'];
        $customerid = $tx['customerid'];

        // Items
        $itemlines = [];
        $items = $database->select('lines', ['amount', 'name', 'itemid', 'qty'], ['txid' => $txid]);
        $subtotal = 0.0;
        $paid = 0.0;
        foreach ($items as $i) {
            $itemlines[] = new ReceiptLine(
                    $i['name'], (float) $i['qty'] . '@' . number_format($i['amount'], 2), '$' . number_format($i['qty'] * $i['amount'] * 1.0, 2)
            );
            $subtotal += $i['qty'] * $i['amount'] * 1.0;
        }

        // Payments
        $total = $subtotal * (1.0 - ((float) $tx['discountpercent'] / 100));
        $paymentlines = [];
        $payments = $database->select('payments', [
            '[>]payment_types' => ['type' => 'typeid']
                ], [
            'amount', 'type', 'typename', 'text'
                ], [
            'txid' => $txid
        ]);
        foreach ($payments as $p) {
            $paymentlines[] = new ReceiptLine(lang($p['text'], false), "", '$' . number_format($p['amount'] * -1.0, 2));
            $paid += $p['amount'] * 1.0;
        }

        // Totals
        $subtotalline = new ReceiptLine("Subtotal:", "", '$' . number_format($subtotal, 2));
        $paidline = new ReceiptLine("Refund:", "", '$' . number_format($paid * -1.0, 2), ReceiptLine::LINEFORMAT_BOLD);
        $totalline = new ReceiptLine("Total:", "", '$' . number_format($subtotal, 2), ReceiptLine::LINEFORMAT_BOLD);

        $receipt->appendLine(new ReceiptLine("Date: $datetime"));
        $receipt->appendLine(new ReceiptLine("Tx. ID: $txid"));
        $receipt->appendLine(new ReceiptLine("Cashier: $cashier"));
        if (!is_null($customerid) && !empty($customerid)) {
            $customer = $database->get('customers', 'name', ['customerid' => $customerid]);
            $receipt->appendLine(new ReceiptLine("Customer: $customer"));
        }
        $receipt->appendBreak();
        $receipt->appendLines($itemlines);
        $receipt->appendBreak();
        $receipt->appendLine($subtotalline);
        $receipt->appendLine($totalline);
        $receipt->appendBreak();
        $receipt->appendLines($paymentlines);
        $receipt->appendBreak();
        $receipt->appendLine($paidline);

        return $receipt;
    }

    static function transactionReceipt($transaction) {
        global $database;
        $txtype = $database->get('transactions', 'type', ['txid' => $transaction]);
        switch ($txtype) {
            case 1:
                return GenerateReceipt::saleReceipt($transaction);
            case 2:
                return GenerateReceipt::returnReceipt($transaction);
            default:
                return new Receipt();
        }
    }

    static function xReceipt($registerid) {
        global $database;
        $receipt = new Receipt();

        $registername = $database->get('registers', 'registername', ['registerid' => $registerid]);
        $cash = $database->get('cash_drawer', ['open', 'start_amount', 'cashid'], ['AND' => ['open[!]' => null, 'close' => null, 'registerid' => $registerid]]);

        $balance = [];
        $paymenttypes = $database->select('payment_types', ['typename (type)', 'text']);
        $payments = $database->select("payments", [
            "[>]transactions" => ['txid' => 'txid'],
            "[>]payment_types" => ['type' => 'typeid']
                ], ['amount', 'typename (type)'], [
            'AND' => [
                'transactions.cashid' => $cash['cashid'],
            ]
        ]);
        $transactioncount = $database->count('transactions', ['cashid' => $cash['cashid']]);
        foreach ($paymenttypes as $t) {
            $balance[$t['type']] = 0.0;
        }
        foreach ($payments as $p) {
            $balance[$p['type']] += $p['amount'];
        }

        $receipt->appendHeader(new ReceiptLine(lang("x report", false), "", "", ReceiptLine::LINEFORMAT_BOLD | ReceiptLine::LINEFORMAT_CENTER));

        $receipt->appendLine(new ReceiptLine("Printed:", "", date(DATETIME_FORMAT)));
        $receipt->appendLine(new ReceiptLine("Register:", "", $registername));
        $receipt->appendLine(new ReceiptLine("Transactions:", "", $transactioncount));

        $receipt->appendBlank();
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Opening", "", "", ReceiptLine::LINEFORMAT_CENTER));
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Date:", "", date(DATETIME_FORMAT, strtotime($cash['open']))));
        $receipt->appendLine(new ReceiptLine("Cash:", "", '$' . number_format($cash['start_amount'], 2)));

        $receipt->appendBlank();
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Sales", "", "", ReceiptLine::LINEFORMAT_CENTER));
        $receipt->appendBreak();
        foreach ($paymenttypes as $t) {
            $receipt->appendLine(new ReceiptLine(lang($t['text'], false) . ":", "", '$' . number_format($balance[$t['type']], 2)));
        }

        $receipt->appendBlank();
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Balance", "", "", ReceiptLine::LINEFORMAT_CENTER));
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Cash:", "", '$' . number_format($balance['cash'] + $cash['start_amount'], 2)));

        return $receipt;
    }

    static function zReceipt($cashid) {
        global $database;
        $receipt = new Receipt();

        $cash = $database->get('cash_drawer', ['open', 'close', 'start_amount', 'end_amount', 'cashid', 'registerid'], ['cashid' => $cashid]);
        $registername = $database->get('registers', 'registername', ['registerid' => $cash['registerid']]);

        $balance = [];
        $paymenttypes = $database->select('payment_types', ['typename (type)', 'text']);
        $payments = $database->select("payments", [
            "[>]transactions" => ['txid' => 'txid'],
            "[>]payment_types" => ['type' => 'typeid']
                ], ['amount', 'typename (type)'], [
            'AND' => [
                'transactions.cashid' => $cash['cashid'],
            ]
        ]);
        $transactioncount = $database->count('transactions', ['cashid' => $cashid]);
        foreach ($paymenttypes as $t) {
            $balance[$t['type']] = 0.0;
        }
        foreach ($payments as $p) {
            $balance[$p['type']] += $p['amount'];
        }

        $receipt->appendHeader(new ReceiptLine(lang("z report", false), "", "", ReceiptLine::LINEFORMAT_BOLD | ReceiptLine::LINEFORMAT_CENTER));

        $receipt->appendLine(new ReceiptLine("Printed:", "", date(DATETIME_FORMAT)));
        $receipt->appendLine(new ReceiptLine("Register:", "", $registername));
        $receipt->appendLine(new ReceiptLine("Transactions:", "", $transactioncount));

        $receipt->appendBlank();
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Opening", "", "", ReceiptLine::LINEFORMAT_CENTER));
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Date:", "", date(DATETIME_FORMAT, strtotime($cash['open']))));
        $receipt->appendLine(new ReceiptLine("Cash:", "", '$' . number_format($cash['start_amount'], 2)));

        $receipt->appendBlank();
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Closing", "", "", ReceiptLine::LINEFORMAT_CENTER));
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Date:", "", date(DATETIME_FORMAT, strtotime($cash['close']))));
        $receipt->appendLine(new ReceiptLine("Cash:", "", '$' . number_format($cash['end_amount'], 2)));

        $receipt->appendBlank();
        $receipt->appendBreak();
        $receipt->appendLine(new ReceiptLine("Sales", "", "", ReceiptLine::LINEFORMAT_CENTER));
        $receipt->appendBreak();
        foreach ($paymenttypes as $t) {
            $receipt->appendLine(new ReceiptLine(lang($t['text'], false) . ":", "", '$' . number_format($balance[$t['type']], 2)));
        }

        return $receipt;
    }

    static function getReceipt($type, $data) {
        switch ($type) {
            case GenerateReceipt::RECEIPT_TYPE_X:
                return GenerateReceipt::xReceipt($data);
                break;
            case GenerateReceipt::RECEIPT_TYPE_Z:
                return GenerateReceipt::zReceipt($data);
                break;
            case GenerateReceipt::RECEIPT_TYPE_TRANSACTION:
                return GenerateReceipt::transactionReceipt($data);
                break;
            default:
                return new Receipt();
                break;
        }
    }

    static function outputReceipt(Receipt $receipt, $format = "html", $width = 64, $title = "") {
        switch ($format) {
            case "text":
                $format = "text";
                header("Content-Type: text/plain");
                break;
            case "json":
                $format = "json";
                header("Content-Type: application/json");
                break;
            default:
                $format = "html";
                header("Content-Type: text/html");
        }

        $output = "";
        switch ($format) {
            case "text":
                $output = $receipt->getPlainText($width);
                break;
            case "json":
                $output = $receipt->getJson($width);
                break;
            default:
                $output = $receipt->getHtml($title);
        }
        return $output;
    }

}
