import { Component, Input, OnChanges } from '@angular/core'; import { ListItemModel, BuyingOptionModel } from '@arrow/bom/core'; @Component({ selector: 'bom-buying-rules', templateUrl: 'buying-rules.component.html', styleUrls: ['buying-rules.component.scss'] }) export class BuyingRulesComponent implements OnChanges { @Input() item: ListItemModel; @Input() buyingOptions: BuyingOptionModel[] = []; uniqueOrderedIncrements: number[] = []; lowestIncrement: number = -1; isMultipleIncrements: boolean = false; uniqueOrderedMinimums: number[] = []; lowestMinimum: number = -1; isMultipleMinimums: boolean = false; // show/hide the "MOQ" element (also used outside of template - to decide row height) showMoq: boolean = false; // show/hide the "increments" element (also used outside of template - to decide row height) showIncrements: boolean = false; ngOnChanges(): void { this._refreshArrays(this.buyingOptions, this.item); } /** * * @param buyingOptions * @param item */ private _refreshArrays( buyingOptions: BuyingOptionModel[], item: ListItemModel ): void { const allIncrements: number[] = []; const allMins: number[] = []; buyingOptions.forEach((bo: BuyingOptionModel) => { allIncrements.push(bo.increments); allMins.push(bo.minimum); }); this.uniqueOrderedIncrements = this._getUniqueOrderedArray(allIncrements); this.isMultipleIncrements = this.uniqueOrderedIncrements.length > 1; this.lowestIncrement = this.uniqueOrderedIncrements[0]; if (!this.isMultipleIncrements) { item.increment = this.lowestIncrement; } this.uniqueOrderedMinimums = this._getUniqueOrderedArray(allMins); this.isMultipleMinimums = this.uniqueOrderedMinimums.length > 1; this.lowestMinimum = this.uniqueOrderedMinimums[0]; this._refreshFlags(item, this.lowestMinimum, this.lowestIncrement); } /** * refresh mbo flag * @param item * @param lowestMinimum * @param lowestIncrement */ private _refreshFlags( item: ListItemModel, lowestMinimum: number, lowestIncrement: number ): void { if (item.hasMBO) { this.showMoq = lowestMinimum > 1; this.showIncrements = lowestIncrement > 1; } else if (item.hasPriceInfo) { this.showMoq = !item.quantity || item.minQuantity > 1; this.showIncrements = item.increment > 1; } else { this.showMoq = false; this.showIncrements = false; } } /** * Takes in a list, strips out duplicate items and orders them from * lowest to highest (lowest value at 0 index) * @param dirtyList */ private _getUniqueOrderedArray(dirtyList: number[]): number[] { const orderedList: number[] = []; dirtyList.forEach(item => { const matched = orderedList.find(olItem => olItem === item); if (!matched) { orderedList.push(item); } }); return orderedList.sort(); } }