Index: /lib/wpsg_db.class.php
===================================================================
--- /lib/wpsg_db.class.php	(revision 8516)
+++ /lib/wpsg_db.class.php	(revision 8517)
@@ -26,8 +26,14 @@
 			global $wpdb;
 
-			$row = $wpdb->get_row($wpdb->prepare($strQuery, ... $arg), ARRAY_N);
-
-			if ($row === null) return null;
-			else return $row[0];
+			$result = $wpdb->get_var($wpdb->prepare($strQuery, ... $arg), ARRAY_N);
+var_dump($result);
+			if ($result === null) {
+				
+				$this->handleError();
+				
+				return null;
+				
+			}
+			else return strval($result);
 			
 		}
@@ -52,5 +58,5 @@
 			} 
 			
-		} // function fetchOne($strQuery)
+		} 
 		
 		/**
Index: /mods/mod_rechnungen/wpsg_invoice.php
===================================================================
--- /mods/mod_rechnungen/wpsg_invoice.php	(revision 8516)
+++ /mods/mod_rechnungen/wpsg_invoice.php	(revision 8517)
@@ -409,17 +409,66 @@
          * @return wpsg_invoice[]
          */
-        public static function findByOrderId(int $order_id) {
-
-            $r = [];
-
-            foreach ($GLOBALS['wpsg_db']->fetchAssocField("SELECT `invoice_id` FROM `".WPSG_TBL_ORDER_INVOICE."` WHERE `order_id` = '".intval($order_id)."' ") as $invoice_id) {
-
-                $r[] = wpsg_invoice::getInstance(intval($invoice_id));
-
-            }
-
-            return $r;
-
-        }
-
+		public static function findByOrderId(int $order_id) {
+			
+			$r = [];
+			
+			foreach ($GLOBALS['wpsg_db']->fetchAssocField("SELECT `invoice_id` FROM `".WPSG_TBL_ORDER_INVOICE."` WHERE `order_id` = '".intval($order_id)."' ") as $invoice_id) {
+				
+				$r[] = wpsg_invoice::getInstance(intval($invoice_id));
+			
+			}
+			
+			return $r;
+		
+		}
+
+		 /**
+		  * @throws \Exception
+		  */
+	    public static function getNextInvoiceNumber(int $order_id, bool $do = false): string {
+			
+			global $wpdb;
+			
+			$lock_name = 'wpsg_rechnungen_format_lock';
+			
+			if (\wpsg_ShopController::getShop()->get_option('wpsg_lockOrderTables') != '1') {
+				
+				$locked = intval($wpdb->get_var($wpdb->prepare("SELECT GET_LOCK(%s, 10)", $lock_name)));			
+				
+				if ($locked !== 1) throw new \Exception("OrderOption kann nicht gesperrt werden 1778490898115");
+				
+			}
+			
+			// Rechnungsnummer bilden
+			$rnr_format = \wpsg_ShopController::getShop()->get_option("wpsg_rechnungen_format");
+			if ($rnr_format == "") $rnr_format = "%nr%";
+			
+			// Neue Fortlaufende Nummer
+			$rnr_nr = $wpdb->get_var("SELECT `option_value` FROM `{$wpdb->options}` WHERE `option_name` = 'wpsg_rechnungen_start'");
+			if ($rnr_nr <= 0) $rnr_nr = 1;
+			
+			// Ersetzungen
+			$rnr = preg_replace("/\%nr\%/i", strval($rnr_nr), $rnr_format);
+			$rnr = preg_replace("/\%Jahr\%/i", date("Y"), $rnr);
+			$rnr = preg_replace("/\%Monat\%/i", date("m"), $rnr);
+			$rnr = preg_replace("/\%Tag\%/i", date("d"), $rnr);
+			
+			$rnr = \wpsg_ShopController::getShop()->replaceUniversalPlatzhalter($rnr, $order_id);
+			
+			if ($do === true) {
+				
+				$wpdb->query($wpdb->prepare("UPDATE `{$wpdb->options}` SET `option_value` = %d WHERE `option_name` = 'wpsg_rechnungen_start' ", $rnr_nr + 1));
+				
+				if (\wpsg_ShopController::getShop()->get_option('wpsg_lockOrderTables') != '1') {
+					
+					$wpdb->query($wpdb->prepare("SELECT RELEASE_LOCK(%s)", $lock_name));
+					
+				}
+				
+			}
+			
+			return $rnr;
+			
+		}
+		
 	}
