1<?php
2/**
3 * 2007-2016 PrestaShop
4 *
5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
6 * Copyright (C) 2017-2018 thirty bees
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Open Software License (OSL 3.0)
11 * that is bundled with this package in the file LICENSE.txt.
12 * It is also available through the world-wide-web at this URL:
13 * http://opensource.org/licenses/osl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * DISCLAIMER
19 *
20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21 * versions in the future. If you wish to customize PrestaShop for your
22 * needs please refer to https://www.thirtybees.com for more information.
23 *
24 * @author    thirty bees <contact@thirtybees.com>
25 * @author    PrestaShop SA <contact@prestashop.com>
26 * @copyright 2017-2018 thirty bees
27 * @copyright 2007-2016 PrestaShop SA
28 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29 *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30 */
31
32/**
33 * Class AdminOutstandingControllerCore
34 *
35 * @since 1.0.0
36 */
37class AdminOutstandingControllerCore extends AdminController
38{
39    /**
40     * AdminOutstandingControllerCore constructor.
41     *
42     * @since 1.0.0
43     */
44    public function __construct()
45    {
46        $this->bootstrap = true;
47        $this->table = 'order_invoice';
48        $this->className = 'OrderInvoice';
49        $this->addRowAction('view');
50
51        $this->context = Context::getContext();
52
53        $this->_select = '`id_order_invoice` AS `id_invoice`,
54		`id_order_invoice` AS `outstanding`,
55		CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
56		c.`outstanding_allow_amount`,
57		r.`color`,
58		rl.`name` AS `risk`';
59        $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_order` = a.`id_order`)
60		LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = o.`id_customer`)
61		LEFT JOIN `'._DB_PREFIX_.'risk` r ON (r.`id_risk` = c.`id_risk`)
62		LEFT JOIN `'._DB_PREFIX_.'risk_lang` rl ON (r.`id_risk` = rl.`id_risk` AND rl.`id_lang` = '.(int) $this->context->language->id.')';
63        $this->_where = 'AND number > 0';
64        $this->_use_found_rows = false;
65
66        $risks = [];
67        foreach (Risk::getRisks() as $risk) {
68            /** @var Risk $risk */
69            $risks[$risk->id] = $risk->name;
70        }
71
72        $this->fields_list = [
73            'number'                   => [
74                'title' => $this->l('Invoice'),
75            ],
76            'date_add'                 => [
77                'title'      => $this->l('Date'),
78                'type'       => 'date',
79                'align'      => 'right',
80                'filter_key' => 'a!date_add',
81            ],
82            'customer'                 => [
83                'title'          => $this->l('Customer'),
84                'filter_key'     => 'customer',
85                'tmpTableFilter' => true,
86            ],
87            'company'                  => [
88                'title' => $this->l('Company'),
89                'align' => 'center',
90            ],
91            'risk'                     => [
92                'title'       => $this->l('Risk'),
93                'align'       => 'center',
94                'orderby'     => false,
95                'type'        => 'select',
96                'color'       => 'color',
97                'list'        => $risks,
98                'filter_key'  => 'r!id_risk',
99                'filter_type' => 'int',
100            ],
101            'outstanding_allow_amount' => [
102                'title'  => $this->l('Outstanding Allowance'),
103                'align'  => 'center',
104                'prefix' => '<b>',
105                'suffix' => '</b>',
106                'type'   => 'price',
107            ],
108            'outstanding'              => [
109                'title'    => $this->l('Current Outstanding'),
110                'align'    => 'center',
111                'callback' => 'printOutstandingCalculation',
112                'orderby'  => false,
113                'search'   => false,
114            ],
115            'id_invoice'               => [
116                'title'    => $this->l('Invoice'),
117                'align'    => 'center',
118                'callback' => 'printPDFIcons',
119                'orderby'  => false,
120                'search'   => false,
121            ],
122        ];
123
124        parent::__construct();
125    }
126
127    /**
128     * Toolbar initialisation
129     *
130     * @return bool Force true (Hide New button)
131     */
132    public function initToolbar()
133    {
134        return true;
135    }
136
137    /**
138     * Column callback for print PDF incon
139     *
140     * @param int   $idInvoice Invoice ID
141     * @param array $tr        Row data
142     *
143     * @return string HTML content
144     */
145    public function printPDFIcons($idInvoice, $tr)
146    {
147        $this->context->smarty->assign(
148            [
149                'id_invoice' => $idInvoice,
150            ]
151        );
152
153        return $this->createTemplate('_print_pdf_icon.tpl')->fetch();
154    }
155
156    /**
157     * Print outstanding calculation
158     *
159     * @param int   $idInvoice
160     * @param array $tr
161     *
162     * @return string
163     * @throws PrestaShopException
164     *
165     * @since 1.0.0
166     */
167    public function printOutstandingCalculation($idInvoice, $tr)
168    {
169        $orderInvoice = new OrderInvoice($idInvoice);
170        if (!Validate::isLoadedObject($orderInvoice)) {
171            throw new PrestaShopException('object OrderInvoice cannot be loaded');
172        }
173        $order = new Order($orderInvoice->id_order);
174        if (!Validate::isLoadedObject($order)) {
175            throw new PrestaShopException('object Order cannot be loaded');
176        }
177        $customer = new Customer((int) $order->id_customer);
178        if (!Validate::isLoadedObject($orderInvoice)) {
179            throw new PrestaShopException('object Customer cannot be loaded');
180        }
181
182        return '<b>'.Tools::displayPrice($customer->getOutstanding(), $this->context->currency).'</b>';
183    }
184
185    /**
186     * View render
187     *
188     * @throws PrestaShopException Invalid objects
189     *
190     * @return string
191     *
192     * @since 1.0.0
193     */
194    public function renderView()
195    {
196        $orderInvoice = new OrderInvoice((int) Tools::getValue('id_order_invoice'));
197        if (!Validate::isLoadedObject($orderInvoice)) {
198            throw new PrestaShopException('object OrderInvoice cannot be loaded');
199        }
200        $order = new Order($orderInvoice->id_order);
201        if (!Validate::isLoadedObject($order)) {
202            throw new PrestaShopException('object Order cannot be loaded');
203        }
204
205        $link = $this->context->link->getAdminLink('AdminOrders');
206        $link .= '&vieworder&id_order='.$order->id;
207        $this->redirect_after = $link;
208        $this->redirect();
209
210        return '';
211    }
212}
213