Skip to content

Commit

Permalink
Determine closed period from database state
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuelsmann committed Jul 22, 2023
1 parent dda4bed commit 0736d97
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
55 changes: 47 additions & 8 deletions lib/LedgerSMB/Workflow/Condition/PeriodClosed.pm
Expand Up @@ -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<!period-closed>).
=head1 PARAMETERS
=head2 offset
<condition .... >
<param name="offset" value="1 day" />
</condition>
Interval to be added to the C<workflow_parameter> to check for being in
a closed period. Defaults to C<0 days>.
=head2 workflow_parameter
<condition .... >
<param name="workflow_parameter" value="transdate" />
</condition>
Name of the parameter in the workflow context which holds the date to check
for being in a closed period. Defaults to C<transdate>.
=head1 METHODS
=cut
Expand All @@ -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 )
Expand All @@ -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;
}


Expand Down
3 changes: 2 additions & 1 deletion old/bin/aa.pl
Expand Up @@ -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"},
Expand Down Expand Up @@ -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'
);
Expand Down
1 change: 0 additions & 1 deletion old/bin/gl.pl
Expand Up @@ -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'
);
Expand Down
7 changes: 2 additions & 5 deletions old/bin/ir.pl
Expand Up @@ -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} );
Expand Down Expand Up @@ -507,12 +508,10 @@ sub form_header {
if ( !$form->{readonly} ) {
print "<tr><td>";

%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 );

Expand Down Expand Up @@ -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'
);
Expand Down
7 changes: 2 additions & 5 deletions old/bin/is.pl
Expand Up @@ -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};
Expand Down Expand Up @@ -580,12 +581,10 @@ sub form_header {
if ( !$form->{readonly} ) {
print "<tr><td>";

%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 );

Expand Down Expand Up @@ -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'
);
Expand Down

0 comments on commit 0736d97

Please sign in to comment.