import 'dart:convert'; import 'package:aman_kassa_flutter/core/base/base_service.dart'; import 'package:aman_kassa_flutter/core/locator.dart'; import 'file:///D:/Work/serik/Flutter/aman-kassa-flutter/lib/core/models/halyk_response_dao.dart'; import 'package:aman_kassa_flutter/core/models/close_day_data.dart'; import 'package:aman_kassa_flutter/core/models/halyk_post_session.dart'; import 'package:aman_kassa_flutter/core/models/transaction_item.dart'; import 'package:aman_kassa_flutter/core/services/ApiService.dart'; import 'package:flutter/services.dart'; import 'package:intl/intl.dart'; import '../models/aman_dao.dart'; class BankService extends BaseService { final ApiService _api = locator(); static const String _url = 'http://195.200.74.83:5000'; final MethodChannel _channel = MethodChannel('channel:com.amanKassa/bank'); Future version() async { String result; try { result = await _channel.invokeMethod('version'); } catch (e, stack) { log.e("BankService", e, stack); result = '0'; } log.i(result); return int.parse(result) ?? 0; } Future renewToken({String token, String login, String password}) async { HalykPosSession result; try { result = await _api.halykPosToken(token, login, password); } catch (e, stack) { log.e("BankService", e, stack); } return result; } Future closeDay({ String token}) async { try { String response = await _channel.invokeMethod("closeDay", {'token': token }); HalykResponse dao = HalykResponse.fromMap(json.decode(response)); return dao; } catch (e, stack) { log.e("BankService", e, stack); return new HalykResponse(result: ResultBean(description: 'Ошибка при закрытии дня', code: -1)); } } Future pay({double amount, String token}) async { try { double total = amount * 100; log.i('total: $total, ${total.toInt()}'); String response = await _channel.invokeMethod("pay", {'amount': total.toInt(), 'token': token }); HalykResponse dao = HalykResponse.fromMap(json.decode(response)); return dao; } catch (e, stack) { log.e("BankService", e, stack); return new HalykResponse(result: ResultBean(description: 'Ошибка оплаты', code: -1)); } } Future refund({double amount, String token, int terminalId, int operDay, int transNum }) async { try { String response = await _channel.invokeMethod("refund", { 'amount': amount.toInt(), 'token': token , 'terminalId': terminalId, 'operDay': operDay, 'transNum': transNum }); HalykResponse dao = HalykResponse.fromMap(json.decode(response)); return dao; } catch (e, stack) { log.e("BankService", e, stack); return new HalykResponse(result: ResultBean(description: 'Ошибка при возврате', code: -1)); } } CloseDayData closeDayDataConvert(dynamic rows) { final DateFormat formatter = DateFormat('dd.MM.yyyy'); final DateTime now = DateTime.now(); final String formatted = formatter.format(now); List items = (rows as List).map((e) => TransactionItem.fromJson(e)).toList(); num totalAmount = 0; int totalCount = 0; num paymentAmount = 0; int paymentCount = 0; num refundAmount = 0; int refundCount = 0; for(TransactionItem item in items) { if(item.transactionType == 'PAYMENT') { paymentCount++; paymentAmount += ( item.amount / 100 ); totalAmount += ( item.amount / 100 ); } else if(item.transactionType == 'REFUND') { refundCount++; refundAmount += ( item.amount / 100 ); totalAmount -= ( item.amount / 100 ); } totalCount++; } CloseDayData closeDayData = new CloseDayData( items: items, title: 'Отчет POS от $formatted', totalAmount: totalAmount, totalCount: totalCount, paymentAmount: paymentAmount, paymentCount: paymentCount, refundAmount: refundAmount, refundCount: refundCount, ); return closeDayData; } }