- Updated CustomerService to simplify getAll method. - Refactored ProductService to utilize HasRoleChecks trait and improved role verification logic. - Enhanced ProductVariantService with new methods for fetching data for restocking and transactions. - Cleaned up RawMaterialService by removing unused methods and improving data retrieval. - Adjusted SupplierService to streamline getAll method. - Refactored RoleService to use Spatie's Role model and improved role filtering logic. - Updated NotificationService to handle role labels more effectively. - Improved StockMutationService by removing redundant paginated method. - Cleaned up various frontend components to directly accept necessary props instead of nested data objects. - Updated tests to reflect changes in service method names and ensure proper notification handling.
122 lines
3.9 KiB
PHP
122 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin\Master\RawMaterial;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\RawMaterial;
|
|
use App\Models\RawMaterialPrice;
|
|
use App\Services\Concerns\RegistersMedia;
|
|
use App\Services\NotificationService;
|
|
use App\Services\S3PresignedService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RawMaterialVariantService
|
|
{
|
|
use RegistersMedia;
|
|
|
|
public function __construct(
|
|
private S3PresignedService $s3Service,
|
|
) {}
|
|
|
|
public function getForCutting(): array
|
|
{
|
|
return RawMaterial::query()
|
|
->select(['id', 'name', 'unit', 'is_active'])
|
|
->with([
|
|
'rawMaterialPrices:id,raw_material_id,variant,price,stock',
|
|
])
|
|
->active()
|
|
->orderBy('name')
|
|
->get()
|
|
->each(function (RawMaterial $rawMaterial) {
|
|
$rawMaterial->rawMaterialPrices->each(function (RawMaterialPrice $price) {
|
|
$media = $price->getFirstMedia('photos');
|
|
$price->photo_url = $media
|
|
? $this->s3Service->getTemporaryUrl($media->file_name)
|
|
: null;
|
|
});
|
|
});
|
|
}
|
|
|
|
public function getForEdit(RawMaterialPrice $variant): array
|
|
{
|
|
$variant->load('media');
|
|
|
|
$media = $variant->getMedia('photos');
|
|
$photoKey = $media->first()?->file_name;
|
|
$photoUrl = $media->first()
|
|
? $this->s3Service->getTemporaryUrl($media->first()->file_name)
|
|
: null;
|
|
|
|
return [
|
|
'id' => $variant->id,
|
|
'raw_material_id' => $variant->raw_material_id,
|
|
'variant' => $variant->variant,
|
|
'price' => $variant->price,
|
|
'stock' => $variant->stock,
|
|
'photo_key' => $photoKey,
|
|
'photo_url' => $photoUrl,
|
|
];
|
|
}
|
|
|
|
public function update(RawMaterialPrice $variant, array $data): RawMaterialPrice
|
|
{
|
|
DB::transaction(function () use ($variant, $data) {
|
|
$variant->update([
|
|
'variant' => $data['variant'],
|
|
'price' => $data['price'],
|
|
'stock' => $data['stock'],
|
|
]);
|
|
|
|
if (array_key_exists('photo_key', $data)) {
|
|
$existingKey = $variant->getMedia('photos')->first()?->file_name;
|
|
$newKey = $data['photo_key'];
|
|
|
|
if ($existingKey !== $newKey) {
|
|
$variant->clearMediaCollection('photos');
|
|
if ($newKey) {
|
|
$this->registerPhoto($variant, $newKey);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
NotificationService::notify(
|
|
roles: [Role::OWNER, Role::DEVELOPER, Role::DIREKTUR, Role::ADMIN_TOKO],
|
|
title: 'Varian Diperbarui',
|
|
body: "Varian \"{$variant->variant}\" berhasil diperbarui".' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.master.raw-materials.index'),
|
|
);
|
|
|
|
return $variant->fresh();
|
|
}
|
|
|
|
public function destroy(RawMaterial $rawMaterial, RawMaterialPrice $variant): bool
|
|
{
|
|
$result = DB::transaction(function () use ($variant) {
|
|
$variant->clearMediaCollection('photos');
|
|
|
|
return $variant->delete();
|
|
});
|
|
|
|
NotificationService::notify(
|
|
roles: [Role::OWNER, Role::DEVELOPER, Role::DIREKTUR, Role::ADMIN_TOKO],
|
|
title: 'Varian Dihapus',
|
|
body: "Varian \"{$variant->variant}\" dari bahan baku \"{$rawMaterial->name}\" berhasil dihapus".' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.master.raw-materials.index'),
|
|
);
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function registerPhoto(RawMaterialPrice $variant, string $s3Key): void
|
|
{
|
|
$this->registerMedia(
|
|
model: $variant,
|
|
s3Key: $s3Key,
|
|
collectionName: 'photos',
|
|
orderColumn: 1,
|
|
);
|
|
}
|
|
}
|