refined document sequence management
This commit is contained in:
@@ -1,30 +1,61 @@
|
||||
import 'package:flux/core/blocs/session/session_cubit.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 DocumentSequenceRepository {
|
||||
final _supabase = GetIt.I.get<SupabaseClient>();
|
||||
final _companyId = GetIt.I.get<SessionCubit>().state.company!.id!;
|
||||
|
||||
Future<List<DocumentSequence>> getDocumentSequences(String companyId) async {
|
||||
final response = await _supabase
|
||||
.from('document_sequences')
|
||||
.select()
|
||||
.eq('company_id', companyId);
|
||||
Future<List<DocumentSequence>> getDocumentSequences() async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('document_sequences')
|
||||
.select()
|
||||
.eq('company_id', _companyId);
|
||||
|
||||
return (response as List).map((e) => DocumentSequence.fromMap(e)).toList();
|
||||
return (response as List)
|
||||
.map((e) => DocumentSequence.fromMap(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
throw ('Errore durante il caricamento delle sequenze: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateSequence({
|
||||
required String companyId,
|
||||
required String docType,
|
||||
required int nextValue,
|
||||
required String prefix,
|
||||
Future<String> getNextDocumentNumber(String docType) async {
|
||||
try {
|
||||
// Chiamiamo la funzione SQL che abbiamo appena creato!
|
||||
final response = await _supabase.rpc(
|
||||
'get_next_sequence',
|
||||
params: {'p_doc_type': docType},
|
||||
);
|
||||
return response as String;
|
||||
} catch (e) {
|
||||
throw ('Errore durante la generazione del numero: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> createOrUpdateSequence({
|
||||
required DocumentSequence sequence,
|
||||
}) async {
|
||||
try {
|
||||
await _supabase.from('document_sequences').upsert({
|
||||
'company_id': _companyId,
|
||||
'doc_type': sequence.docType,
|
||||
'next_value': sequence.nextValue,
|
||||
'prefix': sequence.prefix,
|
||||
});
|
||||
} on Exception catch (e) {
|
||||
throw ('Errore durante la creazione/aggiornamento della sequenza: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateSequence({required DocumentSequence sequence}) async {
|
||||
await _supabase.from('document_sequences').upsert({
|
||||
'company_id': companyId,
|
||||
'doc_type': docType,
|
||||
'next_value': nextValue,
|
||||
'prefix': prefix,
|
||||
'company_id': _companyId,
|
||||
'doc_type': sequence.docType,
|
||||
'next_value': sequence.nextValue,
|
||||
'prefix': sequence.prefix,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user