Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/features/adjust/bloc/tham_so_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_event.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_state.dart';
import 'package:gemstore_frontend/features/adjust/tham_so_repository.dart';

class ThamSoBloc extends Bloc<ThamSoEvent, ThamSoState> {
final ThamSoRepository thamSoRepository;

ThamSoBloc(this.thamSoRepository) : super(ThamSoStateInitial()) {
on<ThamSoEventStart>((event, emit) {
emit(ThamSoStateInitial());
});

on<ThamSoEventGetAll>((event, emit) async {
emit(ThamSoStateLoading());
try {
final thamSoList = await thamSoRepository.getAll();
emit(ThamSoStateLoaded(thamSoList));
} catch (e) {
emit(ThamSoStateError('Failed to get all tham so: $e'));
}
});

on<ThamSoEventUpdate>((event, emit) async {
emit(ThamSoStateLoading());
try {
await thamSoRepository.updateThamSo(event.tenThamSo, event.giaTri);
add(ThamSoEventGetAll());
} catch (e) {
emit(ThamSoStateError('Failed to update tham so: $e'));
}
});
}
}
12 changes: 12 additions & 0 deletions lib/features/adjust/bloc/tham_so_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ThamSoEvent {}

class ThamSoEventStart extends ThamSoEvent {}

class ThamSoEventGetAll extends ThamSoEvent {}

class ThamSoEventUpdate extends ThamSoEvent {
final String tenThamSo;
final String giaTri;

ThamSoEventUpdate(this.tenThamSo, this.giaTri);
}
19 changes: 19 additions & 0 deletions lib/features/adjust/bloc/tham_so_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:gemstore_frontend/models/tham_so.dart';

sealed class ThamSoState {}

class ThamSoStateInitial extends ThamSoState {}

class ThamSoStateLoading extends ThamSoState {}

class ThamSoStateLoaded extends ThamSoState {
final List<ThamSo> thamSoList;

ThamSoStateLoaded(this.thamSoList);
}

class ThamSoStateError extends ThamSoState {
final String message;

ThamSoStateError(this.message);
}
28 changes: 28 additions & 0 deletions lib/features/adjust/tham_so_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:dio/dio.dart';
import 'package:gemstore_frontend/models/tham_so.dart';

class ThamSoApi {
final Dio dio;

ThamSoApi(this.dio);

Future<List<ThamSo>> getAll() async {
try {
final response = await dio.get('/api/thamso');
return ThamSo.fromJsonList(response.data);
} catch (e) {
throw Exception('Error fetching tham so: $e');
}
}

Future<void> updateThamSo(String tenThamSo, String giaTri) async {
try {
await dio.post('/api/thamso', data: {
'tenThamSo': tenThamSo,
'giaTri': giaTri,
});
} catch (e) {
throw Exception('Error updating tham so: $e');
}
}
}
24 changes: 24 additions & 0 deletions lib/features/adjust/tham_so_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:gemstore_frontend/features/adjust/tham_so_api.dart';
import 'package:gemstore_frontend/models/tham_so.dart';

