payment/payment_view.dart

null-safety-migration
suvaissov 2021-09-06 14:20:22 +06:00
parent 4e63ed0556
commit 8b4379a495
11 changed files with 163 additions and 60 deletions

View File

@ -1,18 +1,20 @@
import 'package:satu/core/models/flow/rows_bean.dart'; import 'package:satu/core/models/flow/check_row_bean.dart';
import 'package:satu/core/utils/utils_parse.dart'; import 'package:satu/core/utils/utils_parse.dart';
class CheckBean { class CheckBean {
List<CheckRowsBean> rows =[]; List<CheckRowBean> rows =[];
String? qr; String? qr;
String? link; String? link;
static CheckBean? fromMap(dynamic map) { static CheckBean? fromMap(dynamic map) {
if (map == null) return null; if (map == null) return null;
final CheckBean checkBean = CheckBean(); final CheckBean checkBean = CheckBean();
if(map['rows'] != null) {
checkBean.rows.addAll( checkBean.rows.addAll(
(map['rows'] as List ?? []).map((o) => CheckRowsBean.fromMap(o)) (map['rows'] as List).map((o) => CheckRowBean.fromMap(o))
); );
}
checkBean.qr = cast<String>(map['qr']); checkBean.qr = cast<String>(map['qr']);
checkBean.link = cast<String>(map['link']); checkBean.link = cast<String>(map['link']);
return checkBean; return checkBean;

View File

@ -6,17 +6,17 @@ import 'package:satu/core/utils/utils_parse.dart';
/// name : "toptext" /// name : "toptext"
/// value : null /// value : null
class CheckRowsBean { class CheckRowBean {
late int size; late int size;
String? text; dynamic text;
late bool center; late bool center;
String? name; String? name;
String? value; String? value;
static CheckRowsBean fromMap(dynamic map) { static CheckRowBean fromMap(dynamic map) {
CheckRowsBean rowsBean = CheckRowsBean(); final CheckRowBean rowsBean = CheckRowBean();
rowsBean.size = cast<int>(map['size']) ?? 14; rowsBean.size = cast<int>(map['size']) ?? 14;
rowsBean.text = cast<String>(map['text']); rowsBean.text = cast<dynamic>(map['text']);
rowsBean.center = cast<bool>(map['center']) ?? false; rowsBean.center = cast<bool>(map['center']) ?? false;
rowsBean.name = cast<String>(map['name']); rowsBean.name = cast<String>(map['name']);
rowsBean.value = cast<String>(map['value']); rowsBean.value = cast<String>(map['value']);

View File

@ -8,7 +8,7 @@ import 'package:satu/core/utils/utils_parse.dart';
/// price : 200 /// price : 200
/// excise : "000000000000_gs1_excise_code" /// excise : "000000000000_gs1_excise_code"
class ItemsBean { class ItemBean {
int? id; int? id;
String? name; String? name;
String? articul; String? articul;
@ -16,8 +16,8 @@ class ItemsBean {
double? price; double? price;
String? excise; String? excise;
static ItemsBean fromMap(dynamic map) { static ItemBean fromMap(dynamic map) {
final ItemsBean itemsBean = ItemsBean(); final ItemBean itemsBean = ItemBean();
itemsBean.id = cast<int>(map['id']); itemsBean.id = cast<int>(map['id']);
itemsBean.name = cast<String>(map['name']); itemsBean.name = cast<String>(map['name']);
itemsBean.articul = cast<String>(map['articul']); itemsBean.articul = cast<String>(map['articul']);

View File

@ -6,8 +6,8 @@ class ProductDao {
ProductDao.fromMap(dynamic map) { ProductDao.fromMap(dynamic map) {
id = map['id'] as int; id = map['id'] as int;
categoryId = map['categoryId'] as int; categoryId = map['categoryId'] as int;
count = map['count'] as num; count = map['count'] as double;
price = map['price'] as num; price = map['price'] as double;
productName = cast<String>(map['productName']) ?? ''; productName = cast<String>(map['productName']) ?? '';
categoryName = cast<String>(map['categoryName']); categoryName = cast<String>(map['categoryName']);
eanCode = cast<String>(map['eanCode']); eanCode = cast<String>(map['eanCode']);
@ -18,8 +18,8 @@ class ProductDao {
int? id; int? id;
int? categoryId; int? categoryId;
num? count; double? count;
num? price; double? price;
String productName = ''; String productName = '';
String? categoryName; String? categoryName;
String? eanCode; String? eanCode;

View File

@ -5,10 +5,10 @@ import 'items_bean.dart';
import 'operator_bean.dart'; import 'operator_bean.dart';
class SellRequest { class SellRequest {
String? type; String type = 'g';
List<ItemsBean> items = []; List<ItemBean> items = [];
int card = 0; double card = 0;
int nal = 0; double nal = 0;
String? invoiceId; String? invoiceId;
String? section; String? section;
OperatorBean? operator; OperatorBean? operator;
@ -16,12 +16,14 @@ class SellRequest {
static SellRequest fromMap(dynamic map) { static SellRequest fromMap(dynamic map) {
final SellRequest sellRequestBean = SellRequest(); final SellRequest sellRequestBean = SellRequest();
sellRequestBean.type = cast<String>(map['type']); sellRequestBean.type = cast<String>(map['type']) ?? 'g';
if(map['items'] !=null) {
sellRequestBean.items.addAll( sellRequestBean.items.addAll(
(map['items'] as List ?? []).map((o) => ItemsBean.fromMap(o)) (map['items'] as List).map((o) => ItemBean.fromMap(o))
); );
sellRequestBean.card = cast<int>(map['card']) ?? 0; }
sellRequestBean.nal = cast<int>(map['nal']) ?? 0; sellRequestBean.card = cast<double>(map['card']) ?? 0;
sellRequestBean.nal = cast<double>(map['nal']) ?? 0;
sellRequestBean.invoiceId = cast<String>(map['invoice_id']); sellRequestBean.invoiceId = cast<String>(map['invoice_id']);
sellRequestBean.section = cast<String>(map['section']); sellRequestBean.section = cast<String>(map['section']);
sellRequestBean.operator = OperatorBean.fromMap(map['operator']); sellRequestBean.operator = OperatorBean.fromMap(map['operator']);
@ -29,7 +31,7 @@ class SellRequest {
return sellRequestBean; return sellRequestBean;
} }
Map toJson() => { Map<String, dynamic> toJson() => {
'type': type, 'type': type,
'items': items, 'items': items,
'card': card, 'card': card,

View File

@ -12,8 +12,7 @@ class SellResponse {
String? message; String? message;
late bool operation; late bool operation;
static SellResponse? fromMap(dynamic map) { static SellResponse fromMap(dynamic map) {
if (map == null) return null;
final SellResponse sellResponseBean = SellResponse(); final SellResponse sellResponseBean = SellResponse();
sellResponseBean.journalId = cast<int>(map['journal_id']); sellResponseBean.journalId = cast<int>(map['journal_id']);
sellResponseBean.check = CheckBean.fromMap(map['check']); sellResponseBean.check = CheckBean.fromMap(map['check']);
@ -25,7 +24,7 @@ class SellResponse {
return sellResponseBean; return sellResponseBean;
} }
Map toJson() => { Map<String, dynamic> toJson() => {
'journal_id': journalId, 'journal_id': journalId,
'check': check, 'check': check,
'check_png': checkPng, 'check_png': checkPng,

View File

@ -1,3 +1,4 @@
class TransactionState { class TransactionState {
String? uuid; String? uuid;
int? transactionId;
} }

View File

@ -22,6 +22,7 @@ import '../store.dart';
@immutable @immutable
class SetSellStateAction { class SetSellStateAction {
const SetSellStateAction(this.sellState); const SetSellStateAction(this.sellState);
final SellState sellState; final SellState sellState;
} }
@ -47,7 +48,7 @@ ThunkAction<AppState> counterOrEditSellItem(
item.count = counter; item.count = counter;
} else { } else {
final String val = ((item.count ?? 0) + counter).toStringAsFixed(5); final String val = ((item.count ?? 0) + counter).toStringAsFixed(5);
item.count = num.parse(val); item.count = double.parse(val);
} }
transactionRec.data = jsonEncode(item.toMap()); transactionRec.data = jsonEncode(item.toMap());
_dbService.update(transactionRecTableName, transactionRec.toMap()); _dbService.update(transactionRecTableName, transactionRec.toMap());
@ -119,7 +120,7 @@ ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
if (item == null) { if (item == null) {
item = ProductDao() item = ProductDao()
..id = good.id ..id = good.id
..price = good.price ..price = good.price!.toDouble()
..article = good.articul ..article = good.articul
..count = 1.0 ..count = 1.0
..eanCode = good.ean ..eanCode = good.ean
@ -200,9 +201,11 @@ Future<void> loadSellData(Store<AppState> store) async {
orderBy: '$transactionColumnCreatedAt desc'); orderBy: '$transactionColumnCreatedAt desc');
final List<ProductDao> list = []; final List<ProductDao> list = [];
String? uuid; String? uuid;
int? transactionId;
if (set.isNotEmpty) { if (set.isNotEmpty) {
final Transaction transaction = Transaction.fromMap(set.first); final Transaction transaction = Transaction.fromMap(set.first);
uuid = transaction.uuid; uuid = transaction.uuid;
transactionId = transaction.id;
final List<Map<String, dynamic>> recs = final List<Map<String, dynamic>> recs =
await _dbService.queryRowsWithWhere(transactionRecTableName, await _dbService.queryRowsWithWhere(transactionRecTableName,
'$transactionRecTransactionIdRef = ? ', [transaction.id], '$transactionRecTransactionIdRef = ? ', [transaction.id],
@ -215,7 +218,10 @@ Future<void> loadSellData(Store<AppState> store) async {
} }
} }
store.dispatch(SetSellStateAction(SellState( store.dispatch(SetSellStateAction(SellState(
items: list, transactionState: TransactionState()..uuid = uuid))); items: list,
transactionState: TransactionState()
..uuid = uuid
..transactionId = transactionId)));
} catch (e, stack) { } catch (e, stack) {
log.e('loadSellData', e, stack); log.e('loadSellData', e, stack);
} }

View File

@ -4,6 +4,8 @@ import 'dart:io';
import 'package:satu/core/base/base_service.dart'; import 'package:satu/core/base/base_service.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:satu/core/models/auth/auth_response.dart'; import 'package:satu/core/models/auth/auth_response.dart';
import 'package:satu/core/models/flow/sell_request.dart';
import 'package:satu/core/models/flow/sell_response.dart';
import 'package:satu/core/models/response.dart'; import 'package:satu/core/models/response.dart';
/// The service responsible for networking requests /// The service responsible for networking requests
@ -16,10 +18,11 @@ class ApiService extends BaseService {
//TOKEN //TOKEN
String? token; String? token;
Future<String> _get(String point, {Map<String, String>? requestBody, Map<String, String>? header}) async { Future<String> _get(String point,
{Map<String, String>? requestBody, Map<String, String>? header}) async {
final Map<String, String> headers = <String, String>{ final Map<String, String> headers = <String, String>{
HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.cacheControlHeader: "no-cache" HttpHeaders.cacheControlHeader: 'no-cache'
}; };
if (header != null && header.isNotEmpty) { if (header != null && header.isNotEmpty) {
headers.addAll(header); headers.addAll(header);
@ -29,31 +32,36 @@ class ApiService extends BaseService {
return response.body; return response.body;
} }
Future<String> _post(String point, {Map<String, dynamic>? requestBody, Map<String, String>? header}) async { Future<String> _post(String point,
{Map<String, dynamic>? requestBody, Map<String, String>? header}) async {
final Map<String, String> headers = <String, String>{ final Map<String, String> headers = <String, String>{
HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.cacheControlHeader: "no-cache" HttpHeaders.cacheControlHeader: 'no-cache'
}; };
if (header != null && header.isNotEmpty) { if (header != null && header.isNotEmpty) {
headers.addAll(header); headers.addAll(header);
} }
final String url = '$endpoint$point'; final String url = '$endpoint$point';
if(requestBody!=null) { if (requestBody != null) {
log.i(jsonEncode(requestBody)); log.i(jsonEncode(requestBody));
} }
final response = await http.post(Uri.https(host, url), body: jsonEncode(requestBody), headers: headers); final response = await http.post(Uri.https(host, url),
body: jsonEncode(requestBody), headers: headers);
return response.body; return response.body;
} }
Future<AuthResponse> login(String username, String password) async { Future<AuthResponse> login(String username, String password) async {
final Map<String, String> requestBody = <String, String>{'username': username, 'password': password}; final Map<String, String> requestBody = <String, String>{
'username': username,
'password': password
};
AuthResponse result; AuthResponse result;
try { try {
final String response = await _post('/login', requestBody: requestBody); final String response = await _post('/login', requestBody: requestBody);
result = AuthResponse.fromMap(json.decode(response)); result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) { } catch (e, stack) {
log.e("login", e, stack); log.e('login', e, stack);
result = AuthResponse() result = AuthResponse()
..message = 'Ошибка вызова сервера' ..message = 'Ошибка вызова сервера'
..operation = false; ..operation = false;
@ -65,10 +73,11 @@ class ApiService extends BaseService {
final Map<String, String> requestBody = <String, String>{'token': token}; final Map<String, String> requestBody = <String, String>{'token': token};
AuthResponse result; AuthResponse result;
try { try {
final String response = await _post('/authorization', requestBody: requestBody); final String response =
await _post('/authorization', requestBody: requestBody);
result = AuthResponse.fromMap(json.decode(response)); result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) { } catch (e, stack) {
log.e("authorization", e, stack); log.e('authorization', e, stack);
result = AuthResponse() result = AuthResponse()
..message = 'Ошибка вызова сервера' ..message = 'Ошибка вызова сервера'
..operation = false; ..operation = false;
@ -77,13 +86,15 @@ class ApiService extends BaseService {
} }
Future<AuthResponse> auth(String token) async { Future<AuthResponse> auth(String token) async {
final Map<String, String> headers = <String, String>{HttpHeaders.authorizationHeader: 'Bearer $token'}; final Map<String, String> headers = <String, String>{
HttpHeaders.authorizationHeader: 'Bearer $token'
};
AuthResponse result; AuthResponse result;
try { try {
final String response = await _post('/auth', header: headers); final String response = await _post('/auth', header: headers);
result = AuthResponse.fromMap(json.decode(response)); result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) { } catch (e, stack) {
log.e("auth", e, stack); log.e('auth', e, stack);
result = AuthResponse() result = AuthResponse()
..message = 'Ошибка вызова сервера' ..message = 'Ошибка вызова сервера'
..operation = false; ..operation = false;
@ -92,13 +103,15 @@ class ApiService extends BaseService {
} }
Future<AuthResponse> logout() async { Future<AuthResponse> logout() async {
final Map<String, String> headers = <String, String>{HttpHeaders.authorizationHeader: 'Bearer $token'}; final Map<String, String> headers = <String, String>{
HttpHeaders.authorizationHeader: 'Bearer $token'
};
AuthResponse result; AuthResponse result;
try { try {
final String response = await _post('/logout', header: headers); final String response = await _post('/logout', header: headers);
result = AuthResponse.fromMap(json.decode(response)); result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) { } catch (e, stack) {
log.e("auth", e, stack); log.e('auth', e, stack);
result = AuthResponse() result = AuthResponse()
..message = 'Ошибка вызова сервера' ..message = 'Ошибка вызова сервера'
..operation = false; ..operation = false;
@ -109,13 +122,34 @@ class ApiService extends BaseService {
Future<Response> dictionaries(String target) async { Future<Response> dictionaries(String target) async {
Response result; Response result;
try { try {
final Map<String, String> headers = <String, String>{HttpHeaders.authorizationHeader: 'Bearer $token'}; final Map<String, String> headers = <String, String>{
HttpHeaders.authorizationHeader: 'Bearer $token'
};
final String response = await _post(target, header: headers); final String response = await _post(target, header: headers);
result = Response.fromMapList(json.decode(response), null); result = Response.fromMapList(json.decode(response), null);
} catch (e, stack) { } catch (e, stack) {
log.e("dictionaries", e, stack); log.e('dictionaries', e, stack);
result = Response()..operation=false..list=[]; result = Response()
..operation = false
..list = [];
} }
return result; return result;
} }
Future<SellResponse> sell(SellRequest request) async {
SellResponse response;
try {
final Map<String, String> headers = <String, String>{
HttpHeaders.authorizationHeader: 'Bearer $token'
};
final String responseBody = await _post('/sell', header: headers, requestBody: request.toJson());
response = SellResponse.fromMap(json.decode(responseBody));
} catch (e, stack) {
log.e('dictionaries', e, stack);
response = SellResponse()
..operation = false
..message = e.toString();
}
return response;
}
} }

View File

@ -2,19 +2,61 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:satu/core/base/base_service.dart'; import 'package:satu/core/base/base_service.dart';
import 'package:satu/core/entity/transaction_entity.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/state/sell_state.dart';
import 'package:satu/core/redux/store.dart';
import 'package:satu/core/utils/locator.dart'; import 'package:satu/core/utils/locator.dart';
import 'api_service.dart'; import 'api_service.dart';
import 'db_service.dart'; import 'db_service.dart';
import 'dialog_service.dart';
class DataService extends BaseService { class DataService extends BaseService {
final ApiService _api = locator<ApiService>(); final ApiService _api = locator<ApiService>();
final DbService _db = locator<DbService>(); final DbService _db = locator<DbService>();
final DialogService _dialogService = locator<DialogService>();
Future<bool> sellBtnHandler() async {
Future<bool> sellBtnHandler({double card = 0, double nal = 0}) async {
final SellRequest request = SellRequest();
final SellState sellState = Redux.store!.state.sellState!;
final TransactionState transactionState = sellState.transactionState!;
final List<ProductDao> items = sellState.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.operator = operator;
SellResponse response = await _api.sell(request);
final String msg = '${response.operation} - ${response.message}';
log.w(response.toJson());
_dialogService.showDialog(description: msg);
return true; return true;
} }
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();
return item;
}
} }

View File

@ -3,9 +3,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart'; import 'package:flutter_redux/flutter_redux.dart';
import 'package:satu/core/redux/state/sell_state.dart'; import 'package:satu/core/redux/state/sell_state.dart';
import 'package:satu/core/redux/store.dart'; import 'package:satu/core/redux/store.dart';
import 'package:satu/core/utils/locator.dart';
import 'package:satu/core/utils/utils_parse.dart'; import 'package:satu/core/utils/utils_parse.dart';
import 'package:satu/shared/app_colors.dart'; import 'package:satu/shared/app_colors.dart';
import 'package:satu/views/dictionaries/category/category_view.dart'; import 'package:satu/core/services/data_service.dart';
import 'package:satu/views/work/views/payment/component/combine_dock.dart'; import 'package:satu/views/work/views/payment/component/combine_dock.dart';
import 'package:satu/widgets/bar/products_app_bar.dart'; import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_header_bar.dart'; import 'package:satu/widgets/bar/products_header_bar.dart';
@ -22,12 +23,16 @@ class PaymentView extends StatefulWidget {
} }
class _PaymentViewState extends State<PaymentView> { class _PaymentViewState extends State<PaymentView> {
DataService _dataService = locator<DataService>();
bool combine = true; bool combine = true;
late double _sum; late double _sum;
double _bankSum = 0; double _bankSum = 0;
double _cashSum = 0; double _cashSum = 0;
bool isCard = false; bool isCard = false;
bool loading = false;
late TextEditingController _bankSumCtrl; late TextEditingController _bankSumCtrl;
late TextEditingController _cashSumCtrl; late TextEditingController _cashSumCtrl;
@ -42,24 +47,24 @@ class _PaymentViewState extends State<PaymentView> {
_bankSumCtrl = TextEditingController(text: formatDecimal(_bankSum)); _bankSumCtrl = TextEditingController(text: formatDecimal(_bankSum));
_cashSumCtrl = TextEditingController(text: formatDecimal(_cashSum)); _cashSumCtrl = TextEditingController(text: formatDecimal(_cashSum));
_bankSumCtrl.addListener(() { _bankSumCtrl.addListener(() {
if( _bankSumCtrl.text.isNotEmpty) { if (_bankSumCtrl.text.isNotEmpty) {
setState(() { setState(() {
_bankSum = parseNumeric(_bankSumCtrl.text); _bankSum = parseNumeric(_bankSumCtrl.text);
}); });
} else { } else {
setState(() { setState(() {
_bankSum = 0 ; _bankSum = 0;
}); });
} }
}); });
_cashSumCtrl.addListener(() { _cashSumCtrl.addListener(() {
if( _cashSumCtrl.text.isNotEmpty) { if (_cashSumCtrl.text.isNotEmpty) {
setState(() { setState(() {
_cashSum = parseNumeric(_cashSumCtrl.text); _cashSum = parseNumeric(_cashSumCtrl.text);
}); });
} else { } else {
setState(() { setState(() {
_cashSum = 0 ; _cashSum = 0;
}); });
} }
}); });
@ -127,7 +132,19 @@ class _PaymentViewState extends State<PaymentView> {
Padding( Padding(
padding: padding:
const EdgeInsets.symmetric(horizontal: 45, vertical: 30), const EdgeInsets.symmetric(horizontal: 45, vertical: 30),
child: BusyButton(title: 'ОПЛАТА', onPressed: () {}), child: BusyButton(
title: 'ОПЛАТА',
busy: loading,
onPressed: () {
setState(() {
loading = true;
});
_dataService.sellBtnHandler(
card: _bankSum, nal: _cashSum);
setState(() {
loading = false;
});
}),
), ),
], ],
), ),