Intraface_modules_debtor_DebtorGateway::findAll()   F
last analyzed

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 178
Code Lines 89

Duplication

Lines 178
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 702

Importance

Changes 0
Metric Value
cc 26
eloc 89
nc 70632
nop 0
dl 178
loc 178
rs 2
c 0
b 0
f 0
ccs 0
cts 112
cp 0
crap 702

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
class Intraface_modules_debtor_DebtorGateway
3
{
4
    protected $kernel;
5
    protected $dbquery;
6
    public $error;
7
    protected $type;
8
9
    function __construct($kernel)
10
    {
11
        $this->kernel = $kernel;
12
        $this->error = new Intraface_Error;
13
    }
14
15
    function getType()
16
    {
17
        return $this->type;
18
    }
19
20
    /**
21
     * returns the possible debtor types!
22
     *
23
     * @return array types
24
     */
25
    static function getDebtorTypes()
26
    {
27
        return array(
28
            1 => 'quotation',
29
            2 => 'order',
30
            3 => 'invoice',
31
            4 => 'credit_note');
32
    }
33
34
    function setType($type)
35
    {
36
        $this->type = $type;
37
    }
38
39
    function getTypeKey()
40
    {
41
        return array_search($this->type, $this->getDebtorTypes());
42
    }
43
44 View Code Duplication
    function getDBQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if ($this->dbquery) {
47
            return $this->dbquery;
48
        }
49
50
        $this->dbquery = new Intraface_DBQuery($this->kernel, "debtor", "debtor.active = 1 AND debtor.intranet_id = ".$this->kernel->intranet->get("id"));
51
        $this->dbquery->setJoin("LEFT", "contact", "debtor.contact_id = contact.id AND contact.intranet_id = ".$this->kernel->intranet->get("id"), '');
52
        $this->dbquery->setJoin("LEFT", "address", "address.belong_to_id = contact.id AND address.active = 1 AND address.type = 3", '');
53
        $this->dbquery->setJoin("LEFT", "debtor_item", "debtor_item.debtor_id = debtor.id AND debtor_item.active = 1 AND debtor_item.intranet_id = ".$this->kernel->intranet->get("id"), '');
54
55
        $this->dbquery->useErrorObject($this->error);
56
57
        return $this->dbquery;
58
    }
59
60
    /**
61
     * Bruges til at lave en menu p� kontakten eller produktet
62
     *
63
     * @param string  $type    contact eller product
0 ignored issues
show
Bug introduced by
There is no parameter named $type. 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...
64
     * @param integer $type_id id p� contact eller product.
0 ignored issues
show
Bug introduced by
There is no parameter named $type_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...
65
     *
66
     * @return integer
67
     */
68
    public function findCountByContactId($contact_id)
69
    {
70
                $sql = "SELECT id
71
                FROM debtor
72
                    WHERE intranet_id = " . $this->kernel->intranet->get("id") . "
73
                        AND contact_id = ".(int)$contact_id."
74
              AND type='".$this->type_key."'
0 ignored issues
show
Bug introduced by
The property type_key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
              AND active = 1";
76
77
        $db = new DB_Sql;
78
        $db->query($sql);
79
        return $db->numRows();
80
    }
81
82
    function setNewContactId($old_contact_id, $new_contact_id)
83
    {
84
        $db = new DB_Sql;
85
        $db->query('UPDATE debtor SET contact_id = ' . $new_contact_id . ' WHERE contact_id = ' . $old_contact_id);
86
        return true;
87
    }
88
89 View Code Duplication
    function anyNew()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $db = new DB_Sql;
92
        $db->query('SELECT * FROM debtor WHERE date_created >=
93
        	DATE_SUB(NOW(),INTERVAL 1 DAY)
94
        	AND type = ' .$this->type_key . ' AND intranet_id = ' .$this->kernel->intranet->get('id'));
95
        return $db->numRows();
96
    }
97
98
    function findById($id)
