ticket labels e ticket receipt

This commit is contained in:
2026-05-10 14:09:57 +02:00
parent 385c3da0a5
commit 5c86483563
20 changed files with 1024 additions and 157 deletions

View File

@@ -192,12 +192,42 @@ class TicketRepository {
}
}
/// Salva il ticket con upsert
Future<TicketModel> saveTicket(TicketModel ticket) async {
Future<String> generateTicketReference(String companyId) async {
final response = await Supabase.instance.client.rpc(
'get_next_document_number',
params: {'p_company_id': companyId, 'p_doc_type': 'ticket'},
);
// Estraiamo i dati dal JSON
final int nextValue = response['next_value'];
final String prefix = response['prefix'] ?? '';
final year = DateTime.now().year; // 2026
// Formattazione con zeri iniziali (es. 000125)
final paddedNumber = nextValue.toString().padLeft(6, '0');
// Costruiamo la stringa. Se c'è un prefisso mette "TCK-2026-000125",
// altrimenti solo "2026-000125"
if (prefix.isNotEmpty) {
return '$prefix-$year-$paddedNumber';
} else {
return '$year-$paddedNumber';
}
}
/// Salva il ticket
Future<TicketModel> insertTicket(TicketModel ticket) async {
if (ticket.id != null) {
throw Exception('Impossibile creare un ticket esistente, id not null');
}
try {
final ticketToSave = ticket.copyWith(
referenceId: await generateTicketReference(ticket.companyId),
);
final response = await _supabase
.from(_tableName)
.upsert(ticket.toMap())
.insert(ticketToSave.toMap())
.select()
.single();