SL::DB::(Purchase)Invoice: neue Funktion »mark_as_paid«
[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 link {
92   my ($self) = @_;
93
94   my $html;
95   $html   = SL::Presenter->get->purchase_invoice($self, display => 'inline') if $self->invoice;
96   $html   = SL::Presenter->get->ap_transaction($self, display => 'inline') if !$self->invoice;
97
98   return $html;
99 }
100
101 sub invoice_type {
102   my ($self) = @_;
103
104   return 'ap_transaction' if !$self->invoice;
105   return 'purchase_invoice';
106 }
107
108 sub displayable_type {
109   my ($self) = @_;
110
111   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
112   return t8('Purchase Invoice');
113 }
114
115 sub displayable_name {
116   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
117 };
118
119 sub create_ap_row {
120   my ($self, %params) = @_;
121   # needs chart as param
122   # to be called after adding all AP_amount rows
123
124   # only allow this method for ap invoices (Kreditorenbuchung)
125   die if $self->invoice and not $self->vendor_id;
126
127   my $acc_trans = [];
128   my $chart = $params{chart} || SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ap_chart_id);
129   die "illegal chart in create_ap_row" unless $chart;
130
131   die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
132
133   # hardcoded entry for no tax, tax_id and taxkey should be 0
134   my $tax = SL::DB::Manager::Tax->find_by(id => 0, taxkey => 0) || die "Can't find tax with id 0 and taxkey 0";
135
136   my $sign = $self->vendor_id ? 1 : -1;
137   my $acc = SL::DB::AccTransaction->new(
138     amount     => $self->amount * $sign,
139     chart_id   => $params{chart}->id,
140     chart_link => $params{chart}->link,
141     transdate  => $self->transdate,
142     taxkey     => $tax->taxkey,
143     tax_id     => $tax->id,
144   );
145   $self->add_transactions( $acc );
146   push( @$acc_trans, $acc );
147   return $acc_trans;
148 };
149
150 sub add_ap_amount_row {
151   my ($self, %params ) = @_;
152
153   # only allow this method for ap invoices (Kreditorenbuchung)
154   die "not an ap invoice" if $self->invoice and not $self->vendor_id;
155
156   die "add_ap_amount_row needs a chart object as chart param" unless $params{chart} && $params{chart}->isa('SL::DB::Chart');
157   die unless $params{chart}->link =~ /AP_amount/;
158
159   my $acc_trans = [];
160
161   my $roundplaces = 2;
162   my ($netamount,$taxamount);
163
164   $netamount = $params{amount} * 1;
165   my $tax = SL::DB::Manager::Tax->find_by(id => $params{tax_id}) || die "Can't find tax with id " . $params{tax_id};
166
167   if ( $tax and $tax->rate != 0 ) {
168     ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
169   };
170   next unless $netamount; # netamount mustn't be zero
171
172   my $sign = $self->vendor_id ? -1 : 1;
173   my $acc = SL::DB::AccTransaction->new(
174     amount     => $netamount * $sign,
175     chart_id   => $params{chart}->id,
176     chart_link => $params{chart}->link,
177     transdate  => $self->transdate,
178     taxkey     => $tax->taxkey,
179     tax_id     => $tax->id,
180     project_id => $params{project_id},
181   );
182
183   $self->add_transactions( $acc );
184   push( @$acc_trans, $acc );
185
186   if ( $taxamount ) {
187      my $acc = SL::DB::AccTransaction->new(
188        amount     => $taxamount * $sign,
189        chart_id   => $tax->chart_id,
190        chart_link => $tax->chart->link,
191        transdate  => $self->transdate,
192        taxkey     => $tax->taxkey,
193        tax_id     => $tax->id,
194        project_id => $params{project_id},
195      );
196      $self->add_transactions( $acc );
197      push( @$acc_trans, $acc );
198   };
199   return $acc_trans;
200 };
201
202 sub mark_as_paid {
203   my ($self) = @_;
204
205   $self->update_attributes(paid => $self->amount);
206 }
207
208 1;
209
210
211 __END__
212
213 =pod
214
215 =encoding UTF-8
216
217 =head1 NAME
218
219 SL::DB::PurchaseInvoice: Rose model for purchase invoices (table "ap")
220
221 =head1 FUNCTIONS
222
223 =over 4
224
225 =item C<mark_as_paid>
226
227 Marks the invoice as paid by setting its C<paid> member to the value of C<amount>.
228
229 =back
230
231 =head1 AUTHOR
232
233 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
234
235 =cut