99
    {
100
        if (is_int($id) && $id != 0) {
101
            $types = self::getDebtorTypes();
102
103
            $db = new DB_Sql;
104
            $db->query("SELECT type FROM debtor WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$id);
105
            if ($db->nextRecord()) {
106
                $type = $types[$db->f("type")];
107
            } else {
108
                throw new Exception("Invalid id for debtor in Debtor::factory");
109
            }
110
        } elseif (is_string($id) && $id != '') {
111
            $types = self::getDebtorTypes();
112
113
            $db = new DB_Sql;
114
            $db->query("SELECT type, id FROM debtor WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND identifier_key = \"".$id."\"");
115
            if ($db->nextRecord()) {
116
                $type = $types[$db->f("type")];
117
                $id = $db->f("id");
118
            } else {
119
                throw new Exception("Invalid identifier_key for debtor in Debtor::factory");
120
            }
121
        }
122
123
        switch ($type) {
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
124 View Code Duplication
            case "quotation":
125
                $this->kernel->useModule("quotation");
126
                $object = new Quotation($this->kernel, intval($id));
127
                return $object;
128
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
129
130 View Code Duplication
            case "order":
131
                $this->kernel->useModule("order");
132
                $object = new Order($this->kernel, intval($id));
133
                break;
134
135 View Code Duplication
            case "invoice":
136
                $this->kernel->useModule("invoice");
137
                $object = new Invoice($this->kernel, intval($id));
138
                break;
139
140
            case "credit_note":
141
                $this->kernel->useModule("invoice");
142
                $object = new CreditNote($this->kernel, intval($id));
143
                break;
144
145
            default:
146
                throw new Exception("Ugyldig type: '".$type."'");
147
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
148
        }
149
150
        return $object;
151
    }
152
153 View Code Duplication
    function findByIdentifier($identifier)
0 ignored issues
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        if (is_int($id) && $id != 0) {
0 ignored issues
show
Bug introduced by
The variable $id seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
156
            $types = self::getDebtorTypes();
157
158
            $db = new DB_Sql;
159
            $db->query("SELECT type FROM debtor WHERE intranet_id = ".$kernel->intranet->get('id')." AND id = ".$id);
0 ignored issues
show
Bug introduced by
The variable $kernel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $id seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
160
            if ($db->nextRecord()) {
161
                $type = $types[$db->f("type")];
162
            } else {
163
                throw new Exception("Invalid id for debtor in Debtor::factory");
164
            }
165
        } elseif (is_string($id) && $id != '') {
0 ignored issues
show
Bug introduced by
The variable $id seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
166
            $types = self::getDebtorTypes();
167
168
            $db = new DB_Sql;
169
            $db->query("SELECT type, id FROM debtor WHERE intranet_id = ".$kernel->intranet->get('id')." AND identifier_key = \"".$id."\"");
0 ignored issues
show
Bug introduced by
The variable $id seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
170
            if ($db->nextRecord()) {
171
                $type = $types[$db->f("type")];
172
                $id = $db->f("id");
173
            } else {
174
                throw new Exception("Invalid identifier_key for debtor in Debtor::factory");
175
            }
176
        }
177
178
        switch ($type) {
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
179
            case "quotation":
180
                $kernel->useModule("quotation");
181
                $object = new Quotation($kernel, intval($id));
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
182
                return $object;
183
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
184
185
            case "order":
186
                $kernel->useModule("order");
187
                $object = new Order($kernel, intval($id));
188
                break;
189
190
            case "invoice":
191
                $kernel->useModule("invoice");
192
                $object = new Invoice($kernel, intval($id));
193
                break;
194
195
            case "credit_note":
196
                $kernel->useModule("invoice");
197
                $object = new CreditNote($kernel, intval($id));
198
                break;
199
200
            default:
201
                throw new Exception("Ugyldig type: '".$type."'");
202
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
203
        }
204
205
        return $object;
206
    }
207
208
    /**
209
     * Gets a list with debtors
210
     *
211
     * @return array
212
     */
