-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters