File size: 796 Bytes
ae85a28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Injectable } from '@nestjs/common';

@Injectable()
export class ParserService {
  parseInvoice(text: string) {
    const invoiceNumber = text.match(/invoice\s*#?\s*([A-Z0-9-]+)/i);
    const total = text.match(/total\s*[:$]?\s*([0-9.]+)/i);
    const subtotal = text.match(/subtotal\s*[:$]?\s*([0-9.]+)/i);
    const tax = text.match(/tax\s*[:$]?\s*([0-9.]+)/i);
    const date = text.match(/\d{1,2}\/\d{1,2}\/\d{2,4}/);

    return {
      vendor_name: text.split('\n')[0],
      invoice_number: invoiceNumber?.[1] || null,
      invoice_date: date ? new Date(date[0]) : null,
      subtotal: subtotal ? parseFloat(subtotal[1]) : null,
      tax: tax ? parseFloat(tax[1]) : null,
      total: total ? parseFloat(total[1]) : null,
      currency: 'USD',
    };
  }
}