*/ protected function productVariantRules(string $variantsKey = 'variants', ?int $productId = null): 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}.*.retail_stock" => ['required', 'integer', 'min:0'], ...$this->variantImageRules($variantsKey, required: true), ]; 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 */ protected function productVariantAttributes(string $variantsKey = 'variants'): array { return [ $variantsKey => 'varian', "{$variantsKey}.*.name" => 'nama varian', "{$variantsKey}.*.stock" => 'stok', "{$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'), ]; } }