2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

refactor getFilterConditions, add some utils

This commit is contained in:
Faris Ansari 2018-04-19 20:01:17 +05:30
parent d46f6ae78a
commit b6562fe7f3
3 changed files with 65 additions and 28 deletions

View File

@ -388,47 +388,68 @@ module.exports = class Database extends Observable {
getFilterConditions(filters) { getFilterConditions(filters) {
// {"status": "Open"} => `status = "Open"` // {"status": "Open"} => `status = "Open"`
// {"status": "Open", "name": ["like", "apple%"]} // {"status": "Open", "name": ["like", "apple%"]}
// => `status="Open" and name like "apple%" // => `status="Open" and name like "apple%"
let conditions = [];
let values = []; // {"date": [">=", "2017-09-09", "<=", "2017-11-01"]}
// => `date >= 2017-09-09 and date <= 2017-11-01`
let filtersArray = [];
for (let key in filters) { for (let key in filters) {
let value = filters[key];
let field = key;
let operator = '=';
let comparisonValue = value;
let field = `ifnull(${key}, '')`; if (Array.isArray(value)) {
const value = filters[key]; operator = value[0];
comparisonValue = value[1];
operator = operator.toLowerCase();
if (value instanceof Array) { if (operator === 'includes') {
operator = 'like';
let condition = value[0];
let comparisonValue = value[1];
let placeholder = '?';
// if its like, we should add the wildcard "%" if the user has not added
if (condition.toLowerCase()==='includes') {
condition = 'like';
} }
if (['like', 'includes'].includes(condition.toLowerCase()) && !comparisonValue.includes('%')) {
if (['like', 'includes'].includes(operator) && !comparisonValue.includes('%')) {
comparisonValue = `%${comparisonValue}%`; comparisonValue = `%${comparisonValue}%`;
} }
if (['in', 'not in'].includes(condition) && Array.isArray(comparisonValue)) { }
placeholder = `(${comparisonValue.map(v => '?').join(", ")})`;
}
conditions.push(`${field} ${condition} ${placeholder}`);
if (Array.isArray(comparisonValue)) { filtersArray.push([field, operator, comparisonValue]);
values = values.concat(comparisonValue);
} else {
values.push(comparisonValue);
}
} else { if (Array.isArray(value) && value.length > 2) {
conditions.push(`${field} = ?`); // multiple conditions
values.push(value); let operator = value[2];
let comparisonValue = value[3];
filtersArray.push([field, operator, comparisonValue]);
} }
} }
let conditions = filtersArray.map(filter => {
const [field, operator, comparisonValue] = filter;
let placeholder = Array.isArray(comparisonValue) ?
comparisonValue.map(v => '?').join(', ') :
'?';
return `${field} ${operator} (${placeholder})`;
});
let values = filtersArray.reduce((acc, filter) => {
const comparisonValue = filter[2];
if (Array.isArray(comparisonValue)) {
acc = acc.concat(comparisonValue);
} else {
acc.push(comparisonValue);
}
return acc;
}, []);
return { return {
conditions: conditions.length ? conditions.join(" and ") : "", conditions: conditions.length ? conditions.join(" and ") : "",
values: values values
}; };
} }

View File

@ -85,7 +85,8 @@ module.exports = class ReportPage extends Page {
}); });
const rows = this.getRowsForDataTable(data); const rows = this.getRowsForDataTable(data);
this.datatable.refresh(rows); const columns = utils.convertFieldsToDatatableColumns(this.getColumns(data), this.layout);
this.datatable.refresh(rows, columns);
} }
makeDataTable() { makeDataTable() {

View File

@ -66,4 +66,19 @@ module.exports = {
}); });
}, },
/**
* Returns array from 0 to n - 1
* @param {Number} n
*/
range(n) {
return Array.from(Array(4)).map((d, i) => i)
},
unique(list, key = it => it) {
var seen = {};
return list.filter(item => {
var k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
});
}
}; };