Reminder::getList()   F
last analyzed

Complexity

Conditions 21
Paths 4320

Size

Total Lines 101
Code Lines 64

Duplication

Lines 101
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 462

Importance

Changes 0
Metric Value
cc 21
eloc 64
nc 4320
nop 0
dl 101
loc 101
rs 2
c 0
b 0
f 0
ccs 0
cts 75
cp 0
crap 462

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
/**
3
 * @package Intraface_Invoice
4
 */
5
class Reminder extends Intraface_Standard
6
{
7
    public $id;
8
    public $kernel;
9
    public $value;
10
    public $contact;
11
    private $db;
12
    public $item;
13
    public $error;
14
    public $dbquery;
15
16 10 View Code Duplication
    function __construct($kernel, $id = 0)
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...
17 10
    {
18 10
        $this->id     = intval($id);
19 10
        $this->kernel = $kernel;
20
21 10
        $this->db     = new DB_Sql;
22 10
        $this->error  = new Intraface_Error;
23
24 10
        if ($this->id) {
25
            $this->load();
26
        }
27 10
    }
28
29 8 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...
30
    {
31
        if ($this->dbquery) {
32 8
            return $this->dbquery;
33
        }
34
        $this->dbquery = new Intraface_DBQuery($this->kernel, "invoice_reminder", "intranet_id = ".$this->kernel->intranet->get("id")." AND active = 1");
35
        $this->dbquery->useErrorObject($this->error);
36
        return $this->dbquery;
37
    }
38
39
    function getItems($type = null)
40
    {
41
        $this->loadItem();
42
        return $this->item->getList($type);
43
    }
44
45 8
    function load()
46
    {
47 8
        $this->db->query("SELECT *,
48
                DATE_FORMAT(this_date, '%d-%m-%Y') AS dk_this_date,
49
                DATE_FORMAT(due_date, '%d-%m-%Y') AS dk_due_date,
50
                DATE_FORMAT(date_sent, '%d-%m-%Y') AS dk_date_sent,
51
                DATE_FORMAT(date_executed, '%d-%m-%Y') AS dk_date_executed,
52
                DATE_FORMAT(date_cancelled, '%d-%m-%Y') AS dk_date_cancelled
53 8
            FROM invoice_reminder WHERE id = ".$this->id." AND intranet_id = ".$this->kernel->intranet->get("id")."");
54 8 View Code Duplication
        if (!$this->db->nextRecord()) {
55 1
            $this->id = 0;
56 1
            $this->value['id'] = 0;
57 1
            return false;
58
        }
59
60 8
        $this->value["id"] = $this->db->f("id");
61 8
        $this->value["invoice_id"] = $this->db->f("invoice_id");
62 8
        $this->value["intranet_id"] = $this->db->f("intranet_id");
63 8
        $this->value["intranet_address_id"] = $this->db->f("intranet_address_id");
64 8
        $this->value["contact_id"] = $this->db->f("contact_id");
65 8
        $this->value["contact_address_id"] = $this->db->f("contact_address_id");
66 8
        $this->value["contact_person_id"] = $this->db->f("contact_person_id");
67 8
        $this->value["user_id"] = $this->db->f("user_id");
68 8
        $status_types = $this->getStatusTypes();
69 8
        $this->value["status"] = $status_types[$this->db->f("status")]; // skal laves om til db->f('status_key')
70 8
        $this->value["status_id"] = $this->db->f("status"); // skal slettes i n�ste version
71 8
        $this->value["status_key"] = $this->db->f("status");
72 8
        $this->value["this_date"] = $this->db->f("this_date");
73 8
        $this->value["dk_this_date"] = $this->db->f("dk_this_date");
74 8
        $this->value["due_date"] = $this->db->f("due_date");
75 8
        $this->value["dk_due_date"] = $this->db->f("dk_due_date");
76 8
        $this->value["dk_date_sent"] = $this->db->f("dk_date_sent");
77 8
        $this->value["dk_date_executed"] = $this->db->f("dk_date_executed");
78 8
        $this->value["date_stated"] = $this->db->f("date_stated");
79 8
        $this->value["voucher_id"] = $this->db->f("voucher_id");
80 8
        $this->value["description"] = $this->db->f("description");
81 8
        $this->value["number"] = $this->db->f("number");
82 8
        $this->value["payment_method_key"] = $this->db->f("payment_method");
83 8
        $payment_methods = $this->getPaymentMethods();
84 8
        $this->value["payment_method"] = $payment_methods[$this->db->f("payment_method")];
85 8
        $this->value["reminder_fee"] = $this->db->f("reminder_fee");
86
        // @todo should calculate the total amount of everything on the reminder
87 8
        $this->value["total"] = $this->db->f("reminder_fee");
88 8
        $this->value["text"] = $this->db->f("text");
89 8
        $this->value["girocode"] = $this->db->f("girocode");
90 8
        $this->value["send_as"] = $this->db->f("send_as");
91
92 8
        $this->contact = new Contact($this->kernel, $this->db->f("contact_id"), $this->db->f("contact_address_id"));
0 ignored issues
show
Unused Code introduced by
The call to Contact::__construct() has too many arguments starting with $this->db->f('contact_address_id').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93 8 View Code Duplication
        if ($this->contact->get("type") == "corporation" && $this->db->f("contact_person_id") != 0) {
94
            $this->contact_person = new ContactPerson($this->contact, $this->db->f("contact_person_id"));
0 ignored issues
show
Bug introduced by
The property contact_person does not seem to exist. Did you mean contact?

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...
95 2
        }
96
97 8 View Code Duplication
        if ($this->get("status") == "executed" || $this->get("status") == "cancelled") {
98
            $this->value["locked"] = true;
99
        } else {
100 8
            $this->value["locked"] = false;
101
        }
102
103 8
        $this->value['payment_total'] = 0;
104 8
        foreach ($this->getDebtorAccount()->getList() as $payment) {
105
            $this->value['payment_total'] += $payment["amount"];
106 8
        }
107 8
        $this->value["arrears"] = $this->value['total'] - $this->value['payment_total'];
108
109 8
        return true;
110
    }
111
112 8
    function loadItem($id = 0)
113
    {
114 8
        require_once 'Intraface/modules/invoice/ReminderItem.php';
115 8
        $this->item = new ReminderItem($this, (int)$id);
116 8
    }
117
118 View Code Duplication
    function getMaxNumber()
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...
119
    {
120
        $this->db->query("SELECT MAX(number) AS max_number FROM invoice_reminder WHERE intranet_id = ".$this->kernel->intranet->get("id"));
121
        $this->db->nextRecord(); // Hvis der ikke er nogle poster er dette bare den f�rste
122
        $number = $this->db->f("max_number");
123
        return $number;
124
    }
125
126 8 View Code Duplication
    function isNumberFree($number)
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...
127
    {
128 8
        $sql = "SELECT id FROM invoice_reminder WHERE number = ".intval($number)." AND id != ".$this->id . " AND intranet_id = " . $this->kernel->intranet->get('id');
129 8
        $this->db->query($sql);
130 8
        if ($this->db->nextRecord()) {
131
            return false;
132
        } else {
133 8
            return true;
134
        }
135
    }
136
137 9
    function save($input)
138
    {
139 9
        if ($this->get("locked") == 1) {
140
            return(false);
141
        }
142
143 9
        if (!isset($input['payment_method_key'])) {
144 9
            $input['payment_method_key'] = 0;
145 9
        }
146
147 9
        if (!is_array($input)) {
148
            throw new Exception("Input er ikke et array");
149
        }
150
151 9
        $input = safeToDb($input);
152
153 9
        $validator = new Intraface_Validator($this->error);
154
155 9
        if (!isset($input['number'])) {
156 1
            $input['number'] = 0;
157 1
        }
158 9
        if ($validator->isNumeric($input["number"], "Rykkernummer skal v�re et tal st�rre end nul", "greater_than_zero")) {
159 8
            if (!$this->isNumberFree($input["number"])) {
160
                $this->error->set("Rykkernummer er allerede brugt");
161
            }
162 8
        }
163
164 9
        if (!isset($input['contact_id'])) {
165 1
            $input['contact_id'] = 0;
166 1
        }
167 9
        if (!isset($input["contact_person_id"])) {
168 9
            $input["contact_person_id"] = 0;
169 9
        }
170 9
        if ($validator->isNumeric($input["contact_id"], "Du skal angive en kunde", "greater_than_zero")) {
171 8
            $contact = new Contact($this->kernel, (int)$input["contact_id"]);
172 8 View Code Duplication
            if (is_object($contact->address)) {
173 8
                $contact_id = $contact->get("id");
174 8
                $contact_address_id = $contact->address->get("address_id");
175 8
            } else {
176
                $this->error->set("Ugyldig kunde");
177
            }
178
179 8
            if ($contact->get("type") == "corporation") {
180
                $validator->isNumeric($input["contact_person_id"], "Der er ikke angivet en kontaktperson");
181
            }
182 8
        }
183
184
        // $validator->isString($input["attention_to"], "Fejl i att.", "", "allow_empty");
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...
185 9
        if (!isset($input['description'])) {
186 1
            $input['description'] = '';
187 1
        }
188 9
        $validator->isString($input["description"], "Fejl i beskrivelsen", "", "allow_empty");
189
190 9
        if (!isset($input['this_date'])) {
191 1
            $input['this_date'] = '';
192 1
        }
193 9
        if ($validator->isDate($input["this_date"], "Ugyldig dato", "allow_no_year")) {
194 8
            $this_date = new Intraface_Date($input["this_date"]);
195 8
            $this_date->convert2db();
196 8
        }
197
198 9
        if (!isset($input['due_date'])) {
199 1
            $input['due_date'] = '';
200 1
        }
201 9
        if ($validator->isDate($input["due_date"], "Ugyldig forfaldsdato", "allow_no_year")) {
202 8
            $due_date = new Intraface_Date($input["due_date"]);
203 8
            $due_date->convert2db();
204 8
        }
205
206 9
        if (!isset($input['reminder_fee'])) {
207 5
            $input['reminder_fee'] = 0;
208 5
        }
209 9
        $validator->isNumeric($input["reminder_fee"], "Rykkerbebyr skal v�re et tal");
210 9
        if (!isset($input['text'])) {
211 9
            $input['text'] = '';
212 9
        }
213 9
        $validator->isString($input["text"], "Fejl i teksten", "<b><i>", "allow_empty");
214 9
        if (!isset($input['send_as'])) {
215 1
            $input['send_as'] = '';
216 1
        }
217 9
        $validator->isString($input["send_as"], "Ugyldig m�de at sende rykkeren p�");
218
219 9
        if (!isset($input['payment_method_key'])) {
220
            $input['payment_method_key'] = 0;
221
        }
222 9
        $validator->isNumeric($input["payment_method_key"], "Du skal angive en betalingsmetode");
223 9
        if (!isset($input['girocode'])) {
224 9
            $input['girocode'] = '';
225 9
        }
226 9
        if ($input["payment_method_key"] == 3) {
227
            $validator->isString($input["girocode"], "Du skal udfylde girokode");
228
        } else {
229 9
            $validator->isString($input["girocode"], "Ugyldig girokode", "", "allow_empty");
230
        }
231
232 9
        if (!isset($input['checked_invoice'])) {
233 1
            $input['checked_invoice'] = array();
234 1
        }
235 9
        if (!is_array($input["checked_invoice"]) || count($input["checked_invoice"]) == 0) {
236 1
            $this->error->set("Der er ikke valgt nogle fakturaer til rykkeren");
237 1
        }
238
239 9
        if ($this->error->isError()) {
240 1
            return(false);
241
        }
242
243 8
        $sql = "intranet_address_id = ".$this->kernel->intranet->address->get("address_id").",
244 8
            number = ".$input["number"].",
245 8
            contact_id = ".$contact_id.",
0 ignored issues
show
Bug introduced by
The variable $contact_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...
246 8
            contact_address_id = ".$contact_address_id.",
0 ignored issues
show
Bug introduced by
The variable $contact_address_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...
247 8
            contact_person_id = ".$input['contact_person_id'].",
248 8
            description = \"".$input["description"]."\",
249 8
            this_date = \"".$this_date->get()."\",
0 ignored issues
show
Bug introduced by
The variable $this_date 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...
250 8
           due_date = \"".$due_date->get()."\",
0 ignored issues
show
Bug introduced by
The variable $due_date 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...
251 8
            reminder_fee = ".$input["reminder_fee"].",
252 8
            text = \"".$input["text"]."\",
253 8
            send_as = \"".$input["send_as"]."\",
254 8
            payment_method = ".$input["payment_method_key"].",
255 8
            girocode = \"".$input["girocode"]."\",
256 8
            date_changed = NOW()";
257
258
        // attention_to = \"".$input["attention_to"]."\",
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
259 8
        if ($this->id) {
260
            $this->db->query("UPDATE invoice_reminder SET ".$sql." WHERE id = ".$this->id);
261
            $this->load();
262
        } else {
263 8
            $this->db->query("INSERT INTO invoice_reminder SET ".$sql.", intranet_id = ".$this->kernel->intranet->get("id").", date_created = NOW(), user_id = ".$this->kernel->user->get("id"));
264 8
            $this->id = $this->db->insertedId();
265 8
            $this->load();
266
        }
267
268 8
        $this->loadItem();
269 8
        $this->item->clear();
270
271 8 View Code Duplication
        if (isset($input["checked_invoice"]) && is_array($input["checked_invoice"])) {
272 8
            foreach ($input["checked_invoice"] as $invoice_id) {
273 8
                $this->loadItem();
274 8
                $this->item->save(array("invoice_id" => $invoice_id));
275 8
            }
276 8
        }
277
278 8 View Code Duplication
        if (isset($input["checked_reminder"]) && is_array($input["checked_reminder"])) {
279
            foreach ($input["checked_reminder"] as $reminder_id) {
280
                $this->loadItem();
281
                $this->item->save(array("reminder_id" => $reminder_id));
282
            }
283
        }
284
285 8
        return true;
286
    }
287
288 1
    function delete()
289
    {
290 1
        $this->db->query("UPDATE invoice_reminder SET active = 0 WHERE id = ".$this->id." AND intranet_id = ".$this->kernel->intranet->get("id"));
291 1
        $this->id = 0;
292 1
        $this->load();
293 1
        return true;
294
    }
295
296
    /**
297
     * Sets status for the reminder
298
     *
299
     * @return true / false
300
     */
301 3
    function setStatus($status)
302
    {
303
304 3 View Code Duplication
        if (is_string($status)) {
305 3
            $status_id = array_search($status, $this->getStatusTypes());
306 3
            if ($status_id === false) {
307
                throw new Exception("Reminder->setStatus(): Ugyldig status (streng)", FATAL);
308
            }
309 3
        } else {
310
            $status_id = intval($status);
311
            $status_types = $this->getStatusTypes();
312
            if (isset($status_types[$status_id])) {
313
                $status = $status_types[$status];
314
            } else {
315
                throw new Exception("Reminder->setStatus(): Ugyldig status (integer)");
316
            }
317
        }
318
319 3
        if ($status_id <= $this->get("status_id")) {
320
            throw new Exception("Tried to set status the same or lower than it was before. Can be because of reload. In Reminder->setStatus");
321
        }
322
323 View Code Duplication
        switch ($status) {
324 3
            case "sent":
325 3
                $sql = "date_sent = NOW()";
326 3
                break;
327
328
            case "executed":
329
                $sql = "date_executed = NOW()";
330
                break;
331
332
            case "cancelled":
333
                $sql = "date_cancelled = NOW()";
334
                break;
335
336
            default:
337
                throw new Exception("Dette kan ikke lade sig g�re! Reminder->setStatus()", FATAL);
338
        }
339
340 3
        $db = new Db_Sql;
341 3
        $db->query("UPDATE invoice_reminder SET status = ".$status_id.", ".$sql."  WHERE id = " . $this->id . " AND intranet_id = " . $this->kernel->intranet->get('id'));
342 3
        $this->load();
343 3
        return true;
344
    }
345
346
    function updateStatus()
347
    {
348
349
        if ($this->get("arrears") == 0 && $this->get("status") == "sent") {
350
            $this->setStatus("executed");
351
        }
352
        return true;
353
    }
354
355
    /**
356
     * returns DebtorAccount object
357
     *
358
     * @return object DebtorAccount
359
     */
360 8
    public function getDebtorAccount()
361
    {
362 8
        require_once 'Intraface/modules/invoice/DebtorAccount.php';
363 8
        return new DebtorAccount($this);
364
    }
365
366 View Code Duplication
    function getList()
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...
367
    {
368
        $this->dbquery->setSorting("number DESC, this_date DESC");
369
        $i = 0;
370
371
        if ($this->dbquery->checkFilter("contact_id")) {
372
            $this->dbquery->setCondition("contact_id = ".intval($this->dbquery->getFilter("contact_id")));
373
        }
374
375
        if ($this->dbquery->checkFilter("invoice_id")) {
376
            $this->dbquery->setCondition("invoice_id = ".intval($this->dbquery->getFilter("invoice_id")));
377
        }
378
379
        if ($this->dbquery->checkFilter("text")) {
380
            $this->dbquery->setCondition("(description LIKE \"%".$this->dbquery->getFilter("text")."%\" OR girocode = \"".$this->dbquery->getFilter("text")."\" OR number = \"".$this->dbquery->getFilter("text")."\")");
381
        }
382
383
        if ($this->dbquery->checkFilter("from_date")) {
384
            $date = new Intraface_Date($this->dbquery->getFilter("from_date"));
385
            if ($date->convert2db()) {
386
                $this->dbquery->setCondition("this_date >= \"".$date->get()."\"");
387
            } else {
388
                $this->error->set("Fra dato er ikke gyldig");
389
            }
390
        }
391
392
        // Poster med fakturadato før slutdato.
393
        if ($this->dbquery->checkFilter("to_date")) {
394
            $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
395
            if ($date->convert2db()) {
396
                $this->dbquery->setCondition("this_date <= \"".$date->get()."\"");
397
            } else {
398
                $this->error->set("Til dato er ikke gyldig");
399
            }
400
        }
401
402
        if ($this->dbquery->checkFilter("status")) {
403
            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...
404
                // Beh�ves ikke, den tager alle.
405
                // $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...
406
            } elseif ($this->dbquery->getFilter("status") == "-2") {
407
                // Not executed = åbne
408
                if ($this->dbquery->checkFilter("to_date")) {
409
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
410
                    if ($date->convert2db()) {
411
                        // Poster der er executed eller cancelled efter dato, og sikring at executed stadig er det, da faktura kan s�ttes tilbage.
412
                        $this->dbquery->setCondition("(date_executed >= \"".$date->get()."\" AND status = 2) OR (date_cancelled >= \"".$date->get()."\") OR status < 2");
413
                    }
414
                } else {
415
                    // Hvis der ikke er nogen dato så tager vi alle dem som p� nuv�rende tidspunkt har status under
416
                    $this->dbquery->setCondition("status < 2");
417
                }
418
            } else {
419
                switch ($this->dbquery->getFilter("status")) {
420
                    case "0":
421
                        $to_date_field = "date_created";
422
                        break;
423
424
                    case "1":
425
                        $to_date_field = "date_sent";
426
                        break;
427
428
                    case "2":
429
                        $to_date_field = "date_executed";
430
                        break;
431
432
                    case "3":
433
                        $to_date_field = "data_caneled";
434
                        break;
435
                }
436
437
                if ($this->dbquery->checkFilter("to_date")) {
438
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
439
                    if ($date->convert2db()) {
440
                        $this->dbquery->setCondition($to_date_field." <= \"".$date->get()."\"");
0 ignored issues
show
Bug introduced by
The variable $to_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...
441
                    }
442
                } else {
443
                    // tager dem som på nuværende tidspunkt har den angivet status
444
                    $this->dbquery->setCondition("status = ".intval($this->dbquery->getFilter("status")));
445
                }
446
            }
447
        }
448
449
        $this->dbquery->setSorting("number DESC");
450
        $db = $this->dbquery->getRecordset("id", "", false);
451
452
        $list = array();
453
        while ($db->nextRecord()) {
454
            $reminder = new Reminder($this->kernel, $db->f("id"));
455
            $list[$i] = $reminder->get();
456
            if (is_object($reminder->contact->address)) {
457
                $list[$i]['contact_id'] = $reminder->contact->get('id');
458
                $list[$i]['name'] = $reminder->contact->address->get('name');
459
                $list[$i]['address'] = $reminder->contact->address->get('address');
460
                $list[$i]['postalcode'] = $reminder->contact->address->get('postcode');
461
                $list[$i]['city'] = $reminder->contact->address->get('city');
462
            }
463
            $i++;
464
        }
465
        return $list;
466
    }
467
468
    /**
469
     * Bruges ift. kontakter
470
     */
471 View Code Duplication
    function any($contact_id)
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...
472
    {
473
        $contact_id = (int)$contact_id;
474
        if ($contact_id == 0) {
475
            return 0;
476
        }
477
        $db = new DB_Sql;
478
        $db->query("SELECT id
479
            FROM invoice_reminder WHERE intranet_id = ".$this->kernel->intranet->get("id")." AND active = 1 AND contact_id=" . $contact_id);
480
        return $db->numRows();
481
    }
482
483
    function isFilledIn()
484
    {
485
        $db = new DB_Sql;
486
        $db->query("SELECT id FROM invoice_reminder WHERE intranet_id = " . $this->kernel->intranet->get('id'));
487
        return $db->numRows();
488
    }
489
490
    /**
491
     * Set the reminder as stated
492
     *
493
     * @param integer $voucher_id   The voucher id
494
     * @param string  $voucher_date Which date is it stated
495
     *
496
     * @return boolean
497
     */
498 1 View Code Duplication
    public function setStated($voucher_id, $voucher_date)
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...
499
    {
500
        // FIXME - check on date
501 1
        $db = new DB_Sql;
502 1
        $db->query("UPDATE invoice_reminder SET date_stated = '" . $voucher_date . "', voucher_id = '".$voucher_id."' WHERE id = " . $this->id . " AND intranet_id = " . $this->kernel->intranet->get('id'));
503 1
        return true;
504
    }
505
506
    /**
507
     * Check whether reminder has been stated
508
     *
509
     * @return boolean
510
     */
511 3
    public function isStated()
512
    {
513 3
        if ($this->get("date_stated") > '0000-00-00') {
514 1
            return true;
515
        }
516 3
        return false;
517
    }
518
519
    /**
520
     * returns whether there is something to state on the reminder
521
     *
522
     * @return boolean true or false
523
     */
524 5
    public function somethingToState()
525
    {
526
527 5
        if ($this->get('total') == 0) {
528 1
            return false;
529
        }
530 4
        return true;
531
    }
532
533
    /**
534
     * Returns whether the reminder is ready for state
535
     *
536
     * @param object accounting year
537
     * @return boolean true or false
538
     */
539 3
    function readyForState($year)
540
    {
541 3
        if ($this->isStated()) {
542 1
            $this->error->set('reminder is already stated');
543 1
            return false;
544
        }
545
546 3
        if (!$this->somethingToState()) {
547
            $this->error->set('there is nothing to state on the reminder');
548
        }
549
550 3 View Code Duplication
        if ($this->get('status') != 'sent' && $this->get('status') != 'executed') {
551 1
            $this->error->set('the reminder should be sent of executed to be stated');
552 1
            return false;
553
        }
554
555 2
        if (!$year->readyForState()) {
556
            $this->error->set('accounting year is not ready for state');
557
            return false;
558
        }
559
560 2
        $debtor_account = new Account($year, $year->getSetting('debtor_account_id'));
561 2 View Code Duplication
        if ($debtor_account->get('id') == 0 || $debtor_account->get('type') != 'balance, asset') {
562
            $this->error->set('invalid debtor account set in the accounting settings');
563
        }
564
565 2
        if ($this->error->isError()) {
566
            return false;
567
        }
568 2
        return true;
569
    }
570
571
    /**
572
     * State reminder
573
     *
574
     * @param object year stating year
575
     * @param integer voucher_number
576
     * @param string voucher_date
577
     * @return boolean true or false
578
     */
579 1
    function state($year, $voucher_number, $voucher_date, $credit_account_number, $translation)
580
    {
581 1
        if (!is_object($year)) {
582
            throw new Exception('First parameter to state needs to be a Year object!');
583
            return false;
0 ignored issues
show
Unused Code introduced by
return false; 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...
584
        }
585
586 1
        if (!is_object($translation)) {
587
            throw new Exception('5th parameter to state needs to be a translation object!');
588
            return false;
0 ignored issues
show
Unused Code introduced by
return false; 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...
589
        }
590
591 1
        $validator = new Intraface_Validator($this->error);
592 1
        if ($validator->isDate($voucher_date, "Ugyldig dato")) {
593 1
            $this_date = new Intraface_Date($voucher_date);
594 1
            $this_date->convert2db();
595 1
        }
596
597 1
        $validator->isNumeric($voucher_number, 'invalid voucher number', 'greater_than_zero');
598 1
        $validator->isNumeric($credit_account_number, 'invalid account number for stating reminder', 'greater_than_zero');
599
600 1
        if ($this->error->isError()) {
601
            return false;
602
        }
603
604 1
        if (!$this->readyForState($year)) {
605
            $this->error->set('Reminder is not ready for state');
606
            return false;
607
        }
608
609 1
        $text = $translation->get('reminder').' #'.$this->get('number');
610
611 1
        require_once 'Intraface/modules/accounting/Voucher.php';
612 1
        require_once 'Intraface/modules/accounting/Account.php';
613 1
        $voucher = Voucher::factory($year, $voucher_number);
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::factory() has been deprecated.

This method has been deprecated.

Loading history...
614 1
        $voucher->save(array(
615 1
            'voucher_number' => $voucher_number,
616 1
            'date' => $voucher_date,
617
            'text' => $text
618 1
        ));
619
620
621 1
        $credit_account = Account::factory($year, $credit_account_number);
0 ignored issues
show
Deprecated Code introduced by
The method Account::factory() has been deprecated.

This method has been deprecated.

Loading history...
622 1
        if ($credit_account->get('id') == 0 || $credit_account->get('type') != 'operating') {
623
            $this->error->set('invalid account for stating reminder');
624
        }
625 1
        $credit_account_number = $credit_account->get('number');
626
627 1
        $debet_account = new Account($year, $year->getSetting('debtor_account_id'));
628 1
        $debet_account_number = $debet_account->get('number');
629
630 1
        $voucher = Voucher::factory($year, $voucher_number);
0 ignored issues
show
Deprecated Code introduced by
The method Voucher::factory() has been deprecated.

This method has been deprecated.

Loading history...
631 1
        $amount = $this->get('total');
632
633
634
        $input_values = array(
635 1
            'voucher_number' => $voucher_number,
636 1
            'date' => $voucher_date,
637 1
            'amount' => number_format($amount, 2, ",", "."),
638 1
            'debet_account_number' => $debet_account_number,
639 1
            'credit_account_number' => $credit_account_number,
640
            'text' => $text
641 1
        );
642
643 1
        if (!$voucher->saveInDaybook($input_values, true)) {
644
            $this->error->merge($voucher->error->getMessage());
645
        }
646
647 1
        require_once 'Intraface/modules/accounting/VoucherFile.php';
648 1
        $voucher_file = new VoucherFile($voucher);
649 1 View Code Duplication
        if (!$voucher_file->save(array('description' => $text, 'belong_to'=>'reminder','belong_to_id'=>$this->get('id')))) {
650
            $this->error->merge($voucher_file->error->getMessage());
651
            $this->error->set('Filen blev ikke overflyttet');
652
        }
653
654 1 View Code Duplication
        if ($this->error->isError()) {
655
            $this->error->set('An error occured while stating the reminder. This can mean that parts of the reminder was not state correct. Please check the voucher.');
656
            // I am not quite sure if the invoice should be set as stated, but it can give trouble to state it again, if some of it was stated...
657
            $this->setStated($voucher->get('id'), $this_date->get());
0 ignored issues
show
Bug introduced by
The variable $this_date 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...
658
            return false;
659
        }
660
661 1
        $this->setStated($voucher->get('id'), $this_date->get());
662 1
        $this->load();
663 1
        return true;
664
    }
665
666
    function pdf($type = 'stream', $filename = '')
667
    {
668
        if ($this->get('id') == 0) {
669
            throw new Exception('Cannot create pdf from debtor without valid id');
670
        }
671
672
        $translation = $this->kernel->getTranslation('debtor');
673
674
        $filehandler = '';
675
676 View Code Duplication
        if ($this->kernel->intranet->get("pdf_header_file_id") != 0) {
677
            $filehandler = new FileHandler($this->kernel, $this->kernel->intranet->get("pdf_header_file_id"));
678
        }
679
680
        $report = new Intraface_modules_invoice_Pdf_Reminder($translation, $filehandler);
681
        $report->visit($this);
682
        return $report->output($type, $filename);
683
    }
684
685
    /**
686
     * returns possible status types
687
     *
688
     * @return array possible status types
689
     */
690 8
    private static function getStatusTypes()
691
    {
692
        return array(
693 8
            0=>'created',
694 8
            1=>'sent',
695 8
            2=>'executed',
696
            3=>'cancelled'
697 8
        );
698
    }
699
700
    /**
701
     * returns possible payment methods
702
     *
703
     * @return array possible payment methods
704
     */
705 8
    private static function getPaymentMethods()
706
    {
707
        return array(
708 8
            0=>'Ingen',
709 8
            1=>'Kontooverførsel',
710 8
            2=>'Girokort +01',
711
            3=>'Girokort +71'
712 8
        );
713
    }
714
715
    /**
716
     * Gets the payment information
717
     *
718
     * @return array
719
     */
720 View Code Duplication
    function getPaymentInformation()
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...
721
    {
722
        $info = array('bank_name'    => $this->kernel->setting->get("intranet", "bank_name"),
723
                      'bank_reg_number' => $this->kernel->setting->get("intranet", "bank_reg_number"),
724
                      'bank_account_number' => $this->kernel->setting->get("intranet", "bank_account_number"),
725
                      'giro_account_number' => $this->kernel->setting->get("intranet", "giro_account_number")
726
        );
727
728
        return $info;
729
    }
730
}
731