Passed
Push — 1.0 ( 07b100...83f8df )
by Morven
05:31
created

LineItemReport::sourceRecords()   F

Complexity

Conditions 13
Paths 1056

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 33
nc 1056
nop 3
dl 0
loc 57
rs 3.2704
c 0
b 0
f 0

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
namespace SilverCommerce\OrdersAdmin\Reports;
4
5
use SilverStripe\Reports\Report;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Reports\Report was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\FieldType\DBDatetime as SSDateTime;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\TextField;
10
use SilverStripe\View\ViewableData;
11
use SilverCommerce\OrdersAdmin\Model\Invoice;
12
13
// Only load this if reports are active
14
if (class_exists(Report::class)) {
15
    class LineItemReport extends Report
16
    {
17
        
18
        public function title()
19
        {
20
            return _t("Orders.LineItems", "Items ordered");
21
        }
22
23
        public function description()
24
        {
25
            return _t("Orders.LineItemReportDescription", "View all individual products ordered through this site");
26
        }
27
28
        public function columns()
29
        {
30
            return [
31
                "StockID" => "StockID",
32
                "Details" => "Details",
33
                "Price" => "Price",
34
                "Quantity" => "Quantity"
35
            ];
36
        }
37
38
        public function exportColumns()
39
        {
40
            // Loop through all colls and replace BR's with spaces
41
            $cols = [];
42
43
            foreach ($this->columns() as $key => $value) {
44
                $cols[$key] = $value;
45
            }
46
47
            return $cols;
48
        }
49
50
        public function sortColumns()
51
        {
52
            return [];
53
        }
54
55
        public function getReportField()
56
        {
57
            $gridField = parent::getReportField();
58
59
            // Edit CSV export button
60
            $export_button = $gridField->getConfig()->getComponentByType('GridFieldExportButton');
61
            $export_button->setExportColumns($this->exportColumns());
62
63
            return $gridField;
64
        }
65
66
        public function sourceRecords($params, $sort, $limit)
67
        {
68
            $return = ArrayList::create();
69
70
            // Check filters
71
            $where_filter = [];
72
73
            $where_filter[] = (isset($params['Filter_Year'])) ? "YEAR(\"Created\") = '{$params['Filter_Year']}'" : "YEAR(\"Created\") = '".date('Y')."'";
74
            if (!empty($params['Filter_Month'])) {
75
                $where_filter[] = "Month(\"Created\") = '{$params['Filter_Month']}'";
76
            }
77
            if (!empty($params['Filter_Status'])) {
78
                $where_filter[] = "Status = '{$params['Filter_Status']}'";
79
            }
80
            if (!empty($params['Filter_FirstName'])) {
81
                $where_filter[] = "FirstName = '{$params['Filter_FirstName']}'";
82
            }
83
            if (!empty($params['Filter_Surname'])) {
84
                $where_filter[] = "Surname = '{$params['Filter_Surname']}'";
85
            }
86
87
            $orders = Invoice::get()
88
                ->where(implode(' AND ', $where_filter));
89
90
            foreach ($orders as $order) {
91
                // Setup a filter for our order items
92
                $filter = array();
93
94
                if (!empty($params['Filter_ProductName'])) {
95
                    $filter["Title:PartialMatch"] = $params['Filter_ProductName'];
96
                }
97
98
                if (!empty($params['Filter_StockID'])) {
99
                    $filter["StockID"] = $params['Filter_StockID'];
100
                }
101
102
                $list = (count($filter)) ? $order->Items()->filter($filter) : $order->Items();
103
104
                foreach ($list as $order_item) {
105
                    if ($order_item->StockID) {
106
                        if ($list_item = $return->find("StockID", $order_item->StockID)) {
107
                            $list_item->Quantity = $list_item->Quantity + $order_item->Quantity;
108
                        } else {
109
                            $report_item = LineItemReportItem::create();
110
                            $report_item->ID = $order_item->StockID;
111
                            $report_item->StockID = $order_item->StockID;
112
                            $report_item->Details = $order_item->Title;
113
                            $report_item->Price = $order_item->Price;
114
                            $report_item->Quantity = $order_item->Quantity;
115
116
                            $return->add($report_item);
117
                        }
118
                    }
119
                }
120
            }
121
122
            return $return;
123
        }
124
125
        public function parameterFields()
126
        {
127
            $fields = new FieldList();
128
129
            $first_order = Invoice::get()
130
                ->sort('Created', 'ASC')
131
                ->first();
132
133
            // Check if any order exist
134
            if ($first_order) {
0 ignored issues
show
introduced by
The condition $first_order can never be true.
Loading history...
135
                // List all months
136
                $months = array('All');
137
                for ($i = 1; $i <= 12; $i++) {
138
                    $months[] = date("F", mktime(0, 0, 0, $i + 1, 0, 0));
139
                }
140
141
                // Get the first order, then count down from current year to that
142
                $firstyear = new SSDatetime('FirstDate');
143
                $firstyear->setValue($first_order->Created);
144
                $years = array();
145
                for ($i = date('Y'); $i >= $firstyear->Year(); $i--) {
146
                    $years[$i] = $i;
147
                }
148
149
                // Order Status
150
                $statuses = Invoice::config()->statuses;
151
                array_unshift($statuses, 'All');
152
153
                $fields->push(TextField::create(
154
                    'Filter_FirstName',
155
                    'Customer First Name'
156
                ));
157
                
158
                $fields->push(TextField::create(
159
                    'Filter_Surname',
160
                    'Customer Surname'
161
                ));
162
                
163
                $fields->push(TextField::create(
164
                    'Filter_StockID',
165
                    'Stock ID'
166
                ));
167
                
168
                $fields->push(TextField::create(
169
                    'Filter_ProductName',
170
                    'Product Name'
171
                ));
172
                
173
                $fields->push(DropdownField::create(
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Reports\DropdownField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
174
                    'Filter_Month',
175
                    'Month',
176
                    $months
177
                ));
178
                
179
                $fields->push(DropdownField::create(
180
                    'Filter_Year',
181
                    'Year',
182
                    $years
183
                ));
184
                
185
                $fields->push(DropdownField::create(
186
                    'Filter_Status',
187
                    'Order Status',
188
                    $statuses
189
                ));
190
            }
191
192
            return $fields;
193
        }
194
    }
195
}
196
197
/**
198
 * Item that can be loaded into an LineItem report
199
 *
200
 */
201
class LineItemReportItem extends ViewableData
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
202
{
203
    public $ClassName = "LineItemReportItem";
204
205
    public $StockID;
206
    public $Details;
207
    public $Price;
208
    public $Quantity;
209
210
    public function canView($member = null)
0 ignored issues
show
Unused Code introduced by
The parameter $member is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

210
    public function canView(/** @scrutinizer ignore-unused */ $member = null)

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

Loading history...
211
    {
212
        return true;
213
    }
214
}
215