示例#1
0
文件: pdf.php 项目: nemein/openpsa
 public static function render_and_attach_pdf(org_openpsa_invoices_invoice_dba $invoice)
 {
     if ($invoice->date == 0 || $invoice->deliverydate == 0) {
         $time = time();
         if ($invoice->date == 0) {
             $invoice->date = $time;
         }
         if ($invoice->deliverydate == 0) {
             $invoice->deliverydate = $time;
         }
         $invoice->update();
     }
     // renders the pdf and attaches it to the invoice
     $client_class = midcom_baseclasses_components_configuration::get('org.openpsa.invoices', 'config')->get('invoice_pdfbuilder_class');
     if (!class_exists($client_class)) {
         debug_add('Could not find PDF renderer, aborting silently', MIDCOM_LOG_INFO);
         return false;
     }
     $pdf_builder = new $client_class($invoice);
     // tmp filename
     $tmp_dir = $GLOBALS["midcom_config"]["midcom_tempdir"];
     $title = str_replace("#", "", $invoice->get_label());
     $tmp_file = $tmp_dir . "/" . $title . ".pdf";
     // render pdf to tmp filename
     $render = $pdf_builder->render($tmp_file);
     // cleanup old attachments
     $pdf_files = org_openpsa_helpers::get_attachment_urls($invoice, "pdf_file");
     if (count($pdf_files) > 0) {
         foreach ($pdf_files as $guid => $url) {
             $attachment = new midcom_db_attachment($guid);
             $attachment->delete();
         }
     }
     $attachment = $invoice->create_attachment($title . '.pdf', $title, "application/pdf");
     if (!$attachment) {
         debug_add("Failed to create invoice attachment for pdf", MIDCOM_LOG_ERROR);
         return false;
     }
     $copy = $attachment->copy_from_file($tmp_file);
     if (!$copy) {
         debug_add("Failed to copy pdf from " . $tmp_file . " to attachment", MIDCOM_LOG_ERROR);
         return false;
     }
     // set parameter for datamanager to find the pdf
     if (!$invoice->set_parameter("midcom.helper.datamanager2.type.blobs", "guids_pdf_file", $attachment->guid . ":" . $attachment->guid) || !$attachment->set_parameter('org.openpsa.invoices', 'auto_generated', md5_file($tmp_file))) {
         debug_add("Failed to create attachment parameters, last midgard error was: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR);
         return false;
     }
     return true;
 }
示例#2
0
文件: action.php 项目: nemein/openpsa
 private function _send_by_mail(org_openpsa_invoices_invoice_dba $invoice)
 {
     $customerCard = org_openpsa_widgets_contact::get($invoice->customerContact);
     $contactDetails = $customerCard->contact_details;
     $invoice_label = $invoice->get_label();
     $invoice_date = date($this->_l10n_midcom->get('short date'), $invoice->date);
     // generate pdf, only if not existing yet
     $pdf_files = org_openpsa_helpers::get_attachment_urls($invoice, "pdf_file");
     if (count($pdf_files) == 0) {
         org_openpsa_invoices_handler_pdf::render_and_attach_pdf($invoice);
     }
     $mail = new org_openpsa_mail();
     // define replacements for subject / body
     $mail->parameters = array("INVOICE_LABEL" => $invoice_label, "INVOICE_DATE" => $invoice_date, "FIRSTNAME" => $contactDetails["firstname"], "LASTNAME" => $contactDetails["lastname"]);
     $mail->to = $contactDetails["email"];
     $mail->from = $this->_config->get('invoice_mail_from_address');
     $mail->subject = $this->_config->get('invoice_mail_title');
     $mail->body = $this->_config->get('invoice_mail_body');
     // attach pdf to mail
     if ($mail->can_attach()) {
         $pdf_files = org_openpsa_helpers::get_attachment_urls($invoice, "pdf_file");
         if (count($pdf_files) > 0) {
             foreach ($pdf_files as $guid => $url) {
                 $att = array();
                 $att['name'] = basename($url) . ".pdf";
                 $att['mimetype'] = "application/pdf";
                 $fp = fopen($url, "r");
                 if (!$fp) {
                     //Failed to open attachment for reading, skip the file
                     continue;
                 }
                 $att['content'] = '';
                 while (!feof($fp)) {
                     $att['content'] .= fread($fp, 4096);
                 }
                 fclose($fp);
                 debug_add("adding attachment '{$att['name']}' to attachments array of invoice mail");
                 $mail->attachments[] = $att;
                 unset($att);
             }
         }
     }
     if (!$mail->send()) {
         $this->_request_data['message']['message'] = sprintf($this->_l10n->get('unable to deliver mail: %s'), $mail->get_error_message());
         return false;
     } else {
         $invoice->set_parameter($this->_component, 'sent_by_mail', time());
         return $this->_mark_as_sent($invoice);
     }
 }