Auftrags-Controller: überflüssiges "die"-Statement entfernt
[kivitendo-erp.git] / sql / Pg-upgrade2 / ar_ap_fix_notes_as_html_for_non_invoices.pl
1 # @tag: ar_ap_fix_notes_as_html_for_non_invoices
2 # @description: Kreditoren-/Debitorenbuchungen: Bemerkungsfeld darf kein HTML sein
3 # @depends: oe_ar_ap_delivery_orders_edit_notes_as_html
4 package SL::DBUpgrade2::ar_ap_fix_notes_as_html_for_non_invoices;
5
6 use strict;
7 use utf8;
8
9 use SL::DBUtils;
10
11 use parent qw(SL::DBUpgrade2::Base);
12
13 sub fix_column {
14   my ($self, $table) = @_;
15
16   my $sth = $self->dbh->prepare(qq|UPDATE $table SET notes = ? WHERE id = ?|) || $self->dberror;
17
18   my $query = <<SQL;
19     SELECT id, notes
20     FROM $table
21     WHERE (notes IS NOT NULL)
22       AND (NOT COALESCE(invoice, FALSE))
23       AND (itime < (
24         SELECT itime
25         FROM schema_info
26         WHERE tag = 'oe_ar_ap_delivery_orders_edit_notes_as_html'))
27 SQL
28
29   foreach my $row (selectall_hashref_query($::form, $self->dbh, $query)) {
30     next if !$row->{notes} || (($row->{notes} !~ m{^<[a-z]+>}) && ($row->{notes} !~ m{</[a-z]+>$}));
31
32     my $new_content =  $row->{notes};
33     $new_content    =~ s{^<p>|</p>$}{}gi;
34     $new_content    =~ s{<br */>}{\n}gi;
35     $new_content    =~ s{</p><p>}{\n\n}gi;
36     $new_content    =  $::locale->unquote_special_chars('html', $new_content);
37
38     $sth->execute($new_content, $row->{id}) if $new_content ne $row->{notes};
39   }
40
41   $sth->finish;
42 }
43
44 sub run {
45   my ($self) = @_;
46
47   $self->fix_column($_) for qw(ar ap);
48
49   return 1;
50 }
51
52 1;