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

findFromNumber()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
crap 3.004
rs 9.0856
c 0
b 0
f 0
1
<?php
2
class Intraface_modules_accounting_AccountGateway
3
{
4
    private $year;
5
    private $types = array(
6
        1 => 'headline',
7
        2 => 'operating', // drift
8
        3 => 'balance, asset', // aktiv
9
        4 => 'balance, liability', // passiv
10
        5 => 'sum'
11
    );
12
13
    private $use = array(
14
        1 => 'none',
15
        2 => 'income',
16
        3 => 'expenses',
17
        4 => 'finance'
18
    );
19
20
    private $vat = array(
21
        0 => 'none',
22
        1 => 'in',
23
        2 => 'out'
24
    );
25
26
27 17
    function __construct($year)
28
    {
29 17
        $this->year = $year;
30 17
    }
31
32
   /**
33
     * Denne funktion bruges bl.a. under bogf�ringen, s� man bare taster kontonummer
34
     * og s� s�ttes den rigtige konto.
35
     *
36
     * @param integer $account_number
37
     *
38
     * @return object
39
     */
40 13
    public function findFromNumber($account_number)
41
    {
42 12
        $account_number = (int)$account_number;
43
44 12
        if ($this->year->get('id') == 0) {
45
            return 0;
46
        }
47
48
        $sql = "SELECT id FROM accounting_account
49 12
            WHERE number = '".$account_number."'
50 12
                AND intranet_id = ".$this->year->kernel->intranet->get('id')."
51 12
                AND year_id = ".$this->year->get('id')." AND active = 1
52 12
            LIMIT 1";
53
54 13
        $db = new Db_Sql;
55 12
        $db->query($sql);
56
57 12
        if (!$db->nextRecord()) {
58 1
            return new Account($this->year);
59
        }
60
61 11
        return new Account($this->year, $db->f('id'));
62
    }
63
64
    /**
65
     * @deprecated
66
     * @param $id
67
     * @return unknown_type
68
     */
69
    function findFromId($id)
70
    {
71
        return $this->findById($id);
72
    }
73
74 11
    function findById($id)
75
    {
76 11
        require_once dirname(__FILE__) . '/Account.php';
77 11
        return new Account($this->year, $id);
78
    }
79
80
    function findByType($type, $saldo = false)
81
    {
82
        return $this->getList($type, $saldo);
83
    }
84
85
    function getAll()
86
    {
87
        return $this->getList();
88
    }
89
90
    /**
91
     * Returns collection with all accounts
92
     *
93
     * @param string  $type  Typen af konto, kig i Account::type;
94
     * @param boolean $saldo Whether to return the saldo
95
     *
96
     * @return array
97
     */
98 1
    public function getList($type = '', $saldo = false)
99
    {
100 1
        $type = safeToDb($type);
101 1
        $type_sql = '';
102
103
        //if ($this->year->get('id') == 0 || $this->id == 0) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
104 1
        if ($this->year->get('id') == 0) {
105
            //$this->value['id'] = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
106
            //$this->id = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
107
            return array();
108
        }
109
110 1
        $db = new DB_Sql;
111 1
        if (!empty($type)) {
112
            switch ($type) {
113 1
                case 'expenses':
114
                        $type_sql = " AND use_key = '".array_search('expenses', $this->use)."'";
115
                    break;
116 1
                case 'income':
117
                        $type_sql = " AND use_key = '".array_search('income', $this->use)."'";
118
                    break;
119 1
                case 'finance':
120
                        $type_sql = " AND use_key = '".array_search('finance', $this->use)."'";
121
                    break;
122 1
                case 'balance':
123
                    // fall through
124 1
                case 'status':
125
                        $type_sql = " AND (type_key = '".array_search('balance, liability', $this->types)."' OR type_key = '".array_search('balance, asset', $this->types)."')";
126
                    break;
127 1
                case 'drift':
128
                    // fall through
129 1
                case 'operating':
130 1
                        $type_sql = " AND type_key = '".array_search('operating', $this->types)."'";
131 1
                    break;
132
                default:
133
                    break;
134
            }
135 1
        }
136 1
        $accounts = array();
137
        $sql = "SELECT
138
                    account.id,
139
                    account.number,
140
                    account.name,
141
                    account.type_key,
142
                    account.primosaldo_debet,
143
                    account.primosaldo_credit,
144
                    account.created_from_id,
145
                    account.vat_key,
146
                    account.sum_from_account_number,
147
                    account.sum_to_account_number
148
            FROM accounting_account account
149 1
            WHERE intranet_id = ".$this->year->kernel->intranet->get('id')." ".$type_sql."
150 1
                AND active = 1 AND year_id = ".$this->year->get('id')." ORDER BY number ASC";
151
152 1
        $db->query($sql);
153
154 1
        $i = 0;
155 1
        while ($db->nextRecord()) {
156
            $accounts[$i]['id'] = $db->f('id');
157
            $accounts[$i]['name'] = $db->f('name');
158
            $accounts[$i]['number'] = $db->f('number');
159
            $accounts[$i]['type_key'] = $db->f('type_key');
160
            $accounts[$i]['type'] = $this->types[$db->f('type_key')];
161
            $accounts[$i]['sum_from'] = $db->f('sum_from_account_number');
162
            $accounts[$i]['sum_to'] = $db->f('sum_to_account_number');
163
164
165
            $accounts[$i]['primosaldo_debet'] = $db->f('primosaldo_debet');
166
            $accounts[$i]['primosaldo_credit'] = $db->f('primosaldo_credit');
167
            $accounts[$i]['created_from_id'] = $db->f('created_from_id');
168
            $accounts[$i]['vat_shorthand'] = $this->vat[$db->f('vat_key')];
169
170
            if ($saldo === true) {
171
                $account = new Account($this->year, $db->f('id'));
172
                $account->getSaldo();
173
                $accounts[$i]['debet'] = $account->get('debet');
174
                $accounts[$i]['credit'] = $account->get('credit');
175
                $accounts[$i]['saldo'] = $account->get('saldo');
176
            }
177
178
179
            $i++;
180
        }
181 1
        return $accounts;
182
    }
183
184 2
    function anyAccounts()
185
    {
186 2
        $db = new DB_Sql;
187
        $sql = "SELECT id
188
            FROM accounting_account
189 2
            WHERE intranet_id = " . $this->year->kernel->intranet->get("id") . " AND year_id = ".$this->year->get('id')." AND active = 1";
190 2
        $db->query($sql);
191
192 2
        return $db->numRows();
193
    }
194
}
195