コード例 #1
0
ファイル: Invoices.php プロジェクト: kokkez/shineisp
 /**
  * print the invoice
  * @param unknown_type $invoiceid
  */
 public static function PrintPDF($invoice_id, $show = true, $force = false, $path = "/documents/invoices/")
 {
     $currency = Shineisp_Registry::getInstance()->Zend_Currency;
     $pdf = new Shineisp_Commons_PdfOrder();
     if (!is_numeric($invoice_id)) {
         return false;
     }
     $invoice = Invoices::find($invoice_id);
     if (!$invoice) {
         return false;
     }
     $invoice = $invoice->toArray();
     // Set the basepath for the file
     $Order = Doctrine::getTable('Orders')->findOneBy('order_id', $invoice['order_id']);
     $invoicePath = $path . $Order->isp_id . '/' . str_replace('-', '/', $invoice['invoice_id']);
     // Set the name of the file
     $filename = $invoicePath . '/' . $invoice['invoice_id'] . ".pdf";
     $filenameOld = $path . $invoice['invoice_date'] . " - " . $invoice['number'] . ".pdf";
     // 			Invoice already exists, we return it
     if ((file_exists(PUBLIC_PATH . $filename) || file_exists(PUBLIC_PATH . $filenameOld)) && $show && !$force) {
         $outputFilename = !empty($invoice['formatted_number']) ? $invoice['formatted_number'] . ".pdf" : $invoice['invoice_date'] . "_" . $invoice['number'] . ".pdf";
         header('Content-type: application/pdf');
         header('Content-Disposition: attachment; filename="' . $outputFilename . '"');
         $invoice = file_exists(PUBLIC_PATH . $filename) ? file_get_contents(PUBLIC_PATH . $filename) : file_get_contents(PUBLIC_PATH . $filenameOld);
         die($invoice);
     }
     $translator = Shineisp_Registry::getInstance()->Zend_Translate;
     $payments = Payments::findbyorderid($invoice['order_id'], null, true);
     $order = Orders::getAllInfo($invoice['order_id'], null, true);
     $database['header']['label'] = $translator->translate('Invoice No.') . " " . (!empty($invoice['formatted_number']) ? $invoice['formatted_number'] : $database['records']['invoice_number']) . " - " . Shineisp_Commons_Utilities::formatDateOut($invoice['invoice_date']);
     $database['columns'][] = array("value" => $translator->translate("SKU"), "size" => 20, "align" => "left", "key" => "sku");
     $database['columns'][] = array("value" => $translator->translate("Description"), "size" => 65, "align" => "left", "key" => "description");
     $database['columns'][] = array("value" => $translator->translate("Qty"), "size" => 2, "align" => "right", "key" => "qty");
     $database['columns'][] = array("value" => $translator->translate("Unit"), "size" => 12, "align" => "left", "key" => "unit");
     $database['columns'][] = array("value" => $translator->translate("Tax Free Price"), "size" => 9, "align" => "right", "key" => "taxfreeprice");
     $database['columns'][] = array("value" => $translator->translate("Discount"), "size" => 10, "align" => "right", "key" => "discount");
     $database['columns'][] = array("value" => $translator->translate("Setup fee"), "size" => 15, "align" => "right", "key" => "setup");
     $database['columns'][] = array("value" => $translator->translate("Total"), "size" => 18, "align" => "right", "key" => "total");
     if (isset($order[0])) {
         $orderinfo['order_number'] = !empty($order[0]['order_number']) ? $order[0]['order_number'] : Orders::formatOrderId($order[0]['order_id']);
         $orderinfo['invoice_id'] = $invoice['number'];
         $orderinfo['date'] = Shineisp_Commons_Utilities::formatDateOut($invoice['invoice_date']);
         //if customer comes from reseller
         if ($order[0]['Customers']['parent_id']) {
             $invoice_dest = Customers::getAllInfo($order[0]['Customers']['parent_id'], 'c.*, a.*');
             $orderinfo['customer']['customer_id'] = $invoice_dest['customer_id'];
             $orderinfo['customer']['company'] = $invoice_dest['company'];
             $orderinfo['customer']['firstname'] = $invoice_dest['firstname'];
             $orderinfo['customer']['lastname'] = $invoice_dest['lastname'];
             $orderinfo['customer']['vat'] = $invoice_dest['vat'];
             $orderinfo['customer']['taxpayernumber'] = $invoice_dest['taxpayernumber'];
             $orderinfo['customer']['email'] = $invoice_dest['email'];
             if (isset($invoice_dest['Addresses'][0])) {
                 $orderinfo['customer']['address'] = $invoice_dest['Addresses'][0]['address'];
                 $orderinfo['customer']['city'] = $invoice_dest['Addresses'][0]['city'];
                 $orderinfo['customer']['code'] = $invoice_dest['Addresses'][0]['code'];
                 $orderinfo['customer']['country'] = !empty($invoice_dest['Addresses'][0]['Countries']['name']) ? $invoice_dest['Addresses'][0]['Countries']['name'] : "";
             }
         } else {
             $orderinfo['customer']['customer_id'] = $order[0]['Customers']['customer_id'];
             $orderinfo['customer']['company'] = $order[0]['Customers']['company'];
             $orderinfo['customer']['firstname'] = $order[0]['Customers']['firstname'];
             $orderinfo['customer']['lastname'] = $order[0]['Customers']['lastname'];
             $orderinfo['customer']['vat'] = $order[0]['Customers']['vat'];
             $orderinfo['customer']['taxpayernumber'] = $order[0]['Customers']['taxpayernumber'];
             $orderinfo['customer']['email'] = $order[0]['Customers']['email'];
             if (isset($order[0]['Customers']['Addresses'][0])) {
                 $orderinfo['customer']['address'] = $order[0]['Customers']['Addresses'][0]['address'];
                 $orderinfo['customer']['city'] = $order[0]['Customers']['Addresses'][0]['city'];
                 $orderinfo['customer']['code'] = $order[0]['Customers']['Addresses'][0]['code'];
                 $orderinfo['customer']['country'] = $order[0]['Customers']['Addresses'][0]['Countries']['name'];
             }
         }
         $orderinfo['payments'] = array();
         if (count($payments) > 0) {
             if ($payments > 1) {
                 $numPayment = 1;
                 foreach ($payments as $payment) {
                     if ($numPayment++ > 10) {
                         break;
                     }
                     $payment['paymentdate'] = Shineisp_Commons_Utilities::formatDateOut($payment['paymentdate']);
                     $payment['income'] = $currency->toCurrency($payment['income'], array('currency' => Settings::findbyParam('currency')));
                     $orderinfo['payments'][] = $payment;
                 }
             }
             $orderinfo['payment_date'] = Shineisp_Commons_Utilities::formatDateOut($payments[0]['paymentdate']);
             $orderinfo['payment_mode'] = $payments[0]['Banks']['name'];
             $orderinfo['payment_description'] = $payments[0]['description'];
             $orderinfo['payment_transaction_id'] = $payments[0]['reference'];
         }
         $orderinfo['invoice_number'] = $invoice['number'];
         $orderinfo['company']['name'] = $order[0]['Isp']['company'];
         $orderinfo['company']['manager'] = $order[0]['Isp']['manager'];
         $orderinfo['company']['vat'] = $order[0]['Isp']['vatnumber'];
         $orderinfo['company']['bankname'] = $order[0]['Isp']['bankname'];
         $orderinfo['company']['iban'] = $order[0]['Isp']['iban'];
         $orderinfo['company']['bic'] = $order[0]['Isp']['bic'];
         $orderinfo['company']['address'] = $order[0]['Isp']['address'];
         $orderinfo['company']['zip'] = $order[0]['Isp']['zip'];
         $orderinfo['company']['city'] = $order[0]['Isp']['city'];
         $orderinfo['company']['country'] = $order[0]['Isp']['country'];
         $orderinfo['company']['telephone'] = $order[0]['Isp']['telephone'];
         $orderinfo['company']['fax'] = $order[0]['Isp']['fax'];
         $orderinfo['company']['website'] = $order[0]['Isp']['website'];
         $orderinfo['company']['email'] = $order[0]['Isp']['email'];
         $orderinfo['company']['slogan'] = $order[0]['Isp']['slogan'];
         $orderinfo['company']['custom1'] = $order[0]['Isp']['custom1'];
         $orderinfo['company']['custom2'] = $order[0]['Isp']['custom2'];
         $orderinfo['company']['custom3'] = $order[0]['Isp']['custom3'];
         $orderinfo['subtotal'] = $currency->toCurrency($order[0]['total'], array('currency' => Settings::findbyParam('currency')));
         $orderinfo['grandtotal'] = $currency->toCurrency($order[0]['grandtotal'], array('currency' => Settings::findbyParam('currency')));
         $orderinfo['vat'] = $currency->toCurrency($order[0]['vat'], array('currency' => Settings::findbyParam('currency')));
         $orderinfo['delivery'] = 0;
         if ($order[0]['status_id'] == Statuses::id("tobepaid", "orders")) {
             // To be payed
             $orderinfo['ribbon']['text'] = $translator->translate("To be Paid");
             $orderinfo['ribbon']['color'] = "#D60000";
         } elseif ($order[0]['status_id'] == Statuses::id("paid", "orders")) {
             // Paid
             $orderinfo['ribbon']['text'] = $translator->translate("Paid");
             $orderinfo['ribbon']['color'] = "#009926";
         } elseif ($order[0]['status_id'] == Statuses::id("complete", "orders")) {
             // Complete
             $orderinfo['ribbon']['text'] = $translator->translate("Complete");
             $orderinfo['ribbon']['color'] = "#009926";
         } else {
             $orderinfo['ribbon']['text'] = $translator->translate(Statuses::getLabel($order[0]['status_id']));
             $orderinfo['ribbon']['color'] = "#FFCC33";
         }
         $database['records'] = $orderinfo;
         foreach ($order[0]['OrdersItems'] as $item) {
             $billingCycle = BillingCycle::getAllinfo($item['billing_cycle_id']);
             if (!empty($billingCycle)) {
                 if (!empty($billingCycle['months']) && empty($item['tld_id'])) {
                     $price = $item['price'] * $billingCycle['months'] + $item['setupfee'];
                 } else {
                     $price = $item['price'] + $item['setupfee'];
                 }
             } else {
                 $price = $item['price'] * $item['quantity'] + $item['setupfee'];
             }
             $rowtotal = $price + $item['vat'];
             $item['price'] = $currency->toCurrency($item['price'], array('currency' => Settings::findbyParam('currency')));
             $item['setupfee'] = $currency->toCurrency($item['setupfee'], array('currency' => Settings::findbyParam('currency')));
             $rowtotal = $currency->toCurrency($rowtotal, array('currency' => Settings::findbyParam('currency')));
             if (!empty($item['discount'])) {
                 $item['discount'] = $item['discount'] . "%";
             }
             if (!empty($billingCycle['name']) && empty($item['tld_id'])) {
                 $item['date_end'] = Shineisp_Commons_Utilities::formatDateOut($item['date_end']);
                 $billingCycleName = $billingCycle['name'];
                 $item['description'] .= "<br/><br/> - " . $translator->translate('Expiring date') . ": " . $item['date_end'];
             } else {
                 $billingCycleName = "-";
             }
             // 					var_dump($item);
             $database['records']['items'][] = array($item['Products']['sku'], $item['description'], $item['quantity'], $billingCycleName, $item['price'], $item['discount'], $item['setupfee'], $rowtotal);
         }
         // 				var_dump($database['records']);
         // 				die;
         // Sanitize some fields
         $database['records']['invoice_number'] = !empty($database['records']['invoice_number']) ? $database['records']['invoice_number'] : "";
         $database['records']['formatted_number'] = !empty($invoice['formatted_number']) ? $invoice['formatted_number'] : $database['records']['invoice_number'];
         $database['records']['payment_description'] = !empty($database['records']['payment_description']) ? $database['records']['payment_description'] : "";
         $database['records']['payment_mode'] = !empty($database['records']['payment_mode']) ? $database['records']['payment_mode'] : "";
         $database['records']['payment_date'] = !empty($database['records']['payment_date']) ? $database['records']['payment_date'] : "";
         $database['records']['totalPayments'] = count($database['records']['payments']);
         // QRCode Image
         $code['order'] = $database['records']['order_number'];
         $code['customer'] = $database['records']['customer']['customer_id'];
         $jcode = base64_encode(json_encode($code));
         $database['records']['qrcode_url'] = $_SERVER['HTTP_HOST'] . "/index/qrcode/q/" . $jcode;
         $database['records']['skip_barcode'] = 1;
         if (!empty($database['records']['invoice_number'])) {
             $database['records']['barcode'] = $database['records']['invoice_number'];
             $database['records']['skip_barcode'] = 0;
         }
         if (isset($order[0])) {
             // Create the path structure
             if (!is_dir(PUBLIC_PATH . $invoicePath)) {
                 mkdir(PUBLIC_PATH . $invoicePath, 0700, true);
             }
             // Template name
             $templateName = Settings::findByParam('invoice_template');
             if (empty($templateName)) {
                 $templateName = Shineisp_Commons_Utilities::getFirstFile(PUBLIC_PATH . '/skins/commons/invoices', '/\\.phtml$/');
             }
             $invoiceview = new Shineisp_Invoice();
             $invoiceview->assign('header', $database['header']);
             $invoiceview->assign('columns', $database['columns']);
             $invoiceview->assign('data', $database['records']);
             $html = $invoiceview->render($templateName);
             $html2pdf = new HTML2PDF('P', 'A4', 'it', true, 'UTF-8', array(4, 4, 4, 1));
             $html2pdf->WriteHTML($html);
             $html2pdf->Output(PUBLIC_PATH . $filename, "F");
             // Execute a custom event
             self::events()->trigger('invoices_pdf_created', "Invoices", array('order' => $order, 'invoice' => $invoice, 'file' => $filename));
             return PUBLIC_PATH . $filename;
         }
     }
     return false;
 }