Class: GitQuickBooks::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/gitquickbooks/cli.rb

Overview

Provides two CLI actions init and tally

Instance Method Summary collapse

Instance Method Details

#authorizeObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitquickbooks/cli.rb', line 14

def authorize
  api = GitQuickBooks::Api.new

  say 'To use Quickbooks, you need to grant access to the app.'
  say 'Press enter to launch your web browser and grant access.'

  Launchy.open api.authorize_url

  oauth_verifier = ask 'Now, copy the PIN below and press enter:'

  api.authorize(oauth_verifier)
end

#companiesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitquickbooks/cli.rb', line 28

def companies
  api = GitQuickBooks::Api.new

  @customer_service = Quickbooks::Service::Customer.new
  @customer_service.access_token = api.access_token
  @customer_service.company_id = api.realm

  # Called without args you get the first page of results
  @customer_service.query_in_batches(nil, per_page: 20) do |batch|
    batch.each do |customer|
      puts "#{customer.id} : #{customer.display_name || customer.method}"
    end
  end
end

#processObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gitquickbooks/cli.rb', line 54

def process
  path = File.expand_path(options.file)
  start_date = Date.parse(options.start_on)
  end_date = Date.today

  # == Setup QB
  GitWakaTime.config.setup_local_db
  api = GitQuickBooks::Api.new
  @time_service = Quickbooks::Service::TimeActivity.new
  @time_service.access_token = api.access_token
  @time_service.company_id = api.realm

  @customer_service = Quickbooks::Service::Customer.new
  @customer_service.access_token = api.access_token
  @customer_service.company_id = api.realm

  customer = @customer_service.fetch_by_id(options.customer)

  puts "running for #{customer.display_name} for #{start_date} #{end_date}".blue

  return unless customer
  @total_time = 0

  controller = GitWakaTime::Controller.new(path: path, date: start_date)
  @timer = controller.timer

  @slips = @timer.map do |date, commits|
    next unless date >= start_date
    next unless date <= end_date
    commits = commits.reject { |c| c.message.to_s.downcase.delete(' ').include?('nocharge') }

    total_time = commits.map(&:time_in_seconds).compact.reduce(&:+) || 0

    # has a local time flaw
    recorded_time = GitWakaTime::Heartbeat.where(
      'DATE(time) >= ? and DATE(time) <= ? ', date.to_time.utc.to_date, date.to_time.utc.to_date
    ).where(project: controller.project).sum(:duration) || 0

    msgs = commits.map(&:message)
    message = CommitMsgCleaner.new(msgs).call

    @total_time += total_time

    hours   =  (total_time / 60.0 / 60).floor
    minutes =  (total_time / 60.0).remainder(60).ceil

    GitWakaTime::Log.new format(
      '%-100s %-30s %-30s %30s'.purple,
      date,
      "Recorded #{ChronicDuration.output recorded_time}",
      "Commited #{ChronicDuration.output total_time}",
      seconds_to_money(total_time, rate: options.rate)
    )

    message.split("\n").each do |msg_line|
      GitWakaTime::Log.new format(
        '%-120s'.green,
        msg_line
      )
    end

    # commits.each do |commit|
    #   GitWakaTime::Log.new format(
    #     '%-120s %-30s %30s'.green,
    #     commit.message.split("\n").first,
    #     "Total #{ChronicDuration.output commit.time_in_seconds.to_f}",
    #     seconds_to_money(commit.time_in_seconds, rate: options.rate)
    #   )
    # end

    slip = Quickbooks::Model::TimeActivity.new
    slip.txn_date = date
    slip.customer_id = options.customer
    slip.name_of = 'Employee'
    slip.employee_id = 196

    slip.billable_status = 'Billable' # or NotBillable
    slip.taxable = false
    slip.hours = hours
    slip.hourly_rate = options.rate
    slip.minutes = minutes
    slip.description = message

    slip
  end

  GitWakaTime::Log.new format(
    '%-120s %-30s %30s'.red,
    "#{@slips.size} slips",
    "Total #{ChronicDuration.output @total_time}",
    seconds_to_money(@total_time, rate: options.rate)
  )

  return unless options.add_to_qb
  # Send slips to Quickbooks Online
  @slips.compact.each do |slip|
    next unless ((slip.hours * 60) + slip.minutes) > 0
    puts 'Saving Slip'
    @time_service.create(slip)
  end
end