From bbb7edd269c3597aff926ba462322b576734b115 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 14 Aug 2019 14:00:34 +0100 Subject: [PATCH] custom graph duration and dateRange added (untested) --- .DS_Store | Bin 0 -> 6148 bytes .../Controller/Api/V1/Customer/Graphs.pm | 65 +++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..191c7db4be52e2ec91f1153c266a44a0839652eb GIT binary patch literal 6148 zcmeHKUvJYe5I=8WxIu_MOk$6D;cc*1493f(s0=0~gpk6Y2S8h*gf3Eut0Y~4Qk1VT zz7d~*ufy+bx7z3?@qiFwzRLGIpM7WhCpqpBkr<5AK2ev5BpRdVL!Bc$&TUCntY-}< zI7c$6M@gX)^uI67b`HmYW8hz7fbXtHcW6jCHK*@yGAi=A>Gghyl~wP`+Epoi>0kHX zMMr89HE}bp2XXa=d(Y$MD6#enrHX|eC$I8sa?o9WsLCeJ%dFDDd75F$+t+!Ss>wi& z%e2;cA~PUmM|KXn8`J5%2ffW;d%Hi|45m9fY~Q^CXwrN<%7*NuPQn%+!mccAg(CFo$RFtGBtnBuI|dvBXBk*E-7dfXk1nqN&jz`kW56-+ zUojxOy=ZTMBiXlg>2Unk255(9EIh9@s6(LabF2-1iZ{@-U@Tw*7+P!%!UJ)C1T+m! KIR^eJ13v+1Vuniq literal 0 HcmV?d00001 diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm index f48e52b..42a55f2 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm @@ -20,9 +20,20 @@ sub index { avg_spend_last_week total_last_month avg_spend_last_month + total_duration + total_range + avg_spend_duration + avg_spend_range / ); return $c->api_validation_error if $validation->has_error; + if ($validation->param('graph') == 'total_range' || $validation->param('graph') == 'avg_spend_range') { + $validation->required('start_date', 'end_date'); + } + + if ($validation->param('graph') == 'total_duration' || $validation->param('graph') == 'avg_spend_duration') { + $validation->required('duration'); + } my $graph_sub = "graph_" . $validation->param('graph'); @@ -44,15 +55,26 @@ sub index { sub graph_total_last_week { return shift->_purchases_total_duration( 7 ) } sub graph_total_last_month { return shift->_purchases_total_duration( 30 ) } +sub graph_total_duration { + my ( $c, $duration ) = @_; + return shift->__purchases_total_duration( $duration ); +} + +sub graph_total_range { + my ( $c, $start_date, $end_date ) = @_; + return shift->__purchases_total_duration( $duration, $start_date, $end_date ); +} + sub _purchases_total_duration { - my ( $c, $day_duration ) = @_; + my ( $c, $day_duration, $start_date, $end_date ) = @_; my $duration = DateTime::Duration->new( days => $day_duration ); my $entity = $c->stash->{api_user}->entity; my $data = { labels => [], data => [] }; - my ( $start, $end ) = $c->_get_start_end_duration( $duration ); +# if $start_date and $end_date are not present it will use $duration + my ( $start, $end ) = $c->_get_start_end_duration( $duration, $start_date, $end_date ); $data->{bounds} = { min => $c->format_iso_datetime( $start ), @@ -81,16 +103,27 @@ sub _purchases_total_duration { sub graph_avg_spend_last_week { return shift->_purchases_avg_spend_duration( 7 ) } sub graph_avg_spend_last_month { return shift->_purchases_avg_spend_duration( 30 ) } +sub graph_avg_spend_duration { + my ( $c, $duration ) = @_; + return shift->_purchases_avg_spend_duration( $duration ); +} + +sub graph_avg_spend_range { + my ( $c, $start_date, $end_date ) = @_; + return shift->_purchases_avg_spend_duration( $duration, $start_date, $end_date ); +} + sub _purchases_avg_spend_duration { - my ( $c, $day_duration ) = @_; + my ( $c, $day_duration, $start_date, $end_date ) = @_; my $duration = DateTime::Duration->new( days => $day_duration ); my $entity = $c->stash->{api_user}->entity; my $data = { labels => [], data => [] }; - my ( $start, $end ) = $c->_get_start_end_duration( $duration ); - +# if $start_date and $end_date are not present it will use $duration + my ( $start, $end ) = $c->_get_start_end_duration( $duration, $start_date, $end_date ); + $data->{bounds} = { min => $c->format_iso_datetime( $start ), max => $c->format_iso_datetime( $end ), @@ -143,9 +176,25 @@ sub _purchases_avg_spend_duration { } sub _get_start_end_duration { - my ( $c, $duration ) = @_; - my $end = DateTime->today; - my $start = $end->clone->subtract_duration( $duration ); + my ( $c, $duration, $start_date, $end_date ) = @_; + my $start; + my $end; + + if ($end_date && $start_date) { + $start = DateTime->new( + year => substr $start_date, 0, 4, + month => substr $start_date, 4, 2, + days => substr $start_date, 7, 2, + ); + $end = DateTime->new( + year => substr $end_date, 0, 4, + month => substr $end_date, 4, 2, + days => substr $end_date, 7, 2, + ); + } else { + $end = $end->clone->subtract_duration( $duration ); + } + return ( $start, $end ); }