Example #1
0
 /**
  * Generate a receipt
  *
  * @return     void
  */
 public function receiptTask()
 {
     // Incoming
     $id = Request::getInt('id', 0);
     // Load the order
     $row = new Order($this->database);
     $row->load($id);
     // Instantiate an OrderItem object
     $oi = new OrderItem($this->database);
     if ($id) {
         // Get order items
         $orderitems = $oi->getOrderItems($id);
         if ($orderitems) {
             foreach ($orderitems as $r) {
                 $params = new Registry($r->params);
                 $selections = new Registry($r->selections);
                 // Get size selection
                 $r->sizes = $params->get('size', '');
                 $r->sizes = str_replace(' ', '', $r->sizes);
                 $r->selectedsize = trim($selections->get('size', ''));
                 $r->sizes = preg_split('/,/', $r->sizes);
                 $r->sizeavail = in_array($r->selectedsize, $r->sizes) ? 1 : 0;
                 // Get color selection
                 $r->colors = $params->get('color', '');
                 $r->colors = str_replace(' ', '', $r->colors);
                 $r->selectedcolor = trim($selections->get('color', ''));
                 $r->colors = preg_split('/,/', $r->colors);
             }
         } else {
             App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('Order empty, cannot generate receipt'), 'error');
             return;
         }
         $customer = User::getInstance($row->uid);
     } else {
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('Need order ID to issue a receipt'), 'error');
         return;
     }
     // Include needed libraries
     // require_once(JPATH_COMPONENT . DS . 'helpers' . DS . 'receipt.pdf.php');
     // Build the link displayed
     $sef = Route::url('index.php?option=' . $this->_option);
     if (substr($sef, 0, 1) == '/') {
         $sef = substr($sef, 1, strlen($sef));
     }
     $webpath = str_replace('/administrator/', '/', Request::base() . $sef);
     $webpath = str_replace('//', '/', $webpath);
     if (isset($_SERVER['HTTPS'])) {
         $webpath = str_replace('http:', 'https:', $webpath);
     }
     if (!strstr($webpath, '://')) {
         $webpath = str_replace(':/', '://', $webpath);
     }
     //require_once(PATH_CORE . DS . 'libraries/tcpdf/tcpdf.php');
     $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
     $receipt_title = $this->config->get('receipt_title') ? $this->config->get('receipt_title') : 'Your Order';
     $hubaddress = array();
     $hubaddress[] = $this->config->get('hubaddress_ln1') ? $this->config->get('hubaddress_ln1') : '';
     $hubaddress[] = $this->config->get('hubaddress_ln2') ? $this->config->get('hubaddress_ln2') : '';
     $hubaddress[] = $this->config->get('hubaddress_ln3') ? $this->config->get('hubaddress_ln3') : '';
     $hubaddress[] = $this->config->get('hubaddress_ln4') ? $this->config->get('hubaddress_ln4') : '';
     $hubaddress[] = $this->config->get('hubaddress_ln5') ? $this->config->get('hubaddress_ln5') : '';
     $hubaddress[] = $this->config->get('hubemail') ? $this->config->get('hubemail') : '';
     $hubaddress[] = $this->config->get('hubphone') ? $this->config->get('hubphone') : '';
     $headertext_ln1 = $this->config->get('headertext_ln1') ? $this->config->get('headertext_ln1') : '';
     $headertext_ln2 = $this->config->get('headertext_ln2') ? $this->config->get('headertext_ln2') : Config::get('sitename');
     $footertext = $this->config->get('footertext') ? $this->config->get('footertext') : 'Thank you for contributions to our HUB!';
     $receipt_note = $this->config->get('receipt_note') ? $this->config->get('receipt_note') : '';
     // Get front-end template name
     $sql = "SELECT template FROM `#__template_styles` WHERE `client_id`=0 AND `home`=1";
     $this->database->setQuery($sql);
     $tmpl = $this->database->loadResult();
     // set default header data
     $pdf->SetHeaderData(NULL, 0, strtoupper($receipt_title) . ' - #' . $id, NULL, array(84, 94, 124), array(146, 152, 169));
     $pdf->setFooterData(array(255, 255, 255), array(255, 255, 255));
     // set header and footer fonts
     $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
     $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
     // set margins
     $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
     $pdf->SetHeaderMargin(10);
     $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
     // set auto page breaks
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     // set image scale factor
     $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
     // Set font
     $pdf->SetFont('dejavusans', '', 11, '', true);
     $pdf->AddPage();
     // HTML content
     $this->view->setLayout('receipt');
     $this->view->hubaddress = $hubaddress;
     $this->view->headertext_ln1 = $headertext_ln1;
     $this->view->headertext_ln2 = $headertext_ln2;
     $this->view->receipt_note = $receipt_note;
     $this->view->receipt_title = $receipt_title;
     $this->view->option = $this->_option;
     $this->view->url = $webpath;
     $this->view->customer = $customer;
     $this->view->row = $row;
     $this->view->orderitems = $orderitems;
     $html = $this->view->loadTemplate();
     // output the HTML content
     $pdf->writeHTML($html, true, false, true, false, '');
     // ---------------------------------------------------------
     $dir = PATH_APP . DS . 'site' . DS . 'store' . DS . 'temp';
     $tempFile = $dir . DS . 'receipt_' . $id . '.pdf';
     if (!is_dir($dir)) {
         if (!\Filesystem::makeDirectory($dir)) {
             throw new Exception(Lang::txt('Failed to create folder to store receipts'), 500);
         }
     }
     // Close and output PDF document
     $pdf->Output($tempFile, 'F');
     if (is_file($tempFile)) {
         $xserver = new Server();
         $xserver->filename($tempFile);
         $xserver->serve_inline($tempFile);
         exit;
     } else {
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('There was an error creating a receipt'), 'error');
         return;
     }
     return;
 }