2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 23:00:56 +00:00
books/reports/BankReconciliation/BankReconciliationImport.js
18alantom 27aed52044 incr: type some files, frappe -> fyo
- get books to serve albiet broken
- one step at a time
2022-05-23 16:18:22 +05:30

90 lines
2.8 KiB
JavaScript

import csv2json from 'csvjson-csv2json';
import { fyo } from 'src/initFyo';
import ReconciliationValidation from '../../src/components/ReconciliationValidation';
export const fileImportHandler = (file, report) => {
const reader = new FileReader();
reader.onload = () => {
const csv = reader.result;
const json = csvToJsonHandler(csv);
findMatchingReferences(json, report);
};
reader.readAsBinaryString(file);
};
export const csvToJsonHandler = (csv) => {
const json = csv2json(csv, { parseNumbers: true });
return json;
};
export const findMatchingReferences = async (json, report) => {
const referenceField = Object.keys(json[0]).filter((field) => {
return field.toLowerCase().indexOf('ref') > -1 ? true : false;
});
const clearanceDateField = Object.keys(json[0]).filter((field) => {
return field.toLowerCase().indexOf('date') > -1 ? true : false;
});
const debitField = Object.keys(json[0]).filter((field) => {
return field.toLowerCase().indexOf('debit') > -1 ||
field.toLowerCase().indexOf('deposit') > -1
? true
: false;
});
const creditField = Object.keys(json[0]).filter((field) => {
return field.toLowerCase().indexOf('credit') > -1 ||
field.toLowerCase().indexOf('withdraw') > -1
? true
: false;
});
const balanceField = Object.keys(json[0]).filter((field) => {
return field.toLowerCase().indexOf('balance') > -1 ? true : false;
});
const references = json.map((row) => {
return row[referenceField];
});
const payments = await fyo.db.getAll({
doctype: 'Payment',
fields: ['*'],
filters: {
referenceId: ['in', references],
paymentAccount: report.currentFilters.paymentAccount,
clearanceDate: ['in', [null, undefined, '']],
},
});
if (payments.length) {
const entries = payments.map((payment) => {
const jsonEntry = json.filter((row) => {
return row[referenceField] === payment.referenceId;
});
return Object.assign(payment, jsonEntry[0]);
});
const normalizedEntries = entries.map((entry) => {
return {
'Posting Date': fyo.format(entry.date, 'Date'),
'Payment Entry': entry.name,
'Ref/Cheq. ID': entry[referenceField],
'Cr/Dr':
fyo.parseNumber(entry[debitField]) > 0
? entry[debitField] + ' Dr.'
: entry[creditField] + ' Cr.',
'Clearance Date': entry[clearanceDateField],
};
});
report.$modal.show({
modalProps: {
title: `Validate Matching Entries`,
noFooter: true,
},
component: ReconciliationValidation,
props: {
entries: normalizedEntries,
afterReconcile: async () => {
await report.getReportData(report.currentFilters);
},
},
});
} else {
// 'No entries found with matching Ref / Cheque ID'
}
};