@@ -5,6 +5,8 @@ import { MatDialog, MAT_DIALOG_DATA } from '@angular/material/dialog';
55import { BarcodeScanner } from '@awesome-cordova-plugins/barcode-scanner/ngx' ;
66import { HttpClient } from '@angular/common/http' ;
77
8+ import { Globals } from '../app.globals' ;
9+
810import { Filesystem , Directory , Encoding } from '@capacitor/filesystem' ;
911import { FileOpener } from '@ionic-native/file-opener/ngx' ;
1012
@@ -42,7 +44,10 @@ export class AfipComponent implements OnInit {
4244 expandedElement : InvoiceElement | null | undefined ;
4345 @ViewChild ( MatTable ) table : MatTable < InvoiceElement > | undefined ;
4446
47+ globals = Globals ;
4548 loading = false ;
49+ qrs : any = [ ] ;
50+ qrErrors : any = [ ] ;
4651
4752 constructor ( private barcodeScanner : BarcodeScanner , public dialog : MatDialog , private http : HttpClient ) { }
4853
@@ -55,7 +60,23 @@ export class AfipComponent implements OnInit {
5560 }
5661 }
5762
58- ngDoCheck ( ) : void { }
63+ ngDoCheck ( ) : void {
64+ if ( this . qrs . length > 0 ) {
65+ console . log ( this . qrs ) ;
66+ let res = this . qrs . every ( ( i : any ) => {
67+ return i === false
68+ } ) ;
69+ if ( res ) {
70+ this . loading = false ;
71+ this . qrs . length = 0 ;
72+ if ( this . qrErrors . length > 0 ) {
73+ this . openDialog ( 'Algunos QR de facturas tienen errores y no se pudieron importar masivamente.' ) ;
74+ this . qrErrors . length = 0 ;
75+ }
76+ }
77+ }
78+ this . globals = Globals ;
79+ }
5980
6081 scanQR ( ) {
6182 this . barcodeScanner . scan ( {
@@ -72,21 +93,45 @@ export class AfipComponent implements OnInit {
7293 } ) ;
7394 }
7495
96+ scanQRContinue ( ) {
97+ this . barcodeScanner . scan ( {
98+ formats : 'QR_CODE' ,
99+ prompt : 'Coloque un Código QR de AFIP en el Escaner. Puede escanear todas las Facturas AFIP que desee. Al finalizar, oprima en el botón Volver.' ,
100+ resultDisplayDuration : 0
101+ } )
102+ . then ( barcodeData => {
103+ if ( barcodeData . cancelled === false ) {
104+ this . qrs . push ( barcodeData . text ) ;
105+ this . scanQRContinue ( ) ;
106+ } else {
107+ this . processQRMassive ( ) ;
108+ }
109+ } ) . catch ( err => {
110+ alert ( err ) ;
111+ } ) ;
112+ }
113+
75114 async test ( ) {
76115 // FA-C
77116 const urlText1 = "https://www.afip.gob.ar/fe/qr/?p=eyJ2ZXIiOjEsImZlY2hhIjoiMjAyMi0wMS0yNSIsImN1aXQiOjIwMzY3MzYyNDczLCJwdG9WdGEiOjIsInRpcG9DbXAiOjExLCJucm9DbXAiOjg4LCJpbXBvcnRlIjoyMDAwLCJtb25lZGEiOiJQRVMiLCJjdHoiOjEsInRpcG9Eb2NSZWMiOjgwLCJucm9Eb2NSZWMiOjMwNzE2NzQzMjk5LCJ0aXBvQ29kQXV0IjoiRSIsImNvZEF1dCI6NzIwNDMzMjQ5NjcwOTl9" ;
78117 // FA-A
79118 const urlText2 = "https://www.afip.gob.ar/fe/qr/?p=eyJ2ZXIiOiAxLCAiZmVjaGEiOiAiMjAyMi0wMi0wMSIsICJjdWl0IjogMzA3MTY3MTg1MjksICJwdG9WdGEiOiAyLCAidGlwb0NtcCI6IDEsICJucm9DbXAiOiAxNjcsICJpbXBvcnRlIjogMTgxNTAuMCwgIm1vbmVkYSI6ICJQRVMiLCAiY3R6IjogMS4wLCAidGlwb0NvZEF1dCI6ICJFIiwgImNvZEF1dCI6IDcyMDU5MDA0NTQ5NTc1LCAibnJvRG9jUmVjIjogMjAzNzAzODYwNTcsICJ0aXBvRG9jUmVjIjogODB9" ;
80119 // FA-B
81120 const urlText3 = "https://www.afip.gob.ar/fe/qr/?p=eyJjb2RBdXQiOjcyMDQ2MTkwMDUyNDc3LCJjdHoiOjEsImN1aXQiOjMwNzEwMTE0MTc2LCJmZWNoYSI6IjIwMjItMDEtMjMiLCJpbXBvcnRlIjo2Mzk4LjAwLCJtb25lZGEiOiJQRVMiLCJucm9DbXAiOjEwMDk3OTksIm5yb0RvY1JlYyI6MCwicHRvVnRhIjozMSwidGlwb0NtcCI6NiwidGlwb0NvZEF1dCI6IkUiLCJ0aXBvRG9jUmVjIjo5NiwidmVyIjoxfQ=="
121+
122+ // Massive Import
123+ /*this.qrs.push(urlText1);
124+ this.qrs.push(urlText2);
125+ this.qrs.push(urlText3);
126+ this.processQRMassive();*/
82127 }
83128
84129 removeInvoices ( ) : void {
85130 this . invoices . length = 0 ;
86131 localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
87132 }
88133
89- async processQR ( t : string , i : number = 0 ) {
134+ async processQR ( t : string ) {
90135 this . loading = true ;
91136 const url = new URL ( t ) ;
92137 const p : any = url . searchParams . get ( 'p' ) ;
@@ -140,6 +185,62 @@ export class AfipComponent implements OnInit {
140185 }
141186 }
142187
188+ async processQRMassive ( ) {
189+ this . loading = true ;
190+ for ( let i = 0 ; i < this . qrs . length ; i ++ ) {
191+ const t = this . qrs [ i ] ;
192+ const url = new URL ( t ) ;
193+ const p : any = url . searchParams . get ( 'p' ) ;
194+ let encode = p . replace ( / b \' ( .* ) \' / , '$1' ) . replace ( / \n / g, '' ) ;
195+ const decode = decodeURIComponent ( escape ( atob ( encode ) ) ) . replace ( / \' / g, '"' ) ;
196+ const obj = JSON . parse ( decode ) ;
197+
198+ if ( url . host === 'www.afip.gob.ar' || url . host === 'afip.gob.ar' ) {
199+ if ( this . invoices . find ( x => x . cae === obj . codAut ) !== undefined ) {
200+ this . qrErrors . push ( obj . codAut ) ;
201+ this . qrs [ i ] = false ;
202+ } else {
203+ let newinvoice = await this . processQrFeAr ( obj ) ;
204+ this . http . get < any > ( 'https://afip.tangofactura.com/Rest/GetContribuyenteFull?cuit=' + obj . cuit , { } ) . subscribe ( dataE => {
205+ if ( dataE . Contribuyente ) {
206+ newinvoice . nameEmi = dataE . Contribuyente . nombre ;
207+ if ( obj . nroDocRec !== 0 ) {
208+ this . http . get < any > ( 'https://afip.tangofactura.com/Rest/GetContribuyenteFull?cuit=' + obj . nroDocRec , { } ) . subscribe ( dataR => {
209+ if ( dataR . Contribuyente ) {
210+ newinvoice . nameRec = dataR . Contribuyente . nombre ;
211+ this . invoices . push ( newinvoice ) ;
212+ localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
213+ if ( this . table ) {
214+ this . table ! . renderRows ( ) ;
215+ }
216+ this . qrs [ i ] = false ;
217+ } else { }
218+ } , ( err : any ) => {
219+ this . qrs [ i ] = false ;
220+ this . qrErrors . push ( obj . codAut ) ;
221+ } ) ;
222+ } else {
223+ newinvoice . nameRec = 'Consumidor Final' ;
224+ this . invoices . push ( newinvoice ) ;
225+ localStorage . setItem ( 'invoices' , JSON . stringify ( this . invoices ) ) ;
226+ if ( this . table ) {
227+ this . table ! . renderRows ( ) ;
228+ }
229+ this . qrs [ i ] = false ;
230+ }
231+ } else { }
232+ } , ( err : any ) => {
233+ this . qrs [ i ] = false ;
234+ this . qrErrors . push ( obj . codAut ) ;
235+ } ) ;
236+ }
237+ } else {
238+ this . qrs [ i ] = false ;
239+ this . qrErrors . push ( 'QR no es de AFIP' ) ;
240+ }
241+ }
242+ }
243+
143244 processQrFeAr ( obj : any ) : any {
144245 if ( obj . ver === '1' || obj . ver === 1 ) {
145246 let comp = '' ;
0 commit comments