execute(input) database = service "database_sales" start = Dates.startOfDay(input.day, input.timezone) nextDay = Dates.add(input.day, 1, "days") until = Dates.startOfDay(nextDay, input.timezone) filter = { and: [ { "supplier.id": input.supplierId, deleted: false }, { timestamp: { op: "gte", value: start } }, { timestamp: { op: "lt", value: until } } ] } cursor = database.query({ from: "PurchaseOrder (id, reference, total)", where: filter, orderBy: { timestamp: "desc", id: "desc" }, offset: input.offset, count: input.count }) orders = [] for each record in cursor orders.add({ id: record.id, reference: record.reference, total: record.total }) end // Count and page share the filter. pageTotal covers only this page. total = database.count({ from: "PurchaseOrder", where: filter }) pageTotal = sum order.total for each order in orders return { orders: orders, total: total, pageTotal: pageTotal, from: start, until: until } end