Verknüpfungen zwischen Angeboten, Aufträgen, Lieferscheinen, Rechnungen in einer...
[kivitendo-erp.git] / SL / RecordLinks.pm
1 package RecordLinks;
2
3 use SL::Common;
4 use SL::DBUtils;
5
6 sub create_links {
7   $main::lxdebug->enter_sub();
8
9   my $self     = shift;
10   my %params   = @_;
11
12   Common::check_params(\%params, qw(links));
13
14   if (!scalar @{ $params{links} }) {
15     $main::lxdebug->leave_sub();
16     return;
17   }
18
19   my $myconfig = \%main::myconfig;
20   my $form     = $main::form;
21
22   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
23
24   my $query    = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
25   my $sth      = prepare_query($form, $dbh, $query);
26
27   foreach my $link (@{ $params{links} }) {
28     next if ('HASH' ne ref $link);
29     next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
30
31     do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
32   }
33
34   $dbh->commit() unless ($params{dbh});
35
36   $main::lxdebug->leave_sub();
37 }
38
39 sub get_links {
40   $main::lxdebug->enter_sub();
41
42   my $self     = shift;
43   my %params   = @_;
44
45   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
46
47   my $myconfig   = \%main::myconfig;
48   my $form       = $main::form;
49
50   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
51
52   my @conditions = ();
53   my @values     = ();
54
55   foreach my $col (qw(from_table from_id to_table to_id)) {
56     next unless ($params{$col});
57
58     if ('ARRAY' eq ref $params{$col}) {
59       push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
60       push @values,     $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
61
62     } else {
63       push @conditions, "$col = ?";
64       push @values,     $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
65     }
66   }
67
68   my $query = qq|SELECT from_table, from_id, to_table, to_id
69                  FROM record_links|;
70
71   if (scalar @conditions) {
72     $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
73   }
74
75   my $links = selectall_hashref_query($form, $dbh, $query, @values);
76
77   $main::lxdebug->leave_sub();
78
79   return wantarray ? @{ $links } : $links;
80 }
81
82 1;