a
This commit is contained in:
1 parent
aaa6cdf704
commit
282a959f88
6 files changed
+738
No files matched your search
@@ -18,6 +18,8 @@ class Tables {
|
||||
static const String staffInStores = 'staff_in_stores';
|
||||
static const String staffMembers = 'staff_members';
|
||||
static const String stores = 'stores';
|
||||
static const String targets = 'targets';
|
||||
static const String individualTargets = 'individual_targets';
|
||||
static const String tasks = 'tasks';
|
||||
static const String taskAssignments = 'task_assignments';
|
||||
static const String taskReminders = 'task_reminders';
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flux/core/enums_and_consts/consts.dart';
|
||||
import 'package:flux/features/targets/models/individual_target_model.dart';
|
||||
import 'package:flux/features/targets/models/target_model.dart';
|
||||
import 'package:flux/service_locator.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class TargetsRepository {
|
||||
final SupabaseClient _supabase = sl<SupabaseClient>();
|
||||
|
||||
final StreamController<void> _targetsChangedController =
|
||||
StreamController<void>.broadcast();
|
||||
|
||||
Stream<void> get onTargetsChanged => _targetsChangedController.stream;
|
||||
|
||||
void notifyTargetsChanged() {
|
||||
if (!_targetsChangedController.isClosed) {
|
||||
_targetsChangedController.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// STORE TARGETS (Tabella 'targets')
|
||||
// ==========================================
|
||||
|
||||
Future<TargetModel?> getStoreTargetById(String id) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from(Tables.targets)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.stores}(*),
|
||||
${Tables.individualTargets}(
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
)
|
||||
''')
|
||||
.eq('id', id)
|
||||
.single();
|
||||
return TargetModel.fromMap(response);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore getStoreTargetById: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<TargetModel>> getStoreTargets({
|
||||
required String companyId,
|
||||
String? storeId,
|
||||
DateTime? fromDate,
|
||||
DateTime? toDate,
|
||||
}) async {
|
||||
try {
|
||||
var query = _supabase
|
||||
.from(Tables.targets)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.stores}(*),
|
||||
${Tables.individualTargets}(
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
)
|
||||
''')
|
||||
.eq('company_id', companyId);
|
||||
|
||||
if (storeId != null && storeId.isNotEmpty) {
|
||||
query = query.eq('store_id', storeId);
|
||||
}
|
||||
|
||||
if (fromDate != null) {
|
||||
query = query.gte('end_day', fromDate.toIso8601String());
|
||||
}
|
||||
|
||||
if (toDate != null) {
|
||||
query = query.lte('start_day', toDate.toIso8601String());
|
||||
}
|
||||
|
||||
final response = await query.order('start_day', ascending: false);
|
||||
return (response as List<dynamic>)
|
||||
.map((e) => TargetModel.fromMap(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore getStoreTargets: $e');
|
||||
throw Exception('Errore nel recupero degli obiettivi di negozio: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<TargetModel?> getStoreTargetForMonth({
|
||||
required String companyId,
|
||||
required String storeId,
|
||||
required DateTime month,
|
||||
}) async {
|
||||
try {
|
||||
final firstDay = DateTime(month.year, month.month, 1);
|
||||
final lastDay = DateTime(month.year, month.month + 1, 0, 23, 59, 59);
|
||||
|
||||
final response = await _supabase
|
||||
.from(Tables.targets)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.stores}(*),
|
||||
${Tables.individualTargets}(
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
)
|
||||
''')
|
||||
.eq('company_id', companyId)
|
||||
.eq('store_id', storeId)
|
||||
.gte('end_day', firstDay.toIso8601String())
|
||||
.lte('start_day', lastDay.toIso8601String())
|
||||
.order('start_day', ascending: false)
|
||||
.limit(1)
|
||||
.maybeSingle();
|
||||
|
||||
if (response == null) return null;
|
||||
return TargetModel.fromMap(response);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore getStoreTargetForMonth: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<TargetModel> saveStoreTarget(TargetModel target) async {
|
||||
try {
|
||||
final Map<String, dynamic> data = target.toMap();
|
||||
final response = await _supabase
|
||||
.from(Tables.targets)
|
||||
.upsert(data)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.stores}(*),
|
||||
${Tables.individualTargets}(
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
)
|
||||
''')
|
||||
.single();
|
||||
|
||||
notifyTargetsChanged();
|
||||
return TargetModel.fromMap(response);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore saveStoreTarget: $e');
|
||||
throw Exception('Errore nel salvataggio dell\'obiettivo di negozio: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteStoreTarget(String id) async {
|
||||
try {
|
||||
await _supabase.from(Tables.targets).delete().eq('id', id);
|
||||
notifyTargetsChanged();
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore deleteStoreTarget: $e');
|
||||
throw Exception('Errore nell\'eliminazione dell\'obiettivo di negozio: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// INDIVIDUAL TARGETS (Tabella 'individual_targets')
|
||||
// ==========================================
|
||||
|
||||
Future<IndividualTargetModel?> getIndividualTargetById(String id) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from(Tables.individualTargets)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
''')
|
||||
.eq('id', id)
|
||||
.single();
|
||||
return IndividualTargetModel.fromMap(response);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore getIndividualTargetById: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<IndividualTargetModel>> getIndividualTargets({
|
||||
required String companyId,
|
||||
String? targetId,
|
||||
String? staffId,
|
||||
}) async {
|
||||
try {
|
||||
var query = _supabase
|
||||
.from(Tables.individualTargets)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
''')
|
||||
.eq('company_id', companyId);
|
||||
|
||||
if (targetId != null && targetId.isNotEmpty) {
|
||||
query = query.eq('target_id', targetId);
|
||||
}
|
||||
|
||||
if (staffId != null && staffId.isNotEmpty) {
|
||||
query = query.eq('staff_id', staffId);
|
||||
}
|
||||
|
||||
final response = await query.order('created_at', ascending: false);
|
||||
return (response as List<dynamic>)
|
||||
.map((e) => IndividualTargetModel.fromMap(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore getIndividualTargets: $e');
|
||||
throw Exception('Errore nel recupero degli obiettivi individuali: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<IndividualTargetModel?> getIndividualTargetForStaffAndTarget({
|
||||
required String companyId,
|
||||
required String targetId,
|
||||
required String staffId,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from(Tables.individualTargets)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
''')
|
||||
.eq('company_id', companyId)
|
||||
.eq('target_id', targetId)
|
||||
.eq('staff_id', staffId)
|
||||
.maybeSingle();
|
||||
|
||||
if (response == null) return null;
|
||||
return IndividualTargetModel.fromMap(response);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore getIndividualTargetForStaffAndTarget: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<IndividualTargetModel> saveIndividualTarget(
|
||||
IndividualTargetModel target,
|
||||
) async {
|
||||
try {
|
||||
final Map<String, dynamic> data = target.toMap();
|
||||
final response = await _supabase
|
||||
.from(Tables.individualTargets)
|
||||
.upsert(data)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.staffMembers}(*)
|
||||
''')
|
||||
.single();
|
||||
|
||||
notifyTargetsChanged();
|
||||
return IndividualTargetModel.fromMap(response);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore saveIndividualTarget: $e');
|
||||
throw Exception('Errore nel salvataggio dell\'obiettivo individuale: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteIndividualTarget(String id) async {
|
||||
try {
|
||||
await _supabase.from(Tables.individualTargets).delete().eq('id', id);
|
||||
notifyTargetsChanged();
|
||||
} on Exception catch (e) {
|
||||
debugPrint('Errore deleteIndividualTarget: $e');
|
||||
throw Exception('Errore nell\'eliminazione dell\'obiettivo individuale: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_targetsChangedController.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/enums_and_consts/consts.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'target_metrics.dart';
|
||||
|
||||
class IndividualTargetModel extends Equatable {
|
||||
final String? id;
|
||||
final DateTime? createdAt;
|
||||
final String targetId;
|
||||
final String companyId;
|
||||
final String staffId;
|
||||
final StaffMemberModel? staff;
|
||||
final String? description;
|
||||
final TargetMetrics metrics;
|
||||
|
||||
const IndividualTargetModel({
|
||||
this.id,
|
||||
this.createdAt,
|
||||
required this.targetId,
|
||||
required this.companyId,
|
||||
required this.staffId,
|
||||
this.staff,
|
||||
this.description,
|
||||
this.metrics = const TargetMetrics(),
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
createdAt,
|
||||
targetId,
|
||||
companyId,
|
||||
staffId,
|
||||
staff,
|
||||
description,
|
||||
metrics,
|
||||
];
|
||||
|
||||
IndividualTargetModel copyWith({
|
||||
String? id,
|
||||
DateTime? createdAt,
|
||||
String? targetId,
|
||||
String? companyId,
|
||||
String? staffId,
|
||||
StaffMemberModel? staff,
|
||||
String? description,
|
||||
TargetMetrics? metrics,
|
||||
}) {
|
||||
return IndividualTargetModel(
|
||||
id: id ?? this.id,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
targetId: targetId ?? this.targetId,
|
||||
companyId: companyId ?? this.companyId,
|
||||
staffId: staffId ?? this.staffId,
|
||||
staff: staff ?? this.staff,
|
||||
description: description ?? this.description,
|
||||
metrics: metrics ?? this.metrics,
|
||||
);
|
||||
}
|
||||
|
||||
factory IndividualTargetModel.empty({
|
||||
String targetId = '',
|
||||
String companyId = '',
|
||||
String staffId = '',
|
||||
}) {
|
||||
return IndividualTargetModel(
|
||||
targetId: targetId,
|
||||
companyId: companyId,
|
||||
staffId: staffId,
|
||||
metrics: TargetMetrics.empty(),
|
||||
);
|
||||
}
|
||||
|
||||
factory IndividualTargetModel.fromMap(Map<String, dynamic> map) {
|
||||
return IndividualTargetModel(
|
||||
id: map['id'] as String?,
|
||||
createdAt:
|
||||
map['created_at'] != null
|
||||
? DateTime.parse(map['created_at'] as String)
|
||||
: null,
|
||||
targetId: map['target_id'] as String? ?? '',
|
||||
companyId: map['company_id'] as String? ?? '',
|
||||
staffId: map['staff_id'] as String? ?? '',
|
||||
staff:
|
||||
map[Tables.staffMembers] != null
|
||||
? StaffMemberModel.fromMap(
|
||||
map[Tables.staffMembers] as Map<String, dynamic>,
|
||||
)
|
||||
: null,
|
||||
description: map['description'] as String?,
|
||||
metrics:
|
||||
map['metrics'] != null && map['metrics'] is Map<String, dynamic>
|
||||
? TargetMetrics.fromMap(map['metrics'] as Map<String, dynamic>)
|
||||
: TargetMetrics.fromMap(map),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
if (id != null && id!.isNotEmpty) 'id': id,
|
||||
'target_id': targetId,
|
||||
'company_id': companyId,
|
||||
'staff_id': staffId,
|
||||
'description': description,
|
||||
'metrics': metrics.toMap(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class TargetMetrics extends Equatable {
|
||||
final int mnp1;
|
||||
final int mnp2;
|
||||
final int unica1;
|
||||
final int unica2;
|
||||
final int nip1;
|
||||
final int nip2;
|
||||
final int business1;
|
||||
final int business2;
|
||||
final int contenuti1;
|
||||
final int contenuti2;
|
||||
final int fin1;
|
||||
final int fin2;
|
||||
final int upselling1;
|
||||
final int upselling2;
|
||||
final int cb1;
|
||||
final int cb2;
|
||||
final int al1;
|
||||
final int al2;
|
||||
final int telepass1;
|
||||
final int telepass2;
|
||||
final int energiaTim;
|
||||
final int energiaAltri;
|
||||
|
||||
const TargetMetrics({
|
||||
this.mnp1 = 0,
|
||||
this.mnp2 = 0,
|
||||
this.unica1 = 0,
|
||||
this.unica2 = 0,
|
||||
this.nip1 = 0,
|
||||
this.nip2 = 0,
|
||||
this.business1 = 0,
|
||||
this.business2 = 0,
|
||||
this.contenuti1 = 0,
|
||||
this.contenuti2 = 0,
|
||||
this.fin1 = 0,
|
||||
this.fin2 = 0,
|
||||
this.upselling1 = 0,
|
||||
this.upselling2 = 0,
|
||||
this.cb1 = 0,
|
||||
this.cb2 = 0,
|
||||
this.al1 = 0,
|
||||
this.al2 = 0,
|
||||
this.telepass1 = 0,
|
||||
this.telepass2 = 0,
|
||||
this.energiaTim = 0,
|
||||
this.energiaAltri = 0,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
mnp1,
|
||||
mnp2,
|
||||
unica1,
|
||||
unica2,
|
||||
nip1,
|
||||
nip2,
|
||||
business1,
|
||||
business2,
|
||||
contenuti1,
|
||||
contenuti2,
|
||||
fin1,
|
||||
fin2,
|
||||
upselling1,
|
||||
upselling2,
|
||||
cb1,
|
||||
cb2,
|
||||
al1,
|
||||
al2,
|
||||
telepass1,
|
||||
telepass2,
|
||||
energiaTim,
|
||||
energiaAltri,
|
||||
];
|
||||
|
||||
TargetMetrics copyWith({
|
||||
int? mnp1,
|
||||
int? mnp2,
|
||||
int? unica1,
|
||||
int? unica2,
|
||||
int? nip1,
|
||||
int? nip2,
|
||||
int? business1,
|
||||
int? business2,
|
||||
int? contenuti1,
|
||||
int? contenuti2,
|
||||
int? fin1,
|
||||
int? fin2,
|
||||
int? upselling1,
|
||||
int? upselling2,
|
||||
int? cb1,
|
||||
int? cb2,
|
||||
int? al1,
|
||||
int? al2,
|
||||
int? telepass1,
|
||||
int? telepass2,
|
||||
int? energiaTim,
|
||||
int? energiaAltri,
|
||||
}) {
|
||||
return TargetMetrics(
|
||||
mnp1: mnp1 ?? this.mnp1,
|
||||
mnp2: mnp2 ?? this.mnp2,
|
||||
unica1: unica1 ?? this.unica1,
|
||||
unica2: unica2 ?? this.unica2,
|
||||
nip1: nip1 ?? this.nip1,
|
||||
nip2: nip2 ?? this.nip2,
|
||||
business1: business1 ?? this.business1,
|
||||
business2: business2 ?? this.business2,
|
||||
contenuti1: contenuti1 ?? this.contenuti1,
|
||||
contenuti2: contenuti2 ?? this.contenuti2,
|
||||
fin1: fin1 ?? this.fin1,
|
||||
fin2: fin2 ?? this.fin2,
|
||||
upselling1: upselling1 ?? this.upselling1,
|
||||
upselling2: upselling2 ?? this.upselling2,
|
||||
cb1: cb1 ?? this.cb1,
|
||||
cb2: cb2 ?? this.cb2,
|
||||
al1: al1 ?? this.al1,
|
||||
al2: al2 ?? this.al2,
|
||||
telepass1: telepass1 ?? this.telepass1,
|
||||
telepass2: telepass2 ?? this.telepass2,
|
||||
energiaTim: energiaTim ?? this.energiaTim,
|
||||
energiaAltri: energiaAltri ?? this.energiaAltri,
|
||||
);
|
||||
}
|
||||
|
||||
factory TargetMetrics.empty() {
|
||||
return const TargetMetrics();
|
||||
}
|
||||
|
||||
factory TargetMetrics.fromMap(Map<String, dynamic> map) {
|
||||
return TargetMetrics(
|
||||
mnp1: (map['mnp_1'] as num?)?.toInt() ?? 0,
|
||||
mnp2: (map['mnp_2'] as num?)?.toInt() ?? 0,
|
||||
unica1: (map['unica_1'] as num?)?.toInt() ?? 0,
|
||||
unica2: (map['unica_2'] as num?)?.toInt() ?? 0,
|
||||
nip1: (map['nip_1'] as num?)?.toInt() ?? 0,
|
||||
nip2: (map['nip_2'] as num?)?.toInt() ?? 0,
|
||||
business1: (map['business_1'] as num?)?.toInt() ?? 0,
|
||||
business2: (map['business_2'] as num?)?.toInt() ?? 0,
|
||||
contenuti1: (map['contenuti_1'] as num?)?.toInt() ?? 0,
|
||||
contenuti2: (map['contenuti_2'] as num?)?.toInt() ?? 0,
|
||||
fin1: (map['fin_1'] as num?)?.toInt() ?? 0,
|
||||
fin2: (map['fin_2'] as num?)?.toInt() ?? 0,
|
||||
upselling1: (map['upselling_1'] as num?)?.toInt() ?? 0,
|
||||
upselling2: (map['upselling_2'] as num?)?.toInt() ?? 0,
|
||||
cb1: (map['cb_1'] as num?)?.toInt() ?? 0,
|
||||
cb2: (map['cb_2'] as num?)?.toInt() ?? 0,
|
||||
al1: (map['al_1'] as num?)?.toInt() ?? 0,
|
||||
al2: (map['al_2'] as num?)?.toInt() ?? 0,
|
||||
telepass1: (map['telepass_1'] as num?)?.toInt() ?? 0,
|
||||
telepass2: (map['telepass_2'] as num?)?.toInt() ?? 0,
|
||||
energiaTim:
|
||||
(map['energia_tim'] as num?)?.toInt() ??
|
||||
(map['Energia_tim'] as num?)?.toInt() ??
|
||||
0,
|
||||
energiaAltri:
|
||||
(map['energia_altri'] as num?)?.toInt() ??
|
||||
(map['Energia_altri'] as num?)?.toInt() ??
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'mnp_1': mnp1,
|
||||
'mnp_2': mnp2,
|
||||
'unica_1': unica1,
|
||||
'unica_2': unica2,
|
||||
'nip_1': nip1,
|
||||
'nip_2': nip2,
|
||||
'business_1': business1,
|
||||
'business_2': business2,
|
||||
'contenuti_1': contenuti1,
|
||||
'contenuti_2': contenuti2,
|
||||
'fin_1': fin1,
|
||||
'fin_2': fin2,
|
||||
'upselling_1': upselling1,
|
||||
'upselling_2': upselling2,
|
||||
'cb_1': cb1,
|
||||
'cb_2': cb2,
|
||||
'al_1': al1,
|
||||
'al_2': al2,
|
||||
'telepass_1': telepass1,
|
||||
'telepass_2': telepass2,
|
||||
'energia_tim': energiaTim,
|
||||
'energia_altri': energiaAltri,
|
||||
};
|
||||
}
|
||||
|
||||
/// Somma totale di tutti i target del Livello 1 (inclusa energia)
|
||||
int get totalLevel1 =>
|
||||
mnp1 +
|
||||
unica1 +
|
||||
nip1 +
|
||||
business1 +
|
||||
contenuti1 +
|
||||
fin1 +
|
||||
upselling1 +
|
||||
cb1 +
|
||||
al1 +
|
||||
telepass1 +
|
||||
energiaTim +
|
||||
energiaAltri;
|
||||
|
||||
/// Somma totale di tutti i target del Livello 2
|
||||
int get totalLevel2 =>
|
||||
mnp2 +
|
||||
unica2 +
|
||||
nip2 +
|
||||
business2 +
|
||||
contenuti2 +
|
||||
fin2 +
|
||||
upselling2 +
|
||||
cb2 +
|
||||
al2 +
|
||||
telepass2;
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/enums_and_consts/consts.dart';
|
||||
import 'package:flux/features/master_data/store/models/store_model.dart';
|
||||
import 'individual_target_model.dart';
|
||||
import 'target_metrics.dart';
|
||||
|
||||
class TargetModel extends Equatable {
|
||||
final String? id;
|
||||
final DateTime? createdAt;
|
||||
final DateTime startDay;
|
||||
final DateTime endDay;
|
||||
final String? description;
|
||||
final String companyId;
|
||||
final String? storeId;
|
||||
final StoreModel? store;
|
||||
final TargetMetrics metrics;
|
||||
final List<IndividualTargetModel> individualTargets;
|
||||
|
||||
const TargetModel({
|
||||
this.id,
|
||||
this.createdAt,
|
||||
required this.startDay,
|
||||
required this.endDay,
|
||||
this.description,
|
||||
required this.companyId,
|
||||
this.storeId,
|
||||
this.store,
|
||||
this.metrics = const TargetMetrics(),
|
||||
this.individualTargets = const [],
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
createdAt,
|
||||
startDay,
|
||||
endDay,
|
||||
description,
|
||||
companyId,
|
||||
storeId,
|
||||
store,
|
||||
metrics,
|
||||
individualTargets,
|
||||
];
|
||||
|
||||
TargetModel copyWith({
|
||||
String? id,
|
||||
DateTime? createdAt,
|
||||
DateTime? startDay,
|
||||
DateTime? endDay,
|
||||
String? description,
|
||||
String? companyId,
|
||||
String? storeId,
|
||||
StoreModel? store,
|
||||
TargetMetrics? metrics,
|
||||
List<IndividualTargetModel>? individualTargets,
|
||||
}) {
|
||||
return TargetModel(
|
||||
id: id ?? this.id,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
startDay: startDay ?? this.startDay,
|
||||
endDay: endDay ?? this.endDay,
|
||||
description: description ?? this.description,
|
||||
companyId: companyId ?? this.companyId,
|
||||
storeId: storeId ?? this.storeId,
|
||||
store: store ?? this.store,
|
||||
metrics: metrics ?? this.metrics,
|
||||
individualTargets: individualTargets ?? this.individualTargets,
|
||||
);
|
||||
}
|
||||
|
||||
factory TargetModel.empty({
|
||||
String companyId = '',
|
||||
String? storeId,
|
||||
DateTime? startDay,
|
||||
DateTime? endDay,
|
||||
}) {
|
||||
final now = DateTime.now();
|
||||
final firstDayOfMonth = DateTime(now.year, now.month, 1);
|
||||
final lastDayOfMonth = DateTime(now.year, now.month + 1, 0, 23, 59, 59);
|
||||
|
||||
return TargetModel(
|
||||
startDay: startDay ?? firstDayOfMonth,
|
||||
endDay: endDay ?? lastDayOfMonth,
|
||||
companyId: companyId,
|
||||
storeId: storeId,
|
||||
metrics: TargetMetrics.empty(),
|
||||
individualTargets: const [],
|
||||
);
|
||||
}
|
||||
|
||||
factory TargetModel.fromMap(Map<String, dynamic> map) {
|
||||
return TargetModel(
|
||||
id: map['id'] as String?,
|
||||
createdAt:
|
||||
map['created_at'] != null
|
||||
? DateTime.parse(map['created_at'] as String)
|
||||
: null,
|
||||
startDay: DateTime.parse(map['start_day'] as String),
|
||||
endDay: DateTime.parse(map['end_day'] as String),
|
||||
description: map['description'] as String?,
|
||||
companyId: map['company_id'] as String? ?? '',
|
||||
storeId: map['store_id'] as String?,
|
||||
store:
|
||||
map[Tables.stores] != null
|
||||
? StoreModel.fromMap(map[Tables.stores] as Map<String, dynamic>)
|
||||
: null,
|
||||
metrics:
|
||||
map['metrics'] != null && map['metrics'] is Map<String, dynamic>
|
||||
? TargetMetrics.fromMap(map['metrics'] as Map<String, dynamic>)
|
||||
: TargetMetrics.fromMap(map),
|
||||
individualTargets:
|
||||
map[Tables.individualTargets] != null &&
|
||||
map[Tables.individualTargets] is List<dynamic>
|
||||
? (map[Tables.individualTargets] as List<dynamic>)
|
||||
.map(
|
||||
(e) =>
|
||||
IndividualTargetModel.fromMap(e as Map<String, dynamic>),
|
||||
)
|
||||
.toList()
|
||||
: const [],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
if (id != null && id!.isNotEmpty) 'id': id,
|
||||
'start_day': startDay.toIso8601String(),
|
||||
'end_day': endDay.toIso8601String(),
|
||||
'description': description,
|
||||
'company_id': companyId,
|
||||
if (storeId != null && storeId!.isNotEmpty) 'store_id': storeId,
|
||||
'metrics': metrics.toMap(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import 'package:flux/features/tasks/data/task_repository.dart';
|
||||
import 'package:flux/features/tickets/blocs/ticket_list_cubit.dart';
|
||||
import 'package:flux/features/tickets/data/ticket_repository.dart';
|
||||
import 'package:flux/features/tickets/data/tickets_shipping_repository.dart';
|
||||
import 'package:flux/features/targets/data/targets_repository.dart';
|
||||
import 'package:flux/features/tracking/blocs/tracking_cubit.dart';
|
||||
import 'package:flux/features/tracking/data/tracking_repository.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
@@ -69,6 +70,7 @@ Future<void> init() async {
|
||||
sl.registerLazySingleton<DocumentSequenceRepository>(
|
||||
() => DocumentSequenceRepository(),
|
||||
);
|
||||
sl.registerLazySingleton<TargetsRepository>(() => TargetsRepository());
|
||||
|
||||
//Cubit globali
|
||||
sl.registerLazySingleton<SessionCubit>(
|
||||
|
||||
Reference in new issue
Block a user