__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
/**
3
 * Doctrine Gateway to DebtorDoctrine
4
 *
5
 * Bruges til at holde styr p� debtor.
6
 *
7
 * @package Intraface_Debtor
8
 * @author Sune Jensen
9
 * @see DebtorDoctrine
10
 */
11
12
class Intraface_modules_debtor_DebtorDoctrineGateway
13
{
14
15
    /**
16
     * @var object
17
     */
18
    private $user;
19
20
    /**
21
     *
22
     * @var object doctrine record table
23
     */
24
    private $table;
25
26
    /**
27
     * @var integer type
28
     */
29
    private $type_key;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param object  $user                Userobject
35
     *
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    function __construct($doctrine, $user)
39
    {
40
        $this->user = $user;
41
        $this->table = $doctrine->getTable('Intraface_modules_debtor_DebtorDoctrine');
42
        $this->type_key = 3; // invoice
43
    }
44
45
    /**
46
     * Finds a product with an id
47
     *
48
     * @param integer $id product id
49
     * @return object
50
     */
51
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
52
    function findById($id)
53
    {
54
55
        die('Not created');
56
        $collection = $this->table
57
            ->createQuery()
58
            ->select('*, details.*')
59
            ->innerJoin('Intraface_modules_product_ProductDoctrine.details AS details')
60
            ->addWhere('active = 1')
61
            ->addWhere('id = ?', $id)
62
            ->addOrderBy('details.id')
63
            ->execute();
64
65
        if ($collection == NULL || $collection->count() != 1) {
66
            throw new Intraface_Gateway_Exception('Error finding product from id '.$id);
67
        } else {
68
            return $collection->getLast();
69
        }
70
71
    }*/
72
73
    /**
74
     * Finds all products
75
     *
76
     * Hvis den er fra webshop b�r den faktisk opsamle oplysninger om s�gningen
77
     * s� man kan se, hvad folk er interesseret i.
78
     * S�gemaskinen skal v�re tolerant for stavefejl
79
     *
80
     * @param object $search
0 ignored issues
show
Bug introduced by
There is no parameter named $search. 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...
81
     *
82
     * @return object collection containing products
83
     */
84
    public function findByDbQuerySearch($dbquery = null)
85
    {
86
        $query = $this->table
87
            ->createQuery()
88
            ->select('due_date, status, number, description, this_date, date_sent, contact_id
89
                item.id, item.product_detail_id, item.product_variation_id, item.product_variation_detail_id, item.quantity,
90
                item_product.id, item_product.has_variation,
91
                item_product_details.id, item_product_details.number, item_product_details_translation.name, item_product_details_translation.description, item_product_details.price, item_product_details.vat,
92
                item_product_variation.id, item_product_variation.number,
93
                item_product_variation_detail.id, item_product_variation_detail.price_difference, item_product_variation_detail.weight_difference')
94
            ->leftJoin('Intraface_modules_debtor_DebtorDoctrine.item item')
95
            ->innerJoin('item.product item_product')
96
            ->innerJoin('item_product.details item_product_details WITH item.product_detail_id = item_product_details.id')
97
            ->innerJoin('item_product_details.Translation item_product_details_translation WITH item_product_details.id = item_product_details_translation.id')
98
            ->leftJoin('item_product.variation item_product_variation WITH item.product_variation_id = item_product_variation.id AND (item_product_variation.deleted_at IS NULL OR item_product_variation.deleted_at IS NOT NULL)') // also deleted variations is used
99
            ->leftJoin('item_product_variation.detail item_product_variation_detail')
100
            ->addWhere('active = 1')
101
            ->addWhere('item.active = 1')
102
            ->addWhere('type = ?', $this->type_key)
103
            ->addWhere('intranet_id = ?', $this->user->getActiveIntranetId());
104
105
        if ($dbquery->checkFilter("contact_id")) {
106
            $query = $query->addWhere("contact_id = ?", intval($dbquery->getFilter("contact_id")));
107
        }
108
109
        if ($dbquery->checkFilter("text")) {
110
            $query = $query->addWhere('description LIKE ? OR girocode = ? OR number = ?', array('%'.$dbquery->getFilter("text").'%', $dbquery->getFilter("text"), $dbquery->getFilter("text")));
111
            //  OR contact_address.name LIKE \"%".$dbquery->getFilter("text")."%\")
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
112
        }
113
114
        /* To be implemented
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...
115
        if ($dbquery->checkFilter("product_id")) {
116
            $dbquery->setCondition("debtor_item.product_id = ".$dbquery->getFilter('product_id'));
117
            if ($dbquery->checkFilter("product_variation_id")) {
118
                $dbquery->setCondition("debtor_item.product_variation_id = ".$dbquery->getFilter('product_variation_id'));
119
            } else {
120
                $dbquery->setCondition("debtor_item.product_variation_id = 0");
121
            }
122
        } */
123
124
        if ($dbquery->checkFilter("date_field")) {
125
            if (in_array($dbquery->getFilter("date_field"), array('this_date', 'date_created', 'date_sent', 'date_executed', 'data_cancelled'))) {
126
                $date_field = $dbquery->getFilter("date_field");
127
            } else {
128
                $this->error->set("Ugyldigt datointerval felt");
0 ignored issues
show
Bug introduced by
The property error 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...
129
            }
130
        } else {
131
            $date_field = 'this_date';
132
        }
133
134 View Code Duplication
        if ($dbquery->checkFilter("from_date")) {
135
            $date = new Intraface_Date($dbquery->getFilter("from_date"));
136
            if ($date->convert2db()) {
137
                $query = $query->addWhere($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...
138
            } else {
139
                $this->error->set("Fra dato er ikke gyldig");
140
            }
141
        }
142
143
        // Poster med fakturadato f�r slutdato.
144 View Code Duplication
        if ($dbquery->checkFilter("to_date")) {
145
            $date = new Intraface_Date($dbquery->getFilter("to_date"));
146
            if ($date->convert2db()) {
147
                $query = $query->addWhere($date_field." <= ? ", $date->get());
148
            } else {
149
                $this->error->set("Til dato er ikke gyldig");
150
            }
151
        }
152
        // alle ikke bogf�rte skal findes
153
        if ($dbquery->checkFilter("not_stated")) {
154
            $query = $query->addWhere("voucher_id = 0");
155
        }
156
157
        if ($dbquery->checkFilter("status")) {
158
            if ($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...
159
                // Beh�ves ikke, den tager alle.
160
                // $query = $query->addWhere("status >= 0");
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
161 View Code Duplication
            } elseif ($dbquery->getFilter("status") == "-2") {
162
                // Not executed = �bne
163
                if ($dbquery->checkFilter("to_date")) {
164
                    $date = new Intraface_Date($dbquery->getFilter("to_date"));
165
                    if ($date->convert2db()) {
166
                        // Poster der er executed eller cancelled efter dato, og sikring at executed stadig er det, da faktura kan s�ttes tilbage.
167
                        $query = $query->addWhere("(date_executed >= \"".$date->get()."\" AND status = 2) OR (date_cancelled >= \"".$date->get()."\") OR status < 2");
168
                    }
169
                } else {
170
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
171
                    $query = $query->addWhere("status < 2");
172
                }
173
            } elseif ($dbquery->getFilter("status") == "-3") {
174
                //  Afskrevne. Vi tager f�rst alle sendte og executed.
175
176
                if ($this->get("type") != "invoice") {
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Intraface_modules..._DebtorDoctrineGateway>.

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...
177
                    throw new Exception("Afskrevne kan kun benyttes ved faktura");
178
                }
179
                die('functionality not implemented yet! contact Intraface');
0 ignored issues
show
Coding Style Compatibility introduced by
The method findByDbQuerySearch() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
180
181
                $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");
0 ignored issues
show
Unused Code introduced by
$dbquery->setJoin('INNER...ment.payment_for = 1'); 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...
182
                $query = $query->addWhere("invoice_payment.type = -1");
183
184 View Code Duplication
                if ($dbquery->checkFilter("to_date")) {
185
                    $date = new Intraface_Date($dbquery->getFilter("to_date"));
186
                    if ($date->convert2db()) {
187
                        // alle som er sendte p� datoen og som ikke er cancelled
188
                        $query = $query->addWhere("debtor.date_sent <= '".$date->get()."' AND debtor.status != 3");
189
                        $query = $query->addWhere("invoice_payment.payment_date <= '".$date->get()."'");
190
                    }
191
                } else {
192
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
193
                    $query = $query->addWhere("status = 1 OR status = 2");
194
                }
195
            } else {
196
                $query = $query->addWhere("status = ?", intval($dbquery->getFilter("status")));
197
            }
198
        }
199
200
        switch ($dbquery->getFilter("sorting")) {
201
            case 1:
202
                $query =  $query->addOrderBy('number ASC, item.position');
203
                break;
204
            case 2:
205
                $query =  $query->addOrderBy('contact.number ASC, item.position');
206
                break;
207
            case 3:
208
                $query =  $query->addOrderBy('contact_address.name ASC, item.position');
209
                break;
210
            default:
211
                $query =  $query->addOrderBy('number DESC, item.position');
212
        }
213
214
215
        // $query = $query->getSql(); die($query);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
216
217
        $collection = $query->execute();
218
219
        return $collection;
220
    }
221
222
223
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
224
    public function getMaxNumber()
225
    {
226
        $collection = $this->table
227
            ->createQuery()
228
            ->select('id, details.number')
229
            ->innerJoin('Intraface_modules_product_ProductDoctrine.details AS details')
230
            ->addWhere('Intraface_modules_product_ProductDoctrine.active = 0 OR Intraface_modules_product_ProductDoctrine.active = 1')
231
            ->orderBy('details.number')
232
            ->execute();
233
234
        if ($collection == NULL || $collection->count() == 0) {
235
            return 0;
236
        } else {
237
            return $collection->getLast()->getDetails()->getNumber();
238
        }
239
    }*/
240
}
241