diff --git a/src/components/modelo720/DropZone.tsx b/src/components/modelo720/DropZone.tsx index 7cbadfd..ea1ddad 100644 --- a/src/components/modelo720/DropZone.tsx +++ b/src/components/modelo720/DropZone.tsx @@ -71,14 +71,15 @@ export default class DropZone extends React.Component { this.data = new InteractiveBrokersActivity(file_texts[0]); }).then(() => { - let warning = (this.data!.open_positions!.filter((item)=>item.ISIN?false:true).length > 0) + let warning = (this.data!.open_positions!.filter((item) => item.ISIN ? false : true).length > 0) if (warning) this.setState({ status: 'warning', text: 'Fichero cargado. Revise los elementos' }) else this.setState({ status: 'success', text: 'Fichero cargado' }) - }).catch(() => { + }).catch((e) => { this.setState({ status: 'danger', text: 'Ha ocurrido un error al leer el fichero.' }) + console.log(e) }) } diff --git a/src/parsers/IBactivity.tsx b/src/parsers/IBactivity.tsx index 9ce6423..b305702 100644 --- a/src/parsers/IBactivity.tsx +++ b/src/parsers/IBactivity.tsx @@ -1,29 +1,90 @@ -import { SSL_OP_EPHEMERAL_RSA } from 'constants'; import { parse } from 'papaparse'; import { Position } from '../aforix/Position'; -const STATEMENT = "Statement"; -const OPEN_POSITIONS = "Posiciones abiertas"; -const INFO = "Información de instrumento financiero" -const ACCOUNT_INFO = "Información sobre la cuenta" +type HeadersType = { + STATEMENT: string, + OPEN_POSITIONS: string, + INFO: string, + ACCOUNT_INFO: string, + FIELD_NAME: string, + NAME: string, + FIELD_VALUE: string, + SYMBOL: string, + DESCRIPTION: string, + SECURITY_ID: string, + TOTAL: string, + CURRENCY: string, + VALUE: string, + QUANTITY: string, + COUNTRY: string, +} + +const ES: HeadersType = { + STATEMENT: "Statement", + OPEN_POSITIONS: "Posiciones abiertas", + INFO: "Información de instrumento financiero", + ACCOUNT_INFO: "Información sobre la cuenta", + FIELD_NAME: "Nombre del campo", + NAME: "Nombre", + FIELD_VALUE: "Valor del campo", + SYMBOL: "Símbolo", + DESCRIPTION: "Descripción", + SECURITY_ID: "Id. de seguridad", + TOTAL: "Total", + CURRENCY: "Divisa", + VALUE: "Valor", + QUANTITY: "Cantidad", + COUNTRY: "País", +} +const EN: HeadersType = { + STATEMENT: "Statement", + OPEN_POSITIONS: "Open Positions", + INFO: "Financial Instrument Information", + ACCOUNT_INFO: "Account Information", + FIELD_NAME: "Field Name", + NAME: "Name", + FIELD_VALUE: "Field Value", + SYMBOL: "Symbol", + DESCRIPTION: "Description", + SECURITY_ID: "Security ID", + TOTAL: "Total", + CURRENCY: "Currency", + VALUE: "Value", + QUANTITY: "Quantity", + COUNTRY: "Country", +} export default class InteractiveBrokersActivity { open_positions: Position[]; data: any; - forex: {[name:string]: number}; + forex: { [name: string]: number }; + lang: HeadersType; constructor(file: string) { this.open_positions = []; this.data = {} - this.forex = {'EUR': 1} + this.forex = { 'EUR': 1 } + + this.lang = this.detectLanguage(file); this.parse(file) } getName() { - return this.data[ACCOUNT_INFO] - .filter((e:any) => e['Nombre del campo'] == "Nombre")[0]["Valor del campo"]; + return this.data[this.lang.ACCOUNT_INFO] + .filter((e: any) => e[this.lang.FIELD_NAME] == this.lang.NAME)[0][this.lang.FIELD_VALUE]; + } + + detectLanguage(file: string) { + let firstLine = file.split("\n")[0]; + console.log(firstLine) + if (firstLine === "Statement,Header,Nombre del campo,Valor del campo") + return ES; + else if (firstLine === "Statement,Header,Field Name,Field Value") + return EN; + else + throw "language not detected"; } parse(file: string) { @@ -52,30 +113,30 @@ export default class InteractiveBrokersActivity { this.data = groups; - let info = groups[INFO].reduce((acc: any, val: any) => { - acc[val["Símbolo"]] = { - 'Name': val["Descripción"], - 'ISIN': val["Id. de seguridad"] + let info = groups[this.lang.INFO].reduce((acc: any, val: any) => { + acc[val[this.lang.SYMBOL]] = { + 'Name': val[this.lang.DESCRIPTION], + 'ISIN': val[this.lang.SECURITY_ID] } return acc; }, {}) - var result = groups[OPEN_POSITIONS] + var result = groups[this.lang.OPEN_POSITIONS] .filter((row: any) => row['Header'] == 'Data') .map((item: any) => { - item['ISIN'] = info[item["Símbolo"]]?.['ISIN'] ?? '' - item['Name'] = info[item["Símbolo"]]?.['Name'] ?? '' + item['ISIN'] = info[item[this.lang.SYMBOL]]?.['ISIN'] ?? '' + item['Name'] = info[item[this.lang.SYMBOL]]?.['Name'] ?? '' return item; }) this.open_positions = result.map((p: any) => { - return new Position(p['ISIN'], p['Name'], p['Cantidad'], p['Valor'], p['Divisa'], 'Country', 0, 0) + return new Position(p['ISIN'], p['Name'], p[this.lang.QUANTITY], p[this.lang.VALUE], p[this.lang.CURRENCY], '', 0, 0) }); - groups[OPEN_POSITIONS] - .filter((row:any) => row['Header'] == 'Total') - .map((row:any, position:number, elements:any) =>{ - if( row['Divisa'] != 'EUR' ){ - this.forex[row['Divisa']] = row['Valor']/elements[position+1]['Valor'] + groups[this.lang.OPEN_POSITIONS] + .filter((row: any) => row['Header'] == 'Total') + .map((row: any, position: number, elements: any) => { + if (row[this.lang.CURRENCY] != 'EUR') { + this.forex[row[this.lang.CURRENCY]] = row[this.lang.VALUE] / elements[position + 1][this.lang.VALUE] } }) }