db.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 FROM status_meldung sm
  18. LEFT JOIN status_kommentar sk USING (datum, kunde, start)
  19. WHERE sm.datum = '{$datum}' AND sm.kunde = '{$kunde}' AND sm.start = '{$start}' LIMIT 1");
  20. $result = $q->fetch(PDO::FETCH_ASSOC);
  21. $result['fehlerbericht'] = str_replace("\r\`", "`", str_replace("\r\"", "\"", str_replace("\r\n", "", $result['fehlerbericht'])));
  22. $result['fehlerbericht'] = json_decode($result['fehlerbericht'], true);
  23. }
  24. break;
  25. case 'zeit':
  26. $q = $dbh->exec("UPDATE kunden SET start_soll = '{$_REQUEST['start_soll']}', ende_soll = '{$_REQUEST['ende_soll']}' WHERE kunde = '{$_REQUEST['kunde']}' ");
  27. $error = $dbh->errorInfo();
  28. if ($error[0] != '00000') {
  29. $result = $error;
  30. } else {
  31. $result = $_REQUEST;
  32. }
  33. break;
  34. case 'kunden':
  35. if (count($data) > 0) {
  36. foreach ($data as $kunde) {
  37. if (isset($kunde['kunde'])) {
  38. $q = $dbh->exec("UPDATE kunden SET system = '{$kunde['system']}', start_soll = '{$kunde['start_soll']}', ende_soll = '{$kunde['ende_soll']}',
  39. erster_status = '{$kunde['erster_status']}', aktiv = '{$kunde['aktiv']}', woche = '{$kunde['woche']}',
  40. bundesland = '{$kunde['bundesland']}', plz = '{$kunde['plz']}', ort = '{$kunde['ort']}' WHERE kunde = '{$kunde['kunde']}' ");
  41. }
  42. }
  43. }
  44. $q = $dbh->query("SELECT * FROM kunden");
  45. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  46. break;
  47. case 'config':
  48. $q = $dbh->query("SELECT kunde, datum FROM kunden_config");
  49. $result = array('options' => array());
  50. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  51. $result['options'][$row['kunde']][] = $row['datum'];
  52. }
  53. if (isset($_REQUEST['kunde']) && $_REQUEST['kunde'] != 'undefined') {
  54. if (preg_match('/\d{4}-\d{2}-\d{2}/', $_REQUEST['datum'])) {
  55. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$_REQUEST['kunde']}' AND datum = '{$_REQUEST['datum']}'");
  56. } else {
  57. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$_REQUEST['kunde']}' ORDER BY datum DESC LIMIT 1");
  58. }
  59. if ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  60. $row['info'] = json_decode($row['info'], true);
  61. $result['current'] = $row;
  62. }
  63. }
  64. break;
  65. case 'benutzer':
  66. $q = $dbh->query("SELECT * FROM benutzer");
  67. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  68. break;
  69. case 'kommentar':
  70. $id = (isset($_REQUEST['id'])) ? $_REQUEST['id'] : '';
  71. if (count($data) > 0) {
  72. if ($id == '') {
  73. $now = date('Y-m-d H:m:i');
  74. $q = $dbh->exec("INSERT INTO status_kommentar (kunde, datum, start, benutzer, fehler, status, kommentar, cdate)
  75. VALUES ('{$data['kunde']}','{$data['datum']}','{$data['start']}','{$data['benutzer']}','{$data['fehler']}','{$data['status']}','{$data['kommentar']}','{$now}')");
  76. $id = $dbh->lastInsertId();
  77. $q = $dbh->exec("UPDATE status_meldung SET bearbeitet = '1', kommentar_id = '{$id}'
  78. WHERE kunde = '{$kunde}' AND datum = '{$datum}' AND start = '{$start}' ");
  79. } else {
  80. $q = $dbh->exec("UPDATE status_kommentar SET
  81. benutzer = '{$data['benutzer']}',
  82. fehler = '{$data['fehler']}',
  83. status = '{$data['status']}',
  84. kommentar = '{$data['kommentar']}'
  85. WHERE id = '{$id}' ");
  86. }
  87. }
  88. $q = $dbh->query("SELECT * FROM status_kommentar
  89. WHERE datum > date_add(now(), INTERVAL -32 DAY)
  90. ORDER BY datum DESC, mdate DESC");
  91. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  92. break;
  93. default:
  94. case 'liste':
  95. $datum = (!isset($_REQUEST['datum']) || $_REQUEST['datum'] == '') ? "a.datum > date_add(now(), INTERVAL -32 DAY) AND k.erster_status <= a.datum" : "a.datum = '{$_REQUEST['datum']}' ";
  96. $kunde = (!isset($_REQUEST['kunde']) || $_REQUEST['kunde'] == '') ? "1" : "k.kunde LIKE '%{$_REQUEST['kunde']}%'";
  97. $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,
  98. timediff(sm.ende, k.ende_soll) - timediff(sm.start, k.start_soll) as abweichung,
  99. case
  100. when woche = 'Mo-Fr' then mofr
  101. when woche = 'Mo-Sa' then mosa
  102. when woche = 'Mo-So' then moso
  103. when woche = 'Di-So' then diso
  104. else 1
  105. 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,
  106. if(sm.anzahl is null and now() > concat(a.datum, ' ', k.ende_soll), 1, 0) as fehlend,
  107. ifnull(sk.benutzer, '') as benutzer
  108. FROM arbeitstage a
  109. INNER JOIN kunden k USING (bundesland)
  110. LEFT JOIN status_meldung sm USING (datum, kunde)
  111. LEFT JOIN status_kommentar sk ON sm.kommentar_id = sk.id
  112. WHERE {$datum} AND {$kunde} AND a.datum <= now()
  113. AND k.aktiv = 1
  114. ORDER BY a.datum, k.ende_soll");
  115. $result = $q->fetchAll(PDO::FETCH_ASSOC);
  116. if (isset($_REQUEST['events'])) {
  117. $color = array("green", "#bb0", "#b80", "#d55", "#c33", "#a00", '?' => "#999");
  118. $events = array();
  119. foreach ($result as $e) {
  120. if ($e['anzahl'] != '0') {
  121. $event = array();
  122. $anzahl = (!is_null($e['anzahl'])) ? $e['anzahl'] : "?";
  123. $ende = ($e['ende']) ? $e['ende'] : $e['ende_soll'];
  124. $event['title'] = "{$e['kunde']} ({$anzahl})";
  125. $event['start'] = "{$e['datum']}T{$ende}";
  126. $event['end'] = date("Y-m-d H:i:s", strtotime("+1 hour", strtotime($event['start'])));
  127. $event['color'] = ($anzahl > 4) ? "darkred" : $color[$anzahl];
  128. $events[] = $event;
  129. }
  130. }
  131. $result = $events;
  132. }
  133. }
  134. echo json_encode($result);