ProductPresenter::getSale()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace App\Libraries\Presenterable\Presenters;
4
5
use App\Traits\HasImagesPresentable;
6
use Carbon\Carbon;
7
8
class ProductPresenter extends Presenter
9
{
10
    use HasImagesPresentable;
11
12
    const END_DATE = 'expire_date';
13
    const PRICE_EMPTY = '0.00';
14
15
    /**
16
     * Render product's name in uppercase.
17
     *
18
     * @return string
19
     */
20
    public function renderName()
21
    {
22
        if($this->model->name)
23
            return strtoupper($this->model->name);
24
25
        return $this->renderDraftedName();
26
    }
27
28
    public function renderDraftedName()
29
    {
30
        if($lot = $this->model->lot)
31
            return sprintf('Drafted #%s product %s', $lot->id, $this->model->id);
32
33
        return sprintf('#tempname%s', str_random(9));
34
    }
35
36
    public function renderNameSimple()
37
    {
38
        return ucfirst($this->model->name);
39
    }
40
41
    public function renderCountItem()
42
    {
43
            return strtoupper($this->model->count);
44
45
    }
46
47
    public function renderPrice($price = null, $currency = null)
48
    {
49
        return sprintf("%s %s", ($price) ? $price : self::PRICE_EMPTY, ($currency) ? $currency : '');
50
    }
51
52
    public function renderNewPrice() {
53
54
        if($price = $this->model->price) {
55
            return $price;
56
        }
57
58
        return self::PRICE_EMPTY;
59
    }
60
61
    public function renderOldPrice()
62
    {
63
        if($price = $this->model->old_price) {
64
            $currency = $this->renderCurrency();
65
            return sprintf('%s %s', $price,$currency);
66
        }
67
68
        return self::PRICE_EMPTY;
69
    }
70
71
    public function renderCurrency($inst = 'title')
72
    {
73
        if($lot = $this->model->lot)
74
        {
75
            if($currency = $lot->currency)
76
                return $currency->$inst;
77
        }
78
79
        return '';
80
    }
81
82
    /**
83
     * Calculate price amount sale.
84
     *
85
     * @return string
86
     */
87
    public function renderSalePercent()
88
    {
89
        if($this->model->price && $this->model->old_price)
90
            return round((($this->model->old_price - $this->model->price) / ($this->model->old_price)) * 100);
91
92
        return 0;
93
    }
94
95
    /**
96
     * Render price with calc. sale..
97
     *
98
     * @param $onlyPrice
99
     * @return string
100
     */
101
   /* public function renderPriceWithSale($onlyPrice = false)
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
    {
103
        $price = $this->reformatPrice($this->model->price - $this->getPriceAmountSale());
104
//        $price = $this->getPriceAmountSale();
105
        $currency = $this->renderCurrency();
106
        if($onlyPrice)
107
            return ($price != 0) ? $price : '';
108
109
        return sprintf('%s %s', $price,$currency);
110
    }*/
111
112
    public function renderPriceWithSale($onlyPrice = false)
113
    {
114
        $price = $this->reformatPrice($this->model->price);
115
//        $price = $this->getPriceAmountSale();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
        $currency = $this->renderCurrency();
117
        if($onlyPrice)
118
            return ($price != 0) ? $price : '';
119
120
        return sprintf('%s %s', $price, $currency);
121
    }
122
123
    public function getSaledPrice()
124
    {
125
        return number_format($this->model->price - $this->getPriceAmountSale());
126
    }
127
128
    /**
129
     * Calculate price amount sale.
130
     *
131
     * @return string
132
     */
133
    public function getPriceAmountSale()
134
    {
135
        return $this->model->sale * $this->model->price / 100;
136
    }
137
138
    /**
139
     * Reformat price.
140
     *
141
     * @param $price
142
     * @return string
143
     */
144
    public function reformatPrice($price)
145
    {
146
        return number_format($price, 0, ',', ' ');
147
    }
148
149
    /**
150
     * Render economy price.
151
     * - rest from sale.
152
     *
153
     * @return string
154
     */
155
    public function economyPrice()
156
    {
157
        $currency = $this->renderCurrency();
158
        $economy = $this->renderPrice($this->reformatPrice($this->getPriceAmountSale()));
159
160
        return sprintf('%s%s',$economy,$currency);
161
    }
162
163
    /**
164
     * Render sale of product.
165
     *
166
     * @return string
167
     */
168
    public function renderSale()
169
    {
170
        return sprintf('%s%%', number_format($this->model->sale));
171
    }
172
173
    /**
174
     * Render end date from expiration_date timestamp.
175
     *
176
     * @param string $format
177
     * @return mixed
178
     */
179
    public function endDate($format = 'm/d/Y')
180
    {
181
        $enddate = self::END_DATE;
182
183
        return $this->model->$enddate->format($format);
184
    }
185
186
    /**
187
     * Calc. difference from (expiration_date and today),
188
     * end_date MUST be bigger than now date.
189
     *
190
     * @return \DateInterval
191
     */
192
    public function diffEndDate()
193
    {
194
        $enddate = self::END_DATE;
195
196
        return $this->model->$enddate->diff(Carbon::now());
197
    }
198
199
    public function getTotalSumm()
200
    {
201
        if ($this->model->count) {
202
            $number = $this->getSaledPrice() * $this->model->count;
203
        }else{
204
            $number = $this->getSaledPrice();
205
        }
206
        return number_format($number);
207
    }
208
209
    public function getSalesSumm()
210
    {
211
        $involveds = $this->model->involved()->active()->get();
212
213
        $totalSalesSumm = 0;
214
215
        $involveds->each(function ($involved) use (&$totalSalesSumm) {
216
            $price = $this->getSaledPrice() * $involved->count;
217
218
            $totalSalesSumm += $price;
219
        });
220
221
        return $totalSalesSumm;
222
    }
223
224
    /**
225
     * @param bool $rotate
226
     * @return float|string
227
     */
228
    public function getSalesPercent($rotate = true)
229
    {
230
        if($this->getSalesSumm() != 0) {
231
            $result = ($this->getSalesSumm() * 100) / $this->getTotalSumm();
232
        }
233
        else {
234
            $result=0;
235
        }
236
        if($rotate)
237
            return number_format(round(number_format($result)));
238
        
239
        return $result;
240
    }
241
242
    public function getSale($showPercent = false)
243
    {
244
        $sale = $this->model->sale;
245
        
246
        if(empty($sale) && $sale == 0)
247
            $sale = 0;
248
249
        if($showPercent)
250
            return sprintf('%s%%', $sale);
251
252
        return $sale;
253
    }
254
255
    public function renderInvolvedPriceSumm($count)
256
    {
257
        $summ = $this->model->price * $count;
258
        
259
        return $this->renderPrice($summ);
260
    }
261
262
    public function getInfoLabel()
263
    {
264
        return '';
265
    }
266
}