/** * 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; }
/** * editAction * Get a record and populate the application form * @return unknown_type */ public function editAction() { $form = $this->getForm('/admin/orders/process'); $currency = Shineisp_Registry::getInstance()->Zend_Currency; $customer = null; $createInvoiceConfirmText = $this->translator->translate('Are you sure you want to create or overwrite the invoice for this order?'); $id = intval($this->getRequest()->getParam('id')); $this->view->description = $this->translator->translate("Here you can edit the selected order."); if (!empty($id) && is_numeric($id)) { $rs = $this->orders->find($id); if (!empty($rs)) { $rs = $rs->toArray(); $rs['setupfee'] = Orders::getSetupfee($id); $rs['order_date'] = Shineisp_Commons_Utilities::formatDateOut($rs['order_date']); $rs['expiring_date'] = Shineisp_Commons_Utilities::formatDateOut($rs['expiring_date']); $rs['received_income'] = 0; $rs['missing_income'] = $rs['grandtotal']; $rs['order_number'] = !empty($rs['order_number']) ? $rs['order_number'] : Orders::formatOrderId($rs['order_id']); $payments = Payments::findbyorderid($id, 'income', true); if (isset($payments)) { foreach ($payments as $payment) { $rs['received_income'] += isset($payment['income']) ? $payment['income'] : 0; $rs['missing_income'] -= isset($payment['income']) ? $payment['income'] : 0; } } $rs['profit'] = $rs['total'] - $rs['cost']; $rs['profit'] = $currency->toCurrency($rs['profit'], array('currency' => Settings::findbyParam('currency'))); // set the default income to prepare the payment task $rs['income'] = $rs['missing_income']; $rs['missing_income'] = sprintf('%.2f', $rs['missing_income']); unset($payments); $parent = Customers::find($rs['customer_id']); //if customer comes from reseller if ($parent['parent_id']) { $rs['customer_parent_id'] = $parent['parent_id']; } else { $rs['customer_parent_id'] = $rs['customer_id']; } $link = Fastlinks::findlinks($id, $rs['customer_id'], 'Orders'); if (isset($link[0])) { $rs['fastlink'] = $link[0]['code']; $rs['visits'] = $link[0]['visits']; } $form->populate($rs); $this->view->id = $id; $this->view->customerid = $rs['customer_id']; if (!empty($rs['fastlink'])) { $this->view->titlelink = "/index/link/id/" . $rs['fastlink']; } if (!empty($rs['order_number'])) { $this->view->title = $this->translator->_("Order nr. %s", $rs['order_number']); } $this->view->messages = Messages::getbyOrderId($id); $createInvoiceConfirmText = $rs['missing_income'] > 0 ? $this->translator->translate('Are you sure you want to create or overwrite the invoice for this order? The order status is: not paid.') : $createInvoiceConfirmText; $customer = Customers::get_by_customerid($rs['customer_id'], 'company, firstname, lastname, email'); } else { $this->_helper->redirector('list', 'orders', 'admin'); } } $this->view->mex = urldecode($this->getRequest()->getParam('mex')); $this->view->mexstatus = $this->getRequest()->getParam('status'); // Create the buttons in the edit form $this->view->buttons = array(array("url" => "#", "label" => $this->translator->translate('Save'), "params" => array('id' => 'submit', 'css' => array('btn btn-success'))), array("url" => "/admin/orders/print/id/{$id}", "label" => $this->translator->translate('Print'), "params" => array('css' => null)), array("url" => "/admin/orders/dropboxit/id/{$id}", "label" => $this->translator->translate('Dropbox It'), "params" => array('css' => null)), array("url" => "/admin/orders/clone/id/{$id}", "label" => $this->translator->translate('Clone'), "params" => array('css' => null)), array("url" => "/admin/orders/sendorder/id/{$id}", "label" => $this->translator->translate('Email'), "params" => array('css' => array('btn btn-danger'))), array("url" => "/admin/orders/confirm/id/{$id}", "label" => $this->translator->translate('Delete'), "params" => array('css' => array('btn btn-danger'))), array("url" => "/admin/orders/new/", "label" => $this->translator->translate('New'), "params" => array('css' => null))); // Check if the order has been invoiced $invoice_id = Orders::isInvoiced($id); if ($invoice_id) { $this->view->buttons[] = array("url" => "/admin/orders/sendinvoice/id/{$invoice_id}", "label" => $this->translator->translate('Email invoice'), "params" => array('css' => array('btn btn-danger'))); $this->view->buttons[] = array("url" => "/admin/invoices/print/id/{$invoice_id}", "label" => $this->translator->translate('Print invoice'), "params" => array('css' => null)); } else { // Order not invoiced, show button to create a new invoice $this->view->buttons[] = array("url" => "/admin/orders/createinvoice/id/{$id}", "label" => $this->translator->translate('Invoice'), "params" => array('css' => array('btn btn-danger')), 'onclick' => "return confirm('" . $createInvoiceConfirmText . "')"); } $this->view->customer = array('records' => $customer, 'editpage' => 'customers'); $this->view->ordersdatagrid = $this->orderdetailGrid(); $this->view->paymentsdatagrid = $this->paymentsGrid(); $this->view->statushistory = StatusHistory::getStatusList($id); // Get Order status history $this->view->filesgrid = $this->filesGrid(); $this->view->statushistorygrid = $this->statusHistoryGrid(); $this->view->form = $form; $this->render('applicantform'); }