RequestController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. require_once(dirname(__FILE__)."/../config.php");
  3. if(!function_exists("array_column"))
  4. {
  5. function array_column($array,$column_name)
  6. {
  7. return array_map(function($element) use($column_name){return $element[$column_name];}, $array);
  8. }
  9. }
  10. if(!function_exists("printer_write"))
  11. {
  12. function printer_write($printer, $text) {
  13. shell_exec("echo \"" . $text . "\" > " . $printer);
  14. }
  15. }
  16. class RequestController
  17. {
  18. private $mdate;
  19. private $invoice_date;
  20. private $dbh;
  21. private $project;
  22. private $project_id;
  23. function __construct ($path = "", $path2 = "")
  24. {
  25. date_default_timezone_set('Europe/Berlin');
  26. $this->mdate = date('Y-m-d H:i:s', strtotime("now"));
  27. $this->invoice_date = date('Y-m-d', strtotime("now"));
  28. $this->dbh = new PDO(DB_CONN_STRING, DB_USER, DB_PASSWORD);
  29. $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
  30. $q = $this->dbh->query("SELECT * FROM projects
  31. WHERE active = '1'
  32. ORDER BY project_id DESC
  33. LIMIT 1");
  34. $this->project = $q->fetch(PDO::FETCH_ASSOC);
  35. $this->project_id = $this->project['project_id'];
  36. }
  37. function bootstrap ($data)
  38. {
  39. $result = array();
  40. $action = (isset($_REQUEST['a'])) ? $_REQUEST['a'] : "";
  41. switch ($action) {
  42. case 'pos_token':
  43. $result = $this->posToken($data);
  44. break;
  45. case 'pos_invoices':
  46. $result = $this->posInvoices($data);
  47. break;
  48. case 'pos_upload':
  49. $result = $this->posUpload($data);
  50. break;
  51. case 'pos_checkout':
  52. $result = $this->posCheckout($data);
  53. break;
  54. case 'pos_print':
  55. $result = $this->posPrint($data);
  56. break;
  57. case 'pos_infos':
  58. $result = $this->posInfos($data);
  59. break;
  60. case 'pos_infos2':
  61. $result = $this->posInfos2($data);
  62. break;
  63. case 'pos_cancel':
  64. $result = $this->posCancel($data);
  65. break;
  66. case 'projects':
  67. $result = $this->projects($data);
  68. break;
  69. case 'sellers':
  70. $result = $this->sellers($data);
  71. break;
  72. default:
  73. $result = array("No action defined!");
  74. break;
  75. }
  76. $this->setJsonHeader();
  77. echo json_encode($result);
  78. }
  79. private function setJsonHeader ()
  80. {
  81. header('Access-Control-Allow-Origin: *');
  82. header('Access-Control-Allow-Methods: GET, POST, PUT');
  83. header('Access-Control-Allow-Headers: accept, content-type');
  84. header('Cache-Control: no-cache, must-revalidate');
  85. header('Content-type: application/json');
  86. }
  87. private function posToken ($data)
  88. {
  89. $disabled = array('token' => 0, 'active' => 0, 'disabled' => 1);
  90. $q = $this->dbh->query("SELECT * FROM pos
  91. WHERE pos_id = '{$data['pos_id']}' LIMIT 1");
  92. $result = $q->fetch(PDO::FETCH_ASSOC);
  93. if (!$result || $result['disabled'] != 0) {
  94. return $disabled;
  95. }
  96. if ($data['token'] == 0) {
  97. if ($result['token'] != 0) {
  98. return $disabled;
  99. }
  100. $result['token'] = rand(1, 65000);
  101. $result['active'] = 1;
  102. } else {
  103. if ($data['token'] != $result['token']) {
  104. return $disabled;
  105. }
  106. $result['active'] = ($result['disabled'] == 1) ? 0 : $data['active'];
  107. $result['disabled'] = $data['disabled'];
  108. }
  109. $this->dbh->exec("UPDATE pos SET token = '{$result['token']}', active = '{$result['active']}', disabled = '{$result['disabled']}', mdate = '{$this->mdate}'
  110. WHERE pos_id = '{$result['pos_id']}' ");
  111. return $result;
  112. }
  113. private function posInvoices ($data)
  114. {
  115. /*
  116. if (!isset($data['params']) || isset($data['params']['token']) || $data['params']['token'] == 0) {
  117. return false;
  118. }
  119. */
  120. if (isset($data['invoice'])) {
  121. $d = $data['invoice'];
  122. if ($d['invoice_number'] == 0) {
  123. $this->dbh->exec("INSERT INTO invoice_header (project_id, pos_id, invoice_date, cashier, line_count, total, paid, mdate, cdate)
  124. VALUES ( '{$this->project_id}','{$d['pos_id']}', '{$d['invoice_date']}',
  125. '{$d['cashier']}','{$d['line_count']}', '{$d['total']}','{$d['paid']}',
  126. '{$this->mdate}','{$this->mdate}' )");
  127. $data['invoice_number'] = $this->dbh->lastInsertId();
  128. } else {
  129. $data['invoice_number'] = $d['invoice_number'];
  130. $this->dbh->exec("UPDATE invoice_header SET
  131. pos_id = '{$d['pos_id']}',
  132. invoice_date = '{$d['invoice_date']}',
  133. cashier = '{$d['cashier']}',
  134. line_count = '{$d['line_count']}',
  135. total = '{$d['total']}',
  136. paid = '{$d['paid']}',
  137. mdate = '{$this->mdate}'
  138. WHERE invoice_number = '{$data['invoice_number']}' ");
  139. }
  140. $this->dbh->exec("DELETE FROM invoice_details
  141. WHERE invoice_number = '{$data['invoice_number']}' ");
  142. foreach ($d['details'] as $item) {
  143. if ($item['item'] != "") {
  144. $this->dbh->exec("INSERT INTO invoice_details (project_id, invoice_number, line_number, item, seller_id, item_id, price)
  145. VALUES ( '{$this->project_id}','{$data['invoice_number']}', '{$item['line_number']}',
  146. '{$item['item']}','{$item['seller_id']}','{$item['item_id']}',
  147. '{$item['price']}' )");
  148. }
  149. }
  150. }
  151. if (!isset($data['invoice_number'])) {
  152. $data['invoice_number'] = $data['invoice']['invoice_number'];
  153. }
  154. $q = $this->dbh->query("SELECT * FROM invoice_header WHERE invoice_number = '{$data['invoice_number']}' LIMIT 1");
  155. $result = $q->fetch(PDO::FETCH_ASSOC);
  156. if ($result) {
  157. $q = $this->dbh->query("SELECT * FROM invoice_details
  158. WHERE invoice_number = '{$data['invoice_number']}'
  159. ORDER BY line_number DESC ");
  160. $result['details'] = $q->fetchAll(PDO::FETCH_ASSOC);
  161. }
  162. return $result;
  163. }
  164. private function posUpload ($data)
  165. {
  166. $data = array(
  167. 'post' => $_POST,
  168. 'get' => $_GET
  169. );
  170. $data = json_encode($data, JSON_PRETTY_PRINT);
  171. $this->dbh->exec("INSERT INTO logs VALUES ( '{$this->mdate}', 'pos_upload', '{$data}');");
  172. $data = array();
  173. $total = 0;
  174. $items = array();
  175. $i = 1;
  176. foreach ($_POST as $item => $price) {
  177. $split = explode('-', $item);
  178. if (count($split) < 2) {
  179. $split[] = '000';
  180. }
  181. $items[] = array(
  182. 'line_number' => $i,
  183. 'item' => $item,
  184. 'seller_id' => $split[0],
  185. 'item_id' => $split[1],
  186. 'price' => $price
  187. );
  188. $i++;
  189. $total += $price;
  190. }
  191. $d = array(
  192. 'pos_id' => '0',
  193. 'invoice_date' => $this->mdate,
  194. 'cashier' => '',
  195. 'line_count' => count($items),
  196. 'total' => $total,
  197. 'paid' => 0,
  198. 'details' => $items
  199. );
  200. $d = array_merge($d, $_GET);
  201. $this->dbh->exec("INSERT INTO invoice_header (project_id, pos_id, invoice_date, cashier, line_count, total, paid, mdate, cdate)
  202. VALUES ( '{$this->project_id}','{$d['pos_id']}', '{$d['invoice_date']}',
  203. '{$d['cashier']}','{$d['line_count']}', '{$d['total']}','{$d['paid']}',
  204. '{$this->mdate}','{$this->mdate}' )");
  205. $d['invoice_number'] = $this->dbh->lastInsertId();
  206. foreach ($d['details'] as $item) {
  207. if ($item['item'] != "") {
  208. $this->dbh->exec("INSERT INTO invoice_details (project_id, invoice_number, line_number, item, seller_id, item_id, price)
  209. VALUES ( '{$this->project_id}','{$d['invoice_number']}', '{$item['line_number']}',
  210. '{$item['item']}','{$item['seller_id']}','{$item['item_id']}',
  211. '{$item['price']}' )");
  212. }
  213. }
  214. // header("Location: {$_SERVER['HTTP_REFERER']}");
  215. echo '<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>';
  216. echo "<body><pre>" . $this->printFormat($d) . "</pre><br/>";
  217. echo "<form action=\"" . $_SERVER['HTTP_REFERER'] . "\" method=\"POST\"><input type=\"submit\" value=\"Nächster Kunde\"/></form></body></html>";
  218. exit();
  219. }
  220. private function posInfos ($data)
  221. {
  222. $result = array('items' => array(), 'invoices' => array());
  223. $q = $this->dbh->query("SELECT item FROM invoice_details
  224. WHERE project_id = '{$this->project_id}' AND item NOT IN ('000-000','123-456') AND cancelled = 0
  225. ORDER BY item ");
  226. if ($q) {
  227. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  228. $result['items'] = array_column($r, 'item');
  229. }
  230. if ($data['pos_id'] == 0) {
  231. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  232. WHERE a.project_id = '{$this->project_id}'
  233. ORDER BY a.invoice_number ");
  234. } else {
  235. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  236. WHERE a.project_id = '{$this->project_id}' AND a.pos_id = '{$data['pos_id']}'
  237. ORDER BY a.invoice_number ");
  238. }
  239. if ($q) {
  240. $result['invoices'] = $q->fetchAll(PDO::FETCH_ASSOC);
  241. }
  242. return $result;
  243. }
  244. private function posInfos2 ($data)
  245. {
  246. $result = array('items' => array(), 'invoices' => array());
  247. if ($data['pos_id'] == 0) {
  248. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  249. WHERE a.project_id = '{$this->project_id}'
  250. ORDER BY a.invoice_number ");
  251. } else {
  252. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  253. WHERE a.project_id = '{$this->project_id}' AND a.pos_id = '{$data['pos_id']}'
  254. ORDER BY a.invoice_number ");
  255. }
  256. if ($q) {
  257. $result['invoices'] = $q->fetchAll(PDO::FETCH_ASSOC);
  258. }
  259. return $result;
  260. }
  261. private function posCheckout ($data)
  262. {
  263. $result = $data['invoice'];
  264. $this->dbh->exec("UPDATE invoice_header
  265. SET checkout = '{$result['checkout']}'
  266. WHERE invoice_number = '{$result['invoice_number']}' ");
  267. return $result;
  268. }
  269. private function posPrint ($data)
  270. {
  271. $result = $this->posInvoices($data);
  272. $this->dbh->exec("UPDATE invoice_header
  273. SET printed = '1'
  274. WHERE invoice_number = '{$result['invoice_number']}' ");
  275. // print options
  276. // $ptr = printer_open("POS-58");
  277. // printer_set_option($ptr, PRINTER_MODE, "raw");
  278. $ptr = "/dev/usb/lp0";
  279. printer_write($ptr, $this->printFormat($result));
  280. // printer_close($ptr);
  281. return $result;
  282. }
  283. private function printFormat ($data)
  284. {
  285. $result = array();
  286. $result[] = $this->project['print_header'];
  287. $result[] = str_repeat("\n", 1);
  288. $result[] = "Rechn.-Nr. " . $data['invoice_number'];
  289. $result[] = "Kasse " . $data['pos_id'] . ", " . $data['cashier'];
  290. $data['details'] = array_reverse($data['details']);
  291. foreach ($data['details'] as $entry) {
  292. $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);
  293. }
  294. $result[] = "________________________________";
  295. $result[] = str_pad(number_format($data['total'], 2, ",", "."), 31, " ", STR_PAD_LEFT);
  296. $result[] = $this->project['print_footer'];
  297. $result[] = str_repeat("\n", 7);
  298. return implode("\n", $result);
  299. }
  300. private function posCancel ($data)
  301. {
  302. $result = "error";
  303. if (isset($data['item']) && $data['item'] != "") {
  304. $item = $data['item'];
  305. $this->dbh->exec("UPDATE invoice_details SET
  306. cancelled = '1'
  307. WHERE item = '{$item}' ");
  308. $result = "OK";
  309. }
  310. return $result;
  311. }
  312. private function projects ($data)
  313. {
  314. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  315. if (in_array($seller['seller_id'], $seller_ids)) {
  316. $this->dbh->exec("UPDATE projects
  317. SET event_date = '{$data['event_date']}',
  318. event_location = '{$data['event_location']}',
  319. print_header = '{$data['print_header']}',
  320. print_footer = '{$data['print_footer']}',
  321. active = '{$data['active']}',
  322. mdate = '{$this->mdate}'
  323. WHERE project_id = '{$data['project_id']}' ");
  324. } else {
  325. $this->dbh->exec("INSERT INTO projects (project_id, event_date, event_location, print_header, print_footer, active, mdate, cdate)
  326. VALUES ( '{$data['project_id']}','{$data['event_date']}', '{$data['event_location']}', '{$data['print_header']}',
  327. '{$data['print_footer']}', '{$data['active']}','{$this->mdate}','{$this->mdate}' )");
  328. }
  329. }
  330. $q = $this->dbh->query("SELECT * FROM projects ORDER BY project_id");
  331. return $q->fetchAll(PDO::FETCH_ASSOC);
  332. }
  333. private function sellers ($data)
  334. {
  335. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  336. $q = $this->dbh->query("SELECT seller_id FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  337. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  338. $seller_ids = array();
  339. if ($r) {
  340. $seller_ids = array_column($r, 'seller_id');
  341. }
  342. foreach ($data['sellers'] as $seller) {
  343. if (in_array($seller['seller_id'], $seller_ids)) {
  344. $this->dbh->exec("UPDATE sellers
  345. SET name = '{$seller['name']}',
  346. first_name = '{$seller['first_name']}',
  347. address = '{$seller['address']}',
  348. zip_code = '{$seller['zip_code']}',
  349. place = '{$seller['place']}',
  350. mail = '{$seller['mail']}',
  351. phone = '{$seller['phone']}',
  352. mdate = '{$this->mdate}'
  353. WHERE project_id = '{$this->project_id}' AND seller_id = '{$seller['seller_id']}' ");
  354. } else {
  355. $this->dbh->exec("INSERT INTO sellers (project_id, seller_id, name, first_name, address, zip_code, place, mail, phone, mdate, cdate)
  356. VALUES ( '{$this->project_id}','{$seller['seller_id']}', '{$seller['name']}', '{$seller['first_name']}',
  357. '{$seller['address']}', '{$seller['zip_code']}', '{$seller['place']}','{$seller['mail']}',
  358. '{$seller['phone']}','{$this->mdate}','{$this->mdate}' )");
  359. }
  360. }
  361. }
  362. $q = $this->dbh->query("SELECT * FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  363. return $q->fetchAll(PDO::FETCH_ASSOC);
  364. }
  365. public function CsvExport ($data)
  366. {
  367. $q = $this->dbh->query("SELECT seller_id, name, first_name, address, zip_code, place, mail, phone, 0.0 as total, 0.0 as provision, 0.0 as cash_out
  368. FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  369. $sellers = array();
  370. foreach ($q->fetchAll(PDO::FETCH_ASSOC) as $s) {
  371. $sellers[$s['seller_id']] = $s;
  372. for ($j = 1; $j <= 50; $j++) {
  373. $item_id = substr("00".$j, -3);
  374. $sellers[$s['seller_id']][$item_id] = "n.v.";
  375. }
  376. }
  377. // Verkäufe nachladen,
  378. $q = $this->dbh->query("SELECT * FROM invoice_details WHERE project_id = '{$this->project_id}' AND cancelled = 0");
  379. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  380. if (isset($sellers[$row['seller_id']])) {
  381. $sellers[$row['seller_id']][$row['item_id']] = $row['price'];
  382. } else {
  383. $sellers[] = $row;
  384. }
  385. }
  386. // Summen berechnen
  387. $q = $this->dbh->query("SELECT seller_id, sum(price) as price FROM invoice_details WHERE project_id = '{$this->project_id}' AND cancelled = 0 GROUP BY seller_id ");
  388. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  389. if (isset($sellers[$row['seller_id']])) {
  390. $sellers[$row['seller_id']]['total'] = $row['price'];
  391. $sellers[$row['seller_id']]['provision'] = $row['price'] * 0.2;
  392. $sellers[$row['seller_id']]['cash_out'] = $row['price'] * 0.8;
  393. } else {
  394. $sellers[] = $row;
  395. }
  396. }
  397. $result = "Verk-Nr;Name;Vorname;Adresse;PLZ;Ort;E-Mail;Telefon;Gesamt;Provision;Auszahlung;A001;A002;A003;A004;A005;A006;A007;A008;A009;A010;" .
  398. "A011;A012;A013;A014;A015;A016;A017;A018;A019;A020;A021;A022;A023;A024;A025;A026;A027;A028;A029;A030;A031;A032;A033;A034;A035;" .
  399. "A036;A037;A038;A039;A040;A041;A042;A043;A044;A045;A046;A047;A048;A049;A050\r\n";
  400. $blacklist = array("seller_id", "name", "first_name", "address", "zip_code", "place", "mail", "phone");
  401. foreach ($sellers as $s) {
  402. foreach ($s as $k => $v) {
  403. if (!in_array($k, $blacklist) && $v != "n.v.") {
  404. $s[$k] = @number_format($v, 2, ",", ".");
  405. }
  406. }
  407. $result .= implode(";", $s) . "\r\n";
  408. }
  409. return $result;
  410. }
  411. private function jsonLastErrorMsg ()
  412. {
  413. switch (json_last_error()) {
  414. case JSON_ERROR_NONE:
  415. return ' - No errors';
  416. case JSON_ERROR_DEPTH:
  417. return ' - Maximum stack depth exceeded';
  418. case JSON_ERROR_STATE_MISMATCH:
  419. return ' - Underflow or the modes mismatch';
  420. case JSON_ERROR_CTRL_CHAR:
  421. return ' - Unexpected control character found';
  422. case JSON_ERROR_SYNTAX:
  423. return ' - Syntax error, malformed JSON';
  424. case JSON_ERROR_UTF8:
  425. return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  426. default:
  427. return ' - Unknown error';
  428. }
  429. }
  430. }