diff --git a/api/actions/client.php b/api/actions/client.php new file mode 100644 index 0000000..f0e0004 --- /dev/null +++ b/api/actions/client.php @@ -0,0 +1,45 @@ +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 + ]); +} \ No newline at end of file diff --git a/api/apisettings.php b/api/apisettings.php index 3146b0b..fbe0d1a 100644 --- a/api/apisettings.php +++ b/api/apisettings.php @@ -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" => [ - ] ] ]; diff --git a/lib/Client.lib.php b/lib/Client.lib.php index 3e552e7..8c4f75a 100644 --- a/lib/Client.lib.php +++ b/lib/Client.lib.php @@ -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, diff --git a/lib/Clients.lib.php b/lib/Clients.lib.php index c8c8dee..a24d42f 100644 --- a/lib/Clients.lib.php +++ b/lib/Clients.lib.php @@ -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); diff --git a/lib/Machine.lib.php b/lib/Machine.lib.php index 918d250..e4ca950 100644 --- a/lib/Machine.lib.php +++ b/lib/Machine.lib.php @@ -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) {