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