RequestController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. $url = "https://pos58.global-cube.online/";
  281. $payload = array('data' => $this->printFormat($result));
  282. $curl = curl_init($url);
  283. curl_setopt($curl, CURLOPT_POST, true);
  284. curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
  285. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  286. $response = curl_exec($curl);
  287. curl_close($curl);
  288. // printer_close($ptr);
  289. return $response;
  290. }
  291. private function printFormat ($data)
  292. {
  293. $result = array();
  294. $result[] = $this->project['print_header'];
  295. $result[] = str_repeat("\n", 1);
  296. $result[] = "Rechn.-Nr. " . $data['invoice_number'];
  297. $result[] = "Kasse " . $data['pos_id'] . ", " . $data['cashier'];
  298. $data['details'] = array_reverse($data['details']);
  299. foreach ($data['details'] as $entry) {
  300. $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);
  301. }
  302. $result[] = "________________________________";
  303. $result[] = str_pad(number_format($data['total'], 2, ",", "."), 31, " ", STR_PAD_LEFT);
  304. $result[] = $this->project['print_footer'];
  305. $result[] = str_repeat("\n", 4);
  306. return implode("\n", $result);
  307. }
  308. private function posCancel ($data)
  309. {
  310. $result = "error";
  311. if (isset($data['item']) && $data['item'] != "") {
  312. $item = $data['item'];
  313. $this->dbh->exec("UPDATE invoice_details SET
  314. cancelled = '1'
  315. WHERE item = '{$item}' ");
  316. $result = "OK";
  317. }
  318. return $result;
  319. }
  320. private function projects ($data)
  321. {
  322. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  323. if (in_array($seller['seller_id'], $seller_ids)) {
  324. $this->dbh->exec("UPDATE projects
  325. SET event_date = '{$data['event_date']}',
  326. event_location = '{$data['event_location']}',
  327. print_header = '{$data['print_header']}',
  328. print_footer = '{$data['print_footer']}',
  329. active = '{$data['active']}',
  330. mdate = '{$this->mdate}'
  331. WHERE project_id = '{$data['project_id']}' ");
  332. } else {
  333. $this->dbh->exec("INSERT INTO projects (project_id, event_date, event_location, print_header, print_footer, active, mdate, cdate)
  334. VALUES ( '{$data['project_id']}','{$data['event_date']}', '{$data['event_location']}', '{$data['print_header']}',
  335. '{$data['print_footer']}', '{$data['active']}','{$this->mdate}','{$this->mdate}' )");
  336. }
  337. }
  338. $q = $this->dbh->query("SELECT * FROM projects ORDER BY project_id");
  339. return $q->fetchAll(PDO::FETCH_ASSOC);
  340. }
  341. private function sellers ($data)
  342. {
  343. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  344. $q = $this->dbh->query("SELECT seller_id FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  345. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  346. $seller_ids = array();
  347. if ($r) {
  348. $seller_ids = array_column($r, 'seller_id');
  349. }
  350. foreach ($data['sellers'] as $seller) {
  351. if (in_array($seller['seller_id'], $seller_ids)) {
  352. $this->dbh->exec("UPDATE sellers
  353. SET name = '{$seller['name']}',
  354. first_name = '{$seller['first_name']}',
  355. address = '{$seller['address']}',
  356. zip_code = '{$seller['zip_code']}',
  357. place = '{$seller['place']}',
  358. mail = '{$seller['mail']}',
  359. phone = '{$seller['phone']}',
  360. mdate = '{$this->mdate}'
  361. WHERE project_id = '{$this->project_id}' AND seller_id = '{$seller['seller_id']}' ");
  362. } else {
  363. $this->dbh->exec("INSERT INTO sellers (project_id, seller_id, name, first_name, address, zip_code, place, mail, phone, mdate, cdate)
  364. VALUES ( '{$this->project_id}','{$seller['seller_id']}', '{$seller['name']}', '{$seller['first_name']}',
  365. '{$seller['address']}', '{$seller['zip_code']}', '{$seller['place']}','{$seller['mail']}',
  366. '{$seller['phone']}','{$this->mdate}','{$this->mdate}' )");
  367. }
  368. }
  369. }
  370. $q = $this->dbh->query("SELECT * FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  371. return $q->fetchAll(PDO::FETCH_ASSOC);
  372. }
  373. public function CsvExport ($data)
  374. {
  375. $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
  376. FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  377. $sellers = array();
  378. foreach ($q->fetchAll(PDO::FETCH_ASSOC) as $s) {
  379. $sellers[$s['seller_id']] = $s;
  380. for ($j = 1; $j <= 50; $j++) {
  381. $item_id = substr("00".$j, -3);
  382. $sellers[$s['seller_id']][$item_id] = "n.v.";
  383. }
  384. }
  385. // Verkäufe nachladen,
  386. $q = $this->dbh->query("SELECT * FROM invoice_details WHERE project_id = '{$this->project_id}' AND cancelled = 0");
  387. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  388. if (isset($sellers[$row['seller_id']])) {
  389. $sellers[$row['seller_id']][$row['item_id']] = $row['price'];
  390. } else {
  391. $sellers[] = $row;
  392. }
  393. }
  394. // Summen berechnen
  395. $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 ");
  396. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  397. if (isset($sellers[$row['seller_id']])) {
  398. $sellers[$row['seller_id']]['total'] = $row['price'];
  399. $sellers[$row['seller_id']]['provision'] = $row['price'] * 0.2;
  400. $sellers[$row['seller_id']]['cash_out'] = $row['price'] * 0.8;
  401. } else {
  402. $sellers[] = $row;
  403. }
  404. }
  405. $result = "Verk-Nr;Name;Vorname;Adresse;PLZ;Ort;E-Mail;Telefon;Gesamt;Provision;Auszahlung;A001;A002;A003;A004;A005;A006;A007;A008;A009;A010;" .
  406. "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;" .
  407. "A036;A037;A038;A039;A040;A041;A042;A043;A044;A045;A046;A047;A048;A049;A050\r\n";
  408. $blacklist = array("seller_id", "name", "first_name", "address", "zip_code", "place", "mail", "phone");
  409. foreach ($sellers as $s) {
  410. foreach ($s as $k => $v) {
  411. if (!in_array($k, $blacklist) && $v != "n.v.") {
  412. $s[$k] = @number_format($v, 2, ",", ".");
  413. }
  414. }
  415. $result .= implode(";", $s) . "\r\n";
  416. }
  417. return $result;
  418. }
  419. private function jsonLastErrorMsg ()
  420. {
  421. switch (json_last_error()) {
  422. case JSON_ERROR_NONE:
  423. return ' - No errors';
  424. case JSON_ERROR_DEPTH:
  425. return ' - Maximum stack depth exceeded';
  426. case JSON_ERROR_STATE_MISMATCH:
  427. return ' - Underflow or the modes mismatch';
  428. case JSON_ERROR_CTRL_CHAR:
  429. return ' - Unexpected control character found';
  430. case JSON_ERROR_SYNTAX:
  431. return ' - Syntax error, malformed JSON';
  432. case JSON_ERROR_UTF8:
  433. return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  434. default:
  435. return ' - Unknown error';
  436. }
  437. }
  438. }