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)');
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;
104 return 'ap_transaction' if !$self->invoice;
105 return 'purchase_invoice';
108 sub displayable_type {
111 return t8('AP Transaction') if $self->invoice_type eq 'ap_transaction';
112 return t8('Purchase Invoice');
115 sub displayable_name {
116 join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
120 my ($self, %params) = @_;
121 # needs chart as param
122 # to be called after adding all AP_amount rows
124 # only allow this method for ap invoices (Kreditorenbuchung)
125 die if $self->invoice and not $self->vendor_id;
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;
131 die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
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";
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,
145 $self->add_transactions( $acc );
146 push( @$acc_trans, $acc );
150 sub add_ap_amount_row {
151 my ($self, %params ) = @_;
153 # only allow this method for ap invoices (Kreditorenbuchung)
154 die "not an ap invoice" if $self->invoice and not $self->vendor_id;
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/;
162 my ($netamount,$taxamount);
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};
167 if ( $tax and $tax->rate != 0 ) {
168 ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
170 next unless $netamount; # netamount mustn't be zero
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,
180 project_id => $params{project_id},
183 $self->add_transactions( $acc );
184 push( @$acc_trans, $acc );
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,
194 project_id => $params{project_id},
196 $self->add_transactions( $acc );
197 push( @$acc_trans, $acc );
205 $self->update_attributes(paid => $self->amount);
219 SL::DB::PurchaseInvoice: Rose model for purchase invoices (table "ap")
225 =item C<mark_as_paid>
227 Marks the invoice as paid by setting its C<paid> member to the value of C<amount>.
233 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>