RequestController.php 12 KB

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