* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file netdiag/api/orders.php * \ingroup netdiag * \brief API-Endpunkt: Aufträge listen (?open=1&q=) oder Auftragsdetail (?id=). * * Auftragsstatus (Dolibarr commande.fk_statut): * -1 = storniert, 0 = Entwurf, 1 = validiert, 2 = in Bearbeitung, * 3 = abgeschlossen. "Aktiv" = Status 0/1/2. */ require_once __DIR__.'/netdiag_api.lib.php'; netdiag_api_bootstrap(); /** @var DoliDB $db */ $user = netdiag_api_authenticate($db); $id = isset($_GET['id']) ? (int) $_GET['id'] : 0; $q = isset($_GET['q']) ? trim((string) $_GET['q']) : ''; $onlyopen = !empty($_GET['open']); $limit = isset($_GET['limit']) ? min(200, max(1, (int) $_GET['limit'])) : 80; $prefix = $db->prefix(); // ---- Auftragsdetail ---- if ($id > 0) { $sql = "SELECT c.rowid, c.ref, c.ref_client, c.date_commande, c.fk_statut, c.note_public,"; $sql .= " s.rowid as socid, s.nom as socname, s.address, s.zip, s.town, s.phone, s.email"; $sql .= " FROM ".$prefix."commande as c"; $sql .= " LEFT JOIN ".$prefix."societe as s ON s.rowid = c.fk_soc"; $sql .= " WHERE c.rowid = ".((int) $id)." AND c.entity IN (".getEntity('commande').")"; $resql = $db->query($sql); if (!$resql || !($obj = $db->fetch_object($resql))) { netdiag_api_error('Auftrag nicht gefunden', 404); } $order = array( 'id' => (int) $obj->rowid, 'ref' => $obj->ref, 'refClient' => $obj->ref_client, 'date' => $db->jdate($obj->date_commande), 'status' => (int) $obj->fk_statut, 'open' => ((int) $obj->fk_statut >= 0 && (int) $obj->fk_statut < 3), 'note' => $obj->note_public, 'customer' => array( 'id' => (int) $obj->socid, 'name' => $obj->socname, 'address' => $obj->address, 'zip' => $obj->zip, 'town' => $obj->town, 'phone' => $obj->phone, 'email' => $obj->email, ), ); $protocols = netdiag_api_protocol_list($db, ' AND p.fk_commande = '.((int) $id)); netdiag_api_respond(array('order' => $order, 'protocols' => $protocols)); } // ---- Auftragsliste ---- $sql = "SELECT c.rowid, c.ref, c.ref_client, c.date_commande, c.fk_statut, c.note_public,"; $sql .= " s.rowid as socid, s.nom as socname, s.address, s.zip, s.town,"; $sql .= " (SELECT COUNT(*) FROM ".$prefix."netdiag_protocol p WHERE p.fk_commande = c.rowid) as protocolcount"; $sql .= " FROM ".$prefix."commande as c"; $sql .= " LEFT JOIN ".$prefix."societe as s ON s.rowid = c.fk_soc"; $sql .= " WHERE c.entity IN (".getEntity('commande').")"; if ($onlyopen) { // Aktive Aufträge: Entwurf / validiert / in Bearbeitung $sql .= " AND c.fk_statut IN (0, 1, 2)"; } if ($q !== '') { $sql .= " AND (".natural_search(array('c.ref', 'c.ref_client', 's.nom', 's.town'), $q, 0, 1).")"; } $sql .= " ORDER BY c.date_commande DESC, c.rowid DESC"; $sql .= $db->plimit($limit, 0); $resql = $db->query($sql); if (!$resql) { netdiag_api_error('Datenbankfehler: '.$db->lasterror(), 500); } $orders = array(); while ($obj = $db->fetch_object($resql)) { $orders[] = array( 'id' => (int) $obj->rowid, 'ref' => $obj->ref, 'refClient' => $obj->ref_client, 'date' => $db->jdate($obj->date_commande), 'status' => (int) $obj->fk_statut, 'open' => ((int) $obj->fk_statut >= 0 && (int) $obj->fk_statut < 3), 'note' => $obj->note_public, 'protocolCount' => (int) $obj->protocolcount, 'customer' => array( 'id' => $obj->socid ? (int) $obj->socid : null, 'name' => $obj->socname, 'address' => $obj->address, 'zip' => $obj->zip, 'town' => $obj->town, ), ); } netdiag_api_respond(array('orders' => $orders));