From 9486186973d8e7bf58f7751f672de9a080aa0ca1 Mon Sep 17 00:00:00 2001 From: error500 Date: Mon, 6 Sep 2021 18:30:13 +0600 Subject: [PATCH] option_pill.dart --- lib/core/entity/transaction_entity.dart | 5 + .../models/entity_data/transaction_data.dart | 12 +- lib/core/models/flow/transaction_state.dart | 2 + lib/core/redux/actions/sell_actions.dart | 4 +- lib/core/services/data_service.dart | 69 ++++++--- lib/core/services/db_service.dart | 5 +- .../work/tabs/component/option_pill.dart | 31 ---- lib/views/work/tabs/journal_view.dart | 138 ++++++++++-------- .../work/views/payment/payment_view.dart | 47 ++++-- lib/widgets/buttons/option_pill.dart | 52 +++++++ 10 files changed, 239 insertions(+), 126 deletions(-) delete mode 100644 lib/views/work/tabs/component/option_pill.dart create mode 100644 lib/widgets/buttons/option_pill.dart diff --git a/lib/core/entity/transaction_entity.dart b/lib/core/entity/transaction_entity.dart index 3f96474..8366e04 100644 --- a/lib/core/entity/transaction_entity.dart +++ b/lib/core/entity/transaction_entity.dart @@ -6,11 +6,13 @@ const String transactionColumnStatus = 'status'; const String transactionColumnData = 'data'; const String transactionColumnAppCompanyId = 'appCompanyId'; const String transactionColumnCreatedAt = 'createdAt'; +const String transactionColumnUpdatedAt = 'updatedAt'; const int transactionTypeSell = 1; const int transactionTypeBuy = 2; const int transactionStatusPrepare = 0; +const int transactionStatusFinish = 7; class Transaction { @@ -25,6 +27,7 @@ class Transaction { appCompanyId = map[transactionColumnAppCompanyId] as int; data = map[transactionColumnData] as String; createdAt = map[transactionColumnCreatedAt] as String; + updatedAt = map[transactionColumnUpdatedAt] as String; } int? id; @@ -34,6 +37,7 @@ class Transaction { String? data; int? appCompanyId; String? createdAt; + String? updatedAt; Map toMap() { final Map map = { @@ -43,6 +47,7 @@ class Transaction { transactionColumnData: data, transactionColumnAppCompanyId: appCompanyId, transactionColumnCreatedAt: createdAt, + transactionColumnUpdatedAt: updatedAt, }; if (id != null) { map[transactionColumnId] = id; diff --git a/lib/core/models/entity_data/transaction_data.dart b/lib/core/models/entity_data/transaction_data.dart index bebfbca..66cf76a 100644 --- a/lib/core/models/entity_data/transaction_data.dart +++ b/lib/core/models/entity_data/transaction_data.dart @@ -1,3 +1,4 @@ +import 'package:satu/core/models/flow/sell_response.dart'; import 'package:satu/core/utils/utils_parse.dart'; class TransactionData { @@ -7,16 +8,23 @@ class TransactionData { contragentName = map['contragentName'] as String; card = map['card'] as double; nal = map['nal'] as double; + sellResponse = map['sellResponse'] != null + ? SellResponse.fromMap(map['sellResponse']) + : null; } + String contragentName = ''; double card = 0; double nal = 0; + double total = 0; + SellResponse? sellResponse; Map toMap() { final Map map = { 'contragentName': contragentName, - 'card' : card, - 'nal' : nal, + 'card': card, + 'nal': nal, + 'sellResponse': sellResponse }; return map; } diff --git a/lib/core/models/flow/transaction_state.dart b/lib/core/models/flow/transaction_state.dart index ee7dca2..4a1a1a5 100644 --- a/lib/core/models/flow/transaction_state.dart +++ b/lib/core/models/flow/transaction_state.dart @@ -1,4 +1,6 @@ class TransactionState { String? uuid; + String? contragentName = 'Частное лицо'; + String? sectionName = 'Основной'; int? transactionId; } diff --git a/lib/core/redux/actions/sell_actions.dart b/lib/core/redux/actions/sell_actions.dart index 795167b..f9ceb80 100644 --- a/lib/core/redux/actions/sell_actions.dart +++ b/lib/core/redux/actions/sell_actions.dart @@ -80,6 +80,7 @@ ThunkAction addSellItem({required Good good, String? excise}) { } if (transaction == null) { + final DateTime now = DateTime.now(); const uuidTool = Uuid(); final String uuid = uuidTool.v4(); final TransactionData transactionData = TransactionData(); @@ -88,7 +89,8 @@ ThunkAction addSellItem({required Good good, String? excise}) { ..status = transactionStatusPrepare ..appCompanyId = appCompanyId ..type = transactionTypeSell - ..createdAt = DateTime.now().toIso8601String() + ..createdAt = now.toIso8601String() + ..updatedAt = now.toIso8601String() ..data = jsonEncode(transactionData.toMap()); final int returnKey = await _dbService.insert(transactionTableName, transaction.toMap()); diff --git a/lib/core/services/data_service.dart b/lib/core/services/data_service.dart index f10b0f8..a5911ce 100644 --- a/lib/core/services/data_service.dart +++ b/lib/core/services/data_service.dart @@ -1,14 +1,16 @@ - import 'dart:convert'; +import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:satu/core/base/base_service.dart'; import 'package:satu/core/entity/transaction_entity.dart'; +import 'package:satu/core/models/entity_data/transaction_data.dart'; import 'package:satu/core/models/flow/items_bean.dart'; import 'package:satu/core/models/flow/operator_bean.dart'; import 'package:satu/core/models/flow/product_dao.dart'; import 'package:satu/core/models/flow/sell_request.dart'; import 'package:satu/core/models/flow/sell_response.dart'; import 'package:satu/core/models/flow/transaction_state.dart'; +import 'package:satu/core/redux/actions/sell_actions.dart'; import 'package:satu/core/redux/state/sell_state.dart'; import 'package:satu/core/redux/store.dart'; import 'package:satu/core/utils/locator.dart'; @@ -23,40 +25,73 @@ class DataService extends BaseService { final DialogService _dialogService = locator(); - - - Future sellBtnHandler({double card = 0, double nal = 0}) async { + Future sellBtnHandler( + {double card = 0, double nal = 0, double total = 0}) async { final SellRequest request = SellRequest(); final SellState sellState = Redux.store!.state.sellState!; final TransactionState transactionState = sellState.transactionState!; final List items = sellState.items!; - for(final ProductDao item in items){ + for (final ProductDao item in items) { request.items.add(_productToItemBean(item)); } request.card = card; request.nal = nal; request.invoiceId = transactionState.uuid; - request.section = 'section'; - request.contragent = 'contragent'; - final OperatorBean operator = OperatorBean()..name='operator'..code=1; + request.section = transactionState.sectionName; + request.contragent = transactionState.contragentName; + final OperatorBean operator = OperatorBean() + ..name = 'operator' + ..code = 1; request.operator = operator; - SellResponse response = await _api.sell(request); - final String msg = '${response.operation} - ${response.message}'; - log.w(response.toJson()); - _dialogService.showDialog(description: msg); + final SellResponse response = await _api.sell(request); + if (response.operation == false) { + _dialogService.showDialog(description: response.message); + return false; + } + await _updateTransaction( + transactionState: transactionState, + total: total, + card: card, + nal: nal, + sellResponse: response); + await Redux.store!.dispatch(loadSellData); return true; } + Future _updateTransaction( + {required TransactionState transactionState, + required double card, + required double nal, + required double total, + SellResponse? sellResponse}) async { + final Map? map = await _db.queryById( + transactionTableName, transactionState.uuid, + idColumnName: transactionColumnUuid); + + final DateTime now = DateTime.now(); + final Transaction transaction = Transaction.fromMap(map!); + final TransactionData data = + TransactionData.fromMap(jsonDecode(transaction.data!)); + transaction.status = transactionStatusFinish; + transaction.updatedAt = now.toIso8601String(); + data.sellResponse = sellResponse; + data.contragentName = transactionState.contragentName ?? ''; + data.card = card; + data.nal = nal; + data.total = total; + transaction.data = jsonEncode(data.toMap()); + await _db.update(transactionTableName, transaction.toMap()); + } ItemBean _productToItemBean(ProductDao product) { final ItemBean item = ItemBean() ..id = product.id - ..price = product.price - ..cnt = product.count - ..name = product.productName - ..excise = product.excise - ..articul = product.article?.toString(); + ..price = product.price + ..cnt = product.count + ..name = product.productName + ..excise = product.excise + ..articul = product.article?.toString(); return item; } } diff --git a/lib/core/services/db_service.dart b/lib/core/services/db_service.dart index 907a5ea..0c6c4f3 100644 --- a/lib/core/services/db_service.dart +++ b/lib/core/services/db_service.dart @@ -82,7 +82,8 @@ class DbService extends BaseService { $transactionColumnAppCompanyId integer not null, $transactionColumnStatus integer not null, $transactionColumnData text, - $transactionColumnCreatedAt text not null + $transactionColumnCreatedAt text not null, + $transactionColumnUpdatedAt text not null ); '''); await db.execute(''' @@ -139,7 +140,7 @@ class DbService extends BaseService { return result; } - Future?> queryById(String table, int id, + Future?> queryById(String table, dynamic id, {String? idColumnName}) async { final Database db = await instance.database; final List> result = await db diff --git a/lib/views/work/tabs/component/option_pill.dart b/lib/views/work/tabs/component/option_pill.dart deleted file mode 100644 index 5649a44..0000000 --- a/lib/views/work/tabs/component/option_pill.dart +++ /dev/null @@ -1,31 +0,0 @@ -import 'package:flutter/material.dart'; - - -class OptionPill extends StatelessWidget { - - final String text; - final bool selected; - - OptionPill({required this.text, required this.selected}); - - @override - Widget build(BuildContext context) { - return Container( - padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16), - decoration: BoxDecoration( - borderRadius: BorderRadius.all( - Radius.circular(10), - ), - color: selected ? Colors.black : Colors.transparent, - ), - child: Text( - text, - style: TextStyle( - color: selected ? Colors.white : Colors.grey[400], - fontWeight: FontWeight.bold, - fontSize: 14, - ), - ), - ); - } -} \ No newline at end of file diff --git a/lib/views/work/tabs/journal_view.dart b/lib/views/work/tabs/journal_view.dart index 642ea9d..8a3d41b 100644 --- a/lib/views/work/tabs/journal_view.dart +++ b/lib/views/work/tabs/journal_view.dart @@ -1,47 +1,67 @@ import 'package:flutter/material.dart'; import 'package:satu/widgets/bar/products_app_bar.dart'; - -import 'component/custom_field.dart'; -import 'component/option_pill.dart'; +import 'package:satu/widgets/buttons/option_pill.dart'; import 'component/transaction_item.dart'; -class JournalView extends StatelessWidget { +class JournalView extends StatefulWidget { + @override + _JournalViewState createState() => _JournalViewState(); +} + +class _JournalViewState extends State { + + int tabIndex = 0; + + @override Widget build(BuildContext context) { return Scaffold( appBar: ProductsAppBar(title: 'Журнал транзакции',), body: Padding( - padding: EdgeInsets.symmetric(horizontal: 24), + padding: const EdgeInsets.symmetric(horizontal: 24), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - // CustomField( - // hintText: "Name, Email or Mobile number", - // iconData: Icons.search, - // ), + Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ + OptionPill( + text: 'Все', + selected: tabIndex == 0, + onTap: () { + setState(() { + tabIndex = 0; + }); + }, + ), - OptionPill( - text: "Все", - selected: true, - ), + OptionPill( + text: 'Приход', + selected: tabIndex == 1, + onTap: () { + setState(() { + tabIndex = 1; + }); + }, + ), - OptionPill( - text: "Приход", - selected: false, - ), + OptionPill( + text: 'Расход', + selected: tabIndex == 2, + onTap: () { + setState(() { + tabIndex = 2; + }); + }, + ), - OptionPill( - text: "Расход", - selected: false, - ), - - ], + ], + ), ), SizedBox( @@ -60,7 +80,7 @@ class JournalView extends StatelessWidget { ), Text( - "03/06/2020", + '03/06/2020', style: TextStyle( //color: kGreyColor, fontSize: 14, @@ -73,16 +93,16 @@ class JournalView extends StatelessWidget { ), TransactionItem( - fullName: "Чек № 52021 13:03:05", - status: "Частное лицо", - amount: "2706.00", + fullName: 'Чек № 52021 13:03:05', + status: 'Частное лицо', + amount: '2706.00', received: true, ), TransactionItem( - fullName: "Чек № 52020 13:01:05", - status: "ИП Иванов В.И.", - amount: "19000.63", + fullName: 'Чек № 52020 13:01:05', + status: 'ИП Иванов В.И.', + amount: '19000.63', received: false, ), @@ -101,7 +121,7 @@ class JournalView extends StatelessWidget { ), Text( - "02/06/2020", + '02/06/2020', style: TextStyle( //color: kGreyColor, fontSize: 14, @@ -114,16 +134,16 @@ class JournalView extends StatelessWidget { ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "114.00", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '114.00', received: true, ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "70.16", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '70.16', received: true, ), @@ -132,7 +152,7 @@ class JournalView extends StatelessWidget { ), Text( - "29/05/2020", + '29/05/2020', style: TextStyle( //color: kGreyColor, fontSize: 14, @@ -145,44 +165,44 @@ class JournalView extends StatelessWidget { ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "44.50", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '44.50', received: true, ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "ТОО Рога и копыта", - amount: "85.50", + fullName: 'Чек № 5019 13:03:05', + status: 'ТОО Рога и копыта', + amount: '85.50', received: false, ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "155.00", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '155.00', received: true, ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "23.50", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '23.50', received: true, ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "11.50", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '11.50', received: true, ), TransactionItem( - fullName: "Чек № 5019 13:03:05", - status: "Частное лицо", - amount: "36.00", + fullName: 'Чек № 5019 13:03:05', + status: 'Частное лицо', + amount: '36.00', received: true, ), diff --git a/lib/views/work/views/payment/payment_view.dart b/lib/views/work/views/payment/payment_view.dart index 846da95..ee24b54 100644 --- a/lib/views/work/views/payment/payment_view.dart +++ b/lib/views/work/views/payment/payment_view.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_redux/flutter_redux.dart'; import 'package:satu/core/redux/state/sell_state.dart'; import 'package:satu/core/redux/store.dart'; +import 'package:satu/core/services/navigator_service.dart'; import 'package:satu/core/utils/locator.dart'; import 'package:satu/core/utils/utils_parse.dart'; import 'package:satu/shared/app_colors.dart'; @@ -23,9 +24,10 @@ class PaymentView extends StatefulWidget { } class _PaymentViewState extends State { - DataService _dataService = locator(); + final DataService _dataService = locator(); + final NavigatorService _navigatorService = locator(); - bool combine = true; + bool combine = false; late double _sum; double _bankSum = 0; double _cashSum = 0; @@ -133,18 +135,7 @@ class _PaymentViewState extends State { padding: const EdgeInsets.symmetric(horizontal: 45, vertical: 30), child: BusyButton( - title: 'ОПЛАТА', - busy: loading, - onPressed: () { - setState(() { - loading = true; - }); - _dataService.sellBtnHandler( - card: _bankSum, nal: _cashSum); - setState(() { - loading = false; - }); - }), + title: 'ОПЛАТА', busy: loading, onPressed: _payment), ), ], ), @@ -152,6 +143,34 @@ class _PaymentViewState extends State { }); } + Future _payment() async { + setState(() { + loading = true; + }); + + double card = 0; + double nal = 0; + if (combine == false) { + if (isCard) { + card = _sum; + } else { + nal = _sum; + } + } else { + card = _bankSum; + nal = _cashSum; + } + + bool result = + await _dataService.sellBtnHandler(card: card, nal: nal, total: _sum); + if(result) { + _navigatorService.pop(); + } + setState(() { + loading = false; + }); + } + Column buildPaymentSelect() { if (combine) { return Column( diff --git a/lib/widgets/buttons/option_pill.dart b/lib/widgets/buttons/option_pill.dart new file mode 100644 index 0000000..81c14d2 --- /dev/null +++ b/lib/widgets/buttons/option_pill.dart @@ -0,0 +1,52 @@ +import 'package:flutter/material.dart'; +import 'package:satu/shared/app_colors.dart'; +import 'package:satu/shared/shared_styles.dart'; + + +class OptionPill extends StatelessWidget { + + const OptionPill({required this.text, required this.selected, this.onTap}); + + final String text; + final bool selected; + final Function()? onTap; + + @override + Widget build(BuildContext context) { + return Container( + decoration: const BoxDecoration( + borderRadius: BorderRadius.all( + Radius.circular(5), + ), + ), + child: Material( + color: Colors.transparent, + child: InkWell( + onTap: onTap, + child: Container( + constraints: const BoxConstraints( + minWidth: 100.0 + ), + padding: const EdgeInsets.symmetric(vertical: 7, horizontal: 16), + decoration: BoxDecoration( + gradient: !selected ? null : primaryGradient, + borderRadius: const BorderRadius.all( + Radius.circular(5), + ), + color: selected ? Colors.black : Colors.transparent, + ), + child: Text( + text, + textAlign: TextAlign.center, + style: TextStyle( + color: selected ? blackColor : placeholderColor, + fontWeight: FontWeight.w500, + fontSize: 14, + ), + ), + ), + ), + ), + ); + } +} \ No newline at end of file