Files
mrKamooandClaude Sonnet 4.6 8d51f4de3e feat: add sous-devis generation from commandes
From a commande, users can now create one or more sub-quotes
(sous-devis) to ventilate purchases across communes for approval.

- Add parent_devis_id and commande_id FKs on devis table
- Order model: sousDevis() hasMany relation
- Devis model: commande() and parent() relations
- DevisController: createFromCommande / storeFromCommande methods
- OrderController: load and pass sous_devis on show
- Commandes/Show: "Créer un sous-devis" button + ventilation panel
- Devis/CreateFromCommande: form pre-loaded with commande context
- Devis/Index: badge "Sous-devis · CMD-XXXX" on linked quotes
- Devis/Show: back-link to originating commande

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 15:14:45 +02:00

50 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Devis extends Model
{
protected $guarded = [];
protected $casts = [
'items' => 'array',
'date_devis' => 'date',
'total_ht' => 'decimal:2',
'total_ttc' => 'decimal:2',
'tva_rate' => 'decimal:2',
];
public function parent(): BelongsTo
{
return $this->belongsTo(Devis::class, 'parent_devis_id');
}
public function sousDevis(): HasMany
{
return $this->hasMany(Devis::class, 'parent_devis_id');
}
public function commande(): BelongsTo
{
return $this->belongsTo(\App\Models\Order::class, 'commande_id');
}
protected static function booted()
{
static::creating(function ($devis) {
if (empty($devis->number)) {
$year = date('Y');
$prefix = sprintf('DEV-%s-', $year);
$last = static::where('number', 'like', $prefix . '%')
->max('number');
$next = $last ? (intval(substr($last, strlen($prefix))) + 1) : 1;
$devis->number = $prefix . sprintf('%04d', $next);
}
});
}
}