72 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| /*
 | |
|  * Copyright 2020 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/.
 | |
|  */
 | |
| 
 | |
| class TrackingCode {
 | |
| 
 | |
|     public $code = "";
 | |
|     public $type = "";
 | |
|     public $containerid = "";
 | |
|     public $trackingurl = "";
 | |
|     public $exists = false;
 | |
| 
 | |
|     public function __construct($code) {
 | |
|         global $database;
 | |
| 
 | |
|         if ($database->has("trackingcodes", ["code" => $code])) {
 | |
|             $this->exists = true;
 | |
|             $info = $database->get("trackingcodes", ["containerid", "code", "codetype"], ["code" => $code]);
 | |
|             $this->code = $info["code"];
 | |
|             $this->containerid = $info["containerid"];
 | |
|             $this->type = $info["codetype"];
 | |
|         } else {
 | |
|             $this->exists = false;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function getCarrier(): string {
 | |
|         return TrackingCode::typeToCarrier($this->type);
 | |
|     }
 | |
| 
 | |
|     public function save() {
 | |
|         global $database;
 | |
| 
 | |
|         if (!$database->has("containers", ["containerid" => $this->containerid])) {
 | |
|             throw new Exception("No container with ID $this->containerid exists.");
 | |
|             return false;
 | |
|         }
 | |
|         if ($database->has("trackingcodes", ["code" => $code])) {
 | |
|             $database->update("trackingcodes", [
 | |
|                 "codetype" => $this->type,
 | |
|                 "containerid" => $this->containerid
 | |
|                     ], ["code" => $this->code]);
 | |
|         } else {
 | |
|             $database->insert("trackingcodes", [
 | |
|                 "code" => $this->code,
 | |
|                 "containerid" => $this->containerid,
 | |
|                 "codetype" => $this->type
 | |
|             ]);
 | |
|             $this->exists = true;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public static function typeToCarrier($type) {
 | |
|         switch (strtolower($type)) {
 | |
|             case "usps":
 | |
|                 return "USPS";
 | |
|             case "ups":
 | |
|                 return "UPS";
 | |
|             case "fedex":
 | |
|                 return "FedEx";
 | |
|             default:
 | |
|                 return "";
 | |
|         }
 | |
|     }
 | |
| 
 | |
| }
 |