1 package SL::DB::PurchaseInvoice;
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);
17 # The calculator hasn't been adjusted for purchase invoices yet.
18 # use SL::DB::Helper::PriceTaxCalculator;
20 __PACKAGE__->meta->add_relationship(
22 type => 'one to many',
23 class => 'SL::DB::InvoiceItem',
24 column_map => { id => 'trans_id' },
25 manager_args => { with_objects => [ 'part' ] }
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' ] }
34 type => 'many to many',
35 map_class => 'SL::DB::SepaExportItem',
37 map_to => 'sepa_export',
41 class => 'SL::DB::Shipto',
42 column_map => { id => 'trans_id' },
43 query_args => [ module => 'AP' ],
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' }
54 __PACKAGE__->meta->initialize;
56 __PACKAGE__->attr_html('notes');
57 __PACKAGE__->attr_sorted('items');
59 sub items { goto &invoiceitems; }
60 sub add_items { goto &add_invoiceitems; }
61 sub record_number { goto &invnumber; };
64 # For compatibility with Order, DeliveryOrder
65 croak 'not an accessor' if @_ > 1;
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)');
94 return sprintf("%s: %s %s (%s)", $self->abbreviation, $self->invnumber, $self->vendor->name, $self->transdate->to_kivitendo);
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;
110 return 'ap_transaction' if !$self->invoice;
111 return 'purchase_invoice';
114 sub displayable_type {
117 return t8('AP Transaction') if $self->invoice_type eq 'ap_transaction';
118 return t8('Purchase Invoice');
121 sub displayable_name {
122 join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
126 my ($self, %params) = @_;
127 # needs chart as param
128 # to be called after adding all AP_amount rows
130 # only allow this method for ap invoices (Kreditorenbuchung)
131 die if $self->invoice and not $self->vendor_id;
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;
137 die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
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";
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,
151 $self->add_transactions( $acc );
152 push( @$acc_trans, $acc );
156 sub add_ap_amount_row {
157 my ($self, %params ) = @_;
159 # only allow this method for ap invoices (Kreditorenbuchung)
160 die "not an ap invoice" if $self->invoice and not $self->vendor_id;
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/;
168 my ($netamount,$taxamount);
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};
173 if ( $tax and $tax->rate != 0 ) {
174 ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
176 next unless $netamount; # netamount mustn't be zero
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,
186 project_id => $params{project_id},
189 $self->add_transactions( $acc );
190 push( @$acc_trans, $acc );
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,
200 project_id => $params{project_id},
202 $self->add_transactions( $acc );
203 push( @$acc_trans, $acc );
211 $self->update_attributes(paid => $self->amount);
225 SL::DB::PurchaseInvoice: Rose model for purchase invoices (table "ap")
231 =item C<mark_as_paid>
233 Marks the invoice as paid by setting its C<paid> member to the value of C<amount>.
239 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>