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);
$form->{payables} = $form->{invpaid};
}
+ my %already_cleared = %{ $params{already_cleared} // {} };
+
# add paid transactions
for my $i (1 .. $form->{paidaccounts}) {
$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| .
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);
# 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| .
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);
# 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;
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}));
}
if ($where) {
- substr($where, 0, 4, " WHERE ");
+ $where =~ s{\s*AND\s*}{ WHERE };
$query .= $where;
}
$old_form = save_form();
+ $query = <<SQL;
+ SELECT at.acc_trans_id, at.amount, at.cleared, c.accno
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (at.trans_id = ?)
+SQL
+
+ my %already_cleared = selectall_as_map($form, $dbh, $query, 'acc_trans_id', [ qw(amount cleared accno) ], $form->{id});
+
# Delete all entries in acc_trans from prior payments.
if (SL::DB::Default->get->payments_changeable != 0) {
$self->_delete_payments($form, $dbh);
($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);