1 package SL::BackgroundJob::CheckBelowMinimumStock;
6 use parent qw(SL::BackgroundJob::Base);
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);
16 sub check_below_minimum_stock {
19 my $dbh = SL::DB->client->dbh;
21 SELECT id, partnumber, description, rop, onhand
26 my $result = selectall_hashref_query($::form, $dbh, $query);
28 if (scalar @$result) {
29 my $error_string = t8("Missing parts:\nPartnumber\t- Name\t- Onhand\t- ROP\n");
31 foreach my $part_hash (@$result) {
33 $part_hash->{partnumber}
34 . "\t- " . $part_hash->{description}
35 . "\t- " . $part_hash->{onhand}
36 . "\t- " . $part_hash->{rop}
38 push @ids, $part_hash->{id};
40 $self->{errors} = $error_string;
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');
56 my $email = $self->{job_obj}->data_as_hash->{mail_to} || $self->_email_user || undef;
60 $email .= " " . $self->{job_obj}->data_as_hash->{email} if $self->{job_obj}->data_as_hash->{email} =~ m/(\S+)@(\S+)$/;
62 my ($output, $content_type) = $self->_prepare_report;
64 my $mail = Mailer->new;
65 $mail->{from} = $self->{config}->{email_from};
67 $mail->{subject} = $self->{config}->{email_subject};
68 $mail->{content_type} = $content_type;
69 $mail->{message} = $$output;
71 my $err = $mail->send;
74 $self->{errors} .= t8('Mailer error #1', $err);
83 my $template = Template->new({ 'INTERPOLATE' => 0,
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';
94 my @ids = @{ $self->{ids} };
95 my @parts = @{ SL::DB::Manager::Part->get_all(where => [id => \@ids]) };
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'))
106 $table_body .= html_tag('tr', $_ ) for
110 $ENV{HTTP_ORIGIN} . $ENV{REQUEST_URI}
111 . '?action=Part/edit'
112 . '&part.id=' . escape($_->id)
117 html_tag('td', $_->rop).
118 html_tag('td', $_->onhand)
124 part_table => html_tag('table', $table_head . $table_body),
128 $template->process($filename, \%params, \$output) || die $template->error;
130 return (\$output, $content_type);
134 my ($self, $job_obj) = @_;
135 $self->{job_obj} = $job_obj;
137 $self->{config} = $::lx_office_conf{check_below_minimum_stock} || {};
139 $self->check_below_minimum_stock();
141 if ($self->{errors}) {
142 # on error we have to inform the user
156 SL::BackgroundJob::CheckMinimumStock - Checks for all parts if on hand is greater
157 as minimum stock (onhand > rop)
161 use SL::BackgroundJob::CheckMinimumStock;
162 SL::BackgroundJob::CheckMinimumStock->new->run;;