1 package SL::DB::PurchaseInvoice;
 
   7 use SL::DB::MetaSetup::PurchaseInvoice;
 
   8 use SL::DB::Manager::PurchaseInvoice;
 
   9 use SL::DB::Helper::AttrHTML;
 
  10 use SL::DB::Helper::AttrSorted;
 
  11 use SL::DB::Helper::LinkedRecords;
 
  12 use SL::DB::Helper::Payment qw(:ALL);
 
  13 use SL::Locale::String qw(t8);
 
  14 use Rose::DB::Object::Helpers qw(has_loaded_related forget_related);
 
  16 # The calculator hasn't been adjusted for purchase invoices yet.
 
  17 # use SL::DB::Helper::PriceTaxCalculator;
 
  19 __PACKAGE__->meta->add_relationship(
 
  21     type         => 'one to many',
 
  22     class        => 'SL::DB::InvoiceItem',
 
  23     column_map   => { id => 'trans_id' },
 
  24     manager_args => { with_objects => [ 'part' ] }
 
  26   sepa_export_items => {
 
  27     type            => 'one to many',
 
  28     class           => 'SL::DB::SepaExportItem',
 
  29     column_map      => { id => 'ap_id' },
 
  30     manager_args    => { with_objects => [ 'sepa_export' ] }
 
  33     type            => 'many to many',
 
  34     map_class       => 'SL::DB::SepaExportItem',
 
  36     map_to          => 'sepa_export',
 
  40     class           => 'SL::DB::Shipto',
 
  41     column_map      => { id => 'trans_id' },
 
  42     query_args      => [ module => 'AP' ],
 
  45     type         => 'one to many',
 
  46     class        => 'SL::DB::AccTransaction',
 
  47     column_map   => { id => 'trans_id' },
 
  48     manager_args => { with_objects => [ 'chart' ],
 
  49                       sort_by      => 'acc_trans_id ASC' }
 
  53 __PACKAGE__->meta->initialize;
 
  55 __PACKAGE__->attr_html('notes');
 
  56 __PACKAGE__->attr_sorted('items');
 
  58 sub items { goto &invoiceitems; }
 
  59 sub add_items { goto &add_invoiceitems; }
 
  60 sub record_number { goto &invnumber; };
 
  63   # For compatibility with Order, DeliveryOrder
 
  64   croak 'not an accessor' if @_ > 1;
 
  83   return t8('AP Transaction (abbreviation)') if !$self->invoice && !$self->storno;
 
  84   return t8('AP Transaction (abbreviation)') . '(' . t8('Storno (one letter abbreviation)') . ')' if !$self->invoice && $self->storno;
 
  85   return t8('Invoice (one letter abbreviation)'). '(' . t8('Storno (one letter abbreviation)') . ')' if $self->storno;
 
  86   return t8('Invoice (one letter abbreviation)');
 
  94   $html   = SL::Presenter->get->purchase_invoice($self, display => 'inline') if $self->invoice;
 
  95   $html   = SL::Presenter->get->ap_transaction($self, display => 'inline') if !$self->invoice;
 
 103   return 'ap_transaction' if !$self->invoice;
 
 104   return 'purchase_invoice';
 
 107 sub displayable_type {
 
 110   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
 
 111   return t8('Purchase Invoice');
 
 114 sub displayable_name {
 
 115   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
 
 119   my ($self, %params) = @_;
 
 120   # needs chart as param
 
 121   # to be called after adding all AP_amount rows
 
 123   # only allow this method for ap invoices (Kreditorenbuchung)
 
 124   die if $self->invoice and not $self->vendor_id;
 
 127   my $chart = $params{chart} || SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ap_chart_id);
 
 128   die "illegal chart in create_ap_row" unless $chart;
 
 130   die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
 
 132   # hardcoded entry for no tax, tax_id and taxkey should be 0
 
 133   my $tax = SL::DB::Manager::Tax->find_by(id => 0, taxkey => 0) || die "Can't find tax with id 0 and taxkey 0";
 
 135   my $sign = $self->vendor_id ? 1 : -1;
 
 136   my $acc = SL::DB::AccTransaction->new(
 
 137     amount     => $self->amount * $sign,
 
 138     chart_id   => $params{chart}->id,
 
 139     chart_link => $params{chart}->link,
 
 140     transdate  => $self->transdate,
 
 141     taxkey     => $tax->taxkey,
 
 144   $self->add_transactions( $acc );
 
 145   push( @$acc_trans, $acc );
 
 149 sub add_ap_amount_row {
 
 150   my ($self, %params ) = @_;
 
 152   # only allow this method for ap invoices (Kreditorenbuchung)
 
 153   die "not an ap invoice" if $self->invoice and not $self->vendor_id;
 
 155   die "add_ap_amount_row needs a chart object as chart param" unless $params{chart} && $params{chart}->isa('SL::DB::Chart');
 
 156   die unless $params{chart}->link =~ /AP_amount/;
 
 161   my ($netamount,$taxamount);
 
 163   $netamount = $params{amount} * 1;
 
 164   my $tax = SL::DB::Manager::Tax->find_by(id => $params{tax_id}) || die "Can't find tax with id " . $params{tax_id};
 
 166   if ( $tax and $tax->rate != 0 ) {
 
 167     ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
 
 169   next unless $netamount; # netamount mustn't be zero
 
 171   my $sign = $self->vendor_id ? -1 : 1;
 
 172   my $acc = SL::DB::AccTransaction->new(
 
 173     amount     => $netamount * $sign,
 
 174     chart_id   => $params{chart}->id,
 
 175     chart_link => $params{chart}->link,
 
 176     transdate  => $self->transdate,
 
 177     taxkey     => $tax->taxkey,
 
 179     project_id => $params{project_id},
 
 182   $self->add_transactions( $acc );
 
 183   push( @$acc_trans, $acc );
 
 186      my $acc = SL::DB::AccTransaction->new(
 
 187        amount     => $taxamount * $sign,
 
 188        chart_id   => $tax->chart_id,
 
 189        chart_link => $tax->chart->link,
 
 190        transdate  => $self->transdate,
 
 191        taxkey     => $tax->taxkey,
 
 193        project_id => $params{project_id},
 
 195      $self->add_transactions( $acc );
 
 196      push( @$acc_trans, $acc );