epic-s6ts
[kivitendo-erp.git] / SL / DB / AccTransaction.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::AccTransaction;
5
6 use strict;
7
8 use SL::DB::MetaSetup::AccTransaction;
9 use SL::DB::Manager::AccTransaction;
10 use SL::Locale::String qw(t8);
11
12 require SL::DB::GLTransaction;
13 require SL::DB::Invoice;
14 require SL::DB::PurchaseInvoice;
15
16 __PACKAGE__->meta->add_relationship(
17   ar => {
18     type         => 'many to one',
19     class        => 'SL::DB::Invoice',
20     column_map   => { trans_id => 'id' },
21   },
22   ap => {
23     type         => 'many to one',
24     class        => 'SL::DB::PurchaseInvoice',
25     column_map   => { trans_id => 'id' },
26   },
27   gl => {
28     type         => 'many to one',
29     class        => 'SL::DB::GLTransaction',
30     column_map   => { trans_id => 'id' },
31   },
32 );
33
34 __PACKAGE__->meta->initialize;
35
36 sub record {
37   my ($self) = @_;
38
39   my @classes = qw(Invoice PurchaseInvoice GLTransaction);
40
41   foreach my $class ( @classes ) {
42     $class = 'SL::DB::' . $class;
43     my $record = $class->new(id => $self->trans_id);
44     return $record if $record->load(speculative => 1);
45   };
46
47 };
48
49 sub get_type {
50   my $self = shift;
51
52   my $ref = ref $self->record;
53
54   return "ar" if $ref->isa('SL::DB::Invoice');
55   return "ap" if $ref->isa('SL::DB::PurchaseInvoice');
56   return "gl" if $ref->isa('SL::DB::GLTransaction');
57
58   die "Can't find trans_id " . $self->trans_id . " in ar, ap or gl" unless $ref;
59
60 };
61
62 sub transaction_name {
63   my $self = shift;
64
65   my $ref = ref $self->record;
66   my $name = "trans_id: " . $self->trans_id;
67   if ( $self->get_type eq 'ar' ) {
68     $name .= " (" . $self->record->abbreviation . " " . t8("AR") . ") " . t8("Invoice Number") . ": " . $self->record->invnumber;
69   } elsif ( $self->get_type eq 'ap' ) {
70     $name .= " (" . $self->record->abbreviation . " " . t8("AP") . ") " . t8("Invoice Number") . ": " . $self->record->invnumber;
71   } elsif ( $self->get_type eq 'gl' ) {
72     $name = "trans_id: " . $self->trans_id . " (" . $self->record->abbreviation . ") " . $self->record->reference . " - " . $self->record->description;
73   } else {
74     die "can't determine type of acc_trans line with trans_id " . $self->trans_id;
75   };
76
77   $name .= "   " . t8("Date") . ": " . $self->transdate->to_kivitendo;
78
79   return $name;
80
81 };
82
83 1;
84
85 __END__
86
87 =pod
88
89 =encoding utf8
90
91 =head1 NAME
92
93 SL::DB::AccTransaction: Rose model for transactions (table "acc_trans")
94
95 =head1 FUNCTIONS
96
97 =over 4
98
99 =item C<record>
100
101 Returns the ar, ap or gl object of the current acc_trans object.
102
103 Example:
104   my $acc_trans = SL::DB::Manager::AccTransaction->find_by( trans_id => '427' );
105   my $record = $acc_trans->record;
106
107 Each acc_trans entry is associated with an ar, ap or gl record. If we only have
108 an acc_trans object, and we want to find out which kind of record it belongs
109 to, we have to look for its trans_id in the tables ar, ap and gl. C<record>
110 does this for you and returns an Invoice, PurchaseInvoice or GLTransaction
111 object.
112
113 We use the Rose::DB::Object load function with the C<speculative> parameter for
114 each record type, which returns true if the load was successful, so we don't
115 bother to check the ref of the object.
116
117 =item C<get_type>
118
119 Returns the type of transaction the acc_trans entry belongs to: ar, ap or gl.
120
121 Example:
122  my $acc = SL::DB::Manager::AccTransaction->get_first();
123  my $type = $acc->get_type;
124
125 =item C<transaction_name>
126
127 Generate a meaningful transaction name for an acc_trans line from the
128 corresponding ar/ap/gl object, a combination of trans_id,
129 invnumber/description, abbreviation. Can be used for better error output of the
130 DATEV export and contains some database information, e.g. the trans_id, and is
131 a kind of displayable_name for debugging or in the console.
132
133 =back
134
135 =head1 BUGS
136
137 Nothing here yet.
138
139 =head1 AUTHOR
140
141 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
142
143 =cut