Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / BackgroundJob / CloseProjectsBelongingToClosedSalesOrders.pm
1 package SL::BackgroundJob::CloseProjectsBelongingToClosedSalesOrders;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use SL::DB::Project;
8 use SL::DB::ProjectStatus;
9
10 sub run {
11   my ($self, $db_obj)     = @_;
12
13   my $data                = $db_obj->data_as_hash;
14   $data->{new_status}   ||= 'done';
15   $data->{set_inactive}   = 1 if !exists $data->{set_inactive};
16
17   my $new_status          = SL::DB::Manager::ProjectStatus->find_by(name => $data->{new_status}) || die "No project status named '$data->{new_status}' found!";
18
19   my %attributes          = (project_status_id => $new_status->id);
20   $attributes{active}     = 0 if $data->{set_inactive};
21
22   my $sql                 = <<EOSQL;
23     id IN (
24       SELECT oe.globalproject_id
25       FROM oe
26       WHERE (oe.globalproject_id IS NOT NULL)
27         AND (oe.customer_id      IS NOT NULL)
28         AND NOT COALESCE(oe.quotation, FALSE)
29         AND     COALESCE(oe.closed,    FALSE)
30     )
31 EOSQL
32
33   SL::DB::Manager::Project->update_all(
34     set   => \%attributes,
35     where => [
36       '!project_status_id' => $new_status->id,
37       \$sql,
38     ],
39   );
40
41   return 1;
42 }
43
44 1;
45
46 __END__
47
48 =encoding utf8
49
50 =head1 NAME
51
52 SL::BackgroundJob::CloseProjectsBelongingToClosedSalesOrders —
53 Background job for closing all projects which are linked to a closed
54 sales order (via C<oe.globalproject_id>)
55
56 =head1 SYNOPSIS
57
58 This background job searches all closed sales orders for linked
59 projects. Those projects whose status is not C<done> will be modified:
60 their status will be set to C<done> and their C<active> flag will be
61 set to C<false>.
62
63 Both of these can be configured via the job's data hash: C<new_status>
64 is the new status' name (defaults to C<done>), and C<set_inactive>
65 determines whether or not the project will be set to inactive
66 (defaults to 1).
67
68 The job is deactivated by default. Administrators of installations
69 where such a feature is wanted have to create a job entry manually.
70
71 =head1 AUTHOR
72
73 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
74
75 =cut