summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LICENSE21
-rw-r--r--kakeibo-monthly.pl33
2 files changed, 54 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..1baf524
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Masaya Tojo
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/kakeibo-monthly.pl b/kakeibo-monthly.pl
new file mode 100644
index 0000000..160351a
--- /dev/null
+++ b/kakeibo-monthly.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+use List::Util qw(sum);
+
+my (%cost, %month);
+while (<>) {
+ chomp;
+ s/^(\d{4}-\d{2})-\d{2}/$1/; # YYYY-MM-DD -> YYYY-MM
+ my ($month, $category, undef, $cost) = split /\t/;
+ $month{$month} = 1;
+ $cost{$category} = {} unless $cost{$category};
+ &check_consistency($cost{$category}->{$month}, $cost, $month, $category);
+ $cost{$category}->{$month} += $cost;
+}
+
+my @category = sort(keys %cost);
+my @month = sort(keys %month);
+say join("\t" => "Month", @category, "Out", "In", "Total");
+for my $month (@month) {
+ my @cost = map { $cost{$_}->{$month} // 0 } @category;
+ my $out = sum(grep { $_ > 0 } @cost) // 0;
+ my $in = sum(grep { $_ < 0 } @cost) // 0;
+ my $total = sum(@cost) // 0;
+ say join("\t" => $month, @cost, $out, $in, $total);
+}
+
+# 月毎にカテゴリーのコストの正負が一貫していることをチェックする
+sub check_consistency($sum, $cost, $month, $category) {
+ !defined($sum) || $sum >= 0 == $cost >= 0
+ or die "$month の $category カテゴリーのコストの正負が一貫していません";
+}