From c1fb530eea90fcf7c1c294b44d6d833e4dbcd809 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 24 Jun 2018 12:01:23 -0400 Subject: add scheduled transactions --- lib/WWW/YNAB.pm | 72 +++++++++++++++++++----- lib/WWW/YNAB/Budget.pm | 42 ++++++++++++++ lib/WWW/YNAB/ScheduledSubTransaction.pm | 48 ++++++++++++++++ lib/WWW/YNAB/ScheduledTransaction.pm | 98 +++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 15 deletions(-) create mode 100644 lib/WWW/YNAB/ScheduledSubTransaction.pm create mode 100644 lib/WWW/YNAB/ScheduledTransaction.pm (limited to 'lib') diff --git a/lib/WWW/YNAB.pm b/lib/WWW/YNAB.pm index 192b5b0..74d0f4d 100644 --- a/lib/WWW/YNAB.pm +++ b/lib/WWW/YNAB.pm @@ -9,6 +9,8 @@ use WWW::YNAB::CategoryGroup; use WWW::YNAB::Category; use WWW::YNAB::Month; use WWW::YNAB::Payee; +use WWW::YNAB::ScheduledSubTransaction; +use WWW::YNAB::ScheduledTransaction; use WWW::YNAB::SubTransaction; use WWW::YNAB::Transaction; use WWW::YNAB::UA; @@ -117,21 +119,27 @@ sub budget { my @transactions = map { my %transaction = %$_; - ($transaction{account_name}) = map { - $_->{name} - } grep { - $_->{id} eq $transaction{account_id} - } @{ $budget{accounts} }; - ($transaction{payee_name}) = map { - $_->{name} - } grep { - $_->{id} eq $transaction{payee_id} - } @{ $budget{payees} }; - ($transaction{category_name}) = map { - $_->{name} - } grep { - $_->{id} eq $transaction{category_id} - } @{ $budget{categories} }; + if ($transaction{account_id}) { + ($transaction{account_name}) = map { + $_->{name} + } grep { + $_->{id} eq $transaction{account_id} + } @{ $budget{accounts} }; + } + if ($transaction{payee_id}) { + ($transaction{payee_name}) = map { + $_->{name} + } grep { + $_->{id} eq $transaction{payee_id} + } @{ $budget{payees} }; + } + if ($transaction{category_id}) { + ($transaction{category_name}) = map { + $_->{name} + } grep { + $_->{id} eq $transaction{category_id} + } @{ $budget{categories} }; + } $transaction{subtransactions} = [ map { $self->model_from_data('WWW::YNAB::SubTransaction', $_) @@ -143,6 +151,40 @@ sub budget { } @{ $budget{transactions} }; $budget{transactions} = \@transactions; + my @scheduled_transactions = map { + my %transaction = %$_; + if ($transaction{account_id}) { + ($transaction{account_name}) = map { + $_->{name} + } grep { + $_->{id} eq $transaction{account_id} + } @{ $budget{accounts} }; + } + if ($transaction{payee_id}) { + ($transaction{payee_name}) = map { + $_->{name} + } grep { + $_->{id} eq $transaction{payee_id} + } @{ $budget{payees} }; + } + if ($transaction{category_id}) { + ($transaction{category_name}) = map { + $_->{name} + } grep { + $_->{id} eq $transaction{category_id} + } @{ $budget{categories} }; + } + $transaction{subtransactions} = [ + map { + $self->model_from_data('WWW::YNAB::ScheduledSubTransaction', $_) + } grep { + $_->{scheduled_transaction_id} eq $transaction{id} + } @{ $budget{scheduled_subtransactions} } + ]; + $self->model_from_data('WWW::YNAB::ScheduledTransaction', \%transaction) + } @{ $budget{scheduled_transactions} }; + $budget{scheduled_transactions} = \@scheduled_transactions; + $self->model_from_data( 'WWW::YNAB::Budget', \%budget, diff --git a/lib/WWW/YNAB/Budget.pm b/lib/WWW/YNAB/Budget.pm index 97d0d84..5a8bdee 100644 --- a/lib/WWW/YNAB/Budget.pm +++ b/lib/WWW/YNAB/Budget.pm @@ -98,6 +98,18 @@ has _transactions => ( } ); +has _scheduled_transactions => ( + traits => ['Array'], + is => 'ro', + isa => 'ArrayRef[WWW::YNAB::ScheduledTransaction]', + init_arg => 'scheduled_transactions', + lazy => 1, + builder => '_build_scheduled_transactions', + handles => { + scheduled_transactions => 'elements', + } +); + has _ua => ( is => 'ro', isa => 'WWW::YNAB::UA', @@ -257,6 +269,36 @@ sub transaction { $self->model_from_data('WWW::YNAB::Transaction', \%transaction); } +sub _build_scheduled_transactions { + my $self = shift; + + my $data = $self->_ua->get("/budgets/${\$self->id}/scheduled_transactions"); + [ + map { + my %transaction = %$_; + my @subtransactions = map { + $self->model_from_data('WWW::YNAB::ScheduledSubTransaction', $_) + } @{ $transaction{subtransactions} }; + $transaction{subtransactions} = \@subtransactions; + $self->model_from_data('WWW::YNAB::ScheduledTransaction', \%transaction) + } @{ $data->{data}{scheduled_transactions} } + ] +} + +sub scheduled_transaction { + my $self = shift; + my ($id) = @_; + + my $data = $self->_ua->get("/budgets/${\$self->id}/scheduled_transactions/$id"); + my $transaction = $data->{data}{scheduled_transaction}; + my %transaction = %$transaction; + my @subtransactions = map { + $self->model_from_data('WWW::YNAB::ScheduledSubTransaction', $_) + } @{ $transaction{subtransactions} }; + $transaction{subtransactions} = \@subtransactions; + $self->model_from_data('WWW::YNAB::ScheduledTransaction', \%transaction); +} + __PACKAGE__->meta->make_immutable; no Moose; diff --git a/lib/WWW/YNAB/ScheduledSubTransaction.pm b/lib/WWW/YNAB/ScheduledSubTransaction.pm new file mode 100644 index 0000000..d275f4d --- /dev/null +++ b/lib/WWW/YNAB/ScheduledSubTransaction.pm @@ -0,0 +1,48 @@ +package WWW::YNAB::ScheduledSubTransaction; +use Moose; + +has id => ( + is => 'ro', + isa => 'Str', + required => 1, +); + +has scheduled_transaction_id => ( + is => 'ro', + isa => 'Str', +); + +has amount => ( + is => 'ro', + isa => 'Int', +); + +has memo => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has payee_id => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has category_id => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has transfer_account_id => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has deleted => ( + is => 'ro', + isa => 'Bool', +); + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; diff --git a/lib/WWW/YNAB/ScheduledTransaction.pm b/lib/WWW/YNAB/ScheduledTransaction.pm new file mode 100644 index 0000000..98c67f1 --- /dev/null +++ b/lib/WWW/YNAB/ScheduledTransaction.pm @@ -0,0 +1,98 @@ +package WWW::YNAB::ScheduledTransaction; +use Moose; + +use Moose::Util::TypeConstraints qw(enum maybe_type); + +has id => ( + is => 'ro', + isa => 'Str', + required => 1, +); + +has date_first => ( + is => 'ro', + isa => 'Str', +); + +has date_next => ( + is => 'ro', + isa => 'Str', +); + +has frequency => ( + is => 'ro', + isa => enum([qw( + never daily weekly everyOtherWeek twiceAMonth every4Weeks + monthly everyOtherMonth every3Months every4Months twiceAYear + yearly everyOtherYear + )]), +); + +has amount => ( + is => 'ro', + isa => 'Int', +); + +has memo => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has flag_color => ( + is => 'ro', + isa => maybe_type(enum([qw(red orange yellow green blue purple)])), +); + +has account_id => ( + is => 'ro', + isa => 'Str', +); + +has payee_id => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has category_id => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has transfer_account_id => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has deleted => ( + is => 'ro', + isa => 'Bool', +); + +has account_name => ( + is => 'ro', + isa => 'Str', +); + +has payee_name => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has category_name => ( + is => 'ro', + isa => 'Maybe[Str]', +); + +has subtransactions => ( + traits => ['Array'], + is => 'bare', + isa => 'ArrayRef[WWW::YNAB::ScheduledSubTransaction]', + handles => { + subtransactions => 'elements', + } +); + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; -- cgit v1.2.3-54-g00ecf