Passed
Push — master ( f4b030...a2b3af )
by Matthijs
22:06 queued 15:53
created

ProductCombinationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace App\Http\Controllers\Backend;
2
3
/**
4
 * ProductCombinationController
5
 *
6
 * This is the controller of the product combination of the shop
7
 * @author Matthijs Neijenhuijs <[email protected]>
8
 * @version 0.1
9
 */
10
11
use App\Http\Controllers\Controller;
12
13
use Hideyo\Ecommerce\Framework\Services\Product\ProductFacade as ProductService;
14
use Hideyo\Ecommerce\Framework\Services\Product\ProductCombinationFacade as ProductCombinationService;
15
use Hideyo\Ecommerce\Framework\Services\ExtraField\ExtraFieldFacade as ExtraFieldService;
16
use Hideyo\Ecommerce\Framework\Services\Attribute\AttributeFacade as AttributeService;
17
use Hideyo\Ecommerce\Framework\Services\TaxRate\TaxRateFacade as TaxRateService;
18
19
use Illuminate\Http\Request;
20
use Notification;
21
22
23
class ProductCombinationController extends Controller
24
{
25
    public function index(Request $request, $productId)
26
    {
27
        $product = ProductService::find($productId);
28
29
        if($product) {
30
            if ($request->wantsJson()) {
31
32
                $query = ProductCombinationService::getModel()->select(
33
                    ['id', 'tax_rate_id', 'amount', 'price', 'product_id', 'reference_code',
34
                    'default_on']
35
                )->where('product_id', '=', $productId);
36
37
                $datatables = \Datatables::of($query)->addColumn('action', function ($query) use ($productId) {
0 ignored issues
show
Bug introduced by
The type Datatables 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...
38
                    $deleteLink = \Form::deleteajax(url()->route('product-combination.destroy', array('productId' => $productId, 'id' => $query->id)), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'));
39
                    $links = '<a href="'.url()->route('product-combination.edit', array('productId' => $productId, 'id' => $query->id)).'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Edit</a>  '.$deleteLink;
40
                
41
                    return $links;
42
                })
43
44
                ->addColumn('amount', function ($query) {
45
                    return '<input type="text" class="change-amount-product-attribute" value="'.$query->amount.'" data-url="/admin/product/'.$query->product_id.'/product-combination/change-amount-attribute/'.$query->id.'">';
46
                })
47
48
                ->addColumn('price', function ($query) {
49
                    $result = 0;
50
                    if ($query->price) {
51
52
                        $taxRate = 0;
53
                        $priceInc = 0;
54
                        $taxValue = 0;
55
56
                        if (isset($query->taxRate->rate)) {
57
                            $taxRate = $query->taxRate->rate;
58
                            $priceInc = (($query->taxRate->rate / 100) * $query->price) + $query->price;
59
                            $taxValue = $priceInc - $query->price;
60
                        }
61
62
                        $discountPriceInc = false;
63
                        $discountPriceEx = false;
64
                        $discountTaxRate = 0;
65
                        if ($query->discount_value) {
66
                            if ($query->discount_type == 'amount') {
67
                                $discountPriceInc = $priceInc - $query->discount_value;
68
                                $discountPriceEx = $discountPriceInc / 1.21;
69
                            } elseif ($query->discount_type == 'percent') {
70
                                $tax = ($query->discount_value / 100) * $priceInc;
71
                                $discountPriceInc = $priceInc - $tax;
72
                                $discountPriceEx = $discountPriceInc / 1.21;
73
                            }
74
                            $discountTaxRate = $discountPriceInc - $discountPriceEx;
75
                            $discountPriceInc = $discountPriceInc;
76
                            $discountPriceEx = $discountPriceEx;
77
                        }
78
79
80
                        $output = array(
81
                            'orginal_price_ex_tax'  => $query->price,
82
                            'orginal_price_ex_tax_number_format'  => number_format($query->price, 2, '.', ''),
83
                            'orginal_price_inc_tax' => $priceInc,
84
                            'orginal_price_inc_tax_number_format' => number_format($priceInc, 2, '.', ''),
85
                            'tax_rate' => $taxRate,
86
                            'tax_value' => $taxValue,
87
                            'currency' => 'EU',
88
                            'discount_price_inc' => $discountPriceInc,
89
                            'discount_price_inc_number_format' => number_format($discountPriceInc, 2, '.', ''),
0 ignored issues
show
Bug introduced by
It seems like $discountPriceInc can also be of type false; however, parameter $number of number_format() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

89
                            'discount_price_inc_number_format' => number_format(/** @scrutinizer ignore-type */ $discountPriceInc, 2, '.', ''),
Loading history...
90
                            'discount_price_ex' => $discountPriceEx,
91
                            'discount_price_ex_number_format' => number_format($discountPriceEx, 2, '.', ''),
92
                            'discount_tax_value' => $discountTaxRate,
93
                            'discount_value' => $query->discount_value,
94
                            'amount' => $query->amount
95
                            );
96
97
                        $result =  '&euro; '.$output['orginal_price_ex_tax_number_format'].' / &euro; '.$output['orginal_price_inc_tax_number_format'];
98
99
100
                        if ($query->discount_value) {
101
                            $result .= '<br/> discount: yes';
102
                        }
103
                    }
104
105
                    return $result;
106
                })
107
108
                ->addColumn('combinations', function ($query) use ($productId) {
0 ignored issues
show
Unused Code introduced by
The import $productId is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
109
                    $items = array();
110
                    foreach ($query->combinations as $row) {
111
                        $items[] = $row->attribute->attributeGroup->title.': '.$row->attribute->value;
112
                    }
113
           
114
                    return implode(', ', $items);
115
                });
116
117
                return $datatables->make(true);
118
119
            }
120
            
121
            return view('backend.product-combination.index')->with(array('product' => $product, 'attributeGroups' => AttributeService::selectAllGroups()->pluck('title', 'id')));
122
        }
123
        
124
        return redirect()->route('product.index');            
125
    }
126
127
    public function create(Request $request, $productId)
128
    {
129
        $product = ProductService::find($productId);
130
131
        if ($request->wantsJson()) {
132
            $input = $request->all();
133
            $attributeGroup = AttributeService::findGroup($input['attribute_group_id']);
134
            if ($attributeGroup->count()) {
135
                if ($attributeGroup->attributes()) {
136
                    return response()->json($attributeGroup->attributes);
137
                }
138
            }
139
        }
140
        
141
        return view('backend.product-combination.create')->with(array('taxRates' => TaxRateService::selectAll()->pluck('title', 'id'), 'product' => $product, 'attributeGroups' => AttributeService::selectAllGroups()->pluck('title', 'id')));
142
    }
143
144
    public function changeAmount($productId, $id, $amount)
0 ignored issues
show
Unused Code introduced by
The parameter $productId 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

144
    public function changeAmount(/** @scrutinizer ignore-unused */ $productId, $id, $amount)

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...
145
    {
146
        $result = ProductCombinationService::changeAmount($id, $amount);
147
148
        return response()->json($result);
149
    }
150
151
    public function store(Request $request, $productId)
152
    {
153
        $result  = ProductCombinationService::create($request->all(), $productId);
154
        if($result) {
155
            return ProductCombinationService::notificationRedirect(array('product-combination.index', $productId), $result, 'The product extra fields are updated.');
156
157
        }
158
159
        Notification::error('combination already exist');
160
        return redirect()->back()->withInput();
161
    }
162
163
    public function edit(Request $request, $productId, $id)
164
    {
165
        $product = ProductService::find($productId);
166
        $productCombination = ProductCombinationService::find($id);
167
        $selectedAttributes = array();
168
        $attributes = array();
169
        foreach ($productCombination->combinations as $row) {
170
            $selectedAttributes[] = $row->attribute->id;
171
            $attributes[$row->attribute->id]['group_id'] = $row->attribute->attributeGroup->id;
172
            $attributes[$row->attribute->id]['value'] = $row->attribute->value;
173
        }
174
175
        if ($request->wantsJson()) {
176
            $input = $request->all();
177
            $attributeGroup = AttributeService::find($input['attribute_group_id']);
178
            if ($attributeGroup->count()) {
179
                if ($attributeGroup->attributes()) {
180
                    return response()->json($attributeGroup->attributes);
181
                }
182
            }
183
        } else {
184
            return view('backend.product-combination.edit')->with(array('taxRates' => TaxRateService::selectAll()->pluck('title', 'id'), 'selectedAttributes' => $selectedAttributes, 'attributes' => $attributes, 'productCombination' => $productCombination, 'product' => $product, 'attributeGroups' => AttributeService::selectAllGroups()->pluck('title', 'id')));
185
        }
186
    }
187
188
    public function update(Request $request, $productId, $id)
189
    {
190
191
        $result  = ProductCombinationService::updateById($request->all(), $productId, $id);
192
        return ProductCombinationService::notificationRedirect(array('product-combination.index', $productId), $result, 'The product combinations are updated.');
193
194
    }
195
196
    public function destroy($productId, $id)
197
    {
198
        $result  = ProductCombinationService::destroy($id);
199
200
        if ($result) {
201
            Notification::success('The product combination is deleted.');
202
            return redirect()->route('product-combination.index', $productId);
203
        }
204
    }
205
}
206