2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/reports/BankReconciliation/BankReconciliationImport.js

96 lines
2.9 KiB
JavaScript
Raw Normal View History

import csv2json from 'csvjson-csv2json';
import frappe from 'frappejs';
import ReconciliationValidation from '../../src/components/ReconciliationValidation';
2019-09-03 09:43:16 +00:00
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) => {
2019-09-03 09:43:16 +00:00
const json = csv2json(csv, { parseNumbers: true });
return json;
};
export const findMatchingReferences = async (json, report) => {
const referenceField = Object.keys(json[0]).filter((field) => {
2019-09-03 09:43:16 +00:00
return field.toLowerCase().indexOf('ref') > -1 ? true : false;
});
const clearanceDateField = Object.keys(json[0]).filter((field) => {
2019-09-03 09:43:16 +00:00
return field.toLowerCase().indexOf('date') > -1 ? true : false;
});
const debitField = Object.keys(json[0]).filter((field) => {
2019-09-03 09:43:16 +00:00
return field.toLowerCase().indexOf('debit') > -1 ||
field.toLowerCase().indexOf('deposit') > -1
? true
: false;
});
const creditField = Object.keys(json[0]).filter((field) => {
2019-09-03 09:43:16 +00:00
return field.toLowerCase().indexOf('credit') > -1 ||
field.toLowerCase().indexOf('withdraw') > -1
? true
: false;
});
const balanceField = Object.keys(json[0]).filter((field) => {
2019-09-03 09:43:16 +00:00
return field.toLowerCase().indexOf('balance') > -1 ? true : false;
});
const references = json.map((row) => {
2019-09-03 09:43:16 +00:00
return row[referenceField];
});
const payments = await frappe.db.getAll({
doctype: 'Payment',
fields: ['*'],
filters: {
referenceId: ['in', references],
paymentAccount: report.currentFilters.paymentAccount,
clearanceDate: ['in', [null, undefined, '']],
},
2019-09-03 09:43:16 +00:00
});
if (payments.length) {
const entries = payments.map((payment) => {
const jsonEntry = json.filter((row) => {
2019-09-03 09:43:16 +00:00
return row[referenceField] === payment.referenceId;
});
return Object.assign(payment, jsonEntry[0]);
});
const normalizedEntries = entries.map((entry) => {
2019-09-03 09:43:16 +00:00
return {
'Posting Date': frappe.format(entry.date, 'Date'),
'Payment Entry': entry.name,
'Ref/Cheq. ID': entry[referenceField],
'Cr/Dr':
frappe.parseNumber(entry[debitField]) > 0
? entry[debitField] + ' Dr.'
: entry[creditField] + ' Cr.',
'Clearance Date': entry[clearanceDateField],
2019-09-03 09:43:16 +00:00
};
});
report.$modal.show({
modalProps: {
title: `Validate Matching Entries`,
noFooter: true,
2019-09-03 09:43:16 +00:00
},
component: ReconciliationValidation,
2019-09-03 09:43:16 +00:00
props: {
entries: normalizedEntries,
afterReconcile: async () => {
await report.getReportData(report.currentFilters);
},
},
2019-09-03 09:43:16 +00:00
});
} else {
frappe.call({
method: 'show-dialog',
args: {
title: 'Message',
message: 'No entries found with matching Ref / Cheque ID',
},
2019-09-03 09:43:16 +00:00
});
}
};