@@ -3,12 +3,15 @@ import {animate, state, style, transition, trigger} from '@angular/animations';
33import { MatTable } from '@angular/material/table' ;
44import { MatDialog , MAT_DIALOG_DATA } from '@angular/material/dialog' ;
55import { BarcodeScanner } from '@awesome-cordova-plugins/barcode-scanner/ngx' ;
6+ import { HttpClient } from '@angular/common/http' ;
67
78export interface InvoiceElement {
89 docName : string ;
910 docNumber : string ;
1011 cuitEmi : string ;
1112 cuitRec : string ;
13+ nameEmi : string ;
14+ nameRec : string ;
1215 date : string ;
1316 amount : number ;
1417 currency : string ;
@@ -33,7 +36,7 @@ export class AfipComponent implements OnInit {
3336 expandedElement : InvoiceElement | null | undefined ;
3437 @ViewChild ( MatTable ) table : MatTable < InvoiceElement > | undefined ;
3538
36- constructor ( private barcodeScanner : BarcodeScanner , public dialog : MatDialog ) { }
39+ constructor ( private barcodeScanner : BarcodeScanner , public dialog : MatDialog , private http : HttpClient ) { }
3740
3841 ngOnInit ( ) : void {
3942 let inv = localStorage . getItem ( 'invoices' ) ;
@@ -66,20 +69,34 @@ export class AfipComponent implements OnInit {
6669 localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
6770 }
6871
69- processQR ( t : string ) {
72+ async processQR ( t : string ) {
7073 const url = new URL ( t ) ;
7174 const p : any = url . searchParams . get ( 'p' ) ;
7275 let encode = p . replace ( / b \' ( .* ) \' / , '$1' ) . replace ( / \n / g, '' ) ;
7376 const decode = decodeURIComponent ( escape ( atob ( encode ) ) ) . replace ( / \' / g, '"' ) ;
7477 const obj = JSON . parse ( decode ) ;
7578 console . log ( obj ) ;
7679 if ( url . host === 'www.afip.gob.ar' || url . host === 'afip.gob.ar' ) {
77- this . invoices . push ( this . processQrFeAr ( obj ) ) ;
78- localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
79- console . log ( this . invoices ) ;
80- if ( this . table ) {
81- this . table ! . renderRows ( ) ;
82- }
80+ let newinvoice = await this . processQrFeAr ( obj ) ;
81+ this . http . get < any > ( 'https://afip.tangofactura.com/Rest/GetContribuyenteFull?cuit=' + obj . cuit , { } ) . subscribe ( dataE => {
82+ if ( dataE . Contribuyente ) {
83+ newinvoice . nameEmi = dataE . Contribuyente . nombre ;
84+ this . http . get < any > ( 'https://afip.tangofactura.com/Rest/GetContribuyenteFull?cuit=' + obj . nroDocRec , { } ) . subscribe ( dataR => {
85+ if ( dataR . Contribuyente ) {
86+ newinvoice . nameRec = dataR . Contribuyente . nombre ;
87+ this . invoices . push ( newinvoice ) ;
88+ localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
89+ if ( this . table ) {
90+ this . table ! . renderRows ( ) ;
91+ }
92+ }
93+ } , ( err : any ) => {
94+ this . openDialog ( err ) ;
95+ } ) ;
96+ }
97+ } , ( err : any ) => {
98+ this . openDialog ( err ) ;
99+ } )
83100 } else {
84101 this . openDialog ( 'El QR escaneado no es de AFIP' ) ;
85102 }
@@ -148,12 +165,16 @@ export class AfipComponent implements OnInit {
148165
149166 let pdv = obj . ptoVta . toLocaleString ( 'es-AR' , { minimumIntegerDigits : 5 , useGrouping : false } ) ;
150167 let num = obj . nroCmp . toLocaleString ( 'es-AR' , { minimumIntegerDigits : 8 , useGrouping : false } ) ;
168+ let nameEmi = '' ;
169+ let nameRec = '' ;
151170
152171 return {
153172 docName : comp ,
154173 docNumber : pdv + '-' + num ,
155174 cuitEmi : obj . cuit ,
156175 cuitRec : obj . nroDocRec ,
176+ nameEmi : '' ,
177+ nameRec : '' ,
157178 date : obj . fecha ,
158179 amount : obj . importe ,
159180 currency : obj . moneda
@@ -163,10 +184,10 @@ export class AfipComponent implements OnInit {
163184
164185 exportToCSV ( ) : void {
165186 let csvContent = 'data:text/csv;charset=utf-8,' ;
166- csvContent += 'Tipo,Nro,Emisor,Receptor,Fecha,Importe,Moneda\r\n' ;
187+ csvContent += 'Tipo,Nro,Emisor,Emisor CUIT, Receptor,Receptor CUIT, Fecha,Importe,Importe sin IVA 21 ,Moneda\r\n' ;
167188
168189 this . invoices . forEach ( ( row ) => {
169- csvContent += row . docName + ',' + row . docNumber + ',' + row . cuitEmi + ',' + row . cuitRec + ',' + row . date + ',' + row . amount + ',' + row . currency + '\r\n' ;
190+ csvContent += row . docName + ',' + row . docNumber + ',' + row . nameEmi + ',' + row . cuitEmi + ',' + row . nameRec + ',' + row . cuitRec + ',' + row . date + ',' + row . amount + ',' + Number ( row . amount ) / 1.21 + ',' + row . currency + '\r\n' ;
170191 } ) ;
171192
172193 let date = new Date ( )
@@ -177,7 +198,7 @@ export class AfipComponent implements OnInit {
177198 const encodedUri = encodeURI ( csvContent ) ;
178199 const link = document . createElement ( 'a' ) ;
179200 link . setAttribute ( 'href' , encodedUri ) ;
180- link . setAttribute ( 'download' , 'invoices_afip_' + `${ day } ${ month } ${ year } ` + '.csv' ) ;
201+ link . setAttribute ( 'download' , 'invoices_afip_' + `${ year } ${ month } ${ day } ` + '.csv' ) ;
181202 document . body . appendChild ( link ) ;
182203 link . click ( ) ;
183204 }
0 commit comments