RequestController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. if(!function_exists("array_column"))
  3. {
  4. function array_column($array,$column_name)
  5. {
  6. return array_map(function($element) use($column_name){return $element[$column_name];}, $array);
  7. }
  8. }
  9. class RequestController
  10. {
  11. private $mdate;
  12. private $dbh;
  13. private $project;
  14. private $project_id;
  15. function __construct ($path = "", $path2 = "")
  16. {
  17. date_default_timezone_set('Europe/Berlin');
  18. $this->mdate = date('Y-m-d H:i:s', strtotime("now"));
  19. $this->dbh = new PDO("mysql:host=localhost;dbname=kasse", "root", "gc01mysql");
  20. $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
  21. $q = $this->dbh->query("SELECT * FROM projects
  22. WHERE active = '1'
  23. ORDER BY project_id DESC
  24. LIMIT 1");
  25. $this->project = $q->fetch(PDO::FETCH_ASSOC);
  26. $this->project_id = $this->project['project_id'];
  27. }
  28. function bootstrap ($data)
  29. {
  30. $result = array();
  31. switch ($_REQUEST['a']) {
  32. case 'pos_token':
  33. $result = $this->posToken($data);
  34. break;
  35. case 'pos_invoices':
  36. $result = $this->posInvoices($data);
  37. break;
  38. case 'pos_print':
  39. $result = $this->posPrint($data);
  40. break;
  41. case 'pos_infos':
  42. $result = $this->posInfos($data);
  43. break;
  44. case 'pos_infos2':
  45. $result = $this->posInfos2($data);
  46. break;
  47. case 'pos_cancel':
  48. $result = $this->posCancel($data);
  49. break;
  50. case 'projects':
  51. $result = $this->projects($data);
  52. break;
  53. case 'sellers':
  54. $result = $this->sellers($data);
  55. break;
  56. }
  57. $this->setJsonHeader();
  58. echo json_encode($result);
  59. }
  60. private function setJsonHeader ()
  61. {
  62. header('Access-Control-Allow-Origin: *');
  63. header('Access-Control-Allow-Methods: GET, POST, PUT');
  64. header('Access-Control-Allow-Headers: accept, content-type');
  65. header('Cache-Control: no-cache, must-revalidate');
  66. header('Content-type: application/json');
  67. }
  68. private function posToken ($data)
  69. {
  70. $disabled = array('token' => 0, 'active' => 0, 'disabled' => 1);
  71. $q = $this->dbh->query("SELECT * FROM pos
  72. WHERE pos_id = '{$data['pos_id']}' LIMIT 1");
  73. $result = $q->fetch(PDO::FETCH_ASSOC);
  74. if (!$result || $result['disabled'] != 0) {
  75. return $disabled;
  76. }
  77. if ($data['token'] == 0) {
  78. if ($result['token'] != 0) {
  79. return $disabled;
  80. }
  81. $result['token'] = rand(1, 65000);
  82. $result['active'] = 1;
  83. } else {
  84. if ($data['token'] != $result['token']) {
  85. return $disabled;
  86. }
  87. $result['active'] = ($result['disabled'] == 1) ? 0 : $data['active'];
  88. $result['disabled'] = $data['disabled'];
  89. }
  90. $this->dbh->exec("UPDATE pos SET token = '{$result['token']}', active = '{$result['active']}', disabled = '{$result['disabled']}', mdate = '{$this->mdate}'
  91. WHERE pos_id = '{$result['pos_id']}' ");
  92. return $result;
  93. }
  94. private function posInvoices ($data)
  95. {
  96. /*
  97. if (!isset($data['params']) || isset($data['params']['token']) || $data['params']['token'] == 0) {
  98. return false;
  99. }
  100. */
  101. if (isset($data['invoice'])) {
  102. $d = $data['invoice'];
  103. if ($d['invoice_number'] == 0) {
  104. $this->dbh->exec("INSERT INTO invoice_header (project_id, pos_id, invoice_date, cashier, line_count, total, paid, mdate, cdate)
  105. VALUES ( '{$this->project_id}','{$d['pos_id']}', '{$d['invoice_date']}',
  106. '{$d['cashier']}','{$d['line_count']}', '{$d['total']}','{$d['paid']}',
  107. '{$this->mdate}','{$this->mdate}' )");
  108. $data['invoice_number'] = $this->dbh->lastInsertId();
  109. } else {
  110. $data['invoice_number'] = $d['invoice_number'];
  111. $this->dbh->exec("UPDATE invoice_header SET
  112. pos_id = '{$d['pos_id']}',
  113. invoice_date = '{$d['invoice_date']}',
  114. cashier = '{$d['cashier']}',
  115. line_count = '{$d['line_count']}',
  116. total = '{$d['total']}',
  117. paid = '{$d['paid']}',
  118. mdate = '{$this->mdate}'
  119. WHERE invoice_number = '{$data['invoice_number']}' ");
  120. }
  121. $this->dbh->exec("DELETE FROM invoice_details
  122. WHERE invoice_number = '{$data['invoice_number']}' ");
  123. foreach ($d['details'] as $item) {
  124. if ($item['item'] != "") {
  125. $this->dbh->exec("INSERT INTO invoice_details (project_id, invoice_number, line_number, item, seller_id, item_id, price)
  126. VALUES ( '{$this->project_id}','{$data['invoice_number']}', '{$item['line_number']}',
  127. '{$item['item']}','{$item['seller_id']}','{$item['item_id']}',
  128. '{$item['price']}' )");
  129. }
  130. }
  131. }
  132. if (!isset($data['invoice_number'])) {
  133. $data['invoice_number'] = $data['invoice']['invoice_number'];
  134. }
  135. $q = $this->dbh->query("SELECT * FROM invoice_header WHERE invoice_number = '{$data['invoice_number']}' LIMIT 1");
  136. $result = $q->fetch(PDO::FETCH_ASSOC);
  137. if ($result) {
  138. $q = $this->dbh->query("SELECT * FROM invoice_details WHERE invoice_number = '{$data['invoice_number']}' ORDER BY line_number DESC ");
  139. $result['details'] = $q->fetchAll(PDO::FETCH_ASSOC);
  140. }
  141. return $result;
  142. }
  143. private function posInfos ($data)
  144. {
  145. $result = array('items' => array(), 'invoices' => array());
  146. $q = $this->dbh->query("SELECT item FROM invoice_details WHERE project_id = '{$this->project_id}' AND item NOT IN ('000-000','123-456') AND cancelled = 0 ORDER BY item ");
  147. if ($q) {
  148. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  149. $result['items'] = array_column($r, 'item');
  150. }
  151. if ($data['pos_id'] == 0) {
  152. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  153. WHERE a.project_id = '{$this->project_id}' ORDER BY a.invoice_number ");
  154. } else {
  155. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  156. WHERE a.project_id = '{$this->project_id}' AND a.pos_id = '{$data['pos_id']}' ORDER BY a.invoice_number ");
  157. }
  158. if ($q) {
  159. $result['invoices'] = $q->fetchAll(PDO::FETCH_ASSOC);
  160. }
  161. return $result;
  162. }
  163. private function posInfos2 ($data)
  164. {
  165. $result = array('items' => array(), 'invoices' => array());
  166. if ($data['pos_id'] == 0) {
  167. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  168. WHERE a.project_id = '{$this->project_id}' ORDER BY a.invoice_number ");
  169. } else {
  170. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  171. WHERE a.project_id = '{$this->project_id}' AND a.pos_id = '{$data['pos_id']}' ORDER BY a.invoice_number ");
  172. }
  173. if ($q) {
  174. $result['invoices'] = $q->fetchAll(PDO::FETCH_ASSOC);
  175. }
  176. return $result;
  177. }
  178. private function posPrint ($data)
  179. {
  180. $result = $this->posInvoices($data);
  181. $this->dbh->exec("UPDATE invoice_header SET
  182. printed = '1'
  183. WHERE invoice_number = '{$result['invoice_number']}' ");
  184. // print options
  185. $ptr = printer_open("POS-58");
  186. printer_set_option($ptr, PRINTER_MODE, "raw");
  187. printer_write($ptr, $this->project['print_header']);
  188. printer_write($ptr, str_repeat("\r\n", 2));
  189. printer_write($ptr, "Rechn.-Nr. " . $result['invoice_number'] . "\r\n");
  190. printer_write($ptr, "Kasse " . $result['pos_id'] . ", " . $result['cashier'] . "\r\n");
  191. printer_write($ptr, "\r\n");
  192. $result['details'] = array_reverse($result['details']);
  193. foreach ($result['details'] as $entry) {
  194. 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) . "\r\n");
  195. }
  196. printer_write($ptr, "________________________________\r\n");
  197. printer_write($ptr, str_pad(str_replace(".",",",$result['total']), 31, " ", STR_PAD_LEFT) . "\r\n");
  198. printer_write($ptr, $this->project['print_footer']);
  199. printer_write($ptr, str_repeat("\r\n", 7));
  200. printer_close($ptr);
  201. return $result;
  202. }
  203. private function posCancel ($data)
  204. {
  205. $result = "error";
  206. if (isset($data['item']) && $data['item'] != "") {
  207. $item = $data['item'];
  208. $this->dbh->exec("UPDATE invoice_details SET
  209. cancelled = '1'
  210. WHERE item = '{$item}' ");
  211. $result = "OK";
  212. }
  213. return $result;
  214. }
  215. private function projects ($data)
  216. {
  217. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  218. if (in_array($seller['seller_id'], $seller_ids)) {
  219. $this->dbh->exec("UPDATE projects
  220. SET event_date = '{$data['event_date']}',
  221. event_location = '{$data['event_location']}',
  222. print_header = '{$data['print_header']}',
  223. print_footer = '{$data['print_footer']}',
  224. active = '{$data['active']}',
  225. mdate = '{$this->mdate}'
  226. WHERE project_id = '{$data['project_id']}' ");
  227. } else {
  228. $this->dbh->exec("INSERT INTO projects (project_id, event_date, event_location, print_header, print_footer, active, mdate, cdate)
  229. VALUES ( '{$data['project_id']}','{$data['event_date']}', '{$data['event_location']}', '{$data['print_header']}',
  230. '{$data['print_footer']}', '{$data['active']}','{$this->mdate}','{$this->mdate}' )");
  231. }
  232. }
  233. $q = $this->dbh->query("SELECT * FROM projects ORDER BY project_id");
  234. return $q->fetchAll(PDO::FETCH_ASSOC);
  235. }
  236. private function sellers ($data)
  237. {
  238. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  239. $q = $this->dbh->query("SELECT seller_id FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  240. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  241. $seller_ids = array();
  242. if ($r) {
  243. $seller_ids = array_column($r, 'seller_id');
  244. }
  245. foreach ($data['sellers'] as $seller) {
  246. if (in_array($seller['seller_id'], $seller_ids)) {
  247. $this->dbh->exec("UPDATE sellers
  248. SET name = '{$seller['name']}',
  249. first_name = '{$seller['first_name']}',
  250. address = '{$seller['address']}',
  251. zip_code = '{$seller['zip_code']}',
  252. place = '{$seller['place']}',
  253. mail = '{$seller['mail']}',
  254. phone = '{$seller['phone']}',
  255. mdate = '{$this->mdate}'
  256. WHERE project_id = '{$this->project_id}' AND seller_id = '{$seller['seller_id']}' ");
  257. } else {
  258. $this->dbh->exec("INSERT INTO sellers (project_id, seller_id, name, first_name, address, zip_code, place, mail, phone, mdate, cdate)
  259. VALUES ( '{$this->project_id}','{$seller['seller_id']}', '{$seller['name']}', '{$seller['first_name']}',
  260. '{$seller['address']}', '{$seller['zip_code']}', '{$seller['place']}','{$seller['mail']}',
  261. '{$seller['phone']}','{$this->mdate}','{$this->mdate}' )");
  262. }
  263. }
  264. }
  265. $q = $this->dbh->query("SELECT * FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  266. return $q->fetchAll(PDO::FETCH_ASSOC);
  267. }
  268. public function CsvExport ($data)
  269. {
  270. $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
  271. FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  272. $sellers = array();
  273. foreach ($q->fetchAll(PDO::FETCH_ASSOC) as $s) {
  274. $sellers[$s['seller_id']] = $s;
  275. for ($j = 1; $j <= 50; $j++) {
  276. $item_id = substr("00".$j, -3);
  277. $sellers[$s['seller_id']][$item_id] = "n.v.";
  278. }
  279. }
  280. // Verkäufe nachladen,
  281. $q = $this->dbh->query("SELECT * FROM invoice_details WHERE project_id = '{$this->project_id}' AND cancelled = 0");
  282. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  283. if (isset($sellers[$row['seller_id']])) {
  284. $sellers[$row['seller_id']][$row['item_id']] = $row['price'];
  285. } else {
  286. $sellers[] = $row;
  287. }
  288. }
  289. // Summen berechnen
  290. $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 ");
  291. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  292. if (isset($sellers[$row['seller_id']])) {
  293. $sellers[$row['seller_id']]['total'] = $row['price'];
  294. $sellers[$row['seller_id']]['provision'] = $row['price'] * 0.2;
  295. $sellers[$row['seller_id']]['cash_out'] = $row['price'] * 0.8;
  296. } else {
  297. $sellers[] = $row;
  298. }
  299. }
  300. $result = "Verk-Nr;Name;Vorname;Adresse;PLZ;Ort;E-Mail;Telefon;Gesamt;Provision;Auszahlung;A001;A002;A003;A004;A005;A006;A007;A008;A009;A010;" .
  301. "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;" .
  302. "A036;A037;A038;A039;A040;A041;A042;A043;A044;A045;A046;A047;A048;A049;A050\r\n";
  303. $blacklist = array("seller_id", "name", "first_name", "address", "zip_code", "place", "mail", "phone");
  304. foreach ($sellers as $s) {
  305. foreach ($s as $k => $v) {
  306. if (!in_array($k, $blacklist) && $v != "n.v.") {
  307. $s[$k] = @number_format($v, 2, ",", ".");
  308. }
  309. }
  310. $result .= implode(";", $s) . "\r\n";
  311. }
  312. return $result;
  313. }
  314. private function jsonLastErrorMsg ()
  315. {
  316. switch (json_last_error()) {
  317. case JSON_ERROR_NONE:
  318. return ' - No errors';
  319. case JSON_ERROR_DEPTH:
  320. return ' - Maximum stack depth exceeded';
  321. case JSON_ERROR_STATE_MISMATCH:
  322. return ' - Underflow or the modes mismatch';
  323. case JSON_ERROR_CTRL_CHAR:
  324. return ' - Unexpected control character found';
  325. case JSON_ERROR_SYNTAX:
  326. return ' - Syntax error, malformed JSON';
  327. case JSON_ERROR_UTF8:
  328. return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  329. default:
  330. return ' - Unknown error';
  331. }
  332. }
  333. }