Skip to content

Commit

Permalink
Merge pull request #105 from Pear-Trading/finn/recurring
Browse files Browse the repository at this point in the history
Added script for recurring transactions and changed recurring logic
  • Loading branch information
piratefinn authored Mar 8, 2018
2 parents dc0efdc + 87a03c1 commit fa7baa7
Show file tree
Hide file tree
Showing 54 changed files with 23,071 additions and 19 deletions.
137 changes: 137 additions & 0 deletions lib/Pear/LocalLoop/Command/recur_transactions.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package Pear::LocalLoop::Command::recur_transactions;
use Mojo::Base 'Mojolicious::Command';

use Mojo::Util 'getopt';

use DateTime;
use DateTime::Format::Strptime;

has description => 'Recur Transactions';

has usage => sub { shift->extract_usage };

sub run {
my ( $self, @args ) = @_;

my $app = $self->app;

getopt \@args,
'f|force' => \my $force,
'd|date=s' => \my $date;

unless ( defined $force ) {
say "Will not do anything without force option";
return;
}

my $date_formatter = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d'
);

my $datetime;

if ( defined $date ) {

$datetime = $date_formatter->parse_datetime($date);

unless ( defined $datetime ) {
say "Unrecognised date format, please use 'YYYY-MM-DD' Format";
return;
}
} else {
$datetime = DateTime->today;
}

my $match_date_day = $app->format_iso_date($datetime->clone->subtract( days => 1 ));
my $match_date_week = $app->format_iso_date($datetime->clone->subtract( weeks => 1 ));
my $match_date_fortnight = $app->format_iso_date($datetime->clone->subtract( weeks => 2 ));
my $match_date_month = $app->format_iso_date($datetime->clone->subtract( months => 1 ));
my $match_date_quarter = $app->format_iso_date($datetime->clone->subtract( months => 3));

my $schema = $app->schema;
my $dtf = $schema->storage->datetime_parser;
my $recur_rs = $schema->resultset('TransactionRecurring');

for my $recur_result ( $recur_rs->all ) {

my $start_time_dt;
if ( defined $recur_result->last_updated ) {
$start_time_dt = $recur_result->last_updated;
} else {
$start_time_dt = $recur_result->start_time;
}
my $start_time = $app->format_iso_date($start_time_dt);
my $recurring_period = $recur_result->recurring_period;

if ( $recurring_period eq 'daily' ) {
next unless $start_time eq $match_date_day;
say "matched recurring transaction ID " . $recur_result->id . " to daily";
} elsif ( $recurring_period eq 'weekly' ) {
next unless $start_time eq $match_date_week;
say "matched recurring transaction ID " . $recur_result->id . " to weekly";
} elsif ( $recurring_period eq 'fortnightly' ) {
next unless $start_time eq $match_date_fortnight;
say "matched recurring transaction ID " . $recur_result->id . " to fortnightly";
} elsif ( $recurring_period eq 'monthly' ) {
next unless $start_time eq $match_date_month;
say "matched recurring transaction ID " . $recur_result->id . " to monthly";
} elsif ( $recurring_period eq 'quarterly' ) {
next unless $start_time eq $match_date_quarter;
say "matched recurring transaction ID " . $recur_result->id . " to quarterly";
} else {
say "Invalid recurring time period given";
return;
}

my $now = DateTime->now();
my $purchase_time = DateTime->new(
year => $now->year,
month => $now->month,
day => $now->day,
hour => $start_time_dt->hour,
minute => $start_time_dt->minute,
second => $start_time_dt->second,
time_zone => 'UTC',
);
my $category = $recur_result->category_id;
my $essential = $recur_result->essential;
my $distance = $recur_result->distance;

my $new_transaction = $schema->resultset('Transaction')->create({
buyer_id => $recur_result->buyer_id,
seller_id => $recur_result->seller_id,
value => $recur_result->value,
purchase_time => $app->format_db_datetime($purchase_time),
distance => $distance,
essential => ( defined $essential ? $essential : 0 ),
});

unless ( defined $new_transaction ) {
say "Error Adding Transaction";
return;
}

if ( defined $category ) {
$schema->resultset('TransactionCategory')->create({
category_id => $category,
transaction_id => $new_transaction->id,
});
}

$recur_result->update({ last_updated => $purchase_time });

}
}

=head1 SYNOPSIS
Usage: APPLICATION recur_transactions [OPTIONS]
Options:
-f, --force Actually insert the data
-d, --date Date to recur the transactions on
=cut

