172 lines
6.1 KiB
Dart
172 lines
6.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:logger/logger.dart';
|
|
import 'package:meta/meta.dart';
|
|
import 'package:redux/redux.dart';
|
|
import 'package:redux_thunk/redux_thunk.dart';
|
|
import 'package:satu/core/entity/Category.dart';
|
|
import 'package:satu/core/entity/Goods.dart';
|
|
import 'package:satu/core/entity/Transaction.dart';
|
|
import 'package:satu/core/models/flow/product_dao.dart';
|
|
import 'package:satu/core/models/flow/transaction_state.dart';
|
|
import 'package:satu/core/redux/state/sell_state.dart';
|
|
import 'package:satu/core/services/db_service.dart';
|
|
import 'package:satu/core/utils/locator.dart';
|
|
import 'package:satu/core/utils/logger.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import '../store.dart';
|
|
|
|
@immutable
|
|
class SetSellStateAction {
|
|
final SellState sellState;
|
|
|
|
SetSellStateAction(this.sellState);
|
|
}
|
|
|
|
final Logger log = getLogger('SetSellStateAction');
|
|
|
|
final DbService _dbService = locator<DbService>();
|
|
|
|
ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
|
|
return (Store<AppState> store) async {
|
|
log.i('addSellItem');
|
|
int? appCompanyId = store.state.userState!.auth!.companyId;
|
|
String? uuid = store.state.sellState!.transactionState!.uuid;
|
|
|
|
Transaction? transaction;
|
|
|
|
if (uuid != null ) {
|
|
List<Map<String, dynamic>> set = await _dbService.queryRowsWithWhere(
|
|
TransactionTableName,
|
|
'$TransactionColumnAppCompanyId = ? and $TransactionColumnStatus = ? and ${TransactionColumnType} = ?',
|
|
[appCompanyId, TransactionStatusPrepare, TransactionTypeSell],
|
|
orderBy: '$TransactionColumnCreatedAt desc');
|
|
if (set.isNotEmpty) {
|
|
for (Map<String, dynamic> map in set) {
|
|
Transaction _transaction = Transaction.fromMap(map);
|
|
ProductDao _product = ProductDao.fromMap(jsonDecode(_transaction.data!));
|
|
if (_product.id == good.id && _product.excise == excise) {
|
|
transaction = _transaction;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (transaction != null) {
|
|
ProductDao item = ProductDao.fromMap(jsonDecode(transaction.data!));
|
|
item..count = item.count! + 1;
|
|
transaction.data = jsonEncode(item.toMap());
|
|
transaction.createdAt = DateTime.now().toIso8601String();
|
|
_dbService.update(TransactionTableName, transaction.toMap());
|
|
} else {
|
|
ProductDao item = new ProductDao()
|
|
..id = good.id
|
|
..price = good.price
|
|
..article = good.articul
|
|
..count = 1.0
|
|
..eanCode = good.ean
|
|
..productName = good.name
|
|
..excise = excise;
|
|
//category add logic
|
|
if (good.categoryId != null) {
|
|
List<Map<String, dynamic>> set =
|
|
await _dbService.queryRowsWithWhere(CategoryTableName, 'id = ?', [good.categoryId]);
|
|
if (set.isNotEmpty) {
|
|
Category category = Category.fromMap(set.first);
|
|
item.categoryId = category.id;
|
|
item.categoryName = category.name;
|
|
}
|
|
}
|
|
|
|
if (uuid == null) {
|
|
var uuidTool = Uuid();
|
|
;
|
|
uuid = uuidTool.v4();
|
|
}
|
|
|
|
transaction = new Transaction()
|
|
..uuid = uuid
|
|
..status = TransactionStatusPrepare
|
|
..appCompanyId = appCompanyId
|
|
..type = TransactionTypeSell
|
|
..createdAt = DateTime.now().toIso8601String()
|
|
..data = jsonEncode(item.toMap());
|
|
await _dbService.insert(TransactionTableName, transaction.toMap());
|
|
}
|
|
// refresh from db ? after save data
|
|
await loadSellData(store);
|
|
};
|
|
}
|
|
|
|
ThunkAction<AppState> removeSellItem({required int transactionId}) {
|
|
return (Store<AppState> store) async {
|
|
|
|
int? appCompanyId = store.state.userState!.auth!.companyId;
|
|
String? uuid = store.state.sellState!.transactionState!.uuid;
|
|
|
|
int count = await _dbService.delete(TransactionTableName, transactionId);
|
|
log.i('removeSellItem ${count} by transactionId:${transactionId}');
|
|
// List<Map<String, dynamic>> set = await _dbService.queryRowsWithWhere(
|
|
// TransactionTableName,
|
|
// '$TransactionColumnId = ? ',
|
|
// [transactionId]);
|
|
// if (set.isNotEmpty) {
|
|
// for (Map<String, dynamic> map in set) {
|
|
// Transaction _transaction = Transaction.fromMap(map);
|
|
// ProductDao _product = ProductDao.fromMap(jsonDecode(_transaction.data));
|
|
// if (_product.id == good.id && _product.excise == excise) {
|
|
// transaction = _transaction;
|
|
// break;
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// refresh from db ? after save data
|
|
await loadSellData(store);
|
|
};
|
|
}
|
|
|
|
Future<void> removeAllSellData(Store<AppState> store) async {
|
|
try {
|
|
log.i('removeAllSellData');
|
|
int? appCompanyId = store.state.userState!.auth!.companyId;
|
|
String? uuid = store.state.sellState!.transactionState!.uuid;
|
|
await _dbService.deleteByWhere(
|
|
TransactionTableName,
|
|
'$TransactionColumnAppCompanyId = ? '
|
|
' and $TransactionColumnStatus = ? '
|
|
' and ${TransactionColumnType} = ?'
|
|
' and ${TransactionColumnUuid} = ?',
|
|
[appCompanyId, TransactionStatusPrepare, TransactionTypeSell, uuid]);
|
|
await loadSellData(store);
|
|
} catch (e, stack) {
|
|
log.e('removeAllSellData', e, stack);
|
|
}
|
|
}
|
|
|
|
Future<void> loadSellData(Store<AppState> store) async {
|
|
try {
|
|
log.i('loadSellData');
|
|
int? appCompanyId = store.state.userState!.auth!.companyId;
|
|
List<Map<String, dynamic>> set = await _dbService.queryRowsWithWhere(
|
|
TransactionTableName,
|
|
'$TransactionColumnAppCompanyId = ? and $TransactionColumnStatus = ? and ${TransactionColumnType} = ?',
|
|
[appCompanyId, TransactionStatusPrepare, TransactionTypeSell],
|
|
orderBy: '$TransactionColumnCreatedAt desc');
|
|
List<ProductDao> list = [];
|
|
String? uuid;
|
|
for (Map<String, dynamic> map in set) {
|
|
Transaction transaction = Transaction.fromMap(map);
|
|
uuid = transaction.uuid;
|
|
ProductDao productDao = ProductDao.fromMap(jsonDecode(transaction.data!));
|
|
productDao.transactionId = transaction.id;
|
|
list.add(productDao);
|
|
}
|
|
store.dispatch(SetSellStateAction(SellState(items: list, transactionState: TransactionState()..uuid = uuid)));
|
|
} catch (e, stack) {
|
|
log.e('loadSellData', e, stack);
|
|
}
|
|
}
|