db.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $result = array();
  11. switch ($_REQUEST['a']) {
  12. case 'fehlerbericht':
  13. $kunde = $_REQUEST['kunde'];
  14. $datum = $_REQUEST['datum'];
  15. $start = ($_REQUEST['start'] == "null") ? '00:00:00' : $_REQUEST['start'];
  16. if (count($data) > 0) {
  17. } else {
  18. $q = $dbh->query("SELECT sm.*, k.whitelist FROM status_meldung sm
  19. INNER JOIN kunden k USING (kunde)
  20. WHERE sm.datum = '{$datum}' AND sm.kunde = '{$kunde}' AND sm.start = '{$start}' LIMIT 1");
  21. $result = $q->fetch(PDO::FETCH_ASSOC);
  22. if ($result) {
  23. $result['fehlerbericht'] = str_replace("\r\`", "`", str_replace("\r\"", "\"", str_replace("\r\n", "", $result['fehlerbericht'])));
  24. $fehlerbericht = json_decode($result['fehlerbericht'], true);
  25. if ($fehlerbericht) {
  26. $result['fehlerbericht'] = $fehlerbericht;
  27. }
  28. $result['whitelist'] = (isset($result['whitelist'])) ? json_decode(stripslashes($result['whitelist']), true) : null;
  29. } else {
  30. $result = array();
  31. }
  32. if (isset($result['kommentar_id']) && $result['kommentar_id'] > 0) {
  33. $q = $dbh->query("SELECT sk.* FROM status_kommentar sk
  34. WHERE sk.id = '{$result['kommentar_id']}' ");
  35. $comment = $q->fetch(PDO::FETCH_ASSOC);
  36. } else {
  37. $q = $dbh->query("SELECT sk.* FROM status_kommentar sk
  38. WHERE sk.datum = '{$datum}' AND sk.kunde = '{$kunde}' AND sk.start = '{$start}'
  39. ORDER BY cdate DESC LIMIT 1");
  40. $comment = $q->fetch(PDO::FETCH_ASSOC);
  41. }
  42. $result['kommentar'] = ($comment) ? $comment : array();
  43. }
  44. break;
  45. case 'zeit':
  46. $q = $dbh->exec("UPDATE kunden SET start_soll = '{$_REQUEST['start_soll']}', ende_soll = '{$_REQUEST['ende_soll']}' WHERE kunde = '{$_REQUEST['kunde']}' ");
  47. $error = $dbh->errorInfo();
  48. if ($error[0] != '00000') {
  49. $result = $error;
  50. } else {
  51. $result = $_REQUEST;
  52. }
  53. break;
  54. case 'whitelist':
  55. $q = $dbh->query("SELECT * FROM kunden WHERE kunde = '{$_REQUEST['kunde']}' ");
  56. $result = $q->fetch(PDO::FETCH_ASSOC);
  57. $whitelist = json_decode(stripslashes($result['whitelist']), true);
  58. if (!is_array($whitelist)) {
  59. $whitelist = array();
  60. }
  61. if ($_REQUEST['aktiv'] == "J") {
  62. if (!isset($whitelist[$_REQUEST['typ']])) {
  63. $whitelist[$_REQUEST['typ']] = array($_REQUEST['wert']);
  64. } else if (in_array($_REQUEST['wert'], $whitelist[$_REQUEST['typ']])) {
  65. $result = array('whitelist' => $whitelist);
  66. break;
  67. } else {
  68. $whitelist[$_REQUEST['typ']][] = $_REQUEST['wert'];
  69. }
  70. } else {
  71. if (!isset($whitelist[$_REQUEST['typ']]) || !in_array($_REQUEST['wert'], $whitelist[$_REQUEST['typ']])) break;
  72. $whitelist[$_REQUEST['typ']] = array_diff($whitelist[$_REQUEST['typ']], array($_REQUEST['wert']));
  73. }
  74. $q = $dbh->exec("UPDATE kunden SET whitelist = '" . addslashes(json_encode($whitelist)) . "' WHERE kunde = '{$_REQUEST['kunde']}' ");
  75. $error = $dbh->errorInfo();
  76. if ($error[0] != '00000') {
  77. $result = $error;
  78. } else {
  79. $result = array('whitelist' => $whitelist);
  80. }
  81. break;
  82. case 'changelog':
  83. $kunde = "Entenmann";
  84. $q = $dbh->query("SELECT * FROM kunden_gcstarter WHERE kunde = '{$kunde}' ORDER BY datum DESC LIMIT 1");
  85. $result = $q->fetch(PDO::FETCH_ASSOC);
  86. $r = json_decode($result['gcstarter'], true);
  87. $byDate = $r['Kontenzuordnung GAPS.csv.log'];
  88. $states = array("neu", "akt", "entf");
  89. $list = array();
  90. foreach($byDate as $entry) {
  91. foreach ($states as $state) {
  92. foreach($entry[$state] as $account) {
  93. $account['Datum'] = $entry['datum'];
  94. $account['Status'] = $state;
  95. $list[] = $account;
  96. }
  97. }
  98. }
  99. $q = $dbh->query("SELECT * FROM kunden_gcstruct WHERE kunde = '{$kunde}' ORDER BY datum DESC LIMIT 1");
  100. $result = $q->fetch(PDO::FETCH_ASSOC);
  101. $struct = json_decode($result['gcstruct'], true);
  102. $result = array('Historie' => $byDate, 'Liste' => $list, 'GCStruct' => $struct);
  103. break;
  104. case 'kunden':
  105. if (count($data) > 0) {
  106. foreach ($data as $kunde) {
  107. if (isset($kunde['kunde'])) {
  108. $q = $dbh->exec("UPDATE kunden SET system = '{$kunde['system']}', start_soll = '{$kunde['start_soll']}', ende_soll = '{$kunde['ende_soll']}',
  109. erster_status = '{$kunde['erster_status']}', aktiv = '{$kunde['aktiv']}', woche = '{$kunde['woche']}',
  110. bundesland = '{$kunde['bundesland']}', plz = '{$kunde['plz']}', ort = '{$kunde['ort']}' WHERE kunde = '{$kunde['kunde']}' ");
  111. }
  112. }
  113. }
  114. $q = $dbh->query("SELECT * FROM kunden");
  115. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  116. foreach ($result as $i => $r) {
  117. $result[$i]['whitelist'] = json_decode(stripslashes($r['whitelist']), true);
  118. $result[$i]['ort'] = utf8_decode($r['ort']);
  119. }
  120. break;
  121. case 'tickets':
  122. if (count($data) > 0) {
  123. if ($data['id'] == '') {
  124. $q = $dbh->exec("INSERT INTO tickets (datum, benutzer, kunde, kontakt, beschreibung, termin, dauer, status, prioritaet, fortschritt, kategorie, programm, kommentar)
  125. VALUES ('{$data['datum']}', '{$data['benutzer']}', '{$data['kunde']}', '{$data['kontakt']}', '{$data['beschreibung']}', '{$data['termin']}', '{$data['dauer']}',
  126. '{$data['status']}', '{$data['prioritaet']}', '{$data['fortschritt']}', '{$data['kategorie']}', '{$data['programm']}', '{$data['kommentar']}')");
  127. } else {
  128. $q = $dbh->exec("UPDATE tickets SET datum = '{$data['datum']}', benutzer = '{$data['benutzer']}', kunde = '{$data['kunde']}', kontakt = '{$data['kontakt']}',
  129. beschreibung = '{$data['beschreibung']}', termin = '{$data['termin']}', dauer = '{$data['dauer']}', status = '{$data['status']}',
  130. prioritaet = '{$data['prioritaet']}', fortschritt = '{$data['fortschritt']}', kategorie = '{$data['kategorie']}', programm = '{$data['programm']}',
  131. kommentar = '{$data['kommentar']}'
  132. WHERE id = '{$data['id']}' ");
  133. }
  134. }
  135. $q = $dbh->query("SELECT * FROM tickets");
  136. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  137. break;
  138. case 'config':
  139. $q = $dbh->query("SELECT kunde, datum FROM kunden_config ORDER BY 1, 2 DESC");
  140. $result = array('options' => array());
  141. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  142. $result['options'][$row['kunde']][] = $row['datum'];
  143. }
  144. if (isset($_REQUEST['kunde']) && $_REQUEST['kunde'] != 'undefined') {
  145. if (preg_match('/\d{4}-\d{2}-\d{2}/', $_REQUEST['datum'])) {
  146. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$_REQUEST['kunde']}' AND datum = '{$_REQUEST['datum']}'");
  147. } else {
  148. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$_REQUEST['kunde']}' ORDER BY datum DESC LIMIT 1");
  149. }
  150. if ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  151. $row['info'] = json_decode($row['info'], true);
  152. $result['current'] = $row;
  153. }
  154. }
  155. break;
  156. case 'benutzer':
  157. $q = $dbh->query("SELECT * FROM benutzer");
  158. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  159. break;
  160. case 'kommentar':
  161. $id = (isset($_REQUEST['id']) && $_REQUEST['id'] != "undefined") ? $_REQUEST['id'] : '';
  162. if (count($data) > 0) {
  163. if ($id == '') {
  164. $now = date('Y-m-d H:m:i');
  165. $q = $dbh->exec("INSERT INTO status_kommentar (kunde, datum, start, benutzer, fehler, status, kommentar, cdate)
  166. VALUES ('{$data['kunde']}','{$data['datum']}','{$data['start']}','{$data['benutzer']}','{$data['fehler']}','{$data['status']}','{$data['kommentar']}','{$now}')");
  167. $id = $dbh->lastInsertId();
  168. if ($data['status'] == '7' && $data['benutzer2'] != '') {
  169. $q = $dbh->exec("INSERT INTO status_kommentar (kunde, datum, start, benutzer, fehler, status, kommentar, cdate)
  170. VALUES ('{$data['kunde']}','{$data['datum']}','{$data['start']}','{$data['benutzer2']}','{$data['fehler']}','2','','{$now}')");
  171. $id = $dbh->lastInsertId();
  172. }
  173. $q = $dbh->exec("UPDATE status_meldung SET bearbeitet = '1', kommentar_id = '{$id}'
  174. WHERE kunde = '{$data['kunde']}' AND datum = '{$data['datum']}' AND start = '{$data['start']}' ");
  175. } else {
  176. $q = $dbh->exec("UPDATE status_kommentar SET
  177. benutzer = '{$data['benutzer']}',
  178. fehler = '{$data['fehler']}',
  179. status = '{$data['status']}',
  180. kommentar = '{$data['kommentar']}'
  181. WHERE id = '{$id}' ");
  182. }
  183. }
  184. $q = $dbh->query("SELECT * FROM status_kommentar
  185. WHERE datum > date_add(now(), INTERVAL -32 DAY)
  186. ORDER BY datum DESC, mdate DESC");
  187. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  188. break;
  189. default:
  190. case 'liste':
  191. $datum = (!isset($_REQUEST['datum']) || $_REQUEST['datum'] == '') ? "a.datum > date_add(now(), INTERVAL -32 DAY)" : "a.datum >= '{$_REQUEST['datum']}' ";
  192. $datum_bis = (!isset($_REQUEST['datum_bis']) || $_REQUEST['datum_bis'] == '') ? "a.datum > date_add(now(), INTERVAL -32 DAY)" : "a.datum <= '{$_REQUEST['datum_bis']}' ";
  193. $kunde = (!isset($_REQUEST['kunde']) || $_REQUEST['kunde'] == '') ? "1" : "k.kunde LIKE '%{$_REQUEST['kunde']}%'";
  194. $q = $dbh->query("SELECT DISTINCT a.datum, k.kunde, sm.aufgabe, k.system, a.bundesland, a.feiertag, k.start_soll, k.ende_soll, sm.start, sm.ende, sm.anzahl, if(sk2.id IS NULL, sm.bearbeitet, '1') as bearbeitet,
  195. case
  196. when woche = 'Mo-Fr' then mofr
  197. when woche = 'Mo-Sa' then mosa
  198. when woche = 'Mo-So' then moso
  199. when woche = 'Di-So' then diso
  200. else 1
  201. end * if(sk.status <= 1, 0, 1) * if(sm.anzahl > 0 or (sm.anzahl is null and now() > date_add(concat(a.datum, ' ', k.ende_soll), INTERVAL 1 HOUR)), 1, 0) as fehler,
  202. if(sm.anzahl is null and now() > date_add(concat(a.datum, ' ', k.ende_soll), INTERVAL 1 HOUR), 1, 0) as fehlend,
  203. ifnull(sk.benutzer, '') as benutzer
  204. FROM arbeitstage a
  205. INNER JOIN kunden k USING (bundesland)
  206. LEFT JOIN status_meldung sm USING (datum, kunde)
  207. LEFT JOIN status_kommentar sk ON sm.kommentar_id = sk.id
  208. LEFT JOIN status_kommentar as sk2 ON a.datum = sk2.datum AND k.kunde = sk2.kunde
  209. WHERE {$datum} AND {$datum_bis} AND {$kunde} AND a.datum <= now() AND k.erster_status <= a.datum
  210. AND k.aktiv = 1
  211. ORDER BY a.datum, k.ende_soll");
  212. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  213. if (isset($_REQUEST['events'])) {
  214. $color = array("green", "#bb0", "#b80", "#d55", "#c33", "#a00", '?' => "#999");
  215. $events = array();
  216. foreach ($result as $e) {
  217. if ($e['anzahl'] != '0') {
  218. $event = array();
  219. $anzahl = (!is_null($e['anzahl'])) ? $e['anzahl'] : "?";
  220. $ende = ($e['ende']) ? $e['ende'] : $e['ende_soll'];
  221. $event['title'] = "{$e['kunde']} ({$anzahl})";
  222. $event['start'] = "{$e['datum']}T{$ende}";
  223. $event['end'] = date("Y-m-d H:i:s", strtotime("+1 hour", strtotime($event['start'])));
  224. $event['color'] = ($anzahl > 4) ? "darkred" : $color[$anzahl];
  225. $events[] = $event;
  226. }
  227. }
  228. $result = $events;
  229. }
  230. }
  231. echo json_encode($result);
  232. // echo json_last_error();