function prepare_add_file($fieldname, $file_extension, $file_type, $file_id)
 {
     log_debug("template_engine", "Executing prepare_add_file({$fieldname}, {$file_extension}, {$file_type}, {$file_id})");
     $tmp_filename = file_generate_name("/tmp/{$fieldname}", $file_extension);
     // output file data
     $file_obj = new file_storage();
     $file_obj->data["type"] = $file_type;
     $file_obj->data["customid"] = $file_id;
     if (!$file_obj->load_data_bytype()) {
         log_write("error", "template_engine", "Unable to find company logo image - use the administration config page to upload a company logo");
         return 0;
     }
     $file_obj->filedata_write($tmp_filename);
     // work out the filename without path or extension
     //
     // we have to do this, since latex will not tolerate file extensions in the filename when
     // the file is referenced in the tex data.
     //
     preg_match("/\\S*\\/(\\S*).{$file_extension}\$/", $tmp_filename, $matches);
     $tmp_filename_short = $matches[1];
     // map fieldname to filename
     $this->data_files[$fieldname]["filename"] = $tmp_filename;
     $this->data_files[$fieldname]["filename_short"] = $tmp_filename_short;
     log_debug("template_engine", "Wrote tempory file {$tmp_filename} with shortname of {$tmp_filename_short}");
     return 1;
 }
Example #2
0
 function action_delete()
 {
     log_debug("journal_process", "Executing action_delete()");
     /*
     	Start Transaction
     */
     $sql_obj = new sql_query();
     $sql_obj->trans_begin();
     /*
     	Delete files (if applicable)
     */
     if ($this->structure["type"] == "file") {
         $file_obj = new file_storage();
         $file_obj->data["type"] = "journal";
         $file_obj->data["customid"] = $this->structure["id"];
         $file_obj->load_data_bytype();
         $file_obj->action_delete();
     }
     /*
     	Delete journal record
     */
     $sql_obj->string = "DELETE FROM `journal` WHERE id='" . $this->structure["id"] . "' LIMIT 1";
     $sql_obj->execute();
     /*
     	Commit
     */
     if (error_check()) {
         log_write("error", "journal_process", "An error occured preventing the journal record from being deleted");
         $sql_obj->trans_rollback();
         return 0;
     } else {
         log_write("notification", "journal_process", "Journal record cleanly removed.");
         $sql_obj->trans_commit();
         return 1;
     }
     return 0;
 }
