Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Sep 24, 2024
1 parent f290baf commit fb967f9
Show file tree
Hide file tree
Showing 34 changed files with 282 additions and 337 deletions.
1 change: 0 additions & 1 deletion database/factories/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function modelName(): string
public function definition(): array
{
return [
'tax' => mt_rand(10, 100),
'name' => $this->faker->company(),
'price' => mt_rand(10, 100),
'quantity' => mt_rand(1, 10),
Expand Down
3 changes: 1 addition & 2 deletions database/factories/ShippingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function modelName(): string
public function definition(): array
{
return [
'tax' => mt_rand(0, 20),
'cost' => mt_rand(0, 100),
'fee' => mt_rand(0, 100),
'driver' => Manager::getDefaultDriver(),
];
}
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function up(): void
$table->nullableMorphs('buyable');
$table->string('name');
$table->float('price')->unsigned();
$table->float('tax')->unsigned()->default(0);
$table->float('quantity')->unsigned();
$table->json('properties')->nullable();
$table->timestamps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public function up(): void
$table->id();
$table->morphs('shippable');
$table->string('driver');
$table->float('cost')->unsigned()->default(0);
$table->float('tax')->unsigned()->default(0);
$table->float('fee')->unsigned()->default(0);
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function up(): void
{
Schema::create('bazar_tax_rates', static function (Blueprint $table): void {
$table->id();
$table->integer('value')->unsigned();
$table->boolean('shipping')->default(false);
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ 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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public function up(): void
{
Schema::create('bazar_taxes', static function (Blueprint $table): void {
$table->id();
$table->foreignId('tax_rate_id')->nullable()->constrained('bazar_tax_rates')->nullOnDelete();
$table->morphs('taxable');
$table->float('value')->unsigned();
$table->timestamps();

$table->unique(['taxable_id', 'taxable_type', 'tax_rate_id']);
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/BazarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class BazarServiceProvider extends ServiceProvider
Interfaces\Models\Property::class => Models\Property::class,
Interfaces\Models\PropertyValue::class => Models\PropertyValue::class,
Interfaces\Models\Shipping::class => Models\Shipping::class,
Interfaces\Models\Tax::class => Models\Tax::class,
Interfaces\Models\TaxRate::class => Models\TaxRate::class,
Interfaces\Models\Transaction::class => Models\Transaction::class,
Interfaces\Models\Variant::class => Models\Variant::class,
];
Expand All @@ -33,7 +35,6 @@ class BazarServiceProvider extends ServiceProvider
Interfaces\Cart\Manager::class => Cart\Manager::class,
Interfaces\Gateway\Manager::class => Gateway\Manager::class,
Interfaces\Repositories\DiscountRepository::class => Repositories\DiscountRepository::class,
Interfaces\Repositories\TaxRepository::class => Repositories\TaxRepository::class,
Interfaces\Shipping\Manager::class => Shipping\Manager::class,
];

Expand Down
16 changes: 9 additions & 7 deletions src/Cart/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function getModel(): Cart
if (! $cart->locked && $cart->currency !== Bazar::getCurrency()) {
$cart->setAttribute('currency', Bazar::getCurrency());
$cart->syncItems();
$cart->shipping->calculateCost(false);
$cart->shipping->calculateTax();
$cart->shipping->calculateFee();
$cart->shipping->calculateTaxes();
$cart->calculateDiscount();
}
});
Expand Down Expand Up @@ -147,7 +147,7 @@ public function removeItems(array $ids): void
public function updateItem(string $id, array $properties = []): void
{
if ($item = $this->getItem($id)) {
$item->fill($properties)->calculateTax();
$item->fill($properties)->calculateTaxes();

$this->sync();
}
Expand All @@ -161,7 +161,7 @@ public function updateItems(array $data): void
$items = $this->getItems()->whereIn('id', array_keys($data));

$items->each(static function (Item $item) use ($data): void {
$item->fill($data[$item->getKey()])->calculateTax();
$item->fill($data[$item->getKey()])->calculateTaxes();
});

if ($items->isNotEmpty()) {
Expand Down Expand Up @@ -229,7 +229,7 @@ public function empty(): void
$this->getModel()->items()->delete();
$this->getModel()->setRelation('items', $this->getModel()->items()->getRelated()->newCollection());

$this->getShipping()->update(['tax' => 0, 'cost' => 0]);
$this->getShipping()->update(['tax' => 0, 'fee' => 0]);

$this->getModel()->calculateDiscount();
}
Expand Down Expand Up @@ -273,8 +273,10 @@ public function isNotEmpty(): bool
*/
public function sync(): void
{
$this->getShipping()->calculateCost(false);
$this->getShipping()->calculateTax();
$this->getShipping()->calculateFee();
$this->getShipping()->save();

$this->getShipping()->calculateTaxes();

$this->getModel()->calculateDiscount();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Interfaces/Buyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Bazar\Interfaces;

use Cone\Bazar\Models\Item;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

interface Buyable
{
Expand All @@ -11,6 +12,11 @@ interface Buyable
*/
public function buyable(Checkoutable $checkoutable): bool;

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

/**
* Get the item representation of the buyable instance.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/Checkoutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getFormattedTax(): string;
/**
* Calculate the tax.
*/
public function calculateTax(bool $update = true): float;
public function calculateTax(): float;

/**
* Find an item by its attributes or make a new instance.
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/Models/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Shipping extends LineItem
public function shippable(): MorphTo;

/**
* Calculate the cost.
* Calculate the fee.
*/
public function calculateCost(bool $update = true): float;
public function calculateFee(): float;
}
18 changes: 17 additions & 1 deletion src/Interfaces/Models/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@

namespace Cone\Bazar\Interfaces\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

interface Tax
{
//
/**
* Get the taxable model for the model.
*/
public function taxable(): MorphTo;

/**
* Get the tax rate for the model.
*/
public function taxRate(): BelongsTo;

/**
* Get the formatted tax.
*/
public function format(): string;
}
8 changes: 4 additions & 4 deletions src/Interfaces/Models/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
interface Variant extends Buyable, Stockable
{
/**
* Get the items for the product.
* Get the items for the variant.
*/
public function items(): MorphMany;

/**
* Get the orders for the product.
* Get the orders for the variant.
*/
public function orders(): HasManyThrough;

/**
* Get the carts for the product.
* Get the carts for the variant.
*/
public function carts(): HasManyThrough;

/**
* Get the product for the transaction.
* Get the product for the variant.
*/
public function product(): BelongsTo;
}
11 changes: 0 additions & 11 deletions src/Interfaces/Tax.php

This file was deleted.

22 changes: 12 additions & 10 deletions src/Interfaces/Taxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

namespace Cone\Bazar\Interfaces;

use Illuminate\Database\Eloquent\Relations\MorphMany;

interface Taxable
{
/**
* Get the tax.
* Get the taxes for the model.
*/
public function getTax(): float;
public function taxes(): MorphMany;

/**
* Get the formatted tax.
* Get the tax base.
*/
public function getFormattedTax(): string;
public function getTaxBase(): float;

/**
* Get the tax rate.
* Get the tax total.
*/
public function getTaxRate(): float;
public function getTaxTotal(): float;

/**
* Get the formatted tax rate.
* Get the formatted tax.
*/
public function getFormattedTaxRate(): string;
public function getFormattedTaxTotal(): string;

/**
* Calculate the tax.
* Calculate the taxes.
*/
public function calculateTax(bool $update = true): float;
public function calculateTaxes(): float;
}
Loading

0 comments on commit fb967f9

Please sign in to comment.