--- /home/klavs/drupal6/invoice/invoice.module	2010-02-17 20:55:45.000000000 +0100
+++ invoice.module	2010-03-17 15:37:31.000000000 +0100
@@ -373,14 +373,15 @@
         }
         
         // Get customer number
-        if (!empty($node->company_name)) {
-          $customer_number = db_result(db_query("SELECT customer_number FROM {invoice_customers}
-            WHERE company_name='%s' AND country='%s' LIMIT 1",
-            $node->company_name,
-            $node->country
-          ));
-        }
-        elseif (!empty($node->lastname)) {
+//        if (!empty($node->company_name)) {
+//          $customer_number = db_result(db_query("SELECT customer_number FROM {invoice_customers}
+//            WHERE company_name='%s' AND country='%s' LIMIT 1",
+//            $node->company_name,
+//            $node->country
+//          ));
+//        }
+//        elseif (!empty($node->lastname)) {
+        	if (!empty($node->lastname)) {
           $customer_number = db_result(db_query("SELECT customer_number FROM {invoice_customers}
             WHERE lastname='%s' AND zipcode='%s' AND building_number='%s' LIMIT 1",
             $node->lastname,
@@ -622,6 +623,30 @@
   }
 }
 
+function invoice_save(&$node) {
+	if ($node->order_id != '') { //if you set order_id - you want it to be unique!
+		$iid = _invoice_for_order_exists($node->order_id);
+		if ($iid) {
+			drupal_set_message("Invoice nr. $iid for order ".$node->order_id." already exists - no duplicates allowed!");
+			return;
+		}
+	}
+			
+	//if $node contains $invoice_items - we have been called from another module - and thus set
+	//necessary invoice nr. and invoice_items our selves.
+	if ($node->paystatus != "") {
+		
+  	// Insert invoice item into the invoice items table
+    foreach ($node->invoice_items as $k => $v) {
+    	db_query("INSERT INTO {invoice_items}
+      (description,vat,quantity,unitcost,invoice_id,uid,created) VALUES ('%s',%f,%f,%f,%d,%d,'%s')",
+      $v['description'], $v['vat'], $v['quantity'], $v['unitcost'], 0, $node->uid, date('Y-m-d H:i:s'));
+ 		}
+		node_save($node);
+		invoice_set_pay_status($node->invoice_number,$node->paystatus);
+	}
+}
+
 /**
  * Implementation of hook_insert()
  */
@@ -631,7 +656,9 @@
   $tid = db_result(db_query("SELECT tid FROM {invoice_templates} WHERE name='%s'", $node->template));
   
   // Create invoice
-  db_query("INSERT INTO {invoice_invoices} (iid,nid,leading_zeros,prefix,description,tid,pay_limit,uid) VALUES (%d,%d,%d,'%s','%s',%d,'%s',%d)",
+	if ($node->order_id == '')
+		$node->order_id = 0; 
+  db_query("INSERT INTO {invoice_invoices} (iid,nid,leading_zeros,prefix,description,tid,pay_limit,uid,order_id) VALUES (%d,%d,%d,'%s','%s',%d,'%s',%d,%d)",
     $node->invoice_number,
     $node->nid,
     empty($node->invoice_invoice_number_zerofill) ? variable_get('invoice_invoice_number_zerofill', 0) : $node->invoice_invoice_number_zerofill,
@@ -639,7 +666,8 @@
     $node->invoice_description,
     $tid,
     $node->pay_limit,
-    $GLOBALS['user']->uid
+    $node->uid,
+		$node->order_id
   );
       
   // Create customer
@@ -661,10 +689,7 @@
   );
       
   // Add all temporary invoice items to this invoice
-  db_query("UPDATE {invoice_items} SET invoice_id=%d WHERE uid=%d AND invoice_id=0",
-    $node->invoice_number,
-    $GLOBALS['user']->uid
-  );
+ 	db_query("UPDATE {invoice_items} SET invoice_id=%d WHERE uid=%d AND invoice_id=0", $node->invoice_number, $node->uid);
   
   db_query("UPDATE {node} SET promote=0 WHERE type='invoice' AND nid=%d", $node->nid);
   
@@ -827,6 +852,13 @@
 }
 
 /**
+ * Check if invoice for given order_id already exists
+ */
+function _invoice_for_order_exists($order_id) {
+  return db_result(db_query("SELECT iid FROM {invoice_invoices} WHERE order_id=%d", $order_id));
+}
+
+/**
  * Theme function for displaying the invoice
  */
 function theme_invoice_body($node, $type=NULL) {
