Module: AsciiMixins

Defined in:
lib/ascii_invoicer/mixins.rb

Instance Method Summary collapse

Instance Method Details

#caterers_string(project, join = ", ") ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/ascii_invoicer/mixins.rb', line 181

def caterers_string project, join = ", "
  data = project.data
  data[:hours][:caterers].map{
    |name, hours|
    if (hours.class == Fixnum || hours.class == Float ) and hours > 0
    "#{name} (#{hours})"
    end
  }.keep_if{|e|e}
  .join join if data[:hours][:caterers]
end

#check_project(path) ⇒ Object



297
298
299
300
301
302
303
304
305
306
# File 'lib/ascii_invoicer/mixins.rb', line 297

def check_project(path)
  project = InvoiceProject.new $SETTINGS
  project.open path
  unless project.validate(:offer)
    puts "\nWARNING: the file you just edited contains errors! (#{project.errors})"
    unless no? "would you like to edit it again? [y|N]"
      edit_files path
    end
  end
end

#color_from_date(date) ⇒ Object

TODO turn color_from_date(date) into a loopuk into $SETTINGS



60
61
62
63
64
65
66
67
68
69
# File 'lib/ascii_invoicer/mixins.rb', line 60

def color_from_date(date)
  diff = date - Date.today
  return (rand * 256**3).to_i.to_s(16) if Date.today.day == 1 and Date.today.month == 4 #april fools
  return :magenta                      if diff < -28
  return :cyan                         if diff < 0
  return [:yellow,:bright]             if diff == 0
  return :red                          if diff < 7
  return :yellow                       if diff < 14
  return [:green]
end

#create_cal_file(projects) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ascii_invoicer/mixins.rb', line 155

def create_cal_file(projects)
  cal = Icalendar::Calendar.new
  projects.each_index do |i|
    project = projects[i]
    events = project.data[:event][:calendaritems]
    if events
      events.each { |event| cal.add_event event}
    else
      $logger.warn "Calendar can't be parsed. (#{project.data[:name]})", :file
    end
  end

  cal_file_path = File.join(FileUtils.pwd, $SETTINGS.calendar_file)
  cal_file = File.open(cal_file_path, ?w)
  cal_file.write cal.to_ical
  puts "created #{cal_file_path}"
end

#display_all(project, choice, show_errors = true) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/ascii_invoicer/mixins.rb', line 250

def display_all project, choice, show_errors = true
  raise "choice must be either :invoice or :offer" unless choice == :invoice or choice == :offer
  data = project.data

  table = Textboxes.new
  table.style[:border] = true
  table.title = "#{choice}:" + "\"#{data[:event][:name]}\"".rjust(25)
  table.add_row [nil, "name", "amount","price", "cost"]
  table.set_alignments :r, :l, :r, :r, :r

  i = 0
  data[:products].each {|product|
    amount = product.amount choice
    price = product.price
    cost  = product.cost choice
    table.add_row [i+=1,product.name, amount, price, cost]
  }
  table.add_row [i+1,"service", "#{data[:hours][:time]}h",  data[:hours][:salary], data[:hours][:total]] if project.data.get_path('hours/time').to_i> 0

  separator = table.column_widths.map{|w| ?=*w}
  separator[0] = nil
  table.add_row separator

  table.add_row  [nil,"Kosten",nil,nil,"#{data[choice][:costs]}"]
  data[:productsbytax].each {|tax,products|
    tpv = 0.to_euro # tax per value
    tax = (tax.rationalize * 100).to_f
    products.each{|p|
      tpv += p.hash[:tax_offer]   if choice == :offer
      tpv += p.hash[:tax_invoice] if choice == :invoice
    }
    table.add_row  [nil, "MWST #{tax}%",nil,nil,"#{tpv}"]
  }
  table.add_row   [nil,  "Final", nil, nil, "#{data[choice][:final]}"]

  if show_errors
    table.footer = "Errors: #{project.errors.length} (#{ project.errors.join ',' })" if project.errors.length >0
  end

  return table
end

#display_products(project, choice = :offer, standalone = true) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/ascii_invoicer/mixins.rb', line 233

def display_products project, choice = :offer, standalone = true
  table.style[:border] = standalone
  table.title = "#{choice}:" + "\"#{project.data[:event][:name]}\"".rjust(25) if standalone
  table.add_row ["#", "name", "price", "cost"]
  table.set_alignments :r, :l, :r, :r
  project.data[:products].each {|product|
    amount = product.amount choice
    price = product.price
    cost  = product.cost choice
    table.add_row [amount, product.name, price, cost]
  }
  table.add_row ["#{project.data[:hours][:time]}h", "service" , project.data[:hours][:salary], project.data[:hours][:total]] if project.data.get_path('hours/time').to_i> 0
  table.add_row [nil, caterers_string(project)]

  return table
end

#display_products_csv(project) ⇒ Object



292
293
294
295
# File 'lib/ascii_invoicer/mixins.rb', line 292

def display_products_csv project
  puts [['name', 'price', 'amount', 'sold', 'tax_value'].to_csv(col_sep:?;)]+
  project.data[:products].map{|p| p.to_csv(col_sep:?;)}
end

#edit_files(paths, editor = $SETTINGS.editor) ⇒ Object

hand path to editor



321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/ascii_invoicer/mixins.rb', line 321

def edit_files(paths, editor = $SETTINGS.editor)
  paths = [paths] if paths.class == String
  paths.select! {|path| path}
  if paths.empty?
    $logger.error "no paths to open"
    return false
  end
  paths.map!{|path| "\"#{path}\"" }
  paths = paths.join ' '
  editor = $SETTINGS.editor unless editor
  $logger.info "Opening #{paths} in #{editor}"
  pid = spawn "#{editor} #{paths}"
  Process.wait pid
