1- import { Component , OnInit , ViewChild , Inject } from '@angular/core' ;
1+ import { Component , OnInit , DoCheck , ViewChild , Inject } from '@angular/core' ;
22import { animate , state , style , transition , trigger } from '@angular/animations' ;
33import { MatTable } from '@angular/material/table' ;
44import { MatDialog , MAT_DIALOG_DATA } from '@angular/material/dialog' ;
@@ -55,42 +55,50 @@ export class AfipComponent implements OnInit {
5555 }
5656 }
5757
58+ ngDoCheck ( ) : void { }
59+
5860 scanQR ( ) {
59- this . barcodeScanner . scan ( { formats : 'QR_CODE' } )
61+ this . barcodeScanner . scan ( {
62+ formats : 'QR_CODE' ,
63+ prompt : 'Coloque un Código QR de AFIP en el Escaner.' ,
64+ resultDisplayDuration : 0
65+ } )
6066 . then ( barcodeData => {
61- this . processQR ( barcodeData . text ) ;
67+ if ( barcodeData . cancelled === false ) {
68+ this . processQR ( barcodeData . text ) ;
69+ }
6270 } ) . catch ( err => {
6371 alert ( err ) ;
6472 } ) ;
6573 }
6674
67- test ( ) {
75+ async test ( ) {
6876 // FA-C
69- const urlText = "https://www.afip.gob.ar/fe/qr/?p=eyJ2ZXIiOjEsImZlY2hhIjoiMjAyMi0wMS0yNSIsImN1aXQiOjIwMzY3MzYyNDczLCJwdG9WdGEiOjIsInRpcG9DbXAiOjExLCJucm9DbXAiOjg4LCJpbXBvcnRlIjoyMDAwLCJtb25lZGEiOiJQRVMiLCJjdHoiOjEsInRpcG9Eb2NSZWMiOjgwLCJucm9Eb2NSZWMiOjMwNzE2NzQzMjk5LCJ0aXBvQ29kQXV0IjoiRSIsImNvZEF1dCI6NzIwNDMzMjQ5NjcwOTl9" ;
77+ const urlText1 = "https://www.afip.gob.ar/fe/qr/?p=eyJ2ZXIiOjEsImZlY2hhIjoiMjAyMi0wMS0yNSIsImN1aXQiOjIwMzY3MzYyNDczLCJwdG9WdGEiOjIsInRpcG9DbXAiOjExLCJucm9DbXAiOjg4LCJpbXBvcnRlIjoyMDAwLCJtb25lZGEiOiJQRVMiLCJjdHoiOjEsInRpcG9Eb2NSZWMiOjgwLCJucm9Eb2NSZWMiOjMwNzE2NzQzMjk5LCJ0aXBvQ29kQXV0IjoiRSIsImNvZEF1dCI6NzIwNDMzMjQ5NjcwOTl9" ;
7078 // FA-A
71- // const urlText = "https://www.afip.gob.ar/fe/qr/?p=eyJ2ZXIiOiAxLCAiZmVjaGEiOiAiMjAyMi0wMi0wMSIsICJjdWl0IjogMzA3MTY3MTg1MjksICJwdG9WdGEiOiAyLCAidGlwb0NtcCI6IDEsICJucm9DbXAiOiAxNjcsICJpbXBvcnRlIjogMTgxNTAuMCwgIm1vbmVkYSI6ICJQRVMiLCAiY3R6IjogMS4wLCAidGlwb0NvZEF1dCI6ICJFIiwgImNvZEF1dCI6IDcyMDU5MDA0NTQ5NTc1LCAibnJvRG9jUmVjIjogMjAzNzAzODYwNTcsICJ0aXBvRG9jUmVjIjogODB9";
79+ const urlText2 = "https://www.afip.gob.ar/fe/qr/?p=eyJ2ZXIiOiAxLCAiZmVjaGEiOiAiMjAyMi0wMi0wMSIsICJjdWl0IjogMzA3MTY3MTg1MjksICJwdG9WdGEiOiAyLCAidGlwb0NtcCI6IDEsICJucm9DbXAiOiAxNjcsICJpbXBvcnRlIjogMTgxNTAuMCwgIm1vbmVkYSI6ICJQRVMiLCAiY3R6IjogMS4wLCAidGlwb0NvZEF1dCI6ICJFIiwgImNvZEF1dCI6IDcyMDU5MDA0NTQ5NTc1LCAibnJvRG9jUmVjIjogMjAzNzAzODYwNTcsICJ0aXBvRG9jUmVjIjogODB9" ;
7280 // FA-B
73- // const urlText = "https://www.afip.gob.ar/fe/qr/?p=eyJjb2RBdXQiOjcyMDQ2MTkwMDUyNDc3LCJjdHoiOjEsImN1aXQiOjMwNzEwMTE0MTc2LCJmZWNoYSI6IjIwMjItMDEtMjMiLCJpbXBvcnRlIjo2Mzk4LjAwLCJtb25lZGEiOiJQRVMiLCJucm9DbXAiOjEwMDk3OTksIm5yb0RvY1JlYyI6MCwicHRvVnRhIjozMSwidGlwb0NtcCI6NiwidGlwb0NvZEF1dCI6IkUiLCJ0aXBvRG9jUmVjIjo5NiwidmVyIjoxfQ=="
74- this . processQR ( urlText ) ;
81+ const urlText3 = "https://www.afip.gob.ar/fe/qr/?p=eyJjb2RBdXQiOjcyMDQ2MTkwMDUyNDc3LCJjdHoiOjEsImN1aXQiOjMwNzEwMTE0MTc2LCJmZWNoYSI6IjIwMjItMDEtMjMiLCJpbXBvcnRlIjo2Mzk4LjAwLCJtb25lZGEiOiJQRVMiLCJucm9DbXAiOjEwMDk3OTksIm5yb0RvY1JlYyI6MCwicHRvVnRhIjozMSwidGlwb0NtcCI6NiwidGlwb0NvZEF1dCI6IkUiLCJ0aXBvRG9jUmVjIjo5NiwidmVyIjoxfQ=="
7582 }
7683
7784 removeInvoices ( ) : void {
7885 this . invoices . length = 0 ;
7986 localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
8087 }
8188
82- async processQR ( t : string ) {
89+ async processQR ( t : string , i : number = 0 ) {
8390 this . loading = true ;
8491 const url = new URL ( t ) ;
8592 const p : any = url . searchParams . get ( 'p' ) ;
8693 let encode = p . replace ( / b \' ( .* ) \' / , '$1' ) . replace ( / \n / g, '' ) ;
8794 const decode = decodeURIComponent ( escape ( atob ( encode ) ) ) . replace ( / \' / g, '"' ) ;
8895 const obj = JSON . parse ( decode ) ;
8996 console . log ( obj ) ;
97+
9098 if ( url . host === 'www.afip.gob.ar' || url . host === 'afip.gob.ar' ) {
9199 if ( this . invoices . find ( x => x . cae === obj . codAut ) !== undefined ) {
92- this . loading = false ;
93100 this . openDialog ( '¡Factura ya escaneada!' ) ;
101+ this . loading = false ;
94102 } else {
95103 let newinvoice = await this . processQrFeAr ( obj ) ;
96104 this . http . get < any > ( 'https://afip.tangofactura.com/Rest/GetContribuyenteFull?cuit=' + obj . cuit , { } ) . subscribe ( dataE => {
@@ -102,32 +110,33 @@ export class AfipComponent implements OnInit {
102110 newinvoice . nameRec = dataR . Contribuyente . nombre ;
103111 this . invoices . push ( newinvoice ) ;
104112 localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
105- this . loading = false ;
106113 if ( this . table ) {
107114 this . table ! . renderRows ( ) ;
108115 }
109- }
116+ this . loading = false ;
117+ } else { }
110118 } , ( err : any ) => {
111- this . loading = false ;
112119 this . openDialog ( err ) ;
120+ this . loading = false ;
113121 } ) ;
114122 } else {
115123 newinvoice . nameRec = 'Consumidor Final' ;
116124 this . invoices . push ( newinvoice ) ;
117125 localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
118- this . loading = false ;
119126 if ( this . table ) {
120127 this . table ! . renderRows ( ) ;
121128 }
129+ this . loading = false ;
122130 }
123- }
131+ } else { }
124132 } , ( err : any ) => {
125- this . loading = false ;
126133 this . openDialog ( err ) ;
134+ this . loading = false ;
127135 } ) ;
128136 }
129137 } else {
130138 this . openDialog ( 'El QR escaneado no es de AFIP' ) ;
139+ this . loading = false ;
131140 }
132141 }
133142
0 commit comments