1;
12 changes: 9 additions & 3 deletions lib/Pear/LocalLoop/Controller/Api/Upload.pm
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ sub post_upload {
my $valid_org_rs = $c->schema->resultset('Organisation')->search({
pending => 0,
entity_id => { "!=" => $user->entity_id },
});
});
$validation->required('organisation_id')->number->in_resultset( 'id', $valid_org_rs );

return $c->api_validation_error if $validation->has_error;
Expand Down Expand Up @@ -197,8 +197,8 @@ sub post_upload {
value => $transaction_value * 100000,
( defined $file ? ( proof_image => $file ) : () ),
purchase_time => $c->format_db_datetime($purchase_time),
distance => $distance,
essential => ( defined $essential ? $essential : 0 ),
distance => $distance,
}
);

Expand All @@ -222,8 +222,14 @@ sub post_upload {

if ( defined $recurring_period ) {
$c->schema->resultset('TransactionRecurring')->create({
buyer => $user->entity,
seller => $organisation->entity,
value => $transaction_value * 100000,
start_time => $c->format_db_datetime($purchase_time),
essential => ( defined $essential ? $essential : 0 ),
distance => $distance,
category_id => ( defined $category ? $category : undef ),
recurring_period => $recurring_period,
transaction_id => $new_transaction->id,
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Pear/LocalLoop/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use warnings;

use base 'DBIx::Class::Schema';

our $VERSION = 27;
our $VERSION = 33;

__PACKAGE__->load_namespaces;

Expand Down
5 changes: 0 additions & 5 deletions lib/Pear/LocalLoop/Schema/Result/Transaction.pm
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ __PACKAGE__->might_have(
"Pear::LocalLoop::Schema::Result::TransactionCategory" => "transaction_id",
);

__PACKAGE__->might_have(
"recurring",
"Pear::LocalLoop::Schema::Result::TransactionRecurring" => "transaction_id",
);

sub sqlt_deploy_hook {
my ( $source_instance, $sqlt_table ) = @_;
my $pending_field = $sqlt_table->get_field('essential');
Expand Down
65 changes: 59 additions & 6 deletions lib/Pear/LocalLoop/Schema/Result/TransactionRecurring.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use warnings;

use base 'DBIx::Class::Core';

__PACKAGE__->load_components(qw/
InflateColumn::DateTime
TimeStamp
/);

__PACKAGE__->table("transaction_recurring");

__PACKAGE__->add_columns(
Expand All @@ -13,9 +18,45 @@ __PACKAGE__->add_columns(
is_auto_increment => 1,
is_nullable => 0,
},
"transaction_id" => {
data_type => 'integer',
"buyer_id" => {
data_type => "integer",
is_foreign_key => 1,
is_nullable => 0,
},
"seller_id" => {
data_type => "integer",
is_foreign_key => 1,
is_nullable => 0,
},
"value" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
"start_time" => {
data_type => "datetime",
timezone => "UTC",
is_nullable => 0,
},
"last_updated" => {
data_type => "datetime",
timezone => "UTC",
is_nullable => 1,
datetime_undef_if_invalid => 1,
},
"essential" => {
data_type => "boolean",
default_value => \"false",
is_nullable => 0,
},
"distance" => {
data_type => 'numeric',
size => [15],
is_nullable => 1,
},
"category_id" => {
data_type => "integer",
is_nullable => 1,
is_foreign_key => 1,
},
"recurring_period" => {
Expand All @@ -27,12 +68,24 @@ __PACKAGE__->add_columns(

__PACKAGE__->set_primary_key("id");

__PACKAGE__->add_unique_constraint(["transaction_id"]);
__PACKAGE__->belongs_to(
"buyer",
"Pear::LocalLoop::Schema::Result::Entity",
{ id => "buyer_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);

__PACKAGE__->belongs_to(
"seller",
"Pear::LocalLoop::Schema::Result::Entity",
{ id => "seller_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);

__PACKAGE__->belongs_to(
"transaction",
"Pear::LocalLoop::Schema::Result::Transaction",
"transaction_id",
"category",
"Pear::LocalLoop::Schema::Result::Category",
"category_id",
{ cascade_delete => 0 },
);

Expand Down
18 changes: 18 additions & 0 deletions share/ddl/PostgreSQL/deploy/28/001-auto-__VERSION.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Wed Mar 7 15:17:20 2018
--
;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE "dbix_class_deploymenthandler_versions" (
"id" serial NOT NULL,
"version" character varying(50) NOT NULL,
"ddl" text,
"upgrade_sql" text,
PRIMARY KEY ("id"),
CONSTRAINT "dbix_class_deploymenthandler_versions_version" UNIQUE ("version")
);

;
Loading

0 comments on commit fa7baa7

Please sign in to comment.