Voucher::validate()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 8
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
ccs 12
cts 12
cp 1
crap 4
1
<?php
2
/**
3
 * Voucher
4
 *
5
 * @package Intraface_Accounting
6
 * @author  Lars Olesen
7
 * @since   1.0
8
 * @version     1.0
9
 */
10
require_once 'Intraface/modules/accounting/Account.php';
11
require_once 'Intraface/modules/accounting/Post.php';
12
13
class Voucher extends Intraface_Standard
14
{
15
    private $id; // integer
16
    public $year; // object
17
    public $error; // object
18
    public $value; // array
19
    private $vatpercent; // float
20
21
    /**
22
     * Constructor
23
     *
24
     * @param object  $year_object
25
     * @param integer $post_id (optional)
0 ignored issues
show
Bug introduced by
There is no parameter named $post_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     *
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29 20
    function __construct($year_object, $id = 0)
30
    {
31 20
        $this->error      = new Intraface_Error;
32 20
        $this->year       = $year_object;
33 20
        $this->id         = (int)$id;
34 20
        $this->vatpercent = $this->year->kernel->getSetting()->get('intranet', 'vatpercent');
35
36 20
        if ($this->id > 0) {
37 12
            $this->load();
38 12
        }
39 20
    }
40
41
    /**
42
     * Creates a voucher for the voucher number
43
     *
44
     * @deprecated
45
     * @param object $year
46
     * @param string $voucher_number
47
     *
48
     * @return void
49
     */
50
    function factory($year, $voucher_number)
51
    {
52
        $gateway = new Intraface_modules_accounting_VoucherGateway($year);
53
        return $gateway->findFromVoucherNumber($voucher_number);
54
    }
55
56
    /**
57
     * Loads data
58
     *
59
     * @return boolean
60
     */
61 18
    private function load()
62
    {
63
        $sql = "SELECT
64
                    voucher.id AS id,
65
                    voucher.number,
66
                    voucher.text,
67
                    DATE_FORMAT(voucher.date, '%d-%m-%Y') AS date_dk,
68
                    voucher.date,
69
                    voucher.reference
70
            FROM accounting_voucher voucher
71 18
            WHERE voucher.id = " . $this->id. " AND intranet_id = ". $this->year->kernel->intranet->getId();
72
73 18
        $db = new DB_Sql;
74 18
        $db->query($sql);
75
76 18
        if (!$db->nextRecord()) {
77 2
            return false;
78
        }
79 16
        $this->value['id'] = $db->f('id');
80 16
        $this->value['number'] = $db->f('number');
81 16
        $this->value['text'] = $db->f('text');
82 16
        $this->value['reference'] = $db->f('reference');
83 16
        $this->value['date'] = $db->f('date');
84 16
        $this->value['date_dk'] = $db->f('date_dk');
85
86 16
        return true;
87
    }
88
89
    /**
90
     * Valideringsfunktioner
91
     *
92
     * @param array $var Array to validate
93
     *
94
     * @return boolean
95
     */
96 16
    function validate($var)
97
    {
98 16
        $validator = new Intraface_Validator($this->error);
99 16
        if (!empty($var['voucher_number'])) {
100 16
            $validator->isNumeric($var['voucher_number'], 'Voucher er ikke et tal', 'allow_empty');
101 16
        }
102 16
        if (!empty($var['reference'])) {
103 4
            $validator->isString($var['reference'], 'Reference er ikke en streng', '', 'allow_empty');
104 4
        }
105 16
        $validator->isString($var['text'], 'Beskrivelsen skal være en tekststreng');
106
107 16
        if ($this->error->isError()) {
108 1
            return false;
109
        }
110 16
        return true;
111
    }
112
113
    /**
114
     * Updates voucher
115
     *
116
     * You can edit the voucher, but not touch the posts when they are stated
117
     *
118
     * @param array $var With information
119
     *
120
     * @return 0 = error; 1 = success
0 ignored issues
show
Documentation introduced by
The doc-type 0 could not be parsed: Unknown type name "0" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
121
     */
122 16
    public function save($var)
123
    {
124 16
        if (empty($var['reference'])) {
125 14
            $var['reference'] = '';
126 14
        }
127
128 16
        $var = safeToDb($var);
129
130 16
        $post_date = new Intraface_Date($var['date']);
131 16
        $post_date->convert2db();
132
133 16
        if (empty($var['voucher_number'])) {
134 6
            $var['voucher_number'] = $this->getMaxNumber() + 1;
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::getMaxNumber() has been deprecated.

This method has been deprecated.

Loading history...
135 6
        }
136
137 16
        if (!$this->validate($var)) {
138
            return 0;
139
        }
140
141 16 View Code Duplication
        if (empty($this->id)) {
142 16
            $sql_type = "INSERT INTO";
143 16
            $sql_end = ", date_created = NOW()";
144 16
        } else {
145 1
            $sql_type = "UPDATE";
146 1
            $sql_end = " WHERE id = " . (int)$this->id;
147
        }
148
149 16
        $db = new DB_Sql;
150
        $sql = $sql_type . " accounting_voucher
151 16
            SET intranet_id = ".$this->year->kernel->intranet->get('id').",
152 16
                year_id = ".$this->year->get('id').",
153 16
                user_id = ".$this->year->kernel->user->get('id').",
154
                date_updated = NOW(),
155 16
                number = '".$var['voucher_number']."',
156 16
                date = '".$post_date->get()."',
157 16
                reference = '".$var['reference']."',
158 16
                text = '".$var['text']."'" . $sql_end;
159
160 16
        $db->query($sql);
161
162 16
        if ($this->id == 0) {
163 16
            $this->id = $db->insertedId();
164 16
        }
165
166 16
        $this->load();
167
168 16
        return $this->id;
169
    }
170
171 11
    function saveInDaybook($var, $skip_draft = false)
172
    {
173 11
        $var = safeToDb($var);
174
175 11
        $post_date = new Intraface_Date($var['date']);
176 11
        if (!$post_date->convert2db()) {
177 1
            $this->error->set('Kunne ikke konvertere datoen');
178 1
        }
179
180 11
        $validator = new Intraface_Validator($this->error);
181 11
        $validator->isNumeric($var['debet_account_number'], 'Debetkontoen er ikke et tal');
182 11
        $validator->isNumeric($var['credit_account_number'], 'Kreditkontoen er ikke et tal');
183 11
        $validator->isDouble($var['amount'], 'Beløbet skal være et tal');
184 11
        settype($var['vat_off'], 'integer');
185 11
        if ($var['vat_off'] != 0 and $var['vat_off'] != 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
186
            $this->error->set('vat_off');
187
        }
188
189 11
        if (!$this->year->isYearOpen()) {
190
            $this->error->set('Dette år er ikke åbent til bogføring');
191 11
        } elseif (!$this->year->isDateInYear($post_date->get())) {
192
            $this->error->set('Denne dato er ikke i det pågældende år');
193
        }
194
195 11
        if (empty($var['vat_off'])) {
196 6
            $var['vat_off'] = 0;
197 6
        }
198 11
        if (empty($var['debet_account_number'])) {
199 1
            $var['debet_account_number'] = 0;
200 1
        }
201 11
        if (empty($var['credit_account_number'])) {
202 1
            $var['credit_account_number'] = 0;
203 1
        }
204
205 11
        $amount = new Intraface_Amount($var['amount']);
206 11
        if (!$amount->convert2db()) {
207
            $this->error->set('Beløbet kunne ikke konverteres');
208
        }
209 11
        $var['amount'] = $amount->get();
210
211
        // Treat vales for save
212 11
        $var = safeToDb($var);
213
214 11
        if (!$this->validate($var)) {
215 1
            return 0;
216
        }
217
218 10
        $debetaccount = $this->getAccount($var['debet_account_number']);
219
220 10
        if (!$debetaccount->validForState()) {
221
            $this->error->set('Du kan ikke bogføre på den valgte debetkonto');
222
        }
223
224 10
        $creditaccount = $this->getAccount($var['credit_account_number']);
225
226 10
        if (!$creditaccount->validForState()) {
227
            $this->error->set('Du kan ikke bogføre på den valgte kreditkonto');
228
        }
229
230 10
        if ($this->error->isError()) {
231
            return 0;
232
        }
233
234
        // if already found, do not save ahead of time
235 10
        if ($this->id == 0) {
236 3
            $this->save($var);
237 3
        }
238
239 10
        $this->value['text'] = $var['text'];
240 10
        $this->value['amount'] = $var['amount'];
241 10
        $this->value['date'] = $post_date->get();
242 10
        $this->value['debet_account_number'] = $debetaccount->get('number');
243 10
        $this->value['debet_account_id'] = $debetaccount->get('id');
244 10
        $this->value['debet_account_name'] = $debetaccount->get('name');
245 10
        $this->value['credit_account_id'] = $creditaccount->get('id');
246 10
        $this->value['credit_account_number'] = $creditaccount->get('number');
247 10
        $this->value['credit_account_name'] = $creditaccount->get('name');
248 10
        $this->value['vat_off'] = $var['vat_off'];
249 10
        $this->value['saldo'] = 0;
250
251 10
        $this->state($skip_draft);
252
253 10
        return $this->id;
254
    }
255
256 9
    protected function getAccount($number)
257
    {
258 9
        $gateway = new Intraface_modules_accounting_AccountGateway($this->year);
259 9
        return $gateway->findFromNumber($number);
260
    }
261
262
    /**
263
     * @return boolean
264
     */
265 1
    function delete()
266
    {
267 1
        return true;
268
    }
269
270
    /**
271
     * @return array
272
     */
273
274 1
    function getList($filter = '')
275
    {
276 1
        $gateway = new Intraface_modules_accounting_VoucherGateway($this->year);
277 1
        return $gateway->getList($filter);
278
    }
279
280
    /**
281
     * States voucher
282
     *
283
     * @param boolean $skip_draft if daybook is to be skipped
284
     */
285 10
    function state($skip_draft = false)
286
    {
287
        // Bogføring af almindeligt køb i Danmark
288
        // Ifølge det dobbelte bogholderis princip skal alle poster bogføres på mindst to
289
        // konti.
290
291
        // debetkontoen
292 10
        $this->_stateHelper($this->get('date'), $this->get('text'), $this->get('debet_account_id'), $this->get('amount'), 0, $this->get('vat_off'), $skip_draft);
293
294
        // kreditkontoen
295 10
        $this->_stateHelper($this->get('date'), $this->get("text"), $this->get('credit_account_id'), 0, $this->get('amount'), $this->get('vat_off'), $skip_draft);
296
297
        // Varekøb i udlandet
298
        // Der skal udregnes moms af alle varekøb i udlandet, og de skal bogføres
299
        // på den tilhørende konto
300
301 10
        $buy_abroad = unserialize($this->year->getSetting('buy_abroad_accounts'));
302 10
        $buy_eu = unserialize($this->year->getSetting('buy_eu_accounts'));
303
304 10
        if (is_array($buy_eu) and is_array($buy_abroad)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
305 10
            $buy_all_abroad = array_merge($buy_abroad, $buy_eu);
306 10
        } elseif (is_array($buy_eu)) {
307
            $buy_all_abroad = $buy_eu;
308
        } elseif (is_array($buy_abroad)) {
309
            $buy_all_abroad = $buy_abroad;
310
        }
311
312 10
        $amount = $this->get('amount') * ($this->vatpercent / 100);
313
314
        // I det omfang du har fradragsret for momsen, kan du medregne det beregnede
315
        // momsbeløb til konto for indgående moms. Det beregnede momsbeløb af EU-varekøb
316
        // behandles dermed på samme måde som momsen af varekøb foretaget i Danmark.
317
318 10
        if ($this->get('vat_off') == 0) {
319
            // gemme moms hvis det er nødvendigt
320 6
            if (isset($buy_all_abroad) && is_array($buy_all_abroad) and in_array($this->get('debet_account_id'), $buy_all_abroad)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
321
                // så skal beløbet ganges med momsprocenten og smides på moms af varekøb i udlandet
322
                $credit = new Post($this);
323
                $credit->save($this->get('date'), $this->year->getSetting('vat_abroad_account_id'), 'Moms af varekøb i udland', 0, $amount, $skip_draft);
324
                $debet = new Post($this);
325
                $debet->save($this->get('date'), $this->year->getSetting('vat_in_account_id'), 'Moms af varekøb i udland', $amount, 0, $skip_draft);
326 6
            } elseif (!empty($buy_all_abroad) and is_array($buy_all_abroad) and in_array($this->get('credit_account_id'), $buy_all_abroad)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
327
                // tilbageføring af moms hvis nødevndigt
328
                // så skal beløbet ganges med momsprocenten og smides på moms af varekøb i udlandet
329
                $debet = new Post($this);
330
                $debet->save($this->get('date'), $this->year->getSetting('vat_abroad_account_id'), 'Tilbageført: Moms af varekøb i udland', $amount, 0, $skip_draft);
331
                $credit = new Post($this);
332
                $credit->save($this->get('date'), $this->year->getSetting('vat_in_account_id'), 'Tilbageført: Moms af varekøb i udland', 0, $amount, $skip_draft);
333
            }
334 6
        }
335 10
        return true;
336
    }
337
338
339
    /**
340
     * Prepares amounts for stating, e.g. whether the accounts needs vat calculations
341
     *
342
     * @param integer $year_id
0 ignored issues
show
Bug introduced by
There is no parameter named $year_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
343
     * @param string  $date
344
     * @param string  $voucher_number
0 ignored issues
show
Bug introduced by
There is no parameter named $voucher_number. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
345
     * @param string  $text
346
     * @param integer $account_id
347
     * @param float   $debet
348
     * @param float   $credit
349
     *
350
     * @return boolean
351
     */
352 10
    private function _stateHelper($date, $text, $account_id, $debet, $credit, $vat_off = 0, $skip_draft = false)
353
    {
354
355 10
        $text = safeToDb($text);
356 10
        $vat_percent = $this->vatpercent;
357
358
        // Kontoen
359 10
        $account = new Account($this->year, $account_id);
360 10
        $vat_account_id = $account->get('vat_account_id');
361
362
        // Konti uden moms
363 10
        if ($vat_off == 1) {
364 4
            $post = new Post($this);
365 4
            $post->save($date, $account_id, $text, $debet, $credit, $skip_draft);
366 10
        } elseif ($vat_off == 0) {
367
            // Konti med moms
368
369
            // Hvis der er moms på kontoen skal den trækkes fra beløbet først
370 6
            switch ($account->get('vat')) {
371 6 View Code Duplication
                case 'in': // indgående moms - købsmoms
372 2
                    if ($debet > 0) { // bogfør til momskonto hvis det er et debet-beløb
373 2
                        $vat_amount = $this->calculateVat($debet, $vat_percent);
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::calculateVat() has been deprecated.

This method has been deprecated.

Loading history...
374 2
                        $debet = $debet - $vat_amount;
375
                        // bogfør momsen
376 2
                        $post = new Post($this);
377 2
                        $post->save($date, $vat_account_id, $text . " - købsmoms", $vat_amount, 0, $skip_draft);
378 2
                    } else {
379
                        $vat_amount = $this->calculateVat($credit, $vat_percent);
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::calculateVat() has been deprecated.

This method has been deprecated.

Loading history...
380
                        $credit = $credit - $vat_amount;
381
                        // bogføre udgående moms
382
                        $post = new Post($this);
383
                        $post->save($date, $vat_account_id, $text . " - tilbageført moms", 0, $vat_amount, $skip_draft);
384
                    }
385
386
                        // bogføre selve posten
387 2
                        $post = new Post($this);
388 2
                        $post->save($date, $account_id, $text, $debet, $credit, $skip_draft);
389
390 2
                    break;
391
392
                // udgående moms
393 6 View Code Duplication
                case 'out': // Bogfør til momskonto hvis det er et credit beløb
394
                    if ($credit > 0) {
395
                        $vat_amount = $this->calculateVat($credit, $vat_percent);
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::calculateVat() has been deprecated.

This method has been deprecated.

Loading history...
396
                        $credit = $credit - $vat_amount;
397
                        // bogføre udgående moms
398
                        $post = new Post($this);
399
                        $post->save($date, $vat_account_id, $text . " - salgsmoms", 0, $vat_amount, $skip_draft);
400
                    } else {
401
                        // tilbagefører momsen hvis det er et debet beløb
402
                        $vat_amount = $this->calculateVat($debet, $vat_percent);
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::calculateVat() has been deprecated.

This method has been deprecated.

Loading history...
403
                        $debet = $debet - $vat_amount;
404
405
                        // bogføre momsen
406
                        $post = new Post($this);
407
                        $post->save($date, $vat_account_id, "Tilbageført moms", $vat_amount, 0, $skip_draft);
408
                    }
409
410
                        // bogføre selve posten
411
                        $post = new Post($this);
412
                        $post->save($date, $account_id, $text, $debet, $credit, $skip_draft);
413
                    break;
414
415
                // hvis kontoen ikke er en momskonto
416 6
                default:
417
                        // bogføre bilaget hvor der ikke er moms
418 6
                        $post = new Post($this);
419 6
                        $post->save($date, $account_id, $text, $debet, $credit, $skip_draft);
420 6
                    break;
421 6
            }
422 6
        }
423
424 10
        return true;
425
    }
426
427
    /**
428
     * Bogfører de poster, der er i kassekladden
429
     *
430
     * Klassen vælger automatisk alle poster i kladden og bogfører dem en efter en.
431
     * De poster der kan bogføres, mens de resterende ikke bogføres.
432
     * Der laves igen tjek på, om året er åbent og om datoen for posten er i året.
433
     *
434
     * @return boolean
435
     */
436 1
    public function stateDraft()
437
    {
438 1
        if (!$this->year->vatAccountIsSet()) {
439
            $this->error->set('Du skal først sætte momskonti, inden du kan bogføre.');
440
        }
441
442 1
        if ($this->error->isError()) {
443
            return false;
444
        }
445
446 1
        $post = new Post($this);
447 1
        $posts = $post->getList('draft');
448 1
        if (!is_array($posts) or count($posts) == 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
449 1
            $this->error->set('Der var ikke nogen poster at bogføre');
450 1
            return false;
451
        }
452
453
        foreach ($posts as $p) {
454
            $post = new Post($this, $p['id']);
455
456
            if (!$post->setStated()) {
457
                $this->error->set('id#' .$p['id'] . ': Det lykkedes ikke at bogføre denne post.');
458
            }
459
460
            // tjekker om der har været nogle fejl i bogføringen
461
            if ($this->error->isError()) {
462
                //$this->error->view();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
463
                return false;
464
            }
465
        }
466
467
        return true;
468
    }
469
470 1
    function stateVoucher()
471
    {
472 1
        if (!$this->year->vatAccountIsSet()) {
473
            $this->error->set('Du skal først sætte momskonti, inden du kan bogføre.');
474
        }
475
476 1
        if ($this->id == 0) {
477
            $this->error->set('Kan kun bogføre et bilag, hvis den har et id');
478
        }
479
480 1
        if ($this->get('saldo') <> 0) {
481
            $this->error->set('Du kan kun bogføre et bilag, hvis det stemmer. Saldoen på dette bilag er ' . $this->get('saldo') . '.');
482
        }
483
484 1
        if ($this->error->isError()) {
485
            return false;
486
        }
487
488 1
        $posts = $this->getPosts();
489
490 1
        foreach ($posts as $p) {
491
            $post = new Post($this, $p['id']);
492
            if ($post->get('stated') == 1) {
493
                continue;
494
            }
495
496
            if (!$post->setStated()) {
497
                $this->error->set('id#' .$p['id'] . ': Det lykkedes ikke at bogføre denne post.');
498
            }
499 1
        }
500
        // tjekker om der har været nogle fejl i bogføringen
501 1
        if ($this->error->isError()) {
502
            //$this->error->view();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
503
            return false;
504
        }
505 1
        return true;
506
    }
507
508 9
    function getPosts()
509
    {
510 9
        $db = new DB_Sql;
511 9
        $db->query("SELECT id, text, debet, credit, account_id, stated, date, DATE_FORMAT(date, '%d-%m-%Y') AS date_dk FROM accounting_post WHERE voucher_id = " . $this->id . " AND intranet_id=".$this->year->kernel->intranet->get('id'));
512 9
        $list = array();
513 9
        $i = 0;
514 9
        $this->value['saldo'] = 0;
515 9
        while ($db->nextRecord()) {
516 8
            $list[$i]['id'] = $db->f('id');
517 8
            $list[$i]['date_dk'] = $db->f('date_dk');
518 8
            $list[$i]['date'] = $db->f('date');
519 8
            $list[$i]['text'] = $db->f('text');
520 8
            $list[$i]['debet'] = $db->f('debet');
521 8
            $list[$i]['credit'] = $db->f('credit');
522 8
            $list[$i]['voucher_number'] = $this->get('number');
523 8
            $list[$i]['reference'] = $this->get('reference');
524 8
            $list[$i]['voucher_id'] = $this->get('id');
525 8
            $list[$i]['account_id'] = $db->f('account_id');
526 8
            $list[$i]['stated'] = $db->f('stated');
527 8
            $account = new Account($this->year, $db->f('account_id'));
528 8
            $list[$i]['account_number'] = $account->get('number');
529 8
            $list[$i]['account_name'] = $account->get('name');
530
531 8
            $this->value['saldo'] += $db->f('debet');
532 8
            $this->value['saldo'] -= $db->f('credit');
533
534 8
            $i++;
535 8
        }
536 9
        return $list;
537
    }
538
539
    /**
540
     * Udregner momsbeløbet
541
     *
542
     * @deprecated
543
     *
544
     * @param float $amount
545
     * @param float $vat_percent
546
     *
547
     * @return float
548
     */
549
    public function calculateVat($amount, $vat_percent)
550
    {
551
        return Account::calculateVat($amount, $vat_percent);
552
    }
553
554
    /**
555
     * Returns highest voucher number
556
     *
557
     * @deprecated
558
     *
559
     * @return (int) maks vouchernumber
560
     */
561
    function getMaxNumber()
562
    {
563
        $gateway = new Intraface_modules_accounting_VoucherGateway($this->year);
564
        return $gateway->getMaxNumber();
565
    }
566
567 8
    public function getId()
568
    {
569 8
        return $this->id;
570
    }
571
}
572