first commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
class DesignationListModel {
|
||||
DesignationListModel({
|
||||
this.message,
|
||||
this.data,
|
||||
});
|
||||
|
||||
DesignationListModel.fromJson(dynamic json) {
|
||||
message = json['message'];
|
||||
if (json['data'] != null) {
|
||||
data = [];
|
||||
json['data'].forEach((v) {
|
||||
data?.add(DesignationData.fromJson(v));
|
||||
});
|
||||
}
|
||||
}
|
||||
String? message;
|
||||
List<DesignationData>? data;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['message'] = message;
|
||||
if (data != null) {
|
||||
map['data'] = data?.map((v) => v.toJson()).toList();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
class DesignationData {
|
||||
DesignationData({
|
||||
this.id,
|
||||
this.name,
|
||||
this.businessId,
|
||||
this.description,
|
||||
this.status,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
DesignationData.fromJson(dynamic json) {
|
||||
id = json['id'];
|
||||
name = json['name'];
|
||||
businessId = json['business_id'];
|
||||
description = json['description'];
|
||||
status = json['status'];
|
||||
createdAt = json['created_at'];
|
||||
updatedAt = json['updated_at'];
|
||||
}
|
||||
num? id;
|
||||
String? name;
|
||||
num? businessId;
|
||||
String? description;
|
||||
num? status;
|
||||
String? createdAt;
|
||||
String? updatedAt;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['id'] = id;
|
||||
map['name'] = name;
|
||||
map['business_id'] = businessId;
|
||||
map['description'] = description;
|
||||
map['status'] = status;
|
||||
map['created_at'] = createdAt;
|
||||
map['updated_at'] = updatedAt;
|
||||
return map;
|
||||
}
|
||||
}
|
||||
153
lib/Screens/hrm/designation/add_new_designation.dart
Normal file
153
lib/Screens/hrm/designation/add_new_designation.dart
Normal file
@@ -0,0 +1,153 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/Model/designation_list_model.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/repo/designation_repo.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/widgets/label_style.dart';
|
||||
import 'package:mobile_pos/generated/l10n.dart' as lang;
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
|
||||
class AddEditDesignation extends ConsumerStatefulWidget {
|
||||
final bool isEdit;
|
||||
final DesignationData? designation;
|
||||
|
||||
const AddEditDesignation({super.key, this.isEdit = false, this.designation});
|
||||
|
||||
@override
|
||||
ConsumerState<AddEditDesignation> createState() => _AddEditDesignationState();
|
||||
}
|
||||
|
||||
class _AddEditDesignationState extends ConsumerState<AddEditDesignation> {
|
||||
final nameController = TextEditingController();
|
||||
final descriptionController = TextEditingController();
|
||||
String? selectedValue = 'Active';
|
||||
final key = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.isEdit && widget.designation != null) {
|
||||
nameController.text = widget.designation?.name ?? '';
|
||||
descriptionController.text = widget.designation?.description ?? '';
|
||||
selectedValue = widget.designation?.status.toString() == '1' ? 'Active' : 'InActive';
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
if (!key.currentState!.validate()) return;
|
||||
|
||||
final repo = DesignationRepo();
|
||||
EasyLoading.show(status: '${lang.S.of(context).saving}...');
|
||||
|
||||
if (widget.isEdit) {
|
||||
await repo.updateDesignation(
|
||||
ref: ref,
|
||||
context: context,
|
||||
id: widget.designation!.id.toString(),
|
||||
name: nameController.text,
|
||||
status: selectedValue ?? lang.S.of(context).active,
|
||||
description: descriptionController.text,
|
||||
);
|
||||
} else {
|
||||
await repo.createDesignation(
|
||||
ref: ref,
|
||||
context: context,
|
||||
name: nameController.text,
|
||||
status: selectedValue ?? lang.S.of(context).active,
|
||||
description: descriptionController.text,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
nameController.dispose();
|
||||
descriptionController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _lang = lang.S.of(context);
|
||||
return Scaffold(
|
||||
backgroundColor: kWhite,
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
title: Text(
|
||||
widget.isEdit ? _lang.editDesignation : _lang.addDesignation,
|
||||
),
|
||||
bottom: PreferredSize(
|
||||
preferredSize: const Size.fromHeight(1),
|
||||
child: Divider(height: 2, color: kBackgroundColor),
|
||||
),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: key,
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: nameController,
|
||||
decoration: InputDecoration(
|
||||
label: labelSpan(title: _lang.designationName, context: context),
|
||||
hintText: _lang.enterDesignationName,
|
||||
),
|
||||
validator: (value) => value!.isEmpty ? _lang.pleaseEnterDesignationName : null,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
DropdownButtonFormField(
|
||||
value: selectedValue,
|
||||
icon: Icon(Icons.keyboard_arrow_down, color: kNeutral800),
|
||||
decoration: InputDecoration(
|
||||
labelText: _lang.status,
|
||||
hintText: _lang.select,
|
||||
),
|
||||
items:
|
||||
['Active', 'InActive'].map((value) => DropdownMenuItem(value: value, child: Text(value))).toList(),
|
||||
onChanged: (String? newValue) {
|
||||
setState(() {
|
||||
selectedValue = newValue;
|
||||
});
|
||||
},
|
||||
validator: (value) => value == null ? _lang.pleaseSelectAStatus : null,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
TextFormField(
|
||||
controller: descriptionController,
|
||||
decoration: InputDecoration(
|
||||
labelText: _lang.description,
|
||||
hintText: '${_lang.enterDescription}...',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed: () {
|
||||
key.currentState?.reset();
|
||||
nameController.clear();
|
||||
descriptionController.clear();
|
||||
selectedValue = null;
|
||||
setState(() {});
|
||||
},
|
||||
child: Text(_lang.resets),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: _submit,
|
||||
child: Text(widget.isEdit ? _lang.update : _lang.save),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
246
lib/Screens/hrm/designation/designation_list.dart
Normal file
246
lib/Screens/hrm/designation/designation_list.dart
Normal file
@@ -0,0 +1,246 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:hugeicons/hugeicons.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/add_new_designation.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/provider/designation_list_provider.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/repo/designation_repo.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/widgets/deleteing_alart_dialog.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/widgets/global_search_appbar.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/widgets/model_bottom_sheet.dart';
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
|
||||
import '../../../generated/l10n.dart' as lang;
|
||||
import '../../../service/check_user_role_permission_provider.dart';
|
||||
import '../../../widgets/empty_widget/_empty_widget.dart';
|
||||
|
||||
class DesignationListScreen extends ConsumerStatefulWidget {
|
||||
const DesignationListScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<DesignationListScreen> createState() => _DesignationListScreenState();
|
||||
}
|
||||
|
||||
class _DesignationListScreenState extends ConsumerState<DesignationListScreen> {
|
||||
bool _isSearch = false;
|
||||
final _searchController = TextEditingController();
|
||||
String _searchQuery = '';
|
||||
|
||||
Future<void> _refreshList() async {
|
||||
await ref.refresh(designationListProvider.future);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _lang = lang.S.of(context);
|
||||
final theme = Theme.of(context);
|
||||
final designationAsync = ref.watch(designationListProvider);
|
||||
final permissionService = PermissionService(ref);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: kWhite,
|
||||
appBar: GlobalSearchAppBar(
|
||||
isSearch: _isSearch,
|
||||
onSearchToggle: () => setState(() {
|
||||
_isSearch = !_isSearch;
|
||||
_searchController.clear();
|
||||
_searchQuery = '';
|
||||
}),
|
||||
title: _lang.designation,
|
||||
controller: _searchController,
|
||||
onChanged: (query) {
|
||||
setState(() => _searchQuery = query.trim().toLowerCase());
|
||||
},
|
||||
),
|
||||
body: designationAsync.when(
|
||||
data: (data) {
|
||||
if (!permissionService.hasPermission(Permit.designationsRead.value)) {
|
||||
return const Center(child: PermitDenyWidget());
|
||||
}
|
||||
final designationList = data.data ?? [];
|
||||
|
||||
// Search filter
|
||||
final filteredList = designationList.where((item) {
|
||||
final name = item.name?.toLowerCase() ?? '';
|
||||
final desc = item.description?.toLowerCase() ?? '';
|
||||
return name.contains(_searchQuery) || desc.contains(_searchQuery);
|
||||
}).toList();
|
||||
|
||||
if (filteredList.isEmpty) {
|
||||
return Center(child: Text(_lang.noDesignationFound));
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: _refreshList,
|
||||
child: ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: EdgeInsets.zero,
|
||||
itemBuilder: (_, index) {
|
||||
final desig = filteredList[index];
|
||||
return ListTile(
|
||||
onTap: () {
|
||||
viewModalSheet(
|
||||
context: context,
|
||||
item: {
|
||||
_lang.designation: desig.name ?? 'n/a',
|
||||
_lang.status: (desig.status == 1) ? _lang.active : _lang.inactive,
|
||||
},
|
||||
description: desig.description ?? _lang.noDescriptionAvailableForThisDesignation,
|
||||
);
|
||||
},
|
||||
contentPadding: const EdgeInsetsDirectional.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 0,
|
||||
),
|
||||
visualDensity: const VisualDensity(horizontal: -4, vertical: -4),
|
||||
title: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
desig.name ?? 'n/a',
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: desig.status.toString() != '1'
|
||||
? kMainColor.withOpacity(0.1)
|
||||
: Colors.green.withOpacity(0.1)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(right: 8.0, left: 8),
|
||||
child: Text(
|
||||
desig.status.toString() == '1' ? _lang.active : _lang.inactive ?? '',
|
||||
style: theme.textTheme.titleMedium?.copyWith(),
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
padding: EdgeInsets.zero,
|
||||
visualDensity: const VisualDensity(horizontal: -4, vertical: -4),
|
||||
onPressed: () {
|
||||
if (!permissionService.hasPermission(Permit.designationsUpdate.value)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.red,
|
||||
content: Text(_lang.youDoNotPermissionToUpdateDesignation),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => AddEditDesignation(
|
||||
isEdit: true,
|
||||
designation: desig,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
icon: const HugeIcon(
|
||||
icon: HugeIcons.strokeRoundedPencilEdit02,
|
||||
color: kSuccessColor,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
subtitle: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
desig.description ?? 'n/a',
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
color: kNeutral800,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
padding: EdgeInsets.zero,
|
||||
visualDensity: const VisualDensity(horizontal: -4, vertical: -4),
|
||||
onPressed: () async {
|
||||
if (!permissionService.hasPermission(Permit.designationsDelete.value)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.red,
|
||||
content: Text(_lang.youDoNotHavePermissionToDeleteDesignation),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
final confirm = await showDeleteConfirmationDialog(
|
||||
itemName: _lang.designation,
|
||||
context: context,
|
||||
);
|
||||
|
||||
if (confirm) {
|
||||
EasyLoading.show(status: _lang.deleting);
|
||||
final repo = DesignationRepo();
|
||||
try {
|
||||
final result =
|
||||
await repo.deleteDesignation(id: desig.id.toString(), ref: ref, context: context);
|
||||
if (result) {
|
||||
ref.refresh(designationListProvider);
|
||||
EasyLoading.showSuccess(_lang.deletedSuccessFully);
|
||||
} else {
|
||||
EasyLoading.showError(_lang.failedToDeleteTheTax);
|
||||
}
|
||||
} catch (e) {
|
||||
EasyLoading.showError('${_lang.errorDeletingTax}: $e');
|
||||
} finally {
|
||||
EasyLoading.dismiss();
|
||||
}
|
||||
}
|
||||
},
|
||||
icon: const HugeIcon(
|
||||
icon: HugeIcons.strokeRoundedDelete03,
|
||||
color: Colors.red,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (_, __) => const Divider(
|
||||
color: kBackgroundColor,
|
||||
height: 2,
|
||||
),
|
||||
itemCount: filteredList.length,
|
||||
),
|
||||
);
|
||||
},
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (err, _) => Center(child: Text('Failed to load designations.\n$err')),
|
||||
),
|
||||
bottomNavigationBar: permissionService.hasPermission(Permit.designationsCreate.value)
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const AddEditDesignation(),
|
||||
),
|
||||
);
|
||||
},
|
||||
label: Text(_lang.addDesignation),
|
||||
icon: const Icon(Icons.add, color: Colors.white),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/department/model/department_list_model.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/department/repo/department_repo.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/Model/designation_list_model.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/repo/designation_repo.dart';
|
||||
|
||||
final repo = DesignationRepo();
|
||||
final designationListProvider = FutureProvider<DesignationListModel>((ref) => repo.fetchAllDesignation());
|
||||
131
lib/Screens/hrm/designation/repo/designation_repo.dart
Normal file
131
lib/Screens/hrm/designation/repo/designation_repo.dart
Normal file
@@ -0,0 +1,131 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mobile_pos/Screens/hrm/designation/Model/designation_list_model.dart';
|
||||
import 'package:mobile_pos/Screens/hrm/designation/provider/designation_list_provider.dart';
|
||||
import '../../../../Const/api_config.dart';
|
||||
import '../../../../http_client/custome_http_client.dart';
|
||||
import '../../../../http_client/customer_http_client_get.dart';
|
||||
|
||||
class DesignationRepo {
|
||||
Future<DesignationListModel> fetchAllDesignation() async {
|
||||
CustomHttpClientGet clientGet = CustomHttpClientGet(client: http.Client());
|
||||
final uri = Uri.parse('${APIConfig.url}/designations');
|
||||
|
||||
final response = await clientGet.get(url: uri);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final parsedData = jsonDecode(response.body);
|
||||
|
||||
return DesignationListModel.fromJson(parsedData);
|
||||
} else {
|
||||
throw Exception('Failed to fetch Designation list');
|
||||
}
|
||||
}
|
||||
|
||||
/// Create Designation
|
||||
Future<void> createDesignation({
|
||||
required WidgetRef ref,
|
||||
required BuildContext context,
|
||||
required String name,
|
||||
required String status,
|
||||
required String description,
|
||||
}) async {
|
||||
final uri = Uri.parse('${APIConfig.url}/designations');
|
||||
final body = jsonEncode({
|
||||
'name': name,
|
||||
'status': status == 'Active' ? '1' : "0",
|
||||
'description': description,
|
||||
});
|
||||
|
||||
try {
|
||||
CustomHttpClient client = CustomHttpClient(client: http.Client(), context: context, ref: ref);
|
||||
|
||||
final response = await client.post(
|
||||
url: uri,
|
||||
addContentTypeInHeader: true,
|
||||
body: body,
|
||||
);
|
||||
|
||||
final data = jsonDecode(response.body);
|
||||
EasyLoading.dismiss();
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
ref.refresh(designationListProvider);
|
||||
Navigator.pop(context);
|
||||
EasyLoading.showSuccess('Designation Created Successfully');
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Failed: ${data['message']}')));
|
||||
}
|
||||
} catch (e) {
|
||||
EasyLoading.dismiss();
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error: $e')));
|
||||
}
|
||||
}
|
||||
|
||||
/// Update Designation
|
||||
Future<void> updateDesignation({
|
||||
required WidgetRef ref,
|
||||
required BuildContext context,
|
||||
required String id,
|
||||
required String name,
|
||||
required String status,
|
||||
required String description,
|
||||
}) async {
|
||||
final uri = Uri.parse('${APIConfig.url}/designations/$id');
|
||||
final body = jsonEncode({
|
||||
'_method': 'put',
|
||||
'name': name,
|
||||
'status': status == 'Active' ? '1' : "0",
|
||||
'description': description,
|
||||
});
|
||||
|
||||
try {
|
||||
CustomHttpClient client = CustomHttpClient(client: http.Client(), context: context, ref: ref);
|
||||
|
||||
final response = await client.post(
|
||||
url: uri,
|
||||
addContentTypeInHeader: true,
|
||||
body: body,
|
||||
);
|
||||
|
||||
final data = jsonDecode(response.body);
|
||||
EasyLoading.dismiss();
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
ref.refresh(designationListProvider);
|
||||
Navigator.pop(context);
|
||||
EasyLoading.showSuccess('Designation Updated Successfully');
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Failed: ${data['message']}')));
|
||||
}
|
||||
} catch (e) {
|
||||
EasyLoading.dismiss();
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error: $e')));
|
||||
}
|
||||
}
|
||||
|
||||
///________Delete_Designations______________________________________________________
|
||||
Future<bool> deleteDesignation({required String id, required BuildContext context, required WidgetRef ref}) async {
|
||||
try {
|
||||
final url = Uri.parse('${APIConfig.url}/designations/$id');
|
||||
CustomHttpClient customHttpClient = CustomHttpClient(ref: ref, context: context, client: http.Client());
|
||||
final response = await customHttpClient.delete(url: url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return true;
|
||||
} else {
|
||||
print('Error deleting Designations: ${response.statusCode} - ${response.body}');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
print('Error during delete operation: $error');
|
||||
return false;
|
||||
} finally {
|
||||
EasyLoading.dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user