From 8b094cefc994c11ee0178d69f88b63fb9ab3ffca Mon Sep 17 00:00:00 2001 From: Zeb Fross Date: Sun, 29 Mar 2020 21:19:21 -0700 Subject: [PATCH] Add categories, units, and quantities to Kroger converter Kroger data comes with categories, so used them to help determine locations. --- helpers/KrogerToGrocyConverter.php | 78 ++++++++++++++++++++++++--- tests/KrogerToGrocyConverterTest.php | 9 +++- tests/testdata/receipts_expected.json | 4 +- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/helpers/KrogerToGrocyConverter.php b/helpers/KrogerToGrocyConverter.php index 1e76b365..7a21fb66 100644 --- a/helpers/KrogerToGrocyConverter.php +++ b/helpers/KrogerToGrocyConverter.php @@ -6,7 +6,7 @@ class KrogerToGrocyConverter { // Fields returned: 'name', 'location_id', 'qu_id_purchase', 'qu_id_stock', // 'qu_factor_purchase_to_stock', 'barcode', 'default_best_before_days' - // 'quantity', 'transaction_date', 'price_paid' + // 'quantity', 'transaction_date', 'price_paid', 'picture_url' public static function ConvertJson($data, $default_quantity_units, $default_location_id) { if (array_key_exists("data", $data)) @@ -31,20 +31,82 @@ class KrogerToGrocyConverter continue; } - $product = null; + if (array_key_exists("itemType", $item) && $item['itemType'] == 'MISC_TRANS_RECEIPT') + { + continue; + } + $barcode = KrogerToGrocyConverter::ConvertUpcToBarcode($item["baseUpc"]); + $quantity = $item['quantity']; + if ($item['weighted'] && array_key_exists('averageWeight', $item['detail'])) + { + $quantity = round($item['quantity'] / $item['detail']['averageWeight']); + } + + $quantity_units = $default_quantity_units; + if (array_key_exists('unitOfMeasure', $item)) + { + if ($item['unitOfMeasure'] == 'EA') + { + $quantity_units = 3 /*pack*/; + } + else if ($item['unitOfMeasure'] == 'LBR') + { + $quantity_units = 2 /*piece*/; + } + } + + $location = $default_location_id; + + if (array_key_exists('categories', $item['detail']) && count($item['detail']['categories']) > 0) + { + $category = $item['detail']['categories'][0]['categoryCode']; + switch (intval($category)) + { + case 1 /*adult beverage*/: + $location = 3 /*pantry*/; + $quantity_units = 10 /*bottle*/; + break; + case 6 /*bakery*/: + case 7 /*baking goods*/: + case 10 /*breakfast*/: + case 12 /*canned and packaged*/: + case 13 /*cleaning products*/: + case 33 /*pasta, sauces, & grain*/: + case 36 /*produce*/: + case 73 /*natural & organic*/: + case 75 /*personal care*/: + case 76 /*health*/: + $location = 3 /*pantry*/; + break; + case 15 /*dairy*/: + case 16 /*deli*/: + case 28 /*meet & seafood*/: + $location = 2 /*fridge*/; + break; + case 20 /*frozen*/: + $location = 6 /*freezer*/; + break; + case 37 /*snacks*/: + case 77 /*candy*/: + $location = 4 /*candy cupboard*/; + break; + } + } + $product = array( 'name' => $item["detail"]["description"], - 'location_id' => $default_location_id, - 'qu_id_purchase' => $default_quantity_units, - 'qu_id_stock' => $default_quantity_units, + 'location_id' => $location, + 'qu_id_purchase' => $quantity_units, + 'qu_id_stock' => $quantity_units, 'qu_factor_purchase_to_stock' => 1, 'barcode' => $barcode, 'default_best_before_days' => 21, - 'quantity' => $item['quantity'], + 'quantity' => $quantity, 'transaction_date' => $transactionDate, - 'price_paid' => $item['pricePaid'] + 'price_paid' => $item['pricePaid'], + 'picture_url' => (array_key_exists("mainImage", $item['detail']) ? $item['detail']['mainImage'] : null) ); array_push($products, $product); @@ -70,7 +132,7 @@ class KrogerToGrocyConverter $checkSum = (intval($upc[0]) + intval($upc[2]) + intval($upc[4]) + intval($upc[6]) + intval($upc[8]) + $upc[10]) * 3; $checkSum = $checkSum + intval($upc[1]) + intval($upc[3]) + intval($upc[5]) + intval($upc[7]) + intval($upc[9]); - $checkDigit = 10 - ($checkSum % 10); + $checkDigit = (10 - ($checkSum % 10)) % 10; return $countryCode . $upc . strval($checkDigit); } diff --git a/tests/KrogerToGrocyConverterTest.php b/tests/KrogerToGrocyConverterTest.php index 986dbff9..b86b4149 100644 --- a/tests/KrogerToGrocyConverterTest.php +++ b/tests/KrogerToGrocyConverterTest.php @@ -17,7 +17,12 @@ final class KrogerToGrocyConverterTest extends TestCase $this->assertEquals( "0072220005165", KrogerToGrocyConverter::ConvertUpcToBarcode("0000007222000516") - ); + ); + + $this->assertEquals( + "0000000041300", + KrogerToGrocyConverter::ConvertUpcToBarcode("0000000004130") + ); } public function testConvertJson(): void @@ -37,7 +42,7 @@ final class KrogerToGrocyConverterTest extends TestCase { foreach ($product as $key => $value) { - $this->assertEquals($value, $products[$index][$key]); + $this->assertEquals($value, $products[$index][$key], "Failed matching key " . $key); } } } diff --git a/tests/testdata/receipts_expected.json b/tests/testdata/receipts_expected.json index 6d55302a..7e0cdbad 100644 --- a/tests/testdata/receipts_expected.json +++ b/tests/testdata/receipts_expected.json @@ -1,6 +1,6 @@ [{ "name": "Twix Minis Size Chocolate Cookie Bars", - "location_id": 2, + "location_id": 4, "qu_id_purchase": 3, "qu_id_stock": 3, "barcode": "0040000525509", @@ -8,7 +8,7 @@ "transaction_date": "2020-03-21" }, { "name": "Franz Extra Crisp English Muffins", - "location_id": 2, + "location_id": 3, "qu_id_purchase": 3, "qu_id_stock": 3, "barcode": "0072220005165",