477ae3c81812e6378bdf31510a79b6ccc500796b
[kivitendo-erp.git] / SL / DB / Note.pm
1 package SL::DB::Note;
2
3 use strict;
4
5 use Carp;
6
7 use SL::DB::MetaSetup::Note;
8
9
10 __PACKAGE__->meta->add_relationships(
11   follow_up => {
12     type         => 'one to one',
13     class        => 'SL::DB::FollowUp',
14     column_map   => { id => 'note_id' },
15   },
16 );
17
18 __PACKAGE__->meta->initialize;
19
20 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
21 __PACKAGE__->meta->make_manager_class;
22
23 sub trans_object {
24   my $self = shift;
25
26   croak "Method is not a setter" if @_;
27
28   return undef if !$self->trans_id || !$self->trans_module;
29
30   if ($self->trans_module eq 'fu') {
31     require SL::DB::FollowUp;
32     return SL::DB::Manager::FollowUp->find_by(id => $self->trans_id);
33   }
34
35   if ($self->trans_module eq 'ct') {
36     require SL::DB::Customer;
37     require SL::DB::Vendor;
38     return SL::DB::Manager::Customer->find_by(id => $self->trans_id)
39         || SL::DB::Manager::Vendor  ->find_by(id => $self->trans_id);
40   }
41
42   return undef;
43 }
44
45 1;
46 __END__
47
48 =pod
49
50 =encoding utf8
51
52 =head1 NAME
53
54 SL::DB::Note - Notes
55
56 =head1 FUNCTIONS
57
58 =over 4
59
60 =item C<trans_object>
61
62 A note object is always attached to another database entity. Which one
63 is determined by the columns C<trans_module> and C<trans_id>. This
64 function looks at both, retrieves the corresponding object from the
65 database and returns it.
66
67 Currently the following three types are supported:
68
69 =over 2
70
71 =item * C<SL::DB::FollowUp> for C<trans_module == 'fu'>
72
73 =item * C<SL::DB::Customer> or C<SL::DB::Vendor> for C<trans_module ==
74 'ct'> (which class is used depends on the value of C<trans_id>;
75 customers are looked up first)
76
77 =back
78
79 The method returns C<undef> in three cases: if no C<trans_id> or no
80 C<trans_module> has been assigned yet; if C<trans_module> is unknown;
81 if the referenced object doesn't exist.
82
83 This method is a getter only, not a setter.
84
85 =back
86
87 =head1 BUGS
88
89 Nothing here yet.
90
91 =head1 AUTHOR
92
93 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
94
95 =cut