Skip to content

Commit

Permalink
use default number currency
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 3, 2024
1 parent 37a56c6 commit e5e3557
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/back-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
matrix:
os: ["ubuntu-22.04"]
php: ["8.1", "8.2"]
php: ["8.2", "8.3"]
laravel: ["^10.0"]
dependency-version: ["highest"]

Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:
run: composer exec -- phpunit

- name: Send coverage to Coveralls
if: ${{ matrix.os == 'ubuntu-22.04' && matrix.php == '8.1' && matrix.laravel == '^10.0' }}
if: ${{ matrix.os == 'ubuntu-22.04' && matrix.php == '8.2' && matrix.laravel == '^10.0' }}
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
}
},
"require": {
"php": "^8.1 || ^8.2",
"php": "^8.2 || ^8.3",
"conedevelopment/root": "@dev"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"laravel/laravel": "^10.0",
"mockery/mockery": "^1.4.4",
"phpunit/phpunit": "^10.0",
"nunomaduro/larastan": "^2.1.6",
"laravel/pint": "^1.6"
"laravel/pint": "^1.6",
"larastan/larastan": "^2.8"
},
"extra": {
"laravel": {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/emails/customer-new-order.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@foreach ($order->items as $item)
| {{ $item->name }} | {{ $item->quantity }} | {{ $item->formattedTax }} | {{ $item->formattedPrice }} |
@endforeach
| **Subtotal** ||| {{ Str::currency($order->items->sum('total'), $order->currency) }} |
| **Subtotal** ||| {{ $order->formattedSubtotal }} |
@endcomponent

**Discount**: {{ $order->formattedDiscount }}
Expand Down
14 changes: 0 additions & 14 deletions src/BazarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Auth\Events\Logout;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

class BazarServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -58,7 +57,6 @@ public function boot(): void
}

$this->registerEvents();
$this->registerMacros();
$this->registerViews();
}

Expand Down Expand Up @@ -99,18 +97,6 @@ protected function registerCommands(): void
]);
}

/**
* Register macros.
*/
protected function registerMacros(): void
{
Str::macro('currency', static function (int|float $value, string $currency = null): string {
return sprintf(
'%s %s', number_format($value), strtoupper($currency ?: Bazar::getCurrency())
);
});
}

/**
* Register events.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Fields/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Cone\Root\Fields\Text;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Number as NumberFormatter;

class Items extends MorphMany
{
Expand Down Expand Up @@ -61,14 +61,14 @@ public function fields(Request $request): array
->min(0)
->required()
->format(static function (Request $request, Model $model, ?float $value): string {
return Str::currency($value, $model->itemable->currency);
return NumberFormatter::currency($value, $model->itemable->currency);
}),

Number::make(__('TAX'), 'tax')
->min(0)
->required()
->format(static function (Request $request, Model $model, ?float $value): string {
return Str::currency($value, $model->itemable->currency);
return NumberFormatter::currency($value, $model->itemable->currency);
}),

Number::make(__('Quantity'), 'quantity')
Expand Down
3 changes: 2 additions & 1 deletion src/Fields/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cone\Root\Fields\Meta;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Number;
use Illuminate\Support\Str;

class Price extends Meta
Expand All @@ -29,7 +30,7 @@ public function __construct(string $label, string $currency = null)
$this->field->min(0);

$this->field->format(function (Request $request, Model $model, mixed $value): ?string {
return is_null($value) ? null : Str::currency($value, $this->currency);
return is_null($value) ? null : Number::currency($value, $this->currency);
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/Fields/Transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cone\Root\Fields\Text;
use Cone\Root\Fields\URL;
use Illuminate\Http\Request;
use Illuminate\Support\Number as NumberFormatter;
use Illuminate\Support\Str;

class Transactions extends HasMany
Expand Down Expand Up @@ -50,7 +51,7 @@ public function fields(Request $request): array
->min(0)
->required()
->format(static function (Request $request, Transaction $transaction, ?float $value): string {
return Str::currency($value, $transaction->order->currency);
return NumberFormatter::currency($value, $transaction->order->currency);
}),

Select::make(__('Driver'), 'driver')
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

class Item extends Model implements Contract
{
Expand Down Expand Up @@ -198,7 +198,7 @@ public function getPrice(): float
*/
public function getFormattedPrice(): string
{
return Str::currency($this->getPrice(), $this->itemable->getCurrency());
return Number::currency($this->getPrice(), $this->itemable->getCurrency());
}

