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