Example #3
0
 function email_invoice($email_sender, $email_to, $email_cc, $email_bcc, $email_subject, $email_message)
 {
     log_debug("invoice", "Executing email_invoice([options])");
     // external dependency of Mail_Mime
     if (!@(include_once 'Mail.php')) {
         log_write("error", "invoice", "Unable to find Mail module required for sending email");
         return 0;
     }
     if (!@(include_once 'Mail/mime.php')) {
         log_write("error", "invoice", "Unable to find Mail::Mime module required for sending email");
         return 0;
     }
     // track attachment files to tidy up
     $file_attachments = array();
     /*
     	Prepare Email Mime Data & Headers
     */
     // fetch sender address
     //
     // users have the choice of sending as the company or as their own staff email address & name.
     //
     if ($email_sender == "user") {
         // send as the user
         $email_sender = "\"" . user_information("realname") . "\" <" . user_information("contact_email") . ">";
     } else {
         // send as the system
         $email_sender = "\"" . sql_get_singlevalue("SELECT value FROM config WHERE name='COMPANY_NAME'") . "\" <" . sql_get_singlevalue("SELECT value FROM config WHERE name='COMPANY_CONTACT_EMAIL'") . ">";
     }
     // prepare headers
     $mail_headers = array('From' => $email_sender, 'Subject' => $email_subject, 'Cc' => $email_cc, 'Bcc' => $email_bcc);
     $mail_mime = new Mail_mime("\n");
     $mail_mime->setTXTBody($email_message);
     /*
     	Generate a PDF of the invoice and save to tmp file
     */
     log_debug("invoice", "Generating invoice PDF for emailing");
     // generate PDF
     $this->generate_pdf();
     if (error_check()) {
         return 0;
     }
     // save to a temporary file
     if ($this->type == "ar") {
         $tmp_file_invoice = file_generate_name($GLOBALS["config"]["PATH_TMPDIR"] . "/invoice_" . $this->data["code_invoice"] . "", "pdf");
     } else {
         $tmp_file_invoice = file_generate_name($GLOBALS["config"]["PATH_TMPDIR"] . "/quote_" . $this->data["code_quote"] . "", "pdf");
         //$email_template	= sql_get_singlevalue("SELECT value FROM config WHERE name IN('TEMPLATE_QUOTE_EMAIL') LIMIT 1");
     }
     if (!($fhandle = fopen($tmp_file_invoice, "w"))) {
         log_write("error", "invoice", "A fatal error occured whilst writing invoice PDF to file {$tmp_file_invoice}, unable to send email");
         return 0;
     }
     fwrite($fhandle, $this->obj_pdf->output);
     fclose($fhandle);
     // attach
     $mail_mime->addAttachment($tmp_file_invoice, 'application/pdf');
     $file_attachments[] = $tmp_file_invoice;
     /*
     	Fetch Extra Attachments
     
     	Certain billing processes may add file attachments to the journal that should be sent along with the invoice
     	when an email is generated.
     
     	Here we grab those file attachments and send each one.
     */
     $obj_sql_journal = new sql_query();
     $obj_sql_journal->string = "SELECT id FROM journal WHERE journalname='account_ar' AND customid='" . $this->id . "' AND title LIKE 'SERVICE:%'";
     $obj_sql_journal->execute();
     if ($obj_sql_journal->num_rows()) {
         $obj_sql_journal->fetch_array();
         foreach ($obj_sql_journal->data as $data_journal) {
             // there are journaled attachments to send
             //
             // we don't care about any of the journal data, we just need to pull the file attachment from
             // storage, write to disk and then attach to the email
             //
             // fetch file object
             $file_obj = new file_storage();
             $file_obj->data["type"] = "journal";
             $file_obj->data["customid"] = $data_journal["id"];
             if (!$file_obj->load_data_bytype()) {
                 log_write("error", "inc_invoices", "Unable to load file from journal to attach to invoice email - possible file storage issue?");
                 return 0;
             }
             $file_extension = format_file_extension($file_obj->data["file_name"]);
             $file_name = format_file_noextension($file_obj->data["file_name"]);
             $file_ctype = format_file_contenttype($file_extension);
             // we have to write the file to disk before attaching it
             $tmp_file_attach = file_generate_name($GLOBALS["config"]["PATH_TMPDIR"] . "/" . $file_name, $file_extension);
             if (!$file_obj->filedata_write($tmp_file_attach)) {
                 log_write("error", "inc_invoices", "Unable to write file attachments from journal to tmp space");
                 return 0;
             }
             // add to the invoice
             $mail_mime->addAttachment($tmp_file_attach, $file_ctype);
             $file_attachments[] = $tmp_file_attach;
             // cleanup - tmp file will be removed ;ater
             unset($file_obj);
         }
         // end of for each journal item
     }
     // end if sendable journal items
     unset($obj_sql_journal);
     /*
     	Email the invoice
     */
     log_write("debug", "invoice", "Sending generated email....");
     $mail_body = $mail_mime->get();
     $mail_headers = $mail_mime->headers($mail_headers);
     $mail =& Mail::factory('mail', "-f " . $GLOBALS["config"]["COMPANY_CONTACT_EMAIL"]);
     $status = $mail->send($email_to, $mail_headers, $mail_body);
     if (PEAR::isError($status)) {
         log_write("error", "inc_invoice", "An error occured whilst attempting to send the email: " . $status->getMessage() . "");
     } else {
         log_write("debug", "inc_invoice", "Successfully sent email invoice");
         /*
         	Start SQL Transaction to post email to journal
         */
         $sql_obj = new sql_query();
         $sql_obj->trans_begin();
         /*
         	Mark the invoice as having been sent
         */
         $sql_obj = new sql_query();
         $sql_obj->string = "UPDATE account_" . $this->type . " SET date_sent='" . date("Y-m-d") . "', sentmethod='email' WHERE id='" . $this->id . "'";
         $sql_obj->execute();
         /*
         	Add the email information to the journal, including attaching a copy
         	of the generated PDF
         */
         log_write("debug", "inc_invoice", "Uploading PDF and email details to journal...");
         // create journal entry
         $journal = new journal_process();
         $journal->prepare_set_journalname("account_" . $this->type);
         $journal->prepare_set_customid($this->id);
         $journal->prepare_set_type("file");
         $journal->prepare_set_title("EMAIL: {$email_subject}");
         $data["content"] = NULL;
         $data["content"] .= "To:\t" . $email_to . "\n";
         $data["content"] .= "Cc:\t" . $email_cc . "\n";
         $data["content"] .= "Bcc:\t" . $email_bcc . "\n";
         $data["content"] .= "From:\t" . $email_sender . "\n";
         $data["content"] .= "\n";
         $data["content"] .= $email_message;
         $data["content"] .= "\n";
         $journal->prepare_set_content($data["content"]);
         $journal->action_update();
         // create journal entry
         $journal->action_lock();
         // lock it to prevent any changes to historical record of delivered email
         // upload PDF file as an attachement
         $file_obj = new file_storage();
         $file_obj->data["type"] = "journal";
         $file_obj->data["customid"] = $journal->structure["id"];
         if (!$file_obj->action_update_file($tmp_file_invoice)) {
             log_write("error", "inc_invoice", "Unable to upload emailed PDF to journal entry");
         }
         /*
         	Commit
         */
         if (error_check()) {
             $sql_obj->trans_rollback();
         } else {
             $sql_obj->trans_commit();
         }
     }
     // end if successful send
     // cleanup - remove the temporary files
     log_debug("inc_invoice", "Performing cleanup, removing temporary files used for emails");
     foreach ($file_attachments as $filename) {
         log_debug("inc_invoice", "Removing tmp file {$filename}");
         unlink($filename);
     }
     // return
     if (error_check()) {
         return 0;
     } else {
         return 1;
     }
 }
 	We have already loaded the data for all the fields, so simply need to go and set all the values
 	based on the naming of the $data array.
 */
 foreach (array_keys($data) as $data_key) {
     $sql_obj->string = "UPDATE config SET value='" . $data[$data_key] . "' WHERE name='{$data_key}' LIMIT 1";
     $sql_obj->execute();
 }
 /*
 	Update the logo file if required
 */
 if ($_FILES["COMPANY_LOGO"]["size"] > 1) {
     // set file variables
     $file_obj->data["type"] = "COMPANY_LOGO";
     $file_obj->data["customid"] = "0";
     // see if a file already exists
     if ($file_obj->load_data_bytype()) {
         log_debug("process", "Old file exists, will overwrite.");
     } else {
         log_debug("process", "No previous file exists, performing clean upload.");
     }
     // force the filename
     $file_obj->data["file_name"] = "company_logo.png";
     // call the upload function
     if (!$file_obj->action_update_form("COMPANY_LOGO")) {
         log_write("error", "process", "Unable to upload company logo");
     }
 }
 /*
 	Commit
 */
 if (error_check()) {