Issues (103)

src/FlexiPeeHP/Company.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * FlexiPeeHP - Objekt Společnosti.
5
 *
6
 * @author     Vítězslav Dvořák <[email protected]>
7
 * @copyright  (C) 2015-2020 Spoje.Net
8
 */
9
10
namespace FlexiPeeHP;
11
12
use FlexiPeeHP\RW;
13
14
/**
15
 * Firmy/účetní jednotky
16
 *
17
 * @note Tato položka nemá dostupné položky evidence
18
 */
19
class Company extends RW
20
{
21
    /**
22
     * Základní namespace pro komunikaci s FlexiBEE.
23
     *
24
     * @var string Jmený prostor datového bloku odpovědi
25
     */
26
    public $nameSpace = 'companies';
27
28
    /**
29
     * Default Line Prefix.
30
     *
31
     * @var string
32
     */
33
    public $prefix = '/c';
34
35
    /**
36
     * Company.
37
     *
38
     * @var string
39
     */
40
    public $evidence = '';
41
42
    /**
43
     * Tato třída nepracuje sezvolenou firmou.
44
     *
45
     * @var string
46
     */
47
    public $company = '';
48
49
    /**
50
     * Key Column for this evidence
51
     * @var string
52
     */
53
    public $keyColumn = 'dbNazev';
54
55
    /**
56
     * FlexiBee Company Class .
57
     *
58
     * @param string|array $init    company dbNazev or initial data
59
     * @param array        $options Connection settings override
60
     */
61
    public function __construct($init = null, $options = [])
62
    {
63
        if (is_string($init)) {
64
            $init = ['dbNazev' => $init];
65
        }
66
        parent::__construct($init, $options);
67
    }
68
69
    /**
70
     * Zinicializuje objekt dle daných dat. Možné hodnoty:
71
     *
72
     *  * ['dbNazev'=>'company']           - load company form FlexiBee
73
     *  * 234                              - interní číslo záznamu k načtení
74
     *  * code:LOPATA                      - kód záznamu
75
     *  * BAGR                             - kód záznamu k načtení
76
     *  * ['id'=>24,'nazev'=>'hoblík']     - pole hodnot k předvyplnění
77
     *  * 743.json?relations=adresa,vazby  - část url s parametry k načtení
78
     *
79
     * @param mixed $init číslo/"(code:)kód"/(část)URI záznamu k načtení | pole hodnot k předvyplnění
80
     */
81
    public function processInit($init)
82
    {
83
84
        parent::processInit($init);
85
        if (is_array($init) && array_key_exists('dbNazev', $init)) {
86
            $companyInfo = $this->getFlexiData('/c/'.$init['dbNazev']);
87
            if (!array_key_exists('message', $companyInfo)) {
88
                $this->takeData(current($companyInfo));
89
            }
90
        }
91
    }
92
93
    /**
94
     * Vrací základní URL pro užitou evidenci
95
     *
96
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
97
     * @param string $urlSuffix
98
     */
99
    public function getEvidenceURL($urlSuffix = null)
100
    {
101
        if (is_null($urlSuffix)) {
102
            $urlSuffix = $this->evidence;
103
        }
104
105
        $url = $this->url.$this->prefix.'/'.$this->company;
106
        if (!empty($urlSuffix)) {
107
            $url .= (($urlSuffix[0] == '.') ? '' : '/').$urlSuffix;
108
        }
109
        return $url;
110
    }
111
112
    /**
113
     * Gives you current ApiURL with given format suffix
114
     * 
115
     * @param string $format json|html|xml|...
116
     * 
117
     * @return string API URL for current record or object/evidence
118
     */
119
    public function getApiURL($format = null)
120
    {
121
        return dirname(parent::getApiURL($format));
122
    }
123
124
    /**
125
     * Vrací název evidence použité v odpovědích z FlexiBee
126
     *
127
     * @return string
128
     */
129
    public function getResponseEvidence()
130
    {
131
        return 'company';
132
    }
133
134
    /**
135
     * Parse Raw FlexiBee response in several formats
136
     *
137
     * @param string $responseRaw raw response body
138
     * @param string $format      Raw Response format json|xml|etc
139
     *
140
     * @return array
141
     */
142
    public function rawResponseToArray($responseRaw, $format)
143
    {
144
        if (strstr($responseRaw, 'winstrom')) {
145
            $nsbackup        = $this->nameSpace;
146
            $this->nameSpace = 'winstrom';
147
            $response        = parent::rawResponseToArray($responseRaw, $format);
148
            $this->nameSpace = $nsbackup;
149
        } else {
150
            $response = parent::rawResponseToArray($responseRaw, $format);
151
        }
152
        return $response;
153
    }
154
155
    /**
156
     * Save company backup to file
157
     *
158
     * @param string $filename
159
     *
160
     * @return boolean was backup saved to file ?
161
     */
162
    public function saveBackupTo($filename)
163
    {
164
        $result                                   = false;
165
        $headersBackup                            = $this->defaultHttpHeaders;
166
        $this->defaultHttpHeaders['Accept']       = '*/*';
167
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
168
        $this->performRequest('backup', 'GET');
169
        $this->defaultHttpHeaders                 = $headersBackup;
170
171
        if ($this->lastResponseCode == 200) {
172
            if (file_put_contents($filename, $this->lastCurlResponse)) {
173
                $result = true;
174
            }
175
        }
176
        $this->defaultHttpHeaders = $headersBackup;
177
        return $result;
178
    }
179
180
    /**
181
     * Restore company from given file
182
     *
183
     * @link https://www.flexibee.eu/api/dokumentace/ref/restore-backup/ Obnovení ze zálohy
184
     *
185
     * @param string $filename             *.winstrom-backup file
186
     * @param string $name                 Extra name for restored company   
187
     * @param boolean $disableEet          Disable EET on restored company 
188
     * @param boolean $disableAutoSendMail Dsable auto sending of all documents 
189
     * @param boolean $disableWebHooks     Remove Registered webhooks 
190
     *
191
     * @return boolean restore result
192
     */
193
    public function restoreBackupFrom($filename, $name = null,
194
                                      $disableEet = false,
195
                                      $disableAutoSendMail = false,
196
                                      $disableWebHooks = false)
197
    {
198
        $options = [];
199
        if (!empty($name)) {
200
            $options['name'] = $name;
201
        }
202
        if ($disableEet === true) {
203
            $options['disableEet'] = 1;
204
        }
205
        if ($disableAutoSendMail === true) {
206
            $options['disableAutoSendMail'] = 1;
207
        }
208
        if ($disableWebHooks === true) {
209
            $options['disableWebHooks'] = 1;
210
        }
211
        $headersBackup                            = $this->defaultHttpHeaders;
212
        $this->defaultHttpHeaders['Accept']       = '*/*';
213
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
214
        $this->setPostFields(file_get_contents($filename));
215
        $this->performRequest('restore'.(empty($options) ? '' : '?'.http_build_query($options) ),
216
            'PUT');
217
        $this->defaultHttpHeaders = $headersBackup; 
218
        return $this->lastResponseCode == 200;
219
    }
220
221
    /**
222
     * Create new company
223
     *
224
     * @param string $name
225
     *
226
     * @return boolean
227
     */
228
    public function createNew($name)
229
    {
230
        $this->performRequest('/admin/zalozeni-firmy?name='.urlencode($name),
231
            'PUT');
232
        return $this->lastResponseCode == 201;
233
    }
234
235
    /**
236
     * Obtain company identifier
237
     *
238
     * @return string company database name
239
     */
240
    public function getRecordID()
241
    {
242
        return $this->getDataValue('dbNazev');
243
    }
244
245
    /**
246
     * Company has no relations
247
     *
248
     * @return null
249
     */
250
    public function getVazby($id = null)
251
    {
252
        throw new Exception(_
0 ignored issues
show
The type FlexiPeeHP\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
253
                ('Company has no relations'));
254
    }
255
256
    /**
257
     * Smaže firmu ve FlexiBee
258
     * Delete  company in FlexiBee
259
     *
260
     * @param string $company identifikátor záznamu
261
     * 
262
     * @return boolean Response code is 200 ?
263
     */
264
    public function deleteFromAbraFlexi($company = null)
265
    {
266
        if (is_null($company)) {
267
            $company = $this->getDataValue('dbNazev');
268
        }
269
        $this->performRequest('/c/'.$company.'.'.$this->format, 'DELETE');
270
        return $this->lastResponseCode == 200;
271
    }
272
}
273