0.10.77 • Published 2 days ago

parser-de-notas-de-corretagem v0.10.77

Weekly downloads
-
License
GNU GPLv3
Repository
github
Last release
2 days ago

Parse Brazilian brokerage notes PDFs (Rico, Clear, and Inter holders available)

npm CI Assets auto update

Easing the PITA of making IRPF. Inter support only v0.8.0 onwards ❗

Note: This is a JS/TS package. If you want the end-user solution, check the Leitor de notas de corretagem

Example result

The price and average fields already include the fees paid

[
  {
    "number": "11111",    // Brokerage note number
    "buyTotal": "4054.58",
    "sellTotal": "0.00",
    "buyFees": "1.24",
    "sellFees": "0.00",
    "fees": "1.24",
    "date": "02/02/2022", // Can also output in yyyy-MM-dd format changing `NoteParser.dateFormat`
    "holder": "rico",
    "deals": [
      {
        "type": "buy",
        "code": "FLRY3",
        "quantity": 62,
        "average": "16.30",
        "price": "1010.91",
        "date": "02/02/2022",
        "cnpj": "60.840.055/0001-31",
        "isFII": false
      },
      {
        "type": "buy",
        "code": "ALZR11",
        "quantity": 5,
        "average": "112.80",
        "price": "564.02",
        "date": "02/02/2022",
        "cnpj": "28.737.771/0001-85",
        "isFII": true
      },
      {
        "type": "buy",
        "code": "HGRU11",
        "quantity": 5,
        "average": "112.03",
        "price": "560.17",
        "date": "02/02/2022",
        "cnpj": "29.641.226/0001-53",
        "isFII": true
      },
      {
        "type": "buy",
        "code": "VISC11",
        "quantity": 15,
        "average": "97.38",
        "price": "1460.69",
        "date": "02/02/2022",
        "cnpj": "17.554.274/0001-25",
        "isFII": true
      },
      {
        "type": "buy",
        "code": "XPML11",
        "quantity": 5,
        "average": "91.76",
        "price": "458.79",
        "date": "02/02/2022",
        "cnpj": "28.757.546/0001-00",
        "isFII": true
      }
    ]
  }
]

Install

npm i parser-de-notas-de-corretagem

Usage

Full NodeJS example

import fs from 'fs';
import path from 'path';
import { Deal, NoteParser, type NegotiationNote } from 'parser-de-notas-de-corretagem';

async function main() {

  console.log(`Leitor de Notas de Negociação - GNU GPLv3`);

  const assets = new NoteParser();
  try {

    // Get all negotiation notes inside a PDF, even with password
    const possiblePDFpasswords: string[] = ['123', '456'];
    let pdfPath = path.join(__dirname, 'note.pdf');
    let parseResult: NegotiationNote[]
    try {
      parseResult = await assets.parseNote(path.basename(pdfPath), fs.readFileSync(pdfPath), possiblePDFpasswords);
    } catch (error: unknown) {
      if (error instanceof UnknownAsset) {
        console.log(`Unknown asset found: ${error.asset}`)
        // Ignore unknown assets and parse again. Unknown assets will have `code` as `UNDEF: <name>`
        parseResult = await assets.parseNote(path.basename(pdfPath), fs.readFileSync(pdfPath), possiblePDFpasswords, true);
      } else throw error
    }

    // Merge all negotiation notes
    let allDeals: Deal[][] = [];
    parseResult.forEach(note => {
      note.deals.forEach(deal => {
        let index = allDeals.findIndex(el => el.some(subEl => subEl.code === deal.code));
        if (index === -1) {
          allDeals.push([deal]);
        } else {
          allDeals[index].push(deal);
        }
      })
    })

    // Generate a .csv result
    let result: string = `Código\tCNPJ\tData\tC/V\tQuantidade\tPreço+custos\n`;
    allDeals.forEach(asset => {
      asset.forEach(deal => {
        result += `${deal.code}\t${deal.cnpj}\t${deal.date}\t${deal.type=='buy'?'C':'V'}\t${deal.quantity}\t${deal.price.replace(/\./g, ',')}\n`;
      })
      result += `\n`;
    });

    fs.writeFileSync(path.join(__dirname, '..', '..', 'Resultado.csv'), result);

    console.log(`Todas as ${parseResult.length} notas foram processadas`);
    console.log(`O arquivo "Resultado.csv" foi gerado no diretório atual.`);

  } catch (error) {
    console.log(error);
  }
}

