<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2023 PrestaShop SA
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

use PrestaShop\Module\AgileMultisellerPro\Adapter\SellerOrderInvoice;
use PrestaShop\Module\AgileMultisellerPro\Controller\Front\AgileModuleFrontController;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoCartCarrier;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoOrder;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoProduct;

if (!defined('_PS_VERSION_')) {
    exit;
}

class AgileMultiSellerProSellerPdfInvoiceModuleFrontController extends AgileModuleFrontController
{
    protected $display_header = false;
    protected $display_footer = false;

    public $content_only = true;

    protected $template;
    public $filename;

    public function postProcess()
    {
        $id_order = (int) Tools::getValue('id_order');

        if (!(int) Configuration::get('PS_INVOICE')) {
            exit($this->trans('Invoices are disabled in this shop.', [], 'Modules.Agilemultisellerpro.Orders'));
        }

        if (isset($id_order) && Validate::isUnsignedId($id_order)) {
            $order = new Order($id_order);
        }

        if (!isset($order) || !Validate::isLoadedObject($order)) {
            exit($this->trans('Invoice not found', [], 'Modules.Agilemultisellerpro.Orders'));
        }

        $sellerInfoOrderRepository = $this->entityManager->getRepository(SellerinfoOrder::class);

        $sellerInfoOrder = $sellerInfoOrderRepository->findOneBy([
            'sellerInfo' => $this->sellerinfo,
            'idOrder' => $id_order,
        ]);

        if (!$sellerInfoOrder) {
            exit($this->trans('You do not have permission to see this invoice', [], 'Modules.Agilemultisellerpro.Orders'));
        }

        if (Tools::isSubmit('secure_key') && $order->secure_key != Tools::getValue('secure_key')) {
            exit($this->trans('You do not have permission to see this invoice', [], 'Modules.Agilemultisellerpro.Orders'));
        }

        if (!OrderState::invoiceAvailable($order->getCurrentState()) && !$order->invoice_number) {
            exit($this->trans('No invoice available', [], 'Modules.Agilemultisellerpro.Orders'));
        }

        $this->order = $order;
    }

    public function display()
    {
        $order_invoices = new PrestaShopCollection(SellerOrderInvoice::class);
        $order_invoice_list = $order_invoices->where('id_order', '=', $this->order->id);

        Hook::exec('actionPDFSellerInvoiceRender', ['order_invoice_list' => $order_invoice_list]);

        HTMLTemplateSellerInvoice::$seller = $this->sellerinfo;
        HTMLTemplateSellerInvoice::$sellerProductRepository = $this->entityManager->getRepository(SellerinfoProduct::class);

        $sellerinfoCartCarrierRepository = $this->entityManager->getRepository(SellerinfoCartCarrier::class);

        $sellerinfoCartCarrier = $sellerinfoCartCarrierRepository->findOneBy([
            'idSellerInfo' => $this->sellerinfo->getIdSellerinfo(),
            'idCart' => $this->order->id_cart,
        ]);
        HTMLTemplateSellerInvoice::$sellerinfoCartCarrier = $sellerinfoCartCarrier;

        $pdf = new PDF(
            $order_invoice_list,
            'SellerInvoice',
            // PDF::TEMPLATE_INVOICE,
            // $this->context->smarty,
            HTMLTemplateSellerInvoice::getSmarty(),
            $this->context->language->id
        );

        $pdf->render();
    }

    public function getTemplate($iso_country)
    {
        $template = _PS_THEME_PDF_DIR_ . '/invoice.tpl';

        $iso_template = _PS_THEME_PDF_DIR_ . '/invoice.' . $iso_country . '.tpl';

        if (file_exists($iso_template)) {
            $template = $iso_template;
        }

        return $template;
    }
}
