Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 23, 2023
1 parent 2212d86 commit d2dbc8c
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 49 deletions.
79 changes: 79 additions & 0 deletions src/Fields/Items.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Cone\Bazar\Fields;

use Closure;
use Cone\Bazar\Models\Product;
use Cone\Bazar\Models\Variant;
use Cone\Root\Fields\MorphMany;
use Cone\Root\Fields\MorphTo;
use Cone\Root\Fields\Number;
use Cone\Root\Fields\Text;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class Items extends MorphMany
{
/**
* The relations to eager load on every query.
*/
protected array $with = [
'buyable',
'itemable',
];

/**
* Create a new relation field instance.
*/
public function __construct(string $label = null, Closure|string $modelAttribute = null, Closure|string $relation = null)
{
parent::__construct($label ?: __('Products'), $modelAttribute ?: 'items', $relation);

$this->display('name');
$this->asSubResource();
}

/**
* {@inheritdoc}
*/
public function fields(Request $request): array
{
return [
MorphTo::make(__('Buyable Item'), 'buyable')
->required()
->types([
Product::class,
Variant::class,
])
->display(static function (Model $relatable): ?string {
return (string) match ($relatable::class) {
Product::class => $relatable->name,
Variant::class => $relatable->alias,
default => $relatable->getKey(),
};
}),

Text::make(__('Name'), 'name')
->required(),

Number::make(__('Price'), 'price')
->min(0)
->required()
->format(static function (Request $request, Model $model, ?float $value): string {
return Str::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);
}),

Number::make(__('Quantity'), 'quantity')
->required()
->min(0),
];
}
}
68 changes: 68 additions & 0 deletions src/Fields/Transactions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Cone\Bazar\Fields;

use Closure;
use Cone\Bazar\Gateway\Driver;
use Cone\Bazar\Models\Transaction;
use Cone\Bazar\Support\Facades\Gateway;
use Cone\Root\Fields\Date;
use Cone\Root\Fields\HasMany;
use Cone\Root\Fields\Number;
use Cone\Root\Fields\Select;
use Cone\Root\Fields\Text;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class Transactions extends HasMany
{
/**
* The relations to eager load on every query.
*/
protected array $with = [
'order',
];

/**
* Create a new relation field instance.
*/
public function __construct(string $label = null, Closure|string $modelAttribute = null, Closure|string $relation = null)
{
parent::__construct($label ?: __('Transactions'), $modelAttribute ?: 'transactions', $relation);

$this->asSubResource();
}

/**
* {@inheritdoc}
*/
public function fields(Request $request): array
{
return [
Select::make(__('Type'), 'type')
->options([
Transaction::PAYMENT => __('Payment'),
Transaction::REFUND => __('Refund'),
]),

Number::make(__('Amount'), 'amount')
->min(0)
->required()
->format(static function (Request $request, Transaction $transaction, ?float $value): string {
return Str::currency($value, $transaction->order->currency);
}),

Select::make(__('Driver'), 'driver')
->options(static function (Request $request, Transaction $transaction): array {
return array_map(static function (Driver $driver): string {
return $driver->getName();
}, Gateway::getAvailableDrivers($transaction->order));
}),

Text::make(__('Key'), 'key'),

Date::make(__('Completed At'), 'completed_at')
->withTime(),
];
}
}
59 changes: 11 additions & 48 deletions src/Resources/OrderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
namespace Cone\Bazar\Resources;

use Cone\Bazar\Bazar;
use Cone\Bazar\Fields\Items;
use Cone\Bazar\Fields\Transactions;
use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Product;
use Cone\Bazar\Models\Variant;
use Cone\Root\Fields\BelongsTo;
use Cone\Root\Fields\ID;
use Cone\Root\Fields\MorphMany;
use Cone\Root\Fields\MorphTo;
use Cone\Root\Fields\Number;
use Cone\Root\Fields\Select;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class OrderResource extends Resource
{
Expand All @@ -43,51 +38,19 @@ public function fields(Request $request): array
BelongsTo::make(__('Customer'), 'user')
->display('name'),

Select::make(__('Currency'), 'currency')
->options(Bazar::getCurrencies()),

MorphMany::make(__('Products'), 'items')
->display('name')
->asSubResource()
->with(['buyable', 'itemable'])
->withFields(static function (): array {
return [
MorphTo::make(__('Buyable Item'), 'buyable')
->required()
->types([
Product::class,
Variant::class,
])
->display(static function (Model $relatable): ?string {
return (string) match ($relatable::class) {
Product::class => $relatable->name,
Variant::class => $relatable->alias,
default => $relatable->getKey(),
};
}),
Text::make(__('Total'), static function (Request $request, Order $model): string {
return $model->formattedTotal;
}),

Text::make(__('Name'), 'name')
->required(),
Select::make(__('Currency'), 'currency')
->options(Bazar::getCurrencies())
->hiddenOn(['index']),

Number::make(__('Price'), 'price')
->min(0)
->required()
->format(static function (Request $request, Model $model, ?float $value): string {
return Str::currency($value, $model->itemable->currency);
}),
// Select::make(__('Status'), 'status)->options([]),

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

Number::make(__('Quantity'), 'quantity')
->required()
->min(0),
];
}),
Transactions::make(),
];
}
}
1 change: 0 additions & 1 deletion src/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function fields(Request $request): array
Editor::make(__('Description'), 'description'),

BelongsToMany::make(__('Categories'), 'categories')
->asSubResource()
->display('name'),

Meta::make(__('Price :currency', ['currency' => 'HUF']), 'price_huf')
Expand Down

0 comments on commit d2dbc8c

Please sign in to comment.