Initial commit

This commit is contained in:
2026-05-30 14:33:11 +01:00
commit a8c372177f
156 changed files with 38163 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'
// GET single purchase invoice
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const invoice = await db.factureAchat.findUnique({
where: { id: id },
include: {
fournisseur: true,
lignes: {
include: {
produit: true
}
},
fichiers: true
}
})
if (!invoice) {
return NextResponse.json(
{ error: 'Facture non trouvée' },
{ status: 404 }
)
}
return NextResponse.json(invoice)
} catch (error) {
console.error('Error fetching purchase invoice:', error)
return NextResponse.json(
{ error: 'Impossible de récupérer la facture' },
{ status: 500 }
)
}
}
// DELETE purchase invoice (only if BROUILLON)
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
// Check if invoice exists and is in BROUILLON status
const invoice = await db.factureAchat.findUnique({
where: { id: id }
})
if (!invoice) {
return NextResponse.json(
{ error: 'Facture non trouvée' },
{ status: 404 }
)
}
if (invoice.statut !== 'BROUILLON') {
return NextResponse.json(
{ error: 'Seules les factures en brouillon peuvent être supprimées' },
{ status: 400 }
)
}
// Delete invoice (lines will be cascade deleted)
await db.factureAchat.delete({
where: { id: id }
})
return NextResponse.json({ success: true })
} catch (error) {
console.error('Error deleting purchase invoice:', error)
return NextResponse.json(
{ error: 'Impossible de supprimer la facture' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,70 @@
import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'
// POST validate invoice and update stock
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
// Get invoice with lines
const invoice = await db.factureAchat.findUnique({
where: { id: id },
include: {
lignes: true
}
})
if (!invoice) {
return NextResponse.json(
{ error: 'Facture non trouvée' },
{ status: 404 }
)
}
if (invoice.statut !== 'BROUILLON') {
return NextResponse.json(
{ error: 'Cette facture a déjà été validée' },
{ status: 400 }
)
}
// Update stock for each line
for (const ligne of invoice.lignes) {
await db.produit.update({
where: { id: ligne.produitId },
data: {
stock: {
increment: ligne.quantite
}
}
})
}
// Update invoice status and set reception date
const updatedInvoice = await db.factureAchat.update({
where: { id: id },
data: {
statut: 'VALIDE',
dateReception: new Date()
},
include: {
fournisseur: true,
lignes: {
include: {
produit: true
}
}
}
})
return NextResponse.json(updatedInvoice)
} catch (error) {
console.error('Error validating purchase invoice:', error)
return NextResponse.json(
{ error: 'Impossible de valider la facture' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,123 @@
import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'
// GET all purchase invoices
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const statut = searchParams.get('statut')
const invoices = await db.factureAchat.findMany({
where: statut ? { statut: statut as any } : undefined,
include: {
fournisseur: true,
lignes: {
include: {
produit: true
}
},
fichiers: true
},
orderBy: {
date: 'desc'
}
})
return NextResponse.json(invoices)
} catch (error) {
console.error('Error fetching purchase invoices:', error)
return NextResponse.json(
{ error: 'Impossible de récupérer les factures d\'achat' },
{ status: 500 }
)
}
}
// POST create new purchase invoice
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const {
fournisseurId,
date,
lignes,
montantHT,
montantTVA,
montantTTC,
notes
} = body
// Validation
if (!fournisseurId) {
return NextResponse.json(
{ error: 'Le fournisseur est requis' },
{ status: 400 }
)
}
if (!lignes || lignes.length === 0) {
return NextResponse.json(
{ error: 'Au moins une ligne est requise' },
{ status: 400 }
)
}
// Generate invoice number
const now = new Date(date)
const year = now.getFullYear()
const month = String(now.getMonth() + 1).padStart(2, '0')
// Count invoices for this month
const monthInvoices = await db.factureAchat.count({
where: {
date: {
gte: new Date(year, now.getMonth(), 1),
lt: new Date(year, now.getMonth() + 1, 1)
}
}
})
const numero = `A${year}${month}${String(monthInvoices + 1).padStart(4, '0')}`
// Create invoice with lines in a transaction
const invoice = await db.factureAchat.create({
data: {
fournisseurId,
numero,
date: new Date(date),
statut: 'BROUILLON',
montantHT: Number(montantHT),
montantTVA: Number(montantTVA),
montantTTC: Number(montantTTC),
notes: notes || null,
lignes: {
create: lignes.map((ligne: any) => ({
produitId: ligne.produitId,
quantite: ligne.quantite,
prixUnitaireHT: ligne.prixUnitaireHT,
tauxTVA: ligne.tauxTVA,
montantHT: ligne.montantHT,
montantTVA: ligne.montantTVA,
montantTTC: ligne.montantTTC
}))
}
},
include: {
fournisseur: true,
lignes: {
include: {
produit: true
}
}
}
})
return NextResponse.json(invoice, { status: 201 })
} catch (error) {
console.error('Error creating purchase invoice:', error)
return NextResponse.json(
{ error: 'Impossible de créer la facture d\'achat' },
{ status: 500 }
)
}
}