db.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. header('Access-Control-Allow-Methods: GET, POST, PUT');
  4. header('Access-Control-Allow-Headers: accept, content-type');
  5. header('Cache-Control: no-cache, must-revalidate');
  6. header('Content-type: application/json');
  7. $dbh = new PDO("mysql:host=localhost;dbname=tasks", "root", "gc01mysql");
  8. $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
  9. $data = json_decode(file_get_contents('php://input'), true);
  10. switch ($_REQUEST['a']) {
  11. case 'fehlerbericht':
  12. $kunde = $_REQUEST['kunde'];
  13. $datum = $_REQUEST['datum'];
  14. $start = $_REQUEST['start'];
  15. if (count($data) > 0) {
  16. } else {
  17. $q = $dbh->query("SELECT sm.*, ifnull(sk.benutzer, '') as benutzer, k.whitelist FROM status_meldung sm
  18. INNER JOIN kunden k USING (kunde)
  19. LEFT JOIN status_kommentar sk USING (datum, kunde, start)
  20. WHERE sm.datum = '{$datum}' AND sm.kunde = '{$kunde}' AND sm.start = '{$start}' LIMIT 1");
  21. $result = $q->fetch(PDO::FETCH_ASSOC);
  22. $result['fehlerbericht'] = str_replace("\r\`", "`", str_replace("\r\"", "\"", str_replace("\r\n", "", $result['fehlerbericht'])));
  23. $result['fehlerbericht'] = json_decode($result['fehlerbericht'], true);
  24. $result['whitelist'] = json_decode(stripslashes($result['whitelist']), true);
  25. }
  26. break;
  27. case 'zeit':
  28. $q = $dbh->exec("UPDATE kunden SET start_soll = '{$_REQUEST['start_soll']}', ende_soll = '{$_REQUEST['ende_soll']}' WHERE kunde = '{$_REQUEST['kunde']}' ");
  29. $error = $dbh->errorInfo();
  30. if ($error[0] != '00000') {
  31. $result = $error;
  32. } else {
  33. $result = $_REQUEST;
  34. }
  35. break;
  36. case 'whitelist':
  37. $q = $dbh->query("SELECT * FROM kunden WHERE kunde = '{$_REQUEST['kunde']}' ");
  38. $result = $q->fetch(PDO::FETCH_ASSOC);
  39. $whitelist = json_decode(stripslashes($result['whitelist']), true);
  40. if (!is_array($whitelist)) {
  41. $whitelist = array();
  42. }
  43. if ($_REQUEST['aktiv'] == "J") {
  44. if (!isset($whitelist[$_REQUEST['typ']])) {
  45. $whitelist[$_REQUEST['typ']] = array($_REQUEST['wert']);
  46. } else if (in_array($_REQUEST['wert'], $whitelist[$_REQUEST['typ']])) {
  47. $result = array('whitelist' => $whitelist);
  48. break;
  49. } else {
  50. $whitelist[$_REQUEST['typ']][] = $_REQUEST['wert'];
  51. }
  52. } else {
  53. if (!isset($whitelist[$_REQUEST['typ']]) || !in_array($_REQUEST['wert'], $whitelist[$_REQUEST['typ']])) break;
  54. $whitelist[$_REQUEST['typ']] = array_diff($whitelist[$_REQUEST['typ']], array($_REQUEST['wert']));
  55. }
  56. $q = $dbh->exec("UPDATE kunden SET whitelist = '" . addslashes(json_encode($whitelist)) . "' WHERE kunde = '{$_REQUEST['kunde']}' ");
  57. $error = $dbh->errorInfo();
  58. if ($error[0] != '00000') {
  59. $result = $error;
  60. } else {
  61. $result = array('whitelist' => $whitelist);
  62. }
  63. break;
  64. case 'kunden':
  65. if (count($data) > 0) {
  66. foreach ($data as $kunde) {
  67. if (isset($kunde['kunde'])) {
  68. $q = $dbh->exec("UPDATE kunden SET system = '{$kunde['system']}', start_soll = '{$kunde['start_soll']}', ende_soll = '{$kunde['ende_soll']}',
  69. erster_status = '{$kunde['erster_status']}', aktiv = '{$kunde['aktiv']}', woche = '{$kunde['woche']}',
  70. bundesland = '{$kunde['bundesland']}', plz = '{$kunde['plz']}', ort = '{$kunde['ort']}' WHERE kunde = '{$kunde['kunde']}' ");
  71. }
  72. }
  73. }
  74. $q = $dbh->query("SELECT * FROM kunden");
  75. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  76. break;
  77. case 'config':
  78. $q = $dbh->query("SELECT kunde, datum FROM kunden_config");
  79. $result = array('options' => array());
  80. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  81. $result['options'][$row['kunde']][] = $row['datum'];
  82. }
  83. if (isset($_REQUEST['kunde']) && $_REQUEST['kunde'] != 'undefined') {
  84. if (preg_match('/\d{4}-\d{2}-\d{2}/', $_REQUEST['datum'])) {
  85. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$_REQUEST['kunde']}' AND datum = '{$_REQUEST['datum']}'");
  86. } else {
  87. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$_REQUEST['kunde']}' ORDER BY datum DESC LIMIT 1");
  88. }
  89. if ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  90. $row['info'] = json_decode($row['info'], true);
  91. $result['current'] = $row;
  92. }
  93. }
  94. break;
  95. case 'benutzer':
  96. $q = $dbh->query("SELECT * FROM benutzer");
  97. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  98. break;
  99. case 'kommentar':
  100. $id = (isset($_REQUEST['id'])) ? $_REQUEST['id'] : '';
  101. if (count($data) > 0) {
  102. if ($id == '') {
  103. $now = date('Y-m-d H:m:i');
  104. $q = $dbh->exec("INSERT INTO status_kommentar (kunde, datum, start, benutzer, fehler, status, kommentar, cdate)
  105. VALUES ('{$data['kunde']}','{$data['datum']}','{$data['start']}','{$data['benutzer']}','{$data['fehler']}','{$data['status']}','{$data['kommentar']}','{$now}')");
  106. $id = $dbh->lastInsertId();
  107. if ($data['status'] == '7' && $data['benutzer2'] != '') {
  108. $q = $dbh->exec("INSERT INTO status_kommentar (kunde, datum, start, benutzer, fehler, status, kommentar, cdate)
  109. VALUES ('{$data['kunde']}','{$data['datum']}','{$data['start']}','{$data['benutzer2']}','{$data['fehler']}','2','','{$now}')");
  110. $id = $dbh->lastInsertId();
  111. }
  112. $q = $dbh->exec("UPDATE status_meldung SET bearbeitet = '1', kommentar_id = '{$id}'
  113. WHERE kunde = '{$data['kunde']}' AND datum = '{$data['datum']}' AND start = '{$data['start']}' ");
  114. } else {
  115. $q = $dbh->exec("UPDATE status_kommentar SET
  116. benutzer = '{$data['benutzer']}',
  117. fehler = '{$data['fehler']}',
  118. status = '{$data['status']}',
  119. kommentar = '{$data['kommentar']}'
  120. WHERE id = '{$id}' ");
  121. }
  122. }
  123. $q = $dbh->query("SELECT * FROM status_kommentar
  124. WHERE datum > date_add(now(), INTERVAL -32 DAY)
  125. ORDER BY datum DESC, mdate DESC");
  126. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  127. break;
  128. default:
  129. case 'liste':
  130. $datum = (!isset($_REQUEST['datum']) || $_REQUEST['datum'] == '') ? "a.datum > date_add(now(), INTERVAL -32 DAY) AND k.erster_status <= a.datum" : "a.datum = '{$_REQUEST['datum']}' ";
  131. $kunde = (!isset($_REQUEST['kunde']) || $_REQUEST['kunde'] == '') ? "1" : "k.kunde LIKE '%{$_REQUEST['kunde']}%'";
  132. $q = $dbh->query("SELECT a.datum, k.kunde, k.system, a.bundesland, a.feiertag, k.start_soll, k.ende_soll, sm.start, sm.ende, sm.anzahl, sm.bearbeitet,
  133. timediff(sm.ende, k.ende_soll) - timediff(sm.start, k.start_soll) as abweichung,
  134. case
  135. when woche = 'Mo-Fr' then mofr
  136. when woche = 'Mo-Sa' then mosa
  137. when woche = 'Mo-So' then moso
  138. when woche = 'Di-So' then diso
  139. else 1
  140. end * if(sk.status <= 1, 0, 1) * if(sm.anzahl > 0 or (sm.anzahl is null and now() > concat(a.datum, ' ', k.ende_soll)), 1, 0) as fehler,
  141. if(sm.anzahl is null and now() > concat(a.datum, ' ', k.ende_soll), 1, 0) as fehlend,
  142. ifnull(sk.benutzer, '') as benutzer
  143. FROM arbeitstage a
  144. INNER JOIN kunden k USING (bundesland)
  145. LEFT JOIN status_meldung sm USING (datum, kunde)
  146. LEFT JOIN status_kommentar sk ON sm.kommentar_id = sk.id
  147. WHERE {$datum} AND {$kunde} AND a.datum <= now()
  148. AND k.aktiv = 1
  149. ORDER BY a.datum, k.ende_soll");
  150. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  151. if (isset($_REQUEST['events'])) {
  152. $color = array("green", "#bb0", "#b80", "#d55", "#c33", "#a00", '?' => "#999");
  153. $events = array();
  154. foreach ($result as $e) {
  155. if ($e['anzahl'] != '0') {
  156. $event = array();
  157. $anzahl = (!is_null($e['anzahl'])) ? $e['anzahl'] : "?";
  158. $ende = ($e['ende']) ? $e['ende'] : $e['ende_soll'];
  159. $event['title'] = "{$e['kunde']} ({$anzahl})";
  160. $event['start'] = "{$e['datum']}T{$ende}";
  161. $event['end'] = date("Y-m-d H:i:s", strtotime("+1 hour", strtotime($event['start'])));
  162. $event['color'] = ($anzahl > 4) ? "darkred" : $color[$anzahl];
  163. $events[] = $event;
  164. }
  165. }
  166. $result = $events;
  167. }
  168. }
  169. echo json_encode($result);