|
|
@@ -0,0 +1,102 @@
|
|
|
+import { Component, Inject } from '@angular/core';
|
|
|
+import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-cost-analysis-modal',
|
|
|
+ templateUrl: './cost-analysis-modal.component.html',
|
|
|
+ styleUrl: './cost-analysis-modal.component.css',
|
|
|
+ standalone: false
|
|
|
+})
|
|
|
+export class CostAnalysisModalComponent {
|
|
|
+ comments: string = '';
|
|
|
+ costData: any = null;
|
|
|
+ currencies = ['MXN', 'USD'];
|
|
|
+ userCosts: { [key: string]: { amount: number, currency: string } } = {};
|
|
|
+ totals: { [key: string]: number } = {};
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ public dialogRef: MatDialogRef<CostAnalysisModalComponent>,
|
|
|
+ @Inject(MAT_DIALOG_DATA) public data: { idOrder: string, costData?: any }
|
|
|
+ ) {
|
|
|
+ this.costData = data.costData;
|
|
|
+ this.initializeUserCosts();
|
|
|
+ this.calculateTotals();
|
|
|
+ }
|
|
|
+
|
|
|
+ initializeUserCosts(): void {
|
|
|
+ if (this.costData?.USUARIOS) {
|
|
|
+ this.costData.USUARIOS.forEach((user: any) => {
|
|
|
+ this.userCosts[user.ID] = { amount: user.COSTO || 0, currency: 'MXN' };
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ calculateTotals(): void {
|
|
|
+ this.totals = {};
|
|
|
+
|
|
|
+ // Add tools subtotals
|
|
|
+ if (this.costData?.SUBTOTAL_HERRAMIENTAS) {
|
|
|
+ this.costData.SUBTOTAL_HERRAMIENTAS.forEach((item: any) => {
|
|
|
+ this.totals[item.SIMBOLO] = (this.totals[item.SIMBOLO] || 0) + item.SUBTOTAL;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add parts subtotals
|
|
|
+ if (this.costData?.SUBTOTAL_REFACCIONES) {
|
|
|
+ this.costData.SUBTOTAL_REFACCIONES.forEach((item: any) => {
|
|
|
+ this.totals[item.SIMBOLO] = (this.totals[item.SIMBOLO] || 0) + item.SUBTOTAL;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add user costs
|
|
|
+ Object.values(this.userCosts).forEach((userCost: any) => {
|
|
|
+ this.totals[userCost.currency] = (this.totals[userCost.currency] || 0) + userCost.amount;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ onUserCostChange(): void {
|
|
|
+ this.calculateTotals();
|
|
|
+ }
|
|
|
+
|
|
|
+ onCancel(): void {
|
|
|
+ this.dialogRef.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ onSave(): void {
|
|
|
+ const payload = this.prepareSavePayload();
|
|
|
+ this.dialogRef.close({ payload, comments: this.comments, userCosts: this.userCosts });
|
|
|
+ }
|
|
|
+
|
|
|
+ prepareSavePayload(): any {
|
|
|
+ const userSubtotals: { [key: string]: number } = {};
|
|
|
+ const usuarios = this.costData?.USUARIOS?.map((user: any) => ({
|
|
|
+ ID: user.ID,
|
|
|
+ COSTO: this.userCosts[user.ID]?.amount || 0,
|
|
|
+ MONEDA: this.userCosts[user.ID]?.currency || 'MXN'
|
|
|
+ })) || [];
|
|
|
+
|
|
|
+ // Calculate user subtotals by currency
|
|
|
+ Object.values(this.userCosts).forEach((userCost: any) => {
|
|
|
+ userSubtotals[userCost.currency] = (userSubtotals[userCost.currency] || 0) + userCost.amount;
|
|
|
+ });
|
|
|
+
|
|
|
+ const subtotalUsuarios = Object.keys(userSubtotals).map(currency => ({
|
|
|
+ SIMBOLO: currency,
|
|
|
+ SUBTOTAL: userSubtotals[currency]
|
|
|
+ }));
|
|
|
+
|
|
|
+ return {
|
|
|
+ COMMENTS: this.comments,
|
|
|
+ USUARIOS: usuarios,
|
|
|
+ HERRAMIENTAS: this.costData?.HERRAMIENTAS || [],
|
|
|
+ REFACCIONES: this.costData?.REFACCIONES || [],
|
|
|
+ SUBTOTAL_USUARIOS: subtotalUsuarios,
|
|
|
+ SUBTOTAL_HERRAMIENTAS: this.costData?.SUBTOTAL_HERRAMIENTAS || [],
|
|
|
+ SUBTOTAL_REFACCIONES: this.costData?.SUBTOTAL_REFACCIONES || [],
|
|
|
+ TOTAL: Object.keys(this.totals).map(currency => ({
|
|
|
+ SIMBOLO: currency,
|
|
|
+ TOTAL: this.totals[currency]
|
|
|
+ }))
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|