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 AdminPdfControllerCore 34 * 35 * @since 1.0.0 36 */ 37class AdminPdfControllerCore extends AdminController 38{ 39 /** 40 * Post processing 41 * 42 * @return void 43 * 44 * @since 1.0.0 45 */ 46 public function postProcess() 47 { 48 parent::postProcess(); 49 50 // We want to be sure that displaying PDF is the last thing this controller will do 51 exit; 52 } 53 54 /** 55 * Initialize processing 56 * 57 * @return void 58 * 59 * @since 1.0.0 60 */ 61 public function initProcess() 62 { 63 parent::initProcess(); 64 $this->checkCacheFolder(); 65 $access = Profile::getProfileAccess($this->context->employee->id_profile, (int) Tab::getIdFromClassName('AdminOrders')); 66 if ($access['view'] === '1' && ($action = Tools::getValue('submitAction'))) { 67 $this->action = $action; 68 } else { 69 $this->errors[] = Tools::displayError('You do not have permission to view this.'); 70 } 71 } 72 73 /** 74 * Check cache folder 75 * 76 * @return void 77 * 78 * @since 1.0.0 79 */ 80 public function checkCacheFolder() 81 { 82 if (!is_dir(_PS_CACHE_DIR_.'tcpdf/')) { 83 mkdir(_PS_CACHE_DIR_.'tcpdf/'); 84 } 85 } 86 87 /** 88 * Process generate invoice PDF 89 * 90 * @return void 91 * 92 * @since 1.0.0 93 */ 94 public function processGenerateInvoicePdf() 95 { 96 if (Tools::isSubmit('id_order')) { 97 $this->generateInvoicePDFByIdOrder(Tools::getValue('id_order')); 98 } elseif (Tools::isSubmit('id_order_invoice')) { 99 $this->generateInvoicePDFByIdOrderInvoice(Tools::getValue('id_order_invoice')); 100 } else { 101 die(Tools::displayError('The order ID -- or the invoice order ID -- is missing.')); 102 } 103 } 104 105 /** 106 * Generate PDF invoice by Order ID 107 * 108 * @param int $idOrder 109 * 110 * @return void 111 * 112 * @since 1.0.0 113 */ 114 public function generateInvoicePDFByIdOrder($idOrder) 115 { 116 $order = new Order((int) $idOrder); 117 if (!Validate::isLoadedObject($order)) { 118 die(Tools::displayError('The order cannot be found within your database.')); 119 } 120 121 $orderInvoiceList = $order->getInvoicesCollection(); 122 Hook::exec('actionPDFInvoiceRender', ['order_invoice_list' => $orderInvoiceList]); 123 $this->generatePDF($orderInvoiceList, PDF::TEMPLATE_INVOICE); 124 } 125 126 /** 127 * Generate PDF 128 * 129 * @param $object 130 * @param $template 131 * 132 * @return void 133 * 134 * @since 1.0.0 135 */ 136 public function generatePDF($object, $template) 137 { 138 $pdf = new PDF($object, $template, $this->context->smarty); 139 $pdf->render(); 140 } 141 142 /** 143 * Generate PDF Invoice by OrderInvoice ID 144 * 145 * @param int $idOrderInvoice 146 * 147 * @return void 148 * 149 * @since 1.0.0 150 */ 151 public function generateInvoicePDFByIdOrderInvoice($idOrderInvoice) 152 { 153 $orderInvoice = new OrderInvoice((int) $idOrderInvoice); 154 if (!Validate::isLoadedObject($orderInvoice)) { 155 die(Tools::displayError('The order invoice cannot be found within your database.')); 156 } 157 158 Hook::exec('actionPDFInvoiceRender', ['order_invoice_list' => [$orderInvoice]]); 159 $this->generatePDF($orderInvoice, PDF::TEMPLATE_INVOICE); 160 } 161 162 /** 163 * Generate Order Slip PDF 164 * 165 * @return void 166 * 167 * @since 1.0.0 168 */ 169 public function processGenerateOrderSlipPDF() 170 { 171 $orderSlip = new OrderSlip((int) Tools::getValue('id_order_slip')); 172 173 if ( ! Validate::isLoadedObject($orderSlip)) { 174 die(Tools::displayError('The order slip cannot be found within your database.')); 175 } 176 177 $this->generatePDF($orderSlip, PDF::TEMPLATE_ORDER_SLIP); 178 } 179 180 /** 181 * Process generate Delivery Slip PDF 182 * 183 * @return void 184 * 185 * @since 1.0.0 186 */ 187 public function processGenerateDeliverySlipPDF() 188 { 189 if (Tools::isSubmit('id_order')) { 190 $this->generateDeliverySlipPDFByIdOrder((int) Tools::getValue('id_order')); 191 } elseif (Tools::isSubmit('id_order_invoice')) { 192 $this->generateDeliverySlipPDFByIdOrderInvoice((int) Tools::getValue('id_order_invoice')); 193 } elseif (Tools::isSubmit('id_delivery')) { 194 $order = Order::getByDelivery((int) Tools::getValue('id_delivery')); 195 $this->generateDeliverySlipPDFByIdOrder((int) $order->id); 196 } else { 197 die(Tools::displayError('The order ID -- or the invoice order ID -- is missing.')); 198 } 199 } 200 201 /** 202 * Generate Delivery Slip PDF by Order ID 203 * 204 * @param int $idOrder 205 * 206 * @throws PrestaShopException 207 * 208 * @return void 209 * 210 * @since 1.0.0 211 */ 212 public function generateDeliverySlipPDFByIdOrder($idOrder) 213 { 214 $order = new Order((int) $idOrder); 215 if (!Validate::isLoadedObject($order)) { 216 throw new PrestaShopException('Can\'t load Order object'); 217 } 218 219 $orderInvoiceCollection = $order->getInvoicesCollection(); 220 $this->generatePDF($orderInvoiceCollection, PDF::TEMPLATE_DELIVERY_SLIP); 221 } 222 223 /** 224 * Generate Delivery Slip PDF by OrderInvoice ID 225 * 226 * @param int $idOrderInvoice 227 * 228 * @throws PrestaShopException 229 * 230 * @return void 231 * 232 * @since 1.0.0 233 */ 234 public function generateDeliverySlipPDFByIdOrderInvoice($idOrderInvoice) 235 { 236 $orderInvoice = new OrderInvoice((int) $idOrderInvoice); 237 if (!Validate::isLoadedObject($orderInvoice)) { 238 throw new PrestaShopException('Can\'t load Order Invoice object'); 239 } 240 241 $this->generatePDF($orderInvoice, PDF::TEMPLATE_DELIVERY_SLIP); 242 } 243 244 /** 245 * Generate PDF invoices 246 * 247 * @return void 248 * 249 * @since 1.0.0 250 */ 251 public function processGenerateInvoicesPDF() 252 { 253 $orderInvoiceCollection = OrderInvoice::getByDateInterval(Tools::getValue('date_from'), Tools::getValue('date_to')); 254 255 if (!count($orderInvoiceCollection)) { 256 die(Tools::displayError('No invoice was found.')); 257 } 258 259 $this->generatePDF($orderInvoiceCollection, PDF::TEMPLATE_INVOICE); 260 } 261 262 /** 263 * Generate PDF invoices 2 264 * 265 * @return void 266 * 267 * @since 1.0.0 268 */ 269 public function processGenerateInvoicesPDF2() 270 { 271 $orderInvoiceCollection = []; 272 foreach (explode('-', Tools::getValue('id_order_state')) as $idOrderState) { 273 if (is_array($orderInvoices = OrderInvoice::getByStatus((int) $idOrderState))) { 274 $orderInvoiceCollection = array_merge($orderInvoices, $orderInvoiceCollection); 275 } 276 } 277 278 if (!count($orderInvoiceCollection)) { 279 die(Tools::displayError('No invoice was found.')); 280 } 281 282 $this->generatePDF($orderInvoiceCollection, PDF::TEMPLATE_INVOICE); 283 } 284 285 /** 286 * Generate Order Slip PDFs 287 * 288 * @return void 289 * 290 * @since 1.0.0 291 */ 292 public function processGenerateOrderSlipsPDF() 293 { 294 $idOrderSlipsList = OrderSlip::getSlipsIdByDate(Tools::getValue('date_from'), Tools::getValue('date_to')); 295 if (!count($idOrderSlipsList)) { 296 die(Tools::displayError('No order slips were found.')); 297 } 298 299 $orderSlips = []; 300 foreach ($idOrderSlipsList as $idOrderSlips) { 301 $orderSlips[] = new OrderSlip((int) $idOrderSlips); 302 } 303 304 $this->generatePDF($orderSlips, PDF::TEMPLATE_ORDER_SLIP); 305 } 306 307 /** 308 * Generate Delivery Slip PDFs 309 * 310 * @return void 311 * 312 * @since 1.0.0 313 */ 314 public function processGenerateDeliverySlipsPDF() 315 { 316 $orderInvoiceCollection = OrderInvoice::getByDeliveryDateInterval(Tools::getValue('date_from'), Tools::getValue('date_to')); 317 318 if (!count($orderInvoiceCollection)) { 319 die(Tools::displayError('No invoice was found.')); 320 } 321 322 $this->generatePDF($orderInvoiceCollection, PDF::TEMPLATE_DELIVERY_SLIP); 323 } 324 325 /** 326 * Generate Supply Order Form PDFs 327 * 328 * @return void 329 * 330 * @since 1.0.0 331 */ 332 public function processGenerateSupplyOrderFormPDF() 333 { 334 if (!Tools::isSubmit('id_supply_order')) { 335 die(Tools::displayError('The supply order ID is missing.')); 336 } 337 338 $idSupplyOrder = (int) Tools::getValue('id_supply_order'); 339 $supplyOrder = new SupplyOrder($idSupplyOrder); 340 341 if (!Validate::isLoadedObject($supplyOrder)) { 342 die(Tools::displayError('The supply order cannot be found within your database.')); 343 } 344 345 $this->generatePDF($supplyOrder, PDF::TEMPLATE_SUPPLY_ORDER_FORM); 346 } 347} 348