X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FInvoice.pm;h=302179c807777fb9eafef980f8d70482c8bf6cfb;hb=a34ae1630d888a6c824dad13db000bd7e0e1c3e6;hp=6e0cf60a120fb3970ecabaa13172325b61ea9162;hpb=6834acd9f95035050fc9bbe591bcbbd6d1c2a4f4;p=kivitendo-erp.git diff --git a/SL/DB/Invoice.pm b/SL/DB/Invoice.pm index 6e0cf60a1..302179c80 100644 --- a/SL/DB/Invoice.pm +++ b/SL/DB/Invoice.pm @@ -10,10 +10,13 @@ use List::Util qw(first); use SL::DB::MetaSetup::Invoice; use SL::DB::Manager::Invoice; +use SL::DB::Helper::FlattenToForm; use SL::DB::Helper::LinkedRecords; use SL::DB::Helper::PriceTaxCalculator; +use SL::DB::Helper::PriceUpdater; use SL::DB::Helper::TransNumberGenerator; use SL::DB::AccTransaction; +use SL::DB::Chart; use SL::DB::Employee; __PACKAGE__->meta->add_relationship( @@ -25,10 +28,16 @@ __PACKAGE__->meta->add_relationship( with_objects => [ 'part' ] } }, - payment_term => { - type => 'one to one', - class => 'SL::DB::PaymentTerm', - column_map => { payment_id => 'id' }, + storno_invoices => { + type => 'one to many', + class => 'SL::DB::Invoice', + column_map => { id => 'storno_id' }, + }, + sepa_export_items => { + type => 'one to many', + class => 'SL::DB::SepaExportItem', + column_map => { id => 'ar_id' }, + manager_args => { with_objects => [ 'sepa_export' ] } }, ); @@ -38,6 +47,18 @@ __PACKAGE__->meta->initialize; sub items { goto &invoiceitems; } +sub items_sorted { + my ($self) = @_; + + return [ sort {$a->id <=> $b->id } @{ $self->items } ]; +} + +sub is_sales { + # For compatibility with Order, DeliveryOrder + croak 'not an accessor' if @_ > 1; + return 1; +} + # it is assumed, that ordnumbers are unique here. sub first_order_by_ordnumber { my $self = shift; @@ -65,7 +86,7 @@ sub taxamount { my $self = shift; die 'not a setter method' if @_; - return $self->amount - $self->netamount; + return ($self->amount || 0) - ($self->netamount || 0); } __PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)'); @@ -81,11 +102,11 @@ sub new_from { croak("Unsupported source object type '" . ref($source) . "'") unless ref($source) =~ m/^ SL::DB:: (?: Order | DeliveryOrder ) $/x; croak("Cannot create invoices for purchase records") unless $source->customer_id; - my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_term->terms_netto : 0; + my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_terms->terms_netto : 0; - my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes curr salesman_id cusordnumber ordnumber quonumber + my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes salesman_id cusordnumber ordnumber quonumber department_id cp_id language_id payment_id delivery_customer_id delivery_vendor_id taxzone_id shipto_id - globalproject_id transaction_description)), + globalproject_id transaction_description currency_id)), transdate => DateTime->today_local, gldate => DateTime->today_local, duedate => DateTime->today_local->add(days => $terms * 1), @@ -113,7 +134,7 @@ sub new_from { base_qty subtotal longdescription lastcost price_factor_id)), deliverydate => $source_item->reqdate, fxsellprice => $source_item->sellprice,); - } @{ $source->items }; + } @{ $source->items_sorted }; $invoice->invoiceitems(\@items); @@ -123,7 +144,13 @@ sub new_from { sub post { my ($self, %params) = @_; - croak("Missing parameter 'ar_id'") unless $params{ar_id}; + if (!$params{ar_id}) { + my $chart = SL::DB::Manager::Chart->get_all(query => [ SL::DB::Manager::Chart->link_filter('AR') ], + sort_by => 'id ASC', + limit => 1)->[0]; + croak("No AR chart found and no parameter `ar_id' given") unless $chart; + $params{ar_id} = $chart->id; + } my $worker = sub { my %data = $self->calculate_prices_and_taxes; @@ -144,7 +171,7 @@ sub post { if ($self->db->in_transaction) { $worker->(); } elsif (!$self->db->do_transaction($worker)) { - $::lxdebug->message(0, "convert_to_invoice failed: " . join("\n", (split(/\n/, $self->db->error))[0..2])); + $::lxdebug->message(LXDebug->WARN(), "convert_to_invoice failed: " . join("\n", (split(/\n/, $self->db->error))[0..2])); return undef; } @@ -154,14 +181,22 @@ sub post { sub _post_add_acctrans { my ($self, $entries) = @_; + my $default_tax_id = SL::DB::Manager::Tax->find_by(taxkey => 0)->id; + my $chart_link; + while (my ($chart_id, $spec) = each %{ $entries }) { - $spec = { taxkey => 0, amount => $spec } unless ref $spec; + $spec = { taxkey => 0, tax_id => $default_tax_id, amount => $spec } unless ref $spec; + $chart_link = SL::DB::Manager::Chart->find_by(id => $chart_id)->{'link'}; + $chart_link ||= ''; + SL::DB::AccTransaction->new(trans_id => $self->id, chart_id => $chart_id, amount => $spec->{amount}, + tax_id => $spec->{tax_id}, taxkey => $spec->{taxkey}, project_id => $self->globalproject_id, - transdate => $self->transdate)->save; + transdate => $self->transdate, + chart_link => $chart_link)->save; } } @@ -202,6 +237,26 @@ sub _post_update_allocated { } } +sub invoice_type { + my ($self) = @_; + + return 'ar_transaction' if !$self->invoice; + return 'credit_note' if $self->type eq 'credit_note' && $self->amount < 0 && !$self->storno; + return 'invoice_storno' if $self->type ne 'credit_note' && $self->amount < 0 && $self->storno; + return 'credit_note_storno' if $self->type eq 'credit_note' && $self->amount > 0 && $self->storno; + return 'invoice'; +} + +sub displayable_state { + my $self = shift; + + return $self->closed ? $::locale->text('closed') : $::locale->text('open'); +} + +sub date { + goto &transdate; +} + 1; __END__ @@ -241,7 +296,8 @@ Posts the invoice. Required parameters are: =item * C The ID of the accounds receivable chart the invoices amounts are -posted to. +posted to. If it is not set then the first chart configured for +accounts receivables is used. =back @@ -266,14 +322,16 @@ and recorded in C. =item 6. Items in C are updated according to their allocation status (regarding for costs of goold sold). Will only be done if -Lx-Office is not configured to use Einnahmenüberschussrechnungen -(C<$::eur>). +kivitendo is not configured to use Einnahmenüberschussrechnungen. =item 7. The invoice and its items are saved. =back -Returns C<$self> on success and C on failure. +Returns C<$self> on success and C on failure. The whole process +is run inside a transaction. If it fails then nothing is saved to or +changed in the database. A new transaction is only started if none is +active. =item C