Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 201da0fb

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 201da0fb63b335bdd139afff152d41e2e9086784
  • Vorgänger c2342266
  • Nachfolger 1044cafb

BackgroundJob: Teste Artikel auf Mindestlagerbestand

Unterschiede anzeigen:

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::DB::Part;
9
use SL::Presenter::Part qw(part);
10
use SL::Presenter::Tag qw(html_tag);
11
use SL::DBUtils qw(selectall_hashref_query);
12
use SL::Locale::String qw(t8);
13

  
14
use constant WAITING_FOR_EXECUTION => 0;
15
use constant START                 => 1;
16
use constant DONE                  => 2;
17

  
18
sub check_below_minimum_stock {
19
  my ($self) = @_;
20

  
21
  my $dbh = SL::DB->client->dbh;
22
  my $query = <<SQL;
23
  SELECT id, partnumber, description, rop, onhand
24
  FROM parts
25
  WHERE onhand < rop
26
SQL
27

  
28
  my $result = selectall_hashref_query($::form, $dbh, $query);
29

  
30
  if (scalar @$result) {
31
    my $error_string = t8("Missing parts:\nPartnumber\t- Name\t- Onhand\t- ROP\n");
32
    my @ids = ();
33
    foreach my $part_hash (@$result) {
34
      $error_string.=
35
          $part_hash->{partnumber}
36
          . "\t- " . $part_hash->{description}
37
          . "\t- " . $part_hash->{onhand}
38
          . "\t- " . $part_hash->{rop}
39
          . "\n";
40
      push @ids, $part_hash->{id};
41
    }
42
    $self->{job_obj}->set_data(
43
      errors => $error_string,
44
      ids => \@ids,
45
    )->save;
46
  }
47
  return;
48
}
49

  
50
sub _email_user {
51
  my ($self) = @_;
52
  $self->{email_user} ||= SL::DB::Manager::AuthUser->find_by(login => $self->{config}->{send_email_to});
53
}
54

  
55

  
56
sub send_email {
57
  my ($self) = @_;
58

  
59
  my @ids = @{$self->{job_obj}->data_as_hash->{ids}};
60
  return unless (scalar @ids && $self->{config} && $self->{config}->{send_email_to});
61

  
62
  my $user  = $self->_email_user;
63
  my $email = $self->{job_obj}->data_as_hash->{mail_to} ? $self->{job_obj}->data_as_hash->{mail_to}
64
            : $user                                     ? $user->get_config_value('email')
65
            : undef;
66
  return unless $email;
67

  
68
  # additional email
69
  $email .= $self->{job_obj}->data_as_hash->{email} if $self->{job_obj}->data_as_hash->{email} =~ m/(\S+)@(\S+)$/;
70

  
71
  my ($output, $content_type) = $self->_prepare_report;
72

  
73
  my $mail              = Mailer->new;
74
  $mail->{from}         = $self->{config}->{email_from};
75
  $mail->{to}           = $email;
76
  $mail->{subject}      = $self->{config}->{email_subject};
77
  $mail->{content_type} = $content_type;
78
  $mail->{message}      = $$output;
79

  
80
  my $err = $mail->send;
81

  
82
  if ($err) {
83
    my $error = $self->{job_obj}->data_as_hash->{errors} . ('Mailer error #1', $err);
84
    $self->{job_obj}->set_data(errors => $error)->save;
85
  }
86
}
87

  
88
sub _prepare_report {
89
  my ($self) = @_;
90

  
91
  my $template = Template->new({ 'INTERPOLATE' => 0,
92
                                 'EVAL_PERL'   => 0,
93
                                 'ABSOLUTE'    => 1,
94
                                 'CACHE_SIZE'  => 0,
95
                               });
96

  
97
  return unless $template;
98
  my $email_template = $self->{config}->{email_template};
99
  my $filename       = $email_template || ( (SL::DB::Default->get->templates || "templates/mails") . "/below_minimum_stock/error_email.html" );
100
  my $content_type   = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
101

  
102
  my @ids = @{$self->{job_obj}->data_as_hash->{ids}};
103
  my @parts = @{SL::DB::Manager::Part->get_all(id => @ids)};
104

  
105

  
106
  my $table_head = html_tag('tr',
107
    html_tag('th', t8('Partnumber')) .
108
    html_tag('th', t8('ROP')) .
109
    html_tag('th', t8('Onhand'))
110
  );
111

  
112
  my $table_body;
113

  
114
  $table_body .= html_tag('tr', $_ ) for
115
    map {
116
      html_tag('td', part($_)). # TODO: links only to current site
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
  $job_obj->set_data(status => START())->save;
138

  
139
  $self->{config} = $::lx_office_conf{check_below_minimum_stock} || {};
140

  
141
  $self->check_below_minimum_stock();
142

  
143
  $self->send_email();
144

  
145
  my $data = $job_obj->data_as_hash;
146
  die $data->{errors} if $data->{errors};
147

  
148
  $job_obj->set_data(status => DONE())->save;
149
  return ;
150
}
151

  
152
1;
153

  
154
__END__
155

  
156
=head1 NAME
157

  
158
SL::BackgroundJob::CheckMinimumStock - Checks for all parts if on hand is greater
159
as minimum stock (onhand > rop)
160

  
161
=head1 SYNOPSIS
162

  
163
  use SL::BackgroundJob::CheckMinimumStock;
164
  SL::BackgroundJob::CheckMinimumStock->new->run;;
165

  
166
=cut
config/kivitendo.conf.default
261 261
# template. currently txt and html templates are recognized and correctly mime send.
262 262
email_template = templates/mail/self_test/status_mail.txt
263 263

  
264
[check_below_minimum_stock]
265
# The user name or email address a report about the under stock parts is sent
266
# to.
267
send_email_to  =
268
# The "From:" header for said email.
269
email_from     = kivitendo Daemon <root@localhost>
270
# The subject for said email.
271
email_subject  = Benachrichtigung: Artikel unter Mindestbestand
272
# The template file used for the email's body.
273
email_template = templates/mail/below_minimum_stock/error_email.html
274

  
264 275
[follow_up_reminder]
265 276
# Email notifications for due follow ups.
266 277
# The "From:" header for said email.
templates/mail/below_minimum_stock/error_email.html
1
kivitendo below minimum stock report.
2

  
3
[% part_table %]

Auch abrufbar als: Unified diff