213 View Code Duplication
    public function findAll()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
    {
215
        $db = new DB_Sql;
0 ignored issues
show
Unused Code introduced by
$db 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...
216
217
        $this->dbquery = $this->getDBQuery();
218
219
        $this->dbquery->setCondition("debtor.type = ".$this->getTypeKey());
220
221
        if ($this->dbquery->checkFilter("contact_id")) {
222
            $this->dbquery->setCondition("debtor.contact_id = ".intval($this->dbquery->getFilter("contact_id")));
223
        }
224
225
        if ($this->dbquery->checkFilter("text")) {
226
            $this->dbquery->setCondition("(debtor.description LIKE \"%".$this->dbquery->getFilter("text")."%\" OR debtor.girocode = \"".$this->dbquery->getFilter("text")."\" OR debtor.number = \"".$this->dbquery->getFilter("text")."\" OR address.name LIKE \"%".$this->dbquery->getFilter("text")."%\")");
227
        }
228
229
        if ($this->dbquery->checkFilter("product_id")) {
230
            $this->dbquery->setCondition("debtor_item.product_id = ".$this->dbquery->getFilter('product_id'));
231
            if ($this->dbquery->checkFilter("product_variation_id")) {
232
                $this->dbquery->setCondition("debtor_item.product_variation_id = ".$this->dbquery->getFilter('product_variation_id'));
233
            } else {
234
                $this->dbquery->setCondition("debtor_item.product_variation_id = 0");
235
            }
236
        }
237
238
        if ($this->dbquery->checkFilter("date_field")) {
239
            if (in_array($this->dbquery->getFilter("date_field"), array('this_date', 'date_created', 'date_sent', 'date_executed', 'data_cancelled'))) {
240
                $date_field = $this->dbquery->getFilter("date_field");
241
            } else {
242
                $this->error->set("Ugyldigt datointerval felt");
243
            }
244
        } else {
245
            $date_field = 'this_date';
246
        }
247
248
        if ($this->dbquery->checkFilter("from_date")) {
249
            $date = new Intraface_Date($this->dbquery->getFilter("from_date"));
250
            if ($date->convert2db()) {
251
                $this->dbquery->setCondition("debtor.".$date_field." >= \"".$date->get()."\"");
0 ignored issues
show
Bug introduced by
The variable $date_field does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
252
            } else {
253
                $this->error->set("Fra dato er ikke gyldig");
254
            }
255
        }
256
257
        // Poster med fakturadato f�r slutdato.
258
        if ($this->dbquery->checkFilter("to_date")) {
259
            $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
260
            if ($date->convert2db()) {
261
                $this->dbquery->setCondition("debtor.".$date_field." <= \"".$date->get()."\"");
262
            } else {
263
                $this->error->set("Til dato er ikke gyldig");
264
            }
265
        }
266
        // alle ikke bogf�rte skal findes
267
        if ($this->dbquery->checkFilter("not_stated")) {
268
            $this->dbquery->setCondition("voucher_id = 0");
269
        }
270
271
        if ($this->dbquery->checkFilter("status")) {
272
            if ($this->dbquery->getFilter("status") == "-1") {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
273
                // Beh�ves ikke, den tager alle.
274
                // $this->dbquery->setCondition("status >= 0");
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
275
            } elseif ($this->dbquery->getFilter("status") == "-2") {
276
                // Not executed = �bne
277
                if ($this->dbquery->checkFilter("to_date")) {
278
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
279
                    if ($date->convert2db()) {
280
                        // Poster der er executed eller cancelled efter dato, og sikring at executed stadig er det, da faktura kan s�ttes tilbage.
281
                        $this->dbquery->setCondition("(debtor.date_executed >= \"".$date->get()."\" AND debtor.status = 2) OR (debtor.date_cancelled >= \"".$date->get()."\") OR debtor.status < 2");
282
                    }
283
                } else {
284
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
285
                    $this->dbquery->setCondition("debtor.status < 2");
286
                }
287
            } elseif ($this->dbquery->getFilter("status") == "-3") {
288
                //  Afskrevne. Vi tager f�rst alle sendte og executed.
289
290
                if ($this->get("type") != "invoice") {
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Intraface_modules_debtor_DebtorGateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
291
                    throw new Exception("Afskrevne kan kun benyttes ved faktura");
292
                }
293
294
                $this->dbquery->setJoin("INNER", "invoice_payment", "invoice_payment.payment_for_id = debtor.id", "invoice_payment.intranet_id = ".$this->kernel->intranet->get("id")." AND invoice_payment.payment_for = 1");
295
                $this->dbquery->setCondition("invoice_payment.type = -1");
296
297
                if ($this->dbquery->checkFilter("to_date")) {
298
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
299
                    if ($date->convert2db()) {
300
                        // alle som er sendte p� datoen og som ikke er cancelled
301
                        $this->dbquery->setCondition("debtor.date_sent <= '".$date->get()."' AND debtor.status != 3");
302
                        $this->dbquery->setCondition("invoice_payment.payment_date <= '".$date->get()."'");
303
                    }
304
                } else {
305
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
306
                    $this->dbquery->setCondition("status = 1 OR status = 2");
307
                }
308
            } else {
309
                $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
310
311
                /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
312
                // New date_field handles this instead
313
                switch ($this->dbquery->getFilter("status")) {
314
                    case "0":
315
                        $to_date_field = "date_created";
316
                        break;
317
318
                    case "1":
319
                        $to_date_field = "date_sent";
320
                        break;
321
322
                    case "2":
323
                        $to_date_field = "date_executed";
324
                        break;
325
326
                    case "3":
327
                        $to_date_field = "data_cancelled";
328
                        break;
329
                }
330
331
                if ($this->dbquery->checkFilter("to_date")) {
332
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
333
                    if ($date->convert2db()) {
334
                        // This gives a problem: We have an invoice created 20/4 and is executed 5/5
335
                        // If we make a search: executed 1/4-30/4 the above invoice will not be calculated in with date search below.
336
                        // And if we make a search : executed 1/5-30/5 it will not even be included in that search.
337
                        // Why was this made in that way?
338
                        // $this->dbquery->setCondition("debtor.".$to_date_field." <= \"".$date->get()."\"");
339
                        // So instead we add this normal status search: Changed 12/7 2009 /Sune
340
                        $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
341
                    }
342
                } else {
343
                    // tager dem som p� nuv�rende tidspunkt har den angivet status
344
                    $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
345
                }
346
                */
347
            }
348
        }
349
350
        switch ($this->dbquery->getFilter("sorting")) {
351
            case 1:
352
                $this->dbquery->setSorting("debtor.number ASC");
353
                break;
354
            case 2:
355
                $this->dbquery->setSorting("contact.number ASC");
356
                break;
357
            case 3:
358
                $this->dbquery->setSorting("address.name ASC");
359
                break;
360
            default:
361
                $this->dbquery->setSorting("debtor.number DESC");
362
        }
363
364
        $db = $this->dbquery->getRecordset("DISTINCT(debtor.id)", "", false);
365
        $i = 0;
366
        $list = array();
367
368
        while ($db->nextRecord()) {
369
            $debtor = $this->findById((int)$db->f("id"));
370
            $list[$i] = $debtor->get();
371
372
            // $contact = new Contact($this->kernel, $db->f('contact_id'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
373
            if (is_object($debtor->contact->address)) {
374
                $list[$i]['contact'] = $debtor->contact->get();
375
                $list[$i]['contact']['address'] = $debtor->contact->address->get();
376
377
                // f�lgende skal v�k
378
                $list[$i]['contact_id'] = $debtor->contact->get('id');
379
                $list[$i]['name'] = $debtor->contact->address->get('name');
380
                $list[$i]['address'] = $debtor->contact->address->get('address');
381
                $list[$i]['postalcode'] = $debtor->contact->address->get('postcode');
382
                $list[$i]['city'] = $debtor->contact->address->get('city');
383
            }
384
            $debtor->destruct();
385
            unset($debtor);
386
            $i++;
387
        }
388
        unset($db);
389
        return $list;
390
    }
391
392
    /**
393
     * Funktion til at finde ud af, om der er oprettet nogen poster af den aktuelle bruger
394
     *
395
     * @return integer
396
     */
397
    public function isFilledIn()
398
    {
399
        $db = new DB_Sql;
400
        $db->query("SELECT id FROM debtor WHERE type = " . $this->getTypeKey() . " AND intranet_id = " . $this->kernel->intranet->get('id'));
401
        return $db->numRows();
402
    }
403
}
404