import { PriceRangeModel } from './price-range.model'; //only reason to have this one is because circular dependency as core module cant have dependencies with other modules // this exact enum exists in '@arrow/bom/list/src/buying-options/buying-options.interfaces'; export enum PackagingFilterOption { invalid = 0, cutStrip = 1, reel, other, arrowReel } export class BuyingOptionModel { // buying option data availability: number; increments: number; leadTime: number; minimum: number; packaging: string; packagingTypeId: PackagingFilterOption; priceRanges: PriceRangeModel[]; pipeLineDetails: any[] = []; region: string; shipIn: number; sourcePartId: string; sourceCode: string; canAddToCart: boolean; hasTariff: boolean; isArrowReel = false; // Has this BO utilized the "Arrow Reel" option? isReelable: boolean; // Is it possible to make an "Arrow Reel" from this BO? isHidden: boolean; isTemporalHidden: boolean; reelCharge: number; displayedPriceRanges: any; selectedPriceIndex: number; hasPriceGroup: any; reelOptions: { reelCount: number; reelLength: number; }; reelTotalQuantity: number; bufferStock: number; factoryStock: number; // selected data selected: any; selectedPrice: number; quantity: number; input: number; valid: boolean = true; inCart: number = 0; cartPartId: string; cartIpn: string; autoGenerated: boolean = false; // Is the quantity at or below the TOTAL row quantity (regardless of availability) isQtyValid: boolean = true; isIncrementsValid: boolean = true; isMinimumsValid: boolean = true; isArrowReelValid = true; // True if the current quantity is at or below the available quantity // False if the quantity is above what is available in stock isQtyAvailable: boolean = true; isUserSelection: boolean = false; isDirty: boolean = false; isLocked: boolean; isNumber: boolean = false; validBuyingOption: boolean = true; tieredPriceGroup: boolean; giWarehouseCountry: String; isGlobalInventory: String; constructor(data: any, selectedSources: Array) { this.tieredPriceGroup = data.isGroupPrice; this.availability = data.availabilityQty; this.increments = data.increment; this.leadTime = data.mfrLeadTime; this.minimum = data.minimum; this.packaging = PackagingFilterOption[data.packagingTypeId]; this.packagingTypeId = data.packagingTypeId; this.priceRanges = data.priceRanges; this.pipeLineDetails = data.pipeLineDetails; this.sourceCode = data.sourceCode; this.canAddToCart = data.canAddToCart; this.region = data.region; this.shipIn = data.shipIn; this.sourcePartId = data.sourcePartId; this.selected = this.selectedSource(selectedSources); this.hasTariff = !!data.tariffFlag; this.isReelable = data.reelableFlag; this.reelCharge = data.reelCharge; this.reelOptions = null; this.isHidden = data.isHidden; this.isTemporalHidden = data.isHidden; this.bufferStock = data.bufferStock; this.factoryStock = data.factoryStock; this.giWarehouseCountry = data.giWarehouseCountry; this.isGlobalInventory = data.isGlobalInventory; this.isHidden = data.isHidden; this.isTemporalHidden = data.isHidden; if (!!this.selected) { this.selectedPrice = this.selected.currentPrice; this.quantity = this.selected.quantityRequested; this.autoGenerated = this.selected.autoGenerated; this.input = this.quantity > 0 ? this.quantity : undefined; this.valid = this.isValidSelectedBuyingOption(); this.setLockStatus(); if ( this.selected.reelOptions && this.selected.reelOptions.reelLength && this.selected.reelOptions.reelCount ) { this.reelOptions = this.selected.reelOptions; this.reelTotalQuantity = this.selected.reelOptions.reelLength * this.selected.reelOptions.reelCount; this.isArrowReel = true; } } } public getPackageNameId(): string{ switch(this.packagingTypeId){ case PackagingFilterOption.arrowReel: return "arrow-reel"; case PackagingFilterOption.cutStrip: return "cut-strip"; case PackagingFilterOption.other: return "other"; case PackagingFilterOption.reel: return "reel"; default: return ""; } } public isArrowReelPackage():boolean { return this.packagingTypeId == PackagingFilterOption.arrowReel; } public isCutStrip():boolean { return this.packagingTypeId == PackagingFilterOption.cutStrip; } selectedSource(selectedSources: Array): BuyingOptionModel { return selectedSources.find( selected => selected.sourcePartId === this.sourcePartId ); } private _isArrowReelValid(): boolean { if (this.isArrowReel) { if (this.reelOptions) { const totalReqQty: any = this.reelOptions.reelLength * this.reelOptions.reelCount; return isNaN(totalReqQty) || totalReqQty <= 1 ? false : true; } else { return false; } } else { return true; } } isValidSelectedBuyingOption(): boolean { this.isArrowReelValid = this._isArrowReelValid(); let qty = Number(this.input); if (this.isArrowReel && this.isArrowReelValid) { qty = this.reelOptions.reelLength * this.reelOptions.reelCount; this.isQtyAvailable = !(qty > this.availability); this.isNumber = !Number.isNaN(qty); this.isIncrementsValid = true; this.isMinimumsValid = true; } else { this.isNumber = !Number.isNaN(qty); this.isQtyAvailable = !(qty > this.availability); this.isIncrementsValid = !(qty % this.increments); this.isMinimumsValid = !qty || !(qty < this.minimum); } this.valid = this.isQtyAvailable && this.isIncrementsValid && this.isMinimumsValid && this.isNumber && this.isArrowReelValid; return this.valid; } setLockStatus(): void { this.isLocked = this.isDirty ? this.valid : !this.autoGenerated && this.selected; } addCartInfo(cartEntries: Array): void { cartEntries.forEach(entry => { if (this.sourcePartId === entry.catalogSourcePartId) { this.inCart = entry.quantity; this.cartPartId = entry.vendorItemNo; this.cartIpn = entry.ipn; } }); } reset(): void { this.input = this.quantity > 0 ? this.quantity : undefined; this.isQtyValid = true; this.isDirty = false; this.isLocked = undefined; this.setLockStatus(); } hasValidCartPartId(): boolean { if (!this.cartPartId) { return false; } else if ( typeof this.cartPartId === 'string' && this.cartPartId.length > 0 ) { return true; } else { return false; } } isValidBuyingOption(): boolean { return this.isQtyAvailable || this.isQtyValid || this.input > 0; } }