X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAP.pm;h=caae2bb730a0f61797b6c525f3ee5e74c3ec4e23;hb=d1d70f4ad540f4d57d1b577b337b6db38074e223;hp=8d700640e532b86b80506c23286ff1af7c1e1383;hpb=aa63ebae1c0a758ec438c594acb328b1f0e5e001;p=kivitendo-erp.git diff --git a/SL/AP.pm b/SL/AP.pm index 8d700640e..caae2bb73 100644 --- a/SL/AP.pm +++ b/SL/AP.pm @@ -40,6 +40,7 @@ use SL::DBUtils; use SL::IO; use SL::MoreCommon; use SL::DB::Default; +use SL::DB::Draft; use SL::Util qw(trim); use SL::DB; use Data::Dumper; @@ -47,17 +48,19 @@ use Data::Dumper; use strict; sub post_transaction { - my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_; + my ($self, $myconfig, $form, $provided_dbh, %params) = @_; $main::lxdebug->enter_sub(); - my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form, $provided_dbh, $payments_only); + my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form, $provided_dbh, %params); $::lxdebug->leave_sub; return $rc; } sub _post_transaction { - my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_; + my ($self, $myconfig, $form, $provided_dbh, %params) = @_; + + my $payments_only = $params{payments_only}; my $dbh = $provided_dbh || SL::DB->client->dbh; my ($null, $taxrate, $amount); @@ -209,6 +212,8 @@ sub _post_transaction { $form->{payables} = $form->{invpaid}; } + my %already_cleared = %{ $params{already_cleared} // {} }; + # add paid transactions for my $i (1 .. $form->{paidaccounts}) { @@ -242,10 +247,18 @@ sub _post_transaction { $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1, 2); + + my $new_cleared = !$form->{"acc_trans_id_$i"} ? 'f' + : !$already_cleared{$form->{"acc_trans_id_$i"}} ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{amount} != $amount * -1 ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{accno} != $form->{"AP_paid_account_$i"} ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{cleared} ? 't' + : 'f'; + if ($form->{payables}) { $query = - qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) | . - qq|VALUES (?, ?, ?, ?, ?, | . + qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, cleared, taxkey, tax_id, chart_link) | . + qq|VALUES (?, ?, ?, ?, ?, ?, | . qq| (SELECT taxkey_id FROM chart WHERE id = ?),| . qq| (SELECT tax_id| . qq| FROM taxkeys| . @@ -254,7 +267,7 @@ sub _post_transaction { qq| ORDER BY startdate DESC LIMIT 1),| . qq| (SELECT c.link FROM chart c WHERE c.id = ?))|; @values = ($form->{id}, $form->{AP_chart_id}, $amount, - conv_date($form->{"datepaid_$i"}), $project_id, + conv_date($form->{"datepaid_$i"}), $project_id, $new_cleared, $form->{AP_chart_id}, $form->{AP_chart_id}, conv_date($form->{"datepaid_$i"}), $form->{AP_chart_id}); do_query($form, $dbh, $query, @values); @@ -264,8 +277,8 @@ sub _post_transaction { # add payment my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig)); $query = - qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, taxkey, tax_id, chart_link) | . - qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, | . + qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, cleared, taxkey, tax_id, chart_link) | . + qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, ?, | . qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | . qq| (SELECT tax_id| . qq| FROM taxkeys| . @@ -277,7 +290,7 @@ sub _post_transaction { qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|; @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"}, conv_date($form->{"datepaid_$i"}), $gldate, $form->{"source_$i"}, - $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"}, + $form->{"memo_$i"}, $project_id, $new_cleared, $form->{"AP_paid_account_$i"}, $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}), $form->{"AP_paid_account_$i"}); do_query($form, $dbh, $query, @values); @@ -361,19 +374,17 @@ sub _post_transaction { IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh); + if ($form->{draft_id}) { + SL::DB::Manager::Draft->delete_all(where => [ id => delete($form->{draft_id}) ]); + } + # safety check datev export if ($::instance_conf->get_datev_check_on_ap_transaction) { - my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef; - $transdate ||= DateTime->today; - my $datev = SL::DATEV->new( - exporttype => DATEV_ET_BUCHUNGEN, - format => DATEV_FORMAT_KNE, dbh => $dbh, trans_id => $form->{id}, ); - - $datev->export; + $datev->generate_datev_data; if ($datev->errors) { die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors; @@ -433,12 +444,37 @@ sub ap_transactions { my $where = ''; - unless ( $::auth->assert('show_ap_transactions', 1) ) { - $where .= " AND NOT invoice = 'f' "; # remove ap transactions from Sales -> Reports -> Invoices - }; - my @values; + # Permissions: + # - Always return invoices & AP transactions for projects the employee has "view invoices" permissions for, no matter what the other rules say. + # - Exclude AP transactions if no permissions for them exist. + # - Filter by employee if requested. + my (@permission_where, @permission_values); + + if ($::auth->assert('vendor_invoice_edit', 1)) { + if (!$::auth->assert('show_ap_transactions', 1)) { + push @permission_where, "NOT invoice = 'f'"; # remove ap transactions from Purchase -> Reports -> Invoices + } + + if ($form->{employee_id}) { + push @permission_where, "a.employee_id = ?"; + push @permission_values, conv_i($form->{employee_id}); + } + } + + if (@permission_where || !$::auth->assert('vendor_invoice_edit', 1)) { + my $permission_where_str = @permission_where ? "OR (" . join(" AND ", map { "($_)" } @permission_where) . ")" : ""; + $where .= qq| + AND ( (a.globalproject_id IN ( + SELECT epi.project_id + FROM employee_project_invoices epi + WHERE epi.employee_id = ?)) + $permission_where_str) + |; + push @values, SL::DB::Manager::Employee->current->id, @permission_values; + } + if ($form->{vendor}) { $where .= " AND v.name ILIKE ?"; push(@values, like($form->{vendor})); @@ -518,7 +554,7 @@ SQL } if ($where) { - substr($where, 0, 4, " WHERE "); + $where =~ s{\s*AND\s*}{ WHERE }; $query .= $where; } @@ -616,6 +652,15 @@ sub _post_payment { $old_form = save_form(); + $query = <{id}); + # Delete all entries in acc_trans from prior payments. if (SL::DB::Default->get->payments_changeable != 0) { $self->_delete_payments($form, $dbh); @@ -644,7 +689,7 @@ sub _post_payment { # Get the AP accno. $query = - qq|SELECT c.accno + qq|SELECT c.id FROM acc_trans at LEFT JOIN chart c ON (at.chart_id = c.id) WHERE (trans_id = ?) @@ -652,10 +697,10 @@ sub _post_payment { ORDER BY at.acc_trans_id LIMIT 1|; - ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); + ($form->{AP_chart_id}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); # Post the new payments. - $self->post_transaction($myconfig, $form, $dbh, 1); + $self->post_transaction($myconfig, $form, $dbh, payments_only => 1, already_cleared => \%already_cleared); restore_form($old_form);