'decimal:2', 'stock_minimum' => 'decimal:2', 'prix_unitaire' => 'decimal:2', 'conditionnement_quantite' => 'decimal:2', 'date_expiration' => 'date', ]; /** * Get the fournisseur that owns the product. */ public function fournisseur(): BelongsTo { return $this->belongsTo(Fournisseur::class); } /** * Get the category that owns the product. */ public function category(): BelongsTo { return $this->belongsTo(ProductCategory::class, 'categorie_id'); } /** * Handle image upload */ public function uploadImage($image) { if ($image) { // Delete old image if exists if ($this->image) { $this->deleteImage(); } // Store the new image $imageName = time() . '_' . uniqid() . '.' . $image->getClientOriginalExtension(); $imagePath = $image->storeAs('products', $imageName, 'public'); $this->image = $imagePath; $this->save(); return $imagePath; } return null; } /** * Delete the product image */ public function deleteImage() { if ($this->image) { Storage::disk('public')->delete($this->image); $this->image = null; $this->save(); } } /** * Get the full URL of the image */ public function getImageUrlAttribute() { if ($this->image) { return Storage::url($this->image); } return null; } /** * Get the stock items for the product. */ public function stockItems(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(StockItem::class); } /** * Get the packagings for the product. */ public function packagings(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(\App\Models\Stock\ProductPackaging::class); } /** * Get the stock moves for the product. */ public function stockMoves(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(StockMove::class); } /** * Get the goods receipt lines for the product. */ public function goodsReceiptLines(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(GoodsReceiptLine::class); } /** * Boot the model */ protected static function boot() { parent::boot(); static::deleting(function ($product) { // Delete the image when the product is deleted $product->deleteImage(); }); } }