From 0736d9773ccc93a05c14173612719512a7194e9d Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Sat, 22 Jul 2023 19:53:21 +0200 Subject: [PATCH] Determine closed period from database state --- .../Workflow/Condition/PeriodClosed.pm | 55 ++++++++++++++++--- old/bin/aa.pl | 3 +- old/bin/gl.pl | 1 - old/bin/ir.pl | 7 +-- old/bin/is.pl | 7 +-- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/lib/LedgerSMB/Workflow/Condition/PeriodClosed.pm b/lib/LedgerSMB/Workflow/Condition/PeriodClosed.pm index fb1994670d..c9844ef281 100644 --- a/lib/LedgerSMB/Workflow/Condition/PeriodClosed.pm +++ b/lib/LedgerSMB/Workflow/Condition/PeriodClosed.pm @@ -20,6 +20,27 @@ Note that to check for open accounting periods, simply check for the negated value of this condition by prefixing it with an exclamation mark (C). +=head1 PARAMETERS + +=head2 offset + + + + + +Interval to be added to the C to check for being in +a closed period. Defaults to C<0 days>. + +=head2 workflow_parameter + + + + + +Name of the parameter in the workflow context which holds the date to check +for being in a closed period. Defaults to C. + + =head1 METHODS =cut @@ -34,6 +55,20 @@ use LedgerSMB::Setting; use Log::Any qw($log); use Workflow::Exception qw( condition_error ); +my @PROPS = qw( offset workflow_parameter ); +__PACKAGE__->mk_accessors(@PROPS); + + +=head2 init( \%params ) + +=cut + +sub init { + my ($self, $params) = @_; + $self->SUPER::init( $params ); + $self->offset( $params->{offset} // '0 days' ); + $self->workflow_parameter( $params->{workflow_parameter} // 'transdate' ); +} =head2 evaluate( $wf ) @@ -46,14 +81,18 @@ sub evaluate { my ($self, $wf) = @_; my $dbh = $wf->_factory-> get_persister_for_workflow_type( $wf->type )->handle; - - # The plan was to query the database for the period being open or - # closed, based on the transaction date in the database. - - # However, at the time, it's simpler to get the indicator from the - # workflow instead. - - return $wf->context->param( '_is_closed' ); + my ($opened) = $dbh->selectrow_array( + q|SELECT (?::date + ?::interval) > MAX(end_date) + OR MAX(end_date) IS NULL + FROM account_checkpoint|, + {}, + $wf->context->param( $self->workflow_parameter ), + $self->offset + ); + die $dbh->errstr if $dbh->err; + + condition_error 'Period open' if $opened; + return 1; } diff --git a/old/bin/aa.pl b/old/bin/aa.pl index 4477206424..7f2b4240f7 100644 --- a/old/bin/aa.pl +++ b/old/bin/aa.pl @@ -468,6 +468,7 @@ sub form_header { ) ); $form->{workflow_id} = $wf->id; } + $wf->context->param( transdate => $form->{transdate} ); $title = $form->{title}; $form->all_business_units($form->{transdate}, $form->{"$form->{vc}_id"}, @@ -1083,7 +1084,7 @@ sub form_footer { {text=> $locale->text('Transaction'), value => 'transaction'}, ] }; - $wf->context->param( _is_closed => $form->is_closed( $transdate ) ); + $wf->context->param( transdate => $transdate ); %button_types = ( print => 'lsmb/PrintButton' ); diff --git a/old/bin/gl.pl b/old/bin/gl.pl index 16460b19c2..4647fbc319 100644 --- a/old/bin/gl.pl +++ b/old/bin/gl.pl @@ -366,7 +366,6 @@ sub display_form $transdate = $form->datetonum( \%myconfig, $form->{transdate} ); my @buttons; if ( !$form->{readonly} ) { - $wf->context->param( _is_closed => $form->is_closed( $transdate ) ); %button_types = ( print => 'lsmb/PrintButton' ); diff --git a/old/bin/ir.pl b/old/bin/ir.pl index c7872a60b0..4a76af6a67 100644 --- a/old/bin/ir.pl +++ b/old/bin/ir.pl @@ -305,6 +305,7 @@ sub form_header { $form->{workflow_id} = $wf->id; } $transdate = $form->datetonum( \%myconfig, $form->{transdate} ); + $wf->context->param( transdate => $transdate ); $form->{exchangerate} = $form->format_amount( \%myconfig, $form->{exchangerate} ); @@ -507,12 +508,10 @@ sub form_header { if ( !$form->{readonly} ) { print ""; - %button; - - $wf->context->param( _is_closed => $form->is_closed( $transdate ) ); %button_types = ( print => 'lsmb/PrintButton' ); + %button = (); for my $action_name ( $wf->get_current_actions( 'main') ) { my $action = $wf->get_action( $action_name ); @@ -966,8 +965,6 @@ sub form_footer { print_select($form, $printops->{format}); print_select($form, $printops->{media}); - - $wf->context->param( _is_closed => $form->is_closed( $transdate ) ); %button_types = ( print => 'lsmb/PrintButton' ); diff --git a/old/bin/is.pl b/old/bin/is.pl index e717aecd9f..8a4383e8e9 100644 --- a/old/bin/is.pl +++ b/old/bin/is.pl @@ -303,6 +303,7 @@ sub form_header { $form->{workflow_id} = $wf->id; } $transdate = $form->datetonum( \%myconfig, $form->{transdate} ); + $wf->context->param( transdate => $transdate ); $status_div_id = 'AR-invoice'; $status_div_id .= '-reverse' if $form->{reverse}; @@ -580,12 +581,10 @@ sub form_header { if ( !$form->{readonly} ) { print ""; - %button; - - $wf->context->param( _is_closed => $form->is_closed( $transdate ) ); %button_types = ( print => 'lsmb/PrintButton' ); + %button = (); for my $action_name ( $wf->get_current_actions( 'main') ) { my $action = $wf->get_action( $action_name ); @@ -1037,8 +1036,6 @@ sub form_footer { print_select($form, $printops->{format}); print_select($form, $printops->{media}); - - $wf->context->param( _is_closed => $form->is_closed( $transdate ) ); %button_types = ( print => 'lsmb/PrintButton' );