File

src/app/components/layout/pages/historial-venta/historial-venta.component.ts

Implements

OnInit AfterViewInit

Metadata

Index

Properties
Methods

Constructor

constructor(_ventaServ: VentaService, _utilidadServ: UtilidadService, fb: FormBuilder, dialog: MatDialog)
Parameters :
Name Type Optional
_ventaServ VentaService No
_utilidadServ UtilidadService No
fb FormBuilder No
dialog MatDialog No

Methods

aplicarFiltroTabla
aplicarFiltroTabla(event: Event)
Parameters :
Name Type Optional
event Event No
Returns : void
buscarVentas
buscarVentas()
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnInit
ngOnInit()
Returns : void
verDetalleVenta
verDetalleVenta(venta: Venta)
Parameters :
Name Type Optional
venta Venta No
Returns : void

Properties

columnasTabla
Type : string[]
Default value : ['fechaRegistro', 'numeroDocumento', 'tipoPago', 'total', 'accion']
dataInicio
Type : Venta[]
Default value : []
datosListaVentas
Default value : new MatTableDataSource(this.dataInicio)
formularioBusqueda
Type : FormGroup
opcionesBusqueda
Type : any[]
Default value : [ { value: 'fecha', descripcion: 'Por fechas' }, { value: 'numero', descripcion: 'Número de venta' }, ]
paginacionTabla
Type : MatPaginator
Decorators :
@ViewChild(MatPaginator)
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MAT_DATE_FORMATS } from '@angular/material/core';
import { MatDialog } from '@angular/material/dialog';
import * as moment from 'moment';
import { ModalHistorialVentasComponent } from '../../modales/modal-historial-ventas/modal-historial-ventas.component';
import { VentaService } from '../../../../services/venta.service';
import { UtilidadService } from '../../../../shared/utilidad.service';
import { Venta } from '../../../../interfaces/venta';

export const MY_DATA_FORMATS = {
  parse: {
    dateInput: 'DD/MMMM/YYYY'
  },
  display: {
    dateInput: 'DD/MMMM/YYYY',
    monthYearLabel: 'MMMM YYYY'
  }
}

@Component({
  selector: 'app-historial-venta',
  templateUrl: './historial-venta.component.html',
  styleUrls: ['./historial-venta.component.css'],
  providers: [
    {provide: MAT_DATE_FORMATS, useValue: MY_DATA_FORMATS}
  ]
})
export class HistorialVentaComponent implements OnInit, AfterViewInit{

  formularioBusqueda: FormGroup;
  opcionesBusqueda: any[] = [
    { value: 'fecha', descripcion: 'Por fechas' },
    { value: 'numero', descripcion: 'Número de venta' },
  ];
  columnasTabla: string[] = ['fechaRegistro', 'numeroDocumento', 'tipoPago', 'total', 'accion'];
  dataInicio: Venta[] = [];
  datosListaVentas = new MatTableDataSource(this.dataInicio);
  @ViewChild(MatPaginator) paginacionTabla! : MatPaginator;


  constructor(
    private _ventaServ: VentaService, 
    private _utilidadServ: UtilidadService,
    private fb: FormBuilder,
    private dialog: MatDialog){

      this.formularioBusqueda = this.fb.group({
        buscarPor: ['fecha'],
        numero: [''],
        fechaInicio: [''],
        fechaFin: ['']
      });

      this.formularioBusqueda.get('buscarPor')?.valueChanges.subscribe(value => {
        this.formularioBusqueda.patchValue({
          numero: '',
          fechaInicio: '',
          fechaFin: ''
        })
      });

    }
  
  ngOnInit(): void { }
  
  ngAfterViewInit(): void {
    this.datosListaVentas.paginator = this.paginacionTabla;
  }

  aplicarFiltroTabla(event: Event){
    const filterValue = (event.target as HTMLInputElement).value;
    this.datosListaVentas.filter = filterValue.trim().toLowerCase();
  }

  buscarVentas(){
    let _fechaInicio: string = '';
    let _fechaFin: string = '';

    if(this.formularioBusqueda.value.buscarPor === 'fecha'){
      _fechaInicio = moment(this.formularioBusqueda.value.fechaInicio).format('DD/MM/YYYY');
      _fechaFin = moment(this.formularioBusqueda.value.fechaFin).format('DD/MM/YYYY');
      if(_fechaInicio === 'Invalid date' || _fechaFin === 'Invalid date'){
        this._utilidadServ.mostrarAlerta('Debes ingresar una fecha de inicio y una fecha de fin.', 'Opps ❌');
  
        return;
      }
    }

    this._ventaServ.historial(this.formularioBusqueda.value.buscarPor, this.formularioBusqueda.value.numero, _fechaInicio, _fechaFin).subscribe({
      next: (data) => {
        if(data.status){
          this.datosListaVentas.data = data.value;
        }else{
          this.formularioBusqueda.value.buscarPor,
          this._utilidadServ.mostrarAlerta('No se encontraron datos. Por favor intente de nuevo.', 'Opps ❌');
        }
      },
      error: (err) => {
        this._utilidadServ.mostrarAlerta(`Parece que ocurrio un error inesperado: ${err.message}`, 'Opps ❌');
      },
    });
  }