end

#open_file(path) ⇒ Object

hand path to default programm



309
310
311
312
313
314
315
316
317
318
# File 'lib/ascii_invoicer/mixins.rb', line 309

def open_file path
  unless path.class == String and File.exists? path
    $logger.error "Cannot open #{path}", :both
    return false
  end
  opener = $SETTINGS.opener
  $logger.info "Opening #{path} in #{opener}"
  pid = spawn "#{opener} \"#{path}\""
  Process.wait pid
end


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
# File 'lib/ascii_invoicer/mixins.rb', line 71

def print_project_list projects, hash = {}
  table = Textboxes.new
  table.style[:border]             = false
  table.style[:column_borders]     = false
  table.style[:row_borders]        = false
  table.style[:padding_horizontal] = 1
  projects.each_index do |i|
    project  = projects[i]
    if  Date.today.day == 1 and Date.today.month == 4
      color = color_from_date(project.date)
    elsif !hash[:colors].nil? and hash[:colors]
      color = color_from_date(project.date)
      color = :default if project.validate(:invoice)
      color = [:blue] if project.status == :canceled
    end
    if hash[:verbose]
      row = print_row_verbose project, hash
    else
      row = print_row_simple project, hash
    end

    row << caterers_string(project) if hash[:caterers] and project.data[:hours][:caterers]
    row << wages_string(project) if hash[:wages] and project.data[:hours][:caterers]

    row << project.blockers(:archive)      if hash[:blockers]
    if hash[:details]
      hash[:details].each {|detail|
        row << project.data.get_path(detail)
      }
    end

    row << project.errors                  if hash[:errors] and project.status == :ok
    row << project.status                  if hash[:errors] and project.status == :canceled
    row.insert 0, i+1
    table.add_row row, color
  end
  table.set_alignments(:r, :l, :l)
  puts table
end

takes an array of invoices (@plumber.working_projects)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ascii_invoicer/mixins.rb', line 199

def print_project_list_csv(projects)
  header = [
    'Rnum',
    'Bezeichnung',
    'Datum',
    'Rechnungsdatum',
    'Betreuer',
    'verantwortlich',
    'Bezahlt am',
    'Betrag',
    'Canceled',
  ]
  puts header.to_csv(col_sep:";")
  projects.each do |p|
    canceled = ""
    canceled = "canceled" if p.data[:canceled]
    line = [
      p.data[:invoice][:number],
      p.data[:event][:name],
      p.data[:event][:date],
      p.data[:invoice][:date],
      caterers_string(p),
      p.data[:manager].words[0],
      p.data[:invoice][:payed_date],
      p.data[:invoice][:final],
      canceled,
      #  p.valid_for[:invoice]
    ]
    canceled = "canceled" if p.data[:canceled]
    line.map! {|v| v ? v : "" } # wow, that looks cryptic
    puts line.to_csv(col_sep:";")
  end
end


141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ascii_invoicer/mixins.rb', line 141

def print_project_list_paths(projects)
  table = Textboxes.new
  projects.each_index do |i|
    p  = projects[i]
    table.add_row [
      (i+1).to_s+".",
      p.name.ljust(35),
      p.data[:project_path]
    ]
  end
  table.set_alignments(:r, :l, :l)
  puts table
end

takes an array of invoices (@plumber.working_projects)



174
175
176
177
178
179
# File 'lib/ascii_invoicer/mixins.rb', line 174

def print_project_list_yaml(projects)
  projects.each do |p|
    puts p.data.to_yaml
    puts "...\n\n"
  end
end


111
112
113
114
115
116
117
118
119
120
# File 'lib/ascii_invoicer/mixins.rb', line 111

def print_row_simple(project,hash)
  row = [
    project.pretty_name,
    project.data[:manager],
    project.data[:event][:invoice_number],
    project.data[:event][:date].strftime("%d.%m.%Y"),
    #project.index
  ]
  return row
end


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ascii_invoicer/mixins.rb', line 122

def print_row_verbose (project, hash)
  name = "##{project.data[:name]}#"
  if not project.data[:event][:name].nil? and project.data[:event][:name].size > 0
    name = project.data[:event][:name]
    name = "CANCELED: " + name if project.data :canceled
  end
  row = [
    name,
    project.data[:manager],
    project.data[:invoice][:number],
    project.date.strftime("%d.%m.%Y"),
    project.state_sign(:offer),
    project.state_sign(:invoice),
    project.validate(:payed).print($SETTINGS.currency_symbol),
    # try these: ☑☒✉☕☀☻
  ]
  return row
end

#render_project(project, choice) ⇒ Object

Use Option parser or leave it if only one argument is given



50
51
52
53
54
55
56
57
# File 'lib/ascii_invoicer/mixins.rb', line 50

def render_project project, choice
  project.validate choice
  if project.valid_for[choice]
    project.create_tex choice, options[:check]
  else
    $logger.error "#{project.name} is not ready for creating an #{choice.to_s}! #{project.data[:valid]} #{project.errors if project.errors.length > 0}"
  end
end

#wages_string(project, join = ", ") ⇒ Object



192
193
194
195
196
# File 'lib/ascii_invoicer/mixins.rb', line 192

def wages_string project, join = ", "
    data = project.data
    salary = data[:hours][:salary]
    data[:hours][:caterers].map{|name, hours| "#{name} (#{salary* hours})" if hours > 0 }.join join if data[:hours][:caterers]
end