diff --git a/lib/core/entity/goods_entity.dart b/lib/core/entity/goods_entity.dart index 459e81e..18c1b5e 100644 --- a/lib/core/entity/goods_entity.dart +++ b/lib/core/entity/goods_entity.dart @@ -3,6 +3,7 @@ import 'package:satu/core/utils/utils_parse.dart'; const String goodTableName = 'goods'; const String GoodColumnId = 'id'; const String GoodColumnCategoryId = 'category_id'; +const String GoodColumnCategoryName = 'categoryName'; const String GoodColumnName = 'name'; const String GoodColumnEan = 'ean'; const String GoodColumnArticul = 'articul'; @@ -21,6 +22,7 @@ class Good { name = (map[GoodColumnName] ?? '') as String ; price = map[GoodColumnPrice] as num; categoryId = map[GoodColumnCategoryId] as int; + categoryName = map[GoodColumnCategoryName] as String?; ean = map[GoodColumnEan] as String; appCompanyId = map[GoodColumnAppCompanyId] as int; optPrice = map[GoodColumnOptPrice] as num; @@ -31,6 +33,7 @@ class Good { int? id; int? categoryId; + String? categoryName; String name = ''; String? ean; int? articul; @@ -49,6 +52,7 @@ class Good { GoodColumnName: name, GoodColumnPrice: price, GoodColumnCategoryId: categoryId, + GoodColumnCategoryName: categoryName, GoodColumnEan: ean, GoodColumnAppCompanyId: appCompanyId, GoodColumnOptPrice: optPrice, diff --git a/lib/core/models/dictionary/good/good_response_entity.dart b/lib/core/models/dictionary/good/good_response_entity.dart index 08ae0d2..fd5a779 100644 --- a/lib/core/models/dictionary/good/good_response_entity.dart +++ b/lib/core/models/dictionary/good/good_response_entity.dart @@ -19,6 +19,8 @@ class GoodResponseEntity { double? price; @JsonKey(name: 'opt_price') double? optPrice; + @JsonKey(name: 'app_company_id') + int? appCompanyId; @JsonKey(name: 'base_price') double? basePrice; @JsonKey(name: 'auto_base_price') diff --git a/lib/core/models/dictionary/good/good_response_entity.g.dart b/lib/core/models/dictionary/good/good_response_entity.g.dart index 80234fc..e1c4816 100644 --- a/lib/core/models/dictionary/good/good_response_entity.g.dart +++ b/lib/core/models/dictionary/good/good_response_entity.g.dart @@ -15,6 +15,7 @@ GoodResponseEntity _$GoodResponseEntityFromJson(Map json) => ..articul = json['articul'] as int? ..price = (json['price'] as num?)?.toDouble() ..optPrice = (json['opt_price'] as num?)?.toDouble() + ..appCompanyId = json['app_company_id'] as int? ..basePrice = (json['base_price'] as num?)?.toDouble() ..autoBasePrice = (json['auto_base_price'] as num?)?.toDouble() ..divisible = json['divisible'] as int? @@ -30,6 +31,7 @@ Map _$GoodResponseEntityToJson(GoodResponseEntity instance) => 'articul': instance.articul, 'price': instance.price, 'opt_price': instance.optPrice, + 'app_company_id': instance.appCompanyId, 'base_price': instance.basePrice, 'auto_base_price': instance.autoBasePrice, 'divisible': instance.divisible, diff --git a/lib/core/models/flow/dao/product_dao.dart b/lib/core/models/flow/dao/product_dao.dart index 0852360..a176aeb 100644 --- a/lib/core/models/flow/dao/product_dao.dart +++ b/lib/core/models/flow/dao/product_dao.dart @@ -5,7 +5,7 @@ class ProductDao { ProductDao.fromMap(dynamic map) { id = map['id'] as int; - categoryId = map['categoryId'] as int; + categoryId = map['categoryId'] as int? ; count = map['count'] as double; price = map['price'] as double; productName = cast(map['productName']) ?? ''; diff --git a/lib/core/redux/actions/sell_actions.dart b/lib/core/redux/actions/sell_actions.dart index 1fdf3b1..edeb378 100644 --- a/lib/core/redux/actions/sell_actions.dart +++ b/lib/core/redux/actions/sell_actions.dart @@ -127,17 +127,10 @@ ThunkAction addSellItem({required Good good, String? excise}) { ..count = 1.0 ..eanCode = good.ean ..productName = good.name - ..excise = excise; - //category add logic - if (good.categoryId != null) { - final List> set = await _dbService - .queryRowsWithWhere(categoryTableName, 'id = ?', [good.categoryId]); - if (set.isNotEmpty) { - final Category category = Category.fromMap(set.first); - item.categoryId = category.id; - item.categoryName = category.name; - } - } + ..excise = excise + ..categoryId = good.categoryId + ..categoryName = good.categoryName; + final TransactionRec rec = TransactionRec() ..data = jsonEncode(item.toMap()) ..transactionId = transaction.id diff --git a/lib/core/services/api_service.dart b/lib/core/services/api_service.dart index f913c3b..76f0e76 100644 --- a/lib/core/services/api_service.dart +++ b/lib/core/services/api_service.dart @@ -44,12 +44,12 @@ class ApiService extends BaseService { headers.addAll(header); } final String url = '$endpoint$point'; - // if (requestBody != null) { - // log.i(host); - // log.i(url); - // log.i(headers); - // log.i(jsonEncode(requestBody)); - // } + if (requestBody != null) { + //log.i(host); + //log.i(url); + //log.i(headers); + log.i(jsonEncode(requestBody)); + } final response = await http.post(Uri.https(host, url), body: jsonEncode(requestBody), headers: headers); diff --git a/lib/core/services/db_service.dart b/lib/core/services/db_service.dart index 77be000..8b0bd2d 100644 --- a/lib/core/services/db_service.dart +++ b/lib/core/services/db_service.dart @@ -50,30 +50,6 @@ class DbService extends BaseService { log.i('create tables'); //Goods table - await db.execute(''' - CREATE TABLE IF NOT EXISTS $goodTableName ( - $GoodColumnId integer primary key unique, - $GoodColumnArticul integer not null, - $GoodColumnName text not null, - $GoodColumnPrice real not null, - $GoodColumnCategoryId integer not null, - $GoodColumnEan text, - $GoodColumnAppCompanyId integer, - $GoodColumnOptPrice real, - $GoodColumnBasePrice real, - $GoodColumnDivisible integer, - $GoodColumnUpdatedAt text not null - ); - '''); - await db.execute(''' - CREATE TABLE IF NOT EXISTS $categoryTableName ( - $categoryColumnId integer primary key unique, - $categoryColumnName text not null, - $categoryColumnParentIn integer, - $categoryColumnAppCompanyId integer, - $categoryColumnUpdatedAt text not null - ); - '''); await db.execute(''' CREATE TABLE IF NOT EXISTS $transactionTableName ( $transactionColumnId integer primary key autoincrement, diff --git a/lib/core/services/dictionary_service.dart b/lib/core/services/dictionary_service.dart index af34188..c0a3ccc 100644 --- a/lib/core/services/dictionary_service.dart +++ b/lib/core/services/dictionary_service.dart @@ -15,21 +15,42 @@ class DictionaryService extends BaseService { final ApiService _api = locator(); final DbService _db = locator(); + Good goodResponseToGood(GoodResponseEntity response) { + return Good() + ..id = response.id + ..name = response.name + ..basePrice = response.basePrice + ..optPrice = response.optPrice + ..price = response.price + ..articul = response.articul + ..categoryId = response.categoryId + ..categoryName = response.categoryName + ..divisible = response.divisible + ..ean = response.ean13 + ..appCompanyId = response.appCompanyId; + } + + Category categoryResponseToCategory(CategoryResponse response) { + return Category() + ..id = response.id + ..parentId = response.parentId + ..name = response.name; + } + Future refreshFull() async {} - Future> getCategoryByParentId(int parentId) async { + Future> getCategoryByParentId(int? parentId) async { final List list = []; try { - final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; - final List> elements = await _db.queryRowsWithWhere( - categoryTableName, - '$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?', - [appCompanyId, parentId]); - for (final Map element in elements) { - list.add(Category.fromMap(element)); + dynamic filter = {'col': 'parent_id', 'action': 'equal', 'val': parentId}; + List responses = + await getCategories(page: 1, perpage: 100, filter: filter); + for (final CategoryResponse response in responses) { + final Category category = categoryResponseToCategory(response); + list.add(category); } } catch (e, stack) { - log.e('getCategoryByParentId', e, stack); + log.e('getCategories', e, stack); } return list; } @@ -52,19 +73,22 @@ class DictionaryService extends BaseService { return result; } - Future> getGoodsByCategoryId(int categoryId) async { + Future> getGoodsByCategoryId(int? categoryId) async { final List list = []; try { - final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; - final List> elements = await _db.queryRowsWithWhere( - goodTableName, - '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', - [appCompanyId, categoryId]); - for (final Map element in elements) { - list.add(Good.fromMap(element)); + dynamic filter = { + 'col': 'eacc_good_category_id', + 'action': 'equal', + 'val': categoryId + }; + List responses = + await getGoods(page: 1, perpage: 100, filter: filter); + for (final GoodResponseEntity response in responses) { + final Good good = goodResponseToGood(response); + list.add(good); } } catch (e, stack) { - log.e('getGoodsByCategoryId', e, stack); + log.e('getGoods', e, stack); } return list; } @@ -76,7 +100,7 @@ class DictionaryService extends BaseService { String where = '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) '; final List args = [appCompanyId, '%$query%']; - if (_isNumericInt(query)) { + if (_isNumericInt(query) && query.length >= 8) { where += ' or $GoodColumnEan like ?'; args.add('${int.parse(query).toString()}%'); } @@ -99,15 +123,13 @@ class DictionaryService extends BaseService { } Future> getCategories( - {required int page, required int perpage, String? query}) async { + {required int page, required int perpage, dynamic filter}) async { List list = []; try { final Map requestBody = { 'page': page, 'perpage': perpage, - 'filter': [ - {'col': 'name', 'action': 'like', 'val': query ?? ''} - ] + 'filter': [filter] }; DictResponseEntity categories = await _api @@ -148,15 +170,13 @@ class DictionaryService extends BaseService { } Future> getGoods( - {required int page, required int perpage, String? query}) async { + {required int page, required int perpage, dynamic filter}) async { List list = []; try { final Map requestBody = { 'page': page, 'perpage': perpage, - 'filter': [ - {'col': 'name', 'action': 'like', 'val': query ?? ''} - ] + 'filter': [filter] }; DictResponseEntity categories = @@ -177,11 +197,9 @@ class DictionaryService extends BaseService { Future saveGood(GoodResponseEntity good) async { DictResponseEntity? status; if (good.id != null) { - status = - await _api.dictionarySave('/goods_goods_edit', good.toJson()); + status = await _api.dictionarySave('/goods_goods_edit', good.toJson()); } else { - status = - await _api.dictionarySave('/goods_goods_add', good.toJson()); + status = await _api.dictionarySave('/goods_goods_add', good.toJson()); } String? message = getErrorMsg(status); diff --git a/lib/views/dictionaries/category/category_select_view.dart b/lib/views/dictionaries/category/category_select_view.dart index e02d155..678d8eb 100644 --- a/lib/views/dictionaries/category/category_select_view.dart +++ b/lib/views/dictionaries/category/category_select_view.dart @@ -132,8 +132,9 @@ class _CategorySelectViewState extends State { } Future _fetchData(int pageKey, int perPage, String? query) async { + dynamic filter = {'col': 'name', 'action': 'like', 'val': query ?? ''}; final List newItems = await _dictionaryService - .getCategories(page: pageKey, query: query, perpage: perPage); + .getCategories(page: pageKey, filter: filter, perpage: perPage); final isLastPage = newItems.length < _pageSize; if (isLastPage) { diff --git a/lib/views/dictionaries/category/category_view.dart b/lib/views/dictionaries/category/category_view.dart index 2744937..7f2c700 100644 --- a/lib/views/dictionaries/category/category_view.dart +++ b/lib/views/dictionaries/category/category_view.dart @@ -137,8 +137,9 @@ class _CategoryDictionaryViewState extends State { } Future _fetchData(int pageKey, int perPage, String? query) async { + dynamic filter = {'col': 'name', 'action': 'like', 'val': query ?? ''}; final List newItems = await _dictionaryService - .getCategories(page: pageKey, query: query, perpage: perPage); + .getCategories(page: pageKey, filter: filter, perpage: perPage); final isLastPage = newItems.length < _pageSize; if (isLastPage) { diff --git a/lib/views/dictionaries/goods/goods_view.dart b/lib/views/dictionaries/goods/goods_view.dart index b534e27..3679aae 100644 --- a/lib/views/dictionaries/goods/goods_view.dart +++ b/lib/views/dictionaries/goods/goods_view.dart @@ -152,7 +152,9 @@ class _GoodsDictionaryViewState extends State { Future _fetchData(int pageKey, int perPage, String? query) async { final List newItems = await _dictionaryService.getGoods( - page: pageKey, query: query, perpage: perPage); + page: pageKey, + filter: {'col': 'name', 'action': 'like', 'val': query ?? ''}, + perpage: perPage); final isLastPage = newItems.length < _pageSize; if (isLastPage) { diff --git a/lib/views/work/views/add_product/add_product_view.dart b/lib/views/work/views/add_product/add_product_view.dart index 51e866a..939e6fb 100644 --- a/lib/views/work/views/add_product/add_product_view.dart +++ b/lib/views/work/views/add_product/add_product_view.dart @@ -40,11 +40,11 @@ class _AddProductViewState extends State { reset(); } }); - _history = [Category()..id = 0]; + _history = [Category()..id = null]; _categories = []; _goods = []; super.initState(); - navigateCategory(0); + navigateCategory(null); } @override @@ -128,7 +128,7 @@ class _AddProductViewState extends State { _searchTextController.clear(); } - void navigateCategory(int categoryId) async { + void navigateCategory(int? categoryId) async { List categories = await _dictionaryService.getCategoryByParentId(categoryId); List goods =