df
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flux/features/tickets/models/shipment_document_model.dart';
|
||||
import 'package:flux/features/master_data/providers/models/provider_model.dart';
|
||||
import 'package:flux/features/master_data/providers/models/provider_role.dart';
|
||||
@@ -29,22 +31,51 @@ class TicketsShipmentRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// NUOVO METODO: Salva il DDT e aggiorna i Ticket
|
||||
Future<void> createShipmentDocument({
|
||||
/// Salva il DDT nel DB, fa l'upload del PDF nello Storage e aggiorna il path
|
||||
Future<String> createShipmentWithPdf({
|
||||
required ShipmentDocumentModel document,
|
||||
required TicketStatus newTicketStatus, // es: 'shipped' o 'inExternalLab'
|
||||
required Uint8List pdfBytes,
|
||||
required String newTicketStatus,
|
||||
}) async {
|
||||
try {
|
||||
// 1. Inseriamo il singolo Documento di Trasporto
|
||||
await _supabase.from('shipment_documents').insert(document.toMap());
|
||||
// 1. Definiamo un percorso unico e ordinato per il file nello Storage
|
||||
// Struttura: company_id / ddt / anno / numero_ddt.pdf
|
||||
final year = document.docDate.year;
|
||||
final cleanedDocNumber = document.docNumber
|
||||
.replaceAll('/', '_')
|
||||
.replaceAll(' ', '_');
|
||||
final storagePath =
|
||||
'${document.companyId}/ddt/$year/$cleanedDocNumber.pdf';
|
||||
|
||||
// 2. Aggiorniamo lo stato di TUTTI i ticket inclusi nel DDT
|
||||
// 2. Facciamo l'upload dei bytes grezzi nel bucket 'company_documents'
|
||||
await _supabase.storage
|
||||
.from('company_documents')
|
||||
.uploadBinary(
|
||||
storagePath,
|
||||
pdfBytes,
|
||||
fileOptions: const FileOptions(
|
||||
contentType: 'application/pdf',
|
||||
upsert: true,
|
||||
),
|
||||
);
|
||||
|
||||
// 3. Creiamo la mappa del documento includendo il percorso dello storage
|
||||
final documentData = document.toMap();
|
||||
documentData['storage_path'] = storagePath;
|
||||
|
||||
// 4. Inseriamo il Documento di Trasporto nel DB
|
||||
await _supabase.from('shipment_documents').insert(documentData);
|
||||
|
||||
// 5. Aggiorniamo lo stato di TUTTI i ticket inclusi nel DDT
|
||||
await _supabase
|
||||
.from('ticket')
|
||||
.update({'ticket_status': newTicketStatus.value})
|
||||
.update({'ticket_status': newTicketStatus})
|
||||
.inFilter('id', document.ticketIds);
|
||||
|
||||
// Restituiamo lo storagePath per usarlo subito nell'interfaccia se serve
|
||||
return storagePath;
|
||||
} catch (e) {
|
||||
throw ('Errore durante la creazione della spedizione: $e');
|
||||
throw ('Errore durante il salvataggio e upload della spedizione: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user