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: =>

Invoice # @context.viewport.hideModal() } /> was successfully created.

setNewEntriesProject: (entry) -> entry.set({customer_project: @request.customer_project}) onSelectAll: -> rs = @query.results rs.eachRow (row) -> xd = rs.xtraData(row) xd.selected = false == xd.selected @query.changeCount++ renderToggleAllButton: -> return null if 0 is @query.results.length Toggle render: ->
Create Invoice

Total:

hoursForRow = (row) -> [from, to] = if _.isArray(row) then [ row[2], row[1] ] else [row.end_at, row.start_at] hours = _.moment(from).diff(to, 'hours', true) Math.round(hours * 100) / 100 class InvoiceRequest extends Lanes.Models.Base props: customer_project_id:{"type":"integer"} time_entry_ids: { type: 'array', default: -> [] } api_path: -> "/skr/invoices/from-time-entries" associations: customer_project: { model: "CustomerProject", autoCreate: true } invoice: { model: 'Invoice' }