class Skr.Screens.TimeInvoicing extends Skr.Screens.Base getInitialState: -> isEditing: true commands: new Skr.Screens.Commands(this, modelName: 'request') dataObjects: request: -> new InvoiceRequest query: -> @gridSelections = new LC.Grid.Selections(onChange: @updateTotal) new Lanes.Models.Query({ defaultSort: 'start_at', src: Skr.Models.TimeEntry, fields: [ { id:'id', visible: false } @gridSelections { id: 'start_at', fixedWidth: 200, format: Lanes.u.format.shartDateTime }, { id: 'end_at', fixedWidth: 200, format: Lanes.u.format.shartDateTime }, { id: 'hours', fixedWidth: 80, textAlign: 'center', editable: false, query: false, format: (v, row) -> hoursForRow(row) } { id: 'description', flex: 1} ] }) setDataState: (state) -> @updateTotal() @setState(state) updateTotal: -> total = _.bigDecimal('0') rate = @request.customer_project.rates?.hourly return unless rate # the first call is when the model isn't parsed yet selectedRows = 0 @query.results.eachRow (row, xd) -> unless xd && false == xd.selected selectedRows += 1 total = total.add( hoursForRow(row) * rate ) selectionState = if selectedRows is @query.results.length then 'all' else if selectedRows then 'some' else 'none' @setState({total, selectionState}) editors: -> selected: ({query, rowIndex}) -> x = query.results.xtraData(rowIndex) selected = false != x.selected x.selected = ev.target.checked} /> onModelSet: (project) -> @request.set(customer_project: project) @query.syncOptions = { query: { customer_project_id: project.id, is_invoiced: false } } @query.results.reload() createInvoice: -> idIndex = @query.idIndex @request.time_entry_ids = [] @query.results.eachRow (row, xd) => return if xd and false == xd.selected @request.time_entry_ids.push(row[idIndex]) @request.save(saveAll: true).then (req) => @query.results.reload() @context.viewport.displayModal(@displayInvoiceResults(req)) displayInvoiceResults: (req) -> title: "Invoice created …" buttons: [{ title: 'OK', style: 'primary' }] autoHide: true body: =>