fix calculation on indirect articles by tags
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -15,14 +15,23 @@ class TagGroups
|
||||
public static function getWithTagsAndCountOffers($category_id = false)
|
||||
{
|
||||
$data = [];
|
||||
$tags = Tag::withCountArticlesByCategory($category_id)->get()->toArray();
|
||||
$tagGroups = TagGroup::pluck('name', 'id')->toArray();
|
||||
foreach ($tags as $tag) {
|
||||
$data[$tag['tag_group_id']]['name'] = $tagGroups[$tag['tag_group_id']];
|
||||
$data[$tag['tag_group_id']]['tags'][] = [
|
||||
$tagsDirect = Tag::withCountDirectArticlesByCategory($category_id)->get()->toArray();
|
||||
$tagsIndirect = Tag::withCountIndirectArticlesByCategory($category_id)->get()->toArray();
|
||||
foreach ($tagsDirect as $tag) {
|
||||
$data[$tag['tag_group_id']]['name'] = $tag['group'];
|
||||
$data[$tag['tag_group_id']]['tags'][$tag['id']] = [
|
||||
'id' => $tag['id'],
|
||||
'name' => $tag['name'],
|
||||
'count' => $tag['direct_article_count'] + $tag['indirect_article_count'],
|
||||
'count' => $tag['direct_article_count'],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($tagsIndirect as $tag) {
|
||||
$data[$tag['tag_group_id']]['name'] = $tag['group'];
|
||||
$data[$tag['tag_group_id']]['tags'][$tag['id']] = [
|
||||
'id' => $tag['id'],
|
||||
'name' => $tag['name'],
|
||||
'count' => ($data[$tag['tag_group_id']]['tags'][$tag['id']]['count'] ?? 0) + $tag['indirect_article_count'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user