Initial commit - app-padrao-1.0
This commit is contained in:
69
lib/features/haircuts/data/models/haircut_model.dart
Normal file
69
lib/features/haircuts/data/models/haircut_model.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'haircut_model.g.dart';
|
||||
|
||||
@HiveType(typeId: 1)
|
||||
class HaircutModel extends HiveObject {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
@HiveField(1)
|
||||
final String userId;
|
||||
|
||||
@HiveField(2)
|
||||
final String clientName;
|
||||
|
||||
@HiveField(3)
|
||||
final String serviceType;
|
||||
|
||||
@HiveField(4)
|
||||
final double price;
|
||||
|
||||
@HiveField(5)
|
||||
final DateTime dateTime;
|
||||
|
||||
@HiveField(6)
|
||||
final String? notes;
|
||||
|
||||
@HiveField(7)
|
||||
final String? transactionId; // Link com a transação financeira
|
||||
|
||||
@HiveField(8, defaultValue: 'Dinheiro')
|
||||
final String paymentMethod;
|
||||
|
||||
HaircutModel({
|
||||
required this.id,
|
||||
required this.userId,
|
||||
required this.clientName,
|
||||
required this.serviceType,
|
||||
required this.price,
|
||||
required this.dateTime,
|
||||
this.notes,
|
||||
this.transactionId,
|
||||
this.paymentMethod = 'Dinheiro',
|
||||
});
|
||||
|
||||
HaircutModel copyWith({
|
||||
String? id,
|
||||
String? userId,
|
||||
String? clientName,
|
||||
String? serviceType,
|
||||
double? price,
|
||||
DateTime? dateTime,
|
||||
String? notes,
|
||||
String? transactionId,
|
||||
String? paymentMethod,
|
||||
}) {
|
||||
return HaircutModel(
|
||||
id: id ?? this.id,
|
||||
userId: userId ?? this.userId,
|
||||
clientName: clientName ?? this.clientName,
|
||||
serviceType: serviceType ?? this.serviceType,
|
||||
price: price ?? this.price,
|
||||
dateTime: dateTime ?? this.dateTime,
|
||||
notes: notes ?? this.notes,
|
||||
transactionId: transactionId ?? this.transactionId,
|
||||
paymentMethod: paymentMethod ?? this.paymentMethod,
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/features/haircuts/data/models/haircut_model.g.dart
Normal file
65
lib/features/haircuts/data/models/haircut_model.g.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'haircut_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class HaircutModelAdapter extends TypeAdapter<HaircutModel> {
|
||||
@override
|
||||
final int typeId = 1;
|
||||
|
||||
@override
|
||||
HaircutModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return HaircutModel(
|
||||
id: fields[0] as String,
|
||||
userId: fields[1] as String,
|
||||
clientName: fields[2] as String,
|
||||
serviceType: fields[3] as String,
|
||||
price: fields[4] as double,
|
||||
dateTime: fields[5] as DateTime,
|
||||
notes: fields[6] as String?,
|
||||
transactionId: fields[7] as String?,
|
||||
paymentMethod: fields[8] == null ? 'Dinheiro' : fields[8] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, HaircutModel obj) {
|
||||
writer
|
||||
..writeByte(9)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.userId)
|
||||
..writeByte(2)
|
||||
..write(obj.clientName)
|
||||
..writeByte(3)
|
||||
..write(obj.serviceType)
|
||||
..writeByte(4)
|
||||
..write(obj.price)
|
||||
..writeByte(5)
|
||||
..write(obj.dateTime)
|
||||
..writeByte(6)
|
||||
..write(obj.notes)
|
||||
..writeByte(7)
|
||||
..write(obj.transactionId)
|
||||
..writeByte(8)
|
||||
..write(obj.paymentMethod);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is HaircutModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
126
lib/features/haircuts/data/repositories/haircut_repository.dart
Normal file
126
lib/features/haircuts/data/repositories/haircut_repository.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:barber_app/core/database/database_service.dart';
|
||||
import 'package:barber_app/features/haircuts/data/models/haircut_model.dart';
|
||||
import 'package:barber_app/features/finances/data/repositories/transaction_repository.dart';
|
||||
|
||||
class HaircutRepository {
|
||||
final _uuid = const Uuid();
|
||||
final _transactionRepo = TransactionRepository();
|
||||
|
||||
String? get _currentUserId => DatabaseService.getCurrentUserId();
|
||||
|
||||
// Criar corte (cria transação automaticamente)
|
||||
Future<HaircutModel?> createHaircut({
|
||||
required String clientName,
|
||||
required String serviceType,
|
||||
required double price,
|
||||
required DateTime dateTime,
|
||||
String? notes,
|
||||
bool isPaid = true,
|
||||
String paymentMethod = 'Dinheiro',
|
||||
}) async {
|
||||
if (_currentUserId == null) return null;
|
||||
|
||||
final haircutId = _uuid.v4();
|
||||
|
||||
// Criar transação financeira (Adicionando método de pagamento na descrição p/ facilitar controle)
|
||||
final transaction = await _transactionRepo.createRevenueFromHaircut(
|
||||
haircutId: haircutId,
|
||||
amount: price,
|
||||
clientName: '$clientName ($paymentMethod)',
|
||||
serviceType: serviceType,
|
||||
isPaid: isPaid,
|
||||
);
|
||||
|
||||
final haircut = HaircutModel(
|
||||
id: haircutId,
|
||||
userId: _currentUserId!,
|
||||
clientName: clientName.trim(),
|
||||
serviceType: serviceType,
|
||||
price: price,
|
||||
dateTime: dateTime,
|
||||
notes: notes?.trim(),
|
||||
transactionId: transaction?.id,
|
||||
paymentMethod: paymentMethod,
|
||||
);
|
||||
|
||||
await DatabaseService.haircutsBoxInstance.put(haircutId, haircut);
|
||||
return haircut;
|
||||
}
|
||||
|
||||
// Listar todos os cortes do usuário
|
||||
List<HaircutModel> getAllHaircuts() {
|
||||
if (_currentUserId == null) return [];
|
||||
return DatabaseService.haircutsBoxInstance.values
|
||||
.where((h) => h.userId == _currentUserId)
|
||||
.toList()
|
||||
..sort((a, b) => b.dateTime.compareTo(a.dateTime));
|
||||
}
|
||||
|
||||
// Cortes de hoje
|
||||
List<HaircutModel> getTodayHaircuts() {
|
||||
final now = DateTime.now();
|
||||
final todayStart = DateTime(now.year, now.month, now.day);
|
||||
final todayEnd = todayStart.add(const Duration(days: 1));
|
||||
|
||||
return getAllHaircuts()
|
||||
.where((h) => h.dateTime.isAfter(todayStart) && h.dateTime.isBefore(todayEnd))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Cortes da semana
|
||||
List<HaircutModel> getWeekHaircuts() {
|
||||
final now = DateTime.now();
|
||||
final weekStart = now.subtract(Duration(days: now.weekday - 1));
|
||||
final weekStartDay = DateTime(weekStart.year, weekStart.month, weekStart.day);
|
||||
|
||||
return getAllHaircuts()
|
||||
.where((h) => h.dateTime.isAfter(weekStartDay))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Cortes do mês
|
||||
List<HaircutModel> getMonthHaircuts() {
|
||||
final now = DateTime.now();
|
||||
final monthStart = DateTime(now.year, now.month, 1);
|
||||
|
||||
return getAllHaircuts()
|
||||
.where((h) => h.dateTime.isAfter(monthStart))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Faturamento do dia
|
||||
double getTodayRevenue() {
|
||||
return getTodayHaircuts().fold(0.0, (sum, h) => sum + h.price);
|
||||
}
|
||||
|
||||
// Faturamento da semana
|
||||
double getWeekRevenue() {
|
||||
return getWeekHaircuts().fold(0.0, (sum, h) => sum + h.price);
|
||||
}
|
||||
|
||||
// Faturamento do mês
|
||||
double getMonthRevenue() {
|
||||
return getMonthHaircuts().fold(0.0, (sum, h) => sum + h.price);
|
||||
}
|
||||
|
||||
// Atualizar corte
|
||||
Future<HaircutModel?> updateHaircut(HaircutModel haircut) async {
|
||||
await DatabaseService.haircutsBoxInstance.put(haircut.id, haircut);
|
||||
return haircut;
|
||||
}
|
||||
|
||||
// Deletar corte
|
||||
Future<void> deleteHaircut(String id) async {
|
||||
final haircut = DatabaseService.haircutsBoxInstance.get(id);
|
||||
if (haircut?.transactionId != null) {
|
||||
await _transactionRepo.deleteTransaction(haircut!.transactionId!);
|
||||
}
|
||||
await DatabaseService.haircutsBoxInstance.delete(id);
|
||||
}
|
||||
|
||||
// Buscar corte por ID
|
||||
HaircutModel? getHaircutById(String id) {
|
||||
return DatabaseService.haircutsBoxInstance.get(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user