Index: /mods/wpsg_mod_rechnungen.class.php
===================================================================
--- /mods/wpsg_mod_rechnungen.class.php	(revision 8516)
+++ /mods/wpsg_mod_rechnungen.class.php	(revision 8517)
@@ -653,4 +653,5 @@
 				
 				$this->shop->view['order'] = $this->shop->cache->loadOrder($oInvoice->getOrderIds()[0]);
+				$this->shop->view['kunde'] = $oInvoice->getCustomer()->data; 
 				
 				// Wurde die Bestellung in einer anderen Sprache ausgefÃŒhrt
@@ -659,5 +660,6 @@
 				$this->getAdresse($this->shop->view['order']['k_id']);
 				
-				$email = $this->shop->view['kunde']['email'];
+				$email = $oInvoice->getCustomer()->getEmail();
+				// $email = $this->shop->view['kunde']['email'];
 				
 				$oCustomer = $oInvoice->getCustomer();
@@ -667,5 +669,5 @@
 
                 $rn_file = $oInvoice->getFilePath();
-                $rn_file_copy = dirname($oInvoice->getFilePath()).DIRECTORY_SEPARATOR.'copy_'.basename($oInvoice->getFilePath());
+                $rn_file_copy = dirname($oInvoice->getFilePath()).DIRECTORY_SEPARATOR.'copy_'.$oInvoice->getNr().'.pdf';
 
 				// Nun schauen ob die Original Rechnung existiert
@@ -720,6 +722,6 @@
 				
 				$pdf->Output($rn_file_copy, 'F');
- 
-				$rn_file_copy = $this->copyTemp($rn_file_copy, $nr);
+ 				
+				// $rn_file_copy = $this->copyTemp($rn_file_copy, $nr);
 				
 				// Bestellung in anderer Sprache ?
@@ -739,5 +741,7 @@
 				}
 				
