Class: Skr::Handlers::InvoiceFromTimeEntries

Inherits:
Lanes::API::ControllerBase
  • Object
show all
Defined in:
lib/skr/handlers/invoice_from_time_entries.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, authentication, params, data) ⇒ InvoiceFromTimeEntries

Returns a new instance of InvoiceFromTimeEntries.



5
6
7
8
9
10
11
# File 'lib/skr/handlers/invoice_from_time_entries.rb', line 5

def initialize(model, authentication, params, data)
    @options   = data
    @entry_ids = data['time_entry_ids']
    @project   = CustomerProject.find( data['customer_project_id'] )
    @location  = Location.default # should be set on project maybe?
    @sku_loc   = @project.sku.sku_locs.find_by(location: @location)
end

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/skr/handlers/invoice_from_time_entries.rb', line 13

def create
    invoice = Invoice.new(
        notes:    @options['notes'],
        po_num:   @options['po_num'] || @project.po_num,
        customer: @project.customer,
        customer_project: @project
    )
    @entry_ids.each do | entry_id |
        entry = TimeEntry.find(entry_id)
        invoice.lines.build(
            time_entry: entry,
            sku_loc: @sku_loc,
            price: @project.rates['hourly'],
            description: entry.description,
            qty: ((entry.end_at - entry.start_at) / 1.hour)
        )
    end
    std_api_reply :create, { invoice: invoice }, success: invoice.save
end