RequestController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. print_r($_GET);
  166. }
  167. private function posInfos ($data)
  168. {
  169. $result = array('items' => array(), 'invoices' => array());
  170. $q = $this->dbh->query("SELECT item FROM invoice_details
  171. WHERE project_id = '{$this->project_id}' AND item NOT IN ('000-000','123-456') AND cancelled = 0
  172. ORDER BY item ");
  173. if ($q) {
  174. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  175. $result['items'] = array_column($r, 'item');
  176. }
  177. if ($data['pos_id'] == 0) {
  178. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  179. WHERE a.project_id = '{$this->project_id}'
  180. ORDER BY a.invoice_number ");
  181. } else {
  182. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  183. WHERE a.project_id = '{$this->project_id}' AND a.pos_id = '{$data['pos_id']}'
  184. ORDER BY a.invoice_number ");
  185. }
  186. if ($q) {
  187. $result['invoices'] = $q->fetchAll(PDO::FETCH_ASSOC);
  188. }
  189. return $result;
  190. }
  191. private function posInfos2 ($data)
  192. {
  193. $result = array('items' => array(), 'invoices' => array());
  194. if ($data['pos_id'] == 0) {
  195. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  196. WHERE a.project_id = '{$this->project_id}'
  197. ORDER BY a.invoice_number ");
  198. } else {
  199. $q = $this->dbh->query("SELECT * FROM `view_invoice_header` a
  200. WHERE a.project_id = '{$this->project_id}' AND a.pos_id = '{$data['pos_id']}'
  201. ORDER BY a.invoice_number ");
  202. }
  203. if ($q) {
  204. $result['invoices'] = $q->fetchAll(PDO::FETCH_ASSOC);
  205. }
  206. return $result;
  207. }
  208. private function posCheckout ($data)
  209. {
  210. $result = $data['invoice'];
  211. $this->dbh->exec("UPDATE invoice_header
  212. SET checkout = '{$result['checkout']}'
  213. WHERE invoice_number = '{$result['invoice_number']}' ");
  214. return $result;
  215. }
  216. private function posPrint ($data)
  217. {
  218. $result = $this->posInvoices($data);
  219. $this->dbh->exec("UPDATE invoice_header
  220. SET printed = '1'
  221. WHERE invoice_number = '{$result['invoice_number']}' ");
  222. // print options
  223. // $ptr = printer_open("POS-58");
  224. // printer_set_option($ptr, PRINTER_MODE, "raw");
  225. $ptr = "/dev/usb/lp0";
  226. printer_write($ptr, $this->project['print_header']);
  227. printer_write($ptr, str_repeat("\n", 1));
  228. printer_write($ptr, "Rechn.-Nr. " . $result['invoice_number']);
  229. printer_write($ptr, "Kasse " . $result['pos_id'] . ", " . $result['cashier']);
  230. $result['details'] = array_reverse($result['details']);
  231. foreach ($result['details'] as $entry) {
  232. 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));
  233. }
  234. printer_write($ptr, "________________________________");
  235. printer_write($ptr, str_pad(str_replace(".",",",$result['total']), 31, " ", STR_PAD_LEFT));
  236. printer_write($ptr, $this->project['print_footer']);
  237. printer_write($ptr, str_repeat("\n", 7));
  238. // printer_close($ptr);
  239. return $result;
  240. }
  241. private function posCancel ($data)
  242. {
  243. $result = "error";
  244. if (isset($data['item']) && $data['item'] != "") {
  245. $item = $data['item'];
  246. $this->dbh->exec("UPDATE invoice_details SET
  247. cancelled = '1'
  248. WHERE item = '{$item}' ");
  249. $result = "OK";
  250. }
  251. return $result;
  252. }
  253. private function projects ($data)
  254. {
  255. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  256. if (in_array($seller['seller_id'], $seller_ids)) {
  257. $this->dbh->exec("UPDATE projects
  258. SET event_date = '{$data['event_date']}',
  259. event_location = '{$data['event_location']}',
  260. print_header = '{$data['print_header']}',
  261. print_footer = '{$data['print_footer']}',
  262. active = '{$data['active']}',
  263. mdate = '{$this->mdate}'
  264. WHERE project_id = '{$data['project_id']}' ");
  265. } else {
  266. $this->dbh->exec("INSERT INTO projects (project_id, event_date, event_location, print_header, print_footer, active, mdate, cdate)
  267. VALUES ( '{$data['project_id']}','{$data['event_date']}', '{$data['event_location']}', '{$data['print_header']}',
  268. '{$data['print_footer']}', '{$data['active']}','{$this->mdate}','{$this->mdate}' )");
  269. }
  270. }
  271. $q = $this->dbh->query("SELECT * FROM projects ORDER BY project_id");
  272. return $q->fetchAll(PDO::FETCH_ASSOC);
  273. }
  274. private function sellers ($data)
  275. {
  276. if (isset($data['sellers']) && is_array($data['sellers']) && count($data['sellers']) > 0) {
  277. $q = $this->dbh->query("SELECT seller_id FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  278. $r = $q->fetchAll(PDO::FETCH_ASSOC);
  279. $seller_ids = array();
  280. if ($r) {
  281. $seller_ids = array_column($r, 'seller_id');
  282. }
  283. foreach ($data['sellers'] as $seller) {
  284. if (in_array($seller['seller_id'], $seller_ids)) {
  285. $this->dbh->exec("UPDATE sellers
  286. SET name = '{$seller['name']}',
  287. first_name = '{$seller['first_name']}',
  288. address = '{$seller['address']}',
  289. zip_code = '{$seller['zip_code']}',
  290. place = '{$seller['place']}',
  291. mail = '{$seller['mail']}',
  292. phone = '{$seller['phone']}',
  293. mdate = '{$this->mdate}'
  294. WHERE project_id = '{$this->project_id}' AND seller_id = '{$seller['seller_id']}' ");
  295. } else {
  296. $this->dbh->exec("INSERT INTO sellers (project_id, seller_id, name, first_name, address, zip_code, place, mail, phone, mdate, cdate)
  297. VALUES ( '{$this->project_id}','{$seller['seller_id']}', '{$seller['name']}', '{$seller['first_name']}',
  298. '{$seller['address']}', '{$seller['zip_code']}', '{$seller['place']}','{$seller['mail']}',
  299. '{$seller['phone']}','{$this->mdate}','{$this->mdate}' )");
  300. }
  301. }
  302. }
  303. $q = $this->dbh->query("SELECT * FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  304. return $q->fetchAll(PDO::FETCH_ASSOC);
  305. }
  306. public function CsvExport ($data)
  307. {
  308. $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
  309. FROM sellers WHERE project_id = '{$this->project_id}' ORDER BY seller_id");
  310. $sellers = array();
  311. foreach ($q->fetchAll(PDO::FETCH_ASSOC) as $s) {
  312. $sellers[$s['seller_id']] = $s;
  313. for ($j = 1; $j <= 50; $j++) {
  314. $item_id = substr("00".$j, -3);
  315. $sellers[$s['seller_id']][$item_id] = "n.v.";
  316. }
  317. }
  318. // Verkäufe nachladen,
  319. $q = $this->dbh->query("SELECT * FROM invoice_details WHERE project_id = '{$this->project_id}' AND cancelled = 0");
  320. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  321. if (isset($sellers[$row['seller_id']])) {
  322. $sellers[$row['seller_id']][$row['item_id']] = $row['price'];
  323. } else {
  324. $sellers[] = $row;
  325. }
  326. }
  327. // Summen berechnen
  328. $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 ");
  329. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  330. if (isset($sellers[$row['seller_id']])) {
  331. $sellers[$row['seller_id']]['total'] = $row['price'];
  332. $sellers[$row['seller_id']]['provision'] = $row['price'] * 0.2;
  333. $sellers[$row['seller_id']]['cash_out'] = $row['price'] * 0.8;
  334. } else {
  335. $sellers[] = $row;
  336. }
  337. }
  338. $result = "Verk-Nr;Name;Vorname;Adresse;PLZ;Ort;E-Mail;Telefon;Gesamt;Provision;Auszahlung;A001;A002;A003;A004;A005;A006;A007;A008;A009;A010;" .
  339. "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;" .
  340. "A036;A037;A038;A039;A040;A041;A042;A043;A044;A045;A046;A047;A048;A049;A050\r\n";
  341. $blacklist = array("seller_id", "name", "first_name", "address", "zip_code", "place", "mail", "phone");
  342. foreach ($sellers as $s) {
  343. foreach ($s as $k => $v) {
  344. if (!in_array($k, $blacklist) && $v != "n.v.") {
  345. $s[$k] = @number_format($v, 2, ",", ".");
  346. }
  347. }
  348. $result .= implode(";", $s) . "\r\n";
  349. }
  350. return $result;
  351. }
  352. private function jsonLastErrorMsg ()
  353. {
  354. switch (json_last_error()) {
  355. case JSON_ERROR_NONE:
  356. return ' - No errors';
  357. case JSON_ERROR_DEPTH:
  358. return ' - Maximum stack depth exceeded';
  359. case JSON_ERROR_STATE_MISMATCH:
  360. return ' - Underflow or the modes mismatch';
  361. case JSON_ERROR_CTRL_CHAR:
  362. return ' - Unexpected control character found';
  363. case JSON_ERROR_SYNTAX:
  364. return ' - Syntax error, malformed JSON';
  365. case JSON_ERROR_UTF8:
  366. return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  367. default:
  368. return ' - Unknown error';
  369. }
  370. }
  371. }