]> wagnertech.de Git - mfinanz.git/blob - SL/BackgroundJob/CheckBelowMinimumStock.pm
restart apache2 in postinst
[mfinanz.git] / SL / BackgroundJob / CheckBelowMinimumStock.pm
1 package SL::BackgroundJob::CheckBelowMinimumStock;
2
3 use strict;
4 use warnings;
5
6 use parent qw(SL::BackgroundJob::Base);
7
8 use SL::Mailer;
9 use SL::DB::Part;
10 use SL::Presenter::Part qw(part);
11 use SL::Presenter::Tag qw(html_tag link_tag);
12 use SL::Presenter::EscapedText qw(escape);
13 use SL::DBUtils qw(selectall_hashref_query);
14 use SL::Locale::String qw(t8);
15
16 sub check_below_minimum_stock {
17   my ($self) = @_;
18
19   my $dbh = SL::DB->client->dbh;
20   my $query = <<SQL;
21   SELECT id, partnumber, description, rop, onhand
22   FROM parts
23   WHERE onhand < rop
24 SQL
25
26   my $result = selectall_hashref_query($::form, $dbh, $query);
27
28   if (scalar @$result) {
29     my $error_string = t8("Missing parts:\nPartnumber\t- Name\t- Onhand\t- ROP\n");
30     my @ids = ();
31     foreach my $part_hash (@$result) {
32       $error_string.=
33           $part_hash->{partnumber}
34           . "\t- " . $part_hash->{description}
35           . "\t- " . $part_hash->{onhand}
36           . "\t- " . $part_hash->{rop}
37           . "\n";
38       push @ids, $part_hash->{id};
39     }
40     $self->{errors} = $error_string;
41     $self->{ids}    = \@ids;
42   }
43   return;
44 }
45
46 sub _email_user {
47   my ($self) = @_;
48   return unless ($self->{config} && $self->{config}->{send_email_to});
49   SL::DB::Manager::AuthUser->find_by(login => $self->{config}->{send_email_to})->get_config_value('email');
50 }
51
52
53 sub send_email {
54   my ($self) = @_;
55
56   my $email = $self->{job_obj}->data_as_hash->{mail_to} || $self->_email_user || undef;
57   return unless $email;
58
59   # additional email
60   $email .= " " . $self->{job_obj}->data_as_hash->{email} if $self->{job_obj}->data_as_hash->{email} =~ m/(\S+)@(\S+)$/;
61
62   my ($output, $content_type) = $self->_prepare_report;
63
64   my $mail              = Mailer->new;
65   $mail->{from}         = $self->{config}->{email_from};
66   $mail->{to}           = $email;
67   $mail->{subject}      = $self->{config}->{email_subject};
68   $mail->{content_type} = $content_type;
69   $mail->{message}      = $$output;
70
71   my $err = $mail->send;
72
73   if ($err) {
74     $self->{errors} .= t8('Mailer error #1', $err);
75   }
76
77   return
78 }
79
80 sub _prepare_report {
81   my ($self) = @_;
82
83   my $template = Template->new({ 'INTERPOLATE' => 0,
84                                  'EVAL_PERL'   => 0,
85                                  'ABSOLUTE'    => 1,
86                                  'CACHE_SIZE'  => 0,
87                                });
88
89   return unless $template;
90   my $email_template = $self->{config}->{email_template};
91   my $filename       = $email_template || ( (SL::DB::Default->get->templates || "templates/mails") . "/below_minimum_stock/error_email.html" );
92   my $content_type   = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
93
94   my @ids   = @{ $self->{ids} };
95   my @parts = @{ SL::DB::Manager::Part->get_all(where => [id => \@ids]) };
96
97
98   my $table_head = html_tag('tr',
99     html_tag('th', t8('Partnumber')) .
100     html_tag('th', t8('ROP')) .
101     html_tag('th', t8('Onhand'))
102   );
103
104   my $table_body;
105
106   $table_body .= html_tag('tr', $_ ) for
107     map {
108       html_tag('td',
109         link_tag(
110           $ENV{HTTP_ORIGIN} . $ENV{REQUEST_URI}
111           . '?action=Part/edit'
112           . '&part.id=' . escape($_->id)
113           # text
114           , $_->partnumber
115         )
116       ).
117       html_tag('td', $_->rop).
118       html_tag('td', $_->onhand)
119     } @parts;
120
121   my %params = (
122     SELF  => $self,
123     PARTS => \@parts,
124     part_table => html_tag('table', $table_head . $table_body),
125   );
126
127   my $output;
128   $template->process($filename, \%params, \$output) || die $template->error;
129
130   return (\$output, $content_type);
131 }
132
133 sub run {
134   my ($self, $job_obj) = @_;
135   $self->{job_obj} = $job_obj;
136
137   $self->{config} = $::lx_office_conf{check_below_minimum_stock} || {};
138
139   $self->check_below_minimum_stock();
140
141   if ($self->{errors}) {
142       # on error we have to inform the user
143       $self->send_email();
144       die $self->{errors};
145   }
146
147   return ;
148 }
149
150 1;
151
152 __END__
153
154 =head1 NAME
155
156 SL::BackgroundJob::CheckMinimumStock - Checks for all parts if on hand is greater
157 as minimum stock (onhand > rop)
158
159 =head1 SYNOPSIS
160
161   use SL::BackgroundJob::CheckMinimumStock;
162   SL::BackgroundJob::CheckMinimumStock->new->run;;
163
164 =cut