|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\TaxRate; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use Hideyo\Ecommerce\Framework\Services\TaxRate\Entity\TaxRateRepository; |
|
7
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
|
8
|
|
|
|
|
9
|
|
|
class TaxRateService extends BaseService |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct(TaxRateRepository $taxRate) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->repo = $taxRate; |
|
|
|
|
|
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The validation rules for the model. |
|
18
|
|
|
* |
|
19
|
|
|
* @param integer $taxRateId id attribute model |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public function rules($taxRateId = false) |
|
23
|
|
|
{ |
|
24
|
|
|
$rules = array( |
|
25
|
|
|
'title' => 'required|between:2,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id', |
|
26
|
|
|
'rate' => 'numeric|required' |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
if($taxRateId) { |
|
|
|
|
|
|
30
|
|
|
$rules['title'] = $rules['title'].','.$taxRateId.' = id'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $rules; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* create model |
|
38
|
|
|
* @param array $attributes |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
public function create(array $attributes) |
|
42
|
|
|
{ |
|
43
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
44
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
|
45
|
|
|
|
|
46
|
|
|
if ($validator->fails()) { |
|
47
|
|
|
return $validator; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
51
|
|
|
return $this->updateOrAddModel($this->repo->getModel(), $attributes); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* update model by id |
|
56
|
|
|
* @param array $attributes |
|
57
|
|
|
* @param integer $taxRateId |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function updateById(array $attributes, $taxRateId) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
63
|
|
|
$validator = Validator::make($attributes, $this->rules($id)); |
|
|
|
|
|
|
64
|
|
|
if ($validator->fails()) { |
|
65
|
|
|
return $validator; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$model = $this->find($id); |
|
69
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
70
|
|
|
return $this->updateOrAddModel($model, $attributes); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |