Passed
Push — master ( 103d42...18aa9d )
by Matthijs
02:25
created

CartRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Hideyo\Ecommerce\Framework\Services\Cart\Entity;
3
 
4
use Hideyo\Ecommerce\Framework\Models\Shop;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framework\Models\Shop 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...
5
use Cart;
0 ignored issues
show
Bug introduced by
The type Cart 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 Hideyo\Ecommerce\Framework\Services\Sendingmethod\SendingmethodFacade as SendingmethodService;
7
use Hideyo\Ecommerce\Framework\Services\PaymentMethod\PaymentMethodFacade as PaymentMethodService;
8
use Hideyo\Ecommerce\Framework\Services\Shop\ShopFacade as ShopService;
9
use Hideyo\Ecommerce\Framework\Services\Product\ProductFacade as ProductService;
10
use Hideyo\Ecommerce\Framework\Services\Product\ProductCombinationFacade as ProductCombinationService;
11
use Hideyo\Ecommerce\Framework\Services\Coupon\CouponFacade as CouponService;
12
 
13
class CartRepository 
14
{
15
    public function updateAmountProduct($productId, $amount, $leadingAttributeId, $productAttributeId)
16
    {
17
        $explode = explode('-', $productId);
18
        $product = ProductService::selectOneById($explode[0]);
19
20
        $productCombination = false;
21
        if (isset($explode[1])) {
22
            $productAttributeId = $explode[1];
23
            $productCombination = ProductCombinationService::getModel()->where('id', '=', $productAttributeId)->first();
24
        }
25
26
        if ($product->id) {
27
                $productArray = $product->toArray();
28
                $productArray['product_amount_series'] = false;
29
                $productArray['product_category_slug'] = $product->productCategory->slug;
30
                $productArray['price_details'] = $product->getPriceDetails();
31
            if ($productCombination) {
32
                $productArray['id'] = $productArray['id'].'-'.$productCombination->id;
33
                $productArray['product_id'] = $product->id;
34
                $productArray['price_details'] = $productCombination->getPriceDetails();
35
36
                $productArray['product_combination_title'] = array();
37
                $productArray['attributeIds'] = $productCombination->combinations->pluck('attribute_id')->toArray();
38
                foreach ($productCombination->combinations as $combination) {
39
                    $productArray['product_combination_title'][$combination->attribute->attributeGroup->title] = $combination->attribute->value;
40
                }
41
    
42
                $productArray['product_combination_id'] = $productCombination->id;
43
                if ($productCombination->price) {
44
                    $productArray['price_details'] = $productCombination->getPriceDetails();
45
                }
46
47
                if ($productCombination->reference_code) {
48
                    $productArray['reference_code'] = $productCombination->reference_code;
49
                }
50
51
                $productArray['product_images'] =     ProductService::ajaxProductImages($product, array($leadingAttributeId), $productAttributeId);
52
            }
53
54
            if ($product->amountSeries()->where('active', '=', '1')->count()) {
55
                $productArray['product_amount_series'] = true;
56
                $productArray['product_amount_series_range'] = $product->amountSeries()->where('active', '=', '1')->first()->range();
57
            }
58
59
            if($productArray['price_details']['amount'] > 0) {
60
61
                if($amount >= $productArray['price_details']['amount']) {
62
                    $amount = $productArray['price_details']['amount'];
63
                }
64
65
                Cart::update($productId, array(
66
                  'quantity' => array(
67
                      'relative' => false,
68
                      'value' => $amount
69
                  ),
70
                ));
71
            } else {
72
                Cart::remove($productId);
73
            }
74
75
            if(Cart::getConditionsByType('sending_method_country_price')->count()) {
76
                $this->updateSendingMethodCountryPrice(Cart::getConditionsByType('sending_method_country_price')->first()->getAttributes()['data']['sending_method_country_price_id']);  
77
            }
78
        }
79
    }
80
   
81
    public function postProduct($productId, $productCombinationId = false, $leadingAttributeId, $productAttributeId, $amount)
82
    {
83
        $product = ProductService::selectOneByShopIdAndId(config()->get('app.shop_id'), $productId);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

83
        $product = ProductService::selectOneByShopIdAndId(/** @scrutinizer ignore-call */ config()->get('app.shop_id'), $productId);
Loading history...
84
        $productCombination = false;
85
86
        if ($productCombinationId) {
87
            $productCombination = ProductCombinationService::getModel()->where('id', '=', $productCombinationId)->first();
88
        } elseif ($product->attributes()->count()) {
89
            return false;
90
        }
91
 
92
        if ($product->id) {
93
            $productArray = $product->toArray();
94
            $productArray['product_amount_series'] = false;
95
            $productArray['product_category_slug'] = $product->productCategory->slug;
96
            $productArray['tax_rate'] = $product->taxRate->rate;
97
98
            $productArray['price_details'] = $product->getPriceDetails();
99
            if ($productCombination) {
100
                $productArray['product_combination_title'] = array();
101
                $productArray['attributeIds'] = $productCombination->combinations->pluck('attribute_id')->toArray();
102
                foreach ($productCombination->combinations as $combination) {
103
                    $productArray['product_combination_title'][$combination->attribute->attributeGroup->title] = $combination->attribute->value;
104
                }
105
    
106
                $productArray['product_combination_id'] = $productCombination->id;
107
                if ($productCombination->price) {
108
                    $productArray['price_details'] = $productCombination->getPriceDetails();
109
                }
110
111
                if ($productCombination->reference_code) {
112
                    $productArray['reference_code'] = $productCombination->reference_code;
113
                }
114
115
                $productArray['product_images'] =     ProductService::ajaxProductImages($product, array($leadingAttributeId, $productAttributeId));
116
            }
117
118
            $productId = $productArray['id'];
119
   
120
            if (isset($productArray['product_combination_id'])) {
121
                $productId = $productArray['id'].'-'.$productArray['product_combination_id'];
122
            }
123
124
            $discountValue = false;
125
126
            if(session()->get('preSaleDiscount')) {
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
            if(/** @scrutinizer ignore-call */ session()->get('preSaleDiscount')) {
Loading history...
127
                $preSaleDiscount = session()->get('preSaleDiscount');             
128
129
130
131
                if ($preSaleDiscount['value'] AND $preSaleDiscount['collection_id'] == $product->collection_id) {
132
133
                    if ($preSaleDiscount['discount_way'] == 'amount') {
134
                        $discountValue = "-".$preSaleDiscount->value;
135
                      } elseif ($preSaleDiscount['discount_way'] == 'percent') {          
136
                        $discountValue = "-".$preSaleDiscount['value']."%";                    
137
                    }                     
138
                }
139
140
                if($preSaleDiscount['products']) {
141
142
                    $productIds = array_column($preSaleDiscount['products'], 'id');
143
144
                    if (in_array($product->id, $productIds) OR (isset($product->product_id) AND in_array($product->product_id, $productIds))) {
145
146
                        if ($preSaleDiscount['discount_way'] == 'amount') {
147
                            $discountValue = "-".$preSaleDiscount->value;
148
                        } elseif ($preSaleDiscount['discount_way'] == 'percent') {
149
                            $discountValue = "-".$preSaleDiscount['value']."%";                     
150
                        }
151
                    }
152
153
                }             
154
155
            }
156
157
            if ($product->discount_value) {
158
                if ($product->discount_type == 'amount') {
159
                    $discountValue = "-".$product->discount_value;
160
                } elseif ($product->discount_type == 'percent') {
161
                    $discountValue = "-".$product->discount_value."%"; 
162
                }
163
            }
164
165
            $discountCondition = array();
166
            if($discountValue) {
167
168
                $discountCondition = new \Hideyo\Ecommerce\Framework\Services\Cart\CartCondition(array(
169
                    'name' => 'Discount',
170
                    'type' => 'tax',
171
                    'target' => 'item',
172
                    'value' => $discountValue,
173
                ));
174
            }
175
176
            return Cart::add($productId, $productArray,  $amount, $discountCondition);
177
        }
178
179
        return false;
180
    }
181
182
    public function updateSendingMethod($sendingMethodId)
183
    {
184
        $sendingMethod = SendingmethodService::selectOneByShopIdAndId(config()->get('app.shop_id'), $sendingMethodId);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

184
        $sendingMethod = SendingmethodService::selectOneByShopIdAndId(/** @scrutinizer ignore-call */ config()->get('app.shop_id'), $sendingMethodId);
Loading history...
185
        $sendingMethodArray = array();
186
        if (isset($sendingMethod->id)) {
187
            $sendingMethodArray = $sendingMethod->toArray();          
188
            $sendingMethodArray['price_details'] = $sendingMethod->getPriceDetails();
189
            if($sendingMethod->relatedPaymentMethodsActive) {
190
                $sendingMethodArray['related_payment_methods_list'] = $sendingMethod->relatedPaymentMethodsActive->pluck('title', 'id');                
191
            }
192
193
        }
194
195
        Cart::removeConditionsByType('sending_method');
196
        $condition = new \Hideyo\Ecommerce\Framework\Services\Cart\CartCondition(array(
197
            'name' => 'Sending method',
198
            'type' => 'sending_method',
199
            'target' => 'subtotal',
200
            'value' => 0,
201
            'attributes' => array(
202
                'data' => $sendingMethodArray
203
            )
204
        ));
205
206
        Cart::condition($condition);
207
208
        if (!Cart::getConditionsByType('payment_method')->count() and $sendingMethod->relatedPaymentMethodsActive) {
209
            $this->updatePaymentMethod($sendingMethod->relatedPaymentMethodsActive->first()->id);
210
        }
211
212
        return true;
213
    }
214
215
    public function updatePaymentMethod($paymentMethodId)
216
    {
217
        $paymentMethod = PaymentMethodService::selectOneByShopIdAndId(config()->get('app.shop_id'), $paymentMethodId);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

217
        $paymentMethod = PaymentMethodService::selectOneByShopIdAndId(/** @scrutinizer ignore-call */ config()->get('app.shop_id'), $paymentMethodId);
Loading history...
218
219
        $paymentMethodArray = array();
220
        if (isset($paymentMethod->id)) {
221
            $paymentMethodArray = $paymentMethod->toArray();
222
            $paymentMethodArray['price_details'] = $paymentMethod->getPriceDetails();
223
        }
224
225
        $valueExTax = $paymentMethodArray['price_details']['original_price_ex_tax'];
226
        $valueIncTax = $paymentMethodArray['price_details']['original_price_inc_tax'];
227
        $shop = ShopService::find(config()->get('app.shop_id'));
0 ignored issues
show
Unused Code introduced by
The assignment to $shop is dead and can be removed.
Loading history...
228
        $value = $valueIncTax;
229
        $freeSending = ( $paymentMethodArray['no_price_from'] - Cart::getSubTotalWithTax());
230
231
232
        if ($freeSending < 0) {
233
            $value = 0;
234
            $valueIncTax = 0;
235
            $valueExTax = 0;
236
        }
237
238
        $paymentMethodArray['value_inc_tax'] = $valueIncTax;
239
        $paymentMethodArray['value_ex_tax'] = $valueExTax;
240
241
        Cart::removeConditionsByType('payment_method');
242
        $condition = new \Hideyo\Ecommerce\Framework\Services\Cart\CartCondition(array(
243
            'name' => 'Payment method',
244
            'type' => 'payment_method',
245
            'target' => 'subtotal',
246
            'value' => $value,
247
            'attributes' => array(
248
                'data' => $paymentMethodArray
249
            )
250
        ));
251
252
        return Cart::condition($condition);
253
    }
254
255
    public function updateSendingMethodCountryPrice($sendingMethodCountryPriceId)
256
    {
257
        $sendingMethodCountryPrice = SendingmethodService::selectOneCountryPriceByShopIdAndId(config()->get('app.shop_id'), $sendingMethodCountryPriceId);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

257
        $sendingMethodCountryPrice = SendingmethodService::selectOneCountryPriceByShopIdAndId(/** @scrutinizer ignore-call */ config()->get('app.shop_id'), $sendingMethodCountryPriceId);
Loading history...
258
     
259
        if ($sendingMethodCountryPrice) {
260
            $sendingMethod = $sendingMethodCountryPrice->sendingMethod;
261
            $sendingMethodArray = array();
262
            if (isset($sendingMethod->id)) {
263
                $sendingMethodArray = $sendingMethodCountryPrice->toArray();
264
                if ($sendingMethod->countryPrices()->count()) {
265
                    $sendingMethodArray['countries'] = $sendingMethod->countryPrices->toArray();
266
                    $sendingMethodArray['country_list'] = $sendingMethod->countryPrices->pluck('name', 'id');
267
                }
268
                $sendingMethodArray['sending_method_country_price'] = $sendingMethodCountryPrice;
269
                $sendingMethodArray['sending_method_country_price_id'] = $sendingMethodCountryPrice->id;
270
                $sendingMethodArray['sending_method_country_price_country_code'] = $sendingMethodCountryPrice->country_code;
271
                $sendingMethodArray['no_price_from'] = $sendingMethodCountryPrice->no_price_from;
272
                
273
                $sendingMethodArray['price_details'] = $sendingMethodCountryPrice->getPriceDetails();
274
                $sendingMethodArray['sending_method_country_price'] = $sendingMethodCountryPrice->toArray();
275
            }
276
277
            $shop = ShopService::find(config()->get('app.shop_id'));
0 ignored issues
show
Unused Code introduced by
The assignment to $shop is dead and can be removed.
Loading history...
278
279
            $valueExTax = $sendingMethodArray['price_details']['original_price_ex_tax'];
280
            $valueIncTax = $sendingMethodArray['price_details']['original_price_inc_tax'];
281
            $value = $valueIncTax;
282
            $freeSending = ( $sendingMethodArray['no_price_from'] - Cart::getSubTotalWithTax());
283
      
284
            if ($freeSending < 0) {
285
                $value = 0;
286
                $valueIncTax = 0;
287
                $valueExTax = 0;
288
            }
289
290
            $sendingMethodArray['value_inc_tax'] = $valueIncTax;
291
            $sendingMethodArray['value_ex_tax'] = $valueExTax;
292
293
            Cart::removeConditionsByType('sending_method_country_price');
294
            $condition1 = new \Hideyo\Ecommerce\Framework\Services\Cart\CartCondition(array(
295
                'name' => 'Sending method country price',
296
                'type' => 'sending_method_country_price',
297
                'target' => 'subtotal',
298
                'value' => 0,
299
                'attributes' => array(
300
                    'data' => $sendingMethodArray
301
                )
302
            ));
303
304
            Cart::removeConditionsByType('sending_cost');
305
            $condition2 = new \Hideyo\Ecommerce\Framework\Services\Cart\CartCondition(array(
306
                'name' => 'Sending Cost',
307
                'type' => 'sending_cost',
308
                'target' => 'subtotal',
309
                'value' => $value,
310
                'attributes' => array(
311
                    'data' => $sendingMethodArray
312
                )
313
            ));
314
315
            Cart::condition([$condition1, $condition2]);
316
317
            if (!Cart::getConditionsByType('payment_method')->count() and $sendingMethod->relatedPaymentMethodsActive->first()->id) {
318
                $this->updatePaymentMethod($sendingMethod->relatedPaymentMethodsActive->first()->id);
319
            }
320
321
            return true;
322
        }     
323
    }
324
325
    function updateOrderStatus($orderStatusId) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
326
    {
327
        session()->put('orderStatusId', $orderStatusId);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

327
        /** @scrutinizer ignore-call */ 
328
        session()->put('orderStatusId', $orderStatusId);
Loading history...
328
    }
329
330
    function addClient($clientId) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
331
    {
332
        session()->put('orderClientId', $clientId);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

332
        /** @scrutinizer ignore-call */ 
333
        session()->put('orderClientId', $clientId);
Loading history...
333
        session()->forget('orderClientBillAddressId');
334
        session()->forget('orderClientDeliveryAddressId');
335
    }
336
337
    function addClientBillAddress($clientBillAddressId) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
338
    {
339
        session()->put('orderClientBillAddressId', $clientBillAddressId);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

339
        /** @scrutinizer ignore-call */ 
340
        session()->put('orderClientBillAddressId', $clientBillAddressId);
Loading history...
340
    }
341
342
    function addClientDeliveryAddress($clientDeliveryAddressId) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
343
    {
344
        session()->put('orderClientDeliveryAddressId', $clientDeliveryAddressId);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

344
        /** @scrutinizer ignore-call */ 
345
        session()->put('orderClientDeliveryAddressId', $clientDeliveryAddressId);
Loading history...
345
    }
346
347
    public function updateCouponCode($couponCode) {
348
349
        Cart::removeConditionsByType('coupon');
350
        $coupon = CouponService::selectOneByShopIdAndCode(config()->get('app.shop_id'), $couponCode);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

350
        $coupon = CouponService::selectOneByShopIdAndCode(/** @scrutinizer ignore-call */ config()->get('app.shop_id'), $couponCode);
Loading history...
351
        
352
        $couponData = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $couponData is dead and can be removed.
Loading history...
353
        $discountValue = 0;
354
355
        if($coupon) {
356
357
            $couponData = $coupon->toArray();
358
            if($coupon->type == 'total_price') {
359
360
                if($coupon->discount_way == 'total') {
361
                    $discountValue = $coupon->value;
362
                } elseif ($coupon->discount_way == 'percent') {
363
                    $discountValue = $coupon->value.'%';
364
                } 
365
366
                $this->setCouponCode($discountValue, $couponData, $couponCode);
367
            }
368
369
            if($coupon->type == 'product') {
370
371
                if($coupon->products()->count()) {
372
                    
373
                    foreach (Cart::getContent() as $row) {
374
375
                        $id = $row->id;
376
                        $explode = explode('-', $id);
377
                        $contains = $coupon->products->contains($explode[0]);
378
379
                        if($contains) {
380
381
                            if($coupon->discount_way == 'total') {
382
                                $discountValue += $coupon->value;
383
                            } elseif ($coupon->discount_way == 'percent') {
384
                                $value = $coupon->value / 100;                      
385
                                $discountValue += $row->getOriginalPriceWithTaxSum() * $value;
386
                            }                             
387
                        }
388
                    }
389
390
                    $this->setCouponCode($discountValue, $couponData, $couponCode);
391
                }
392
            }
393
394
            if($coupon->type == 'product_category') {
395
396
                if($coupon->productCategories()->count()) {
397
398
                    foreach (Cart::getContent()->sortBy('id')  as $row) {
399
400
                        $contains = $coupon->productCategories->contains($row['attributes']['product_category_id']);
401
402
                        if($contains) {
403
404
                            if($coupon->discount_way == 'total') {
405
                                $discountValue += $coupon->value;
406
                            } elseif ($coupon->discount_way == 'percent') {
407
                                $value = $coupon->value / 100;                      
408
                                $discountValue += $row->getOriginalPriceWithTaxSum() * $value;
409
                            }                             
410
                        }
411
412
                    }
413
414
                    $this->setCouponCode($discountValue, $couponData, $couponCode);
415
                }
416
            }
417
418
            if($coupon->type == 'sending_method') {
419
420
                if($coupon->sendingMethodCountries()->count()) {
421
422
                    foreach ($coupon->sendingMethodCountries as $country) {
423
424
                        if(Cart::getConditionsByType('sending_method_country_price')){
425
                            if($country->name == Cart::getConditionsByType('sending_method_country_price')->first()->getAttributes()['data']['sending_method_country_price']['name']) {
426
427
                                if($coupon->discount_way == 'total') {
428
                                    $discountValue += $coupon->value;
429
                                } elseif ($coupon->discount_way == 'percent') {
430
                                    $value = $coupon->value / 100; 
431
                                    $discountValue += Cart::getConditionsByType('sending_cost')->first()->getValue() * $value;
432
                                } 
433
                            }
434
                        }
435
                    }
436
437
                    $this->setCouponCode($discountValue, $couponData, $couponCode);
438
439
                } elseif($coupon->sendingMethods()->count()) {
440
441
                    foreach ($coupon->sendingMethods as $sendingMethod) {
442
443
                        if(Cart::getConditionsByType('sending_cost')){
444
445
                            if($sendingMethod->id == Cart::getConditionsByType('sending_cost')->first()->getAttributes()['data']['sending_method']['id']) {
446
447
                                if($coupon->discount_way == 'total') {
448
                                    $discountValue += $coupon->value;
449
                                } elseif ($coupon->discount_way == 'percent') {
450
                                    $value = $coupon->value / 100; 
451
                                    $discountValue += Cart::getConditionsByType('sending_cost')->first()->getValue() * $value;
452
                                }                 
453
                            }
454
                        }            
455
                    }
456
457
                    $this->setCouponCode($discountValue, $couponData, $couponCode);
458
                }
459
            }
460
461
            if($coupon->type == 'payment_method') {
462
463
                if($coupon->paymentMethods()->count()) {
464
465
                    foreach ($coupon->paymentMethods as $paymentMethod) {
466
467
                        if(Cart::getConditionsByType('payment_method')){
468
469
                            if($paymentMethod->id == Cart::getConditionsByType('payment_method')->first()->getAttributes()['data']['id']) {
470
471
                                if($coupon->discount_way == 'total') {
472
                                    $discountValue += $coupon->value;
473
                                } elseif ($coupon->discount_way == 'percent') {
474
                                    $value = $coupon->value / 100; 
475
                                    $discountValue += Cart::getConditionsByType('payment_method')->first()->getValue() * $value;
476
                                }                 
477
                            }
478
                        }            
479
                    }
480
481
                    $this->setCouponCode($discountValue, $couponData, $couponCode);
482
                }
483
            }
484
        }
485
    }
486
487
    public function setCouponCode($discountValue, $couponData, $couponCode)
488
    {
489
        $condition = new \Hideyo\Ecommerce\Framework\Services\Cart\CartCondition(array(
490
            'name' => 'Coupon code',
491
            'type' => 'coupon',
492
            'target' => 'subtotal',
493
            'value' => '-'.$discountValue,
494
            'attributes' => array(
495
                'couponData' => $couponData,
496
                'inputCouponValue' => $couponCode
497
            )
498
        ));
499
500
        Cart::condition($condition);
501
    }
502
503
    public function replaceTags($content, $order)
504
    {
505
        $number = $order->id;
0 ignored issues
show
Unused Code introduced by
The assignment to $number is dead and can be removed.
Loading history...
506
        $replace = array(
507
            'orderId' => $order->id,
508
            'orderCreated' => $order->created_at,
509
            'orderTotalPriceWithTax' => $order->price_with_tax,
510
            'orderTotalPriceWithoutTax' => $order->price_without_tax,
511
            'clientEmail' => $order->client->email,
512
            'clientFirstname' => $order->orderBillAddress->firstname,
513
            'clientLastname' => $order->orderBillAddress->lastname,
514
            'clientDeliveryStreet' => $order->orderDeliveryAddress->street,
515
            'clientDeliveryHousenumber' => $order->orderDeliveryAddress->housenumber,
516
            'clientDeliveryHousenumberSuffix' => $order->orderDeliveryAddress->housenumber_suffix,
517
            'clientDeliveryZipcode' => $order->orderDeliveryAddress->zipcode,
518
            'clientDeliveryCity' => $order->orderDeliveryAddress->city,
519
            'clientDeliveryCounty' => $order->orderDeliveryAddress->country,
520
        );
521
522
        foreach ($replace as $key => $val) {
523
            $content = str_replace("[" . $key . "]", $val, $content);
524
        }
525
        $content = nl2br($content);
526
        return $content;
527
    }  
528
}