class ProjectReport extends Report { get name() { return "Projects"; } get fieldInformation() { return { period_key: { hidden: true }, date: { displayName: "Date", type: "date", aggregationLabel: "Total" }, full_name: { displayName: "Employee" }, id_invoice: { displayName: "Invoiced?", type: "boolean" }, work: { displayName: "Work" }, notes: { displayName: "Notes" }, tariff: { displayName: "Tariff", type: "currency" }, duration: { displayName: "Work Hours", type: "number", aggregation: "sum" }, total_value: { displayName: "Total Value", type: "currency", aggregation: "sum" } }; } get requestPath() { return "Reports/Project"; } buildTitleCell(fieldName, fieldInformation, cell) { super.buildTitleCell(fieldName, fieldInformation, cell); if (fieldName == "id_invoice") { cell.innerHTML += "
"; this.allInvoicedCheckbox = document.createElement("input"); this.allInvoicedCheckbox.type = "checkbox"; this.allInvoicedCheckbox.addEventListener("change", this.onMarkAllAsInvoicedCheckboxChanged.bind(this)); cell.appendChild(this.allInvoicedCheckbox); this.updateAllInvoicedCheckbox(); } } buildCell(fieldName, value, fieldInformation, cell, index) { super.buildCell(fieldName, value, fieldInformation, cell, index); //Create the checkbox list if it does not yet exist. if (!this.invoicedCheckboxes) { this.invoicedCheckboxes = [ ]; } if (fieldName == "id_invoice") { cell.innerHTML = ""; var periodKey = this.data[index][0]; var idInvoice = this.data[index][3]; var invoicedCheckbox = document.createElement("input"); invoicedCheckbox.type = "checkbox"; invoicedCheckbox.setAttribute("period-key", periodKey); invoicedCheckbox.checked = idInvoice !== null; invoicedCheckbox.addEventListener("change", this.onMarkAsInvoicedCheckboxChanged.bind(this)); cell.appendChild(invoicedCheckbox); this.invoicedCheckboxes[index] = invoicedCheckbox; } } buildCSVCell(fieldName, value, fieldInformation, index) { if (fieldName == "id_invoice") { var idInvoice = this.data[index][3]; return idInvoice !== null ? "✓" : "✗"; } return super.buildCSVCell(fieldName, value, fieldInformation, index); } onMarkAllAsInvoicedCheckboxChanged(event) { //Do nothing if there is no data. if (this.data.length <= 1) { return; } for (var i = 1; i < this.data.length; i++) { var row = this.data[i]; this.updatePeriodInvoiced(row[0], event.currentTarget.checked); } this.updateInvoicedCheckboxes(); } onMarkAsInvoicedCheckboxChanged(event) { var periodKey = event.currentTarget.getAttribute("period-key"); this.updatePeriodInvoiced(periodKey, event.currentTarget.checked); this.updateAllInvoicedCheckbox(); } updatePeriodInvoiced(periodKey, invoiced) { var period = OnTime.workTimeManager.getPeriod(periodKey); if (period.id_invoice > 0) { return; } var idInvoice = -1; if (!invoiced) { idInvoice = null; } var periodData = { period_key: periodKey, id_invoice: idInvoice }; OnTime.workTimeManager.updatePeriod(periodData); //Also update the period in the local report data. for (var i = 1; i < this.data.length; i++) { if (this.data[i][0] == periodKey) { this.data[i][3] = idInvoice; } } } updateAllInvoicedCheckbox() { //Iterate over all rows and decide about the checked or indeterminate state of the check all checkbox. var checkedCount = 0; for (var i = 1; i < this.data.length; i++) { if (this.data[i][3] !== null) { checkedCount++; } } this.allInvoicedCheckbox.indeterminate = checkedCount > 0 && checkedCount < this.data.length - 1; this.allInvoicedCheckbox.checked = this.data.length > 1 && checkedCount == this.data.length - 1; } updateInvoicedCheckboxes() { for (var i = 1; i < this.data.length; i++) { this.invoicedCheckboxes[i].checked = this.data[i][3] !== null; } } }