You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.1 KiB
PHP

<?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();
}
sendError("Unable to load client list from InvoiceNinja server:\n" . $ex->getMessage());
}
class Clients {
public static function getAll(): array {
$clients = NinjaClient::all();
$list = [];
foreach ($clients as $client) {
$name = $client->name;
if (empty($name)) {
$name = $client->contacts[0]->first_name . " " . $client->contacts[0]->last_name;
}
$list[] = new Client($client->id, $name);
}
return $list;
}
public static function getClient($id): Client {
$client = NinjaClient::find($id);
return new Client($client->id, $client->name);
}
}
} else {
// Use internal client table
class Clients {
public static function getAll(): array {
global $database;
$clients = $database->select("clients", ["clientid", "name"]);
$list = [];
foreach ($clients as $client) {
$list[] = new Client($client['clientid'], $client['name']);
}
return $list;
}
public static function getClient($id): Client {
global $database;
$client = $database->get("clients", ["clientid", "name"], ["clientid" => $id]);
return new Client($client['clientid'], $client['name']);
}
}
}