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';
class CheckBean {
List<CheckRowsBean> rows =[];
List<CheckRowBean> rows =[];
String? qr;
String? link;
static CheckBean? fromMap(dynamic map) {
if (map == null) return null;
final CheckBean checkBean = CheckBean();
if(map['rows'] != null) {
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.link = cast<String>(map['link']);
return checkBean;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -22,6 +22,7 @@ import '../store.dart';
@immutable
class SetSellStateAction {
const SetSellStateAction(this.sellState);
final SellState sellState;
}
@ -47,7 +48,7 @@ ThunkAction<AppState> counterOrEditSellItem(
item.count = counter;
} else {
final String val = ((item.count ?? 0) + counter).toStringAsFixed(5);
item.count = num.parse(val);
item.count = double.parse(val);
}
transactionRec.data = jsonEncode(item.toMap());
_dbService.update(transactionRecTableName, transactionRec.toMap());
@ -119,7 +120,7 @@ ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
if (item == null) {
item = ProductDao()
..id = good.id
..price = good.price
..price = good.price!.toDouble()
..article = good.articul
..count = 1.0
..eanCode = good.ean
@ -200,9 +201,11 @@ Future<void> loadSellData(Store<AppState> store) async {
orderBy: '$transactionColumnCreatedAt desc');
final List<ProductDao> list = [];
String? uuid;
int? transactionId;
if (set.isNotEmpty) {
final Transaction transaction = Transaction.fromMap(set.first);
uuid = transaction.uuid;
transactionId = transaction.id;
final List<Map<String, dynamic>> recs =
await _dbService.queryRowsWithWhere(transactionRecTableName,
'$transactionRecTransactionIdRef = ? ', [transaction.id],
@ -215,7 +218,10 @@ Future<void> loadSellData(Store<AppState> store) async {
}
}
store.dispatch(SetSellStateAction(SellState(
items: list, transactionState: TransactionState()..uuid = uuid)));
items: list,
transactionState: TransactionState()
..uuid = uuid
..transactionId = transactionId)));
} catch (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:http/http.dart' as http;
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';
/// The service responsible for networking requests
@ -16,10 +18,11 @@ class ApiService extends BaseService {
//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>{
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.cacheControlHeader: "no-cache"
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.cacheControlHeader: 'no-cache'
};
if (header != null && header.isNotEmpty) {
headers.addAll(header);
@ -29,10 +32,11 @@ class ApiService extends BaseService {
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>{
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.cacheControlHeader: "no-cache"
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.cacheControlHeader: 'no-cache'
};
if (header != null && header.isNotEmpty) {
headers.addAll(header);
@ -41,19 +45,23 @@ class ApiService extends BaseService {
if (requestBody != null) {
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;
}
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;
try {
final String response = await _post('/login', requestBody: requestBody);
result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) {
log.e("login", e, stack);
log.e('login', e, stack);
result = AuthResponse()
..message = 'Ошибка вызова сервера'
..operation = false;
@ -65,10 +73,11 @@ class ApiService extends BaseService {
final Map<String, String> requestBody = <String, String>{'token': token};
AuthResponse result;
try {
final String response = await _post('/authorization', requestBody: requestBody);
final String response =
await _post('/authorization', requestBody: requestBody);
result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) {
log.e("authorization", e, stack);
log.e('authorization', e, stack);
result = AuthResponse()
..message = 'Ошибка вызова сервера'
..operation = false;
@ -77,13 +86,15 @@ class ApiService extends BaseService {
}
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;
try {
final String response = await _post('/auth', header: headers);
result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) {
log.e("auth", e, stack);
log.e('auth', e, stack);
result = AuthResponse()
..message = 'Ошибка вызова сервера'
..operation = false;
@ -92,13 +103,15 @@ class ApiService extends BaseService {
}
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;
try {
final String response = await _post('/logout', header: headers);
result = AuthResponse.fromMap(json.decode(response));
} catch (e, stack) {
log.e("auth", e, stack);
log.e('auth', e, stack);
result = AuthResponse()
..message = 'Ошибка вызова сервера'
..operation = false;
@ -109,13 +122,34 @@ class ApiService extends BaseService {
Future<Response> dictionaries(String target) async {
Response result;
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);
result = Response.fromMapList(json.decode(response), null);
} catch (e, stack) {
log.e("dictionaries", e, stack);
result = Response()..operation=false..list=[];
log.e('dictionaries', e, stack);
result = Response()
..operation = false
..list = [];
}
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: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 'api_service.dart';
import 'db_service.dart';
import 'dialog_service.dart';
class DataService extends BaseService {
final ApiService _api = locator<ApiService>();
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;
}
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: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/utils_parse.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/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_header_bar.dart';
@ -22,12 +23,16 @@ class PaymentView extends StatefulWidget {
}
class _PaymentViewState extends State<PaymentView> {
DataService _dataService = locator<DataService>();
bool combine = true;
late double _sum;
double _bankSum = 0;
double _cashSum = 0;
bool isCard = false;
bool loading = false;
late TextEditingController _bankSumCtrl;
late TextEditingController _cashSumCtrl;
@ -127,7 +132,19 @@ class _PaymentViewState extends State<PaymentView> {
Padding(
padding:
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;
});
}),
),
],
),