dd49433c6adb518e9638437439b64509ec7b69d8
[kivitendo-erp.git] / SL / DB / PurchaseInvoice.pm
1 package SL::DB::PurchaseInvoice;
2
3 use strict;
4
5 use Carp;
6 use Data::Dumper;
7
8 use SL::DB::MetaSetup::PurchaseInvoice;
9 use SL::DB::Manager::PurchaseInvoice;
10 use SL::DB::Helper::AttrHTML;
11 use SL::DB::Helper::AttrSorted;
12 use SL::DB::Helper::LinkedRecords;
13 use SL::DB::Helper::Payment qw(:ALL);
14 use SL::Locale::String qw(t8);
15 use Rose::DB::Object::Helpers qw(has_loaded_related forget_related);
16
17 # The calculator hasn't been adjusted for purchase invoices yet.
18 # use SL::DB::Helper::PriceTaxCalculator;
19
20 __PACKAGE__->meta->add_relationship(
21   invoiceitems   => {
22     type         => 'one to many',
23     class        => 'SL::DB::InvoiceItem',
24     column_map   => { id => 'trans_id' },
25     manager_args => { with_objects => [ 'part' ] }
26   },
27   sepa_export_items => {
28     type            => 'one to many',
29     class           => 'SL::DB::SepaExportItem',
30     column_map      => { id => 'ap_id' },
31     manager_args    => { with_objects => [ 'sepa_export' ] }
32   },
33   sepa_exports      => {
34     type            => 'many to many',
35     map_class       => 'SL::DB::SepaExportItem',
36     map_from        => 'ap',
37     map_to          => 'sepa_export',
38   },
39   custom_shipto     => {
40     type            => 'one to one',
41     class           => 'SL::DB::Shipto',
42     column_map      => { id => 'trans_id' },
43     query_args      => [ module => 'AP' ],
44   },
45   transactions   => {
46     type         => 'one to many',
47     class        => 'SL::DB::AccTransaction',
48     column_map   => { id => 'trans_id' },
49     manager_args => { with_objects => [ 'chart' ],
50                       sort_by      => 'acc_trans_id ASC' }
51   },
52 );
53
54 __PACKAGE__->meta->initialize;
55
56 __PACKAGE__->attr_html('notes');
57 __PACKAGE__->attr_sorted('items');
58
59 sub items { goto &invoiceitems; }
60 sub add_items { goto &add_invoiceitems; }
61 sub record_number { goto &invnumber; };
62
63 sub is_sales {
64   # For compatibility with Order, DeliveryOrder
65   croak 'not an accessor' if @_ > 1;
66   return 0;
67 }
68
69 sub date {
70   goto &transdate;
71 }
72
73 sub reqdate {
74   goto &duedate;
75 }
76
77 sub customervendor {
78   goto &vendor;
79 }
80
81 sub abbreviation {
82   my $self = shift;
83
84   return t8('AP Transaction (abbreviation)') if !$self->invoice && !$self->storno;
85   return t8('AP Transaction (abbreviation)') . '(' . t8('Storno (one letter abbreviation)') . ')' if !$self->invoice && $self->storno;
86   return t8('Invoice (one letter abbreviation)'). '(' . t8('Storno (one letter abbreviation)') . ')' if $self->storno;
87   return t8('Invoice (one letter abbreviation)');
88
89 };
90
91 sub oneline_summary {
92   my $self = shift;
93
94   return sprintf("%s: %s %s %s (%s)", $self->abbreviation, $self->invnumber, $self->vendor->name,
95                                       $::form->format_amount(\%::myconfig, $self->amount,2), $self->transdate->to_kivitendo);
96 }
97
98 sub link {
99   my ($self) = @_;
100
101   my $html;
102   $html   = $self->presenter->purchase_invoice(display => 'inline') if $self->invoice;
103   $html   = $self->presenter->ap_transaction(display => 'inline') if !$self->invoice;
104
105   return $html;
106 }
107
108 sub invoice_type {
109   my ($self) = @_;
110
111   return 'ap_transaction' if !$self->invoice;
112   return 'purchase_invoice';
113 }
114
115 sub displayable_type {
116   my ($self) = @_;
117
118   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
119   return t8('Purchase Invoice');
120 }
121
122 sub displayable_name {
123   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
124 };
125
126 sub create_ap_row {
127   my ($self, %params) = @_;
128   # needs chart as param
129   # to be called after adding all AP_amount rows
130
131   # only allow this method for ap invoices (Kreditorenbuchung)
132   die if $self->invoice and not $self->vendor_id;
133
134   my $acc_trans = [];
135   my $chart = $params{chart} || SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ap_chart_id);
136   die "illegal chart in create_ap_row" unless $chart;
137
138   die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
139
140   # hardcoded entry for no tax, tax_id and taxkey should be 0
141   my $tax = SL::DB::Manager::Tax->find_by(id => 0, taxkey => 0) || die "Can't find tax with id 0 and taxkey 0";
142
143   my $sign = $self->vendor_id ? 1 : -1;
144   my $acc = SL::DB::AccTransaction->new(
145     amount     => $self->amount * $sign,
146     chart_id   => $params{chart}->id,
147     chart_link => $params{chart}->link,
148     transdate  => $self->transdate,
149     taxkey     => $tax->taxkey,
150     tax_id     => $tax->id,
151   );
152   $self->add_transactions( $acc );
153   push( @$acc_trans, $acc );
154   return $acc_trans;
155 };
156
157 sub add_ap_amount_row {
158   my ($self, %params ) = @_;
159
160   # only allow this method for ap invoices (Kreditorenbuchung)
161   die "not an ap invoice" if $self->invoice and not $self->vendor_id;
162
163   die "add_ap_amount_row needs a chart object as chart param" unless $params{chart} && $params{chart}->isa('SL::DB::Chart');
164   die unless $params{chart}->link =~ /AP_amount/;
165
166   my $acc_trans = [];
167
168   my $roundplaces = 2;
169   my ($netamount,$taxamount);
170
171   $netamount = $params{amount} * 1;
172   my $tax = SL::DB::Manager::Tax->find_by(id => $params{tax_id}) || die "Can't find tax with id " . $params{tax_id};
173
174   if ( $tax and $tax->rate != 0 ) {
175     ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
176   };
177   next unless $netamount; # netamount mustn't be zero
178
179   my $sign = $self->vendor_id ? -1 : 1;
180   my $acc = SL::DB::AccTransaction->new(
181     amount     => $netamount * $sign,
182     chart_id   => $params{chart}->id,
183     chart_link => $params{chart}->link,
184     transdate  => $self->transdate,
185     gldate     => $self->gldate,
186     taxkey     => $tax->taxkey,
187     tax_id     => $tax->id,
188     project_id => $params{project_id},
189   );
190
191   $self->add_transactions( $acc );
192   push( @$acc_trans, $acc );
193
194   if ( $taxamount ) {
195      my $acc = SL::DB::AccTransaction->new(
196        amount     => $taxamount * $sign,
197        chart_id   => $tax->chart_id,
198        chart_link => $tax->chart->link,
199        transdate  => $self->transdate,
200        gldate     => $self->gldate,
201        taxkey     => $tax->taxkey,
202        tax_id     => $tax->id,
203        project_id => $params{project_id},
204      );
205      $self->add_transactions( $acc );
206      push( @$acc_trans, $acc );
207   };
208   return $acc_trans;
209 };
210
211 sub mark_as_paid {
212   my ($self) = @_;
213
214   $self->update_attributes(paid => $self->amount);
215 }
216
217 sub effective_tax_point {
218   my ($self) = @_;
219
220   return $self->tax_point || $self->deliverydate || $self->transdate;
221 }
222
223 1;
224
225
226 __END__
227
228 =pod
229
230 =encoding UTF-8
231
232 =head1 NAME
233
234 SL::DB::PurchaseInvoice: Rose model for purchase invoices (table "ap")
235
236 =head1 FUNCTIONS
237
238 =over 4
239
240 =item C<mark_as_paid>
241
242 Marks the invoice as paid by setting its C<paid> member to the value of C<amount>.
243
244 =back
245
246 =head1 AUTHOR
247
248 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
249
250 =cut