Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/rapidez/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@
'search_synonym' => Rapidez\Core\Models\SearchSynonym::class,
'store' => Rapidez\Core\Models\Store::class,
'super_attribute' => Rapidez\Core\Models\SuperAttribute::class,
'weee_tax' => Rapidez\Core\Models\WeeeTax::class,
'widget' => Rapidez\Core\Models\Widget::class,
];
9 changes: 9 additions & 0 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ public function tierPrices(): HasMany
)->whereIn('website_id', [0, config('rapidez.website')]);
}

public function fixedProductTaxes(): HasMany
{
return $this->hasMany(
config('rapidez.models.weee_tax', WeeeTax::class),
'entity_id',
'entity_id',
)->afterQuery(fn ($results) => $results->keyBy('attribute_code'));
}

public function getUnitPrice(int $quantity = 1, int $customerGroup = 0)
{
$tierPrice = $this->tierPrices()
Expand Down
56 changes: 56 additions & 0 deletions src/Models/Scopes/ForCurrentWebsiteWithoutLimitScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Rapidez\Core\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\DB;

/**
* Remove results from the default website when website view specific is found.
*/
class ForCurrentWebsiteWithoutLimitScope implements Scope
{
public array $uniquePerWebsiteKeys;

/**
* @param string|array $uniquePerWebsiteKey column(s) that may be duplicate across websites, but must be unique when filtered by website.
*/
public function __construct(string|array $uniquePerWebsiteKey, public $websiteIdColumn = 'website_id')
{
$this->uniquePerWebsiteKeys = is_array($uniquePerWebsiteKey) ? $uniquePerWebsiteKey : [$uniquePerWebsiteKey];
}

public function apply(Builder $query, Model $model)
{
if (! config('rapidez.website')) {
return $query
->where($query->qualifyColumn($this->websiteIdColumn), 0);
}

$scope = $this;

return $query
// Pre-filter results to be default and current website only.
->whereIn($query->qualifyColumn($this->websiteIdColumn), [0, config('rapidez.website')])
// Remove values from the default website where values for the current website exist.
->where(fn ($query) => $query
// Remove values where we already have values in the current website.
->where(function ($query) use ($scope, $model) {
$query
->whereNotExists(function ($query) use ($scope, $model) {
$query
->select(DB::raw(1))
->from($model->getTable() . ' as comparison')
->where('comparison.' . $this->websiteIdColumn, config('rapidez.website'));
foreach ($scope->uniquePerWebsiteKeys as $uniquePerWebsiteKey) {
$query->whereColumn('comparison.' . $uniquePerWebsiteKey, $model->qualifyColumn($uniquePerWebsiteKey));
}
});
})
// Unless the value IS the current website.
->orWhere($query->qualifyColumn($this->websiteIdColumn), config('rapidez.website'))
);
}
}
36 changes: 36 additions & 0 deletions src/Models/WeeeTax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rapidez\Core\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Rapidez\Core\Models\Scopes\ForCurrentWebsiteWithoutLimitScope;

class WeeeTax extends Model
{
const CREATED_AT = null;
const UPDATED_AT = null;
Comment thread
Jade-GG marked this conversation as resolved.
Outdated

protected $table = 'weee_tax';
protected $primaryKey = 'value_id';

protected $casts = [
'value' => 'float',
];

protected static function booting()
{
static::addGlobalScope('website', new ForCurrentWebsiteWithoutLimitScope(['entity_id', 'attribute_id']));
static::addGlobalScope('withAttributeData', fn (Builder $query) => $query->leftJoin('eav_attribute', 'eav_attribute.attribute_id', 'weee_tax.attribute_id')
);
}

public function product(): BelongsTo
{
return $this->belongsTo(
config('rapidez.models.product'),
'entity_id',
'entity_id',
);
}
}
Loading