-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy an EAN code from an EAN product to a receipt product (#14)
* Refactoring (add 'printBasicInfo') & add Future + await for 'readHtmlAndSaveAsCSV' * Set 'printBasicInfo' & 'printSelectedValues' as private methods * Refactor (move 'receiptProducts2CSV' to the main level) * Refactor (move 'eanProductListToCSV' to the main level & some renaming) * Rename a file ('save_rps_as_csv' -> 'receipt_products_2_csv') * Update the main method (give shop selector name to eanProducts2CSV) * Refactor (move 'printSelectedValues' & 'printBasicInfo' to an own file) * Update dart_kassakuitti_cli (the starting point for EAN handling) * Update pubspec.yaml (update the version number) * Update toStrings of models (better printing) * Update the main method (initial product name comparison) * Update 'printSelectedValues' method (add new line to the end of the print) * Update the main method (initial EAN code handling) * Update the main method (continue initial EAN handling) * Add 'eanHandler' & move code from the method to it * Update receipt csv EAN title * Update eanHandler (EAN product name to lower case) * Update eanHandler (clean a bit) * Update eanHandler (refactor & enhance a bit) * Update the main method (clean & give a copy of eanProducts to the EAN handler) * Update eanHandler (better handling for non-found RPs) * Update eanHandler (check if splittedReceiptProcuctNames is empty) * Update eanHandler (a bit fine-tuning) * Update eanHandler (handle cases with >2 filtered results) * Update eanHandler (make 2 methods private)
- Loading branch information
Showing
10 changed files
with
162 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 3 additions & 10 deletions
13
bin/read_html_and_save_as_csv.dart → bin/read_ean_products.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
import 'models/ean_product.dart'; | ||
import 'specific/s_kaupat/load_html_s_kaupat.dart' as s_kaupat; | ||
import 'specific/k_ruoka/load_html_k_ruoka.dart' as k_ruoka; | ||
import 'save_eps_as_csv.dart'; | ||
import 'utils/shop_selector_helper.dart'; | ||
|
||
void readHtmlAndSaveAsCSV( | ||
Future<List<EANProduct>> readEANProducts( | ||
String filePath, ShopSelector shopSelector, String csvFilePath) async { | ||
List<EANProduct> awaitedEANProductList = []; | ||
|
||
switch (shopSelector) { | ||
case ShopSelector.sKaupat: | ||
awaitedEANProductList = await s_kaupat.loadHtmlFromAssets(filePath); | ||
break; | ||
return await s_kaupat.loadHtmlFromAssets(filePath); | ||
case ShopSelector.kRuoka: | ||
awaitedEANProductList = await k_ruoka.loadHtmlFromAssets(filePath); | ||
break; | ||
return await k_ruoka.loadHtmlFromAssets(filePath); | ||
} | ||
|
||
eanProductListToCSV(awaitedEANProductList, csvFilePath, shopSelector); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'dart:io'; | ||
|
||
import '../../models/ean_product.dart'; | ||
import '../../models/receipt_product.dart'; | ||
|
||
void eanHandler( | ||
List<ReceiptProduct> receiptProducts, List<EANProduct> eanProducts) { | ||
print('\nThe first round begins!'); | ||
print('Statistics: ${receiptProducts.length} receiptProducts, ' | ||
'${eanProducts.length} eanProducts\n'); | ||
List<ReceiptProduct> nonFoundReceiptProducts = []; | ||
|
||
for (var receiptProduct in receiptProducts) { | ||
print(receiptProduct); | ||
|
||
var filteredEanProducts = | ||
_filterEANProducts(receiptProduct.name, eanProducts); | ||
|
||
_handleFoundCases(receiptProduct, filteredEanProducts, eanProducts); | ||
|
||
if (filteredEanProducts.isEmpty) { | ||
print('\tNo product found for the 1st round.'); | ||
nonFoundReceiptProducts.add(receiptProduct); | ||
} | ||
} | ||
|
||
print('\nThe second round begins!'); | ||
print( | ||
'Statistics: ${nonFoundReceiptProducts.length} nonFoundReceiptProducts, ' | ||
'${eanProducts.length} eanProducts\n'); | ||
|
||
for (var nonFoundReceiptProduct in nonFoundReceiptProducts) { | ||
print(nonFoundReceiptProduct); | ||
|
||
var splittedReceiptProcuctNames = nonFoundReceiptProduct.name.split(' '); | ||
|
||
if (splittedReceiptProcuctNames.isEmpty) { | ||
print('\tNo product found for the 2nd round.'); | ||
continue; | ||
} | ||
|
||
var filteredEanProducts = | ||
_filterEANProducts(splittedReceiptProcuctNames[0], eanProducts); | ||
|
||
if (filteredEanProducts.length > 2 && | ||
splittedReceiptProcuctNames.length > 1) { | ||
filteredEanProducts = | ||
_filterEANProducts(splittedReceiptProcuctNames[1], eanProducts); | ||
} | ||
|
||
_handleFoundCases(nonFoundReceiptProduct, filteredEanProducts, eanProducts); | ||
|
||
if (filteredEanProducts.isEmpty) { | ||
print('\tNo product found for the 2nd round.'); | ||
} | ||
} | ||
|
||
print('\nFinished!'); | ||
print('Only ${eanProducts.length} unknown eanProducts left.'); | ||
} | ||
|
||
List<EANProduct> _filterEANProducts( | ||
String receiptProductName, List<EANProduct> eanProducts) { | ||
return eanProducts | ||
.where((eanProduct) => | ||
eanProduct.name.toLowerCase().contains(receiptProductName)) | ||
.toList(); | ||
} | ||
|
||
void _handleFoundCases(ReceiptProduct receiptProduct, | ||
List<EANProduct> filteredEanProducts, List<EANProduct> origEanProducts) { | ||
if (filteredEanProducts.length == 1) { | ||
print('\tFound one product:'); | ||
print('\t\t${filteredEanProducts[0]}'); | ||
|
||
receiptProduct.eanCode = filteredEanProducts[0].ean; | ||
origEanProducts.remove(filteredEanProducts[0]); | ||
} else if (filteredEanProducts.length > 1) { | ||
print( | ||
'\tFound multiple products (${filteredEanProducts.length} possible choices):'); | ||
|
||
for (var filteredReceiptProduct in filteredEanProducts) { | ||
print('\t\t$filteredReceiptProduct'); | ||
print('\t\tIs this the right product? (y/n)'); | ||
var answer = stdin.readLineSync(); | ||
if (answer?.toLowerCase() == 'y') { | ||
receiptProduct.eanCode = filteredReceiptProduct.ean; | ||
origEanProducts.remove(filteredReceiptProduct); | ||
break; | ||
} | ||
} | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
..._kaupat/read_receipt_and_save_as_csv.dart → ...cific/s_kaupat/read_receipt_products.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:path/path.dart'; | ||
import 'package:yaml/yaml.dart'; | ||
|
||
void printSelectedValues(String selectedTextFile, String selectedHtmlFile, | ||
String selectedStore, String csvFilesPath) { | ||
print('''Selected values: | ||
- Path to the cash receipt:\t\t$selectedTextFile | ||
- Path to the EAN products file:\t$selectedHtmlFile | ||
- Selected store:\t\t\t$selectedStore | ||
- Path where to save CSV files:\t$csvFilesPath\n'''); | ||
} | ||
|
||
Future<void> printBasicInfo() async { | ||
String pathToYaml = | ||
join(dirname(Platform.script.toFilePath()), '../pubspec.yaml'); | ||
var file = File(pathToYaml); | ||
var fileAsString = await file.readAsString(); | ||
|
||
Map yaml = loadYaml(fileAsString); | ||
print(yaml['name']); | ||
print(yaml['description']); | ||
print('Version: ${yaml['version']}'); | ||
print('Homepage: ${yaml['homepage']}'); | ||
|
||
print('''\nTo get help, run: | ||
dart run bin/dart_kassakuitti_cli.dart help | ||
or when using alias: | ||
kassakuitti help\n'''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters