PriceRepository.php 8.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php

namespace App\Repositories;

use App\Models\Price;
use App\Models\PriceTag;
use App\Models\PriceTagPrice;
use App\Models\PriceCategory;
use App\Models\PriceReview;
use App\Repositories\BaseRepository;

/**
 * Class PriceRepository
 * @package App\Repositories
 * @version August 26, 2019, 2:18 pm UTC
*/

class PriceRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [

    ];

    public function advancedFind($id) {
        $query = $this->model->newQuery();
        $query = $query->where("id", $id)
            ->with("category", "reviews", "products.details", "tags", 'products.description.products.description', 
            'products.description.products.prices', 'products.description.products.details','products.description', 
            'element', 'element.product.description', 'products.description.products.stock', 'products.description.products.detailKeys.detailKey');
        return $query->first();
    }

    public function getPaginatedPrices($filters) {


        $queryCollection = $this->model->whereNull("isHidden");

        $except = ["category_id", "brand_id", "min_price", "max_price"];

        array_map(function($filterKey) use ($except, $filters) {
            if(!in_array($filterKey, $except)) {
                $filterValue = $filters[$filterKey];
                $queryCollection->whereHas("detailKeys", function($query) use ($filterKey, $filterValue) {
                    $query->whereHas("detailKey", function($subQuery) use ($filterKey) {
                        $subQuery->where("tag", $filterKey);
                    })->where("value", $filterValue);
                });
            }
        }, array_keys($filters));

        if($filters["category_id"]) {
            $queryCollection = $queryCollection->where("category_id", $filters["category_id"]);
        }

        if($filters["brand_id"]) {
            $brandId = $filters["brand_id"];
            $queryCollection = $queryCollection->whereHas("products.description", function($query) use ($brandId) {
                $query->where("brandId", $brandId);
            });
        }

        if($filters["min_price"]) {
            $queryCollection = $queryCollection->where("promoPrice", ">=", $filters["min_price"]);
        }

        if($filters["max_price"]) {
            $queryCollection = $queryCollection->where("promoPrice", "<=", $filters["max_price"]);
        }

        return $queryCollection->paginate(10);
    }

    public function getMaxPrice() {
        return Price::max("price");
        // return Price::max("promoPrice");
    }

    public function getMinPrice() {
        return Price::min("price");
        // return Price::min("promoPrice");
    }

    public function addTagToPrice($tag, $priceId) {

        $priceTag = PriceTag::updateOrCreate([
            "tag" => $tag
        ]);

        $priceTagPrice = PriceTagPrice::updateOrCreate([
            "price_id" => $priceId,
            "price_tag_id" => $priceTag->id
        ]);

        return $priceTag;

    }

    public function removeTagToPrice($tag, $priceId) {

        $priceTag = PriceTag::updateOrCreate([
            "tag" => $tag
        ]);

        $priceTagPrice = PriceTagPrice::where("price_id", $priceId)
            ->where("price_tag_id", $priceTag->id)
            ->delete();

        return $priceTagPrice;

    }

    public function getDeliveringPrice() {
        $category = PriceCategory::first();
        $deliveringPrice = setting('constants.DELIVERING_PRICE');
        if($category && $deliveringPrice) {
            return $this->model->updateOrCreate([
                'label' => config('constants.DELIVERING_PRICE_LABEL'),
                'price' => $deliveringPrice,
                'image' => "---",
                'isHidden' => true
            ], [
                'category_id' => $category->id
            ]);
        } else {
            return null;
        }
    }

    public function getPricesFavoriteByUser($userId) {}

    public function getPricesByEntreprise($entrepriseId) {}

    public function getPricesQuery($entreprise = null, $with = [], $whereHas = []) {
        $simpleProducts = $this->getSimplePricesQuery($entreprise, $with, $whereHas);
        // $cdompositPrices = $this->getCompositPricesQuery($entreprise, $with, $whereHas);
        return $simpleProducts;
    }

    public function getBasedQuery($entreprise, $with = [], $whereHas = []) {

        $query = $this->model->newQuery();

        $query->where("isHidden", false);

        if(sizeOf($with) > 0) {
            array_map(function($relation) use ($query) {
                $query = $query->with($relation);
            }, $with);
        }

        if(sizeOf($whereHas) > 0) {
            $relationNames = array_keys($whereHas);
            array_map(function($name) use ($query, $whereHas) {
                $query = $query->whereHas($name, $whereHas[$name]);
            }, $relationNames);
        }

        if ($entreprise == null) { $query->where("isStandard", false); } else {
            $query->where(function ($subQuery) use ($entreprise) {
                $subQuery->where("isStandard", false)->orWhereIn(
                    "Price.id",
                    $entreprise->prices->pluck("priceId")
                );
            });
        }

        return $query;
    }

    public function getRecommendedPricesQuery($entreprise = null) {
        $query = $this->getBasedQuery($entreprise);
        $query->where("isRecommended", 1);
        $query->has("products", 1);
        return $query;
    }

    public function getSimplePricesQuery($entreprise = null, $with = [], $whereHas = []) {
        $query = $this->getBasedQuery($entreprise, $with, $whereHas);
        $query->has("products", 1);
        return $query;
    }

    public function getCompositPricesQuery($entreprise = null, $with = [], $whereHas = []) {
        $query = $this->getBasedQuery($entreprise, $with, $whereHas);
        $query->has("products", "=", 1);
        return $query;
    }

    public function firstOrCreateCompositPrice($inputs, $productListId) {

        $compositPrice = $this->model->has('products', sizeOf($productListId));
        foreach ($productListId as $key => $productId) {
            $compositPrice = $compositPrice->whereHas('products', function($subQuery) use ($productId) {
                $subQuery->where('Product.id', $productId);
            });
        }
        $compositPrice = $compositPrice->first();

        if($compositPrice) { return $compositPrice; } else {

            $priceModel = $this->model->create([
                'isHidden' => true,
                'image' => $inputs["image"],
                'label' => $inputs["label"],
                'category_id' => $inputs["categoryId"],
                'price' => $inputs["price"],
                'promoPrice' => $inputs["promoPrice"]
            ]);

            $combinedProducts = array_map(function($productId) use ($priceModel) {
                return [
                    "productId" => $productId,
                    "priceId" => $priceModel->priceId,
                    "quantity" => 1
                ];
            }, $productListId);

            $priceModel->combinedProducts()->createMany(
                $combinedProducts
            );

            return $priceModel;

        }

    }


    public function findPricesQuery($withQuery, $search) {
        $query = Price::whereHas('products', function ($query) use($search){
            $query->whereHas('description', function ($subQ) use($search) {
                $subQ->where('label', 'LIKE', '%' . $search . '%')
                ->orWhere('description', 'LIKE', '%' . $search . '%');
            });
        })->with($withQuery)->get();
        $query->has("products", 1);
        // ->orWhere('price', '=', $search)->orWhere('promoPrice', '=', $search)
        return $query;
    }

    public function getPricesByIds($list)
    {
246
        return $this->model->withTrashed()->whereIn('id', $list)
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
            ->with("products.description.products.detailKeys.detailKey", "products.detailKeys.detailKey", "commandPrice")
            ->get();
    }

    /**
     * Return searchable fields
     *
     * @return array
     */
    public function getFieldsSearchable()
    {
        return $this->fieldSearchable;
    }

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Price::class;
    }

}