@ -6,6 +6,10 @@ import CTrafDataService from '../../Services/Corresponsalias/Corresponsales.Traf
import { MsgInformativo } from "../Utils/Toast/msgInformativo" ;
import { MsgInformativo } from "../Utils/Toast/msgInformativo" ;
import { FaRegFileExcel } from "react-icons/fa" ;
import { FaRegFileExcel } from "react-icons/fa" ;
import IReporteOperacionesNewell from "../../Interfaces/ReportesClientes/IReporteOperacionesNewell" ;
import IReporteOperacionesNewell from "../../Interfaces/ReportesClientes/IReporteOperacionesNewell" ;
import axios from "axios" ;
import { TargetURL } from "../../Constants/TargetURL" ;
import reportesNewellServices from "../../Services/Reportes/reportesNewell.services" ;
import FileManagerServices from "../../Services/Utils/FileManager.Services" ;
export const ReporteSemanalNewell : React.FC < { } > = ( ) = > {
export const ReporteSemanalNewell : React.FC < { } > = ( ) = > {
@ -17,6 +21,13 @@ export const ReporteSemanalNewell: React.FC<{}> = () => {
const [ msg , setMsg ] = useState ( '' )
const [ msg , setMsg ] = useState ( '' )
const [ msgColor , setMsgColor ] = useState ( 'primary' )
const [ msgColor , setMsgColor ] = useState ( 'primary' )
const [ File , setFile ] = useState < File | null > ( ) ;
const [ File , setFile ] = useState < File | null > ( ) ;
const URL = new TargetURL ( )
const [ UserId , setUserId ] = useState ( ( ) = > {
const stickyValue = window . localStorage . getItem ( 'UserId' )
return stickyValue !== null ? JSON . parse ( stickyValue ) : 0
} ) ;
const token = localStorage . getItem ( 'token' ) ;
const [ FileId , setFileId ] = useState ( 0 ) ;
function currentDate ( days : number ) : string {
function currentDate ( days : number ) : string {
var today = new Date ( )
var today = new Date ( )
@ -27,78 +38,57 @@ export const ReporteSemanalNewell: React.FC<{}> = () => {
return yyyy + '-' + mm + '-' + dd
return yyyy + '-' + mm + '-' + dd
}
}
const readFile = ( ) = > {
const uploadFile = ( file : File ) = > {
if ( ! File ) return ;
const formData = new FormData ( )
formData . append ( 'name' , file . name )
var ext = File ! . name . substr ( File ! . name . lastIndexOf ( '.' ) + 1 )
formData . append ( 'file' , file )
axios . post (
if ( ! ( ext === 'xls' ) && ! ( ext === 'xlsx' ) ) {
URL . get ( ) + ` /FileManager/AppendFileByProcess?IdUsuario= ${ UserId } &Proceso= ${ 42 } &Tags= ${ 0 } &crud=1 ` ,
formData , {
headers : {
'Authorization' : 'Bearer ' + token
}
}
)
. then ( ( res ) = > {
if ( res . data . id > 0 ) {
setFileId ( res . data . id ) ;
} else {
setHeader ( 'Error' )
setHeader ( 'Error' )
setMsg ( 'Seleccione un archivo valido.' )
setMsg ( 'No se guardo el archivo, favor de verificar que no este vacio ' )
setShowMsg ( true )
setShowMsg ( true )
return ;
return
}
const fileReader = new FileReader ( ) ;
fileReader . readAsArrayBuffer ( File ) ;
fileReader . onload = async ( e : any ) = > {
let encabezadosTemp = [ //Se crean los encabezados temporales que se usaran mas adelante.
[
'aduana' , 'patente' , 'pedimento' , 'fechaDeEntrada' , 'fechaDePago' ,
'valorComercialMxn' , 'valorComercialDlls' , 'valorAduana' , 'clave' , 'iva' ,
'dta' , 'prv' , 'lgi' , 'factura' , 'tc' , 'proveedor' , 'taxId' , 'producto' ,
'gc' , 'contenedor' , 'cuentaDeGastos' , 'razon' , 'maniobras'
]
]
const bufferArray = e ? . target . result
const oldWorkBook = XLSX . read ( bufferArray , { type : "buffer" } ) //Se lee el archivo
const oldWorkSheetName = oldWorkBook . SheetNames [ 0 ] //se toma el nombre de la primer hoja del archivo
const oldWorkSheet = oldWorkBook . Sheets [ oldWorkSheetName ] //Se crea una referencia a la misma hoja
XLSX . utils . sheet_add_aoa ( oldWorkSheet , encabezadosTemp , { origin : 'A1' } ) ; //Se cambian los encabezados originales por los temporales para que las columnas coincidan con las propiedades del dto IReporteSemanalNewell
const rows = XLSX . utils . sheet_to_json < IReporteOperacionesNewell > ( oldWorkSheet ) //aqui es donde los renglones pasan a ser un array de objetos
//Se obtienen los datos faltantes del reporte
let newWorkBookData = await getDataByPedimento ( rows ) ;
await exportReporteSemanal ( newWorkBookData ) ;
}
}
} )
. catch ( ( err ) = > alert ( 'File Upload Error ' + err ) )
}
}
const getDataByPedimento = async ( rows : IReporteOperacionesNewell [ ] ) = > {
const downloadReporte = ( ) = > {
let data = [ . . . rows ] ;
reportesNewellServices . getReporteSemanal ( FileId )
data = [ ] ;
. then ( resp = > {
await Promise . all (
const url = window . URL . createObjectURL ( new Blob ( [ resp . data ] ) ) ;
rows . map ( async ( row ) = > {
const link = document . createElement ( 'a' ) ;
await CTrafDataService . GetByPedimento ( row . pedimento ) . then ( ( response ) = > {
link . href = url ;
row . gc = response . data . gc ;
link . setAttribute ( 'download' , ` ReporteSemanal_ ${ Inicio } _ ${ Fin } .xlsx ` ) ;
row . contenedor = response . data . contenedor ;
document . body . appendChild ( link ) ;
row . cuentaDeGastos = response . data . cuentaDeGastos ;
link . click ( ) ;
row . razon = response . data . razon ;
} ) . catch ( e = > {
row . maniobras = response . data . maniobras ;
setHeader ( 'Error' )
} )
setMsg ( 'Ocurrio un error al generar el reporte. Por favor, intentelo de nuevo.' )
data . push ( row ) ;
setShowMsg ( true )
} )
return
)
data = data . sort ( ( a , b ) = > {
return + a . aduana - + b . aduana || + a . pedimento - + b . pedimento
} )
} )
return data ;
}
}
const exportReporteSemanal = async ( jsonData : any [ ] ) = > {
const deleteFile = ( ) = > {
let encabezados = [
FileManagerServices . DeleteFile ( FileId )
[
. then ( resp = > {
'Aduana' , 'Patente' , 'Pedimento' , 'Fecha de Entrada' , 'Fecha de Pago' ,
if ( resp . status === 200 && hiddenFileInputRef . current !== null ) {
'Valor Comercial Mxn' , 'Valor Comercial Dlls' , 'Valor Aduana' , 'Clave' , 'Iva' ,
hiddenFileInputRef . current . value = "" ;
'Dta' , 'Prv' , 'lgi' , 'Factura' , 'TC.' , 'Proveedor' , 'Tax Id' , 'Producto' ,
setFile ( null ) ;
'GC' , 'Contenedor' , 'Cuenta de Gastos' , 'Razon' , 'Maniobras'
setFileId ( 0 )
]
}
]
} )
const newWorkBook = XLSX . utils . book_new ( )
const newWorkBookSheet : XLSX.WorkSheet = XLSX . utils . json_to_sheet ( [ ] )
XLSX . utils . sheet_add_aoa ( newWorkBookSheet , encabezados , { origin : 'A1' } )
XLSX . utils . sheet_add_json ( newWorkBookSheet , jsonData , { origin : 'A2' , skipHeader : true } )
XLSX . utils . book_append_sheet ( newWorkBook , newWorkBookSheet , 'Sheet1' )
XLSX . writeFile ( newWorkBook , ` Reporte_Semanal- ${ Inicio } _ ${ Fin } .xlsx ` ) ;
}
}
return (
return (
@ -155,12 +145,13 @@ export const ReporteSemanalNewell: React.FC<{}> = () => {
>
>
< input type = "file" ref = { hiddenFileInputRef } style = { { display : 'none' } }
< input type = "file" ref = { hiddenFileInputRef } style = { { display : 'none' } }
onChange = { ( e :any ) = > {
onChange = { ( e :any ) = > {
uploadFile ( e . target . files [ 0 ] ) ;
setFile ( e . target . files [ 0 ] ) ;
setFile ( e . target . files [ 0 ] ) ;
} }
} }
/ >
/ >
{
{
! File ? < >
FileId === 0 ? < >
< MdCloudUpload style = { { fontSize : '60px' , color : '#1475cf' } } / >
< MdCloudUpload style = { { fontSize : '60px' , color : '#1475cf' } } / >
< span style = { { display : 'block' , paddingTop : '10px' } } > Seleccione un archivo . . . < / span >
< span style = { { display : 'block' , paddingTop : '10px' } } > Seleccione un archivo . . . < / span >
< / >
< / >
@ -173,15 +164,10 @@ export const ReporteSemanalNewell: React.FC<{}> = () => {
< / Row >
< / Row >
< / Card.Body >
< / Card.Body >
< Card.Footer style = { { textAlign : 'end' } } >
< Card.Footer style = { { textAlign : 'end' } } >
< Button variant = "danger" onClick = { ( ) = > {
< Button variant = "danger" onClick = { deleteFile } title = "Quitar archivo cargado" style = { { marginRight : '10px' } } >
if ( hiddenFileInputRef . current !== null ) {
hiddenFileInputRef . current . value = "" ;
setFile ( null ) ;
}
} } title = "Quitar archivo cargado" style = { { marginRight : '10px' } } >
Borrar < MdDeleteForever style = { { marginLeft : '5px' , fontSize : '20px' } } / >
Borrar < MdDeleteForever style = { { marginLeft : '5px' , fontSize : '20px' } } / >
< / Button >
< / Button >
< Button className = "primary" onClick = { readFil e} title = "Descargar reporte" >
< Button className = "primary" onClick = { downloadReporte } title = "Descargar reporte" >
Generar < MdCloudDownload style = { { marginLeft : '5px' } } / >
Generar < MdCloudDownload style = { { marginLeft : '5px' } } / >
< / Button >
< / Button >
< / Card.Footer >
< / Card.Footer >