From 4685e64ecae88482b289d66f9d683e3fe3c3f01c Mon Sep 17 00:00:00 2001 From: Jessica Woolford Date: Tue, 13 Dec 2022 16:02:21 -0500 Subject: [PATCH] Adding missing function Signed-off-by: Jessica Woolford --- .../app/staff/cat/volcopy/vol-edit.component.ts | 45 +++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts b/Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts index e80922cdcc..2996ef3fe5 100644 --- a/Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts @@ -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; } -- 2.11.0