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::DB::Helper::SalesPurchaseInvoice;
 
  15 use SL::Locale::String qw(t8);
 
  16 use Rose::DB::Object::Helpers qw(has_loaded_related forget_related);
 
  18 # The calculator hasn't been adjusted for purchase invoices yet.
 
  19 # use SL::DB::Helper::PriceTaxCalculator;
 
  21 __PACKAGE__->meta->add_relationship(
 
  23     type         => 'one to many',
 
  24     class        => 'SL::DB::InvoiceItem',
 
  25     column_map   => { id => 'trans_id' },
 
  26     manager_args => { with_objects => [ 'part' ] }
 
  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' ] }
 
  35     type            => 'many to many',
 
  36     map_class       => 'SL::DB::SepaExportItem',
 
  38     map_to          => 'sepa_export',
 
  42     class           => 'SL::DB::Shipto',
 
  43     column_map      => { id => 'trans_id' },
 
  44     query_args      => [ module => 'AP' ],
 
  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' }
 
  55 __PACKAGE__->meta->initialize;
 
  57 __PACKAGE__->attr_html('notes');
 
  58 __PACKAGE__->attr_sorted('items');
 
  60 sub items { goto &invoiceitems; }
 
  61 sub add_items { goto &add_invoiceitems; }
 
  62 sub record_number { goto &invnumber; };
 
  65   # For compatibility with Order, DeliveryOrder
 
  66   croak 'not an accessor' if @_ > 1;
 
  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)');
 
  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);
 
 103   $html   = $self->presenter->purchase_invoice(display => 'inline') if $self->invoice;
 
 104   $html   = $self->presenter->ap_transaction(display => 'inline') if !$self->invoice;
 
 112   return 'ap_transaction' if !$self->invoice;
 
 113   return 'purchase_invoice';
 
 116 sub displayable_type {
 
 119   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
 
 120   return t8('Purchase Invoice');
 
 123 sub displayable_name {
 
 124   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
 
 128   my ($self, %params) = @_;
 
 129   # needs chart as param
 
 130   # to be called after adding all AP_amount rows
 
 132   # only allow this method for ap invoices (Kreditorenbuchung)
 
 133   die if $self->invoice and not $self->vendor_id;
 
 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;
 
 139   die "receivables chart must have link 'AP'" . Dumper($chart) unless $chart->link eq 'AP';
 
 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";
 
 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,
 
 153   $self->add_transactions( $acc );
 
 154   push( @$acc_trans, $acc );
 
 158 sub add_ap_amount_row {
 
 159   my ($self, %params ) = @_;
 
 161   # only allow this method for ap invoices (Kreditorenbuchung)
 
 162   die "not an ap invoice" if $self->invoice and not $self->vendor_id;
 
 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/;
 
 170   my ($netamount,$taxamount);
 
 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};
 
 175   if ( $tax and $tax->rate != 0 ) {
 
 176     ($netamount, $taxamount) = Form->calculate_tax($params{amount}, $tax->rate, $self->taxincluded, $roundplaces);
 
 178   next unless $netamount; # netamount mustn't be zero
 
 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,
 
 189     project_id => $params{project_id},
 
 192   $self->add_transactions( $acc );
 
 193   push( @$acc_trans, $acc );
 
 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,
 
 204        project_id => $params{project_id},
 
 206      $self->add_transactions( $acc );
 
 207      push( @$acc_trans, $acc );
 
 215   $self->update_attributes(paid => $self->amount);
 
 218 sub effective_tax_point {
 
 221   return $self->tax_point || $self->deliverydate || $self->transdate;
 
 235 SL::DB::PurchaseInvoice: Rose model for purchase invoices (table "ap")
 
 241 =item C<mark_as_paid>
 
 243 Marks the invoice as paid by setting its C<paid> member to the value of C<amount>.
 
 249 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>