main();

Browser

Since only Uint8Array is accepted, use the following code to convert a string using the browser

if (typeof fileContent === 'string') fileContent = Uint8Array.from(fileContent, x => x.charCodeAt(0));
await assetsParser.parseNote(filePath, fileContent, filePasswords);

Add a custom stock

There are many assets out there and some of them (like funds) are kind of hard to keep track. If some asset is not recognized, parseNote will throw the error UnknownAsset

const assets = new NoteParser();
try {
  await assets.parseNote(filePath, fileContent, filePasswords)
} catch (error) {
  if (error instanceof UnknownAsset) {
    console.log(`Unknown asset found: ${error.asset}`)
  } else console.log(error)
}

One can parse the note ignoring this error by passing continueOnError as true. Unknown assets will have the code UNDEF: <name> whereas the <name> is the name of the asset as in the note.

const assets = new NoteParser();
await assets.parseNote(filePath, fileContent, filePasswords, true)

For unknown assets to be properly parsed, one can add custom stocks with .defineStock

const assets = new NoteParser();
// Old stocks aren't available by default, but you can add them.
// CNPJ as the third argument is optional
assets.defineStock('BIDI3', 'BANCO INTER ON');
assets.defineStock('BIDI11', 'BANCO INTER UNT');
// Some codes can appear with multiple names. Add as many as needed
assets.defineStock('KDIF11', 'KINEA INFRAF FIDC', '26.324.298/0001-89');
assets.defineStock('KDIF11', 'FDC KINEAINF FIDC', '26.324.298/0001-89');
// Backward compatible with the below too
assets.defineStock('KDIF11_2', 'FDC KINEAINF FIDC', '26.324.298/0001-89');

P.S

  • Total values include fees
  • The values can deviate from cents. It's always a good call to double-check if the result is as expected. Check the License
  • Inter broker has only a few tests, so please open Issues if you find something wrong
  • Local auto-update isn't persistent. New releases are done everyday with persistent updates
  • Other brokers may work with the internal PDF architecture is the same as the supported brokers

Contributors

Thanks to whom sent the notes for the tests ❤️. Personal data is not stored neither used on tests, only the notes' content.

Thanks? U welcome

Consider thanking me: send a "Thanks!" 👋 by PIX 😊

a09e5878-2355-45f7-9f36-6df4ccf383cf

License

As license, this software is provided as is, free of charge, without any warranty whatsoever. Its author is not responsible for its usage. Use it by your own risk.

GNU GPLv3

0.10.77

2 days ago

0.10.76

3 days ago

0.10.75

5 days ago

0.10.74

6 days ago

0.10.73

7 days ago

0.10.72

8 days ago

0.10.71

9 days ago

0.10.70

10 days ago

0.10.69

12 days ago

0.10.68

13 days ago

0.10.66

15 days ago

0.10.67

14 days ago

0.10.64

17 days ago

0.10.65

16 days ago

0.10.63

19 days ago

0.10.62

20 days ago

0.10.61

21 days ago

0.10.60

22 days ago

0.10.58

24 days ago

0.10.59

23 days ago

0.10.56

26 days ago

0.10.57

25 days ago

0.10.55

27 days ago

0.10.54

29 days ago

0.10.53

1 month ago

0.10.52

1 month ago

0.10.51

1 month ago

