197 lines
6.7 KiB
Dart
197 lines
6.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aman_kassa_flutter/core/base/base_service.dart';
|
|
import 'package:aman_kassa_flutter/core/entity/Category.dart';
|
|
import 'package:aman_kassa_flutter/core/entity/Goods.dart';
|
|
import 'package:aman_kassa_flutter/core/entity/Service.dart';
|
|
import 'package:aman_kassa_flutter/core/locator.dart';
|
|
import 'package:aman_kassa_flutter/core/models/calc_model.dart';
|
|
import 'package:aman_kassa_flutter/core/models/check_data.dart';
|
|
import 'package:aman_kassa_flutter/core/models/check_item.dart';
|
|
import 'package:aman_kassa_flutter/core/models/product_dao.dart';
|
|
import 'package:aman_kassa_flutter/core/models/response.dart';
|
|
import 'package:aman_kassa_flutter/core/models/user.dart';
|
|
import 'package:aman_kassa_flutter/core/services/DbService.dart';
|
|
import 'package:aman_kassa_flutter/redux/constants/operation_const.dart';
|
|
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
|
import 'package:aman_kassa_flutter/redux/store.dart';
|
|
|
|
import 'ApiService.dart';
|
|
|
|
class DataService extends BaseService {
|
|
final ApiService _api = locator<ApiService>();
|
|
final DbService _db = locator<DbService>();
|
|
|
|
Future<List<Category>> getCategoriesByParentId({int parentId}) async {
|
|
List<Map<String, dynamic>> list = await _db.queryRowsWithWhere(
|
|
Category_tableName, '$Category_columnParentIn = ?', [parentId ?? 0]);
|
|
return list.map((e) => Category.fromMap(e)).toList();
|
|
}
|
|
|
|
Future<List<Service>> getServices() async {
|
|
List<Map<String, dynamic>> list = await _db.queryAllRows(Service_tableName);
|
|
return list.map((e) => Service.fromMap(e)).toList();
|
|
}
|
|
|
|
Future<List<Good>> getGoodsByCategoryId({int categoryId}) async {
|
|
List<Map<String, dynamic>> list = await _db.queryRowsWithWhere(
|
|
Goog_tableName, '$Goog_columnCategoryId = ?', [categoryId ?? 0]);
|
|
return list.map((e) => Good.fromMap(e)).toList();
|
|
}
|
|
|
|
CheckData _transformProductsToCheckData(
|
|
{String paymentType, String tradeType, List<ProductDao> items}) {
|
|
List<CheckItem> itemsList = [];
|
|
int iterator = 1;
|
|
num summ = 0.0;
|
|
items.forEach((el) {
|
|
int articul = iterator;
|
|
if (el.service != null) {
|
|
articul = el.service.articul;
|
|
} else if (el.good != null) {
|
|
articul = el.good.articul;
|
|
}
|
|
itemsList.add(CheckItem(
|
|
name: el.name ?? 'Позиция №$iterator',
|
|
cnt: el.count,
|
|
price: el.price,
|
|
articul: articul));
|
|
summ += el.total;
|
|
iterator++;
|
|
});
|
|
CheckData checkData = CheckData(type: tradeType, items: itemsList);
|
|
if ((paymentType ?? 'cash') == 'card') {
|
|
checkData.card = summ;
|
|
}
|
|
print(checkData);
|
|
return checkData;
|
|
}
|
|
|
|
CheckData _transformCalcModelToCheckData(
|
|
{String paymentType, String tradeType, List<CalcModel> items}) {
|
|
List<CheckItem> itemsList = [];
|
|
int iterator = 1;
|
|
num summ = 0.0;
|
|
items.forEach((el) {
|
|
int articul = iterator;
|
|
CheckItem item = CheckItem(
|
|
name: 'Позиция $iterator',
|
|
cnt: el.num2 != null ? double.parse(el.num2) : 1.0,
|
|
price: double.parse(el.num1) ,
|
|
articul: articul);
|
|
|
|
summ += item.cnt * item.price;
|
|
itemsList.add(item);
|
|
iterator++;
|
|
});
|
|
CheckData checkData = CheckData(type: tradeType, items: itemsList);
|
|
if ((paymentType ?? 'cash') == 'card') {
|
|
checkData.card = summ;
|
|
}
|
|
print(checkData);
|
|
return checkData;
|
|
}
|
|
|
|
Future<Response<dynamic>> sellOrReturn(
|
|
{String paymentType,
|
|
String tradeType,
|
|
String token,
|
|
List<ProductDao> kassaItems,
|
|
List<CalcModel> calcItems,
|
|
String operationType,
|
|
String mode}) async {
|
|
try {
|
|
String data;
|
|
if(mode == SettingModeKassa) {
|
|
CheckData checkData = _transformProductsToCheckData(
|
|
paymentType: paymentType, tradeType: tradeType, items: kassaItems);
|
|
data = jsonEncode(checkData.toJson());
|
|
} else if(mode == SettingModeCalc) {
|
|
CheckData checkData = _transformCalcModelToCheckData(
|
|
paymentType: paymentType, tradeType: tradeType, items: calcItems);
|
|
data = jsonEncode(checkData.toJson());
|
|
}
|
|
|
|
|
|
log.i('token: $token');
|
|
log.i('data: $data');
|
|
Response<dynamic> response = await (operationType == OperationTypePay
|
|
? _api.sell(token, data)
|
|
: _api.sellReturn(token, data));
|
|
log.i('response status: ${response.status}');
|
|
log.i('response operation: ${response.operation}');
|
|
if (response.status == 200 && response.operation == true) {
|
|
log.i(
|
|
'save to db appCompanyId: ${Redux.store.state.userState.user.appCompanyId}, kassaId: ${Redux.store.state.userState.user.kassaId}');
|
|
/* save data to db
|
|
* data,
|
|
* dateTime,
|
|
* check,
|
|
* appCompanyId,
|
|
* kassaId,
|
|
* total,
|
|
* name,
|
|
* type
|
|
*/
|
|
}
|
|
return response;
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> checkDbFill(User user) async {
|
|
int serviceCount = await _db.queryRowCount(Service_tableName);
|
|
if( serviceCount ==0) {
|
|
int goodCount = await _db.queryRowCount(Goog_tableName);
|
|
if(goodCount == 0){
|
|
await getDataFromServer(user);
|
|
} else {
|
|
log.i('$Goog_tableName is Fill');
|
|
}
|
|
} else {
|
|
log.i('$Service_tableName is Fill');
|
|
}
|
|
}
|
|
|
|
Future<bool> getDataFromServer(User user) async {
|
|
log.i('Get Data from server');
|
|
try {
|
|
String token = user.token;
|
|
Response<dynamic> goods = await _api.getGoodsFromServer(token);
|
|
Response<dynamic> categories = await _api.getCategoryFromServer(token);
|
|
Response<dynamic> services = await _api.getServiceFromServer(token);
|
|
await _db.deleteAll(Goog_tableName);
|
|
await _db.deleteAll(Category_tableName);
|
|
await _db.deleteAll(Service_tableName);
|
|
log.i('All tables cleaned');
|
|
for (var key in goods.body.keys) {
|
|
print(goods.body[key]);
|
|
Good row = Good.fromJson(goods.body[key]);
|
|
await _db.insert(Goog_tableName, row.toMap());
|
|
}
|
|
log.i('Inserted ${goods.body.length} to table $Goog_tableName');
|
|
|
|
for (var el in categories.body) {
|
|
print(el);
|
|
Category row = Category.fromJson(el);
|
|
await _db.insert(Category_tableName, row.toMap());
|
|
}
|
|
log.i('Inserted ${categories.body.length} to table $Category_tableName');
|
|
|
|
for (var key in services.body.keys) {
|
|
print(services.body[key]);
|
|
Service row = Service.fromJson(services.body[key]);
|
|
await _db.insert(Service_tableName, row.toMap());
|
|
}
|
|
log.i('Inserted ${services.body.length} to table $Service_tableName');
|
|
|
|
return true;
|
|
} catch (e) {
|
|
print(e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|