getTotalPriceDiscountStartDateAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php 
2
3
namespace Hideyo\Ecommerce\Framework\Services\SendingMethod\Entity;
4
5
use Hideyo\Ecommerce\Framework\Services\BaseModel;
6
use Carbon\Carbon;
7
8
class SendingMethod extends BaseModel
9
{
10
    /**
11
     * The database table used by the model.
12
     *
13
     * @var string
14
     */    
15
    protected $table = 'sending_method';
16
17
    // Add the 'avatar' attachment to the fillable array so that it's mass-assignable on this model.
18
    protected $fillable = ['total_price_discount_type', 'total_price_discount_value', 'total_price_discount_start_date', 'total_price_discount_end_date', 'active', 'price', 'no_price_from', 'minimal_weight', 'maximal_weight', 'title', 'shop_id', 'tax_rate_id', 'modified_by_user_id'];
19
20
    public function relatedPaymentMethods()
21
    {
22
        return $this->belongsToMany('Hideyo\Ecommerce\Framework\Services\PaymentMethod\Entity\PaymentMethod', 'sending_payment_method_related');
23
    }
24
25
    public function relatedPaymentMethodsActive()
26
    {
27
        return $this->belongsToMany('Hideyo\Ecommerce\Framework\Services\PaymentMethod\Entity\PaymentMethod', 'sending_payment_method_related')->where('active', 1);
28
    }
29
30
    public function countryPrices()
31
    {
32
        return $this->hasMany('Hideyo\Ecommerce\Framework\Services\SendingMethod\Entity\SendingMethodCountryPrice');
33
    }
34
35
36
    public function getPriceDetails()
37
    {
38
        $taxRate = 0;
39
        $priceInc = 0;
40
41
        if (isset($this->taxRate->rate)) {
42
            $taxRate = $this->taxRate->rate;
43
            $priceInc = (($this->taxRate->rate / 100) * $this->price) + $this->price;
44
        }
45
46
        return array(
47
            'original_price_ex_tax' => $this->price,
48
            'original_price_inc_tax' => $priceInc,
49
            'original_price_ex_tax_number_format' => number_format($this->price, 2, '.', ''),
50
            'original_price_inc_tax_number_format' => number_format($priceInc, 2, '.', ''),
51
            'tax_rate' => $taxRate,
52
            'tax_value' => $priceInc - $this->price,
53
            'tax_value_number_format' => number_format(($priceInc - $this->price), 2, '.', ''),
54
            'currency' => $this->shop->currency_code,
55
56
        );
57
    }
58
59
    public function taxRate()
60
    {
61
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\TaxRate\Entity\TaxRate');
62
    }
63
64
    public function shop()
65
    {
66
        return $this->belongsTo('Hideyo\Ecommerce\Framework\Services\Shop\Entity\Shop');
67
    }
68
69
    public function setTotalPriceDiscountStartDateAttribute($value)
70
    {
71
        $this->attributes['total_price_discount_start_date'] = null;
72
73
        if ($value) {
74
            $date = explode('/', $value);
75
            $value = Carbon::createFromDate($date[2], $date[1], $date[0])->toDateTimeString();
0 ignored issues
show
Bug introduced by
$date[1] of type string is incompatible with the type integer|null expected by parameter $month of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

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

75
            $value = Carbon::createFromDate($date[2], /** @scrutinizer ignore-type */ $date[1], $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[0] of type string is incompatible with the type integer|null expected by parameter $day of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

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

75
            $value = Carbon::createFromDate($date[2], $date[1], /** @scrutinizer ignore-type */ $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[2] of type string is incompatible with the type integer|null expected by parameter $year of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

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

75
            $value = Carbon::createFromDate(/** @scrutinizer ignore-type */ $date[2], $date[1], $date[0])->toDateTimeString();
Loading history...
76
            $this->attributes['total_price_discount_start_date'] = $value;
77
        }
78
    }
79
80
    public function getTotalPriceDiscountStartDateAttribute($value)
81
    {
82
        if ($value) {
83
            $date = explode('-', $value);
84
            return $date[2].'/'.$date[1].'/'.$date[0];
85
        }
86
        
87
        return null;
88
    }
89
90
    public function setTotalPriceDiscountEndDateAttribute($value)
91
    {
92
        $this->attributes['total_price_discount_end_date'] = null;
93
94
        if ($value) {
95
            $date = explode('/', $value);
96
            $value = Carbon::createFromDate($date[2], $date[1], $date[0])->toDateTimeString();
0 ignored issues
show
Bug introduced by
$date[0] of type string is incompatible with the type integer|null expected by parameter $day of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

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

96
            $value = Carbon::createFromDate($date[2], $date[1], /** @scrutinizer ignore-type */ $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[2] of type string is incompatible with the type integer|null expected by parameter $year of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

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

96
            $value = Carbon::createFromDate(/** @scrutinizer ignore-type */ $date[2], $date[1], $date[0])->toDateTimeString();
Loading history...
Bug introduced by
$date[1] of type string is incompatible with the type integer|null expected by parameter $month of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

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

96
            $value = Carbon::createFromDate($date[2], /** @scrutinizer ignore-type */ $date[1], $date[0])->toDateTimeString();
Loading history...
97
            $this->attributes['total_price_discount_end_date'] = $value;
98
        }   
99
    }
100
101
    public function getTotalPriceDiscountEndDateAttribute($value)
102
    {
103
        if ($value) {
104
            $date = explode('-', $value);
105
            return $date[2].'/'.$date[1].'/'.$date[0];
106
        }
107
        
108
        return null;
109
    }
110
}