--- /dev/null
+package SL::Controller::CsvImport::ARTransaction;
+
+use strict;
+
+use List::MoreUtils qw(any);
+
+use Data::Dumper;
+use SL::Helper::Csv;
+use SL::Controller::CsvImport::Helper::Consistency;
+use SL::DB::Invoice;
+use SL::DB::AccTransaction;
+use SL::DB::Department;
+use SL::DB::Project;
+use SL::DB::TaxZone;
+use SL::DB::Chart;
+use SL::TransNumber;
+use DateTime;
+
+use parent qw(SL::Controller::CsvImport::BaseMulti);
+
+use Rose::Object::MakeMethods::Generic
+(
+ 'scalar --get_set_init' => [ qw(settings charts_by taxkeys_by) ],
+);
+
+
+sub init_class {
+ my ($self) = @_;
+ $self->class(['SL::DB::Invoice', 'SL::DB::AccTransaction']);
+}
+
+sub set_profile_defaults {
+ my ($self) = @_;
+
+ $self->controller->profile->_set_defaults(
+ ar_column => $::locale->text('Invoice'),
+ transaction_column => $::locale->text('AccTransaction'),
+ max_amount_diff => 0.02,
+ );
+};
+
+
+sub init_settings {
+ my ($self) = @_;
+
+ return { map { ( $_ => $self->controller->profile->get($_) ) } qw(ar_column transaction_column max_amount_diff) };
+}
+
+sub init_profile {
+ my ($self) = @_;
+
+ my $profile = $self->SUPER::init_profile;
+
+ # SUPER::init_profile sets row_ident to the translated class name
+ # overwrite it with the user specified settings
+# TODO: remove hardcoded row_idents
+ foreach my $p (@{ $profile }) {
+ if ($p->{class} eq 'SL::DB::Invoice') {
+ $p->{row_ident} = $self->_ar_column;
+ }
+ if ($p->{class} eq 'SL::DB::AccTransaction') {
+ $p->{row_ident} = $self->_transaction_column;
+ }
+ }
+
+ foreach my $p (@{ $profile }) {
+ my $prof = $p->{profile};
+ if ($p->{row_ident} eq $self->_ar_column) {
+ # no need to handle
+ delete @{$prof}{qw(delivery_customer_id delivery_vendor_id )};
+ }
+ if ($p->{row_ident} eq $self->_transaction_column) {
+ # no need to handle
+ delete @{$prof}{qw(trans_id)};
+ }
+ }
+
+ return $profile;
+}
+
+
+sub setup_displayable_columns {
+ my ($self) = @_;
+
+ $self->SUPER::setup_displayable_columns;
+
+ $self->add_displayable_columns($self->_ar_column,
+ { name => 'datatype', description => $self->_ar_column . ' [1]' },
+ { name => 'currency', description => $::locale->text('Currency') },
+ { name => 'cusordnumber', description => $::locale->text('Customer Order Number') },
+ { name => 'direct_debit', description => $::locale->text('direct debit') },
+ { name => 'donumber', description => $::locale->text('Delivery Order Number') },
+ { name => 'duedate', description => $::locale->text('Due Date') },
+ { name => 'delivery_term_id', description => $::locale->text('Delivery terms (database ID)') },
+ { name => 'delivery_term', description => $::locale->text('Delivery terms (name)') },
+ { name => 'deliverydate', description => $::locale->text('Delivery Date') },
+ { name => 'employee_id', description => $::locale->text('Employee (database ID)') },
+ { name => 'intnotes', description => $::locale->text('Internal Notes') },
+ { name => 'notes', description => $::locale->text('Notes') },
+ { name => 'invnumber', description => $::locale->text('Invoice Number') },
+ { name => 'quonumber', description => $::locale->text('Quotation Number') },
+ { name => 'reqdate', description => $::locale->text('Reqdate') },
+ { name => 'salesman_id', description => $::locale->text('Salesman (database ID)') },
+ { name => 'transaction_description', description => $::locale->text('Transaction description') },
+ { name => 'transdate', description => $::locale->text('Invoice Date') },
+ { name => 'verify_amount', description => $::locale->text('Amount (for verification)') . ' [2]' },
+ { name => 'verify_netamount', description => $::locale->text('Net amount (for verification)') . ' [2]'},
+ { name => 'taxincluded', description => $::locale->text('Tax Included') },
+ { name => 'customer', description => $::locale->text('Customer (name)') },
+ { name => 'customernumber', description => $::locale->text('Customer Number') },
+ { name => 'customer_id', description => $::locale->text('Customer (database ID)') },
+ { name => 'language_id', description => $::locale->text('Language (database ID)') },
+ { name => 'language', description => $::locale->text('Language (name)') },
+ { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
+ { name => 'payment', description => $::locale->text('Payment terms (name)') },
+ { name => 'taxzone_id', description => $::locale->text('Tax zone (database ID)') },
+ { name => 'taxzone', description => $::locale->text('Tax zone (description)') },
+ { name => 'department_id', description => $::locale->text('Department (database ID)') },
+ { name => 'department', description => $::locale->text('Department (description)') },
+ { name => 'globalproject_id', description => $::locale->text('Document Project (database ID)') },
+ { name => 'globalprojectnumber', description => $::locale->text('Document Project (number)') },
+ { name => 'globalproject', description => $::locale->text('Document Project (description)') },
+ { name => 'archart', description => $::locale->text('Receivables account (account number)') },
+ { name => 'orddate', description => $::locale->text('Order Date') },
+ { name => 'ordnumber', description => $::locale->text('Order Number') },
+ { name => 'quonumber', description => $::locale->text('Quotation Number') },
+ { name => 'quodate', description => $::locale->text('Quotation Date') },
+ );
+
+ $self->add_displayable_columns($self->_transaction_column,
+ { name => 'datatype', description => $self->_transaction_column . ' [1]' },
+ { name => 'projectnumber', description => $::locale->text('Project (number)') },
+ { name => 'project', description => $::locale->text('Project (description)') },
+ { name => 'amount', description => $::locale->text('Amount') },
+ { name => 'chart', description => $::locale->text('Account number') },
+ { name => 'taxkey', description => $::locale->text('Taxkey') },
+ );
+}
+
+sub init_taxkeys_by {
+ my ($self) = @_;
+
+ my $all_taxes = SL::DB::Manager::Tax->get_all;
+ return { map { $_->taxkey => $_->id } @{ $all_taxes } };
+}
+
+
+sub init_charts_by {
+ my ($self) = @_;
+
+ my $all_charts = SL::DB::Manager::Chart->get_all;
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_charts } } ) } qw(id accno) };
+}
+
+sub check_objects {
+ my ($self) = @_;
+
+ $self->controller->track_progress(phase => 'building data', progress => 0);
+
+ my $i = 0;
+ my $num_data = scalar @{ $self->controller->data };
+
+ foreach my $entry (@{ $self->controller->data }) {
+ $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
+
+ if ($entry->{raw_data}->{datatype} eq $self->_ar_column) {
+ $self->handle_invoice($entry);
+ } elsif ($entry->{raw_data}->{datatype} eq $self->_transaction_column ) {
+ $self->handle_transaction($entry);
+ } else {
+ die "unknown datatype";
+ };
+
+ } continue {
+ $i++;
+ } # finished data parsing
+
+ $self->add_transactions_to_ar(); # go through all data entries again, adding receivable entry to ar lines while calculating amount and netamount
+
+ foreach my $entry (@{ $self->controller->data }) {
+ next unless ($entry->{raw_data}->{datatype} eq $self->_ar_column);
+ $self->check_verify_amounts($entry->{object});
+ };
+
+ foreach my $entry (@{ $self->controller->data }) {
+ next unless ($entry->{raw_data}->{datatype} eq $self->_ar_column);
+ unless ( $entry->{object}->validate_acc_trans ) {
+ push @{ $entry->{errors} }, $::locale->text('Error: ar transaction doesn\'t validate');
+ };
+ };
+
+ # add info columns that aren't directly part of the object to be imported
+ # but are always determined or should always be shown because they are mandatory
+ $self->add_info_columns($self->_ar_column,
+ { header => $::locale->text('Customer/Vendor'), method => 'vc_name' },
+ { header => $::locale->text('Receivables account'), method => 'archart' },
+ { header => $::locale->text('Amount'), method => 'amount' },
+ { header => $::locale->text('Net amount'), method => 'netamount' },
+ { header => $::locale->text('Tax zone'), method => 'taxzone' });
+
+ # Adding info_header this way only works, if the first invoice $self->controller->data->[0]
+
+ # Todo: access via ->[0] ok? Better: search first order column and use this
+ $self->add_info_columns($self->_ar_column, { header => $::locale->text('Department'), method => 'department' }) if $self->controller->data->[0]->{info_data}->{department} or $self->controller->data->[0]->{raw_data}->{department};
+
+ $self->add_info_columns($self->_ar_column, { header => $::locale->text('Project Number'), method => 'globalprojectnumber' }) if $self->controller->data->[0]->{info_data}->{globalprojectnumber};
+
+ $self->add_columns($self->_ar_column,
+ map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(payment department globalproject taxzone cp currency));
+ $self->add_columns($self->_ar_column, 'globalproject_id') if exists $self->controller->data->[0]->{raw_data}->{globalprojectnumber};
+ $self->add_columns($self->_ar_column, 'notes') if exists $self->controller->data->[0]->{raw_data}->{notes};
+
+ # Todo: access via ->[1] ok? Better: search first item column and use this
+ $self->add_info_columns($self->_transaction_column, { header => $::locale->text('Chart'), method => 'accno' });
+ $self->add_columns($self->_transaction_column, 'amount');
+
+ $self->add_info_columns($self->_transaction_column, { header => $::locale->text('Project Number'), method => 'projectnumber' }) if $self->controller->data->[1]->{info_data}->{projectnumber};
+
+ # $self->add_columns($self->_transaction_column,
+ # map { "${_}_id" } grep { exists $self->controller->data->[1]->{raw_data}->{$_} } qw(project price_factor pricegroup));
+ # $self->add_columns($self->_transaction_column,
+ # map { "${_}_id" } grep { exists $self->controller->data->[2]->{raw_data}->{$_} } qw(project price_factor pricegroup));
+ # $self->add_columns($self->_transaction_column, 'project_id') if exists $self->controller->data->[1]->{raw_data}->{projectnumber};
+ # $self->add_columns($self->_transaction_column, 'taxkey') if exists $self->controller->data->[1]->{raw_data}->{taxkey};
+
+ # If invoice has errors, add error for acc_trans items
+ # If acc_trans item has an error, add an error to the invoice item
+ my $ar_entry;
+ foreach my $entry (@{ $self->controller->data }) {
+ # Search first order
+ if ($entry->{raw_data}->{datatype} eq $self->_ar_column) {
+ $ar_entry = $entry;
+ } elsif ( defined $ar_entry
+ && $entry->{raw_data}->{datatype} eq $self->_transaction_column
+ && scalar @{ $ar_entry->{errors} } > 0 ) {
+ push @{ $entry->{errors} }, $::locale->text('Error: invalid ar row for this transaction');
+ } elsif ( defined $ar_entry
+ && $entry->{raw_data}->{datatype} eq $self->_transaction_column
+ && scalar @{ $entry->{errors} } > 0 ) {
+ push @{ $ar_entry->{errors} }, $::locale->text('Error: invalid acc transactions for this ar row');
+ }
+ }
+}
+
+sub handle_invoice {
+
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ $object->transactions( [] ); # initialise transactions for ar object so methods work on unsaved transactions
+
+ my $vc_obj;
+ if (any { $entry->{raw_data}->{$_} } qw(customer customernumber customer_id)) {
+ $self->check_vc($entry, 'customer_id');
+ # check_vc only sets customer_id, but we need vc_obj later for customer defaults
+ $vc_obj = SL::DB::Customer->new(id => $object->customer_id)->load if $object->customer_id;
+ } elsif (any { $entry->{raw_data}->{$_} } qw(vendor vendornumber vendor_id)) {
+ $self->check_vc($entry, 'vendor_id');
+ $vc_obj = SL::DB::Vendor->new(id => $object->vendor_id)->load if $object->vendor_id;
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor missing');
+ }
+
+ # check for duplicate invnumbers already in database
+ if ( SL::DB::Manager::Invoice->get_all_count( where => [ invnumber => $object->invnumber ] ) ) {
+ push @{ $entry->{errors} }, $::locale->text('Error: invnumber already exists');
+ }
+
+ $self->check_archart($entry); # checks for receivable account
+ # $self->check_amounts($entry); # checks and sets amount and netamount, use verify_amount and verify_netamount instead
+ $self->check_payment($entry); # currency default from customer used below
+ $self->check_department($entry);
+ $self->check_taxincluded($entry);
+ $self->check_project($entry, global => 1);
+ $self->check_taxzone($entry); # taxzone default from customer used below
+ $self->check_currency($entry); # currency default from customer used below
+ $self->handle_salesman($entry);
+ $self->handle_employee($entry);
+
+ if ($vc_obj ) {
+ # copy defaults from customer if not specified in import file
+ foreach (qw(payment_id language_id taxzone_id currency_id)) {
+ $object->$_($vc_obj->$_) unless $object->$_;
+ }
+ }
+}
+
+sub check_taxkey {
+ my ($self, $entry, $chart) = @_;
+
+ die "check_taxkey needs chart object as an argument" unless ref($chart) eq 'SL::DB::Chart';
+ # problem: taxkey is not unique in table tax, normally one of those entries is chosen directly from a dropdown
+ # so we check if the chart has an active taxkey, and if it matches the taxkey from the import, use the active taxkey
+ # if the chart doesn't have an active taxkey, use the first entry from Tax that matches the taxkey
+
+ my $object = $entry->{object};
+ unless ( defined $entry->{raw_data}->{taxkey} ) {
+ push @{ $entry->{errors} }, $::locale->text('Error: taxkey missing'); # don't just assume 0, force taxkey in import
+ return 0;
+ };
+
+ my $tax;
+
+ if ( $entry->{raw_data}->{taxkey} == $chart->get_active_taxkey->tax->taxkey ) {
+ $tax = $chart->get_active_taxkey->tax;
+ } else {
+ # assume there is only one tax entry with that taxkey, can't guess
+ $tax = SL::DB::Manager::Tax->get_first( where => [ taxkey => $entry->{raw_data}->{taxkey} ]);
+ };
+
+ unless ( $tax ) {
+ push @{ $entry->{errors} }, $::locale->text('Error: invalid taxkey');
+ return 0;
+ };
+
+ $object->taxkey($tax->taxkey);
+ $object->tax_id($tax->id);
+ return 1;
+};
+
+sub check_amounts {
+ my ($self, $entry) = @_;
+ # currently not used in favour of verify_amount and verify_netamount
+
+ my $object = $entry->{object};
+
+ unless ($entry->{raw_data}->{amount} && $entry->{raw_data}->{netamount}) {
+ push @{ $entry->{errors} }, $::locale->text('Error: need amount and netamount');
+ return 0;
+ };
+ unless ($entry->{raw_data}->{amount} * 1 && $entry->{raw_data}->{netamount} * 1) {
+ push @{ $entry->{errors} }, $::locale->text('Error: amount and netamount need to be numeric');
+ return 0;
+ };
+
+ $object->amount( $entry->{raw_data}->{amount} );
+ $object->netamount( $entry->{raw_data}->{netamount} );
+};
+
+sub handle_transaction {
+ my ($self, $entry) = @_;
+
+ # Prepare acc_trans data. amount is dealt with in add_transactions_to_ar
+
+ my $object = $entry->{object};
+
+ $self->check_project($entry, global => 0);
+ if ( $self->check_chart($entry) ) {
+ my $chart_obj = SL::DB::Manager::Chart->find_by(id => $object->chart_id);
+
+ unless ( $chart_obj->link =~ /AR_amount/ ) {
+ push @{ $entry->{errors} }, $::locale->text('Error: chart isn\'t an ar_amount chart');
+ return 0;
+ };
+
+ if ( $self->check_taxkey($entry, $chart_obj) ) {
+ # do nothing, taxkey was assigned, just continue
+ } else {
+ # missing taxkey, don't do anything
+ return 0;
+ };
+ } else {
+ return 0;
+ };
+
+ # check whether taxkey and automatic taxkey match
+ # die sprintf("taxkeys don't match: %s not equal default taxkey for chart %s: %s", $object->taxkey, $chart_obj->accno, $active_tax_for_chart->tax->taxkey) unless $object->taxkey == $active_tax_for_chart->tax->taxkey;
+
+ die "no taxkey for transaction object" unless $object->taxkey or $object->taxkey == 0;
+
+}
+
+sub check_chart {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ if (any { $entry->{raw_data}->{$_} } qw(accno chart_id)) {
+
+ # Check whether or not chart ID is valid.
+ if ($object->chart_id && !$self->charts_by->{id}->{ $object->chart_id }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: invalid chart_id');
+ return 0;
+ }
+
+ # Map number to ID if given.
+ if (!$object->chart_id && $entry->{raw_data}->{accno}) {
+ my $chart = $self->charts_by->{accno}->{ $entry->{raw_data}->{accno} };
+ if (!$chart) {
+ push @{ $entry->{errors} }, $::locale->text('Error: invalid chart (accno)');
+ return 0;
+ }
+
+ $object->chart_id($chart->id);
+ }
+
+ # Map description to ID if given.
+ if (!$object->chart_id && $entry->{raw_data}->{description}) {
+ my $chart = $self->charts_by->{description}->{ $entry->{raw_data}->{description} };
+ if (!$chart) {
+ push @{ $entry->{errors} }, $::locale->text('Error: invalid chart');
+ return 0;
+ }
+
+ $object->chart_id($chart->id);
+ }
+
+ if ($object->chart_id) {
+ # add account number to preview
+ $entry->{info_data}->{accno} = $self->charts_by->{id}->{ $object->chart_id }->accno;
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: chart not found');
+ return 0;
+ }
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: chart missing');
+ return 0;
+ }
+
+ return 1;
+}
+
+sub check_archart {
+ my ($self, $entry) = @_;
+
+ my $chart;
+
+ if ( $entry->{raw_data}->{archart} ) {
+ my $archart = $entry->{raw_data}->{archart};
+ $chart = SL::DB::Manager::Chart->find_by(accno => $archart);
+ unless ($chart) {
+ push @{ $entry->{errors} }, $::locale->text("Error: can't find ar chart with accno #1", $archart);
+ return 0;
+ };
+ } elsif ( $::instance_conf->get_ar_chart_id ) {
+ $chart = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_chart_id);
+ } else {
+ push @{ $entry->{errors} }, $::locale->text("Error: neither archart passed, no default receivables chart configured");
+ return 0;
+ };
+
+ unless ($chart->link eq 'AR') {
+ push @{ $entry->{errors} }, $::locale->text('Error: archart isn\'t an AR chart');
+ return 0;
+ };
+
+ $entry->{info_data}->{archart} = $chart->accno;
+ $entry->{object}->{archart} = $chart;
+ return 1;
+};
+
+sub check_taxincluded {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ if ( $entry->{raw_data}->{taxincluded} ) {
+ if ( $entry->{raw_data}->{taxincluded} eq 'f' or $entry->{raw_data}->{taxincluded} eq '0' ) {
+ $object->taxincluded('0');
+ } elsif ( $entry->{raw_data}->{taxincluded} eq 't' or $entry->{raw_data}->{taxincluded} eq '1' ) {
+ $object->taxincluded('1');
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: taxincluded has to be t or f');
+ return 0;
+ };
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: taxincluded wasn\'t set');
+ return 0;
+ };
+ return 1;
+};
+
+sub check_verify_amounts {
+ my ($self) = @_;
+
+ # If amounts are given, show calculated amounts as info and given amounts (verify_xxx).
+ # And throw an error if the differences are too big.
+ my @to_verify = ( { column => 'amount',
+ raw_column => 'verify_amount',
+ info_header => 'Calc. Amount',
+ info_method => 'calc_amount',
+ err_msg => $::locale->text('Amounts differ too much'),
+ },
+ { column => 'netamount',
+ raw_column => 'verify_netamount',
+ info_header => 'Calc. Net amount',
+ info_method => 'calc_netamount',
+ err_msg => $::locale->text('Net amounts differ too much'),
+ } );
+
+ foreach my $tv (@to_verify) {
+ if (exists $self->controller->data->[0]->{raw_data}->{ $tv->{raw_column} }) {
+ $self->add_raw_data_columns($self->_ar_column, $tv->{raw_column});
+ $self->add_info_columns($self->_ar_column,
+ { header => $::locale->text($tv->{info_header}), method => $tv->{info_method} });
+ }
+
+ # check differences
+ foreach my $entry (@{ $self->controller->data }) {
+ if ( @{ $entry->{errors} } ) {
+ push @{ $entry->{errors} }, $::locale->text($tv->{err_msg});
+ return 0;
+ };
+
+ if ($entry->{raw_data}->{datatype} eq $self->_ar_column) {
+ next if !$entry->{raw_data}->{ $tv->{raw_column} };
+ my $parsed_value = $::form->parse_amount(\%::myconfig, $entry->{raw_data}->{ $tv->{raw_column} });
+ # round $abs_diff, otherwise it might trigger for 0.020000000000021
+ my $abs_diff = $::form->round_amount(abs($entry->{object}->${ \$tv->{column} } - $parsed_value),2);
+ if ( $abs_diff > $self->settings->{'max_amount_diff'}) {
+ push @{ $entry->{errors} }, $::locale->text($tv->{err_msg});
+ }
+ }
+ }
+ }
+};
+
+sub add_transactions_to_ar {
+ my ($self) = @_;
+
+ # go through all verified ar and acc_trans rows in import, adding acc_trans objects to ar objects
+
+ my $ar_entry; # the current ar row
+
+ foreach my $entry (@{ $self->controller->data }) {
+ # when we reach an ar_column for the first time, don't do anything, just store in $ar_entry
+ # when we reach an ar_column for the second time, save it
+ if ($entry->{raw_data}->{datatype} eq $self->_ar_column) {
+ if ( $ar_entry && $ar_entry->{object} ) { # won't trigger the first time, finishes the last object
+ if ( $ar_entry->{object}->{archart} && $ar_entry->{object}->{archart}->isa('SL::DB::Chart') ) {
+ $ar_entry->{object}->recalculate_amounts; # determine and set amount and netamount for ar
+ $ar_entry->{object}->create_ar_row(chart => $ar_entry->{object}->{archart});
+ $ar_entry->{info_data}->{amount} = $ar_entry->{object}->amount;
+ $ar_entry->{info_data}->{netamount} = $ar_entry->{object}->netamount;
+ } else {
+ push @{ $entry->{errors} }, $::locale->text("ar_chart isn't a valid chart");
+ };
+ };
+ $ar_entry = $entry; # remember as last ar_entry
+
+ } elsif ( defined $ar_entry && $entry->{raw_data}->{datatype} eq $self->_transaction_column ) {
+ push @{ $entry->{errors} }, $::locale->text('no tax_id in acc_trans') unless $entry->{object}->tax_id || $entry->{object}->tax_id == 0;
+ next if @{ $entry->{errors} };
+
+ my $acc_trans_objects = $ar_entry->{object}->add_ar_amount_row(
+ amount => $entry->{object}->amount,
+ chart => SL::DB::Manager::Chart->find_by(id => $entry->{object}->chart_id), # add_ar_amount takes chart obj. as argument
+ tax_id => $entry->{object}->tax_id,
+ project_id => $entry->{object}->project_id,
+ debug => 0,
+ );
+
+ } else {
+ die "This should never happen\n";
+ };
+ }
+
+ # finish the final object
+ if ( $ar_entry->{object} ) {
+ if ( $ar_entry->{object}->{archart} && $ar_entry->{object}->{archart}->isa('SL::DB::Chart') ) {
+ $ar_entry->{object}->recalculate_amounts;
+ $ar_entry->{info_data}->{amount} = $ar_entry->{object}->amount;
+ $ar_entry->{info_data}->{netamount} = $ar_entry->{object}->netamount;
+
+ $ar_entry->{object}->create_ar_row(chart => $ar_entry->{object}->{archart});
+ } else {
+ push @{ $ar_entry->{errors} }, $::locale->text("The receivables chart isn't a valid chart.");
+ return 0;
+ };
+ } else {
+ die "There was no final ar_entry object";
+ };
+}
+
+sub save_objects {
+ my ($self, %params) = @_;
+
+ # save all the Invoice objects
+ my $objects_to_save;
+ foreach my $entry (@{ $self->controller->data }) {
+ # only push the invoice objects that don't have an error
+ next if $entry->{raw_data}->{datatype} ne $self->_ar_column;
+ next if @{ $entry->{errors} };
+
+ die unless $entry->{object}->validate_acc_trans;
+
+ push @{ $objects_to_save }, $entry;
+ }
+
+ $self->SUPER::save_objects(data => $objects_to_save);
+}
+
+sub _ar_column {
+ $_[0]->settings->{'ar_column'}
+}
+
+sub _transaction_column {
+ $_[0]->settings->{'transaction_column'}
+}
+
+1;
'AR Aging' => 'Offene Forderungen',
'AR Transaction' => 'Debitorenbuchung',
'AR Transaction (abbreviation)' => 'D',
+ 'AR Transaction/AccTrans Item row names' => 'Namen der Rechnungs/Buchungszeilen',
'AR Transactions' => 'Debitorenbuchungen',
'AR transactions changeable' => 'Änderbarkeit von Debitorenbuchungen',
'ASSETS' => 'AKTIVA',
'Amount payable' => 'Noch zu bezahlender Betrag',
'Amount payable less discount' => 'Noch zu bezahlender Betrag abzüglich Skonto',
'An error occured. Letter could not be deleted.' => 'Es ist ein Fehler aufgetreten. Der Brief konnte nicht gelöscht werden.',
+ 'Amounts differ too much' => 'Beträge weichen zu sehr voneinander ab.',
'An exception occurred during execution.' => 'Während der Ausführung trat eine Ausnahme auf.',
'An invalid character was used (invalid characters: #1).' => 'Ein ungültiges Zeichen wurde benutzt (ungültige Zeichen: #1).',
'An invalid character was used (valid characters: #1).' => 'Ein ungültiges Zeichen wurde benutzt (gültige Zeichen: #1).',
'CSS style for pictures' => 'CSS Style für Bilder',
'CSV' => 'CSV',
'CSV export -- options' => 'CSV-Export -- Optionen',
+ 'CSV import: ar transactions' => 'CSV Import: Debitorenbuchungen',
'CSV import: bank transactions' => 'CSV Import: Bankbewegungen',
'CSV import: contacts' => 'CSV-Import: Ansprechpersonen',
'CSV import: customers and vendors' => 'CSV-Import: Kunden und Lieferanten',
'Charge number' => 'Chargennummer',
'Charset' => 'Zeichensatz',
'Chart' => 'Buchungskonto',
+ 'Chart (database ID)' => 'Konto (Datenbank ID)',
'Chart Type' => 'Kontentyp',
'Chart balance' => 'Kontensaldo',
'Chart of Accounts' => 'Kontenübersicht',
'Error: Customer/vendor not found' => 'Fehler: Kunde/Lieferant nicht gefunden',
'Error: Found local bank account number but local bank code doesn\'t match' => 'Fehler: Kontonummer wurde gefunden aber gespeicherte Bankleitzahl stimmt nicht überein',
'Error: Gender (cp_gender) missing or invalid' => 'Fehler: Geschlecht (cp_gender) fehlt oder ungültig',
+ 'Error: Invalid ar transaction for this order item' => 'Fehler: ungültige Debitorenrechnung für diese Buchungszeile',
'Error: Invalid bin' => 'Fehler: Ungültiger Lagerplatz',
'Error: Invalid business' => 'Fehler: Kunden-/Lieferantentyp ungültig',
+ 'Error: Invalid chart' => 'Fehler: ungültiges Konto',
'Error: Invalid contact' => 'Fehler: Ansprechperson ungültig',
'Error: Invalid currency' => 'Fehler: ungültige Währung',
'Error: Invalid delivery terms' => 'Fehler: Lieferbedingungen ungültig',
'Error: Transfer would result in a negative target quantity.' => 'Fehler: Lagerbewegung würde zu einer negativen Zielmenge führen.',
'Error: Unit missing or invalid' => 'Fehler: Einheit fehlt oder ungültig',
'Error: Warehouse not found' => 'Fehler: Lager nicht gefunden',
+ 'Error: amount and netamount need to be numeric' => 'Fehler: amount und netamount müssen numerisch sein',
+ 'Error: ar transaction doesn\'t validate' => 'Fehler: die Debitorenbuchung ist nicht korrekt',
+ 'Error: archart isn\'t an AR chart' => 'Fehler: das Forderungskonto ist nicht als Forderungskonto definiert (link = AR)',
+ 'Error: can\'t find ar chart with accno #1' => 'Fehler: kein Forderungskonto mit Kontonummer #1',
+ 'Error: chart isn\'t an ar_amount chart' => 'Fehler: Konto ist kein Erlöskonto',
+ 'Error: chart missing' => 'Fehler: Konto fehlt',
+ 'Error: chart not found' => 'Fehler: Konto nicht gefunden',
+ 'Error: invalid acc transactions for this ar row' => 'Fehler: ungültige Buchungszeilen für diese Rechnungzeile',
+ 'Error: invalid ar row for this transaction' => 'Ungültige Rechnungszeile für diese Buchungszeile',
+ 'Error: invalid chart' => 'Fehler: ungültiges Konto',
+ 'Error: invalid chart (accno)' => 'Fehler: ungültiges Konto (accno)',
+ 'Error: invalid chart_id' => 'Fehler: ungültige Konto ID (chart_id)',
+ 'Error: invalid taxkey' => 'Fehler: ungültiger Steuerschlüssel',
+ 'Error: invnumber already exists' => 'Fehler: Rechnungsnummer existiert schon',
'Error: local bank account id doesn\'t match local bank account number' => 'Fehler: Bankkonto-ID stimmt nicht mit Kontonummer überein',
'Error: local bank account id doesn\'t match local bank code' => 'Fehler: Bankkonto-ID stimmt nicht mit BLZ überein',
+ 'Error: need amount and netamount' => 'Fehler: amount und netamount werden benötigt',
+ 'Error: neither archart passed, no default receivables chart configured' => 'Fehler: Forderungskonto (archart) fehlt, kein Standardforderungskonto definiert',
+ 'Error: taxincluded has to be t or f' => 'Fehler: Steuer im Preis inbegriffen muß t oder f sein',
+ 'Error: taxincluded wasn\'t set' => 'Fehler: Steuer im Preis inbegriffen nicht gesetzt (taxincluded)',
+ 'Error: taxkey missing' => 'Fehler: Steuerschlüssel fehlt',
'Error: this feature requires that articles with a time-based unit (e.g. \'h\' or \'min\') exist.' => 'Fehler: dieses Feature setzt voraus, dass Artikel mit einer Zeit-basierenden Einheit (z.B. "Std") existieren.',
'Error: unknown local bank account' => 'Fehler: unbekannte Kontnummer',
'Error: unknown local bank account id' => 'Fehler: unbekannte Bankkonto-ID',
'Net Value in delivery orders' => 'Netto mit Lieferschein',
'Net amount' => 'Nettobetrag',
'Net amount (for verification)' => 'Nettobetrag (zur Überprüfung)',
+ 'Net amounts differ too much' => 'Nettobeträge weichen zu sehr ab.',
'Net value in Order' => 'Netto Auftrag',
'Net value transferred in / out' => 'Netto ein- /ausgelagert',
'Net value without delivery orders' => 'Netto ohne Lieferschein',
'Receipts' => 'Zahlungseingänge',
'Receivable account' => 'Forderungskonto',
'Receivables' => 'Forderungen',
- 'Recipients' => 'EmpfängerInnen',
+ 'Receivables account' => 'Forderungskonto',
+ 'Receivables account (account number)' => 'Forderungskonto (Kontonummer)',
'Reconcile' => 'Abgleichen',
'Reconciliation' => 'Kontenabgleich',
'Reconciliation with bank' => 'Kontenabgleich mit Bank',
'The client has been deleted.' => 'Der Mandant wurde gelöscht.',
'The client has been saved.' => 'Der Mandant wurde gespeichert.',
'The clipboard does not contain anything that can be pasted here.' => 'Die Zwischenablage enthält momentan keine Objekte, die hier eingefügt werden können.',
+ 'The column "datatype" must be present and must be at the same position / column in each data set. The values must be the row names (see settings) for invoice and transaction data respectively.' => 'Die Spalte "datatype" muss vorhanden sein und sie muss in jedem Datensatz an der gleichen Stelle / Spalte sein. Die Werte in dieser Spalte müssen die Namen der Rechnungs- und Buchungszeilen (siehe Einstellungen) sein.',
'The column "datatype" must be present and must be at the same position / column in each data set. The values must be the row names (see settings) for order and item data respectively.' => 'Die Spalte "datatype" muss vorhanden sein und sie muss in jedem Datensatz an der gleichen Stelle / Spalte sein. Die Werte in dieser Spalte müssen die Namen der Auftrag-/Positions-Zeilen (siehe Einstellungen) sein.',
'The column "make_X" can contain either a vendor\'s database ID, a vendor number or a vendor\'s name.' => 'Die Spalte "make_X" can entweder die Datenbank-ID des Lieferanten, eine Lieferantennummer oder einen Lieferantennamen enthalten.',
'The column triplets can occur multiple times with different numbers "X" each time (e.g. "make_1", "model_1", "lastcost_1", "make_2", "model_2", "lastcost_2", "make_3", "model_3", "lastcost_3" etc).' => 'Die Spalten-Dreiergruppen können mehrfach auftreten, sofern sie unterschiedliche Nummern "X" verwenden (z.B. "make_1", "model_1", "lastcost_1", "make_2", "model_2", "lastcost_2", "make_3", "model_3", "lastcost_3" etc).',
'The project type has been deleted.' => 'Der Projekttyp wurde gelöscht.',
'The project type has been saved.' => 'Der Projekttyp wurde gespeichert.',
'The project type is in use and cannot be deleted.' => 'Der Projekttyp wird verwendet und kann nicht gelöscht werden.',
+ 'The receivables chart isn\'t a valid chart.' => 'Das Forderungskonto ist kein gültiges Konto',
'The recipient, subject or body is missing.' => 'Der Empfäger, der Betreff oder der Text ist leer.',
'The required information consists of the IBAN and the BIC.' => 'Die benötigten Informationen bestehen aus der IBAN und der BIC. Zusätzlich wird die SEPA-Kreditoren-Identifikation aus der Mandantenkonfiguration benötigt.',
'The required information consists of the IBAN, the BIC, the mandator ID and the mandate\'s date of signature.' => 'Die benötigten Informationen bestehen aus IBAN, BIC, Mandanten-ID und dem Unterschriftsdatum des Mandates. Zusätzlich wird die SEPA-Kreditoren-Identifikation aus der Mandantenkonfiguration benötigt.',
'and' => 'und',
'ap_aging_list' => 'liste_offene_verbindlichkeiten',
'ar_aging_list' => 'liste_offene_forderungen',
+ 'ar_chart isn\'t a valid chart' => 'Das Forderungskonto ist kein gültiges Konto.',
'as at' => 'zum Stand',
'assembly' => 'Erzeugnis',
'assembly_list' => 'erzeugnisliste',
--- /dev/null
+use Test::More tests => 70;
+
+use strict;
+
+use lib 't';
+
+use Carp;
+use Data::Dumper;
+use Support::TestSetup;
+use Test::Exception;
+
+use List::MoreUtils qw(pairwise);
+use SL::Controller::CsvImport;
+
+my $DEBUG = 0;
+
+use_ok 'SL::Controller::CsvImport::ARTransaction';
+
+use SL::DB::Buchungsgruppe;
+use SL::DB::Currency;
+use SL::DB::Customer;
+use SL::DB::Employee;
+use SL::DB::Invoice;
+use SL::DB::TaxZone;
+use SL::DB::Chart;
+use SL::DB::AccTransaction;
+
+my ($customer, $currency_id, $employee, $taxzone, $project, $department);
+
+sub reset_state {
+ # Create test data
+ my %params = @_;
+
+ $params{$_} ||= {} for qw(buchungsgruppe customer tax);
+
+ clear_up();
+ $employee = SL::DB::Manager::Employee->current || croak "No employee";
+ $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
+ $currency_id = $::instance_conf->get_currency_id;
+
+ $customer = SL::DB::Customer->new(
+ name => 'Test Customer',
+ currency_id => $currency_id,
+ taxzone_id => $taxzone->id,
+ %{ $params{customer} }
+ )->save;
+
+ $project = SL::DB::Project->new(
+ projectnumber => 'P1',
+ description => 'Project X',
+ project_type => SL::DB::Manager::ProjectType->find_by(description => 'Standard'),
+ project_status => SL::DB::Manager::ProjectStatus->find_by(name => 'running'),
+ )->save;
+
+ $department = SL::DB::Department->new(
+ description => 'Department 1',
+ )->save;
+}
+
+Support::TestSetup::login();
+
+reset_state(customer => {id => 960, customernumber => 2});
+
+#####
+sub test_import {
+ my $file = shift;
+
+ my $controller = SL::Controller::CsvImport->new();
+
+ my $csv_artransactions_import = SL::Controller::CsvImport::ARTransaction->new(
+ settings => {'ar_column' => 'Rechnung',
+ 'transaction_column' => 'AccTransaction',
+ 'max_amount_diff' => 0.02
+ },
+ controller => $controller,
+ file => $file,
+ );
+
+ # $csv_artransactions_import->init_vc_by;
+ $csv_artransactions_import->test_run(0);
+ $csv_artransactions_import->csv(SL::Helper::Csv->new(file => $csv_artransactions_import->file,
+ profile => $csv_artransactions_import->profile,
+ encoding => 'utf-8',
+ ignore_unknown_columns => 1,
+ strict_profile => 1,
+ case_insensitive_header => 1,
+ sep_char => ',',
+ quote_char => '"',
+ ignore_unknown_columns => 1,
+ ));
+
+ $csv_artransactions_import->csv->parse;
+
+ $csv_artransactions_import->controller->errors([ $csv_artransactions_import->csv->errors ]) if $csv_artransactions_import->csv->errors;
+
+ return if ( !$csv_artransactions_import->csv->header || $csv_artransactions_import->csv->errors );
+
+ my $headers;
+ my $i = 0;
+ foreach my $header (@{ $csv_artransactions_import->csv->header }) {
+
+ my $profile = $csv_artransactions_import->csv->profile->[$i]->{profile};
+ my $row_ident = $csv_artransactions_import->csv->profile->[$i]->{row_ident};
+
+ my $h = { headers => [ grep { $profile->{$_} } @{ $header } ] };
+ $h->{methods} = [ map { $profile->{$_} } @{ $h->{headers} } ];
+ $h->{used} = { map { ($_ => 1) } @{ $h->{headers} } };
+
+ $headers->{$row_ident} = $h;
+ $i++;
+ }
+
+ $csv_artransactions_import->controller->headers($headers);
+
+ my $raw_data_headers;
+ my $info_headers;
+ foreach my $p (@{ $csv_artransactions_import->csv->profile }) {
+ my $ident = $p->{row_ident};
+ $raw_data_headers->{$ident} = { used => { }, headers => [ ] };
+ $info_headers->{$ident} = { used => { }, headers => [ ] };
+ }
+ $csv_artransactions_import->controller->raw_data_headers($raw_data_headers);
+ $csv_artransactions_import->controller->info_headers($info_headers);
+
+ my $objects = $csv_artransactions_import->csv->get_objects;
+ my @raw_data = @{ $csv_artransactions_import->csv->get_data };
+
+ $csv_artransactions_import->controller->data([ pairwise { no warnings 'once'; { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]);
+ $csv_artransactions_import->check_objects;
+
+ # don't try and save objects that have errors
+ $csv_artransactions_import->save_objects unless scalar @{$csv_artransactions_import->controller->data->[0]->{errors}};
+
+ return $csv_artransactions_import->controller->data;
+}
+
+##### manually create an ar transaction from scratch, testing the methods
+$::myconfig{numberformat} = '1000.00';
+my $old_locale = $::locale;
+# set locale to en so we can match errors
+$::locale = Locale->new('en');
+
+my $amount = 10;
+
+my $ar = SL::DB::Invoice->new(
+ invoice => 0,
+ invnumber => 'manual invoice',
+ taxzone_id => $taxzone->id,
+ currency_id => $currency_id,
+ taxincluded => 'f',
+ customer_id => $customer->id,
+ transdate => DateTime->today,
+ employee_id => SL::DB::Manager::Employee->current->id,
+ transactions => [],
+);
+
+my $tax3 = SL::DB::Manager::Tax->find_by(rate => 0.19, taxkey => 3) || die "can't find tax with taxkey 3";
+my $income_chart = SL::DB::Manager::Chart->find_by(accno => '8400') || die "can't find income chart";
+
+$ar->add_ar_amount_row(
+ amount => $amount,
+ chart => $income_chart,
+ tax_id => $tax3->id,
+);
+
+$ar->recalculate_amounts; # set amount and netamount from transactions
+is $ar->amount, '10', 'amount of manual invoice is 10';
+is $ar->netamount, '8.4', 'netamount of manual invoice is 10';
+
+$ar->create_ar_row( chart => SL::DB::Manager::Chart->find_by(accno => '1400', link => 'AR') );
+my $result = $ar->validate_acc_trans(debug => 0);
+is $result, 1, 'manual $ar validates';
+
+$ar->save;
+is ${ $ar->transactions }[0]->chart->accno, '8400', 'assigned income chart after save ok';
+is ${ $ar->transactions }[2]->chart->accno, '1400', 'assigned receivable chart after save ok';
+is scalar @{$ar->transactions}, 3, 'manual invoice has 3 acc_trans entries';
+
+$ar->pay_invoice( chart_id => SL::DB::Manager::Chart->find_by(accno => '1200')->id, # bank
+ amount => $ar->open_amount,
+ transdate => DateTime->now->to_kivitendo,
+ payment_type => 'without_skonto', # default if not specified
+ );
+$result = $ar->validate_acc_trans(debug => 0);
+is $result, 1, 'manual invoice validates after payment';
+
+reset_state(customer => {id => 960, customernumber => 2});
+
+my ($entries, $entry, $file);
+
+# starting test of csv imports
+# to debug errors in certain tests, run after test_import:
+# die Dumper($entry->{errors});
+##### basic test
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice 1",f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+
+is $entry->{object}->invnumber, 'invoice 1', 'simple invnumber ok (customer)';
+is $entry->{object}->customer_id, '960', 'simple customer_id ok (customer)';
+is scalar @{$entry->{object}->transactions}, 3, 'invoice 1 has 3 acc_trans entries';
+is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), 159.48, 'invoice 1 ar amount is 159.48';
+is $entry->{object}->direct_debit, '0', 'no direct debit';
+is $entry->{object}->taxincluded, '0', 'taxincluded is false';
+is $entry->{object}->amount, '189.78', 'ar amount tax not included is 189.78';
+is $entry->{object}->netamount, '159.48', 'ar netamount tax not included is 159.48';
+
+##### test for duplicate invnumber
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice 1",f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{errors}->[0], 'Error: invnumber already exists', 'detects verify_amount differences';
+
+##### test for no invnumber given
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{object}->invnumber =~ /^\d+$/, 1, 'invnumber assigned automatically';
+
+##### basic test without amounts in Rechnung, only specified in AccTransaction
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice 1 no amounts",f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+
+is $entry->{object}->invnumber, 'invoice 1 no amounts', 'simple invnumber ok (customer)';
+is $entry->{object}->customer_id, '960', 'simple customer_id ok (customer)';
+is scalar @{$entry->{object}->transactions}, 3, 'invoice 1 has 3 acc_trans entries';
+is $::form->round_amount($entry->{object}->amount, 2), '189.78', 'not taxincluded ar amount';
+is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), '159.48', 'not taxincluded acc_trans netamount';
+is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), 159.48, 'invoice 1 ar amount is 159.48';
+
+##### basic test: credit_note
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"credit note",f,1400
+"AccTransaction",8400,-159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+
+is $entry->{object}->invnumber, 'credit note', 'simple credit note ok';
+is scalar @{$entry->{object}->transactions}, 3, 'credit note has 3 acc_trans entries';
+is $::form->round_amount($entry->{object}->amount, 2), '-189.78', 'taxincluded ar amount';
+is $::form->round_amount($entry->{object}->netamount, 2), '-159.48', 'taxincluded ar net amount';
+is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), -159.48, 'credit note ar amount is -159.48';
+is $entry->{object}->amount, '-189.78', 'credit note amount tax not included is 189.78';
+is $entry->{object}->netamount, '-159.48', 'credit note netamount tax not included is 159.48';
+
+#### verify_amount differs: max_amount_diff = 0.02, 189.80 is ok, 189.81 is not
+$file = \<<EOL;
+datatype,customer_id,verify_amount,verify_netamount,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,189.81,159.48,4,1,"invoice amounts differing",f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+is $entry->{errors}->[0], 'Amounts differ too much', 'detects verify_amount differences';
+
+##### direct debit
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,direct_debit,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice with direct debit",f,t,1400
+"AccTransaction",8400,159.48,3
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{object}->direct_debit, '1', 'direct debit';
+
+#### tax included
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice 1 tax included no amounts",t,1400
+"AccTransaction",8400,189.78,3
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans(debug => 0);
+is $entry->{object}->taxincluded, '1', 'taxincluded is true';
+is $::form->round_amount($entry->{object}->amount, 2), '189.78', 'taxincluded ar amount';
+is $::form->round_amount($entry->{object}->netamount, 2), '159.48', 'taxincluded ar net amount';
+is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), '159.48', 'taxincluded acc_trans netamount';
+
+#### multiple tax included
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice multiple tax included",t,1400
+"AccTransaction",8400,94.89,3
+"AccTransaction",8400,94.89,3
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $::form->round_amount($entry->{object}->amount, 2), '189.78', 'taxincluded ar amount';
+is $::form->round_amount($entry->{object}->netamount, 2), '159.48', 'taxincluded ar netamount';
+is $::form->round_amount($entry->{object}->transactions->[0]->amount, 2), '79.74', 'taxincluded amount';
+is $::form->round_amount($entry->{object}->transactions->[1]->amount, 2), '15.15', 'taxincluded tax';
+
+# different receivables chart
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice mit archart 1448",f,1448
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{object}->transactions->[2]->chart->accno, '1448', 'archart set to 1448';
+
+# missing customer
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",4,1,"invoice missing customer",f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+is $entry->{errors}->[0], 'Error: Customer/vendor missing', 'detects missing customer or vendor';
+
+
+##### customer by name
+$file = \<<EOL;
+datatype,customer,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung","Test Customer",4,1,"invoice customer name",f,1400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{object}->customer->name, "Test Customer", 'detects customer by name';
+
+##### detect missing chart
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,archart
+datatype,amount,taxkey
+"Rechnung",4,1,"invoice missing chart","Test Customer",1400
+"AccTransaction",4,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[1];
+is $entry->{errors}->[0], 'Error: chart missing', 'detects missing chart (chart_id or accno)';
+
+##### detect illegal chart by accno
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,archart
+datatype,accno,amount,taxkey
+"Rechnung",4,1,"invoice illegal chart accno","Test Customer",1400
+"AccTransaction",9999,4,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[1];
+is $entry->{errors}->[0], 'Error: invalid chart (accno)', 'detects invalid chart (chart_id or accno)';
+
+# ##### detect illegal archart
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",4,1,"invoice illegal archart","Test Customer",f,11400
+"AccTransaction",8400,159.48,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[0];
+is $entry->{errors}->[0], "Error: can't find ar chart with accno 11400", 'detects illegal receivables chart (archart)';
+
+##### detect chart by id
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
+datatype,amount,chart_id,taxkey
+"Rechnung",4,1,"invoice chart_id","Test Customer",f,1400
+"AccTransaction",159.48,184,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[1]; # acc_trans entry is at entry array pos 1
+$entries->[0]->{object}->validate_acc_trans;
+is $entry->{object}->chart->id, "184", 'detects chart by id';
+
+##### detect chart by accno
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
+datatype,amount,accno,taxkey
+"Rechnung",4,1,"invoice by chart accno","Test Customer",f,1400
+"AccTransaction",159.48,8400,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[1];
+$entries->[0]->{object}->validate_acc_trans;
+is $entry->{object}->chart->accno, "8400", 'detects chart by accno';
+
+##### detect chart isn't an ar_chart
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,taxincluded,archart
+datatype,amount,accno,taxkey
+"Rechnung",4,1,"invoice by chart accno","Test Customer",f,1400
+"AccTransaction",159.48,1400,3
+EOL
+$entries = test_import($file);
+$entry = $entries->[1];
+$entries->[0]->{object}->validate_acc_trans;
+is $entry->{errors}->[0], 'Error: chart isn\'t an ar_amount chart', 'detects valid chart that is not an ar_amount chart';
+
+# missing taxkey
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,archart
+datatype,amount,accno
+"Rechnung",4,1,"invoice missing taxkey chart accno","Test Customer",1400
+"AccTransaction",159.48,8400
+EOL
+$entries = test_import($file);
+$entry = $entries->[1];
+is $entry->{errors}->[0], 'Error: taxkey missing', 'detects missing taxkey (DATEV Steuerschlüssel)';
+
+# illegal taxkey
+$file = \<<EOL;
+datatype,taxzone_id,currency_id,invnumber,customer,archart
+datatype,amount,accno,taxkey
+"Rechnung",4,1,"invoice illegal taxkey","Test Customer",1400
+"AccTransaction",4,8400,123
+EOL
+$entries = test_import($file);
+$entry = $entries->[1];
+is $entry->{errors}->[0], 'Error: invalid taxkey', 'detects invalid taxkey (DATEV Steuerschlüssel)';
+
+# taxkey
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice by taxkey",1400
+"AccTransaction",8400,4,3
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[1];
+is $entry->{object}->taxkey, 3, 'detects taxkey';
+
+# acc_trans project
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,archart,taxincluded
+datatype,accno,amount,taxkey,projectnumber
+"Rechnung",960,4,1,"invoice with acc_trans project",1400,f
+"AccTransaction",8400,159.48,3,P1
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[1];
+# die Dumper($entries->[0]->{errors}) if scalar @{$entries->[0]->{errors}};
+is $entry->{object}->project->projectnumber, 'P1', 'detects acc_trans project';
+
+##### various tests
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart,transdate,duedate,globalprojectnumber,department
+datatype,accno,amount,taxkey,projectnumber
+"Rechnung",960,4,1,"invoice various",t,1400,21.04.2016,30.04.2016,P1,Department 1
+"AccTransaction",8400,119,3,P1
+"AccTransaction",8300,107,2,P1
+"AccTransaction",8200,100,0,P1
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{object}->duedate->to_kivitendo, '30.04.2016', 'duedate';
+is $entry->{object}->transdate->to_kivitendo, '21.04.2016', 'transdate';
+is $entry->{object}->globalproject->description, 'Project X', 'project';
+is $entry->{object}->department->description, 'Department 1', 'department';
+# 8300 is third entry after 8400 and tax for 8400
+is $::form->round_amount($entry->{object}->transactions->[2]->amount), '100', '8300 net amount: 100';
+is $::form->round_amount($entry->{object}->transactions->[2]->taxkey), '2', '8300 has taxkey 2';
+is $::form->round_amount($entry->{object}->transactions->[2]->project_id), $project->id, 'AccTrans project';
+
+##### ar amount test
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart,transdate,duedate,globalprojectnumber,department
+datatype,accno,amount,taxkey,projectnumber
+"Rechnung",960,4,1,"invoice various 1",t,1400,21.04.2016,30.04.2016,P1,Department 1
+"AccTransaction",8400,119,3,P1
+"AccTransaction",8300,107,2,P1
+"AccTransaction",8200,100,0,P1
+"Rechnung",960,4,1,"invoice various 2",t,1400,21.04.2016,30.04.2016,P1,Department 1
+"AccTransaction",8400,119,3,P1
+"AccTransaction",8300,107,2,P1
+"AccTransaction",8200,100,0,P1
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans;
+is $entry->{object}->duedate->to_kivitendo, '30.04.2016', 'duedate';
+is $entry->{info_data}->{amount}, '326', "First invoice amount displayed in info data";
+is $entries->[4]->{info_data}->{amount}, '326', "Second invoice amount displayed in info data";
+
+# multiple entries, taxincluded = f
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice 4 acc_trans",f,1400
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"Rechnung",960,4,1,"invoice 4 acc_trans 2",f,1400
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"Rechnung",960,4,1,"invoice 4 acc_trans 3",f,1400
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"Rechnung",960,4,1,"invoice 4 acc_trans 4",f,1448
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+"AccTransaction",8400,39.87,3
+EOL
+$entries = test_import($file);
+
+my $i = 0;
+foreach my $entry ( @$entries ) {
+ next unless $entry->{object}->isa('SL::DB::Invoice');
+ $i++;
+ is scalar @{$entry->{object}->transactions}, 9, "invoice $i: 'invoice 4 acc_trans' has 9 acc_trans entries";
+ $entry->{object}->validate_acc_trans;
+};
+
+##### missing acc_trans
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart,transdate,duedate,globalprojectnumber,department
+datatype,accno,amount,taxkey,projectnumber
+"Rechnung",960,4,1,"invoice acc_trans missing",t,1400,21.04.2016,30.04.2016,P1,Department 1
+"Rechnung",960,4,1,"invoice various a",t,1400,21.04.2016,30.04.2016,P1,Department 1
+"AccTransaction",8400,119,3,P1
+"AccTransaction",8300,107,2,P1
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+is $entry->{errors}->[0], "Error: ar transaction doesn't validate", 'detects invalid ar, maybe acc_trans entry missing';
+
+my $number_of_imported_invoices = SL::DB::Manager::Invoice->get_all_count;
+is $number_of_imported_invoices, 19, 'All invoices saved';
+
+#### taxkey differs from active_taxkey
+$file = \<<EOL;
+datatype,customer_id,taxzone_id,currency_id,invnumber,taxincluded,archart
+datatype,accno,amount,taxkey
+"Rechnung",960,4,1,"invoice 1 tax included no amounts",t,1400
+"AccTransaction",8400,189.78,2
+EOL
+
+$entries = test_import($file);
+$entry = $entries->[0];
+$entry->{object}->validate_acc_trans(debug => 0);
+
+clear_up(); # remove all data at end of tests
+# end of tests
+
+
+sub clear_up {
+ SL::DB::Manager::AccTransaction->delete_all(all => 1);
+ SL::DB::Manager::Invoice->delete_all (all => 1);
+ SL::DB::Manager::Customer->delete_all (all => 1);
+ SL::DB::Manager::Project->delete_all (all => 1);
+ SL::DB::Manager::Department->delete_all (all => 1);
+};
+
+
+1;
+
+#####
+# vim: ft=perl
+# set emacs to perl mode
+# Local Variables:
+# mode: perl
+# End: