feat: enhance stock formatting methods in Product and RawMaterial models to handle unloaded relations gracefully
This commit is contained in:
parent
a37b47a660
commit
1c4f82529c
@ -58,21 +58,27 @@ protected function inactive(Builder $query): void
|
|||||||
public function totalRejectStockFormatted(): Attribute
|
public function totalRejectStockFormatted(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: fn () => number_format($this->variants->sum('reject_stock'), 0, ',', '.'),
|
get: fn () => $this->relationLoaded('variants')
|
||||||
|
? number_format($this->variants->sum('reject_stock'), 0, ',', '.')
|
||||||
|
: '-',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function totalRetailStockFormatted(): Attribute
|
public function totalRetailStockFormatted(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: fn () => number_format($this->variants->sum('retail_stock'), 0, ',', '.'),
|
get: fn () => $this->relationLoaded('variants')
|
||||||
|
? number_format($this->variants->sum('retail_stock'), 0, ',', '.')
|
||||||
|
: '-',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function totalStockFormatted(): Attribute
|
public function totalStockFormatted(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: fn () => number_format($this->variants->sum('stock'), 0, ',', '.'),
|
get: fn () => $this->relationLoaded('variants')
|
||||||
|
? number_format($this->variants->sum('stock'), 0, ',', '.')
|
||||||
|
: '-',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -66,12 +66,14 @@ public function unitLabel(): Attribute
|
|||||||
public function totalInventoryValueFormatted(): Attribute
|
public function totalInventoryValueFormatted(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: fn () => 'Rp '.number_format(
|
get: fn () => $this->relationLoaded('prices')
|
||||||
$this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock * (int) $price->price),
|
? 'Rp '.number_format(
|
||||||
0,
|
$this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock * (int) $price->price),
|
||||||
',',
|
0,
|
||||||
'.',
|
',',
|
||||||
),
|
'.',
|
||||||
|
)
|
||||||
|
: '-',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +81,9 @@ public function totalStockFormatted(): Attribute
|
|||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: function () {
|
get: function () {
|
||||||
|
if (! $this->relationLoaded('prices')) {
|
||||||
|
return '-';
|
||||||
|
}
|
||||||
$total = $this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock);
|
$total = $this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock);
|
||||||
$formatted = rtrim(rtrim(number_format($total, 4, ',', '.'), '0'), ',');
|
$formatted = rtrim(rtrim(number_format($total, 4, ',', '.'), '0'), ',');
|
||||||
|
|
||||||
|
|||||||
@ -38,17 +38,14 @@ public function paginateForIndex(array $tableQuery, string $isActive, string $ca
|
|||||||
'pendingOwnerVerificationRequest.submittedBy.profile',
|
'pendingOwnerVerificationRequest.submittedBy.profile',
|
||||||
'variants' => fn ($query) => $query
|
'variants' => fn ($query) => $query
|
||||||
->with(['media', 'prices' => fn ($query) => $query->orderBy('type')])
|
->with(['media', 'prices' => fn ($query) => $query->orderBy('type')])
|
||||||
->orderBy('created_at'),
|
->orderBy('created_at')
|
||||||
|
->when($tableQuery['search'] !== '', function ($query) use ($tableQuery): void {
|
||||||
|
$query->where('name', 'like', "%{$tableQuery['search']}%");
|
||||||
|
}),
|
||||||
])
|
])
|
||||||
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
||||||
$search = $tableQuery['search'];
|
$search = $tableQuery['search'];
|
||||||
$query->where(function (Builder $query) use ($search): void {
|
$query->whereHas('variants', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"));
|
||||||
$query->where('name', 'like', "%{$search}%")
|
|
||||||
->orWhere('slug', 'like', "%{$search}%")
|
|
||||||
->orWhere('description', 'like', "%{$search}%")
|
|
||||||
->orWhereHas('categories', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"))
|
|
||||||
->orWhereHas('variants', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"));
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
->when($isActive !== '', fn (Builder $query) => $query->where('is_active', $isActive === '1'))
|
->when($isActive !== '', fn (Builder $query) => $query->where('is_active', $isActive === '1'))
|
||||||
->when($categoryId !== '', function (Builder $query) use ($categoryId): void {
|
->when($categoryId !== '', function (Builder $query) use ($categoryId): void {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user