Bläddra i källkod

print und upload

robert 2 år sedan
förälder
incheckning
dc460a9cc1
1 ändrade filer med 82 tillägg och 17 borttagningar
  1. 82 17
      api/controllers/RequestController.php

+ 82 - 17
api/controllers/RequestController.php

@@ -19,6 +19,7 @@ if(!function_exists("printer_write"))
 class RequestController
 {
 	private $mdate;
+	private $invoice_date;
 	private $dbh;
     private $project;
     private $project_id;
@@ -27,6 +28,7 @@ class RequestController
 	{
         date_default_timezone_set('Europe/Berlin');
 		$this->mdate = date('Y-m-d H:i:s', strtotime("now"));
+		$this->invoice_date = date('Y-m-d', strtotime("now"));
 		$this->dbh = new PDO(DB_CONN_STRING, DB_USER, DB_PASSWORD);
 		$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
         $q = $this->dbh->query("SELECT * FROM projects			 				  
@@ -186,8 +188,64 @@ class RequestController
 
 	private function posUpload ($data)
 	{
-		print_r($_POST);
-		print_r($_GET);
+		$data = array(
+			'post' => $_POST,
+			'get' => $_GET
+		);
+		$data = json_encode($data, JSON_PRETTY_PRINT);
+		$this->dbh->exec("INSERT INTO logs VALUES ( '{$this->mdate}', 'pos_upload', '{$data}');");
+		$data = array();
+
+		$total = 0;
+		$items = array();
+		$i = 1;
+
+		foreach ($_POST as $item => $price) {
+			$split = explode('-', $item);
+			if (count($split) < 2) {
+				$split[] = '000';
+			}
+			$items[] = array(
+				'line_number' => $i,
+				'item' => $item,
+				'seller_id' => $split[0],
+				'item_id' => $split[1],
+				'price' => $price
+			);
+			$i++;
+			$total += $price;
+		}
+
+
+		$d = array(
+			'pos_id' => '0',
+			'invoice_date' => $this->mdate,
+			'cashier' => '',
+			'line_count' => count($items),
+			'total' => $total,
+			'paid' => 0,
+			'details' => $items
+		);
+		$d = array_merge($d, $_GET);
+
+		$this->dbh->exec("INSERT INTO invoice_header (project_id, pos_id, invoice_date, cashier, line_count, total, paid, mdate, cdate) 
+							VALUES ( '{$this->project_id}','{$d['pos_id']}',  '{$d['invoice_date']}',
+				 					'{$d['cashier']}','{$d['line_count']}', '{$d['total']}','{$d['paid']}', 
+				 					'{$this->mdate}','{$this->mdate}' )");
+		$d['invoice_number'] = $this->dbh->lastInsertId();
+
+		foreach ($d['details'] as $item) {
+			if ($item['item'] != "") {
+			$this->dbh->exec("INSERT INTO invoice_details (project_id, invoice_number, line_number, item, seller_id, item_id, price) 
+									   VALUES ( '{$this->project_id}','{$d['invoice_number']}', '{$item['line_number']}',
+												   '{$item['item']}','{$item['seller_id']}','{$item['item_id']}', 
+													  '{$item['price']}' )");
+			}
+		}
+		// header("Location: {$_SERVER['HTTP_REFERER']}");
+		echo "<html><body><pre>" . $this->printFormat($d) . "</pre><br/>";
+		echo "<form action=\"" . $_SERVER['HTTP_REFERER'] . "\" method=\"POST\"><input type=\"submit\" value=\"Nächster Kunde\"/></form></body></html>";
+		exit();
 	}
 
 	private function posInfos ($data)
@@ -261,28 +319,35 @@ class RequestController
         // $ptr = printer_open("POS-58");
         // printer_set_option($ptr, PRINTER_MODE, "raw");
 		$ptr = "/dev/usb/lp0";
+		printer_write($ptr, $this->printFormat($result));
+
+        // printer_close($ptr);
+
+        return $result;
+    }
 
-        printer_write($ptr, $this->project['print_header']);
-        printer_write($ptr, str_repeat("\n", 1));
+	private function printFormat ($data)
+	{
+		$result = array();
+        $result[] = $this->project['print_header'];
+        $result[] = str_repeat("\n", 1);
         
-        printer_write($ptr, "Rechn.-Nr. " . $result['invoice_number']);
-        printer_write($ptr, "Kasse " . $result['pos_id'] . ", " . $result['cashier']);
+        $result[] = "Rechn.-Nr. " . $data['invoice_number'];
+        $result[] = "Kasse " . $data['pos_id'] . ", " . $data['cashier'];
 
-        $result['details'] = array_reverse($result['details']);
+        $data['details'] = array_reverse($data['details']);
 
-        foreach ($result['details'] as $entry) {
-            printer_write($ptr, "#" . str_pad(str_replace(".",",",$entry['line_number']), 2, " ", STR_PAD_LEFT) .  ": " . $entry['item'] . "" . str_pad(str_replace(".",",",$entry['price']), 19, " ", STR_PAD_LEFT));
+        foreach ($data['details'] as $entry) {
+            $result[] = "#" . str_pad(str_replace(".",",",$entry['line_number']), 2, " ", STR_PAD_LEFT) .  ": " . $entry['item'] . "" . str_pad(number_format($entry['price'], 2, ",", "."), 19, " ", STR_PAD_LEFT);
         }
 
-        printer_write($ptr, "________________________________");
-        printer_write($ptr, str_pad(str_replace(".",",",$result['total']), 31, " ", STR_PAD_LEFT));
+        $result[] = "________________________________";
+        $result[] = str_pad(number_format($data['total'], 2, ",", "."), 31, " ", STR_PAD_LEFT);
 
-        printer_write($ptr, $this->project['print_footer']);
-        printer_write($ptr, str_repeat("\n", 7));
-        // printer_close($ptr);
-
-        return $result;
-    }
+        $result[] = $this->project['print_footer'];
+        $result[] = str_repeat("\n", 7);
+		return implode("\n", $result);
+	}
 
     private function posCancel ($data)
     {