0.10.50

1 month ago

0.10.48

1 month ago

0.10.49

1 month ago

0.10.47

1 month ago

0.10.45

1 month ago

0.10.46

1 month ago

0.10.43

2 months ago

0.10.44

1 month ago

0.10.42

2 months ago

0.10.41

2 months ago

0.10.40

2 months ago

0.10.39

2 months ago

0.10.38

2 months ago

0.10.37

2 months ago

0.10.36

2 months ago

0.10.35

2 months ago

0.10.34

2 months ago

0.10.33

2 months ago

0.10.32

2 months ago

0.10.29

2 months ago

0.10.30

2 months ago

0.10.31

2 months ago

0.10.28

2 months ago

0.10.25

2 months ago

0.10.26

2 months ago

0.10.27

2 months ago

0.10.23

2 months ago

0.10.24

2 months ago

0.10.22

2 months ago

0.10.21

2 months ago

0.10.20

3 months ago

0.10.18

3 months ago

0.10.19

3 months ago

0.10.17

3 months ago

0.10.14

3 months ago

0.10.15

3 months ago

0.10.16

3 months ago

0.10.13

3 months ago

0.10.12

3 months ago

0.10.11

3 months ago

0.10.10

3 months ago

0.10.9

3 months ago

0.10.8

3 months ago

0.10.7

3 months ago

0.10.6

3 months ago

0.10.4

3 months ago

0.10.5

3 months ago

0.10.1

4 months ago

0.10.2

4 months ago

0.10.3

4 months ago

0.10.0

4 months ago

0.9.85

4 months ago

0.9.84

4 months ago

0.9.82

4 months ago

0.9.83

4 months ago

0.9.81

4 months ago

0.9.80

4 months ago

0.9.79

4 months ago

0.9.78

4 months ago

0.9.75

4 months ago

0.9.76

4 months ago

0.9.77

4 months ago

0.9.74

4 months ago

0.9.72

4 months ago

0.9.73

4 months ago

0.9.71

4 months ago

0.9.70

4 months ago

0.9.69

4 months ago

0.9.67

4 months ago

0.9.68

4 months ago

0.9.66

4 months ago

0.9.64

4 months ago

0.9.65

4 months ago

0.9.63

5 months ago

0.9.62

5 months ago

0.9.60

5 months ago

0.9.61

5 months ago

0.9.58

5 months ago

0.9.59

5 months ago

0.9.57

5 months ago

0.9.56

5 months ago

0.9.52

5 months ago

0.9.53

5 months ago

0.9.54

5 months ago

0.9.55

5 months ago

0.9.50

5 months ago

0.9.51

5 months ago

0.9.45

5 months ago

0.9.46

5 months ago

0.9.47

5 months ago

0.9.48

5 months ago

0.9.41

5 months ago

0.9.42

5 months ago

0.9.43

5 months ago

0.9.44

5 months ago

0.9.49

5 months ago

0.9.40

6 months ago

0.9.34

6 months ago

0.9.8

7 months ago

0.9.35

6 months ago

0.9.7

7 months ago

0.9.36

6 months ago

0.9.37

6 months ago

0.9.9

7 months ago

0.9.30

6 months ago

0.9.4

7 months ago

0.9.31

6 months ago

0.9.3

7 months ago

0.9.32

6 months ago

0.9.6

7 months ago

0.9.33

6 months ago

0.9.5

7 months ago

0.9.38

6 months ago

0.9.39

6 months ago

0.9.23

6 months ago

0.9.24

6 months ago

0.9.25

6 months ago

0.9.26

6 months ago

0.9.20

6 months ago

0.9.21

6 months ago

0.9.22

6 months ago

0.9.27

6 months ago

0.9.28

6 months ago

0.9.29

6 months ago

0.9.12

7 months ago

0.9.13

6 months ago

0.9.14

6 months ago

0.9.15

6 months ago

