Created
April 7, 2020 08:25
-
-
Save wojtha/6e2488fb42a88224520fca92bb426996 to your computer and use it in GitHub Desktop.
Script for easy CZK-USD invoicing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'ostruct' | |
require 'optparse' | |
module Fakturuj | |
class Application | |
# https://github.com/honzajavorek/cs-apis/wiki/Kurzy-devizov%C3%A9ho-trhu-%C4%8CNB | |
CNB_DENNI_KURZ_TXT = URI('https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt').freeze | |
def run(argv) | |
@options = parse_cli_options(argv) | |
@result = calculate | |
$stdout.puts format_result | |
end | |
attr_reader :options, :result | |
private | |
def parse_cli_options(argv) | |
options = OpenStruct.new | |
options.castka = 0.0 | |
options.minula_castka = 0.0 | |
options.zaplaceno = 0.0 | |
opt_parser = OptionParser.new do |opts| | |
opts.banner = <<~BANNER | |
Pouziti: fakturuj CASTKA [--minule CASTKA] [--zaplaceno CASTKA] [--datum DD.MM.RRRR] | |
Napriklad: fakturuj 21000 --minule 21000 --zaplaceno 18938 --datum 31.3.2020 | |
BANNER | |
opts.separator '' | |
opts.separator 'Minula faktura:' | |
opts.on('-m', '--minule CASTKA', Float, 'Minule fakturovana castka') do |value| | |
options.minula_castka = value | |
end | |
opts.on('-z', '--zaplaceno CASTKA', Float, 'Zaplacena castka') do |value| | |
options.zaplaceno = value | |
end | |
opts.separator 'Kurz CZK-USD:' | |
opts.on('-d', '--datum DATUM', String, 'Datum ve formatu dd.mm.rrrr') do |value| | |
options.datum = value | |
end | |
end | |
opt_parser.parse!(argv) | |
options.castka = Float(argv[0]) | |
options.datum ||= Time.now.strftime('%m.%d.%Y') | |
options | |
end | |
def calculate | |
OpenStruct.new.tap do |r| | |
r.castka = options.castka | |
r.minula_castka = options.minula_castka | |
r.zaplaceno = options.zaplaceno | |
r.datum = options.datum | |
r.doplatek = r.minula_castka - r.zaplaceno | |
r.platba_czk = r.castka + r.doplatek | |
r.kurz_usd = kurz_cnb | |
r.platba_usd = r.platba_czk / r.kurz_usd | |
end | |
end | |
def format_result | |
<<~TXT % result.to_h | |
Aktualni castka: %<castka>12.02f CZK | |
Castka na minule fakture: %<minula_castka>12.02f CZK | |
Minule zaplaceno: %<zaplaceno>12.02f CZK | |
Doplatek: %<doplatek>12.02f CZK | |
------------------------------------------------------ | |
K platbe celkem: %<platba_czk>12.02f CZK | |
%<platba_usd>12.02f USD | |
------------------------------------------------------ | |
Kurz CNB USD-CZK k %<datum>s: %<kurz_usd>.3f CZK | |
TXT | |
end | |
def kurz_cnb(country = 'USA') | |
uri = CNB_DENNI_KURZ_TXT.dup | |
uri.query = "date=#{options.datum}" if options.datum | |
body = Net::HTTP.get(uri) | |
data = body.split("\n").map { |l| l.split('|') } | |
usa_data = data.find { |r| r[0] == country } | |
Float(usa_data.last.sub(',', '.')) | |
end | |
end | |
end | |
if File.identical?(__FILE__, $PROGRAM_NAME) | |
app = Fakturuj::Application.new | |
app.run(ARGV) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment