99 lines
3.5 KiB
PHP
99 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin\Manage;
|
|
|
|
use App\Enums\OrderChannel;
|
|
use App\Enums\PaymentType;
|
|
use App\Enums\Permission;
|
|
use App\Enums\PriceType;
|
|
use App\Enums\ProductStockQuality;
|
|
use App\Models\Order;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class OrderRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
$permission = $this->isMethod('POST')
|
|
? Permission::ORDERS_CREATE
|
|
: Permission::ORDERS_UPDATE;
|
|
|
|
return $this->user()?->can($permission->value) ?? false;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'customer_id' => ['nullable', 'integer', Rule::exists('customers', 'id')->whereNull('deleted_at')],
|
|
'marketing_id' => ['nullable', 'integer', Rule::exists('users', 'id')->whereNull('deleted_at')],
|
|
'channel' => ['required', Rule::enum(OrderChannel::class)],
|
|
'price_type' => ['required', Rule::enum(PriceType::class)],
|
|
'payment_type' => ['required', Rule::enum(PaymentType::class)],
|
|
'is_affiliate' => ['nullable', 'boolean'],
|
|
'tiktok_order_id' => [Rule::requiredIf(fn () => $this->channel === OrderChannel::TIKTOK->value), 'string', 'max:100'],
|
|
'shopee_order_id' => [Rule::requiredIf(fn () => $this->channel === OrderChannel::SHOPEE->value), 'string', 'max:100'],
|
|
'discount' => ['nullable', 'integer', 'min:0'],
|
|
'shipping_cost' => ['nullable', 'integer', 'min:0'],
|
|
'notes' => ['nullable', 'string'],
|
|
];
|
|
|
|
if ($this->isMethod('PUT') || $this->isMethod('PATCH')) {
|
|
$rules['items'] = ['required', 'array', 'min:1'];
|
|
$rules['items.*.product_variant_id'] = [
|
|
'required',
|
|
'integer',
|
|
Rule::exists('product_variants', 'id')->whereNull('deleted_at'),
|
|
];
|
|
$rules['items.*.quantity'] = ['required', 'integer', 'min:1'];
|
|
$rules['items.*.stock_quality'] = ['required', Rule::enum(ProductStockQuality::class)];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'customer_id' => 'pelanggan',
|
|
'marketing_id' => 'marketing',
|
|
'channel' => 'channel',
|
|
'price_type' => 'tipe harga',
|
|
'payment_type' => 'tipe pembayaran',
|
|
'is_affiliate' => 'afiliasi',
|
|
'tiktok_order_id' => 'ID pesanan TikTok Shop',
|
|
'shopee_order_id' => 'ID pesanan Shopee',
|
|
'discount' => 'diskon',
|
|
'shipping_cost' => 'ongkos kirim',
|
|
'notes' => 'keterangan',
|
|
'items' => 'produk',
|
|
'items.*.product_variant_id' => 'varian produk',
|
|
'items.*.quantity' => 'jumlah',
|
|
'items.*.stock_quality' => 'kualitas stok',
|
|
];
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
if (! $this->isMethod('PUT') && ! $this->isMethod('PATCH')) {
|
|
return;
|
|
}
|
|
|
|
$validator->after(function (Validator $validator): void {
|
|
/** @var Order $order */
|
|
$order = $this->route('order');
|
|
|
|
if (! in_array($order->status, config('order-status.editable'), true)) {
|
|
$validator->errors()->add('status', 'Pesanan tidak dapat diubah.');
|
|
}
|
|
});
|
|
}
|
|
}
|