Adding missing function
authorJessica Woolford <jwoolford@biblio.org>
Tue, 13 Dec 2022 21:02:21 +0000 (16:02 -0500)
committerChris Sharp <csharp@georgialibraries.org>
Mon, 9 Jan 2023 15:38:38 +0000 (10:38 -0500)
Signed-off-by: Jessica Woolford <jwoolford@biblio.org>
Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts

index e80922c..2996ef3 100644 (file)
@@ -343,6 +343,43 @@ export class VolEditComponent implements OnInit {
         return !this.volcopy.copyStatIsMagic(copy.status());
     }
 
+    checkBarcode(barcode: string): boolean {
+        if (barcode !== Number(barcode).toString()) { return false; }
+
+        const bc = barcode.toString();
+
+        // "16.00" == Number("16.00"), but the . is bad.
+        // Throw out any barcode that isn't just digits
+        if (bc.search(/\D/) !== -1) { return false; }
+
+        const lastDigit = bc.substr(bc.length - 1);
+        const strippedBarcode = bc.substr(0, bc.length - 1);
+        return this.barcodeCheckdigit(strippedBarcode).toString() === lastDigit;
+    }
+
+    barcodeCheckdigit(bc: string): number {
+        let checkSum = 0;
+        let multiplier = 2;
+        const reverseBarcode = bc.toString().split('').reverse();
+
+        reverseBarcode.forEach(ch => {
+            let tempSum = 0;
+            const product = (Number(ch) * multiplier) + '';
+            product.split('').forEach(num => tempSum += Number(num));
+            checkSum += Number(tempSum);
+            multiplier = multiplier === 2 ? 1 : 2;
+        });
+
+        const cSumStr = checkSum.toString();
+        const nextMultipleOf10 =
+            (Number(cSumStr.match(/(\d*)\d$/)[1]) * 10) + 10;
+
+        let checkDigit = nextMultipleOf10 - Number(cSumStr);
+        if (checkDigit === 10) { checkDigit = 0; }
+
+        return checkDigit;
+    }
+
     generateBarcodes() {
         this.autoBarcodeInProgress = true;
 
@@ -418,6 +455,12 @@ export class VolEditComponent implements OnInit {
                 err => {},
                 () => this.emitSaveChange()
             );
+
+            // Assume the barcode is good before checking if it's bad
+            copy._bad_barcode = false;
+            if (this.volcopy.defaults.values.use_checkdigit && this.checkBarcode(barcode) == false) {
+            copy._bad_barcode = true;
+            }
         }
     }
 
@@ -583,7 +626,7 @@ export class VolEditComponent implements OnInit {
         const copies = this.context.copyList();
 
         const badCopies = copies.filter(copy => {
-            return copy._dupe_barcode || !copy.barcode();
+            return copy._dupe_barcode || copy._bad_barcode ||!copy.barcode();
         }).length > 0;
 
         if (badCopies) { return false; }