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