]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DB/Invoice.pm
FiBu Schellsuche in Headerzeile
[mfinanz.git] / SL / DB / Invoice.pm
index 9cf52a24cf28f6817af31a7326f10ed845e589c4..d734003a16084c7bf8313787f6c2b5ed7a5d18c8 100644 (file)
@@ -17,6 +17,7 @@ use SL::DB::Helper::LinkedRecords;
 use SL::DB::Helper::PriceTaxCalculator;
 use SL::DB::Helper::PriceUpdater;
 use SL::DB::Helper::TransNumberGenerator;
+use SL::Locale::String qw(t8);
 
 __PACKAGE__->meta->add_relationship(
   invoiceitems => {
@@ -63,6 +64,7 @@ sub _before_save_set_invnumber {
 # methods
 
 sub items { goto &invoiceitems; }
+sub add_items { goto &add_invoiceitems; }
 
 sub items_sorted {
   my ($self) = @_;
@@ -130,7 +132,10 @@ sub new_from {
 
   require SL::DB::Employee;
 
-  my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_terms->terms_netto : 0;
+  my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_terms
+            : $source->customer_id                              ? $source ->customer->payment_terms
+            :                                                     undef;
+
   my (@columns, @item_columns, $item_parent_id_column, $item_parent_column);
 
   if (ref($source) eq 'SL::DB::Order') {
@@ -151,7 +156,8 @@ sub new_from {
                                                 cp_id language_id taxzone_id shipto_id globalproject_id transaction_description currency_id delivery_term_id), @columns),
                transdate   => DateTime->today_local,
                gldate      => DateTime->today_local,
-               duedate     => DateTime->today_local->add(days => $terms * 1),
+               duedate     => DateTime->today_local->add(days => ($terms ? $terms->terms_netto * 1 : 1)),
+               payment_id  => $terms ? $terms->id : undef,
                invoice     => 1,
                type        => 'invoice',
                storno      => 0,
@@ -166,7 +172,8 @@ sub new_from {
     $args{quodate}      = $source->transdate;
   }
 
-  my $invoice = $class->new(%args, %{ $params{attributes} || {} });
+  my $invoice = $class->new(%args);
+  $invoice->assign_attributes(%{ $params{attributes} }) if $params{attributes};
   my $items   = delete($params{items}) || $source->items_sorted;
   my %item_parents;
 
@@ -311,10 +318,30 @@ sub displayable_state {
   return $self->closed ? $::locale->text('closed') : $::locale->text('open');
 }
 
+sub abbreviation {
+  my $self = shift;
+
+  return t8('AR Transaction (abbreviation)') if !$self->invoice;
+  return t8('Credit note (one letter abbreviation)') if $self->type eq 'credit_note' && $self->amount < 0 && !$self->storno;
+  return t8('Invoice (one letter abbreviation)') . "(" . t8('Storno (one letter abbreviation)') . ")" if $self->type ne 'credit_note' && $self->amount < 0 &&  $self->storno;
+  return t8('Credit note (one letter abbreviation)') . "(" . t8('Storno (one letter abbreviation)') . ")" if $self->type eq 'credit_note' && $self->amount > 0 &&  $self->storno;
+  return t8('Invoice (one letter abbreviation)');
+
+}
+
 sub date {
   goto &transdate;
 }
 
+sub transactions {
+  my ($self) = @_;
+
+  return unless $self->id;
+
+  require SL::DB::AccTransaction;
+  SL::DB::Manager::AccTransaction->get_all(query => [ trans_id => $self->id ]);
+}
+
 1;
 
 __END__