import { Component, ElementRef, inject, OnInit, signal, viewChild } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { FormsModule } from '@angular/forms'; import { ButtonDirective } from 'primeng/button'; import { InputTextModule } from 'primeng/inputtext'; import { DialogModule } from 'primeng/dialog'; import { TransactionModel, WzRowMeyleDto } from '../../core/models'; import { WarehouseService } from '../../core/services/warehouse.service'; import { findCombinations, isValidMeyleScannedValue } from '../../shared/utils/business-logic'; import { PageShellComponent } from '../../shared/components/page-shell/page-shell.component'; import { ContentSectionComponent } from '../../shared/components/content-section/content-section.component'; @Component({ selector: 'app-meyle-pack-list-simple', standalone: true, imports: [ButtonDirective, InputTextModule, DialogModule, FormsModule, PageShellComponent, ContentSectionComponent], template: `
@if (exportValid()) {

Packing List został wygenerowany i wysłany!

} @else if (!emailAddresses?.trim()) {

Błąd: Proszę wprowadzić przynajmniej jeden ADRES EMAIL do wysyłki raportu!

} @else {

Błąd: Nie Wszystkie linie mają wypełniony NUMER PALETY.
Packing List nie zostanie wygenerowany!

}

Błąd skanowania! Wystąpił jeden z wyjątków (Zeskanowana wartość '{{ lastScannedValue }}'):

Błąd skanowania! Wybierz NUMER PALETY większy niż 0 (Aktualnie '{{ palletNumber }}'):

`, }) export class MeylePackListSimpleComponent implements OnInit { private readonly route = inject(ActivatedRoute); private readonly router = inject(Router); private readonly warehouseService = inject(WarehouseService); readonly scanner = viewChild>('scanner'); wzHeaderId = ''; rows: WzRowMeyleDto[] = []; transactionModels: Record = {}; changedRecords: WzRowMeyleDto[] = []; selectedRow: WzRowMeyleDto | null = null; selectedRows: WzRowMeyleDto[] = []; emailAddresses = ''; wzNumber = ''; palletNumber = '0'; scannedValue = ''; lastScannedValue = ''; itemNumber = ''; qty = '0'; palletNumberOutput = '0'; partNumberSl = ''; partNumberMeyle = ''; showInfoDialog = false; showValidationDialog = false; showPalletDialog = false; readonly exportValid = signal(false); get yearPrefix(): number { return new Date().getFullYear() - 2000; } async ngOnInit(): Promise { this.wzHeaderId = this.route.snapshot.paramMap.get('WzHeader') ?? ''; const header = await this.warehouseService.getWzHeaderById(this.wzHeaderId); this.rows = await this.warehouseService.getWzRowsMeyleByWzHeaderId(this.wzHeaderId); this.transactionModels = await this.warehouseService.getTransactionsModels(); this.emailAddresses = header.emailAddresses ?? ''; this.wzNumber = header.wzNumbers ?? ''; setTimeout(() => this.scanner()?.nativeElement.focus()); } changeView(): void { this.router.navigate(['/Warehouse/Meyle/PackList', this.wzHeaderId]); } hideModal(): void { this.showInfoDialog = false; this.showValidationDialog = false; this.showPalletDialog = false; this.lastScannedValue = this.scannedValue; this.scannedValue = ''; setTimeout(() => this.scanner()?.nativeElement.focus()); } async saveChanges(): Promise { if (this.emailAddresses?.trim()) await this.warehouseService.addEmailsToWzHeader(this.wzHeaderId, this.emailAddresses); if (this.changedRecords.length) { await this.warehouseService.updateWzRowsMeyle(this.changedRecords); this.rows = await this.warehouseService.getWzRowsMeyleByWzHeaderId(this.wzHeaderId); } } async exportXls(): Promise { const valid = !this.rows.some((x) => x.palletNumber == null) && !!this.emailAddresses?.trim(); this.exportValid.set(valid); if (valid) { await this.warehouseService.addEmailsToWzHeader(this.wzHeaderId, this.emailAddresses); await this.warehouseService.generateXlsForMeyle(this.wzHeaderId); } this.showInfoDialog = true; } async scanValue(): Promise { const value = this.scannedValue.trim(); if (!value || parseInt(this.palletNumber, 10) <= 0) { this.showPalletDialog = true; return; } const material = this.transactionModels[value]?.[0]; if (!material && isValidMeyleScannedValue(value)) { await this.fillMeylePartNumber(value); this.scannedValue = ''; return; } if (material) await this.fillFaPartNumberAndPalletNumber(material, value); else { this.showValidationDialog = true; this.changedRecords = []; return; } this.lastScannedValue = value; this.scannedValue = ''; setTimeout(() => this.scanner()?.nativeElement.focus()); } private async fillMeylePartNumber(scanned: string): Promise { if (this.selectedRow && !this.selectedRows.length) { this.selectedRow.partNumber = scanned; this.changedRecords.push(this.selectedRow); this.partNumberMeyle = scanned; } for (const row of this.selectedRows) { row.partNumber = scanned; this.changedRecords.push(row); } await this.saveChanges(); this.changedRecords = []; this.selectedRows = []; setTimeout(() => this.scanner()?.nativeElement.focus()); } private async fillFaPartNumberAndPalletNumber(material: TransactionModel, scanned: string): Promise { const palletNum = parseInt(this.palletNumber, 10); let rowIndex = this.rows.findIndex((x) => x.faIndex === material.itemNumber && x.quantity === material.quantity); if (rowIndex === -1) { this.selectedRows = this.rows.filter((x) => x.faIndex === material.itemNumber); if (!this.selectedRows.length) { this.showValidationDialog = true; return; } const validCombinations = findCombinations(this.selectedRows, material.quantity ?? 0); for (const combination of validCombinations) { for (const record of combination) { record.partNumberSl = scanned; record.palletNumber = palletNum; this.changedRecords.push(record); } } this.selectedRows = [...this.changedRecords]; this.selectedRow = this.selectedRows[0] ?? null; } else { this.selectedRow = this.rows[rowIndex]; this.selectedRow.partNumberSl = scanned; this.selectedRow.palletNumber = palletNum; if (!this.changedRecords.some((x) => x.transactionNumber === this.selectedRow!.transactionNumber)) { this.changedRecords.push(this.selectedRow); } } this.partNumberSl = this.selectedRow?.partNumberSl ?? ''; this.palletNumberOutput = String(this.selectedRow?.palletNumber ?? 0); this.itemNumber = this.selectedRow?.faIndex ?? ''; this.qty = String(this.selectedRow?.quantity ?? 0); await this.saveChanges(); this.changedRecords = []; setTimeout(() => this.scanner()?.nativeElement.focus()); } }