db.php 13 KB

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