  verDetalleVenta(venta: Venta){
    this.dialog.open(ModalHistorialVentasComponent, {
      data: venta,
      disableClose: true,
      width: '800px'
    });
  }

}
<h1 style="margin: 20px 0px 0px 40px;" class="animate__animated animate__fadeIn">Historial de Ventas</h1>
<mat-card style="margin: 20px 40px 40px 40px;" class="animate__animated animate__fadeIn">
    <mat-card-content>
        <form [formGroup]="formularioBusqueda" (ngSubmit)="buscarVentas()">
            <div class="campos-formulario">
                <mat-form-field appearance="fill">
                    <mat-label>Buscar por</mat-label>
                    <mat-select formControlName="buscarPor">
                        <mat-option *ngFor="let item of opcionesBusqueda" [value]="item.value">{{item.descripcion}}</mat-option>
                    </mat-select>
                </mat-form-field>
                
                <mat-form-field appearance="fill" *ngIf=" formularioBusqueda.value.buscarPor == 'numero' ">
                    <mat-label>Número Orden</mat-label>
                    <input matInput autocomplete="off" formControlName="numero">
                </mat-form-field>
                
                <mat-form-field appearance="fill" *ngIf=" formularioBusqueda.value.buscarPor == 'fecha' ">
                    <mat-label>Fecha de Inicio</mat-label>
                    <input matInput autocomplete="off" formControlName="fechaInicio" [matDatepicker]="picker1">
                    <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
                    <mat-datepicker #picker1></mat-datepicker>
                </mat-form-field>

                <mat-form-field appearance="fill" *ngIf="formularioBusqueda.value.buscarPor == 'fecha'">
                    <mat-label>Fecha de Fin</mat-label>
                    <input matInput autocomplete="off" formControlName="fechaFin" [matDatepicker]="picker2">
                    <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
                    <mat-datepicker #picker2></mat-datepicker>
                </mat-form-field>
                <button mat-raised-button color="primary" type="submit" class="boton-buscar" [disabled]="formularioBusqueda.invalid">
                    <mat-icon>search</mat-icon>Buscar Ventas
                </button>
            </div>
        </form>
        <mat-divider></mat-divider>
        <mat-form-field appearance="outline" class="input-filter">
            <mat-label>Buscar</mat-label>
            <input matInput (keyup)="aplicarFiltroTabla($event)" #input>
        </mat-form-field>
        <div class="mat-elevation-z2">
            <table mat-table [dataSource]="datosListaVentas">
                <ng-container matColumnDef="fechaRegistro">
                    <th mat-header-cell *matHeaderCellDef>Fecha de registro</th>
                    <td mat-cell *matCellDef="let element">{{element.fechaRegistro}}</td>
                </ng-container>
                <ng-container matColumnDef="numeroDocumento">
                    <th mat-header-cell *matHeaderCellDef>Número de Venta</th>
                    <td mat-cell *matCellDef="let element">{{element.numeroDocumento}}</td>
                </ng-container>
                <ng-container matColumnDef="tipoPago">
                    <th mat-header-cell *matHeaderCellDef>Tipo de Pago</th>
                    <td mat-cell *matCellDef="let element">{{element.tipoPago}}</td>
                </ng-container>
                <ng-container matColumnDef="total">
                    <th mat-header-cell *matHeaderCellDef>Total</th>
                    <td mat-cell *matCellDef="let element">{{element.totalTexto}}</td>
                </ng-container>
                <ng-container matColumnDef="accion">
                    <th mat-header-cell *matHeaderCellDef style="width: 80px;"></th>
                    <td mat-cell *matCellDef="let element">
                        <button matTooltip="Ver detalles" mat-icon-button color="primary" (click)="verDetalleVenta(element)">
                            <mat-icon>visibility</mat-icon>
                        </button>
                    </td>
                </ng-container>
                <tr mat-header-row *matHeaderRowDef="columnasTabla"></tr>
                <tr mat-row *matRowDef="let row; columns: columnasTabla;"></tr>
                <tr class="mat-row" *matNoDataRow>
                    <td class="mat-cell" colspan="4">No hay datos que coincidan con el filtro "{{input.value}}"</td>
                </tr>
            </table>
            <mat-paginator [pageSizeOptions]="[5, 10, 15, 20]" showFirstLastButtons></mat-paginator>
        </div>
    </mat-card-content>
</mat-card>

./historial-venta.component.css

campos-formulario{
    display: flex;
    align-items: flex-start;
}

boton-buscar{
    width: 150px;
    margin-top: 15px;
}

mat-form-field{
    margin-right: 10px;
}

.input-filter{
    width: 100%;
    font-size: 14px;
}

table{
    width: 100%;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""