2021-11-04 10:31:26 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
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) => {
|
2021-11-04 10:31:26 +00:00
|
|
|
const referenceField = Object.keys(json[0]).filter((field) => {
|
2019-09-03 09:43:16 +00:00
|
|
|
return field.toLowerCase().indexOf('ref') > -1 ? true : false;
|
|
|
|
});
|
2021-11-04 10:31:26 +00:00
|
|
|
const clearanceDateField = Object.keys(json[0]).filter((field) => {
|
2019-09-03 09:43:16 +00:00
|
|
|
return field.toLowerCase().indexOf('date') > -1 ? true : false;
|
|
|
|
});
|
2021-11-04 10:31:26 +00:00
|
|
|
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;
|
|
|
|
});
|
2021-11-04 10:31:26 +00:00
|
|
|
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;
|
|
|
|
});
|
2021-11-04 10:31:26 +00:00
|
|
|
const balanceField = Object.keys(json[0]).filter((field) => {
|
2019-09-03 09:43:16 +00:00
|
|
|
return field.toLowerCase().indexOf('balance') > -1 ? true : false;
|
|
|
|
});
|
2021-11-04 10:31:26 +00:00
|
|
|
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,
|
2021-11-04 10:31:26 +00:00
|
|
|
clearanceDate: ['in', [null, undefined, '']],
|
|
|
|
},
|
2019-09-03 09:43:16 +00:00
|
|
|
});
|
|
|
|
if (payments.length) {
|
2021-11-04 10:31:26 +00:00
|
|
|
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]);
|
|
|
|
});
|
2021-11-04 10:31:26 +00:00
|
|
|
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.',
|
2021-11-04 10:31:26 +00:00
|
|
|
'Clearance Date': entry[clearanceDateField],
|
2019-09-03 09:43:16 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
report.$modal.show({
|
|
|
|
modalProps: {
|
|
|
|
title: `Validate Matching Entries`,
|
2021-11-04 10:31:26 +00:00
|
|
|
noFooter: true,
|
2019-09-03 09:43:16 +00:00
|
|
|
},
|
2021-11-04 10:31:26 +00:00
|
|
|
component: ReconciliationValidation,
|
2019-09-03 09:43:16 +00:00
|
|
|
props: {
|
|
|
|
entries: normalizedEntries,
|
|
|
|
afterReconcile: async () => {
|
|
|
|
await report.getReportData(report.currentFilters);
|
2021-11-04 10:31:26 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-03 09:43:16 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
frappe.call({
|
|
|
|
method: 'show-dialog',
|
|
|
|
args: {
|
|
|
|
title: 'Message',
|
2021-11-04 10:31:26 +00:00
|
|
|
message: 'No entries found with matching Ref / Cheque ID',
|
|
|
|
},
|
2019-09-03 09:43:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|