fdsg
This commit is contained in:
67
lib/features/documents/data/tickets_shipment_repository.dart
Normal file
67
lib/features/documents/data/tickets_shipment_repository.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/features/documents/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';
|
||||
import 'package:flux/features/settings/document_sequence/models/document_sequence_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class TicketsShipmentRepository {
|
||||
final _supabase = GetIt.I.get<SupabaseClient>();
|
||||
final _companyId = GetIt.I.get<SessionCubit>().state.company!.id!;
|
||||
|
||||
Future<List<ProviderModel>> fetchRepairCenters() async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('provider')
|
||||
.select('*, provider_locations (*)')
|
||||
.eq('is_active', true)
|
||||
.order('name');
|
||||
|
||||
final allProviders = (response as List)
|
||||
.map((row) => ProviderModel.fromMap(row as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
// Filtriamo lato client per prendere SOLO i repairCenter
|
||||
return allProviders
|
||||
.where((p) => p.roles.contains(ProviderRole.repairCenter))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
throw ('Errore caricamento laboratori: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// NUOVO METODO: Salva il DDT e aggiorna i Ticket
|
||||
Future<void> createShipmentDocument({
|
||||
required ShipmentDocumentModel document,
|
||||
required String newTicketStatus, // es: 'shipped' o 'inExternalLab'
|
||||
}) async {
|
||||
try {
|
||||
// 1. Inseriamo il singolo Documento di Trasporto
|
||||
await _supabase.from('shipment_documents').insert(document.toMap());
|
||||
|
||||
// 2. Aggiorniamo lo stato di TUTTI i ticket inclusi nel DDT
|
||||
await _supabase
|
||||
.from('tickets')
|
||||
.update({'ticket_status': newTicketStatus})
|
||||
.inFilter('id', document.ticketIds);
|
||||
} catch (e) {
|
||||
throw ('Errore durante la creazione della spedizione: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> getNextAutoDocumentNumber() async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('document_sequences')
|
||||
.select('*')
|
||||
.eq('company_id', _companyId)
|
||||
.eq('document_type', DocumentType.shipment.name)
|
||||
.single();
|
||||
|
||||
return DocumentSequence.fromMap(response);
|
||||
} catch (e) {
|
||||
throw ('Errore recupero numero documento: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import 'package:equatable/equatable.dart';
|
||||
class ShipmentDocumentModel extends Equatable {
|
||||
final String? id;
|
||||
final String companyId;
|
||||
final String ticketId;
|
||||
final List<String> ticketIds;
|
||||
final String providerId;
|
||||
final String destinationLocationId;
|
||||
final String docNumber;
|
||||
@@ -16,7 +16,7 @@ class ShipmentDocumentModel extends Equatable {
|
||||
const ShipmentDocumentModel({
|
||||
this.id,
|
||||
required this.companyId,
|
||||
required this.ticketId,
|
||||
required this.ticketIds,
|
||||
required this.providerId,
|
||||
required this.destinationLocationId,
|
||||
required this.docNumber,
|
||||
@@ -27,11 +27,40 @@ class ShipmentDocumentModel extends Equatable {
|
||||
this.notes,
|
||||
});
|
||||
|
||||
ShipmentDocumentModel copyWith({
|
||||
String? id,
|
||||
String? companyId,
|
||||
List<String>? ticketIds,
|
||||
String? providerId,
|
||||
String? destinationLocationId,
|
||||
String? docNumber,
|
||||
DateTime? docDate,
|
||||
int? packageCount,
|
||||
double? weight,
|
||||
String? shippingReason,
|
||||
String? notes,
|
||||
}) {
|
||||
return ShipmentDocumentModel(
|
||||
id: id ?? this.id,
|
||||
companyId: companyId ?? this.companyId,
|
||||
ticketIds: ticketIds ?? this.ticketIds,
|
||||
providerId: providerId ?? this.providerId,
|
||||
destinationLocationId:
|
||||
destinationLocationId ?? this.destinationLocationId,
|
||||
docNumber: docNumber ?? this.docNumber,
|
||||
docDate: docDate ?? this.docDate,
|
||||
packageCount: packageCount ?? this.packageCount,
|
||||
weight: weight ?? this.weight,
|
||||
shippingReason: shippingReason ?? this.shippingReason,
|
||||
notes: notes ?? this.notes,
|
||||
);
|
||||
}
|
||||
|
||||
factory ShipmentDocumentModel.fromMap(Map<String, dynamic> map) {
|
||||
return ShipmentDocumentModel(
|
||||
id: map['id'],
|
||||
companyId: map['company_id'],
|
||||
ticketId: map['ticket_id'],
|
||||
ticketIds: List<String>.from(map['ticket_ids']),
|
||||
providerId: map['provider_id'],
|
||||
destinationLocationId: map['destination_location_id'],
|
||||
docNumber: map['doc_number'],
|
||||
@@ -47,7 +76,7 @@ class ShipmentDocumentModel extends Equatable {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'company_id': companyId,
|
||||
'ticket_id': ticketId,
|
||||
'ticket_ids': ticketIds,
|
||||
'provider_id': providerId,
|
||||
'destination_location_id': destinationLocationId,
|
||||
'doc_number': docNumber,
|
||||
@@ -60,5 +89,5 @@ class ShipmentDocumentModel extends Equatable {
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [id, docNumber, ticketId];
|
||||
List<Object?> get props => [id, docNumber, ticketIds];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user