| 1 | #!/usr/local/bin/perl | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # ABSTRACT: A simple hourly rate calculator | ||
| 6 | # PODNAME: hourly_rate.pl | ||
| 7 | |||
| 8 | use Getopt::Long; | ||
| 9 | use Pod::Usage; | ||
| 10 | use Config::Any; | ||
| 11 | use File::Spec::Functions qw(catfile); | ||
| 12 | use Finance::Salary::Rate; | ||
| 13 | |||
| 14 | my %opts = ( | ||
| 15 | help => 0, | ||
| 16 | config => catfile($ENV{HOME}, qw (.config finance-salary rate.conf)), | ||
| 17 | ); | ||
| 18 | |||
| 19 | { | ||
| 20 | local $SIG{__WARN__}; | ||
| 21 | my $ok = eval { | ||
| 22 | GetOptions( | ||
| 23 | \%opts, qw( | ||
| 24 | help | ||
| 25 | income=s | ||
| 26 | vacation=s | ||
| 27 | taxes=s | ||
| 28 | healthcare=s | ||
| 29 | declarable=s | ||
| 30 | days=i | ||
| 31 | expenses=s | ||
| 32 | config=s | ||
| 33 | ) | ||
| 34 | ); | ||
| 35 | }; | ||
| 36 | if (!$ok) { | ||
| 37 | die($@); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | pod2usage(0) if ($opts{help}); | ||
| 42 | |||
| 43 | |||
| 44 | if (-f $opts{config}) { | ||
| 45 | my $config = Config::Any->load_files({ | ||
| 46 | files => [$opts{config}], | ||
| 47 | use_ext => 1, | ||
| 48 | flatten_hash => 1, | ||
| 49 | |||
| 50 | })->[0]{$opts{config}}; | ||
| 51 | |||
| 52 | foreach (keys %opts) { | ||
| 53 | delete $config->{$_}; | ||
| 54 | } | ||
| 55 | |||
| 56 | foreach (keys %$config) { | ||
| 57 | # If an option is set multiple times in the config file, take | ||
| 58 | # the last value and work with that | ||
| 59 | $opts{$_} ||= ref $config->{$_} eq 'ARRAY' | ||
| 60 | ? $config->{$_}[-1] | ||
| 61 | : $config->{$_}; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | my @required = qw(income); | ||
| 66 | my @optional = qw(vacation taxes healthcare declarable days expenses); | ||
| 67 | my $nok = 0; | ||
| 68 | |||
| 69 | foreach (@required) { | ||
| 70 | if (!exists $opts{$_} || $opts{$_} <= 0) { | ||
| 71 | $nok++; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | foreach (@optional) { | ||
| 76 | if (exists $opts{$_} && $opts{$_} < 0) { | ||
| 77 | $nok++; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | pod2usage(1) if $nok; | ||
| 82 | |||
| 83 | my %mapping = ( | ||
| 84 | income => 'monthly_income', | ||
| 85 | vacation => 'vacaction_perc', | ||
| 86 | taxes => 'tax_perc', | ||
| 87 | healthcare => 'healthcare_perc', | ||
| 88 | declarable => 'declarable_days_perc', | ||
| 89 | days => 'working_days', | ||
| 90 | expenses => 'expenses', | ||
| 91 | ); | ||
| 92 | |||
| 93 | my %args = map { $mapping{$_} => $opts{$_} } grep { $opts{$_} } keys %mapping; | ||
| 94 | |||
| 95 | my $rate = Finance::Salary::Rate->new(%args); | ||
| 96 | printf ("%0.2f\n", $rate->hourly_rate); | ||
| 97 | |||
| 98 | __END__ |