service-provider-app/src/app/carnet/shipping/fees-dialog.component.ts
2026-01-17 16:59:37 -04:00

116 lines
4.5 KiB
TypeScript

import { Component, inject, Inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AngularMaterialModule } from '../../shared/module/angular-material.module';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { CarnetService } from '../../core/services/carnet/carnet.service';
import { Fees } from '../../core/models/carnet/fee';
import { ApiErrorHandlerService } from '../../core/services/common/api-error-handler.service';
import { NotificationService } from '../../core/services/common/notification.service';
@Component({
selector: 'app-fees-dialog',
imports: [AngularMaterialModule, CommonModule, FormsModule],
template: `
<h3 mat-dialog-title>Fees and Charges</h3>
<mat-dialog-content>
<p> The following fees apply:</p>
<ul>
<li *ngIf="estimatedFees.basicFee">Basic fee: <span> {{estimatedFees.basicFee | currency}} </span></li>
<li *ngIf="estimatedFees.counterFoilFee">Counterfoil Fee: <span> {{estimatedFees.counterFoilFee | currency}}
</span></li>
<li *ngIf="estimatedFees.continuationSheetFee">Continuation sheet fee:
<span> {{estimatedFees.continuationSheetFee | currency}}</span></li>
<li *ngIf="estimatedFees.expeditedFee">Expedited fee: <span> {{estimatedFees.expeditedFee | currency}}</span></li>
<li *ngIf="estimatedFees.shippingFee">Shipping fee: <span> {{estimatedFees.shippingFee | currency}}</span></li>
<li *ngIf="estimatedFees.bondPremium">{{bondPremiumLabel}}: <span> {{estimatedFees.bondPremium | currency}}</span></li>
<li *ngIf="estimatedFees.cargoPremium">Cargo Premium: <span> {{estimatedFees.cargoPremium | currency}}</span></li>
<li *ngIf="estimatedFees.ldiPremium">LDI Premium: <span> {{estimatedFees.ldiPremium | currency}}</span></li>
</ul>
<hr/>
<ul>
<li class="total">Total: <span>{{total | currency}}</span></li>
</ul>
<hr/>
<ul>
<li *ngIf="estimatedFees.securityAmount">Security Amount: <span>{{estimatedFees.securityAmount | currency}}</span></li>
<li *ngIf="estimatedFees.insuredValue">Insured Value: <span>{{estimatedFees.insuredValue | currency}}</span></li>
</ul>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-raised-button color="primary" (click)="onClose()">
Ok
</button>
</mat-dialog-actions>
`,
styles: [`
li {
margin-bottom: 8px;
display: flex;
justify-content: space-between;
}
.total {
font-weight: bold;
margin-top: 12px;
}
`]
})
export class FeesDialogComponent {
estimatedFees: Fees = {};
headerid: number | null = null;
bondPremiumLabel: string = 'Bond Premium';
total: number = 0;
private carnetService = inject(CarnetService);
private notificationService = inject(NotificationService);
private errorHandler = inject(ApiErrorHandlerService);
constructor(
public dialogRef: MatDialogRef<FeesDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: { headerid: any }
) {
this.headerid = data.headerid;
}
ngOnInit(): void {
if (this.headerid && this.headerid > 0) {
this.carnetService.getEstimatedFees(this.headerid).subscribe({
next: (data: Fees) => {
if (data) {
this.estimatedFees = data;
if (this.estimatedFees.securityType === 'B') {
this.bondPremiumLabel = 'Bond Premium';
} else if (this.estimatedFees.securityType === 'C') {
this.bondPremiumLabel = 'Cash Deposit';
} else if (this.estimatedFees.securityType === 'G') {
this.bondPremiumLabel = 'Refundable claim deposit';
}
this.calculateTotal();
}
},
error: (error: any) => {
let errorMessage = this.errorHandler.handleApiError(error, 'Failed to get estimated fees');
this.notificationService.showError(errorMessage);
console.error('Error getting estimated fees:', error);
}
});
}
}
calculateTotal(): void {
const excludedProperties = ['securityAmount', 'insuredValue', 'securityType'];
const validFees = Object.entries(this.estimatedFees)
.filter(([key]) => !excludedProperties.includes(key))
.map(([, value]) => value)
.filter((fee): fee is number => typeof fee === 'number');
this.total = validFees.reduce((total, fee) => total + fee, 0);
}
onClose(): void {
this.dialogRef.close();
}
}