diff --git a/app/Http/Controllers/Shop/HomeController.php b/app/Http/Controllers/Shop/HomeController.php index 7f9b871c..1ef9d994 100644 --- a/app/Http/Controllers/Shop/HomeController.php +++ b/app/Http/Controllers/Shop/HomeController.php @@ -19,7 +19,9 @@ class HomeController extends Controller public function index() { $data = self::init(); - $data['offers'] = Offers::getLast()->toArray(); + // $data['offers'] = Offers::getLast()->toArray(); + $data['articles'] = Articles::getArticlesWithOffers()->toArray(); + // $data['prices'] = $data['articles']['offers'][0]['tariff']['price_lists'][0]['price_list_values'][0]; dump($data); exit; return view('Shop.home', $data); diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index 52060aaa..4aa14757 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -11,13 +11,14 @@ use Rinvex\Tags\Traits\Taggable; use Kirschbaum\PowerJoins\PowerJoins; use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin; use Wildside\Userstamps\Userstamps; +use Staudenmeir\EloquentHasManyDeep\HasRelationships; use App\Traits\Model\HasComments; use App\Traits\Model\Imageable; class Article extends Model implements HasMedia { - use Categorizable, EloquentJoin, HasComments, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps; + use Categorizable, EloquentJoin, HasComments, HasRelationships, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps; protected $guarded = ['id']; protected $table = 'shop_articles'; @@ -37,10 +38,12 @@ class Article extends Model implements HasMedia return $this->hasMany(Offer::class); } + /* public function prices() { - return $this->hasMany(Price::class); + return $this->hasManyDeep(PriceListValue::class, [Offer::class, Tariff::class, PriceList::class]); } + */ public function product() { @@ -52,6 +55,12 @@ class Article extends Model implements HasMedia return $this->morphToMany(Tag::class, 'taggable'); } + public function tariffs() + { + return $this->hasManyThrough(Tariff::class, Offer::class, 'article_id', 'id', 'id', 'tariff_id'); + // return $this->belongsToMany(Tariff::class, Offer::$table, 'id', 'id', 'tariff_id', 'tariff_id'); + } + public function scopeByArticle($query, $id) { return $query->where($this->table . '.id', $id); @@ -89,10 +98,15 @@ class Article extends Model implements HasMedia return $query->has('offers'); } - public function scopeWithCurrentOffers($query) + public function scopeWithAvailableOffers($query) { return $query->whereHas('offers', function ($query) { $query->where('status_id', 1); }); } + + public function scopeVisible($query) + { + return $query->where($this->table . '.visible', 1); + } } diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index 30b7a77a..6431600b 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -18,16 +18,6 @@ class Offer extends Model return $this->belongsTo(Article::class); } - public function categories() - { - return $this->article->categories(); - } - - public function tags() - { - return $this->article->tags(); - } - public function tariff() { return $this->belongsTo(Tariff::class); @@ -38,14 +28,54 @@ class Offer extends Model return $this->belongsTo(Variation::class); } + public function price_lists() + { + return $this->hasManyThrough(PriceList::class, Tariff::class, 'id', 'tariff_id', 'id', 'id'); + } + + public function categories() + { + return $this->article->categories(); + } + + public function tags() + { + return $this->article->tags(); + } + + public function get_price_lists() + { + return $this->tariff->price_lists(); + } + + public function get_price_list_values() + { + return $this->tariff->price_lists->price_list_values(); + } + public function scopeActive($query) { - return $query->where('status_id', 1); + return $query->where($this->table . '.status_id', 1); } public function scopeByArticle($query, $id) { - return $query->where('article_id', $id); + return $query->where($this->table . '.article_id', $id); + } + + public function scopeByOffer($query, $id) + { + return $query->where($this->table . '.id', $id); + } + + public function scopeByStatus($query, $id) + { + return $query->where($this->table . '.status_id', $id); + } + + public function scopeByVariation($query, $id) + { + return $query->where($this->table . '.variation_id', $id); } public function scopeByArticleNature($query, $article_nature_id) @@ -69,11 +99,6 @@ class Offer extends Model }); } - public function scopeByStatus($query, $id) - { - return $query->where('status_id', $id); - } - public function scopeByTag($query, $tag_id) { return $query->whereHas('article.tags', function ($query) use ($tag_id) { @@ -88,8 +113,4 @@ class Offer extends Model }); } - public function scopeByVariation($query, $id) - { - return $query->where('variation_id', $id); - } } diff --git a/app/Models/Shop/PriceList.php b/app/Models/Shop/PriceList.php index 251b58e3..3f7bbf37 100644 --- a/app/Models/Shop/PriceList.php +++ b/app/Models/Shop/PriceList.php @@ -20,6 +20,11 @@ class PriceList extends Model return $this->belongsTo(Tariff::class); } + public function offers() + { + return $this->hasManyThrough(Offer::class, Tariff::class, 'id', 'tariff_id', 'id', 'id'); + } + public function sale_channel() { return $this->belongsTo(SaleChannel::class); @@ -44,4 +49,11 @@ class PriceList extends Model { return $query->where($this->table . '.status_id', $id); } + + public function scopeByOffer($query, $id) + { + return $query->whereHas('offers', function ($query) use ($id) { + $query->byOffer($id); + }); + } } diff --git a/app/Models/Shop/PriceListValue.php b/app/Models/Shop/PriceListValue.php index 72d133f6..9c34cf25 100644 --- a/app/Models/Shop/PriceListValue.php +++ b/app/Models/Shop/PriceListValue.php @@ -20,6 +20,20 @@ class PriceListValue extends Model return $this->belongsTo(PriceList::class); } + public function tariff() + { + return $this->belongsToThrough( + 'App\Models\Shop\Tariff', + 'App\Models\Shop\PriceList', + null, + '', + [ + 'App\Models\Shop\Tariff' => 'tariff_id', + 'App\Models\Shop\PriceList' => 'price_list_id' + ] + ); + } + public function scopeByPriceList($query, $id) { return $query->where($this->table . '.price_list_id', $id); diff --git a/app/Models/Shop/Tariff.php b/app/Models/Shop/Tariff.php index d63d5dd7..674477ef 100644 --- a/app/Models/Shop/Tariff.php +++ b/app/Models/Shop/Tariff.php @@ -15,6 +15,11 @@ class Tariff extends Model protected $guarded = ['id']; protected $table = 'shop_tariffs'; + + public function offers() + { + return $this->hasMany(Offer::class); + } public function sale_channel() { @@ -37,6 +42,11 @@ class Tariff extends Model return $this->hasMany(PriceList::class); } + public function price_list_values() + { + return $this->hasManyThrough(PriceListValue::class, PriceList::class); + } + public function scopeByAutocomplete($query, $str) { return $query->where($this->table . '.name', 'LIKE', "%${str}%") @@ -53,4 +63,11 @@ class Tariff extends Model { return $query->where($this->table . '.status_id', $id); } + + public function scopeByOffer($query, $id) + { + return $query->whereHas('offers', function ($query) use ($id) { + $query->where('id', $id); + }); + } } diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 948bd6dc..4622f856 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -19,7 +19,7 @@ class Articles public static function autocomplete($str) { - $data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->get()->pluck('name', 'id'); + $data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->pluck('name', 'id'); $export = []; foreach ($data as $key => $name) { $export[] = ['value' => $key, 'text' => $name]; @@ -59,9 +59,20 @@ class Articles return $data; } + public static function getArticlesToSell() + { + $articles = self::getArticlesWithOffers(); + foreach ($articles as $article) { + $data[$article->article_nature->name][$article->name][] = [ + 'description' => $article->description, + ]; + } + return $data; + } + public static function getArticlesWithOffers() { - return Article::withCurrentOffers()->with('offers')->get(); + return Article::visible()->withAvailableOffers()->with(['image', 'article_nature', 'offers.tariff.price_lists.price_list_values'])->get(); } public static function getFull($id) diff --git a/app/Repositories/Shop/PriceLists.php b/app/Repositories/Shop/PriceLists.php index 46be7adb..99d1f3c3 100644 --- a/app/Repositories/Shop/PriceLists.php +++ b/app/Repositories/Shop/PriceLists.php @@ -11,6 +11,11 @@ use App\Models\Shop\PriceList; class PriceLists { + public static function getByOffer($offer_id) + { + return PriceList::byOffer($offer_id)->get(); + } + public static function getStatus($status_id) { return self::getStatuses()[$status_id]; diff --git a/app/Repositories/Shop/Tariffs.php b/app/Repositories/Shop/Tariffs.php index b84cb16c..9d75be82 100644 --- a/app/Repositories/Shop/Tariffs.php +++ b/app/Repositories/Shop/Tariffs.php @@ -16,6 +16,11 @@ class Tariffs return $export; } + public static function getByOffer($id) + { + return Tariff::byOffer($id)->first(); + } + public static function getPrices($id) { return Tariff::with(['price_lists.price_list_values', 'price_lists.sale_channel'])->find($id); @@ -33,7 +38,7 @@ class Tariffs public static function getStatuses() { - return ['Actif','Suspendu','Invisible','Obsolete']; + return ['Actif', 'Suspendu', 'Invisible', 'Obsolete']; } public static function getAll() diff --git a/composer.json b/composer.json index 6b0cc000..bda8c39a 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "jenssegers/date": "^4.0", "kalnoy/nestedset": "^5.0", "kirschbaum-development/eloquent-power-joins": "^2.3", + "kmlaravel/laravel-geographical-calculator": "^2.1", "knplabs/knp-snappy": "^1.2", "laracasts/utilities": "^3.0", "laravel/framework": "^8.42", @@ -79,6 +80,7 @@ "spatie/laravel-backup": "^6.16", "spatie/laravel-mail-preview": "^4.0", "spatie/laravel-medialibrary": "^9.6", + "spatie/laravel-stats": "^1.0", "staudenmeir/belongs-to-through": "^2.11", "staudenmeir/eloquent-has-many-deep": "^1.13", "stillat/numeral.php": "^2.0", diff --git a/package.json b/package.json index ac6810f0..af775772 100644 --- a/package.json +++ b/package.json @@ -118,6 +118,7 @@ "jstree": "^3.3.11", "jszip": "^3.6.0", "laravel-echo": "^1.8.1", + "list.js": "^2.3.1", "modernizr": "^3.11.3", "moment": "^2.27.0", "morris.js": "^0.5.0", diff --git a/resources/views/Shop/Articles/partials/article.blade.php b/resources/views/Shop/Articles/partials/article.blade.php new file mode 100644 index 00000000..2e63666c --- /dev/null +++ b/resources/views/Shop/Articles/partials/article.blade.php @@ -0,0 +1,25 @@ + +
+ ... +
+ {{ $article['name'] }} + + + + +

+

+
+ 3.50 €
+ Semence +
+ +
+ 1.65 €
+ Plant +
+
+

+
+
+
diff --git a/resources/views/Shop/layout/partials/category_articles.blade.php b/resources/views/Shop/layout/partials/category_articles.blade.php index 3440339d..756fcce1 100644 --- a/resources/views/Shop/layout/partials/category_articles.blade.php +++ b/resources/views/Shop/layout/partials/category_articles.blade.php @@ -1,7 +1,7 @@
- @foreach ($offers as $offer) + @foreach ($articles as $article)
- @include('Shop.Offers.offer') + @include('Shop.Articles.partials.article')
@endforeach
\ No newline at end of file