fix calculation on indirect articles by tags

This commit is contained in:
ludo
2025-01-03 14:45:23 +01:00
parent df78126b12
commit 2e71f17856
2 changed files with 18 additions and 26 deletions

View File

@@ -70,19 +70,16 @@ class Tag extends parentTag
return $query->where($this->table.'.tag_group_id', $id);
}
public function scopeWithCountArticlesByCategory($query, $categoryId)
public function scopeWithCountDirectArticlesByCategory($query, $categoryId)
{
return $query->withCount([
'articles as direct_article_count' => function ($query) use ($categoryId) {
$query->byCategoryParent($categoryId)->withAvailableOffers();
},
'articles as indirect_article_count' => function ($query) use ($categoryId) {
$query->byCategoryParent($categoryId)->withAvailableOffers()->whereHasMorph('product', [Variety::class], function () {});
},
])->havingRaw('(COALESCE(direct_article_count, 0) + COALESCE(indirect_article_count, 0)) > 0');
])->having('direct_article_count', '>', 0);
}
public function scopeWithCountArticlesByProductByCategory($query, $categoryId)
public function scopeWithCountIndirectArticlesByCategory($query, $categoryId)
{
return $query->select(['tags.*'])
->selectRaw('(SELECT COUNT(*)
@@ -124,20 +121,6 @@ class Tag extends parentTag
->having('indirect_article_count', '>', 0);
}
public function scopeWithIndirectArticleCount($query, $categoryId = null)
{
return $query->withCount([
'articles as indirect_article_count' => function ($articleQuery) use ($categoryId) {
$articleQuery->whereHasMorph('product', [Variety::class], function ($varietyQuery) {
$varietyQuery->whereHas('tags', function ($tagQuery) {
$tagQuery->whereColumn('tags.id', 'taggables.tag_id');
});
})->byCategoryParent($categoryId);
},
])
->having('indirect_article_count', '>', 0); // Garde uniquement les tags ayant au moins un article indirect
}
public function scopeById($query, $id)
{
return $query->where($this->table.'.id', $id);