class ThamSoRepository {
final ThamSoApi thamSoApi;

ThamSoRepository(this.thamSoApi);

Future<List<ThamSo>> getAll() async {
try {
return await thamSoApi.getAll();
} catch (e) {
throw Exception('Error fetching tham so: $e');
}
}

Future<void> updateThamSo(String tenThamSo, String giaTri) async {
try {
await thamSoApi.updateThamSo(tenThamSo, giaTri);
} catch (e) {
throw Exception('Error updating tham so: $e');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PhieuBanHangBloc extends Bloc<PhieuBanHangEvent, PhieuBanHangState> {
await phieuBanHangRepository.create(event.khachHang, event.sanPhamBan);
add(PhieuBanHangEventGetAll());
} catch (e) {
emit(PhieuBanHangStateFailure("Lỗi tạo phiếu bán hàng: ${e.toString()}"));
emit(PhieuBanHangStateFailure("- Lỗi tạo: Số lượng bán không hợp lệ"));
}
}

Expand All @@ -43,7 +43,7 @@ class PhieuBanHangBloc extends Bloc<PhieuBanHangEvent, PhieuBanHangState> {
} catch (e) {
emit(
PhieuBanHangStateFailure(
"Lỗi lấy danh sách phiếu bán hàng: ${e.toString()}",
"Lỗi lấy danh sách: ${e.toString()}",
),
);
}
Expand All @@ -58,7 +58,7 @@ class PhieuBanHangBloc extends Bloc<PhieuBanHangEvent, PhieuBanHangState> {
await phieuBanHangRepository.delete(event.soPhieu);
add(PhieuBanHangEventGetAll());
} catch (e) {
emit(PhieuBanHangStateFailure("Lỗi xóa phiếu bán hàng: ${e.toString()}"));
emit(PhieuBanHangStateFailure("Lỗi xóa: ${e.toString()}"));
}
}

Expand All @@ -77,7 +77,7 @@ class PhieuBanHangBloc extends Bloc<PhieuBanHangEvent, PhieuBanHangState> {
} catch (e) {
emit(
PhieuBanHangStateFailure(
"Lỗi cập nhật phiếu bán hàng: ${e.toString()}",
"- Lỗi cập nhật: Số lượng bán không hợp lệ",
),
);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gemstore_frontend/config/http_client.dart';
import 'package:gemstore_frontend/config/router.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_bloc.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_event.dart';
import 'package:gemstore_frontend/features/adjust/tham_so_api.dart';
import 'package:gemstore_frontend/features/adjust/tham_so_repository.dart';
import 'package:gemstore_frontend/features/home/don_vi_tinh/bloc/don_vi_tinh_bloc.dart';
import 'package:gemstore_frontend/features/home/don_vi_tinh/bloc/don_vi_tinh_event.dart';
import 'package:gemstore_frontend/features/home/don_vi_tinh/don_vi_tinh_api.dart';
Expand Down Expand Up @@ -77,6 +81,9 @@ class MyApp extends StatelessWidget {
RepositoryProvider(
create: (context) => BaoCaoRepository(BaoCaoApi(dio)),
),
RepositoryProvider(
create: (context) => ThamSoRepository(ThamSoApi(dio)),
),
],
child: MultiBlocProvider(
providers: [
Expand Down Expand Up @@ -118,6 +125,9 @@ class MyApp extends StatelessWidget {
BlocProvider(
create: (context) => BaoCaoBloc(context.read<BaoCaoRepository>()),
),
BlocProvider(
create: (context) => ThamSoBloc(context.read<ThamSoRepository>()),
),
],
child: AppContent(),
),
Expand Down Expand Up @@ -145,6 +155,7 @@ class _AppContentState extends State<AppContent> {
context.read<PhieuDichVuBloc>().add(PhieuDichVuEventStart());
context.read<SanPhamBloc>().add(SanPhamEventStart());
context.read<BaoCaoBloc>().add(BaoCaoEventStart());
context.read<ThamSoBloc>().add(ThamSoEventStart());
}

@override
Expand Down
27 changes: 27 additions & 0 deletions lib/models/tham_so.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ThamSo {
String tenThamSo;
int giaTri;

ThamSo({
required this.tenThamSo,
required this.giaTri,
});

factory ThamSo.fromJson(Map<String, dynamic> json) {
return ThamSo(
tenThamSo: json['tenThamSo'],
giaTri: json['giaTri'],
);
}

static List<ThamSo> fromJsonList(List<dynamic> jsonList) {
return jsonList.map((json) => ThamSo.fromJson(json as Map<String, dynamic>)).toList();
}

Map<String, dynamic> toJson() {
return {
'tenThamSo': tenThamSo,
'giaTri': giaTri,
};
}
}
27 changes: 26 additions & 1 deletion lib/screens/home/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gemstore_frontend/config/format.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_bloc.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_event.dart';
import 'package:gemstore_frontend/features/adjust/bloc/tham_so_state.dart';
import 'package:gemstore_frontend/features/home/don_vi_tinh/bloc/don_vi_tinh_bloc.dart';
import 'package:gemstore_frontend/features/home/don_vi_tinh/bloc/don_vi_tinh_event.dart';
import 'package:gemstore_frontend/features/home/don_vi_tinh/bloc/don_vi_tinh_state.dart';
Expand Down Expand Up @@ -33,6 +36,7 @@ import 'package:gemstore_frontend/models/phieu_ban_hang.dart';
import 'package:gemstore_frontend/models/phieu_dich_vu.dart';
import 'package:gemstore_frontend/models/phieu_mua_hang.dart';
import 'package:gemstore_frontend/models/san_pham.dart';
import 'package:gemstore_frontend/models/tham_so.dart';
import 'package:gemstore_frontend/screens/home/view_list/bao_cao_screen.dart';
import 'package:gemstore_frontend/screens/home/view_list/danh_sach_san_pham_screen.dart';
import 'package:gemstore_frontend/screens/home/view_list/phieunhapxuat/phieu_ban_hang_screen.dart';
Expand All @@ -42,6 +46,7 @@ import 'package:gemstore_frontend/screens/home/view_list/quanlythongtin/don_vi_t
import 'package:gemstore_frontend/screens/home/view_list/quanlythongtin/loai_dich_vu_screen.dart';
import 'package:gemstore_frontend/screens/home/view_list/quanlythongtin/loai_san_pham_screen.dart';
import 'package:gemstore_frontend/screens/home/view_list/quanlythongtin/nha_cung_cap_screen.dart';
import 'package:gemstore_frontend/screens/home/view_list/tham_so_screen.dart';
import 'package:gemstore_frontend/screens/reusable_widgets/error_dialog.dart';
import 'package:go_router/go_router.dart';

Expand Down Expand Up @@ -71,6 +76,7 @@ class _HomeScreenState extends State<HomeScreen> {
List<PhieuBanHang> _phieuBanHangs = [];
List<PhieuDichVu> _phieuDichVus = [];
List<SanPham> _sanPhams = [];
List<ThamSo> _thamSos = [];

@override
void initState() {
Expand Down Expand Up @@ -174,6 +180,17 @@ class _HomeScreenState extends State<HomeScreen> {
}
},
),
BlocListener<ThamSoBloc, ThamSoState>(
listener: (context, state) {
if (state is ThamSoStateLoaded) {
setState(() {
_thamSos = state.thamSoList;
});
} else if (state is ThamSoStateError) {
_handleError("Lỗi tham số: ${state.message}");
}
},
),
],
child: Scaffold(
body: SafeArea(
Expand Down Expand Up @@ -318,6 +335,11 @@ class _HomeScreenState extends State<HomeScreen> {

// Reports Section
_buildMenuItemWithDot('Báo cáo', 'reports'),

const SizedBox(height: 16.0),

// Reports Section
_buildMenuItemWithDot('Điều chỉnh tham số', 'adjust_parameters'),
],
),
),
Expand Down Expand Up @@ -573,7 +595,7 @@ class _HomeScreenState extends State<HomeScreen> {
return LoaiSanPhamScreen(data: _loaiSanPhams,
listDonViTinh: _donViTinhs);
case 'service_type_management':
return LoaiDichVuScreen(data: _loaiDichVus);
return LoaiDichVuScreen(data: _loaiDichVus, thamSo: _thamSos);
case 'sales_invoice':
return PhieuBanHangScreen(
data: _phieuBanHangs,
Expand All @@ -599,6 +621,8 @@ class _HomeScreenState extends State<HomeScreen> {
_phieuBanHangs,
)
);
case 'adjust_parameters':
return ThamSoScreen(thamSoList: _thamSos);
default:
return Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down Expand Up @@ -626,5 +650,6 @@ class _HomeScreenState extends State<HomeScreen> {
context.read<PhieuMuaHangBloc>().add(PhieuMuaHangEventGetAll());
context.read<PhieuBanHangBloc>().add(PhieuBanHangEventGetAll());
context.read<PhieuDichVuBloc>().add(PhieuDichVuEventGetAll());
context.read<ThamSoBloc>().add(ThamSoEventGetAll());
}
}
21 changes: 14 additions & 7 deletions lib/screens/home/view_list/quanlythongtin/loai_dich_vu_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import 'package:gemstore_frontend/features/home/loai_dich_vu/bloc/loai_dich_vu_b
import 'package:gemstore_frontend/features/home/loai_dich_vu/bloc/loai_dich_vu_event.dart';
import 'package:gemstore_frontend/features/home/loai_dich_vu/bloc/loai_dich_vu_state.dart';
import 'package:gemstore_frontend/models/loai_dich_vu.dart';
import 'package:gemstore_frontend/models/tham_so.dart';
import 'package:gemstore_frontend/screens/reusable_widgets/format_column_data.dart';
import 'package:gemstore_frontend/screens/reusable_widgets/qltt_create_dialog.dart';
import 'package:gemstore_frontend/screens/reusable_widgets/reusable_table_widget.dart';

class LoaiDichVuScreen extends StatefulWidget {
final List<LoaiDichVu> data;
const LoaiDichVuScreen({super.key, required this.data});
final List<ThamSo> thamSo;

const LoaiDichVuScreen({super.key, required this.data, required this.thamSo});

@override
State<LoaiDichVuScreen> createState() => _LoaiDichVuScreenState();
}

class _LoaiDichVuScreenState extends State<LoaiDichVuScreen> {
final List<TableColumn> _columns = [
late List<TableColumn> _columns;
bool _isLoading = false;

@override
void initState() {
super.initState();
_columns = [
TableColumn(
key: 'id',
header: 'Mã loại dịch vụ',
Expand Down Expand Up @@ -47,13 +56,11 @@ class _LoaiDichVuScreenState extends State<LoaiDichVuScreen> {
(value) =>
!(double.tryParse(value) == null || double.tryParse(value)! < 0),
errorMessage: 'Phần trăm trả trước phải lớn hơn hoặc bằng 0',
defaultData: widget.thamSo
.firstWhere((thamSo) => thamSo.tenThamSo == 'TiLeTraTruocMacDinh')
.giaTri.toString(),
),
];
bool _isLoading = false;

@override
void initState() {
super.initState();
}

void _showAddLoaiDichVuDialog() {
Expand Down
Loading