Docs‎ > ‎Live Logic Tutorial‎ > ‎

Group Sales By Employee Month

Under construction.

Create Early Action

Create the empsales:

var enabled = true;
if (row.salesrep_name === null)
    row.salesrep_name = "charlie";  // he needs the help
if (enabled === false || row.salesrep_name === null || row.empsales_ident !== null) return;

// hook this item to the groupBy empsales (child of employees, parent of orders)
// this creates a total of sales / emp for each month / year
var month = 3; //moment(row.placed_date).month(Number);
var year = 2014; //moment(row.placed_date).year(Number);
var employee_name = row.salesrep_name;
var empsalesSQL = "select * from empsales where month=" + month + " and year=" + year + " and employee_name = '" + employee_name + "'";
log.debug("**** seeking empsalesSQL: " + empsalesSQL);
var empsalesRow = null;
var rows = logicContext.getRowsByQuery("empsales", empsalesSQL);
for (var i = 0; i < rows.length; i++) {
    if (empsalesRow === null) {
        empsalesRow = rows[i];
    } else {
        throw "orders.GroupSales error - 2 rows returned for primary key query: " + rows[i];
    }
}
if (empsalesRow === null) {
    log.debug("**** creating empsales: " + employee_name + year + month);
    empsalesRow = logicContext.createPersistentBean("empsales");
    empsalesRow.employee_name = employee_name;
    empsalesRow.month = month;
    empsalesRow.year = year;
    logicContext.insert(empsalesRow);
    log.debug("**** empsales created: " + empsalesRow);
} else {
    log.debug("*** empsales already existed")
}
row.empsales_ident = empsalesRow.ident; // row.empSales.ident
log.debug("**** order linked to empsales: " + row.empSales);


Order Dates

Placed:
if (row.placed_date === null)
    return new Date(); // moment();
else
    return row.placed_date;

ordered:
if (row.placed_date === null)
    return new Date(); // moment();
else
    return row.placed_date;

empSales total

Not firing on placing 2nd order (existing empsales):


Comments