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 (%s)", $self->abbreviation, $self->invnumber, $self->vendor->name,
 
  95                                       $::form->format_amount(\%::myconfig, $self->amount,2), $self->transdate->to_kivitendo);
 
 102   $html   = SL::Presenter->get->purchase_invoice($self, display => 'inline') if $self->invoice;
 
 103   $html   = SL::Presenter->get->ap_transaction($self, display => 'inline') if !$self->invoice;
 
 111   return 'ap_transaction' if !$self->invoice;
 
 112   return 'purchase_invoice';
 
 115 sub displayable_type {
 
 118   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
 
 119   return t8('Purchase Invoice');
 
 122 sub displayable_name {
 
 123   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
 
 127   my ($self, %params) = @_;
 
 128   # needs chart as param
 
 129   # to be called after adding all AP_amount rows
 
 131   # only allow this method for ap invoices (Kreditorenbuchung)
 
 132   die if $self->invoice and not $self->vendor_id;
 
 135   my $chart = $params{chart} || SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ap_chart_id);
 
 136   die "illegal chart in create_ap_row" unless $chart;
 
 138   die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
 
 140   # hardcoded entry for no tax, tax_id and taxkey should be 0
 
 141   my $tax = SL::DB::Manager::Tax->find_by(id => 0, taxkey => 0) || die "Can't find tax with id 0 and taxkey 0";
 
 143   my $sign = $self->vendor_id ? 1 : -1;
 
 144   my $acc = SL::DB::AccTransaction->new(
 
 145     amount     => $self->amount * $sign,
 
 146     chart_id   => $params{chart}->id,
 
 147     chart_link => $params{chart}->link,
 
 148     transdate  => $self->transdate,
 
 149     taxkey     => $tax->taxkey,
 
 152   $self->add_transactions( $acc );
 
 153   push( @$acc_trans, $acc );
 
 157 sub add_ap_amount_row {
 
 158   my ($self, %params ) = @_;
 
 160   # only allow this method for ap invoices (Kreditorenbuchung)
 
 161   die "not an ap invoice" if $self->invoice and not $self->vendor_id;
 
 163   die "add_ap_amount_row needs a chart object as chart param" unless $params{chart} && $params{chart}->isa('SL::DB::Chart');
 
 164   die unless $params{chart}->link =~ /AP_amount/;
 
 169   my ($netamount,$taxamount);
 
 171   $netamount = $params{amount} * 1;
 
 172   my $tax = SL::DB::Manager::Tax->find_by(id => $params{tax_id}) || die "Can't find tax with id " . $params{tax_id};
 
 174   if ( $tax and $tax->rate != 0 ) {
 
 175     ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
 
 177   next unless $netamount; # netamount mustn't be zero
 
 179   my $sign = $self->vendor_id ? -1 : 1;
 
 180   my $acc = SL::DB::AccTransaction->new(
 
 181     amount     => $netamount * $sign,
 
 182     chart_id   => $params{chart}->id,
 
 183     chart_link => $params{chart}->link,
 
 184     transdate  => $self->transdate,
 
 185     taxkey     => $tax->taxkey,
 
 187     project_id => $params{project_id},
 
 190   $self->add_transactions( $acc );
 
 191   push( @$acc_trans, $acc );
 
 194      my $acc = SL::DB::AccTransaction->new(
 
 195        amount     => $taxamount * $sign,
 
 196        chart_id   => $tax->chart_id,
 
 197        chart_link => $tax->chart->link,
 
 198        transdate  => $self->transdate,
 
 199        taxkey     => $tax->taxkey,
 
 201        project_id => $params{project_id},
 
 203      $self->add_transactions( $acc );
 
 204      push( @$acc_trans, $acc );
 
 212   $self->update_attributes(paid => $self->amount);
 
 226 SL::DB::PurchaseInvoice: Rose model for purchase invoices (table "ap")
 
 232 =item C<mark_as_paid>
 
 234 Marks the invoice as paid by setting its C<paid> member to the value of C<amount>.
 
 240 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>