diff -Naur storminvoice/storminvoice.admin.inc storminvoiceNew/storminvoice.admin.inc --- storminvoice/storminvoice.admin.inc 2009-06-22 18:22:16.000000000 +0200 +++ storminvoiceNew/storminvoice.admin.inc 2009-07-02 02:00:25.000000000 +0200 @@ -430,6 +430,195 @@ return $form; } + +/** + * Page + */ +function storminvoice_send_page($node, $language) { + $breadcrumb = array(); + $breadcrumb[] = l(t('Storm'), 'storm'); + $breadcrumb[] = l(t('Invoices'), 'storm/invoices'); + $breadcrumb[] = l($node->title, 'node/'.$node->nid); + + drupal_set_breadcrumb($breadcrumb); + drupal_set_html_head(''); + + $path = 'node/'. $node->nid; + + return drupal_get_form('storminvoice_send_form', $path, $node, $language); +} + +/** + * Form + */ +function storminvoice_send_form(&$form_state, $path = NULL, $node = NULL, $language) { + global $base_url, $user; + + _storminvoice_report_file_pdf( $node, $language); + $pdffile=variable_get('storminvoice_email_pdf_file_path', ''); + + $organization = node_load($node->organization_nid); + $organization_email = $organization->email; + + $form = array(); + $cid = array(); + + $emailtype = 'email'; + + $form['message']['instructions'] = array( + '#type' => 'item', + '#value' => t(variable_get('storminvoice_email_pdf_instructions', '

From here you can send PDF file of invoice as attachment to your client

NOTE: You can change subject and cover note of email. If Bcc field is checked you will also get copy of email.

')), + ); + $form['message']['yemail'] = array( + '#type' => 'textfield', + '#title' => t('Your Email'), + '#size' => 58, + '#maxlength' => 256, + '#required' => TRUE, + ); + $form['message']['yname'] = array( + '#type' => 'textfield', + '#title' => t('Your Name'), + '#size' => 58, + '#maxlength' => 256, + '#required' => TRUE, + ); + $form['message']['recipients'] = array( + '#type' => 'textfield', + '#title' => t('Send To'), + '#default_value' => $organization_email, + '#cols' => 50, + '#rows' => 1, + '#description' => t('Enter multiple addresses on separate lines or separate them with commas.'), + '#required' => TRUE, + ); + + $form['message']['bcc'] = array( + '#type' => 'checkbox', + '#title' => t('Send me a copy'), + '#default_value' => 1, + '#description' => t('Leave this box checked if you want to receive a copy as Bcc.'), + ); + + + $form['message']['page'] = array( + '#type' => 'item', + '#title' => t('You are going to email the following invoice'), + '#value' => l($node->title, $pdffile), + ); + + $form['message']['subject'] = array( + '#type' => 'textfield', + '#title' => t('Message Subject'), + '#value' => t(variable_get('storminvoice_cover_note_subject', '')), + '#description' => t('Enter subject for email.'), + ); + $form['message']['body'] = array( + '#type' => 'textarea', + '#title' => t('Message Body'), + '#value' => t(variable_get('storminvoice_cover_note', '')), + '#description' => 'Cover Note', + '#cols' => 50, + '#rows' => 10, + '#required' => TRUE, + ); + + $form['message']['path'] = array( + '#type' => 'hidden', + '#value' => $path, + ); + + $form['message']['submit'] = array( + '#type' => 'submit', + '#value' => t('Send Invoice'), + ); + + if ($user->uid != 0) { + $form['message']['yemail']['#default_value'] = $user->mail; + $form['message']['yemail']['#disabled'] = TRUE; + $form['message']['yemail']['#value'] = $user->mail; + $form['message']['yname']['#default_value'] = $user->name; + } + + return $form; +} + +function storminvoice_send_form_submit($form, &$form_state) { + $form_filled = $form_state['clicked_button']['#post']; + + $to = $form_filled['recipients']; + $language = null; + $from = $form_state['values']['yname'].'<'.$form_state['values']['yemail'].'>'; + + $params = array(); + + $params['subject'] = $form_filled['subject']; + $params['bcc'] = $form_state['values']['bcc']; + + $trenner = md5(uniqid(time())); + variable_set('storminvoice_email_pdf_trenner', $trenner); + + $headers['Content-Type'] = "multipart/mixed; boundary=$trenner"; + $params['body'] = "\n--$trenner\n"; + $params['body'] .= "Content-Type: text/plain; charset=UTF-8; format=flowed;"."\n\n"; // sets the mime type + $params['body'] .= $form_filled['body']."\n"; + $params['body'] .= "\n\n"; + $params['body'] .= "\n\n"; + + $pdffile = variable_get('storminvoice_email_pdf_file_path', ''); + $pdffile_name = variable_get('storminvoice_email_pdf_file_name', ''); + $pdffile_mime = file_get_mimetype($pdffile); + $pdffile_size = filesize($pdffile); + + if($pdffile){ + $params['body'] .= "--$trenner"."\n"; + $params['body'] .= "Content-Type:$pdffile_mime; name='$pdffile_name'\n"; + $params['body'] .= "Content-Disposition: attachment; filename='$pdffile_name'\n"; + $params['body'] .= "Content-Transfer-Encoding: base64\n\n"; + + $filedata = fread(fopen($pdffile, "rb"), $pdffile_size); + $params['body'] .= chunk_split(base64_encode($filedata)); + } + $params['body'] .= "--$trenner--"; + + drupal_mail('storminvoice', 'invoice', $to, $language, $params, $from , True); + + drupal_set_message(t(variable_get('storminvoice_sent', 'You have sent invoice to !email.'),array('!email' => $to ))); + + if (!file_delete($pdffile)){ + drupal_set_message(t('File !file not deleated!',array('file' => $pdffile)), 'error' ); + }; +} + +/** + * Implementation of hook_mail(). + * + * Constructs the email notification message when the site is out of date. + * + * @param $key + * Unique key to indicate what message to build, always 'storminvoice_sent'. + * @param $message + * Reference to the message array being built. + * @param $params + * Array of parameters to indicate what text to include in the message body. + * + * @see drupal_mail() + * @see _update_cron_notify() + * @see _update_message_text() + */ +function storminvoice_mail($key, &$message, $params) { + + $trenner = variable_get('storminvoice_email_pdf_trenner', ''); + $message['subject'] = $params['subject']; + $message['body'][] = $params['body']; + $message['headers']['MIME-Version'] = '1.0'; + $message['headers']['Content-Type'] = "multipart/mixed; boundary=$trenner"; + if ($params['bcc']){ + $message['headers']['Bcc'] = $message['headers']['From']; + } +} + + function storminvoice_items_submit($form, &$form_state) { $invoiceitems = array(); foreach($form_state['values'] as $key=>$value) { @@ -445,4 +634,223 @@ } drupal_set_message(t('Invoice items saved')); -} \ No newline at end of file +} + +/** + * generate pdf file for sending almost the same as + * theme_storminvoice_report_pdf and code is almost the same + * it would be nice to merge them into one. + */ +function _storminvoice_report_file_pdf($node, $language) { + $tcpdf_dir = variable_get('storminvoice_tcpdf_location', 'sites/all/libraries/tcpdf'); + + // Performs simple check for existance of tcpdf library . If it doesn't exist, revert to node display with message about tcpdf library. + if(!file_exists($tcpdf_dir .'/tcpdf.php')) { + drupal_set_message(t('The tcpdf library has not been installed. See the Storm module README.txt for more details.')); + drupal_goto('node/'. $node->nid); + } + + require_once($tcpdf_dir .'/config/lang/eng.php'); + require_once($tcpdf_dir .'/tcpdf.php'); + $languages = explode(',', $language); + $language = $languages[0]; + $language1 = ''; + if (array_key_exists(1, $languages)) { + $language1 = $languages[1]; + } + + $status = 'open'; + if ($node->paymentdate) { + $status = 'paid'; + } + else if ($node->duedate < time()) { + $status = 'overdue'; + } + + $countries = stormattribute_attributes_bydomain('Country'); + $countries = $countries['values']; + $currencies = stormattribute_attributes_bydomain('Currency'); + $currencies = $currencies['values']; + $myorg = node_load(variable_get('storm_organization_nid', 0)); + $mycurrency = $currencies[$myorg->currency]; + + $organization = node_load($node->organization_nid); + $project = node_load($node->project_nid); + + $o = ''; + $title = t('Invoice', array(), $language); + $complete_title = $title .' '. $myorg->title .' : '. $organization->title .' - '. $node->number; + + $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true); + $pdf->SetCreator(PDF_CREATOR); + $pdf->SetAuthor("Storm"); + $pdf->SetTitle($complete_title); + $pdf->SetSubject($title); + $pdf->SetKeywords($title, $myorg->title, $organization->title, $node->number); + $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + $margins = $pdf->getMargins(); + $pageWidth = $pdf->getPageWidth() - $margins['left'] - $margins['right']; + $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); + $pdf->AddPage(); + $pdf->setDrawColor(204, 204, 204); + $pdf->setFillColor(220, 220, 220); + + $pdf->SetFont("times", "B", 14); + $headerleft = variable_get('site_name', '') .'
'. variable_get('site_slogan', ''); + $pdf->writeHTMLCell($pageWidth *.5, 0, $pdf->getX(), $pdf->getY(), $headerleft, 0 ,0 ,0, false, 'L'); + $pdf->SetFont("times", "N", 10); + $pdf->writeHTMLCell($pageWidth *.5, 0, $pdf->getX(), $pdf->getY(), variable_get('storm_report_header', ''), 0 ,1 ,0, false, 'R'); + $pdf->SetFont("times", "B", 14); + $o = $title; + if ($language1) $o .= "\n". t('Invoice', array(), $language1); + $pdf->MultiCell(0, 0, $o, 0 ,'C', 0, 1,$pdf->getX(), $pdf->getY() + 10); + + $y = $pdf->getY() + 10; + + $pdf->SetFont("times", "B", 10); + $o = t('Bill to', array(), $language); + if ($language1) $o .= "\n". t('Bill to', array(), $language1); + $pdf->MultiCell($pageWidth *.4, 0, $o, 'B', 'L', 0, 1, PDF_MARGIN_LEFT, $y); + $o = $organization->fullname ."\n"; + $o .= $organization->address ."\n"; + $o .= $organization->city ."\n"; + if ($organization->zip) $o .= $organization->zip .' '; + if ($organization->provstate) $o .= $organization->provstate .' '; + $o .= $countries[$organization->country] ."\n";; + if ($organization->taxid) { + $o .= t('Tax ID', array(), $language); + if ($language1) $o .= ' / '. t('Tax ID', array(), $language1); + $o .= ' : '. $organization->taxid; + } + $pdf->SetFont("times", "N", 10); + $pdf->MultiCell($pageWidth *.4, 0, $o,0 , 'L' ,0, 1, PDF_MARGIN_LEFT); + $destY = $pdf->getY(); + + $w = ($pageWidth *.5) / 4; + + $pdf->SetFont("times", "B", 10); + + $o = t('Invoice#', array(), $language); + if ($language1) $o .= "\n". t('Invoice#', array(), $language1); + $pdf->MultiCell($w-2, 0, $o, 1, 'L', 1, 0, PDF_MARGIN_LEFT + $pageWidth *.5, $y); + + $o = t('Currency', array(), $language); + if ($language1) $o .= "\n". t('Currency', array(), $language1); + $pdf->MultiCell($w-3, 0, $o, 1, 'L', 1, 0); + $o = t('Date', array(), $language); + if ($language1) $o .= "\n". t('Date', array(), $language1); + $pdf->MultiCell($w+5, 0, $o, 1, 'L', 1, 0); + $o = t('Reference', array(), $language); + if ($language1) $o .= "\n". t('Reference', array(), $language1); + $pdf->MultiCell($w, 0, $o, 1, 'L', 1, 1); + + $pdf->SetFont("times", "N", 10); + $h = $pdf->getY(); + $pdf->MultiCell($w, 0, $node->reference?$node->reference:'-' , 1, 'L', 0, 1, PDF_MARGIN_LEFT + $pageWidth*.5 + $w*3); + $h = $pdf->getY() - $h; + $pdf->MultiCell($w-2, $h, $node->number, 1, 'L', 0, 0, PDF_MARGIN_LEFT + $pageWidth *.5, $pdf->getY() - $h); + $pdf->MultiCell($w-3, $h, t($mycurrency, array(), $language), 1, 'L', 0, 0); + $pdf->MultiCell($w+5, $h, format_date($node->requestdate, 'custom', 'Y-m-d'), 1, 'L', 0, 1); + + $pdf->SetFont("times", "B", 10); + + $o = t('Due total', array(), $language); + if ($language1) $o .= "\n". t('Due total', array(), $language1); + $pdf->MultiCell($w*2 - 5, 0, $o, 1, 'L', 1, 0, PDF_MARGIN_LEFT + $pageWidth *.5); + + $o = t('Due date', array(), $language); + if ($language1) $o .= "\n". t('Due date', array(), $language1); + $pdf->MultiCell($w + 5, 0, $o, 1, 'L', 1, 0); + + $o = t('Terms', array(), $language); + if ($language1) $o .= "\n". t('Terms', array(), $language1); + $pdf->MultiCell($w, 0, $o, 1, 'L', 1, 1); + + $pdf->SetFont("times", "B", 10); + $o = t($mycurrency, array(), $language) .' '. sprintf('%.2f', $node->total); + if ($organization->currency != $myorg->currency) { + $o .= "\n". $organization->currency .' '. sprintf('%.2f', $node->totalcustomercurr); + } + $pdf->MultiCell($w*2 - 5, 12, $o, 1, 'C', 0, 0, PDF_MARGIN_LEFT + $pageWidth *.5); + $pdf->SetFont("times", "N", 10); + $pdf->MultiCell($w + 5, 12, format_date($node->duedate, 'custom', 'Y-m-d'), 1, 'L', 0, 0); + $pdf->MultiCell($w, 12, t(variable_get('storminvoice_payment_terms', ''), array(), $language), 1, 'L', 0, 1); + + $y = $pdf->getY(); + if($destY > $y) $y = $destY; + $pdf->setY($y+10); + + $pdf->setX(PDF_MARGIN_LEFT); + $pdf->SetFont("times", "B", 10); + + $o = t('Description', array(), $language); + if ($language1) $o .= "\n". t('Description', array(), $language1); + $pdf->MultiCell($pageWidth * .4, 0, $o, 1, 'L', 1, 0); + + $o = t('Amount', array(), $language); + if ($language1) $o .= "\n". t('Amount', array(), $language1); + $pdf->MultiCell($pageWidth * .15, 0, $o, 1, 'C', 1, 0); + + $o = t('Tax %', array(), $language); + if ($language1) $o .= "\n". t('Tax %', array(), $language1); + $pdf->MultiCell($pageWidth * .15, 0, $o, 1, 'C', 1, 0); + + $o = t('Tax', array(), $language); + if ($language1) $o .= "\n". t('Tax', array(), $language1); + $pdf->MultiCell($pageWidth * .15, 0, $o, 1, 'C', 1, 0); + + $o = t('Total', array(), $language); + if ($language1) $o .= "\n". t('Total', array(), $language1); + $pdf->MultiCell($pageWidth * .15, 0, $o, 1, 'C', 1, 1); + + $pdf->SetFont("times", "N", 10); + $items = storminvoiceitem_getitems($node->nid); + $rows = array(); + $pdf->setFillColor(245, 245, 245); + $c = 0; + foreach ($items as $i) { + if ($c==2) $c=0; + $y = $pdf->getY(); + $h = $pdf->getY(); + $pdf->MultiCell($pageWidth * .4, 0, $i->title, 1, 'L', $c, 1); + $h = $pdf->getY() - $h; + $pdf->setY($y); + $pdf->setX(PDF_MARGIN_LEFT + $pageWidth * .4); + $pdf->Cell($pageWidth * .15, $h, sprintf('%.2f', $i->amount), 1, 0, 'R', $c); + $pdf->Cell($pageWidth * .15, $h, $i->taxpercent, 1, 0, 'R', $c); + $pdf->Cell($pageWidth * .15, $h, sprintf('%.2f', $i->tax), 1, 0, 'R', $c); + $pdf->Cell($pageWidth * .15, $h, sprintf('%.2f', $i->total), 1, 1, 'R', $c); + $c++; + } + + if ($node->taxexempt && $language1=='it') { + $o = t('Tax exempt art. 7', array(), $language); + if ($language1) $o .= "\n". t('Tax exempt art. 7', array(), $language1); + $pdf->MultiCell($pageWidth, 0, $o,0 , 'L' ,0, 1, $pdf->getX(), $pdf->getY() + 5); + } + + $y = $pdf->getY() + 10; + $pdf->setY($y); + $pdf->SetFont("times", "B", 10); + $pdf->Cell($pageWidth, 5, t('Payment', array(), $language), 'B', 0, 'L'); + $pdf->SetFont("times", "N", 10); + $pdf->MultiCell($pageWidth, 0, t(variable_get('storminvoice_payment_modes', ''), array(), $language),0 , 'L' ,0, 1, PDF_MARGIN_LEFT, $pdf->getY(), true, 0, true); + + if ($status=='paid') { + $y = $pdf->getY() + 10; + $pdf->setY($y); + $pdf->SetFont("times", "B", 14); + $pdf->Cell(0, 12, t('Paid in full', array(), $language), 0 ,1,'C'); + } + + $filename = strtolower($title . '_' . $myorg->title . '_' . $organization->title . '_' . str_replace('/', '-', $node->number)) .'.pdf'; + + $dir = file_directory_path().'/'; + + $pdf->Output($dir.$filename, "F"); + variable_set('storminvoice_email_pdf_file_path', $dir.$filename); + variable_set('storminvoice_email_pdf_file_name', $filename); + +} diff -Naur storminvoice/storminvoice.module storminvoiceNew/storminvoice.module --- storminvoice/storminvoice.module 2009-06-28 23:12:35.000000000 +0200 +++ storminvoiceNew/storminvoice.module 2009-07-02 01:55:16.000000000 +0200 @@ -190,6 +190,16 @@ 'file' => 'storminvoice.admin.inc', ); + $items['storm/invoice/report/%node/email/%'] = array( + 'title' => t('Send Invoice by Email'), + 'page arguments' => array(3, 5), + 'description' => t('Storm Invoice'), + 'page callback' => 'storminvoice_send_page', + 'access arguments' => array('Storm invoice: access'), + 'type' => MENU_CALLBACK, + 'file' => 'storminvoice.admin.inc', + ); + $items['admin/storm/invoice'] = array( 'title' => t('Storm Invoice'), 'description' => t('Storm Invoice Administration Page'), @@ -560,6 +570,22 @@ '#default_value' => variable_get('storminvoice_payment_modes', ''), '#description' => t('Modes for invoice payment'), ); + + $form['storminvoice_cover_note_subject'] = array( + '#type' => 'textfield', + '#title' => t('Cover note subject'), + '#default_value' => t(variable_get('storminvoice_cover_note_subject', 'Invoice from !site'), array('!site' => variable_get('site_name', 'STORM'))), + '#description' => t('Default subject for cover note'), + '#size' => 50, + ); + + $form['storminvoice_cover_note'] = array( + '#type' => 'textarea', + '#title' => t('Cover note for invoice'), + '#default_value' => variable_get('storminvoice_cover_note', 'Hope this comes easy to you.'), + '#description' => t('Default cover note for pdf invoice sent by email'), + ); + $form['storminvoice_payment_terms'] = array( '#type' => 'textfield', diff -Naur storminvoice/storminvoice.theme.inc storminvoiceNew/storminvoice.theme.inc --- storminvoice/storminvoice.theme.inc 2009-06-08 18:16:25.000000000 +0200 +++ storminvoiceNew/storminvoice.theme.inc 2009-07-02 01:56:47.000000000 +0200 @@ -190,8 +190,22 @@ '#value' => l(t('Print PDF'), 'storm/invoice/report/'. $node->nid .'/pdf/'. $node->language), '#weight' => $w++, ); + $node->content['storminvoice']['links']['print']['email'] = array( + '#value' => t('Send PDF via e-mail'), + '#prefix' => '', + '#value' => l(t('Send PDF via e-mail'), 'storm/invoice/report/'. $node->nid .'/email/'. $node->language, array('query' => drupal_get_destination())), + '#weight' => $w++, + ); } + $form['submit'] = array( + '#type' => 'submit', + '#submit' => array('storminvoice_items_submit'), + '#value' => t('Save item ordering'), + ); + + $node->content['storminvoice']['fields'] = array( '#prefix' => '
', '#suffix' => '
',