X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAP.pm;h=e2709f7342cb48c83260244ea905e2557efcd093;hb=550a75a67c8cd48abdb0e92dc4b2f49c6a1d0879;hp=0f2e8ee0736b5ce46e301af8141c380e8e09407a;hpb=1118dab96d2c42e872c9782b4374709d52b20653;p=kivitendo-erp.git diff --git a/SL/AP.pm b/SL/AP.pm index 0f2e8ee07..e2709f734 100644 --- a/SL/AP.pm +++ b/SL/AP.pm @@ -48,20 +48,16 @@ sub post_transaction { my ($null, $taxrate, $amount); my $exchangerate = 0; + $form->{defaultcurrency} = $form->get_default_currency($myconfig); + ($null, $form->{department_id}) = split(/--/, $form->{department}); $form->{department_id} *= 1; if ($form->{currency} eq $form->{defaultcurrency}) { $form->{exchangerate} = 1; } else { - $exchangerate = - $form->check_exchangerate($myconfig, $form->{currency}, - $form->{transdate}, 'sell'); - - $form->{exchangerate} = - ($exchangerate) - ? $exchangerate - : $form->parse_amount($myconfig, $form->{exchangerate}); + $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell'); + $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate}); } for $i (1 .. $form->{rowcount}) { @@ -100,10 +96,7 @@ sub post_transaction { qq|ORDER BY c.accno|; $sth = $dbh->prepare($query); $sth->execute($form->{"tax_id_$i"}) || $form->dberror($query . " (" . $form->{"tax_id_$i"} . ")"); - ($form->{AP_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = - $sth->fetchrow_array; - $form->{AP_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"}; - $form->{AP_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"}; + ($form->{AP_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = $sth->fetchrow_array(); $sth->finish; if ($form->{taxincluded} *= 1) { @@ -216,7 +209,6 @@ sub post_transaction { if ($form->{"amount_$i"} != 0) { my $project_id; $project_id = conv_i($form->{"project_id_$i"}); - $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey}; # insert detail records in acc_trans $query = @@ -226,7 +218,7 @@ sub post_transaction { qq| ?, ?, ?, ?)|; @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"}, $form->{"amount_$i"}, conv_date($form->{transdate}), - $project_id, $taxkey); + $project_id, $form->{"taxkey_$i"}); do_query($form, $dbh, $query, @values); if ($form->{"tax_$i"} != 0) { @@ -238,7 +230,7 @@ sub post_transaction { qq| ?, ?, ?, ?)|; @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"}, $form->{"tax_$i"}, conv_date($form->{transdate}), - $project_id, $taxkey); + $project_id, $form->{"taxkey_$i"}); do_query($form, $dbh, $query, @values); } @@ -269,14 +261,8 @@ sub post_transaction { if ($form->{currency} eq $form->{defaultcurrency}) { $form->{"exchangerate_$i"} = 1; } else { - $exchangerate = - $form->check_exchangerate($myconfig, $form->{currency}, - $form->{"datepaid_$i"}, 'sell'); - - $form->{"exchangerate_$i"} = - ($exchangerate) - ? $exchangerate - : $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); + $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'); + $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); } $form->{"AP_paid_$i"} =~ s/\"//g; @@ -409,7 +395,7 @@ sub ap_transactions { my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | . qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | . - qq| a.globalproject_id, | . + qq| a.globalproject_id, a.storno, a.storno_id, | . qq| pr.projectnumber AS globalprojectnumber, | . qq| e.name AS employee | . qq|FROM ap a | . @@ -417,7 +403,7 @@ sub ap_transactions { qq|LEFT JOIN employee e ON (a.employee_id = e.id) | . qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) |; - my $where = qq| WHERE COALESCE(storno, false) != true |; + my $where = ''; my @values; if ($form->{vendor_id}) { @@ -468,21 +454,20 @@ sub ap_transactions { } if ($where) { -# substr($where, 0, 4) = "WHERE"; + substr($where, 0, 4, " WHERE "); $query .= $where; } my @a = (transdate, invnumber, name); push @a, "employee" if $self->{l_employee}; - my $sortorder = join(', ', @a); + my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC'; + my $sortorder = join(', ', map { "$_ $sortdir" } @a); - if (grep({ $_ eq $form->{sort} } - qw(transdate id invnumber ordnumber name netamount tax amount - paid datepaid due duedate notes employee))) { - $sortorder = $form->{sort}; + if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) { + $sortorder = $form->{sort} . " $sortdir"; } - $query .= " ORDER by $sortorder"; + $query .= " ORDER BY $sortorder"; my $sth = $dbh->prepare($query); $sth->execute(@values) || @@ -593,6 +578,7 @@ sub post_payment { ($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|); $form->{defaultcurrency} = (split m/:/, $form->{defaultcurrency})[0]; + $form->{currency} = $form->{defaultcurrency} if ($form->{defaultcurrency} && ($form->{currency} =~ m/^\s*$/)); $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); @@ -743,5 +729,57 @@ sub setup_form { $main::lxdebug->leave_sub(); } +sub storno { + $main::lxdebug->enter_sub(); + + my ($self, $form, $myconfig, $id) = @_; + + my ($query, $new_id, $storno_row, $acc_trans_rows); + my $dbh = $form->get_standard_dbh($myconfig); + + $query = qq|SELECT nextval('glid')|; + ($new_id) = selectrow_query($form, $dbh, $query); + + $query = qq|SELECT * FROM ap WHERE id = ?|; + $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id); + + $storno_row->{id} = $new_id; + $storno_row->{storno_id} = $id; + $storno_row->{storno} = 't'; + $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber}; + $storno_row->{amount} *= -1; + $storno_row->{netamount} *= -1; + $storno_row->{paid} = $storno_row->{amount}; + + delete @$storno_row{qw(itime mtime)}; + + $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row); + do_query($form, $dbh, $query, (values %$storno_row)); + + $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|; + do_query($form, $dbh, $query, $id); + + # now copy acc_trans entries + $query = qq|SELECT a.*, c.link FROM acc_trans a LEFT JOIN chart c ON a.chart_id = c.id WHERE a.trans_id = ? ORDER BY a.oid|; + my $rowref = selectall_hashref_query($form, $dbh, $query, $id); + + # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/ + while ($rowref->[-1]{link} =~ /paid/) { + splice(@$rowref, -2); + } + + for my $row (@$rowref) { + delete @$row{qw(itime mtime link)}; + $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row); + $row->{trans_id} = $new_id; + $row->{amount} *= -1; + do_query($form, $dbh, $query, (values %$row)); + } + + $dbh->commit; + + $main::lxdebug->leave_sub(); +} + 1;