Skip to content
This repository was archived by the owner on Sep 24, 2025. It is now read-only.

Commit b6a744b

Browse files
author
platipusica
committed
boolean summary
1 parent 48ee96e commit b6a744b

File tree

2 files changed

+235
-79
lines changed

2 files changed

+235
-79
lines changed

jam/js/modules/field.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,15 +971,24 @@ class Field {
971971
return def;
972972
}
973973

974+
// numeric_field() {
975+
// if (!this.lookup_item && (
976+
// this.data_type === consts.INTEGER ||
977+
// this.data_type === consts.FLOAT ||
978+
// this.data_type === consts.CURRENCY)) {
979+
// return true;
980+
// }
981+
// }
974982
numeric_field() {
975-
if (!this.lookup_item && (
983+
if (!this.lookup_item &&
984+
this.data_type !== consts.BOOLEAN && (
976985
this.data_type === consts.INTEGER ||
977986
this.data_type === consts.FLOAT ||
978987
this.data_type === consts.CURRENCY)) {
979988
return true;
980989
}
990+
return false;
981991
}
982-
983992
system_field() {
984993
if (this.field_name === this.owner._primary_key ||
985994
this.field_name === this.owner._deleted_flag ||

jam/js/modules/table.js

Lines changed: 224 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,92 +1784,239 @@ class DBTable {
17841784

17851785
}
17861786

1787-
calc_summary(callback) {
1788-
var self = this,
1789-
i,
1790-
copy,
1791-
field_name,
1792-
field,
1793-
fields,
1794-
count_field,
1795-
sum_fields,
1796-
count_fields,
1797-
total_records = 0,
1798-
funcs,
1799-
params = {};
1800-
if (!this.item._paginate || this.item.virtual_table) {
1801-
this.item.calc_summary(this.item, undefined, undefined, this.options.summary_fields);
1787+
// calc_summary(callback) {
1788+
// var self = this,
1789+
// i,
1790+
// copy,
1791+
// field_name,
1792+
// field,
1793+
// fields,
1794+
// count_field,
1795+
// sum_fields,
1796+
// count_fields,
1797+
// total_records = 0,
1798+
// funcs,
1799+
// params = {};
1800+
// if (!this.item._paginate || this.item.virtual_table) {
1801+
// this.item.calc_summary(this.item, undefined, undefined, this.options.summary_fields);
1802+
// }
1803+
// else {
1804+
// if (this.item.master) {
1805+
// copy = task.item_by_ID(this.item.prototype_ID).copy({handlers: false, details: false});
1806+
// }
1807+
// else {
1808+
// copy = this.item.copy({handlers: false, details: false});
1809+
// }
1810+
// count_field = copy._primary_key;
1811+
// fields = [];
1812+
// count_fields = [];
1813+
// sum_fields = [];
1814+
// funcs = {};
1815+
// sum_fields.push(count_field);
1816+
// funcs[count_field] = 'count';
1817+
// for (i = 0; i < this.options.summary_fields.length; i++) {
1818+
// field_name = this.options.summary_fields[i];
1819+
// field = this.item.field_by_name(field_name);
1820+
// if (field && this.fields.indexOf(field) !== -1) {
1821+
// fields.push(field_name);
1822+
// if (field.numeric_field()) {
1823+
// sum_fields.push(field_name);
1824+
// funcs[field_name] = 'sum';
1825+
// }
1826+
// else {
1827+
// count_fields.push(field_name);
1828+
// }
1829+
// }
1830+
// }
1831+
// for (var key in self.item._open_params) {
1832+
// if (self.item._open_params.hasOwnProperty(key)) {
1833+
// if (key.substring(0, 2) !== '__') {
1834+
// params[key] = self.item._open_params[key];
1835+
// }
1836+
// }
1837+
// }
1838+
// params.__summary = true;
1839+
// if (self.item._open_params.__filters) {
1840+
// copy._where_list = self.item._open_params.__filters;
1841+
// }
1842+
// if (this.item.master) {
1843+
// copy.ID = this.item.ID
1844+
// params.__master_id = this.item.master.ID;
1845+
// params.__master_rec_id = this.item.master.field_by_name(this.item.master._primary_key).value;
1846+
// }
1847+
// this.item._fields_summary_info = {};
1848+
// copy.open({fields: sum_fields, funcs: funcs, params: params},
1849+
// function() {
1850+
// var i,
1851+
// text;
1852+
// copy.each_field(function(f, i) {
1853+
// if (i == 0) {
1854+
// total_records = f.data;
1855+
// }
1856+
// else {
1857+
// self.item._fields_summary_info[f.field_name] =
1858+
// {text: f.display_text, value: f.value};
1859+
// }
1860+
// });
1861+
// copy.each_field(function(f, i) {
1862+
// if (i == 0) {
1863+
// total_records = f.data;
1864+
// }
1865+
// else {
1866+
// if (f.data_type === consts.BOOLEAN) {
1867+
// let true_count = f.value;
1868+
// let false_count = total_records - true_count;
1869+
// self.item._fields_summary_info[f.field_name] =
1870+
// {text: true_count + ' ✓ / ' + false_count + ' ✗',
1871+
// value: true_count};
1872+
// } else {
1873+
// self.item._fields_summary_info[f.field_name] =
1874+
// {text: f.display_text, value: f.value};
1875+
// }
1876+
// }
1877+
// });
1878+
//
1879+
// for (i = 0; i < count_fields.length; i++) {
1880+
// self.item._fields_summary_info[count_fields[i]] =
1881+
// {text: total_records + '', value: total_records};
1882+
// }
1883+
// self.update_summary()
1884+
// if (callback) {
1885+
// callback.call(this, total_records);
1886+
// }
1887+
// }
1888+
// );
1889+
// }
1890+
// }
1891+
//
1892+
// replace the standard calc_summary on all tables
1893+
calc_summary = function(callback) {
1894+
var self = this,
1895+
i,
1896+
copy,
1897+
field_name,
1898+
field,
1899+
fields,
1900+
count_field,
1901+
sum_fields,
1902+
count_fields,
1903+
total_records = 0,
1904+
funcs,
1905+
params = {};
1906+
1907+
if (!this.item._paginate || this.item.virtual_table) {
1908+
this.item.calc_summary(this.item, undefined, undefined, this.options.summary_fields);
1909+
}
1910+
else {
1911+
if (this.item.master) {
1912+
copy = task.item_by_ID(this.item.prototype_ID).copy({handlers: false, details: false});
18021913
}
18031914
else {
1804-
if (this.item.master) {
1805-
copy = task.item_by_ID(this.item.prototype_ID).copy({handlers: false, details: false});
1806-
}
1807-
else {
1808-
copy = this.item.copy({handlers: false, details: false});
1809-
}
1810-
count_field = copy._primary_key;
1811-
fields = [];
1812-
count_fields = [];
1813-
sum_fields = [];
1814-
funcs = {};
1815-
sum_fields.push(count_field);
1816-
funcs[count_field] = 'count';
1817-
for (i = 0; i < this.options.summary_fields.length; i++) {
1818-
field_name = this.options.summary_fields[i];
1819-
field = this.item.field_by_name(field_name);
1820-
if (field && this.fields.indexOf(field) !== -1) {
1821-
fields.push(field_name);
1822-
if (field.numeric_field()) {
1823-
sum_fields.push(field_name);
1824-
funcs[field_name] = 'sum';
1825-
}
1826-
else {
1827-
count_fields.push(field_name);
1828-
}
1915+
copy = this.item.copy({handlers: false, details: false});
1916+
}
1917+
1918+
count_field = copy._primary_key;
1919+
fields = [];
1920+
count_fields = [];
1921+
sum_fields = [];
1922+
funcs = {};
1923+
1924+
// always request row count via COUNT(pk)
1925+
sum_fields.push(count_field);
1926+
funcs[count_field] = 'count';
1927+
1928+
// set up aggregation funcs
1929+
for (i = 0; i < this.options.summary_fields.length; i++) {
1930+
field_name = this.options.summary_fields[i];
1931+
field = this.item.field_by_name(field_name);
1932+
if (field && this.fields.indexOf(field) !== -1) {
1933+
fields.push(field_name);
1934+
if (field.data_type === consts.BOOLEAN) {
1935+
sum_fields.push(field_name);
1936+
funcs[field_name] = 'sum'; // TRUE count
1937+
}
1938+
else if (field.numeric_field()) {
1939+
sum_fields.push(field_name);
1940+
funcs[field_name] = 'sum';
1941+
}
1942+
else {
1943+
count_fields.push(field_name);
18291944
}
18301945
}
1831-
for (var key in self.item._open_params) {
1832-
if (self.item._open_params.hasOwnProperty(key)) {
1833-
if (key.substring(0, 2) !== '__') {
1834-
params[key] = self.item._open_params[key];
1835-
}
1946+
}
1947+
1948+
// copy params
1949+
for (var key in self.item._open_params) {
1950+
if (self.item._open_params.hasOwnProperty(key)) {
1951+
if (key.substring(0, 2) !== '__') {
1952+
params[key] = self.item._open_params[key];
18361953
}
18371954
}
1838-
params.__summary = true;
1839-
if (self.item._open_params.__filters) {
1840-
copy._where_list = self.item._open_params.__filters;
1955+
}
1956+
params.__summary = true;
1957+
if (self.item._open_params.__filters) {
1958+
copy._where_list = self.item._open_params.__filters;
1959+
}
1960+
if (this.item.master) {
1961+
copy.ID = this.item.ID;
1962+
params.__master_id = this.item.master.ID;
1963+
params.__master_rec_id = this.item.master.field_by_name(this.item.master._primary_key).value;
1964+
}
1965+
1966+
this.item._fields_summary_info = {};
1967+
copy.open({fields: sum_fields, funcs: funcs, params: params}, function() {
1968+
1969+
copy.each_field(function(f, i) {
1970+
console.log(i, f.field_name, f.data_type, f.value, f.display_text);
1971+
});
1972+
1973+
var counts = {};
1974+
1975+
// collect all values
1976+
copy.each_field(function(f) {
1977+
counts[f.field_name] = f.value;
1978+
});
1979+
1980+
// get total row count safely from COUNT(pk)
1981+
total_records = counts[count_field] || 0;
1982+
1983+
// now build summaries
1984+
copy.each_field(function(f) {
1985+
if (f.field_name === count_field) {
1986+
total_records = f.data; // COUNT(*)
1987+
}
1988+
else if (f.data_type === consts.BOOLEAN) {
1989+
var true_count = f.data || 0; // f.data has SUM(PAID)
1990+
var false_count = total_records - true_count;
1991+
self.item._fields_summary_info[f.field_name] = {
1992+
text: true_count + ' ✓ / ' + false_count + ' ✗',
1993+
value: true_count
1994+
};
1995+
}
1996+
else {
1997+
self.item._fields_summary_info[f.field_name] = {
1998+
text: f.display_text,
1999+
value: f.value
2000+
};
2001+
}
2002+
});
2003+
2004+
2005+
// non-numeric, non-boolean fields → show row count
2006+
for (i = 0; i < count_fields.length; i++) {
2007+
self.item._fields_summary_info[count_fields[i]] = {
2008+
text: total_records + '',
2009+
value: total_records
2010+
};
18412011
}
1842-
if (this.item.master) {
1843-
copy.ID = this.item.ID
1844-
params.__master_id = this.item.master.ID;
1845-
params.__master_rec_id = this.item.master.field_by_name(this.item.master._primary_key).value;
2012+
2013+
self.update_summary();
2014+
if (callback) {
2015+
callback.call(this, total_records);
18462016
}
1847-
this.item._fields_summary_info = {};
1848-
copy.open({fields: sum_fields, funcs: funcs, params: params},
1849-
function() {
1850-
var i,
1851-
text;
1852-
copy.each_field(function(f, i) {
1853-
if (i == 0) {
1854-
total_records = f.data;
1855-
}
1856-
else {
1857-
self.item._fields_summary_info[f.field_name] =
1858-
{text: f.display_text, value: f.value};
1859-
}
1860-
});
1861-
for (i = 0; i < count_fields.length; i++) {
1862-
self.item._fields_summary_info[count_fields[i]] =
1863-
{text: total_records + '', value: total_records};
1864-
}
1865-
self.update_summary()
1866-
if (callback) {
1867-
callback.call(this, total_records);
1868-
}
1869-
}
1870-
);
1871-
}
2017+
});
18722018
}
2019+
};
18732020

18742021
update_field(field, refreshingRow) {
18752022
var self = this,

0 commit comments

Comments
 (0)