cad8bb628605d0a6ad2403d0872ff4fc83984b29
[kivitendo-erp.git] / SL / DB / Buchungsgruppe.pm
1 package SL::DB::Buchungsgruppe;
2
3 use strict;
4
5 use SL::DB::MetaSetup::Buchungsgruppe;
6 use SL::DB::Manager::Buchungsgruppe;
7 use SL::DB::Helper::ActsAsList;
8
9 __PACKAGE__->meta->initialize;
10
11 sub inventory_account { goto &inventory_accno; }
12
13 sub validate {
14   my ($self) = @_;
15
16   my @errors;
17   push @errors, $::locale->text('The description is missing.') if !$self->description;
18   if( $self->inventory_accno_id ) {
19     require SL::DB::Chart;
20     my $inventory_accno = SL::DB::Manager::Chart->find_by( id => $self->inventory_accno_id );
21     push(@errors, $::locale->text('Booking group #1 needs a valid inventory account', $self->description)) unless $inventory_accno;
22   } else {
23     push @errors, $::locale->text('The booking group needs an inventory account.');
24   };
25
26   return @errors;
27 }
28
29 sub income_accno_id {
30   my ($self, $taxzone) = @_;
31
32   require SL::DB::TaxZone;
33   require SL::DB::TaxzoneChart;
34
35   my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
36   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
37   return $taxzone_chart->income_accno_id if $taxzone_chart;
38 }
39
40 sub expense_accno_id {
41   my ($self, $taxzone) = @_;
42   require SL::DB::TaxZone;
43   require SL::DB::TaxzoneChart;
44
45   my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
46   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
47   return $taxzone_chart->expense_accno_id if $taxzone_chart;
48 }
49
50 sub income_account {
51   my ($self, $taxzone) = @_;
52
53   require SL::DB::TaxZone;
54   require SL::DB::TaxzoneChart;
55
56   my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
57   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
58   return $taxzone_chart->income_accno if $taxzone_chart;
59 }
60
61 sub expense_account {
62   my ($self, $taxzone) = @_;
63
64   require SL::DB::TaxZone;
65   require SL::DB::TaxzoneChart;
66
67   my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
68   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
69   return $taxzone_chart->expense_accno if $taxzone_chart;
70 }
71
72 sub taxzonecharts {
73   my ($self) = @_;
74   return SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->id ]);
75 }
76
77 sub orphaned {
78   my ($self) = @_;
79   die 'not an accessor' if @_ > 1;
80
81   require SL::DB::Part;
82   return 0 if SL::DB::Manager::Part->get_all_count(query => [ buchungsgruppen_id => $self->id ]);
83   return 1;
84 }
85
86 1;
87 __END__
88
89 =pod
90
91 =encoding utf8
92
93 =head1 NAME
94
95 SL::DB::Buchungsgruppe - RDBO wrapper for the C<buchungsgruppen> table
96
97 =head1 FUNCTIONS
98
99 =over 4
100
101 =item C<expense_accno_id $taxzone>
102
103 Return the chart ID for the expense account for the given taxzone
104 (either the DB id or an instance of L<SL::DB::TaxZone>).
105
106 =item C<expense_account>
107
108 Return the chart (an instance of L<SL::DB::Chart>) for the expense
109 account for the given taxzone (either the DB id or an instance of
110 L<SL::DB::TaxZone>).
111
112 =item C<income_accno_id>
113
114 Return the chart ID for the income account for the given taxzone
115 (either the DB id or an instance of L<SL::DB::TaxZone>).
116 L<SL::DB::TaxZone>).
117
118 =item C<income_account>
119
120 Return the chart (an instance of L<SL::DB::Chart>) for the income
121 account for the given taxzone (either the DB id or an instance of
122 L<SL::DB::TaxZone>).
123
124 =item C<orphaned>
125
126 Checks whether this Buchungsgruppe is assigned to any parts.
127
128 =back
129
130 =head1 BUGS
131
132 Nothing here yet.
133
134 =head1 AUTHOR
135
136 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
137 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
138
139 =cut