| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* 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/. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 21:14:38 -06:00
										 |  |  | class Machine implements JsonSerializable { | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private $machineid = ""; | 
					
						
							| 
									
										
										
										
											2020-06-26 19:49:57 -06:00
										 |  |  |     private $icon = "fas fa-desktop"; | 
					
						
							|  |  |  |     private $typeid = 14; | 
					
						
							|  |  |  |     private $typelabel = "Machine"; | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     private $machine = []; | 
					
						
							|  |  |  |     private $events = []; | 
					
						
							|  |  |  |     private $components = []; | 
					
						
							|  |  |  |     private $exists = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct($machineid) { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         $this->machineid = $machineid; | 
					
						
							|  |  |  |         if (Machine::exists($machineid)) { | 
					
						
							|  |  |  |             $this->exists = true; | 
					
						
							| 
									
										
										
										
											2020-06-27 19:06:25 -06:00
										 |  |  |             $this->machine = $database->get('machines', ['type [Int]', 'model', 'condition [Number]', 'price [Number]', 'os', 'serial', 'manufacturer', 'clientid [Int]', 'privatenotes', 'publicnotes', 'deleted [Bool]'], ['machineid' => $machineid]); | 
					
						
							| 
									
										
										
										
											2020-06-26 20:28:37 -06:00
										 |  |  |             $typeinfo = $database->get("machine_types", ["machinetypeid (id) [Int]", "typename (label)", "icon"], ["machinetypeid" => $this->machine["type"]]); | 
					
						
							| 
									
										
										
										
											2020-06-26 19:49:57 -06:00
										 |  |  |             $this->icon = $typeinfo["icon"]; | 
					
						
							|  |  |  |             $this->typeid = $typeinfo["id"]; | 
					
						
							|  |  |  |             $this->typelabel = $typeinfo["label"]; | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |             $events = $database->select('events', 'historyid', ['machineid' => $machineid, "ORDER" => ["date" => "DESC"]]); | 
					
						
							|  |  |  |             foreach ($events as $e) { | 
					
						
							|  |  |  |                 $this->events[] = new Event($e); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |             $components = $database->select("components", "compid", ["machineid" => $machineid]); | 
					
						
							|  |  |  |             foreach ($components as $c) { | 
					
						
							|  |  |  |                 $this->components[] = new Component($c); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 19:21:49 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 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 []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |     public function toArray() { | 
					
						
							|  |  |  |         global $Strings; | 
					
						
							| 
									
										
										
										
											2020-05-14 21:14:38 -06:00
										 |  |  |         if ($this->exists) { | 
					
						
							| 
									
										
										
										
											2020-07-19 19:34:38 -06:00
										 |  |  |             $info = $this->machine; | 
					
						
							|  |  |  |             // only show deleted if true
 | 
					
						
							|  |  |  |             if (!$this->isDeleted()) { | 
					
						
							|  |  |  |                 unset($info["deleted"]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $clientinfo = []; | 
					
						
							|  |  |  |             if (!empty($this->getClientID())) { | 
					
						
							|  |  |  |                 $client = Clients::getClient($this->getClientID()); | 
					
						
							|  |  |  |                 $clientinfo = [ | 
					
						
							|  |  |  |                     "name" => $client->getName(), | 
					
						
							|  |  |  |                     "phone" => $client->getPhone(), | 
					
						
							|  |  |  |                     "billingaddress" => $client->getBillingAddress(), | 
					
						
							|  |  |  |                     "mailingaddress" => $client->getMailingAddress() | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-14 21:14:38 -06:00
										 |  |  |             return [ | 
					
						
							|  |  |  |                 "status" => "OK", | 
					
						
							|  |  |  |                 "id" => $this->machineid, | 
					
						
							| 
									
										
										
										
											2020-06-26 19:49:57 -06:00
										 |  |  |                 "icon" => $this->icon, | 
					
						
							|  |  |  |                 "type" => [ | 
					
						
							|  |  |  |                     "id" => $this->typeid, | 
					
						
							|  |  |  |                     "label" => $this->typelabel | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2020-07-19 19:34:38 -06:00
										 |  |  |                 "clientinfo" => $clientinfo, | 
					
						
							|  |  |  |                 "info" => $info, | 
					
						
							| 
									
										
										
										
											2020-05-14 21:14:38 -06:00
										 |  |  |                 "events" => $this->events, | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |                 "components" => $this->components, | 
					
						
							|  |  |  |                 "formdata" => [ | 
					
						
							|  |  |  |                     "options" => [ | 
					
						
							| 
									
										
										
										
											2020-08-27 17:32:46 -06:00
										 |  |  |                         "clientid" => ["" => ""] + Clients::getAllAsIDNameArray(), | 
					
						
							| 
									
										
										
										
											2020-07-19 19:04:51 -06:00
										 |  |  |                         "type" => Machine::getTypeList() | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |                     ], | 
					
						
							|  |  |  |                     "inputtypes" => [ | 
					
						
							|  |  |  |                         "model" => "text", | 
					
						
							|  |  |  |                         "condition" => "number", | 
					
						
							|  |  |  |                         "price" => "number", | 
					
						
							|  |  |  |                         "os" => "text", | 
					
						
							|  |  |  |                         "serial" => "text", | 
					
						
							|  |  |  |                         "manufacturer" => "text", | 
					
						
							|  |  |  |                         "clientid" => "select", | 
					
						
							|  |  |  |                         "privatenotes" => "textarea", | 
					
						
							| 
									
										
										
										
											2020-07-19 19:04:51 -06:00
										 |  |  |                         "publicnotes" => "textarea", | 
					
						
							| 
									
										
										
										
											2020-07-19 19:34:38 -06:00
										 |  |  |                         "type" => "select" | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |                     ], | 
					
						
							|  |  |  |                     "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), | 
					
						
							| 
									
										
										
										
											2020-07-19 19:04:51 -06:00
										 |  |  |                         "publicnotes" => $Strings->get("Public Notes", false), | 
					
						
							|  |  |  |                         "type" => $Strings->get("Type", false) | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |                     ], | 
					
						
							| 
									
										
										
										
											2020-09-03 20:22:01 -06:00
										 |  |  |                     "icons" => [ | 
					
						
							|  |  |  |                         "model" => "fas fa-hashtag", | 
					
						
							|  |  |  |                         "condition" => "fas fa-star-half-alt", | 
					
						
							|  |  |  |                         "price" => "fas fa-money-bill-wave", | 
					
						
							|  |  |  |                         "os" => "fas fa-hdd", | 
					
						
							|  |  |  |                         "serial" => "fas fa-barcode", | 
					
						
							|  |  |  |                         "manufacturer" => "fas fa-industry", | 
					
						
							|  |  |  |                         "privatenotes" => "fas fa-comment-dots", | 
					
						
							|  |  |  |                         "publicnotes" => "far fa-comment-dots" | 
					
						
							|  |  |  |                     ] | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2020-05-14 21:14:38 -06:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |     public function jsonSerialize() { | 
					
						
							|  |  |  |         return $this->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     public static function create(): Machine { | 
					
						
							|  |  |  |         return new Machine(Machine::generateId()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function exists($id): bool { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         return $database->has('machines', ['machineid' => $id]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 14:08:53 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if a given serial number can identify exactly one machine. | 
					
						
							|  |  |  |      * @global $database $database | 
					
						
							|  |  |  |      * @param type $serial | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function serialExists(string $serial): bool { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         return $database->has('machines', ['serial' => $serial]) && $database->count('machines', ['serial' => $serial]) == 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Convert a serial into a machine ID. | 
					
						
							|  |  |  |      * @global $database $database | 
					
						
							|  |  |  |      * @param string $serial | 
					
						
							|  |  |  |      * @return string|bool machine ID if found, otherwise false. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getIDFromSerial(string $serial) { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         if (Machine::serialExists($serial)) { | 
					
						
							|  |  |  |             return $database->get('machines', 'machineid', ['serial' => $serial]) . ""; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-19 19:04:51 -06:00
										 |  |  |     public static function getTypeList(): array { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         $typelist = []; | 
					
						
							|  |  |  |         foreach ($database->select("machine_types", ["machinetypeid (id)", "typename (name)"]) as $t) { | 
					
						
							|  |  |  |             $typelist[$t["id"]] = $t["name"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $typelist; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function getTypeListWithIcons(): array { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         $typelist = []; | 
					
						
							|  |  |  |         foreach ($database->select("machine_types", ["machinetypeid (id)", "typename (name)", "icon"]) as $t) { | 
					
						
							|  |  |  |             $typelist[$t["id"]] = [ | 
					
						
							|  |  |  |                 "name" => $t["name"], | 
					
						
							|  |  |  |                 "icon" => $t["icon"] | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $typelist; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     public function save() { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         if ($this->exists) { | 
					
						
							|  |  |  |             $database->update("machines", $this->machine, ["machineid" => $this->machineid]); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $data = $this->machine; | 
					
						
							|  |  |  |             $data["machineid"] = $this->machineid; | 
					
						
							|  |  |  |             $database->insert("machines", $data); | 
					
						
							|  |  |  |             $this->exists = true; | 
					
						
							| 
									
										
										
										
											2020-06-27 19:06:25 -06:00
										 |  |  |             // Insert event for machine creation
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:29:37 -06:00
										 |  |  |             Event::create($data["machineid"], date("Y-m-d H:i:s"), "Device ID Generated"); | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getID(): string { | 
					
						
							|  |  |  |         return $this->machineid . ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getClientID() { | 
					
						
							|  |  |  |         if (!empty($this->machine["clientid"])) { | 
					
						
							|  |  |  |             return $this->machine["clientid"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 19:49:57 -06:00
										 |  |  |     public function getType(): int { | 
					
						
							|  |  |  |         return $this->typeid; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getTypeLabel(): string { | 
					
						
							|  |  |  |         return $this->typelabel; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getIcon(): string { | 
					
						
							|  |  |  |         return $this->icon; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     public function getModel(): string { | 
					
						
							|  |  |  |         if (!empty($this->machine["model"])) { | 
					
						
							|  |  |  |             return $this->machine["model"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getCondition(): float { | 
					
						
							|  |  |  |         if (!empty($this->machine["condition"])) { | 
					
						
							|  |  |  |             return $this->machine["condition"] * 1.0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0.0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getPrice(): float { | 
					
						
							|  |  |  |         if (!empty($this->machine["price"])) { | 
					
						
							|  |  |  |             return $this->machine["price"] * 1.0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0.0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getOS(): string { | 
					
						
							|  |  |  |         if (!empty($this->machine["os"])) { | 
					
						
							|  |  |  |             return $this->machine["os"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getSerial(): string { | 
					
						
							|  |  |  |         if (!empty($this->machine["serial"])) { | 
					
						
							|  |  |  |             return $this->machine["serial"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getManufacturer(): string { | 
					
						
							|  |  |  |         if (!empty($this->machine["manufacturer"])) { | 
					
						
							|  |  |  |             return $this->machine["manufacturer"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getPublicNotes(): string { | 
					
						
							|  |  |  |         if (!empty($this->machine["publicnotes"])) { | 
					
						
							|  |  |  |             return $this->machine["publicnotes"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getPrivateNotes(): string { | 
					
						
							|  |  |  |         if (!empty($this->machine["privatenotes"])) { | 
					
						
							|  |  |  |             return $this->machine["privatenotes"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getEvents() { | 
					
						
							|  |  |  |         return $this->events; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setClientID($id) { | 
					
						
							|  |  |  |         $this->machine["clientid"] = $id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 19:49:57 -06:00
										 |  |  |     public function setType(int $typeid) { | 
					
						
							|  |  |  |         global $database; | 
					
						
							|  |  |  |         $this->typeid = $typeid; | 
					
						
							|  |  |  |         $this->machine["type"] = $typeid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $typeinfo = $database->get("machine_types", ["machinetypeid (id)", "typename (label)", "icon"], ["machinetypeid" => $typeid]); | 
					
						
							|  |  |  |         $this->icon = $typeinfo["icon"]; | 
					
						
							|  |  |  |         $this->typelabel = $typeinfo["label"]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     public function setModel(string $model) { | 
					
						
							|  |  |  |         $this->machine["model"] = $model; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setCondition(float $condition) { | 
					
						
							|  |  |  |         if ($condition < 0) { | 
					
						
							|  |  |  |             $condition = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($condition > 10) { | 
					
						
							|  |  |  |             $condition = 10; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $condition = round($condition); | 
					
						
							|  |  |  |         $this->machine["condition"] = $condition; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setPrice(float $price) { | 
					
						
							|  |  |  |         $this->machine["price"] = $price; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setOS(string $os) { | 
					
						
							|  |  |  |         $this->machine["os"] = $os; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setSerial(string $serial) { | 
					
						
							|  |  |  |         $this->machine["serial"] = $serial; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setManufacturer(string $manufacturer) { | 
					
						
							|  |  |  |         $this->machine["manufacturer"] = $manufacturer; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setPrivateNotes(string $notes) { | 
					
						
							|  |  |  |         $this->machine["privatenotes"] = $notes; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setPublicNotes(string $notes) { | 
					
						
							|  |  |  |         $this->machine["publicnotes"] = $notes; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getComponents(): array { | 
					
						
							|  |  |  |         return $this->components; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 19:06:25 -06:00
										 |  |  |     public function setDeleted(bool $deleted = true) { | 
					
						
							|  |  |  |         $this->machine["deleted"] = $deleted; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function isDeleted(): bool { | 
					
						
							|  |  |  |         return $this->machine["deleted"] == true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:29:37 -06:00
										 |  |  |     public function addEvent(string $date, string $event, string $techuid = "", string $publicnotes = "", string $privatenotes = "") { | 
					
						
							| 
									
										
										
										
											2020-06-07 20:47:02 -06:00
										 |  |  |         $evt = Event::create($this->machineid, $date, $event, $techuid, $publicnotes, $privatenotes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->events[] = $evt; | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-06-26 20:40:01 -06:00
										 |  |  |      * Generate a random ID number that is not in use. | 
					
						
							| 
									
										
										
										
											2020-06-26 21:09:21 -06:00
										 |  |  |      * Default: 680#####[check digit]
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |      * @global $database | 
					
						
							|  |  |  |      * @param int $min Optional minimum number. | 
					
						
							|  |  |  |      * @param int $max Optional maximum number. | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-06-26 21:09:21 -06:00
										 |  |  |     public static function generateId(int $min = 68010000, int $max = 68099999): int { | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |         global $database; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         do { | 
					
						
							| 
									
										
										
										
											2020-06-26 20:40:01 -06:00
										 |  |  |             $id = random_int($min, $max); | 
					
						
							| 
									
										
										
										
											2020-06-26 21:09:21 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // If default gen add check digit
 | 
					
						
							| 
									
										
										
										
											2020-06-26 21:19:44 -06:00
										 |  |  |             if ($id >= 68010000 && $id <= 68099999) { | 
					
						
							| 
									
										
										
										
											2020-06-26 21:09:21 -06:00
										 |  |  |                 $id = ("$id" . CheckDigit::S10($id)) * 1; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-29 22:38:10 -06:00
										 |  |  |         } while ($database->has('machines', ['machineid' => $id]) || $database->has('components', ['compid' => $id])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |