@@ -1295,9 +1295,9 @@ public Product findByCode(ScannedCode scannedCode) {
12951295
12961296 // try again for upc codes
12971297 if (product == null ) {
1298- String upcCode = extractUpcA (scannedCode .getLookupCode ());
1299- if (upcCode != null ) {
1300- return findByCode (upcCode , scannedCode .getTemplateName ());
1298+ String shorterCode = removePrefix (scannedCode .getLookupCode ());
1299+ if (shorterCode != null ) {
1300+ return findByCode (shorterCode , scannedCode .getTemplateName ());
13011301 }
13021302 }
13031303
@@ -1311,14 +1311,28 @@ private Product findByCode(String lookupCode, String templateName) {
13111311 templateName
13121312 }, false );
13131313
1314- return getFirstProductAndClose (cursor );
1314+ Product product = getFirstProductAndClose (cursor );
1315+
1316+ if (product == null ) {
1317+ String shorterCode = removePrefix (lookupCode );
1318+ if (shorterCode != null ) {
1319+ return findByCode (shorterCode , templateName );
1320+ }
1321+ }
1322+
1323+ return product ;
13151324 }
13161325
1317- private String extractUpcA (String code ) {
1318- if (code .length () == 13 && code .startsWith ("0" )) {
1326+ private String removePrefix (String code ) {
1327+ if (code .length () == 12 && code .startsWith ("0000" )) {
1328+ // convert EAN12 to EAN8
1329+ return code .substring (4 );
1330+ } else if (code .length () == 13 && code .startsWith ("0" )) {
1331+ // convert UPC-A or EAN13 to EAN12
1332+ return code .substring (1 );
1333+ } else if (code .length () == 14 && code .startsWith ("0" )) {
1334+ // convert EAN14 to EAN13
13191335 return code .substring (1 );
1320- } else if (code .length () == 14 && code .startsWith ("00" )) {
1321- return code .substring (2 );
13221336 } else {
13231337 return null ;
13241338 }
0 commit comments