dolibarr.netdiag/api/customers.php
Eduard Wisch c576726a26
Some checks are pending
Deploy netdiag / deploy (push) Waiting to run
Initiales Commit — Dolibarr-Modul NetDiag [deploy]
Netzwerk-Diagnose-Modul mit JSON-API für die NetDiag-App:
- 3 Tabellen (protocol/device/measurement), generisches JSON-result
- JSON-API: auth, customers, orders, protocols (idempotenter Sync), pdf
- JWT-Auth (HS256), CORS für die Capacitor-App
- Tabs an Thirdparty + Auftrag, Protokoll-Card, PDF-Generator
- QR-Code zum App-Download in der Modul-Konfiguration
- de_DE + en_US, Rechtesystem netdiag->protocol read/write/delete

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 12:12:11 +02:00

119 lines
3.8 KiB
PHP

<?php
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
*
* 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 <https://www.gnu.org/licenses/>.
*/
/**
* \file netdiag/api/customers.php
* \ingroup netdiag
* \brief API-Endpunkt: Kunden suchen (?q=) oder Kundendetail (?id=).
*/
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']) : '';
$limit = isset($_GET['limit']) ? min(200, max(1, (int) $_GET['limit'])) : 50;
$prefix = $db->prefix();
// ---- Kundendetail ----
if ($id > 0) {
$sql = "SELECT s.rowid, s.nom, s.code_client, s.address, s.zip, s.town, s.phone, s.email, s.tva_intra";
$sql .= " FROM ".$prefix."societe as s";
$sql .= " WHERE s.rowid = ".((int) $id);
$sql .= " AND s.entity IN (".getEntity('societe').")";
$resql = $db->query($sql);
if (!$resql || !($obj = $db->fetch_object($resql))) {
netdiag_api_error('Kunde nicht gefunden', 404);
}
$customer = array(
'id' => (int) $obj->rowid,
'name' => $obj->nom,
'code' => $obj->code_client,
'address' => $obj->address,
'zip' => $obj->zip,
'town' => $obj->town,
'phone' => $obj->phone,
'email' => $obj->email,
'vat' => $obj->tva_intra,
);
// Aufträge des Kunden
$orders = array();
$sqlo = "SELECT rowid, ref, ref_client, date_commande, fk_statut FROM ".$prefix."commande";
$sqlo .= " WHERE fk_soc = ".((int) $id)." AND entity IN (".getEntity('commande').")";
$sqlo .= " ORDER BY date_commande DESC, rowid DESC";
$reso = $db->query($sqlo);
if ($reso) {
while ($o = $db->fetch_object($reso)) {
$orders[] = array(
'id' => (int) $o->rowid,
'ref' => $o->ref,
'refClient' => $o->ref_client,
'date' => $db->jdate($o->date_commande),
'status' => (int) $o->fk_statut,
'open' => ((int) $o->fk_statut >= 0 && (int) $o->fk_statut < 3),
);
}
}
// Diagnose-Protokolle des Kunden
$protocols = netdiag_api_protocol_list($db, ' AND p.fk_soc = '.((int) $id));
netdiag_api_respond(array(
'customer' => $customer,
'orders' => $orders,
'protocols' => $protocols,
));
}
// ---- Kundensuche ----
$sql = "SELECT s.rowid, s.nom, s.code_client, s.zip, s.town, s.phone,";
$sql .= " (SELECT COUNT(*) FROM ".$prefix."netdiag_protocol p WHERE p.fk_soc = s.rowid) as protocolcount";
$sql .= " FROM ".$prefix."societe as s";
$sql .= " WHERE s.entity IN (".getEntity('societe').")";
$sql .= " AND s.client IN (1, 2, 3)";
$sql .= " AND s.status = 1";
if ($q !== '') {
$sql .= " AND (".natural_search(array('s.nom', 's.name_alias', 's.code_client', 's.town', 's.zip'), $q, 0, 1).")";
}
$sql .= " ORDER BY s.nom ASC";
$sql .= $db->plimit($limit, 0);
$resql = $db->query($sql);
if (!$resql) {
netdiag_api_error('Datenbankfehler: '.$db->lasterror(), 500);
}
$customers = array();
while ($obj = $db->fetch_object($resql)) {
$customers[] = array(
'id' => (int) $obj->rowid,
'name' => $obj->nom,
'code' => $obj->code_client,
'zip' => $obj->zip,
'town' => $obj->town,
'phone' => $obj->phone,
'protocolCount' => (int) $obj->protocolcount,
);
}
netdiag_api_respond(array('customers' => $customers));