--- Desktop/worklog/worklog.module	2006-07-16 15:37:26.000000000 -0500
+++ /var/www/localhost/htdocs/drupal/modules/worklog/worklog (copy).module	2006-08-30 08:23:03.000000000 -0500
@@ -6,7 +6,7 @@
  */
 
 define('WORKLOG_ROUND_MINUTES', 15); // 6 minutes = .1 hour
-
+define('WORKLOG_PROFILE_CATEGORY', 'Contact Info'); // profile created for worklog
 // hook_worklog operations
 define('WORKLOG_OP_INVOICE_HEADER', 'worklog_invoice_header');
 define('WORKLOG_OP_INVOICE_FOOTER', 'worklog_invoice_footer');
@@ -726,7 +726,7 @@
 	$node->worklog['elapsed'] = _worklog_parse_duration($node->worklog['_elapsed']);
 	$node->worklog['started'] = strtotime($node->worklog['_started']);
   }
-
+  
   // drupal_set_message(theme('debug', $node));
   $node = node_prepare($node, $teaser);
   $info = theme('worklog_info', $node);
@@ -736,7 +736,6 @@
 
 function _worklog_invoice_view(&$node, $teaser, $page) {
   $node = node_prepare($node, $teaser);
-  
   if (($vid = variable_get('worklog_invoice_view', 'worklog_invoice')) &&
       function_exists('views_get_view')) {
     // use a view to render invoice items
@@ -749,7 +748,14 @@
   $info = theme('worklog_invoice_info', $node);
   $node->body .= $info;
   $node->teaser .= $info;
-
+  if(variable_get('worklog_show_address',0) > 0 && arg(2) == 'print') {
+    $node->body = '<div class="address">'.worklog_taxonomy_to_contacts($node,'client', 1).worklog_taxonomy_to_contacts($node,'consultant',1).'</div>'.$node->body;
+  }
+  // Show invoice number
+  //  TODO: whether there is a shared number (by node id) or individual consultants (with something?)
+  if(variable_get('worklog_show_invoice',0) > 0) {
+    $node->body = '<div class="worklog_invoice_num">'.format_date($node->created, 'custom', 'm/d/Y').'<br/>Invoice#:'.$node->nid.'</div>'.$node->body;
+  }
   // allow third party modules to supply additional header and footer
   $extra_header = module_invoke_all('worklog', WORKLOG_OP_INVOICE_HEADER,
                                     $node, $teaser, $page);
@@ -799,6 +805,11 @@
 							array('name' => t('Worklog: hourly rate'),
 								  'sortable' => true,
 								  ),
+							'description' =>
+							array('name' => t('Worklog: description'),
+							    'notafield' => true,
+								  'handler' => 'worklog_views_handler_field_description',
+								  ),
 							'bill' =>
 							array('notafield' => true,
 								  'name' => t('Worklog: billable'),
@@ -881,7 +892,10 @@
 
   return theme('worklog_price', $billable);
 }
-
+function worklog_views_handler_field_description($fieldinfo, $fielddata, $value, $data) {
+  $node = node_load(array(nid=>$data->nid));
+  return t($node->body);
+}
 
 /**
  * Display seconds as hours
@@ -896,7 +910,6 @@
 }
 
 
-
 function worklog_settings() {
   // Begin price format section
   $form['price_format'] = array(
@@ -941,9 +954,61 @@
     '#size' => 3, '#maxlength' => 5, 
     '#desciption' => t('How many slots are needed after the decimal?')
    );
-
-
-
+   $form['invoice_show'] = array(
+    '#type' => 'fieldset', 
+    '#title' => t('Invoice #'), 
+    '#collapsible' => TRUE, 
+    '#collapsed' => FALSE, 
+  );
+  $options = array(0=> 'No', 1 => 'Yes');
+  $form['invoice_show']['worklog_show_invoice'] = array(
+    '#type' => 'select',
+    '#default_value' => variable_get('worklog_show_invoice',0), 
+    '#options' => $options,
+  );
+   if(module_exist('taxonomy') && module_exist('user') && module_exist('profile') && module_exist('print')) {
+     $form['taxonomy'] = array(
+      '#type' => 'fieldset', 
+      '#title' => t('Taxonomy to use'), 
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+      '#description' => t('This information is used to get address/contact information when printing an invoice.  Not choosing a consultant taxonomy defaults the consultant to invoice node creation user')
+    );
+    $options = array(0=> 'No', 1 => 'Yes');
+    $form['taxonomy']['worklog_show_address'] = array(
+      '#type' => 'select',
+      '#title' => t('Show address information'),
+      '#default_value' => variable_get('worklog_show_address',0), 
+      '#options' => $options,
+    );
+    $options = array(0=>'Choose One');
+    $vocabularies = taxonomy_get_vocabularies('worklog-invoice');
+    foreach ($vocabularies as $vocabulary) {
+      $options[$vocabulary->vid] = $vocabulary->name;
+    }
+    $form['taxonomy']['worklog_consultant'] = array(
+      '#type' => 'select',
+      '#title' => t('Consultant Taxonomy'),
+      '#default_value' => variable_get('worklog_consultant',0), 
+      '#options' => $options,
+      '#desciption' => t('Choose a consultant taxonomy or user who creates invoice is default')
+    );
+    $form['taxonomy']['worklog_client'] = array(
+      '#type' => 'select',
+      '#title' => t('Client Taxonomy'),
+      '#default_value' => variable_get('worklog_client',0),
+      '#options' => $options,
+      '#desciption' => t('Choose which taxonomy to check for clients.')
+    );
+    $options = array(0=> 'No', 1 => 'Yes');
+    $form['taxonomy']['worklog_email'] = array(
+      '#type' => 'select',
+      '#title' => t('Email'),
+      '#default_value' => variable_get('worklog_email',0), 
+      '#options' => $options,
+      '#desciption' => t('Inculde email in contact information?')
+    );
+  }
   return $form;
 }
 
@@ -1027,6 +1092,11 @@
       'field' => 'bill',
       'label' => 'amount',
     ),
+    array (
+      'tablename' => 'worklog',
+      'field' => 'description',
+      'label' => 'description',
+    ),
   );
   $view->filter = array (
   );
@@ -1037,4 +1107,288 @@
 
   return $views;
 }
-?>
\ No newline at end of file
+
+/**
+ * Implementation of a Drupal action.
+ * This is an email action specific to worklogs
+ *
+ */
+function action_send_worklog_email($op, $edit = array(), $node) {
+  switch($op) {
+    case 'metadata':
+      return array(
+        'description' => t('Send Worklog Invoice Email'),
+        'type' => t('Worklog'),
+        'batchable' => false,
+        'configurable' => true,
+      );
+
+    case 'do':
+      $contacts = worklog_taxonomy_to_contacts($node,'both');
+      $client = $contacts->client;
+      $consultant = $contacts->consultant;
+      $from = $consultant->name." <";
+      if(variable_get('worklog_email',0) > 0) {
+        $from .= $consultant->mail.">";
+      } else {
+        $from .= variable_get('site_mail', ini_get('sendmail_from')).">";
+      }
+      $subject = $edit['subject'];
+      $message = $edit['message'];
+      if ($edit['recipient'] == t('%consultant')) {
+        $recipient = $consultant->name.' <'.$consultant->mail.'>';
+      } else if($edit['recipient'] == t('%client')) {
+        $recipient = $client->name.' <'.$client->mail.'>';
+      } else {
+        $recipient = $edit['recipient'];
+      }
+      if (isset($node) && is_object($node)) {
+        $variables = array(
+          '%consultant_name' => $consultant->name,
+          '%node_url' => url('node/' . $node->nid, NULL, NULL, TRUE),
+          '%node_type' => $node->type,
+          '%title' => $node->title,
+          '%teaser' => strip_tags($node->teaser),
+          '%body' => strip_tags($node->body),
+          '%invoice_id' => $node->nid,
+          '%date' => format_date($node->created, 'custom', 'm/d/Y'),
+          '%client_name' => $client->name,
+          '%client_company' => $client->company
+           );
+
+        $subject = strtr($subject, $variables);
+        $subject = str_replace(array("\r", "\n"), '', $subject);
+        $message = strtr($message, $variables);
+      }
+      if (user_mail($recipient, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from" )) {
+        watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient)));
+      }
+      else {
+        watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient)));
+      }
+      break;
+
+    // return an HTML config form for the action
+    case 'form':
+      // default values for form
+      if (!isset($edit['recipient'])) $edit['recipient'] = '%client';
+      if (!isset($edit['subject'])) $edit['subject'] = 'INVOICE# %invoice_id';
+      if (!isset($edit['message'])) $edit['message'] = 
+'%client_name,
+This is a notification that your invoice %title for work completed by %conulstant_name is ready for viewing.  NOTE: Login is required for viewing.  Your username is %client_name.
+%node_url
+Or for easy printing
+%node_url/print
+Thank you for using our services and we hope to assist you with any of your future needs.
+  Sincerely,
+  %consultant_name';
+      $form = array();
+
+      // add form components
+      $form['recipient'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Recipient'),
+        '#default_value' => $edit['recipient'],
+        '#size' => '20',
+        '#maxlength' => '254',
+        '#description' => t('The email address to which the message should be sent OR enter %consultant or %client if you would like to send an e-mail to the consultant or the client of the post.'),
+      );
+      $form['subject'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Subject'),
+        '#default_value' => $edit['subject'],
+        '#size' => '20',
+        '#maxlength' => '254',
+        '#description' => t('The subject of the message.  You may include the following variables: %consultant_name, %client_name, %client_company, %node_url, %node_type, %title, %invoice_id, %date'),
+      );
+      $form['message'] = array(
+        '#type' => 'textarea',
+        '#title' => t('Message'),
+        '#default_value' => $edit['message'],
+        '#cols' => '80',
+        '#rows' => '20',
+        '#description' => t('The message that should be sent.  You may include the following variables: %consultant_name, %client_name, %client_company, %node_url, %node_type, %title, %invoice_id, %date, %teaser, %body'),
+      );
+      return $form;
+
+     // validate the HTML form
+    case 'validate':
+      $errors = array();
+
+      if (!valid_email_address($edit['recipient']) && $edit['recipient'] != t('%consultant') && $edit['recipient'] != t('%client') ) {
+        $errors['recipient'] = t('Please enter a valid email address or %consultant/%client.');
+      }
+      foreach ($errors as $name => $message) {
+        form_set_error($name, $message);
+      }
+
+      return count($errors) == 0;
+
+    // process the HTML form to store configuration
+    case 'submit':
+      $params = array(
+        'recipient' => $edit['recipient'],
+        'subject'   => $edit['subject'],
+        'message'   => $edit['message']);
+      return $params;
+  }
+}
+
+/**
+ * Convert taxonomy term to consultant object,client object or both in an array of objects.  Requires taxonomy, users, and profile modules to be
+ * activated and setup
+ * TODO: multiple consultants/clients
+ * $type ability to specify either client, consultant, or both (default)
+ * $print ability to show formating (1) or just return data array (default).
+ * Return array of contact information.
+ */
+function worklog_taxonomy_to_contacts($node,$type='both',$print=0) {
+  //taxonomy term must be
+  if(variable_get('worklog_client',0) > 0) {
+    if($print > 0) {
+      $output = '';
+    } else {
+      $data = array('consultant' => array(), 'client' => array());
+    }
+    if($type == 'both' || $type == 'consultant') {
+      if(variable_get('worklog_consultant',0) != 0) {
+        $terms = taxonomy_node_get_terms_by_vocabulary($node->nid,variable_get('worklog_consultant'));
+        if(count($terms) == 1) {
+          foreach($terms as $term) {
+            $consultant = user_load(array('name' => $term->name));
+          }
+        }
+      } else {
+        $consultant = user_load(array('uid' => $node->uid));
+      }
+      if($print > 0) {
+        $output .= "<div class=\"consultant\" >\n";
+        $output .= "<div id=\"title\">Please remit payment to:</div>";
+        $output .= "<div id=\"contact_name\">".t($consultant->name)."</div>\n";
+      } else {
+        $data['consultant']['name'] = $consultant->name;
+      }
+      if(isset($consultant->profile_company)) {
+        if($print > 0) {
+          $output .= "<div id=\"contact_company\">".t($consultant->profile_company)."</div>\n";
+        } else {
+          $data['consultant']['profile_company'] = $consultant->profile_company;
+        }
+      }
+      if($print > 0) {
+        $output .= "<div id=\"contact_address\">".t($consultant->profile_address1)."</div>\n";
+      } else {
+        $data['consultant']['profile_address1'] = $consultant->profile_address1;
+      }
+      if(isset($consultant->profile_address2)) {
+        if($print > 0) {
+          $output .= "<div id=\"contact_address\">".t($consultant->profile_address2)."</div>\n";
+        } else {
+          $data['consultant']['profile_address2'] = $consultant->profile_address2;
+        }
+      }
+      if($print > 0) {
+        $output .= "<div id=\"contact_address\">".t($consultant->profile_city.', '.$consultant->profile_state.' '.$consultant->profile_zipcode)."</div>\n";
+        $output .= "<div id=\"contact_address\">".t($consultant->profile_country.' '.(isset($consultant->profile_countrycode) ? $consultant->profile_countrycode : ''))."</div>\n";
+        $output .= "<div id=\"contact_phone\">".t('Tel: '.$consultant->profile_phone)."</div>\n";
+      } else {
+        $data['consultant']['profile_city'] = $consultant->profile_city;
+        $data['consultant']['profile_state'] = $consultant->profile_state;
+        $data['consultant']['profile_zipcode'] = $consultant->profile_zipcode;
+        $data['consultant']['profile_city'] = $consultant->profile_country;
+        if(isset($consultant->profile_countrycode)) $data['consultant']['profile_countrycode'] = $consultant->profile_countrycode;
+        $data['consultant']['profile_phone'] = $consultant->profile_phone;
+      }
+      
+      if($consultant->profile_phone1) {
+        if($print > 0) {
+          $output .= "<div id=\"contact_phone\">".t('ALT Tel: '.$consultant->profile_phone1)."</div>\n";
+        } else {
+          $data['consultant']['profile_phone1'] = $consultant->profile_phone1;
+        }
+      }
+      if(variable_get('worklog_email',0) > 0) {
+        if($print > 0) {
+          $output .= "<div id=\"contact_email\">".t('Email: '.$consultant->mail)."</div>\n";
+        } else {
+          $data['consultant']['mail'] = $consultant->mail;
+        }
+      }
+      if($print > 0) {
+        $output .= "</div>\n";
+      }
+    } 
+    if($type == 'both' || $type == 'client') {
+      $terms = taxonomy_node_get_terms_by_vocabulary($node->nid,variable_get('worklog_client'));
+      foreach($terms as $term) {
+        $client = user_load(array('name' => $term->name));
+        if($print > 0) {
+          $output .= "<div class=\"client\" >\n";
+          $output .= "<div id=\"title\">Billed To:</div>";
+          $output .= "<div id=\"contact_name\">".t($client->name)."</div>\n";
+        } else {
+          $data['client']['name'] = $client->name;
+        }
+        
+        if(isset($client->profile_company)) {
+          if($print > 0) {
+            $output .= "<div id=\"contact_company\">".t($client->profile_company)."</div>\n";
+          } else {
+            $data['client']['profile_company'] = $client->profile_company;
+          }
+        }
+        if($print > 0) {
+          $output .= "<div id=\"contact_address\">".t($client->profile_address1)."</div>\n";
+        } else {
+          $data['client']['profile_address1'] = $client->profile_address1;
+        }
+        
+        if(isset($client->profile_address2)) {
+          if($print > 0) {
+            $output .= "<div id=\"contact_address\">".t($client->profile_address2)."</div>\n";
+          } else {
+            $data['client']['profile_address2'] = $client->profile_address2;
+          }
+        }
+        if($print > 0) {
+          $output .= "<div id=\"contact_address\">".t($client->profile_city.', '.$client->profile_state.' '.$client->profile_zipcode)."</div>\n";
+          $output .= "<div id=\"contact_address\">".t($client->profile_country.' '.(isset($client->profile_countrycode) ? $client->profile_countrycode : ''))."</div>\n";
+          $output .= "<div id=\"contact_phone\">".t('Tel: '.$client->profile_phone)."</div>\n";
+        } else {
+          $data['client']['profile_city'] = $client->profile_city;
+          $data['client']['profile_state'] = $client->profile_state;
+          $data['client']['profile_zipcode'] = $client->profile_zipcode;
+          $data['client']['profile_city'] = $client->profile_country;
+          if(isset($client->profile_countrycode)) $data['client']['profile_countrycode'] = $client->profile_countrycode;
+          $data['client']['profile_phone'] = $client->profile_phone;
+        }
+        
+        if(isset($client->profile_phone1)) {
+          if($print > 0) {
+            $output .= "<div id=\"contact_phone\">".t('ALT TEL: '.$client->profile_phone1)."</div>\n";
+          } else {
+            $data['client']['profile_phone1'] = $client->profile_phone1;
+          }
+        }
+        if(variable_get('worklog_email',0) > 0) {
+          if($print > 0) {
+            $output .= "<div id=\"contact_email\">".t('Email: '.$client->mail)."</div>\n";
+          }
+        } else {
+          if($print == 0) {
+            $data['client']['mail'] = $client->mail;
+          }
+        }
+        if($print > 0) {
+          $output .= "</div>\n";
+        }
+      }
+    }
+    if($print > 0) {
+      return $output;
+    } else {
+      return $data;
+    }
+  } 
+}
+?>
