ProductCombinationController::changeAmount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 3
dl 0
loc 5
rs 10
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
use Illuminate\Http\Request;
19
20
21
class ProductCombinationController extends Controller
22
{
23
    public function index(Request $request, $productId)
24
    {
25
        $product = ProductService::find($productId);
26
27
        if($product) {
28
            if ($request->wantsJson()) {
29
30
                $query = ProductCombinationService::getModel()->where('product_id', '=', $productId);
31
32
                $datatables = \DataTables::of($query)->addColumn('action', function ($query) use ($productId) {
33
                    $deleteLink = \Form::deleteajax(url()->route('product-combination.destroy', array('productId' => $productId, 'id' => $query->id)), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'));
34
                    $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;
35
                
36
                    return $links;
37
                })
38
39
                ->addColumn('amount', function ($query) {
40
                    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.'">';
41
                })
42
43
                ->addColumn('price', function ($query) {
44
                    $result = 0;
45
                    if ($query->price) {
46
47
                        $taxRate = 0;
48
                        $priceInc = 0;
49
                        $taxValue = 0;
50
51
                        if (isset($query->taxRate->rate)) {
52
                            $taxRate = $query->taxRate->rate;
53
                            $priceInc = (($query->taxRate->rate / 100) * $query->price) + $query->price;
54
                            $taxValue = $priceInc - $query->price;
55
                        }
56
57
                        $discountPriceInc = false;
58
                        $discountPriceEx = false;
59
                        $discountTaxRate = 0;
60
                        if ($query->discount_value) {
61
                            if ($query->discount_type == 'amount') {
62
                                $discountPriceInc = $priceInc - $query->discount_value;
63
                                $discountPriceEx = $discountPriceInc / 1.21;
64
                            } elseif ($query->discount_type == 'percent') {
65
                                $tax = ($query->discount_value / 100) * $priceInc;
66
                                $discountPriceInc = $priceInc - $tax;
67
                                $discountPriceEx = $discountPriceInc / 1.21;
68
                            }
69
                            $discountTaxRate = $discountPriceInc - $discountPriceEx;
70
                            $discountPriceInc = $discountPriceInc;
71
                            $discountPriceEx = $discountPriceEx;
72
                        }
73
74
75
                        $output = array(
76
                            'orginal_price_ex_tax'  => $query->price,
77
                            'orginal_price_ex_tax_number_format'  => number_format($query->price, 2, '.', ''),
78
                            'orginal_price_inc_tax' => $priceInc,
79
                            'orginal_price_inc_tax_number_format' => number_format($priceInc, 2, '.', ''),
80
                            'tax_rate' => $taxRate,
81
                            'tax_value' => $taxValue,
82
                            'currency' => 'EU',
83
                            'discount_price_inc' => $discountPriceInc,
84
                            '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

84
                            'discount_price_inc_number_format' => number_format(/** @scrutinizer ignore-type */ $discountPriceInc, 2, '.', ''),
Loading history...
85
                            'discount_price_ex' => $discountPriceEx,
86
                            'discount_price_ex_number_format' => number_format($discountPriceEx, 2, '.', ''),
87
                            'discount_tax_value' => $discountTaxRate,
88
                            'discount_value' => $query->discount_value,
89
                            'amount' => $query->amount
90
                            );
91
92
                        $result =  '&euro; '.$output['orginal_price_ex_tax_number_format'].' / &euro; '.$output['orginal_price_inc_tax_number_format'];
93
94
95
                        if ($query->discount_value) {
96
                            $result .= '<br/> discount: yes';
97
                        }
98
                    }
99
100
                    return $result;
101
                })
102
103
                ->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...
104
                    $items = array();
105
                    foreach ($query->combinations as $row) {
106
                        $items[] = $row->attribute->attributeGroup->title.': '.$row->attribute->value;
107
                    }
108
           
109
                    return implode(', ', $items);
110
                });
111
112
                return $datatables->rawColumns(['amount', 'action'])->make(true);
113
114
            }
115
            
116
            return view('backend.product-combination.index')->with(array('product' => $product, 'attributeGroups' => AttributeService::selectAllGroups()->pluck('title', 'id')));
117
        }
118
        
119
        return redirect()->route('product.index');            
120
    }
121
122
    public function create(Request $request, $productId)
123
    {
124
        $product = ProductService::find($productId);
125
126
        if ($request->wantsJson()) {
127
            $input = $request->all();
128
            $attributeGroup = AttributeService::findGroup($input['attribute_group_id']);
129
            if ($attributeGroup->count()) {
130
                if ($attributeGroup->attributes()) {
131
                    return response()->json($attributeGroup->attributes);
132
                }
133
            }
134
        }
135
        
136
        return view('backend.product-combination.create')->with(array('taxRates' => TaxRateService::selectAll()->pluck('title', 'id'), 'product' => $product, 'attributeGroups' => AttributeService::selectAllGroups()->pluck('title', 'id')));
137
    }
138
139
    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

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