-				$arFiles = array($rn_file_copy);
+				$arFiles = [
+					$nr.'.pdf' => $rn_file_copy
+				];
  			
 				if ($this->shop->get_option('wpsg_widerrufsformular_invoice') === '1' && wpsg_isSizedString($this->shop->get_option('wpsg_revocationform'))) {
@@ -1526,19 +1530,5 @@
 			} else {
 				
-				// Rechnungsnummer bilden
-				$rnr_format = $this->shop->get_option("wpsg_rechnungen_format");
-				if ($rnr_format == "") $rnr_format = "%nr%";
-				
-				// Neue Fortlaufende Nummer
-				$rnr_nr = intval($this->shop->get_option("wpsg_rechnungen_start"));
-				if ($rnr_nr <= 0) $rnr_nr = 1;
-				
-				// Ersetzungen
-				$rnr = preg_replace("/\%nr\%/i", $rnr_nr, $rnr_format);
-				$rnr = preg_replace("/\%Jahr\%/i", date("Y"), $rnr);
-				$rnr = preg_replace("/\%Monat\%/i", date("m"), $rnr);
-				$rnr = preg_replace("/\%Tag\%/i", date("d"), $rnr);
-				
-				$rnr = $this->shop->replaceUniversalPlatzhalter($rnr, $order_id);
+				$rnr = \wpsg\wpsg_invoice::getNextInvoiceNumber($order_id, $preview !== true);				 
 				$oInvoice = null;
 				
@@ -1558,7 +1548,5 @@
 				$this->shop->view['preview'] = false;
 				$this->shop->view['rnr'] = $rnr;
-				
-				$this->shop->update_option("wpsg_rechnungen_start", ($rnr_nr + 1));
-				
+				 
 				// Status Ã€ndern
 				if ($_REQUEST['wpsg_rechnungen_status'] == "1") {
@@ -1644,9 +1632,8 @@
 					
 					//Mail versenden
-
-                    $oInvoice = \wpsg\wpsg_invoice::getInstance(intval($r_id));
-
-                    $file = $oInvoice->getFilePath();
-					$file = $this->copyTemp($file, $rnr);
+					
+					$oInvoice = \wpsg\wpsg_invoice::getInstance(intval($r_id));
+					
+					$file = $oInvoice->getFilePath();
 					
 					$mail_text = $this->shop->render(WPSG_PATH_VIEW."/mods/mod_rechnungen/invoicemail.phtml", false);
@@ -1657,5 +1644,7 @@
 					$empfaenger = $_REQUEST['wpsg_rechnungen_email'];
 
-					$arFiles = array($file);
+					$arFiles = [
+						$rnr.'.pdf' => $file
+					];
 	 
 					if ($this->shop->get_option('wpsg_widerrufsformular_invoice') === '1' && wpsg_isSizedString($this->shop->get_option('wpsg_revocationform'))) {
@@ -1830,19 +1819,5 @@
 			$this->shop->view['arCalculationSum'] = $arCalculationSum;
  
-			// Rechnungsnummer bilden
-			$rnr_format = $this->shop->get_option("wpsg_rechnungen_format");
-			if ($rnr_format == "") $rnr_format = "%nr%";
-			
-			// Neue Fortlaufende Nummer
-			$rnr_nr = intval($this->shop->get_option("wpsg_rechnungen_start"));
-			if ($rnr_nr <= 0) $rnr_nr = 1;
-			
-			// Ersetzungen
-			$rnr = preg_replace("/\%nr\%/i", $rnr_nr, $rnr_format);
-			$rnr = preg_replace("/\%Jahr\%/i", date("Y"), $rnr);
-			$rnr = preg_replace("/\%Monat\%/i", date("m"), $rnr);
-			$rnr = preg_replace("/\%Tag\%/i", date("d"), $rnr);
-			
-			$rnr = $this->shop->replaceUniversalPlatzhalter($rnr, $order_id);
+			$rnr = \wpsg\wpsg_invoice::getNextInvoiceNumber(0, $preview !== true); 
 			
 			if ($preview) {
@@ -1856,7 +1831,5 @@
 				
 				$this->shop->view['preview'] = false;
-				$this->shop->view['rnr'] = $rnr;
-				
-				$this->shop->update_option("wpsg_rechnungen_start", ($rnr_nr + 1));
+				$this->shop->view['rnr'] = $rnr; 
 				
 				$r_id = $this->db->importQuery(
@@ -1918,5 +1891,5 @@
 
 					$file = $oInvoice->getFilePath();
-					$file = $this->copyTemp($file, $rnr);
+					// $file = $this->copyTemp($file, $rnr);
 
 					$mail_text = $this->shop->render(WPSG_PATH_VIEW."/mods/mod_rechnungen/multi_invoicemail.phtml", false);
@@ -1928,5 +1901,8 @@
 					
 					$header = "";
-					$arFiles = array($file);
+					
+					$arFiles = [
+						$rnr.'.pdf' => $file
+					];
 
 					if ($this->shop->get_option('wpsg_widerrufsformular_invoice') === '1' && wpsg_isSizedString($this->shop->get_option('wpsg_revocationform'))) {
@@ -2064,12 +2040,15 @@
 				
 				if (!$bPreview) {
-
-                    $oInvoice = \wpsg\wpsg_invoice::getInstance(intval($g_id));
-                    $file = $oInvoice->getFilePath();
-					$file = $this->copyTemp($file, $gnr);
+					
+					$oInvoice = \wpsg\wpsg_invoice::getInstance(intval($g_id));
+					$file = $oInvoice->getFilePath();
+					
+					// $file = $this->copyTemp($file, $gnr);
 				
 					$to = $_REQUEST['storno_mail'];
 					
-					$arFiles = array($file);
+					$arFiles = [
+						$gnr.'.pdf' => $file
+					];
 					$this->shop->sendMail($mail_text, $to, 'accountingmail', $arFiles, false, false, $mail_html);
 				
@@ -2229,22 +2208,5 @@
 			
 		}
-		
-		/**
-		 * Kopiert eine Datei temporÃ€r und benennt sie anhand der Rechnugnsnummer um
-		 *
-		 */
-		private function copyTemp($file, $rnr) {
-			
-			$path = $this->shop->getTempDir();
-
-// wpsg_debug('Source: '.$file);
-// wpsg_debug('Destination: '.$path.'/'.$rnr.'.pdf');
-
-			wpsg_copy($file, $path.'/'.$rnr.'.pdf');
-
-			return $path.'/'.$rnr.'.pdf';
-			
-		}
-        
+		 
         /**
          * Startet den Download einer Rechnung. Wird im Frontend wie im Backend verwendet