0.9.10

7 months ago

0.9.11

7 months ago

0.9.16

6 months ago

0.9.17

6 months ago

0.9.18

6 months ago

0.9.19

6 months ago

0.9.1

7 months ago

0.8.78

7 months ago

0.8.77

7 months ago

0.8.79

7 months ago

0.8.74

7 months ago

0.8.73

7 months ago

0.8.76

7 months ago

0.8.75

7 months ago

0.8.70

7 months ago

0.8.72

7 months ago

0.8.71

7 months ago

0.8.67

8 months ago

0.8.66

8 months ago

0.8.69

8 months ago

0.8.68

8 months ago

0.8.63

8 months ago

0.8.62

8 months ago

0.8.65

8 months ago

0.8.64

8 months ago

0.8.61

8 months ago

0.9.0

7 months ago

0.8.9

10 months ago

0.8.8

10 months ago

0.8.5

10 months ago

0.8.4

10 months ago

0.8.7

10 months ago

0.8.6

10 months ago

0.7.22

10 months ago

0.7.21

10 months ago

0.7.23

10 months ago

0.7.20

10 months ago

0.7.2

11 months ago

0.7.1

11 months ago

0.7.4

11 months ago

0.7.3

11 months ago

0.7.0

11 months ago

0.7.11

10 months ago

0.7.10

11 months ago

0.7.13

10 months ago

0.7.12

10 months ago

0.7.19

10 months ago

0.7.18

10 months ago

0.7.15

10 months ago

0.7.14

10 months ago

0.7.17

10 months ago

0.7.16

10 months ago

0.8.60

8 months ago

0.8.56

8 months ago

0.8.55

8 months ago

0.8.58

8 months ago

0.8.57

8 months ago

0.8.52

8 months ago

0.8.51

8 months ago

0.8.54

8 months ago

0.8.53

8 months ago

0.8.59

8 months ago

0.8.1

10 months ago

0.8.0

10 months ago

0.8.50

8 months ago

0.8.3

10 months ago

0.8.2

10 months ago

0.8.45

8 months ago

0.8.44

8 months ago

0.8.47

8 months ago

0.8.46

8 months ago

0.8.41

9 months ago

0.8.40

9 months ago

0.8.43

9 months ago

0.8.42

9 months ago

0.8.49

8 months ago

0.8.48

8 months ago

0.8.34

9 months ago

0.8.33

9 months ago

0.8.36

9 months ago

0.8.35

9 months ago

0.8.30

9 months ago

0.8.32

9 months ago

0.8.31

9 months ago

0.8.38

9 months ago

0.8.37

9 months ago

0.8.39

9 months ago

0.8.23

9 months ago

0.8.22

9 months ago

0.7.9

11 months ago

0.8.25

9 months ago

0.8.24

9 months ago

0.7.6

11 months ago

0.7.5

11 months ago

0.8.21

9 months ago

0.7.8

11 months ago

0.8.20

9 months ago

0.7.7

11 months ago

0.8.27

9 months ago

0.8.26

9 months ago

0.8.29

9 months ago

0.8.28

9 months ago

0.8.12

10 months ago

0.8.11

10 months ago

0.8.14

10 months ago

0.8.13

10 months ago

0.8.10

10 months ago

0.8.19

9 months ago

0.8.16

10 months ago

0.8.15

10 months ago

0.8.18

9 months ago

0.8.17

10 months ago

0.5.4

12 months ago

0.5.3

1 year ago

0.5.0

1 year ago

0.6.1

12 months ago

0.5.2

1 year ago

0.6.0

12 months ago

0.5.1

1 year ago

0.1.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.4.4

1 year ago

0.4.1

1 year ago

0.2.3

1 year ago

0.0.5

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.2.2

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.2.4

1 year ago

0.0.3

1 year ago

0.0.4

1 year ago

0.0.2

2 years ago

0.0.1

2 years ago