Skip to content

Commit

Permalink
Merge pull request #193 from Zakarialabib/Stock-Alert-Improvements
Browse files Browse the repository at this point in the history
Improving the stock alert component
  • Loading branch information
Zakarialabib authored Aug 4, 2024
2 parents 4603dd2 + fb43cb3 commit 17fe18d
Show file tree
Hide file tree
Showing 8 changed files with 748 additions and 137 deletions.
45 changes: 40 additions & 5 deletions app/Livewire/Reports/StockAlertReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,57 @@
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Product;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Layout;

#[Layout('layouts.app')]
class StockAlertReport extends Component
{
use WithPagination;

public function getProductAlertProperty()
public $thresholds = [];
public $filterName = '';
public $filterCode = '';
public $filterQuantityMin = null;
public $filterQuantityMax = null;

#[Computed]
public function stockAlert()
{
$query = Product::belowStockAlert();

if ($this->filterName) {
$query->where('name', 'like', '%' . $this->filterName . '%');
}

if ($this->filterCode) {
$query->where('code', 'like', '%' . $this->filterCode . '%');
}

if ($this->filterQuantityMin !== null) {
$query->where('quantity', '>=', $this->filterQuantityMin);
}

if ($this->filterQuantityMax !== null) {
$query->where('quantity', '<=', $this->filterQuantityMax);
}

return $query->paginate();
}

public function setThreshold($productId, $threshold)
{
return Product::select(['id', 'name', 'quantity', 'stock_alert', 'code'])
->whereColumn('quantity', '<=', 'stock_alert')
->paginate();
$product = Product::find($productId);
if ($product) {
$product->stock_alert = $threshold;
$product->save();
}
}

public function render()
{
return view('livewire.reports.stock-alert-report');
return view('livewire.reports.stock-alert-report', [
'products' => $this->stockAlert(),
]);
}
}
14 changes: 13 additions & 1 deletion app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Product extends Model
public function __construct(array $attributes = [])
{
$this->setRawAttributes([
'code' => Carbon::now()->format('Y-m-d').mt_rand(10000000, 99999999),
'code' => Carbon::now()->format('Y-m-d') . mt_rand(10000000, 99999999),
], true);
parent::__construct($attributes);
}
Expand Down Expand Up @@ -166,4 +166,16 @@ public function getAverageCostAttribute(): int|float|null
{
return $this->warehouses->avg('pivot.cost');
}

// Add scope for stock alerts
public function scopeBelowStockAlert($query)
{
return $query->whereColumn('quantity', '<=', 'stock_alert');
}

// Add method to check if product is below stock alert
public function isBelowStockAlert(): bool
{
return $this->quantity <= $this->stock_alert;
}
}
249 changes: 206 additions & 43 deletions lang/ar.json

Large diffs are not rendered by default.

Loading

0 comments on commit 17fe18d

Please sign in to comment.