Initial commit

This commit is contained in:
Kevin Harlay
2026-09-07 12:18:17 +02:00
commit a15811ed32
47 changed files with 16823 additions and 0 deletions
+162
View File
@@ -0,0 +1,162 @@
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule, FormControl, Validators } from '@angular/forms';
import { MatCardModule } from '@angular/material/card';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatSelectModule } from '@angular/material/select';
import { MatListModule } from '@angular/material/list';
import { MatIconModule } from '@angular/material/icon';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { MatDividerModule } from '@angular/material/divider';
import { SettingsService } from '../../core/services/settings.service';
import { CategoryService } from '../../core/services/category.service';
import { MonthService } from '../../core/services/month.service';
import { EntryService } from '../../core/services/entry.service';
import { Category, Month } from '../../core/models';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
selector: 'app-admin',
standalone: true,
imports: [
CommonModule,
ReactiveFormsModule,
MatCardModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatSelectModule,
MatListModule,
MatIconModule,
MatSnackBarModule,
MatDividerModule,
],
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss'],
})
export class AdminComponent implements OnInit {
// Section 1 — Salaire par défaut
salaryControl = new FormControl<number | null>(null, [Validators.required, Validators.min(0)]);
// Section 2 — Catégories
categories: Category[] = [];
newCategoryName = new FormControl('', [Validators.required, Validators.minLength(1)]);
newCategoryColor = new FormControl('#3b82d4');
// Section 3 — Export Excel
months: Month[] = [];
selectedMonthId = new FormControl<string | null>(null);
constructor(
private settingsService: SettingsService,
private categoryService: CategoryService,
private monthService: MonthService,
private entryService: EntryService,
private snackBar: MatSnackBar,
) {}
ngOnInit(): void {
this.loadDefaultSalary();
this.loadCategories();
this.loadMonths();
}
// — Section 1 —
private loadDefaultSalary(): void {
this.settingsService.getDefaultSalary().subscribe({
next: (value) => this.salaryControl.setValue(value),
error: () => this.showError('Impossible de charger le salaire par défaut.'),
});
}
saveSalary(): void {
if (this.salaryControl.invalid || this.salaryControl.value == null) return;
this.settingsService.updateDefaultSalary(this.salaryControl.value).subscribe({
next: () => this.snackBar.open('Salaire enregistré.', 'OK', { duration: 3000 }),
error: () => this.showError('Erreur lors de l\'enregistrement du salaire.'),
});
}
// — Section 2 —
private loadCategories(): void {
this.categoryService.getCategories().subscribe({
next: (cats) => (this.categories = cats),
error: () => this.showError('Impossible de charger les catégories.'),
});
}
addCategory(): void {
if (this.newCategoryName.invalid) return;
const dto = {
name: this.newCategoryName.value!.trim(),
color: this.newCategoryColor.value ?? '#3b82d4',
};
this.categoryService.createCategory(dto).subscribe({
next: () => {
this.newCategoryName.reset('');
this.newCategoryColor.reset('#3b82d4');
this.loadCategories();
this.snackBar.open('Catégorie ajoutée.', 'OK', { duration: 3000 });
},
error: () => this.showError('Erreur lors de l\'ajout de la catégorie.'),
});
}
deleteCategory(cat: Category): void {
if (!confirm(`Supprimer la catégorie "${cat.name}" ?`)) return;
this.categoryService.deleteCategory(cat.id).subscribe({
next: () => {
this.categories = this.categories.filter((c) => c.id !== cat.id);
this.snackBar.open('Catégorie supprimée.', 'OK', { duration: 3000 });
},
error: (err: HttpErrorResponse) => {
if (err.status === 409) {
this.showError('Catégorie utilisée par des saisies — suppression impossible.');
} else {
this.showError('Erreur lors de la suppression.');
}
},
});
}
// — Section 3 —
private loadMonths(): void {
this.monthService.getMonths().subscribe({
next: (months) => (this.months = months),
error: () => this.showError('Impossible de charger les mois.'),
});
}
exportExcel(): void {
const monthId = this.selectedMonthId.value;
if (!monthId) return;
const month = this.months.find((m) => m.id === monthId);
if (!month) return;
const mm = String(month.month).padStart(2, '0');
const fileName = `suivi-${mm}-${month.year}.xlsx`;
this.entryService.exportExcel(monthId, fileName).subscribe({
next: (blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
URL.revokeObjectURL(url);
},
error: () => this.showError('Erreur lors de l\'export Excel.'),
});
}
// — Helpers —
private showError(message: string): void {
this.snackBar.open(message, 'Fermer', { duration: 5000, panelClass: 'snack-error' });
}
}