Completed
Push — master ( d208aa...25518b )
by Lars
12:37
created

Account::getPosts()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.0738

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 3
nop 0
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
ccs 4
cts 23
cp 0.1739
crap 8.0738
1
<?php
2
/**
3
 * Represents an account
4
 *
5
 * @package Intraface_Accounting
6
 *
7
 * @author  Lars Olesen
8
 * @since   1.0
9
 * @version     1.0
10
 */
11
require_once 'Intraface/functions.php';
12
13
class Account extends Intraface_Standard
14
{
15
    /**
16
     * Account id
17
     *
18
     * @var integer
19
     */
20
    protected $id;
21
22
    /**
23
     * Year object
24
     *
25
     * @var object
26
     */
27
    public $year;
28
29
    /**
30
     * Account values
31
     *
32
     * @var array
33
     */
34
    public $value;
35
36
    /**
37
     * Error object
38
     *
39
     * @var object
40
     */
41
    public $error;
42
43
    /**
44
     * Vat percent
45
     *
46
     * @var float
47
     */
48
    public $vat_percent;
49
50
    /**
51
     * Direction of vat
52
     *
53
     * @var array
54
     */
55
    public $vat = array(
56
        0 => 'none',
57
        1 => 'in',
58
        2 => 'out'
59
    );
60
61
    // Disse b�r laves om til engelske termer med sm�t og s� overs�ttes
62
    // husk at �ndre tilsvarende i validForState() - Status b�r
63
    // splittes op i to konti (aktiver og passiver)
64
    // husk at opdatere databasen til alle sum-konti skal have nummer 5 i stedet
65
66
    public $types = array(
67
        1 => 'headline',
68
        2 => 'operating', // drift
69
        3 => 'balance, asset', // aktiv
70
        4 => 'balance, liability', // passiv
71
        5 => 'sum'
72
    );
73
74
    public $use = array(
75
        1 => 'none',
76
        2 => 'income',
77
        3 => 'expenses',
78
        4 => 'finance'
79
    );
80
81
    protected $db;
82
    protected $mdb2;
83
84
    /**
85
     * Constructor
86
     *
87
     * @param object  $year
88
     * @param integer $account_id
89
     *
90
     * @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...
91
     */
92 40
    function __construct($year, $account_id = 0)
93
    {
94 40
        $this->db = new DB_Sql;
95 40
        $this->mdb2 = MDB2::singleton(DB_DSN);
96 40
        if (PEAR::isError($this->mdb2)) {
97
            throw new Exception($this->mdb2->getMessage() . $this->mdb2->getUserInfo());
98
        }
99
100 40
        $this->error = new Intraface_Error;
101 40
        $this->year = $year;
102 40
        $this->id = (int)$account_id;
103
104 40
        $this->vatpercent = $this->year->kernel->getSetting()->get('intranet', 'vatpercent');
0 ignored issues
show
Bug introduced by
The property vatpercent does not seem to exist. Did you mean vat_percent?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
106 40
        if ($this->id > 0) {
107 17
            $this->load();
108 17
        }
109 40
    }
110
111
    /**
112
     * Finds an account from number
113
     *
114
     * @deprecated
115
     * @param integer $account_number
116
     *
117
     * @return object
118
     */
119
    public static function factory($year, $account_number)
120
    {
121
        $gateway = new Intraface_modules_accounting_AccountGateway($year);
122
        return $gateway->findFromNumber($account_number);
123
    }
124
125
    /**
126
     * Loads details about an account
127
     *
128
     * @return integer id
129
     */
130 30
    private function load()
131
    {
132 30
        if ($this->year->get('id') == 0 || $this->id == 0) {
133
            $this->value['id'] = 0;
134
            $this->id = 0;
135
            return 0;
136
        }
137
138
        $sql = "SELECT
139
                account.id,
140
                account.name,
141
                account.type_key,
142
                account.use_key,
143
                account.number,
144
                account.sum_from_account_number,
145
                account.sum_to_account_number,
146
                account.vat_key,
147
                account.vat_percent,
148
                account.primosaldo_debet,
149
                account.primosaldo_credit,
150
                account.active
151
            FROM
152
                accounting_account account
153 30
            WHERE account.id = " . $this->id . "
154 30
                AND account.intranet_id = ".$this->year->kernel->intranet->get('id'). "
155 30
                AND year_id = ".$this->year->get('id')."
156 30
            LIMIT 1";
157
158 30
        $this->db->query($sql);
159
160 30
        if ($this->db->nextRecord()) {
161 26
            $this->value['id'] = $this->db->f('id');
162 26
            $this->value['name'] = $this->db->f('name');
163
            //$this->value['comment'] = $this->db->f('comment');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
164 26
            $this->value['number'] = $this->db->f('number');
165 26
            $this->value['type_key'] = $this->db->f('type_key');
166 26
            $this->value['type'] = $this->types[$this->value['type_key']];
167 26
            $this->value['sum_from'] = $this->db->f('sum_from_account_number');
168 26
            $this->value['sum_to'] = $this->db->f('sum_to_account_number');
169 26
            $this->value['use_key'] = $this->db->f('use_key');
170 26
            $this->value['use'] = $this->use[$this->value['use_key']];
171 26
            $this->value['primosaldo_debet'] = $this->db->f('primosaldo_debet');
172 26
            $this->value['primosaldo_credit'] = $this->db->f('primosaldo_credit');
173 26
            $this->value['vat_key'] = $this->db->f('vat_key');
174 26
            $this->value['active'] = $this->db->f('active');
175
176
            // hvis der ikke er moms p� �ret skal alle momsindstillinger nulstilles
177 26
            if ($this->year->get('vat') == 0) {
178 16
                $this->value['vat_key'] = 0;
179 16
                $this->value['vat'] = $this->vat[$this->db->f('vat_key')];
180 16
                $this->value['vat_percent'] = 0;
181 16
                $this->value['vat_shorthand'] = 'ingen';
182 16
            } else { // hvis der er moms p� �ret
183 10
                $this->value['vat_key'] = $this->db->f('vat_key');
184 10
                $this->value['vat'] = $this->vat[$this->db->f('vat_key')];
185 10
                if ($this->value['vat'] == 'none') {
186 4
                    $this->value['vat_percent'] = 0;
187 4
                } else {
188 10
                    $this->value['vat_percent'] = $this->db->f('vat_percent');
189
                }
190
191 10
                if ($this->value['vat'] == 'in') {
192 10
                    $this->value['vat_account_id'] = $this->year->getSetting('vat_in_account_id');
193 10
                } elseif ($this->value['vat'] == 'out') {
194 4
                    $this->value['vat_account_id'] = $this->year->getSetting('vat_out_account_id');
195 4
                } else {
196 4
                    $this->value['vat_account_id'] = 0;
197
                }
198 10
                $this->value['vat_shorthand'] = $this->value['vat'];
199
            }
200 26
        }
201
202 30
        return $this->get('id');
203
    }
204
205
    /**
206
     * Updateds account
207
     *
208
     * @param array $var info about account
209
     *
210
     * @return integer
211
     */
212 26
    public function save($var)
213
    {
214 26
        $var = safeToDb($var);
215
216 26
        if (empty($var['sum_to'])) {
217 26
            $var['sum_to'] = '';
218 26
        }
219 26
        if (empty($var['sum_from'])) {
220 26
            $var['sum_from'] = '';
221 26
        }
222 26
        if (empty($var['vat_percent'])) {
223
            $var['vat_percent'] = 0;
224
        }
225 26
        if (!$this->isNumberFree($var['number'])) {
226 1
            $this->error->set('Du kan ikke bruge det samme kontonummer flere gange');
227 1
        }
228
229 26
        $validator = new Intraface_Validator($this->error);
230 26
        $validator->isNumeric($var['number'], 'Kontonummeret er ikke et tal');
231
232 26
        $validator->isNumeric($var['type_key'], 'Kontotypen er ikke rigtig');
233
234 26
        if (!array_key_exists($var['type_key'], $this->types)) {
235
            $this->error->set('Ikke en tilladt type');
236
        }
237
238 26
         $validator->isNumeric($var['use_key'], 'Det kan en konto ikke bruges til');
239
240 26
        if (!array_key_exists($var['use_key'], $this->use)) {
241
            $this->error->set('Ikke en tilladt brug af kontoen');
242
        }
243
244 26
        $validator->isString($var['name'], 'Kontonavnet kan kune v�re en tekststreng.');
245 26
        $validator->isNumeric($var['vat_key'], 'Ugyldig moms', 'allow_empty');
246 26
        $validator->isNumeric($var['sum_to'], 'sum_to', 'allow_empty');
247 26
        $validator->isNumeric($var['sum_from'], 'sum_from', 'allow_empty');
248
249 26
        settype($var['comment'], 'integer');
250 26
        $validator->isString($var['comment'], 'Error in comment', '', 'allow_empty');
251
252
253 26
        if ($this->error->isError()) {
254 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Account::save of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
255
        }
256
257 26 View Code Duplication
        if ($this->id > 0) {
258
            $sql_type = "UPDATE accounting_account ";
259
            $sql_end = " WHERE id = " . $this->id;
260
        } else {
261 26
            $sql_type = "INSERT INTO accounting_account ";
262 26
            $sql_end = ", date_created=NOW()";
263
        }
264
265
        $sql = $sql_type . "SET
266 26
            number = '".(int)$var['number']."',
267 26
            intranet_id = " . $this->year->kernel->intranet->get('id') . ",
268 26
            user_id = " . $this->year->kernel->user->get("id") . ",
269 26
            type_key='" . $var['type_key']."',
270 26
            year_id = " . $this->year->get('id').",
271 26
            use_key = '" . $var['use_key']."',
272 26
            name = '" . $var['name']."',
273 26
            comment = '" . $var['comment']."',
274 26
            vat_percent = '" . $var['vat_percent'] . "',
275 26
            sum_to_account_number = " . (int)$var['sum_to'] . ",
276 26
            sum_from_account_number = " . (int)$var['sum_from'] . ",
277
            date_changed = NOW(),
278 26
            vat_key=" . (int)$var['vat_key'] . " " . $sql_end;
279
280 26
        $this->db->query($sql);
281
282 26
        if ($this->id == 0) {
283 26
            $this->id = $this->db->insertedId();
284 26
        }
285
286 26
        if (!empty($var['created_from_id']) and is_numeric($var['created_from_id'])) {
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...
287
            $this->db->query("UPDATE accounting_account SET created_from_id = ".$var['created_from_id']." WHERE id = " . $this->id);
288
        }
289
290 26
        $this->load();
291
292 26
        return $this->id;
293
    }
294
295
    /**
296
     * Saves the primo saldo
297
     *
298
     * @param float $debet
299
     * @param float $credit
300
     *
301
     * @return boolean
302
     */
303 2
    public function savePrimosaldo($debet, $credit)
304
    {
305 2
        if ($this->id == 0) {
306
            return false;
307
        }
308
309 2
        $amount = new Intraface_Amount($debet);
310 2
        if (!$amount->convert2db()) {
311
            $this->error->set('Amount could not be converted');
312
        }
313 2
        $debet = $amount->get();
314
315 2
        $amount = new Intraface_Amount($credit);
316 2
        if (!$amount->convert2db()) {
317
            $this->error->set('Amount could not be converted');
318
        }
319 2
        $credit = $amount->get();
320
321
322 2
        $debet = (double)$debet;
323 2
        $credit = (double)$credit;
324
325 2
        $this->db->query("UPDATE accounting_account
326
            SET
327 2
                primosaldo_debet = '".$debet."',
328 2
                primosaldo_credit = '".$credit."'
329 2
                WHERE id = " . $this->id);
330 2
        return true;
331
    }
332
333
    /**
334
     * Deletes an account
335
     *
336
     * @todo Skal tjekke om der er poster i �ret p� kontoen.
337
     *
338
     * @return boolean
339
     */
340 1
    public function delete()
341
    {
342 1
        if ($this->anyPosts()) {
343
            $this->error->set('Der er poster på kontoen for dette år, så du kan ikke slette den. Næste år kan du lade være med at bogføre på kontoen, og så kan du slette den.');
344
            return false;
345
        }
346 1
        $this->getSaldo();
347 1
        if ($this->get('saldo') != 0) {
348
            $this->error->set('Der er registreret noget på primosaldoen på kontoen, så du kan ikke slette den. Du kan slette kontoen, hvis du nulstiller primosaldoen.');
349
            return false;
350
        }
351
352 1
        $this->db->query("UPDATE accounting_account SET active = 0, date_changed=NOW() WHERE intranet_id = " . $this->year->kernel->intranet->get('id') . " AND year_id = ".$this->year->get('id')." AND id = " . $this->id);
353 1
        $this->value['active'] = 0;
354 1
        return true;
355
    }
356
357
    /*************************************************************************************
358
     * VALIDERINGSFUNKTIONER
359
     ************************************************************************************/
360
361
    /**
362
     * Metoden tjekker om kontoen har den rigtige type, s� vi m� bogf�re p� den.
363
     *
364
     * @return boolean
365
     */
366 11
    public function validForState()
367
    {
368 11
        if ($this->id > 0) {
369 10
            if ($this->get('type_key') == array_search('operating', $this->types) or $this->get('type_key') == array_search('balance, asset', $this->types) or $this->get('type_key') == array_search('balance, liability', $this->types)) {
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...
370 10
                return true;
371
            }
372
        }
373 1
        return false;
374
    }
375
376 1
    public function getType()
377
    {
378 1
        return $this->types[$this->get('type_key')];
379
    }
380
381
    /**
382
     * Metode til at tjekke om kontonummeret er fri.
383
     *
384
     * @see save()
385
     */
386 26
    private function isNumberFree($account_number)
387
    {
388 26
        $account_number = (int)$account_number;
389
390
        $sql = "SELECT
391
                id
392
            FROM accounting_account
393 26
            WHERE number = " . $account_number . "
394 26
                AND intranet_id = " . $this->year->kernel->intranet->get('id') . "
395 26
                AND year_id = " .$this->year->get('id'). "
396 26
                AND id <> " . $this->id . " AND active = 1";
397 26
        $result = $this->mdb2->query($sql);
398 26
        if (PEAR::isError($result)) {
399
            throw new Exception('Error in query: '.$result->getUserInfo());
400
        }
401
402 26
        if ($result->numRows() == 0) {
403 26
            return true;
404
        }
405 1
        return false;
406
    }
407
408
    /*************************************************************************************
409
     * SALDOFUNKTIONER
410
     ************************************************************************************/
411
412
    /**
413
     * Public: Metoden returnerer primosaldoen for en konto
414
     *
415
     * @return (array) med debet, credit og total saldo
416
     */
417 5
    function getPrimoSaldo()
418
    {
419
        $sql = "SELECT primosaldo_debet, primosaldo_credit
420
            FROM accounting_account
421 5
            WHERE year_id = " . $this->year->get('id') . "
422 5
                AND id = ".$this->id . "
423
                AND active = 1
424 5
                AND intranet_id = ".$this->year->kernel->intranet->get('id');
425
426 5
        $this->db->query($sql);
427
428 5
        if (!$this->db->nextRecord()) {
429 3
            return array('debet' => 0, 'credit' => 0, 'saldo' => 0);
430
        }
431
432 2
        $primo['debet'] = $this->db->f('primosaldo_debet');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$primo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $primo = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
433 2
        $primo['credit'] = $this->db->f('primosaldo_credit');
434 2
        $primo['saldo'] = $primo['debet'] - $primo['credit'];
435
436 2
        return $primo;
437
    }
438
    /**
439
     * Public: Metoden returnerer en saldo for en konto
440
     *
441
     * Klassen tager h�jde for primobalancen og den skal ogs� tage h�jde for
442
     * sumkonti, se i f�rste omgang getSaldoList().
443
     *
444
     * Det vil v�re for voldsomt at putte
445
     * den her under get, for s� skal saldoen
446
     * udregnes hver gang jeg skal have fat i
447
     * et eller andet ved en konto!
448
     *
449
     * @param $date_from (date) yyyy-mm-dd Der s�ges jo kun i indev�rende �r
450
     * @param $date_to (date) yyyy-mm-dd   Der s�ges kun i indev�rende �r
451
     *
452
     * @return (array) med debet, credit og total saldo
453
     *
454
     *
455
     *
456
     */
457 40
    function getSaldo($type = 'stated', $date_from = '', $date_to = '')
458
    {
459 3
        if (empty($date_from)) {
460 3
            $date_from = $this->year->get('from_date');
461 3
        }
462 3
        if (empty($date_to)) {
463 3
            $date_to = $this->year->get('to_date');
464 3
        }
465
466 3
        $total_saldo = 0;
467
468
        $primo = array(
0 ignored issues
show
Unused Code introduced by
$primo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
469 3
            'debet' => '',
470 3
            'credit' => '',
471
            'saldo' => ''
472 3
        );
473
474
        // Tjekker p� om datoerne er i indev�rende �r
475
476
            // henter primosaldoen for kontoen
477 3
            $primo = $this->getPrimoSaldo();
478
            $sql = "SELECT
479
                    SUM(post.debet) AS debet_total,
480
                    SUM(post.credit) AS credit_total
481
                FROM accounting_post post
482
                INNER JOIN accounting_account account
483
                    ON account.id = post.account_id
484 40
                WHERE account.id = ".$this->id."
485 3
                    AND post.year_id = ".$this->year->get('id')."
486 3
                    AND post.intranet_id = ".$this->year->kernel->intranet->get('id')."
487 3
                    AND DATE_FORMAT(post.date, '%Y-%m-%d') >= '".$date_from."'
488 3
                    AND DATE_FORMAT(post.date, '%Y-%m-%d') <= '".$date_to."'
489 3
                    AND account.year_id = ".$this->year->get('id');
490 3
        if ($type == 'stated') {
491 40
            $sql .= ' AND post.stated = 1';
492 3
        } elseif ($type == 'draft') {
493
            $sql .= ' AND post.stated = 0';
494
        }
495
496 3
            $sql .= " GROUP BY post.account_id";
497
498 3
        if ($this->get('type_key') == array_search('sum', $this->types)) {
499
            $db2 = new DB_Sql;
500
            $sql = "SELECT id FROM accounting_account
501
                    WHERE number >= " . $this->get('sum_from') . "
502
                        AND type_key != ".array_search('sum', $this->types)."
503
                        AND number <= " . $this->get('sum_to') . "
504
                        AND year_id = ".$this->year->get('id')."
505
                        AND intranet_id = " . $this->year->kernel->intranet->get('id');
506
            $db2->query($sql);
507
            $total = 0;
508 View Code Duplication
            while ($db2->nextRecord()) {
509
                // $sub = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
510
                $sAccount = new Account($this->year, $db2->f('id'));
511
                $sAccount->getSaldo();
512
                $total = $total + $sAccount->get('saldo');
513
            }
514
            $this->value['saldo'] = $total;
515
            $total_saldo = $total_saldo + $total;
0 ignored issues
show
Unused Code introduced by
$total_saldo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
516
        } else {
517 3
            $this->db->query($sql);
518 3
            if (!$this->db->nextRecord()) {
519 3
                $this->value['debet'] = $primo['debet'];
520 3
                $this->value['credit'] = $primo['credit'];
521 3
                $this->value['saldo'] = $this->value['debet'] - $this->value['credit'];
522 3
            } else {
523
                if ($type == 'draft') {
524
                    $this->value['debet_draft'] = $this->db->f('debet_total');
525
                    $this->value['credit_draft'] = $this->db->f('credit_total');
526
                    $this->value['saldo_draft'] = $this->value['debet_draft'] - $this->value['credit_draft'];
527
                } else {
528
                    $this->value['debet'] = $primo['debet'] + $this->db->f('debet_total');
529
                    $this->value['credit'] = $primo['credit'] + $this->db->f('credit_total');
530
                    $this->value['saldo'] = $this->value['debet'] - $this->value['credit'];
531
                }
532
            }
533
            // Det her kan sikkert laves lidt smartere. Den skal egentlig laves inden
534
            // alt det ovenover tror jeg - alst� if-s�tningen
535
        }
536
537 3
            return true;
538
    }
539
540
    /***************************************************************************
541
     * �VRIGE METODER
542
     **************************************************************************/
543
544
    /**
545
     * Returnerer liste med alle kontoerne
546
     *
547
     * @param string  $type  Typen af konto, kig i Account::type;
548
     * @param boolean $saldo Whether to return the saldo
549
     *
550
     * @return array
551
     */
552 1
    public function getList($type = '', $saldo = false)
553
    {
554 1
        $gateway = new Intraface_modules_accounting_AccountGateway($this->year);
555 1
        return $gateway->getList($type, $saldo);
556
    }
557
558 2
    public function anyAccounts()
559
    {
560 2
        $gateway = new Intraface_modules_accounting_AccountGateway($this->year);
561 2
        return $gateway->anyAccounts();
562
    }
563
564 2
    public function anyPosts()
565
    {
566 2
        $this->db->query("SELECT
567
                id
568
            FROM accounting_post post
569 2
            WHERE (post.account_id = ". $this->id . ")
570 2
                AND intranet_id = ".$this->year->kernel->intranet->get('id')."
571 2
                AND year_id = " . $this->year->get('id') . "
572 2
                LIMIT 1");
573 2
        return $this->db->numRows();
574
    }
575
576 1
    public function getPosts()
577
    {
578 1
        $posts = array();
579
580 1
        if ($this->id == 0) {
581 1
            return $posts;
582
        }
583
        $this->db->query("SELECT
584
                    id,
585
                    date,
586
                    DATE_FORMAT(date, '%d-%m-%Y') AS dk_date,
587
                    voucher_id,
588
                    text,
589
                    debet,
590
                    credit
591
                FROM accounting_post post
592
                WHERE (post.account_id = ". $this->get('id') . ")
593
                    AND intranet_id = ".$this->year->kernel->intranet->get('id')."
594
                    AND year_id = " . $this->year->get('id') . "
595
                    AND stated = 1
596
                    ORDER BY date ASC, id ASC");
597
        $i = 1;
598
        while ($this->db->nextRecord()) {
599
            $posts[$i]['id'] = $this->db->f('id');
600
            $posts[$i]['dk_date'] = $this->db->f('dk_date');
601
            $posts[$i]['date'] = $this->db->f('date');
602
            $posts[$i]['voucher_id'] = $this->db->f('voucher_id');
603
            $voucher = new Voucher($this->year, $this->db->f('voucher_id'));
604
            $posts[$i]['voucher_number'] = $voucher->get('number');
605
            $posts[$i]['text'] = $this->db->f('text');
606
            $posts[$i]['debet'] = $this->db->f('debet');
607
            $posts[$i]['credit'] = $this->db->f('credit');
608
            //$posts[$i]['stated'] = $db2->f('stated');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
609
            //$posts[$i]['account_id'] = $db2->f('account_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
610
            $i++;
611
        } // while
612
        return $posts;
613
    }
614
615
    /**
616
     * Calculates the vat amount
617
     *
618
     * @link http://eforum.idg.se/viewmsg.asp?EntriesId=831525
619
     *
620
     * @param float $amount      Amount
621
     * @param float $vat_percent Vat percent
622
     *
623
     * @return float Vat amount
624
     */
625 4
    public function calculateVat($amount, $vat_percent)
626
    {
627 4
        $amount = (float)$amount;
628 4
        $vat_percent = (float)$vat_percent / 100;
629
630 4
        return $amount * ($vat_percent / (1 + $vat_percent));
631
    }
632
633
    public function getId()
634
    {
635
        return $this->id;
636
    }
637
638 1
    public function getNumber()
639
    {
640 1
        return $this->get('number');
641
    }
642
}
643