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

396 lines
13 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/entity/Voucher.dart';
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/models/user.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/DataService.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:aman_kassa_flutter/widgets/loader/Dialogs.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>();
DataService _dataService = locator<DataService>();
final GlobalKey<State> _keyLoader = new GlobalKey<State>();
bool isMoneyCheckBusy;
bool closeSmenaBusy;
bool openSmenaBusy;
bool depositBusy;
bool withdrawalBusy;
bool xReportBusy;
bool updateCatalog;
@override
void initState() {
super.initState();
isMoneyCheckBusy = false;
closeSmenaBusy = false;
openSmenaBusy = false;
depositBusy = false;
withdrawalBusy = false;
xReportBusy = false;
updateCatalog = false;
}
void _closeSmena() async {
setState(() {
closeSmenaBusy = true;
});
try {
await Redux.store.dispatch(closeSmena);
} catch (e) {
print(e);
} finally {
setState(() {
closeSmenaBusy = false;
});
}
}
void _openSmena() async {
setState(() {
openSmenaBusy = true;
});
try {
await Redux.store.dispatch(openSmena);
} catch (e) {
print(e);
} finally {
setState(() {
openSmenaBusy = false;
});
}
}
void _xReport() async {
setState(() {
xReportBusy = true;
});
try {
User user = Redux.store.state.userState.user;
Response response = await _api.xReport(user.token);
if (response.operation) {
_navigator.push(ImageShowRoute,
arguments:
ImageShowModel(data: response.body['check'], title: 'X Отчет'));
String url = response?.body['link'];
_dataService.insertVoucher(
user: user,
name: 'X Отчет',
base64Data: response.body['check'],
type: VoucherTypeReport,
url: url);
} else {
_dialog.showDialog(description: response.body['message']);
}
} catch (e) {
print(e);
} finally {
setState(() {
xReportBusy = false;
});
}
}
void _updateCatalog() async {
setState(() {
updateCatalog = true;
});
try {
Dialogs.showLoadingDialog(context, _keyLoader);
await _dataService.getDataFromServer(Redux.store.state.userState.user);
Navigator.of(_keyLoader.currentContext, rootNavigator: true).pop();
} catch (e) {
print(e);
} finally {
setState(() {
updateCatalog = 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(
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
top: 10.0,
left: 20.0,
right: 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,
Wrap(
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
//crossAxisAlignment: CrossAxisAlignment.start,
runAlignment: WrapAlignment.center,
alignment: WrapAlignment.spaceEvenly,
crossAxisAlignment: WrapCrossAlignment.start,
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,
),
AmanIconButton(
title: 'Х Отчет',
onPressed: _xReport,
busy: xReportBusy,
mainColor: primaryColor,
icon: Icons.description,
),
AmanIconButton(
title: 'История чеков',
onPressed: () {
_navigator.push(HistoryViewRoute);
},
mainColor: yellowColor,
icon: MdiIcons.history,
),
AmanIconButton(
title: 'Обновить номенклатуру',
onPressed: _updateCatalog,
busy: updateCatalog,
mainColor: greenColor,
icon: MdiIcons.databaseRefresh,
),
],
),
verticalSpaceMedium,
Container(
margin:
const EdgeInsets.only(left: 20.0, right: 20.0, bottom: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
flex: 1,
child: BusyButton(
title: 'Взнос наличных',
onPressed: _deposit,
busy: depositBusy,
),
),
horizontalSpaceSmall,
Flexible(
flex: 1,
child: BusyButton(
title: 'Снятие наличных',
onPressed: _withdrawal,
mainColor: redColor,
busy: withdrawalBusy,
),
),
],
),
),
],
),
);
}
void changeMode(el) {
Redux.store.dispatch(changeModeFromSetting(el));
}
}