/**
Expand All @@ -214,7 +214,7 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Str::currency($this->getTotal(), $this->itemable->getCurrency());
return Number::currency($this->getTotal(), $this->itemable->getCurrency());
}

/**
Expand All @@ -230,7 +230,7 @@ public function getSubtotal(): float
*/
public function getFormattedSubtotal(): string
{
return Str::currency($this->getSubtotal(), $this->itemable->getCurrency());
return Number::currency($this->getSubtotal(), $this->itemable->getCurrency());
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Models/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use Throwable;

Expand Down Expand Up @@ -202,7 +203,7 @@ public function getPrice(): float
*/
public function getFormattedPrice(): string
{
return Str::currency($this->getPrice(), $this->shippable->getCurrency());
return Number::currency($this->getPrice(), $this->shippable->getCurrency());
}

/**
Expand All @@ -218,7 +219,7 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Str::currency($this->getTotal(), $this->shippable->getCurrency());
return Number::currency($this->getTotal(), $this->shippable->getCurrency());
}

/**
Expand All @@ -234,7 +235,7 @@ public function getSubtotal(): float
*/
public function getFormattedSubtotal(): string
{
return Str::currency($this->getSubtotal(), $this->shippable->getCurrency());
return Number::currency($this->getSubtotal(), $this->shippable->getCurrency());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Cone\Bazar\Models\Price;
use Cone\Bazar\Relations\Prices;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

trait HasPrices
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public function getFormattedPrice(string $currency = null): ?string

$price = $this->getPrice($currency);

return is_null($price) ? null : Str::currency($price, $currency);
return is_null($price) ? null : Number::currency($price, $currency);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Traits/InteractsWithDiscounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cone\Bazar\Support\Facades\Discount;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Number;
use Illuminate\Support\Str;

trait InteractsWithDiscounts
Expand Down Expand Up @@ -33,7 +34,7 @@ public function getDiscount(): float
*/
public function getFormattedDiscount(): string
{
return Str::currency($this->getDiscount(), $this->currency);
return Number::currency($this->getDiscount(), $this->currency);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Traits/InteractsWithItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

trait InteractsWithItems
{
Expand Down Expand Up @@ -221,7 +221,7 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Str::currency($this->getTotal(), $this->getCurrency());
return Number::currency($this->getTotal(), $this->getCurrency());
}

/**
Expand All @@ -241,7 +241,7 @@ public function getSubtotal(): float
*/
public function getFormattedSubtotal(): string
{
return Str::currency($this->getSubtotal(), $this->getCurrency());
return Number::currency($this->getSubtotal(), $this->getCurrency());
}

/**
Expand All @@ -261,7 +261,7 @@ public function getFeeTotal(): float
*/
public function getFormattedFeeTotal(): string
{
return Str::currency($this->getFeeTotal(), $this->getCurrency());
return Number::currency($this->getFeeTotal(), $this->getCurrency());
}

/**
Expand All @@ -281,7 +281,7 @@ public function getTax(): float
*/
public function getFormattedTax(): string
{
return Str::currency($this->getTax(), $this->getCurrency());
return Number::currency($this->getTax(), $this->getCurrency());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/InteractsWithTaxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Cone\Bazar\Models\Shipping;
use Cone\Bazar\Support\Facades\Tax;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

trait InteractsWithTaxes
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public function getFormattedTax(): string
$currency = $this->shippable->currency;
}

return Str::currency($this->getTax(), $currency);
return Number::currency($this->getTax(), $currency);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Models/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Cone\Bazar\Models\Product;
use Cone\Bazar\Support\Facades\Tax;
use Cone\Bazar\Tests\TestCase;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

class ItemTest extends TestCase
{
Expand All @@ -35,15 +35,15 @@ public function setUp(): void
public function test_item_is_taxable(): void
{
$this->assertInstanceOf(Taxable::class, $this->item);
$this->assertSame(Str::currency($this->item->tax, $this->item->itemable->currency), $this->item->getFormattedTax());
$this->assertSame(Number::currency($this->item->tax, $this->item->itemable->currency), $this->item->getFormattedTax());
$this->assertSame($this->item->getFormattedTax(), $this->item->formattedTax);
}

public function test_item_has_price_attribute(): void
{
$this->assertSame($this->item->price, $this->item->getPrice());
$this->assertSame(
Str::currency($this->item->price, $this->item->itemable->currency), $this->item->getFormattedPrice()
Number::currency($this->item->price, $this->item->itemable->currency), $this->item->getFormattedPrice()
);
$this->assertSame($this->item->getFormattedPrice(), $this->item->formattedPrice);
}
Expand All @@ -56,14 +56,14 @@ public function test_item_has_total_attribute(): void
);
$this->assertSame($this->item->getTotal(), $this->item->total);
$this->assertSame(
Str::currency($this->item->total, $this->item->itemable->currency),
Number::currency($this->item->total, $this->item->itemable->currency),
$this->item->getFormattedTotal()
);
$this->assertSame($this->item->getFormattedTotal(), $this->item->formattedTotal);
$this->assertSame($this->item->price * $this->item->quantity, $this->item->getSubtotal());
$this->assertSame($this->item->getSubtotal(), $this->item->subtotal);
$this->assertSame(
Str::currency($this->item->subtotal, $this->item->itemable->currency),
Number::currency($this->item->subtotal, $this->item->itemable->currency),
$this->item->getFormattedSubtotal()
);
$this->assertSame($this->item->getFormattedSubtotal(), $this->item->formattedSubtotal);
Expand Down
8 changes: 4 additions & 4 deletions tests/Models/ShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Cone\Bazar\Support\Facades\Shipping as ShippingManager;
use Cone\Bazar\Support\Facades\Tax;
use Cone\Bazar\Tests\TestCase;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

class ShippingTest extends TestCase
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testt_is_taxable(): void
$this->assertInstanceOf(Taxable::class, $this->shipping);
$this->assertSame($this->shipping->price * 0.1, $this->shipping->tax);
$this->assertSame(
Str::currency($this->shipping->tax, $this->shipping->shippable->currency), $this->shipping->getFormattedTax()
Number::currency($this->shipping->tax, $this->shipping->shippable->currency), $this->shipping->getFormattedTax()
);
$this->assertSame($this->shipping->getFormattedTax(), $this->shipping->formattedTax);
}
Expand All @@ -97,14 +97,14 @@ public function testt_has_total_attribute(): void
);
$this->assertSame($this->shipping->getTotal(), $this->shipping->total);
$this->assertSame(
Str::currency($this->shipping->total, $this->shipping->shippable->currency),
Number::currency($this->shipping->total, $this->shipping->shippable->currency),
$this->shipping->getFormattedTotal()
);
$this->assertSame($this->shipping->getFormattedTotal(), $this->shipping->formattedTotal);
$this->assertSame($this->shipping->cost, $this->shipping->getSubtotal());
$this->assertSame($this->shipping->getSubtotal(), $this->shipping->subtotal);
$this->assertSame(
Str::currency($this->shipping->subtotal, $this->shipping->shippable->currency),
Number::currency($this->shipping->subtotal, $this->shipping->shippable->currency),
$this->shipping->getFormattedSubtotal()
);
$this->assertSame($this->shipping->getFormattedSubtotal(), $this->shipping->formattedSubtotal);
Expand Down
Loading

0 comments on commit e5e3557

Please sign in to comment.