Skip to content

Commit

Permalink
Duplicate code fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rosmarielnunez committed Oct 10, 2024
1 parent 38832cf commit eca22b2
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 177 deletions.
9 changes: 7 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ import { FacturaCompraComponent } from './components/factura-compra/factura-comp
import { NuevoRenglonFacturaModalComponent } from './components/nuevo-renglon-factura-modal/nuevo-renglon-factura-modal.component';
import { ConfiguracionComponent } from './components/configuracion/configuracion.component';
import { NgChartsModule } from 'ng2-charts';
import { FormsModule } from '@angular/forms';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { ChartStatisticsMonthComponent } from './components/chart-statistics-month/chart-statistics-month.component';
import { ChartStatisticsMonthSupplierComponent } from './components/chart-statistics-month-supplier/chart-statistics-month-supplier.component';
import { ChartStatisticsYearComponent } from './components/chart-statistics-year/chart-statistics-year.component';
import { ChartStatisticsYearSupplierComponent } from './components/chart-statistics-year-supplier/chart-statistics-year-supplier.component';
import { ChartTableSupplierComponent } from './components/chart-table-supplier/chart-table-supplier.component';



registerLocaleData(localeEsAR, 'es-AR', localeEsARExtra);
Expand Down Expand Up @@ -193,7 +196,8 @@ registerLocaleData(localeEsAR, 'es-AR', localeEsARExtra);
ChartStatisticsMonthComponent,
ChartStatisticsMonthSupplierComponent,
ChartStatisticsYearComponent,
ChartStatisticsYearSupplierComponent
ChartStatisticsYearSupplierComponent,
ChartTableSupplierComponent
],
imports: [
BrowserModule,
Expand All @@ -202,7 +206,8 @@ registerLocaleData(localeEsAR, 'es-AR', localeEsARExtra);
NgSelectModule,
AppRoutingModule,
ShareModule,
NgChartsModule
NgChartsModule,
FormsModule
],
providers: [
{ provide: LOCALE_ID, useValue: 'es-AR' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
<section class="container">
<span>{{ title }}</span>
<div class="styleSelect">
<select name="year" class="form-select form-select-sm" (change)="onYearChange($event)">
<option *ngFor="let year of years" [value]="year">{{year}}</option>
</select>
<select name="month" class="form-select form-select-sm" [(ngModel)]="selectedMonth"
(change)="OnMonthChange($event)">
<option *ngFor="let month of months" [value]="month.value">{{month.name}}</option>
</select>
</div>
<div *ngIf="suppliers.length === 0" class="alert alert-secondary mt-2 text-center">
No hay información disponible para este periodo. Seleccione un mes diferente
</div>
<div class="table-responsive custom-table-responsive" *ngIf="suppliers.length > 0">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Proveedor</th>
<th scope="col">Monto $</th>
</tr>
</thead>
<tbody class="table-group-divider">
<tr *ngFor="let supplier of suppliers; let i = index">
<td scope="row">{{i + 1}}</td>
<td>{{supplier.entidad}}</td>
<td>{{supplier.monto | currency:'ARS':'symbol':'1.0-2'}}</td>
</tr>
</tbody>
</table>
</div>
</section>
<app-chart-table-supplier
[title]="title"
[years]="years"
[months]="months"
[suppliers]="suppliers"
[selectedYear]="selectedYear"
[selectedMonth]="selectedMonth"
[showMonthSelector]="true"
(yearChange)="onYearChange($event)"
(monthChange)="onMonthChange($event)">
</app-chart-table-supplier>

Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,33 @@ import { Component, Input } from '@angular/core';
import { ChartDirectiveDirective } from 'src/app/directives/chart-directive.directive';
import { ChartService } from 'src/app/services/chart.service';


@Component({
selector: 'app-chart-statistics-month-supplier',
templateUrl: './chart-statistics-month-supplier.component.html',
styleUrls: ['./chart-statistics-month-supplier.component.scss']
templateUrl: './chart-statistics-month-supplier.component.html'
})

export class ChartStatisticsMonthSupplierComponent extends ChartDirectiveDirective {
@Input() title: string;
@Input() dataType: 'compras' | 'ventas';
@Input() title: string = '';

constructor(protected chartData: ChartService) {
super(chartData);
}

loadChartData(year: number, month: number): void {
let chartDataPurchaseSale;

if (this.dataType === 'compras') {
chartDataPurchaseSale = this.chartData.getChartDataPurchaseMonthSupplier(year, month);
} else if (this.dataType === 'ventas') {
chartDataPurchaseSale = this.chartData.getChartDataSalesMonthSupplier(year, month);
}
const chartDataPurchaseSale = this.dataType === 'compras' ?
this.chartData.getChartDataPurchaseMonthSupplier(year, month) :
this.chartData.getChartDataSalesMonthSupplier(year, month);

chartDataPurchaseSale.subscribe({
next: (data) => {
if (data && data.labels && data.datasets && data.datasets.length > 0) {
const labels = data.labels;
const datasetData = data.datasets[0].data;

if (labels.length === datasetData.length) {
this.suppliers = labels.map((label, index) => ({
entidad: label,
monto: datasetData[index],
}));
this.updateChart(data, labels);
} else {
console.log('Desajuste en la longitud de los datos');
this.suppliers = [];
}
}
},
error: (err) => {
console.log('Error al cargar los datos', err);
this.suppliers = [];
}
})
next: (data) => this.handleChartData(data),
error: (err) => console.log('Error al cargar datos',err),
});
}

isMonthOptional(): boolean {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ export class ChartStatisticsMonthComponent extends ChartDirectiveDirective {
}
}

// onYearChange(year: string): void {
// this.selectedYear = parseInt(year, 10);
// this.loadInitialChartData();
// }

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
<section class="container">
<span>{{ title }}</span>
<select name="yearSupplier" class="form-select form-select-sm ng-pristine ng-invalid ng-touched"
(change)="onYearChange($event)">
<option *ngFor="let year of years" [value]="year">{{year}}</option>
</select>
<div *ngIf="suppliers.length === 0" class="alert alert-secondary mt-2 text-center">
No hay información disponible para este periodo. Seleccione un mes diferente
</div>
<div class="table-responsive custom-table-responsive" *ngIf="suppliers.length > 0">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Proveedor</th>
<th scope="col">Monto $</th>
</tr>
</thead>
<tbody class="table-group-divider">
<tr *ngFor="let supplier of suppliers; let i = index">
<td scope="row">{{i + 1}}</td>
<td>{{supplier.entidad}}</td>
<td>{{supplier.monto | currency:'ARS':'symbol':'1.0-2'}}</td>
</tr>
</tbody>
</table>
</div>
</section>
<app-chart-table-supplier
[dataType]="dataType"
[title]="title"
[years]="years"
[suppliers]="suppliers"
[showMonthSelector]="false"
(change)="onYearChange($event)">
</app-chart-table-supplier>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
import { Component, Input, OnInit } from '@angular/core';
import { Component, Input } from '@angular/core';
import { ChartDirectiveDirective } from 'src/app/directives/chart-directive.directive';
import { ChartService } from 'src/app/services/chart.service';

@Component({
selector: 'app-chart-statistics-year-supplier',
templateUrl: './chart-statistics-year-supplier.component.html',
styleUrls: ['./chart-statistics-year-supplier.component.scss']
templateUrl: './chart-statistics-year-supplier.component.html'
})
export class ChartStatisticsYearSupplierComponent extends ChartDirectiveDirective {
@Input() title: string;
@Input() dataType: 'compras' | 'ventas';
@Input() title: string = '';

constructor(protected chartData: ChartService) {
super(chartData);
}
}

loadChartData(year: number): void {
let chartDataPurchaseSaleSupplier;
const chartDataPurchaseSaleSupplier = this.dataType === 'compras' ?
this.chartData.getChartDataPurchaseAnnualSupplier(year) :
this.chartData.getChartDataSalesAnnualSupplier(year);

if (this.dataType === 'compras') {
chartDataPurchaseSaleSupplier = this.chartData.getChartDataPurchaseAnnualSupplier(year)
} else if (this.dataType === 'ventas') {
chartDataPurchaseSaleSupplier = this.chartData.getChartDataSalesAnnualSupplier(year)
}
chartDataPurchaseSaleSupplier.subscribe({
next: (data) => {
if (data && data.labels && data.datasets && data.datasets.length > 0) {
const labels = data.labels;
const datasetData = data.datasets[0].data;
if (labels.length === datasetData.length) {
this.suppliers = labels.map((label, index) => ({
entidad: label,
monto: datasetData[index],
}));
this.updateChart(data, labels);
} else {
console.log('Desajuste en la longitud de los datos');
this.suppliers = [];
}
}
},
error: (error) => {
console.error('Error al cargar los datos', error);
}
next: (data) => this.handleChartData(data),
error: (err) => console.log('Error al cargar datos', err),
})
}

isMonthOptional(): boolean {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<section class="container">
<span>{{ title }}</span>
<div class="styleSelect">
<select name="year" class="form-select form-select-sm" (change)="onYearChange($event)">
<option *ngFor="let year of years" [value]="year">{{year}}</option>
</select>
<select *ngIf="showMonthSelector" name="month" class="form-select form-select-sm"
[(ngModel)]="selectedMonth" (change)="onMonthChange($event)">
<option *ngFor="let month of months" [value]="month.value">{{month.name}}</option>
</select>
</div>
<div *ngIf="suppliers.length === 0" class="alert alert-secondary mt-2 text-center">
No hay información disponible para este periodo. Seleccione otro periodo
</div>
<div *ngIf="suppliers.length > 0" class="table-responsive custom-table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Proveedor</th>
<th scope="col">Monto $</th>
</tr>
</thead>
<tbody class="table-group-divider">
<tr *ngFor="let supplier of suppliers; let i = index">
<td scope="row">{{i + 1}}</td>
<td>{{supplier.entidad}}</td>
<td>{{supplier.monto | currency:'ARS':'symbol':'1.0-2'}}</td>
</tr>
</tbody>
</table>
</div>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ChartInterface } from 'src/app/models/chart-interface';


@Component({
selector: 'app-chart-table-supplier',
templateUrl: './chart-table-supplier.component.html',
styleUrls: ['./chart-table-supplier.component.scss']
})
export class ChartTableSupplierComponent {
@Input() title: string = '';
@Input() suppliers: ChartInterface[] = [];
@Input() years: number[] = [];
@Input() months: {value: number, name: string}[] = [];
@Input() selectedYear: number | null = null;
@Input() selectedMonth: number | null = null;
@Input() showMonthSelector: boolean = true;

@Output() yearChange = new EventEmitter<number>();
@Output() monthChange = new EventEmitter<number>();

onYearChange($event: Event): void {
const target = $event.target as HTMLSelectElement;
if (target && target.value) {
const year = parseInt(target.value, 10);
this.yearChange.emit(year);
} else {
console.error("Error: El evento no tiene un valor válido", $event);
}
}

onMonthChange($event: Event): void {
const target = $event.target as HTMLSelectElement;
if (target && target.value) {
const month = parseInt(target.value, 10);
this.monthChange.emit(month);
} else {
console.error("Error: El evento no tiene un valor válido", $event);
}
}

}
Loading

0 comments on commit eca22b2

Please sign in to comment.