import { Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core'; import { BuyingOptionModel, ListItemModel, Utils } from '@arrow/bom/core'; @Component({ selector: 'bom-part-stock', templateUrl: './bom-part-stock.component.html', styleUrls: ['./bom-part-stock.component.scss'] }) export class BomPartStockComponent implements OnChanges { @Input() item: ListItemModel; @Input() loading: boolean; @ViewChild('factoryStock') factoryStockEl: ElementRef; @ViewChild('bufferStock') bufferStockEl: ElementRef; /** * Multiple buying options location (value can be any region, PLUS 'multiple') */ mboLocation: string = null; /** * The ealiest ship time for MBO */ mboEarliest: number = -1; /** * Helps in deciding whether to display e.g "in as early as 2 days" versus "in 2 days" * For example there can be 3 buying options and all of them ship in 5 days * we just say "ships in 5 days" * If however there are 3 buying options and one ships in tomorrow and the other ships in 4 days * we say "ships as early as tomorrow" * When this is 'true' then the "as early as" text is added to the shipping message */ mboHasMultipleShipTimes: boolean = false; constructor(public utils: Utils) {} ngOnChanges(changes: SimpleChanges) { if (changes.item && this.item.hasMBO) { this.recalculateMbo(this.item.buyingOptions); } } /** * Determines location and shipping time and text to display for parts with MBO * @param item */ recalculateMbo(buyingOptions: BuyingOptionModel[]) { const uniqueLocations: string[] = []; const allShipIn: number[] = []; buyingOptions.forEach(bOption => { // Collect all ship dates allShipIn.push(bOption.shipIn); // Collect all unique locations if (!uniqueLocations.find(region => region === bOption.region)) { uniqueLocations.push(bOption.region); } }); // location 'region' or 'multiple' if (uniqueLocations.length === 1) { this.mboLocation = uniqueLocations[0]; } else if (uniqueLocations.length > 1) { this.mboLocation = 'multiple'; } // The earlies ship date from all this.mboEarliest = allShipIn.reduce((a, b, i, arr) => Math.min(a, b)); // Removes any duplicate ship times from the array const uniqueShipIn: number[] = Array.from(new Set(allShipIn)); // If there is only one ship time left, then there are NOT multiple ship times this.mboHasMultipleShipTimes = uniqueShipIn.length > 1; } public getPackageName(listItem: ListItemModel): string{ const nameFallback = listItem.packaging; const buyingOption = listItem.buyingOptions.find(bo=>bo.sourcePartId == listItem.sourcePartId); if(buyingOption){ const nameId = buyingOption.getPackageNameId(); return this.utils.translateByKeyWithFallback(nameId, nameFallback); } return nameFallback; } }