From 11d17692325a9d203025120d0e178c33bc921b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=C4=9Bzslav=20Dvo=C5=99=C3=A1k?= Date: Tue, 17 Oct 2023 16:13:54 +0200 Subject: [PATCH] Allow use of parameters --- README.md | 46 +++++++++++ examples/listdefectsdeal.php | 11 +++ src/ApiClient.php | 145 ++++++++++++++++++++++++++--------- 3 files changed, 165 insertions(+), 37 deletions(-) create mode 100644 examples/listdefectsdeal.php diff --git a/README.md b/README.md index 63c5673..1a23b9e 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,51 @@ INSPECTION_DEFECTS_COMMUNAL_AREA, INSPECTION_DEFECTS_COMBINED. The last column contains the unique defect ID from the Realpad database. The second column is the relevant deal ID. +```php +$client = new \SpojeNet\Realpad\ApiClient(); +$defects = $client->listDefects('DEAL_DEFECTS'); +print_r($defects); +``` + +
+Array
+(
+    [2] => Array
+        (
+            [Projekt] => Nove Sidliste
+            [Obchodní případ] => 12323234
+            [Typ kontroly] => Technická přejímka
+            [Typ položky technické přejímky] => Podlahy
+            [Jednotka] => TEST TECHNICKÁ PŘEJÍMKA
+            [Zákazník] => REALPAD TEST
+            [Telefon] => 
+            [E-mail] => realpad@test.eu
+            [Číslo vady] => 25456542
+            [Problémová vada] => Ne
+            [Číslo vady dle zákazníka] => 
+            [Popis] => prasklá dlažba
+            [Lokace (např. místnost)] => 
+            [Poslední vyjádření developera] => 
+            [Odesláno zákazníkovi] => 
+            [Poslední vyjádření dodavatele] => 
+            [Přijato dne] => 7/7/2023
+            [Termín pro odstranění vady] => 8/6/2023
+            [Plánovaný termín opravy] => 
+            [Odstraněna dne] => 
+            [Poznámka] => 
+            [Odpovědná osoba] => 
+            [Speciální záruční lhůta] => Ne
+            [Stav] => Přijato do evidence
+            [Část bytu které sa vada týká] => 
+            [Běžný problém] => 
+            [Místnost, které se vada týká] => Koupelna/WC
+            [Dodavatel] => 
+            [Generální dodavatel] => 
+            [Reklamace ID] => 25654654
+        )
+
+
+ **list-excel-tasks** The last columns contain the task ID, customer ID, and sales agent ID from the Realpad database. @@ -209,6 +254,7 @@ data of its Lines. ## Appendix Unit status enumeration + ● 0 - free. ● 1 - pre-reserved. diff --git a/examples/listdefectsdeal.php b/examples/listdefectsdeal.php new file mode 100644 index 0000000..24c5df1 --- /dev/null +++ b/examples/listdefectsdeal.php @@ -0,0 +1,11 @@ +listDefects('DEAL_DEFECTS'); + +print_r($projects); diff --git a/src/ApiClient.php b/src/ApiClient.php index c1a1e8a..24311d5 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -80,13 +80,56 @@ class ApiClient extends \Ease\Sand */ private $lastResponseCode; + /** + * @var array Unit status enumeration + */ + public $unitStatus = [ + 0 => 'free', + 1 => 'pre-reserved', + 2 => 'reserved', + 3 => 'sold', + 4 => 'not for sale', + 5 => 'delayed' + ]; + + /** + * @var array Unit type enumeration + */ + public $unitType = [ + 1 => 'flat', + 2 => 'parking', + 3 => 'cellar', + 4 => 'outdoor parking', + 5 => 'garage', + 6 => 'commercial space', + 7 => 'family house', + 8 => 'land', + 9 => 'atelier', + 10 => 'office', + 11 => 'art workshop', + 12 => 'non-residential unit', + 13 => 'motorbike parking', + 14 => 'creative workshop', + 15 => 'townhouse', + 16 => 'utility room', + 17 => 'condominium', + 18 => 'storage', + 19 => 'apartment', + 20 => 'accommodation unit', + 21 => 'bike stand', + 22 => 'communal area' + ]; + /** * RealPad Data obtainer + * + * @var string $username - leave empty to use Environment or constant REALPAD_USERNAME + * @var string $password - leave empty to use Environment or constant REALPAD_PASSWORD */ - public function __construct() + public function __construct($username = '', $password = '') { - $this->apiUsername = \Ease\Shared::cfg('REALPAD_USERNAME'); - $this->apiPassword = \Ease\Shared::cfg('REALPAD_PASSWORD'); + $this->apiUsername = strlen($username) ? $username : \Ease\Shared::cfg('REALPAD_USERNAME'); + $this->apiPassword = strlen($password) ? $password : \Ease\Shared::cfg('REALPAD_PASSWORD'); $this->curlInit(); $this->setObjectName(); } @@ -117,11 +160,6 @@ public function curlInit() CURLOPT_USERAGENT, 'RealpadTakeout v' . \Ease\Shared::appVersion() . ' https://github.com/Spoje-NET/Realpad-Takeout' ); - \curl_setopt( - $this->curl, - CURLOPT_POSTFIELDS, - 'login=' . $this->apiUsername . '&password=' . $this->apiPassword - ); return $this->curl; } @@ -133,8 +171,16 @@ public function curlInit() * * @return int HTTP Response CODE */ - public function doCurlRequest($url, $method = 'GET') + public function doCurlRequest($url, $method = 'GET', $postParams = []) { + \curl_setopt( + $this->curl, + CURLOPT_POSTFIELDS, + array_merge( + ['login' => $this->apiUsername, 'password' => $this->apiPassword], + $postParams + ) + ); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, strtoupper($method)); @@ -256,9 +302,9 @@ public function __destruct() * * @return array */ - public function getExcelData($endpoint) + public function getExcelData($endpoint, $params = []) { - $responseCode = $this->doCurlRequest($this->baseEndpoint . 'ws/v10/' . $endpoint, 'POST'); + $responseCode = $this->doCurlRequest($this->baseEndpoint . 'ws/v10/' . $endpoint, 'POST', $params); $excelData = []; if ($responseCode == 200) { $xls = sys_get_temp_dir() . '/' . $endpoint . '_' . \Ease\Functions::randomString() . '.xls'; @@ -379,21 +425,36 @@ public function listInspections() * Accepts an additional optional parameter mode. By default all the Deal * Warranty Claim Defects are returned. Certain developers will also see the * Communal Areas Defects here by default. If mode is specified, other - * Defects can be returned. Available modes are: - * DEAL_DEFECTS, DEAL_DEFECTS_COMMUNAL_AREA, DEAL_DEFECTS_COMBINED, - * INSPECTION_DEFECTS, INSPECTION_DEFECTS_COMMUNAL_AREA, - * INSPECTION_DEFECTS_COMBINED. + * Defects can be returned. * * The last column contains the unique defect ID from the Realpad database. * The second column is the relevant deal ID. * * @todo Implement Modes * + * @var string mode none or one from: DEAL_DEFECTS, + * DEAL_DEFECTS_COMMUNAL_AREA, + * DEAL_DEFECTS_COMBINED, + * INSPECTION_DEFECTS, + * INSPECTION_DEFECTS_COMMUNAL_AREA, + * INSPECTION_DEFECTS_COMBINED. + * * @return array */ - public function listDefects() + public function listDefects($mode = '') { - return $this->getExcelData('list-excel-defects'); + $modesAvailble = [ + 'DEAL_DEFECTS', + 'DEAL_DEFECTS_COMMUNAL_AREA', + 'DEAL_DEFECTS_COMBINED', + 'INSPECTION_DEFECTS', + 'INSPECTION_DEFECTS_COMMUNAL_AREA', + 'INSPECTION_DEFECTS_COMBINED' + ]; + if (strlen($mode) && (array_search($mode, $modesAvailble) === false)) { + throw new \SpojeNet\Realpad\Exception('Iillegal inspection Mode ' . $mode); + } + return $this->getExcelData('list-excel-defects', ['mode' => $mode]); } /** @@ -429,43 +490,53 @@ public function listSalesStatus() } /** - * Accepts an additional required parameter unitid, which has to be a valid - * unit Realpad database ID obtained from some other endpoint. * The first column contains the timestamp of when the given unit started * containing the data on the given row. The second column contains the name * of the user who caused that data to be recorded. * + * @var int $unitID Required parameter unitid, which has to be a valid unit + * Realpad database ID obtained from some other endpoint. + * * @return array */ - public function listUnitHistory() + public function listUnitHistory(int $unitID) { - return $this->getExcelData('list-excel-unit-history'); + return $this->getExcelData('list-excel-unit-history', ['unitid' => $unitID]); } /** - * Accepts several additional optional parameters: - * ● `filter_status` - if left empty, invoices in all statuses are sent. 1 - new invoices. 2 - - * invoices in Review #1. 3 - invoices in Review #2. 4 - invoices in approval. 5 - fully - * approved invoices. 6 - fully rejected invoices. + * Listing of Invoices. The initial set of columns describes the Invoice + * itself, and the last set of columns contains the data of its Lines. + * + * @var array $options ● `filter_status` - if left empty, invoices in all statuses are sent. 1 - new invoices. 2 - + * invoices in Review #1. 3 - invoices in Review #2. 4 - invoices in approval. 5 - fully + * approved invoices. 6 - fully rejected invoices. * - * ● `filter_groupcompany` - if left empty, invoices from all the group companies are sent. If - * Realpad database IDs of group companies are provided (as a comma-separated list), - * then only invoices from these companies are sent. + * ● `filter_groupcompany` - if left empty, invoices from all the group companies are sent. If + * Realpad database IDs of group companies are provided (as a comma-separated list), + * then only invoices from these companies are sent. * - * ● `filter_issued_from`` - specify a date in the 2019-12-31 format to only send invoices - * issues after that date. + * ● `filter_issued_from` - specify a date in the 2019-12-31 format to only send invoices + * issues after that date. * - * ● `filter_issued_to` - specify a date in the 2019-12-31 format to only send invoices issues - * before that date. - * The initial set of columns describes the Invoice itself, and the last set of columns contains the - * data of its Lines. + * ● `filter_issued_to` - specify a date in the 2019-12-31 format to only send invoices issues before that date. * - * @todo Implement Filters * * @return array */ - public function listInvoices() + public function listInvoices($options = []) { - return $this->getExcelData('list-excel-invoices'); + $colsAvailble = [ + 'filter_status', + 'filter_groupcompany', + 'filter_issued_from', + 'filter_issued_to' + ]; + foreach ($options as $key => $value) { + if (array_search($key, $colsAvailble) === false) { + throw new \SpojeNet\Realpad\Exception('Iillegal Invoice option ' . $key); + } + } + return $this->getExcelData('list-excel-invoices', $options); } }