| 
									
										
										
										
											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"]); | 
					
						
							| 
									
										
										
										
											2019-12-07 19:55:40 -07:00
										 |  |  |         NinjaConfig::setPerPage(9999); // https://github.com/invoiceninja/sdk-php/issues/21
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     } 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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |         public static function getAllAsIDNameArray(): array { | 
					
						
							|  |  |  |             $clients = Clients::getAll(); | 
					
						
							|  |  |  |             $arr = []; | 
					
						
							|  |  |  |             foreach ($clients as $c) { | 
					
						
							|  |  |  |                 $arr[$c->getID()] = $c->getName(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return $arr; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |         public static function getClient($id): Client { | 
					
						
							| 
									
										
										
										
											2020-06-03 01:06:19 -06:00
										 |  |  |             return new Client($id, false); | 
					
						
							| 
									
										
										
										
											2019-10-02 16:33:53 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |