RequestController.php 15 KB

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