--- stormtimetracking.module	2010-03-13 19:12:22.340714000 +0100
+++ stormtimetracking_fix735694.module	2010-03-13 19:12:46.419917700 +0100
@@ -666,9 +666,10 @@ function stormtimetracking_storminvoice_
       //Code copied with edits from node form
       $new_invoice->requestdate = time();
       $new_invoice->duedate = $new_invoice->requestdate + (variable_get('storminvoice_payment_days', 30) * 86400);
-      $s = "SELECT MAX(CAST(SUBSTRING_INDEX(sin.number, 'ACM', -1) AS SIGNED)) FROM {storminvoice} sin";
+      $s = "SELECT MAX(CAST(SUBSTRING_INDEX(sin.number, '/', 1) AS SIGNED)) FROM {node} n INNER JOIN {storminvoice} sin ON n.nid=sin.nid
+        WHERE n.type='storminvoice' AND YEAR(FROM_UNIXTIME(sin.requestdate))=YEAR(FROM_UNIXTIME(%d))";
       $date = getdate($new_invoice->requestdate);
-      $new_invoice->number = 'ACM' . (db_result(db_query($s)) + 1);
+      $new_invoice->number = (db_result(db_query($s, $new_invoice->requestdate)) + 1) .'/'. $date['year'];
 
       $new_invoice->title = $node->title;
       $new_invoice->uid = $user->uid;
@@ -691,13 +692,28 @@ function stormtimetracking_storminvoice_
     } else {
       $new_invoice = node_load($invoice_nid);
     }
+
+    global $use_fixed, $use_hourly, $fixed_price, $hourly_rate;
+    
+    $use_fixed = FALSE;
+    $use_hourly = FALSE;
+    $fixed_price = 0;
+    $hourly_rate = 0;
     
-    $hourly_rate = stormtimetracking_timetracking_to_hourly_rate($node);
+    stormtimetracking_timetracking_get_rate($node);
     
     $count = count($new_invoice->items);
     
     $new_invoice->items[$count]->description  = date('d M y', $node->trackingdate) . ': ';
     $new_invoice->items[$count]->description .= ($hourly_rate ? t('@dur hours work at @rate per hour on @desc', array('@dur' => $node->billing_duration, '@rate' => $hourly_rate, '@desc' => $node->title)) : t('@dur hours unbilled work on @desc', array('@dur' => $node->billing_duration, '@desc' => $node->title)));
+
+    if ($use_hourly == TRUE) {
+    	$new_invoice->items[$count]->amount = $node->billing_duration * $hourly_rate;
+    	}
+
+    if ($use_fixed == TRUE) {
+    	$new_invoice->items[$count]->amount = $fixed_price; 
+    	}
     
     $new_invoice->items[$count]->amount = $node->billing_duration * $hourly_rate;
     // Tax percent simply uses default at the moment
@@ -724,41 +740,53 @@ function stormtimetracking_views_api() {
 }
 
 /**
- * Try to obtain an hourly rate from a timetracking node. To do this we will 
+ * Try to obtain an hourly rate or fixed price for the new invoice item for a timetracking node. To do this we will 
  * use the first technique, among the following, to work:
  * 
- *  (1) try to obtain an hourly rate from a ticket
- *  (2) try to obtain an hourly rate from a task
- *  (3) try to obtain an hourly rate from a project
- *  (4) try to obtain an hourly rate from an organization
+ *  (1) try to obtain a rate from a ticket
+ *  (2) try to obtain a rate from a task
+ *  (3) try to obtain a rate from a project
+ *  (4) try to obtain a rate from an organization
  *  (5) return an error if none of this worked.
  *
  * @return
- *   An hourly rate (can be zero).
+ *   Variables are set to global.
  */
-function stormtimetracking_timetracking_to_hourly_rate($node) {
+function stormtimetracking_timetracking_get_rate($node) {
+	global $use_fixed, $use_hourly, $fixed_price, $hourly_rate;
+
   $hours_per_day = 8;
+  $found = FALSE;
   
   foreach (array('ticket' => $node->ticket_nid, 'task' => $node->task_nid, 'project' => $node->project_nid, 'organization' => $node->organization_nid) as $type => $nid) {
     if($nid) {
       $parent_item = node_load($nid);
       switch($parent_item->pricemode) {
         case 'hourly':
+          $use_hourly = TRUE;
+          $found = TRUE;
           $hourly_rate = $parent_item->price;
           break;
         case 'daily':
+          $use_hourly = TRUE;
+          $found = TRUE;
           $hourly_rate = $parent_item->price / $hours_per_day;
           break;
         case 'fixed':
+          $use_fixed = TRUE;
+        	$found = TRUE;
+          $fixed_price = $parent_item->price;
           $hourly_rate = 0;
           break;
         default:
           continue;
       }
     }
+  if ($found == TRUE) {
+  break;
+  }
   }
-  if(!isset($hourly_rate)) {
-    drupal_set_message(t('Error whilst finding hourly rate from ticket, task, project and organization. Consider setting the pricemode and price for your client organizations to avoid this error.'), 'error');
+  if($found == FALSE) {
+    drupal_set_message(t('Error whilst finding a rate from ticket, task, project and organization. Consider setting the pricemode and price for your client organizations to avoid this error.'), 'error');
   } 
-  return $hourly_rate;
 }
