Added Reports (forgot to commit); Added label prototype.
This commit is contained in:
@@ -3,7 +3,7 @@ import { Mongo } from 'meteor/mongo';
|
||||
import { check } from 'meteor/check';
|
||||
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
|
||||
|
||||
Measures = new Mongo.Collection('Measures');
|
||||
let Measures = new Mongo.Collection('Measures');
|
||||
Measures.attachSchema(new SimpleSchema({
|
||||
name: {
|
||||
type: String,
|
||||
|
||||
@@ -2,17 +2,18 @@ import { Meteor } from 'meteor/meteor';
|
||||
|
||||
if(Meteor.isServer) {
|
||||
WebApp.connectHandlers.use("/reports/AnnualTotals", (req, res, next) => {
|
||||
const separator = ";";
|
||||
try {
|
||||
let result = Meteor.collections.Sales.aggregate([{$group: {_id: {$substr: ['$date', 0, 4]}, total: {$sum: {$multiply: ["$price", "$amount"]}}}}]);
|
||||
|
||||
result.toArray().then(function(result) {
|
||||
res.writeHead(200, {'Content-Type': 'text/csv'});
|
||||
|
||||
res.write("Year,Sales Total\n");
|
||||
res.write("Year" + separator + "Sales Total ($)\n");
|
||||
|
||||
for(let i = 0; i < result.length; i++) {
|
||||
res.write(result[i]._id);
|
||||
res.write(",");
|
||||
res.write(separator);
|
||||
res.write("" + result[i].total);
|
||||
res.write("\n");
|
||||
}
|
||||
@@ -25,13 +26,14 @@ if(Meteor.isServer) {
|
||||
}
|
||||
});
|
||||
WebApp.connectHandlers.use("/reports/MonthlyTotals", (req, res, next) => {
|
||||
const separator = ";";
|
||||
try {
|
||||
let result = Meteor.collections.Sales.aggregate([{$group: {_id: {$substr: ['$date', 0, 6]}, total: {$sum: {$multiply: ["$price", "$amount"]}}}}]);
|
||||
|
||||
result.toArray().then(function(result) {
|
||||
res.writeHead(200, {'Content-Type': 'text/csv'});
|
||||
|
||||
res.write("Date,Sales Total\n");
|
||||
res.write("Date" + separator + "Sales Total ($)\n");
|
||||
|
||||
result.sort(function(a, b) {
|
||||
return parseInt(a._id) - parseInt(b._id);
|
||||
@@ -39,7 +41,7 @@ if(Meteor.isServer) {
|
||||
|
||||
for(let i = 0; i < result.length; i++) {
|
||||
res.write(result[i]._id.substr(4, 2) + "/" + result[i]._id.substr(0, 4));
|
||||
res.write(",");
|
||||
res.write(separator);
|
||||
res.write("" + result[i].total);
|
||||
res.write("\n");
|
||||
}
|
||||
@@ -51,7 +53,72 @@ if(Meteor.isServer) {
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
WebApp.connectHandlers.use("/reports/AnnualSaleCountsByMeasure", (req, res, next) => {
|
||||
const separator = ";";
|
||||
try {
|
||||
let result = Meteor.collections.Sales.aggregate([{
|
||||
$group: {
|
||||
_id: {$concat: [{$substr: ['$date', 0, 4]}, "-", "$measureId"]},
|
||||
measureId: {$first: "$measureId"},
|
||||
year: {$first: {$substr: ['$date', 0, 4]}},
|
||||
count: {$sum: "$amount"},
|
||||
total: {$sum: {$multiply: ["$price", "$amount"]}}
|
||||
}
|
||||
}]);
|
||||
|
||||
result.toArray().then(function(result) {
|
||||
let totalByYear = {};
|
||||
|
||||
//Create a map of maps: year -> measure id -> sales count.
|
||||
for(let next of result) {
|
||||
//Get the map of totals by product for the year.
|
||||
let totalByMeasure = totalByYear[next.year];
|
||||
//Create the map if necessary.
|
||||
if(totalByMeasure === undefined) totalByMeasure = totalByYear[next.year] = {};
|
||||
|
||||
totalByMeasure[next.measureId] = {count: next.count, total: next.total};
|
||||
}
|
||||
|
||||
//Create a list of ordered measures. We could use a map, but then getting the ordering correct would be difficult.
|
||||
let measures = Meteor.collections.Measures.find({}, {fields: {_id: 1, name: 1}, sort: {order: 1}}).fetch();
|
||||
|
||||
//Collect the years in ascending oder.
|
||||
let years = Object.keys(totalByYear).sort(function(a, b) {return parseInt(a) - parseInt(b);});
|
||||
|
||||
res.writeHead(200, {'Content-Type': 'text/csv'});
|
||||
|
||||
res.write("Year" + separator + "Measure" + separator + "Sales Count" + separator + "Sales Total ($)\n");
|
||||
|
||||
result.sort(function(a, b) {
|
||||
return parseInt(a._id) - parseInt(b._id);
|
||||
});
|
||||
|
||||
for(let year of years) {
|
||||
for(let measure of measures) {
|
||||
let totals = totalByYear[year][measure._id];
|
||||
|
||||
if(totals) {
|
||||
res.write(year);
|
||||
res.write(separator);
|
||||
res.write(measure.name);
|
||||
res.write(separator);
|
||||
res.write("" + totals.count);
|
||||
res.write(separator);
|
||||
res.write("" + totals.total);
|
||||
res.write("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.end();
|
||||
});
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
WebApp.connectHandlers.use("/reports/TagTotals", (req, res, next) => {
|
||||
const separator = ";";
|
||||
try {
|
||||
//Aggregate all the sales by product id & year, then later create a map between products and tags to create tag totals.
|
||||
let result = Meteor.collections.Sales.aggregate([{
|
||||
@@ -111,7 +178,7 @@ if(Meteor.isServer) {
|
||||
|
||||
//Iterate over the years and add them to the headers.
|
||||
for(let year of years) {
|
||||
res.write("," + year);
|
||||
res.write(separator + year);
|
||||
}
|
||||
|
||||
res.write('\n');
|
||||
@@ -135,7 +202,7 @@ if(Meteor.isServer) {
|
||||
if(productAnnualTotal) annualTotal += productAnnualTotal;
|
||||
}
|
||||
|
||||
res.write("," + annualTotal);
|
||||
res.write(separator + annualTotal);
|
||||
}
|
||||
|
||||
res.write('\n');
|
||||
@@ -201,7 +268,7 @@ if(Meteor.isServer) {
|
||||
|
||||
//Iterate over the years and add them to the headers.
|
||||
for(let year of years) {
|
||||
res.write(separator + year + " Count" + separator + year + " Total");
|
||||
res.write(separator + year + " Count" + separator + year + " Total ($)");
|
||||
}
|
||||
|
||||
res.write('\n');
|
||||
|
||||
Reference in New Issue
Block a user