parser-de-notas-de-corretagem 0.10.1 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -20
- package/notes-parser.js +1 -1
- package/notes-parser.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,16 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
 [](https://github.com/planetsLightningArrester/parser-de-notas-de-corretagem/actions/workflows/ci.yml) [](https://github.com/planetsLightningArrester/parser-de-notas-de-corretagem/actions/workflows/assets-auto-update.yml)
|
|
4
4
|
|
|
5
|
-
Easing the PITA of making IRPF
|
|
5
|
+
Easing the PITA of making IRPF. **Inter support only v0.8.0 onwards ❗**
|
|
6
6
|
|
|
7
7
|
> Note: This is a JS/TS package. If you want the end-user solution, check the [Leitor de notas de corretagem](https://github.com/planetsLightningArrester/leitor-de-notas-de-corretagem)
|
|
8
8
|
|
|
9
|
-
> Note: Inter support only v0.8.0 onwards
|
|
10
|
-
|
|
11
9
|
## Example result
|
|
12
10
|
|
|
13
11
|
> The `price` and `average` fields already include the fees paid
|
|
14
|
-
|
|
12
|
+
|
|
13
|
+
```json
|
|
15
14
|
[
|
|
16
15
|
{
|
|
17
16
|
"number": "11111", // Brokerage note number
|
|
@@ -76,31 +75,42 @@ Easing the PITA of making IRPF
|
|
|
76
75
|
]
|
|
77
76
|
}
|
|
78
77
|
]
|
|
79
|
-
```
|
|
78
|
+
```
|
|
80
79
|
|
|
81
80
|
## Install
|
|
81
|
+
|
|
82
82
|
> npm i parser-de-notas-de-corretagem
|
|
83
83
|
|
|
84
84
|
## Usage
|
|
85
85
|
|
|
86
86
|
### Full NodeJS example
|
|
87
|
-
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
88
89
|
import fs from 'fs';
|
|
89
90
|
import path from 'path';
|
|
90
|
-
import { Deal, NoteParser } from 'parser-de-notas-de-corretagem';
|
|
91
|
+
import { Deal, NoteParser, type NegotiationNote } from 'parser-de-notas-de-corretagem';
|
|
91
92
|
|
|
92
93
|
async function main() {
|
|
93
94
|
|
|
94
95
|
console.log(`Leitor de Notas de Negociação - GNU GPLv3`);
|
|
95
|
-
|
|
96
|
+
|
|
96
97
|
const assets = new NoteParser();
|
|
97
98
|
try {
|
|
98
99
|
|
|
99
100
|
// Get all negotiation notes inside a PDF, even with password
|
|
100
101
|
const possiblePDFpasswords: string[] = ['123', '456'];
|
|
101
102
|
let pdfPath = path.join(__dirname, 'note.pdf');
|
|
102
|
-
let parseResult
|
|
103
|
-
|
|
103
|
+
let parseResult: NegotiationNote[]
|
|
104
|
+
try {
|
|
105
|
+
parseResult = await assets.parseNote(path.basename(pdfPath), fs.readFileSync(pdfPath), possiblePDFpasswords);
|
|
106
|
+
} catch (error: unknown) {
|
|
107
|
+
if (error instanceof UnknownAsset) {
|
|
108
|
+
console.log(`Unknown asset found: ${error.asset}`)
|
|
109
|
+
// Ignore unknown assets and parse again. Unknown assets will have `code` as `UNDEF: <name>`
|
|
110
|
+
parseResult = await assets.parseNote(path.basename(pdfPath), fs.readFileSync(pdfPath), possiblePDFpasswords, true);
|
|
111
|
+
} else throw error
|
|
112
|
+
}
|
|
113
|
+
|
|
104
114
|
// Merge all negotiation notes
|
|
105
115
|
let allDeals: Deal[][] = [];
|
|
106
116
|
parseResult.forEach(note => {
|
|
@@ -113,7 +123,7 @@ async function main() {
|
|
|
113
123
|
}
|
|
114
124
|
})
|
|
115
125
|
})
|
|
116
|
-
|
|
126
|
+
|
|
117
127
|
// Generate a .csv result
|
|
118
128
|
let result: string = `Código\tCNPJ\tData\tC/V\tQuantidade\tPreço+custos\n`;
|
|
119
129
|
allDeals.forEach(asset => {
|
|
@@ -122,9 +132,9 @@ async function main() {
|
|
|
122
132
|
})
|
|
123
133
|
result += `\n`;
|
|
124
134
|
});
|
|
125
|
-
|
|
135
|
+
|
|
126
136
|
fs.writeFileSync(path.join(__dirname, '..', '..', 'Resultado.csv'), result);
|
|
127
|
-
|
|
137
|
+
|
|
128
138
|
console.log(`Todas as ${parseResult.length} notas foram processadas`);
|
|
129
139
|
console.log(`O arquivo "Resultado.csv" foi gerado no diretório atual.`);
|
|
130
140
|
|
|
@@ -137,25 +147,53 @@ main();
|
|
|
137
147
|
```
|
|
138
148
|
|
|
139
149
|
### Browser
|
|
150
|
+
|
|
140
151
|
Since only `Uint8Array` is accepted, use the following code to convert a string using the browser
|
|
141
|
-
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
142
154
|
if (typeof fileContent === 'string') fileContent = Uint8Array.from(fileContent, x => x.charCodeAt(0));
|
|
143
|
-
assetsParser.parseNote(filePath, fileContent, filePasswords);
|
|
155
|
+
await assetsParser.parseNote(filePath, fileContent, filePasswords);
|
|
144
156
|
```
|
|
145
157
|
|
|
146
158
|
### Add a custom stock
|
|
147
|
-
|
|
159
|
+
|
|
160
|
+
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`
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
const assets = new NoteParser();
|
|
164
|
+
try {
|
|
165
|
+
await assets.parseNote(filePath, fileContent, filePasswords)
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (error instanceof UnknownAsset) {
|
|
168
|
+
console.log(`Unknown asset found: ${error.asset}`)
|
|
169
|
+
} else console.log(error)
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
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.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const assets = new NoteParser();
|
|
177
|
+
await assets.parseNote(filePath, fileContent, filePasswords, true)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
For unknown assets to be properly parsed, one can add custom stocks with `.defineStock`
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
148
183
|
const assets = new NoteParser();
|
|
149
184
|
// Old stocks aren't available by default, but you can add them.
|
|
150
185
|
// CNPJ as the third argument is optional
|
|
151
186
|
assets.defineStock('BIDI3', 'BANCO INTER ON');
|
|
152
187
|
assets.defineStock('BIDI11', 'BANCO INTER UNT');
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
188
|
+
// Some codes can appear with multiple names. Add as many as needed
|
|
189
|
+
assets.defineStock('KDIF11', 'KINEA INFRAF FIDC', '26.324.298/0001-89');
|
|
190
|
+
assets.defineStock('KDIF11', 'FDC KINEAINF FIDC', '26.324.298/0001-89');
|
|
191
|
+
// Backward compatible with the below too
|
|
192
|
+
assets.defineStock('KDIF11_2', 'FDC KINEAINF FIDC', '26.324.298/0001-89');
|
|
156
193
|
```
|
|
157
194
|
|
|
158
|
-
## P.S
|
|
195
|
+
## P.S
|
|
196
|
+
|
|
159
197
|
* Total values include fees
|
|
160
198
|
* The values can deviate from cents. It's always a good call to double-check if the result is as expected. Check the [License](#license)
|
|
161
199
|
* Inter broker has only a few tests, so please open [Issues](https://github.com/planetsLightningArrester/parser-de-notas-de-corretagem/issues) if you find something wrong
|
|
@@ -163,13 +201,16 @@ this.defineStock('KDIF11_2', 'FDC KINEAINF FIDC', '26.324.298/0001-89');
|
|
|
163
201
|
* Other brokers may work with the internal PDF architecture is the same as the supported brokers
|
|
164
202
|
|
|
165
203
|
## Contributors
|
|
204
|
+
|
|
166
205
|
Thanks to whom sent the notes for the tests ❤️. Personal data is not stored neither used on tests, only the notes' content.
|
|
167
206
|
|
|
168
207
|
## Thanks? U welcome
|
|
208
|
+
|
|
169
209
|
Consider thanking me: send a "Thanks!" 👋 by [PIX](https://www.bcb.gov.br/en/financialstability/pix_en) 😊
|
|
170
210
|
> a09e5878-2355-45f7-9f36-6df4ccf383cf
|
|
171
211
|
|
|
172
212
|
## License
|
|
213
|
+
|
|
173
214
|
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.
|
|
174
215
|
|
|
175
216
|
[GNU GPLv3](https://choosealicense.com/licenses/gpl-3.0/)
|