- Add reject_stock field alongside stock (renamed to 'Stok Bagus') and retail_stock ('Stok Ecer')
- Update TypeScript types (ProductVariantFormItem, ProductFormInitialData)
- Update ProductVariantSection, ProductVariantEditModal, and ProductForm with new field
- Add validation rule and attribute label for reject_stock
- Update ProductService to persist reject_stock in create, update, and apply flows
81 lines
3.2 KiB
PHP
81 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Concerns;
|
|
|
|
use App\Enums\Role;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* @method FormRequest user()
|
|
*/
|
|
trait HasProductVariantRules
|
|
{
|
|
use ValidatesMediaUploads;
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function productVariantRules(string $variantsKey = 'variants', ?int $productId = null, bool $imagesRequired = true): array
|
|
{
|
|
$isAdminBahanBaku = $this->user()?->hasRole(Role::ADMIN_BAHAN_BAKU->value) ?? false;
|
|
|
|
$rules = [
|
|
"{$variantsKey}" => ['required', 'array', 'min:1'],
|
|
"{$variantsKey}.*.name" => ['required', 'string', 'max:200'],
|
|
"{$variantsKey}.*.stock" => ['required', 'integer', 'min:0'],
|
|
"{$variantsKey}.*.reject_stock" => ['required', 'integer', 'min:0'],
|
|
"{$variantsKey}.*.retail_stock" => ['required', 'integer', 'min:0'],
|
|
...$this->variantImageRules($variantsKey, required: $imagesRequired),
|
|
];
|
|
|
|
if ($productId) {
|
|
$rules["{$variantsKey}.*.id"] = [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('product_variants', 'id')
|
|
->where('product_id', $productId)
|
|
->whereNull('deleted_at'),
|
|
];
|
|
}
|
|
|
|
if (! $isAdminBahanBaku) {
|
|
$rules["{$variantsKey}.*.prices"] = ['required', 'array'];
|
|
$rules["{$variantsKey}.*.prices.distributor"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.agent"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.sub_agent"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.grosir"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.retail"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.tiktok"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.shopee"] = ['required', 'integer', 'min:0'];
|
|
$rules["{$variantsKey}.*.prices.harga_modal"] = ['required', 'integer', 'min:0'];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function productVariantAttributes(string $variantsKey = 'variants'): array
|
|
{
|
|
return [
|
|
$variantsKey => 'varian',
|
|
"{$variantsKey}.*.name" => 'nama varian',
|
|
"{$variantsKey}.*.stock" => 'stok bagus',
|
|
"{$variantsKey}.*.reject_stock" => 'stok reject',
|
|
"{$variantsKey}.*.retail_stock" => 'stok ecer',
|
|
"{$variantsKey}.*.prices" => 'harga',
|
|
"{$variantsKey}.*.prices.distributor" => 'distributor',
|
|
"{$variantsKey}.*.prices.agent" => 'agen',
|
|
"{$variantsKey}.*.prices.sub_agent" => 'sub agen',
|
|
"{$variantsKey}.*.prices.grosir" => 'grosir',
|
|
"{$variantsKey}.*.prices.retail" => 'eceran',
|
|
"{$variantsKey}.*.prices.tiktok" => 'tiktok',
|
|
"{$variantsKey}.*.prices.shopee" => 'shopee',
|
|
"{$variantsKey}.*.prices.harga_modal" => 'harga modal',
|
|
...$this->variantImageAttributes($variantsKey, 'foto varian'),
|
|
];
|
|
}
|
|
}
|