MachineManager/lib/Clients.lib.php

79 lines
2.2 KiB
PHP
Raw Normal View History

2019-09-29 22:38:10 -06:00
<?php
/*
* Copyright 2019 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/.
*/
use InvoiceNinja\Config as NinjaConfig;
use InvoiceNinja\Models\Client as NinjaClient;
if (!empty($SETTINGS["apis"]["invoiceninja"]["token"])) {
// Use InvoiceNinja for clients
try {
NinjaConfig::setURL($SETTINGS["apis"]["invoiceninja"]["url"]);
NinjaConfig::setToken($SETTINGS["apis"]["invoiceninja"]["token"]);
} catch (Exception $ex) {
if ($SETTINGS['debug']) {
echo $ex->getTraceAsString();
}
2019-10-02 16:33:53 -06:00
sendError("Unable to load InvoiceNinja API:\n" . $ex->getMessage());
2019-09-29 22:38:10 -06:00
}
class Clients {
public static function getAll(): array {
2019-10-02 16:33:53 -06:00
try {
$clients = NinjaClient::all();
} catch (Exception $ex) {
if ($SETTINGS['debug']) {
echo $ex->getTraceAsString();
}
sendError("Unable to get InvoiceNinja client list:\n" . $ex->getMessage());
}
2019-09-29 22:38:10 -06:00
$list = [];
foreach ($clients as $client) {
2019-10-02 16:33:53 -06:00
$list[] = new Client($client->id, false, $client->display_name);
2019-09-29 22:38:10 -06:00
}
return $list;
}
public static function getClient($id): Client {
2019-10-02 16:33:53 -06:00
return new Client($client->id, false);
}
public static function areLocal(): bool {
return false;
2019-09-29 22:38:10 -06:00
}
}
} else {
2019-10-02 16:33:53 -06:00
2019-09-29 22:38:10 -06:00
// Use internal client table
class Clients {
public static function getAll(): array {
global $database;
2019-10-02 16:33:53 -06:00
$clients = $database->select("clients", ["clientid"]);
2019-09-29 22:38:10 -06:00
$list = [];
foreach ($clients as $client) {
2019-10-02 16:33:53 -06:00
$list[] = new Client($client['clientid'], true);
2019-09-29 22:38:10 -06:00
}
return $list;
}
public static function getClient($id): Client {
global $database;
2019-10-02 16:33:53 -06:00
return new Client($id, true);
}
public static function areLocal(): bool {
return true;
2019-09-29 22:38:10 -06:00
}
}
}