Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Aug 10, 2024
1 parent bb80afb commit ce380c2
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public function up(): void
{
Schema::create('bazar_discount_rates', function (Blueprint $table) {
Schema::create('bazar_discount_rates', static function (Blueprint $table): void {
$table->id();
$table->string('name');
$table->string('type');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
public function up(): void
{
Schema::create('bazar_discounts', function (Blueprint $table) {
Schema::create('bazar_discounts', static function (Blueprint $table): void {
$table->id();
$table->foreignId('discount_rate_id')->constrained('bazar_discount_rates')->nullOnDelete();
$table->foreignId('discount_rate_id')->nullable()->constrained('bazar_discount_rates')->nullOnDelete();
$table->morphs('discountable');
$table->float('value')->unsigned();
$table->timestamps();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('bazar_buyable_tax_rate', static function (Blueprint $table): void {
$table->id();
$table->foreignId('tax_rate_id')->constrained('bazar_tax_rates')->cascadeOnDelete();
$table->morphs('buyable');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('bazar_buyable_tax_rate');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
public function up(): void
{
Schema::create('bazar_taxes', function (Blueprint $table) {
Schema::create('bazar_taxes', static function (Blueprint $table): void {
$table->id();
$table->foreignId('tax_rate_id')->constrained('bazar_tax_rates')->nullOnDelete();
$table->foreignId('item_id')->constrained('bazar_items')->nullOnDelete();
$table->foreignId('tax_rate_id')->nullable()->constrained('bazar_tax_rates')->nullOnDelete();
$table->foreignId('item_id')->nullable()->constrained('bazar_items')->nullOnDelete();
$table->float('value')->unsigned();
$table->timestamps();
});
Expand Down
5 changes: 5 additions & 0 deletions src/Interfaces/Models/DiscountRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface DiscountRate
*/
public function applicable(Discountable $model): bool;

/**
* Calculate the discount for the given model.
*/
public function calculate(Discountable $model): float;

/**
* Apply the discount rate on the model.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Interfaces/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

interface Product extends Buyable, Stockable
{
Expand Down Expand Up @@ -37,6 +38,11 @@ public function categories(): BelongsToMany;
*/
public function variants(): HasMany;

/**
* Get the tax rates for the product.
*/
public function taxRates(): MorphToMany;

/**
* Get the variant of the given option.
*/
Expand Down
18 changes: 17 additions & 1 deletion src/Interfaces/Models/TaxRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@

namespace Cone\Bazar\Interfaces\Models;

use Cone\Bazar\Interfaces\Taxable;
use Cone\Bazar\Models\Tax;

interface TaxRate
{
//
/**
* Determine wheter the discount rate is applicable on the model.
*/
public function applicable(Taxable $model): bool;

/**
* Calculate the discount for the given model.
*/
public function calculate(Taxable $model): float;

/**
* Apply the discount rate on the model.
*/
public function apply(Taxable $model): ?Tax;
}
9 changes: 9 additions & 0 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder as QueryBuilder;

Expand Down Expand Up @@ -91,6 +92,14 @@ public function variants(): HasMany
return $this->hasMany(Variant::getProxiedClass());
}

/**
* Get the tax rates for the product.
*/
public function taxRates(): MorphToMany
{
return $this->morphToMany(TaxRate::getProxiedClass(), 'buyable', 'bazar_buyable_tax_rate');
}

/**
* Determine whether the buyable object is available for the checkoutable instance.
*/
Expand Down
32 changes: 32 additions & 0 deletions src/Models/TaxRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cone\Bazar\Database\Factories\TaxRateFactory;
use Cone\Bazar\Interfaces\Models\TaxRate as Contract;
use Cone\Bazar\Interfaces\Taxable;
use Cone\Root\Traits\InteractsWithProxy;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -43,4 +44,35 @@ protected static function newFactory(): Factory
{
return TaxRateFactory::new();
}

/**
* Determine wheter the discount rate is applicable on the model.
*/
public function applicable(Taxable $model): bool
{
return true;
}

/**
* Calculate the discount for the given model.
*/
public function calculate(Taxable $model): float
{
return 0;
}

/**
* Apply the discount rate on the model.
*/
public function apply(Taxable $model): ?Tax
{
if (! $this->applicable($model)) {
return null;
}

return $model->taxes()->updateOrCreate(
['discount_rate_id' => $this->getKey()],
['value' => $this->calculate($model)]
);
}
}

0 comments on commit ce380c2

Please sign in to comment.