Add APIs to get client info and search clients

master
Skylar Ittner 4 years ago
parent a5662874a5
commit 0a36863a39

@ -0,0 +1,45 @@
<?php
/*
* Copyright 2020 Netsyms Technologies.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* Get list of machines owned by a clientid */
if ($VARS["action"] == "getclient") {
$allclients = Clients::getAllAsIDNameArray();
if (isset($allclients[$VARS["clientid"]]) && !empty($allclients[$VARS["clientid"]])) {
$machinelist = $database->select("machines", ["machineid (id)"], ["AND" => ["clientid" => $VARS["clientid"], "deleted[!]" => true]]);
$machines = [];
foreach ($machinelist as $m) {
$machines[] = (new Machine($m["id"]))->toArrayLite();
}
$client = Clients::getClient($VARS["clientid"])->toArray();
exitWithJson([
"status" => "OK",
"client" => $client,
"machines" => $machines
]);
} else {
sendJsonResp("Client ID not found.", "ERROR");
}
} else if ($VARS["action"] == "getclients") {
$allclients = Clients::getAllAsIDNameArray();
exitWithJson([
"status" => "OK",
"clients" => $allclients
]);
} else if ($VARS["action"] == "searchclients") {
$clients = Clients::search($VARS["q"]);
exitWithJson([
"status" => "OK",
"q" => $VARS["q"],
"count" => count($clients),
"clients" => $clients
]);
}

@ -49,6 +49,23 @@ $APIS = [
"publicnotes (optional)" => "string"
]
],
"getclient" => [
"load" => "client.php",
"vars" => [
"clientid" => "string"
]
],
"getclients" => [
"load" => "client.php",
"vars" => [
]
],
"searchclients" => [
"load" => "client.php",
"vars" => [
"q" => "string"
]
],
"addevent" => [
"load" => "addevent.php",
"vars" => [
@ -62,7 +79,6 @@ $APIS = [
"geteventtypes" => [
"load" => "geteventtypes.php",
"vars" => [
]
]
];

@ -54,6 +54,23 @@ class Client implements JsonSerializable {
}
}
public function toArray() {
$this->fullerize();
$data = [
"id" => $this->getID(),
"name" => $this->getName(),
"phone" => $this->getPhone(),
"email" => $this->getEmail(),
"billingaddress" => $this->getBillingAddress(),
"mailingaddress" => $this->getMailingAddress(),
"publicnotes" => $this->getPublicNotes(),
"privatenotes" => $this->getPrivateNotes()
];
return $data;
}
public function jsonSerialize() {
return [
"id" => $this->id,

@ -41,6 +41,54 @@ if (!empty($SETTINGS["apis"]["invoiceninja"]["token"])) {
return $list;
}
/**
* Search clients for fields containing $query
* @param string $query
* @return \Client Array of Clients
*/
public static function search(string $query) {
try {
$clients = NinjaClient::all();
} catch (Exception $ex) {
if ($SETTINGS['debug']) {
echo $ex->getTraceAsString();
}
sendError("Unable to get InvoiceNinja client list:\n" . $ex->getMessage());
}
$query = strtolower(trim($query));
$results = [];
$fields = ["name", "display_name", "address1", "work_phone", "private_notes", "public_notes", "website", "shipping_address1"];
$contactfields = ["first_name", "last_name", "email", "phone"];
foreach ($clients as $client) {
$match = false;
$clientarr = (array)$client;
foreach ($fields as $f) {
if (strpos(strtolower($clientarr[$f]), $query) !== FALSE) {
$c = new Client($client->id, false, $client->display_name);
$results[] = $c;
$match = true;
continue 2;
}
}
foreach ($client->contacts as $con) {
$conarr = (array)$con;
foreach ($contactfields as $f) {
if (strpos(strtolower($conarr[$f]), $query) !== FALSE) {
$c = new Client($client->id, false, $client->display_name);
$results[] = $c;
$match = true;
continue 3;
}
}
}
}
return $results;
}
public static function getAllAsIDNameArray(): array {
$clients = Clients::getAll();
$arr = [];
@ -75,6 +123,24 @@ if (!empty($SETTINGS["apis"]["invoiceninja"]["token"])) {
return $list;
}
public static function search(string $query) {
$results = $database->select("clients", ["clientid"], ["OR" => [
"name[~]" => $query,
"phone[~]" => $query,
"email[~]" => $query,
"billingaddress[~]" => $query,
"mailingaddress[~]" => $query,
"publicnotes[~]" => $query,
"privatenotes[~]" => $query
]]);
$clients = [];
foreach ($results as $r) {
$clients[] = new Client($r["clientid"], true);
}
return $clients;
}
public static function getClient($id): Client {
global $database;
return new Client($id, true);

@ -36,6 +36,46 @@ class Machine implements JsonSerializable {
}
}
/**
* Get a shorter array without editing data, events, or components.
* @return type
*/
public function toArrayLite() {
global $Strings;
if ($this->exists) {
$info = $this->machine;
// only show deleted if true
if (!$this->isDeleted()) {
unset($info["deleted"]);
}
return [
"status" => "OK",
"id" => $this->machineid,
"icon" => $this->icon,
"type" => [
"id" => $this->typeid,
"label" => $this->typelabel
],
"info" => $info,
"formdata" => [
"labels" => [
"model" => $Strings->get("Model", false),
"condition" => $Strings->get("Condition", false),
"price" => $Strings->get("Price", false),
"os" => $Strings->get("OS/Software", false),
"serial" => $Strings->get("Serial", false),
"manufacturer" => $Strings->get("Manufacturer", false),
"clientid" => $Strings->get("Client", false),
"privatenotes" => $Strings->get("Private Notes", false),
"publicnotes" => $Strings->get("Public Notes", false),
"type" => $Strings->get("Type", false)
]
]
];
}
return [];
}
public function toArray() {
global $Strings;
if ($this->exists) {

Loading…
Cancel
Save