aman-kassa-flutter/lib/views/home/tabs/AdditionalTab.dart

321 lines
10 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/models/money.dart';
import 'package:aman_kassa_flutter/core/models/response.dart';
import 'package:aman_kassa_flutter/core/models/dialog_models.dart';
import 'package:aman_kassa_flutter/core/route_names.dart';
import 'package:aman_kassa_flutter/core/services/ApiService.dart';
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
import 'package:aman_kassa_flutter/redux/actions/user_actions.dart';
import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart';
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
import 'package:aman_kassa_flutter/redux/state/setting_state.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:aman_kassa_flutter/shared/ui_helpers.dart';
import 'package:aman_kassa_flutter/views/check/image_show_container.dart';
import 'package:aman_kassa_flutter/widgets/fields/aman_icon_button.dart';
import 'package:aman_kassa_flutter/widgets/fields/aman_icon_button_horizontal.dart';
import 'package:aman_kassa_flutter/widgets/fields/busy_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
class AdditionalTab extends StatefulWidget {
final int index;
AdditionalTab(this.index);
@override
_AdditionalTabState createState() => _AdditionalTabState();
}
class _AdditionalTabState extends State<AdditionalTab> {
ApiService _api = locator<ApiService>();
NavigatorService _navigator = locator<NavigatorService>();
DialogService _dialog = locator<DialogService>();
bool isMoneyCheckBusy;
bool closeSmenaBusy;
bool openSmenaBusy;
bool depositBusy;
bool withdrawalBusy;
bool xReportBusy;
@override
void initState() {
super.initState();
isMoneyCheckBusy = false;
closeSmenaBusy = false;
openSmenaBusy = false;
depositBusy = false;
withdrawalBusy = false;
xReportBusy = false;
}
void _closeSmena() async {
setState(() {
closeSmenaBusy = true;
});
await Redux.store.dispatch(closeSmena);
setState(() {
closeSmenaBusy = false;
});
}
void _openSmena() async {
setState(() {
openSmenaBusy = true;
});
await Redux.store.dispatch(openSmena);
setState(() {
openSmenaBusy = false;
});
}
void _xReport() async {
setState(() {
xReportBusy = true;
});
Response response =
await _api.xReport(Redux.store.state.userState.user.token);
if (response.operation) {
_navigator.push(ImageShowRoute,
arguments: ImageShowModel(response.body['check'], 'X Отчет'));
} else {
_dialog.showDialog(description: response.body['message']);
}
setState(() {
xReportBusy = false;
});
}
void _deposit() async {
setState(() {
depositBusy = true;
});
try {
DialogResponse response = await _dialog.showConfirmationDialogInput(
description: 'Укажите сумму',
cancelTitle: 'Отмена',
confirmationTitle: 'Взнос',
);
if (response.confirmed) {
Response<dynamic> result = await _api.deposit(
Redux.store.state.userState.user.token, response.responseText);
_dialog.showDialog(description: result.body['message']);
if (result.operation) {
await Redux.store.dispatch(checkMoney);
}
}
} catch (e) {
print(e);
} finally {
setState(() {
depositBusy = false;
});
}
}
void _withdrawal() async {
setState(() {
withdrawalBusy = true;
});
try {
DialogResponse response = await _dialog.showConfirmationDialogInput(
description: 'Укажите сумму',
cancelTitle: 'Отмена',
confirmationTitle: 'Снятие',
);
if (response.confirmed) {
Response<dynamic> result = await _api.withdrawal(
Redux.store.state.userState.user.token, response.responseText);
_dialog.showDialog(description: result.body['message']);
if (result.operation) {
await Redux.store.dispatch(checkMoney);
}
}
} catch (e) {
print(e);
} finally {
setState(() {
withdrawalBusy = false;
});
}
}
void _checkMoney() async {
setState(() {
isMoneyCheckBusy = true;
});
try {
await Redux.store.dispatch(checkMoney);
setState(() {
isMoneyCheckBusy = false;
});
} catch (e) {
print(e);
setState(() {
isMoneyCheckBusy = false;
});
}
}
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 15),
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: StoreConnector<AppState, SettingState>(
converter: (store) => store.state.settingState,
builder: (context, vm) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
flex: 1,
child: AmanIconButtonHorizontal(
icon: MdiIcons.calculator,
title: 'Калькулятор',
selected: vm.mode == SettingModeCalc,
onPressed: () => changeMode(false),
)),
Center(
child: Switch(
value: vm.mode == SettingModeKassa,
onChanged: changeMode,
inactiveThumbColor: primaryColor,
activeColor: primaryColor,
)),
Flexible(
flex: 1,
child: AmanIconButtonHorizontal(
icon: MdiIcons.cashRegister,
title: 'Режим кассы',
selected: vm.mode == SettingModeKassa,
onPressed: () => changeMode(true))),
],
);
}),
),
Divider(),
verticalSpaceMedium,
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Center(
child: Column(
children: <Widget>[
Text(
'Денег в кассе:',
style: TextStyle(color: primaryColor, fontSize: 15),
),
SizedBox(
height: 50,
width: double.infinity,
child: StoreConnector<AppState, Money>(
converter: (store) => store.state.userState.money,
builder: (_, vm) {
if (vm.loading == true) {
return Center(
child: SizedBox(
width: 30,
height: 30,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: new AlwaysStoppedAnimation<Color>(
primaryColor),
),
),
),
);
}
return Center(
child: Text(
vm.total != null
? '${vm.total} тенге'
: 'нет информации',
style: TextStyle(
color: vm.total != null
? primaryColor
: Colors.grey.withOpacity(0.5),
fontSize: 25,
fontWeight: FontWeight.bold),
),
);
}),
),
],
)),
),
verticalSpaceMedium,
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
AmanIconButton(
title: 'Открыть смену',
onPressed: _openSmena,
mainColor: greenColor,
busy: openSmenaBusy,
icon: Icons.lock_open,
),
AmanIconButton(
title: 'Закрыть смену',
onPressed: _closeSmena,
mainColor: redColor,
busy: closeSmenaBusy,
icon: Icons.lock_outline,
),
AmanIconButton(
title: 'Денег в кассе',
onPressed: _checkMoney,
busy: isMoneyCheckBusy,
icon: MdiIcons.walletOutline,
mainColor: primaryColor,
),
],
),
verticalSpaceTiny,
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AmanIconButton(
title: 'Х Отчет',
onPressed: _xReport,
busy: xReportBusy,
mainColor: primaryColor,
icon: Icons.description,
),
],
),
verticalSpaceMedium,
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
BusyButton(
title: 'Взнос наличных',
onPressed: _deposit,
busy: depositBusy,
),
BusyButton(
title: 'Снятие наличных',
onPressed: _withdrawal,
mainColor: redColor,
busy: withdrawalBusy,
),
],
),
],
),
);
}
void changeMode(el) {
Redux.store.dispatch(changeModeFromSetting(el));
}
}