From: Moritz Bunkus Date: Tue, 3 Apr 2007 14:10:02 +0000 (+0000) Subject: Mahnwesen: Die Tabelle dunning so umgebaut, dass gemeinsam gestartete Mahnungen auch... X-Git-Tag: release-2.4.3^2~536 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=840aea131eef33512dccc13184e01affafaafb37;p=kivitendo-erp.git Mahnwesen: Die Tabelle dunning so umgebaut, dass gemeinsam gestartete Mahnungen auch später gemeinsam erneut ausgedruckt werden können. Dafür auch die Listenansicht bereits gestarteter Mahnungen verbessert. --- diff --git a/SL/DN.pm b/SL/DN.pm index b3c5e6a58..92881569f 100644 --- a/SL/DN.pm +++ b/SL/DN.pm @@ -121,28 +121,33 @@ sub save_config { sub save_dunning { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form, $rows, $userspath,$spool, $sendmail) = @_; + my ($self, $myconfig, $form, $rows, $userspath, $spool, $sendmail) = @_; # connect to database my $dbh = $form->dbconnect_noauto($myconfig); my ($query, @values); + my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|); + foreach my $row (@{ $rows }) { $form->{"interest_$row"} = $form->parse_amount($myconfig,$form->{"interest_$row"}); $form->{"fee_$row"} = $form->parse_amount($myconfig,$form->{"fee_$row"}); $form->{send_email} = $form->{"email_$row"}; - $query = qq|UPDATE ar SET dunning_id = ? WHERE id = ?|; - @values = ($form->{"next_dunning_id_$row"}, $form->{"inv_id_$row"}); + $query = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|; + @values = ($form->{"next_dunning_config_id_$row"}, + $form->{"inv_id_$row"}); do_query($form, $dbh, $query, @values); + $query = - qq|INSERT INTO dunning (dunning_id, dunning_level, trans_id, fee, - interest, transdate, duedate) - VALUES (?, (SELECT dunning_level FROM dunning_config WHERE id = ?), + qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, + trans_id, fee, interest, transdate, duedate) + VALUES (?, ?, (SELECT dunning_level FROM dunning_config WHERE id = ?), ?, ?, ?, current_date, ?)|; - @values = (conv_i($form->{"next_dunning_id_$row"}), - conv_i($form->{"next_dunning_id_$row"}), + @values = ($dunning_id, + conv_i($form->{"next_dunning_config_id_$row"}), + conv_i($form->{"next_dunning_config_id_$row"}), conv_i($form->{"inv_id_$row"}), $form->{"fee_$row"}, $form->{"interest_$row"}, conv_date($form->{"next_duedate_$row"})); @@ -155,7 +160,7 @@ sub save_dunning { template AS formname, email_subject, email_body, email_attachment, da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate - FROM ar LEFT JOIN dunning_config ON (dunning_config.id = ar.dunning_id) + FROM ar LEFT JOIN dunning_config ON (dunning_config.id = ar.dunning_config_id) LEFT JOIN dunning da ON (ar.id = da.trans_id AND dunning_config.dunning_level = da.dunning_level) WHERE ar.id IN (| . join(", ", map("?", @{ $form->{"inv_ids"} })) . qq|)|; @@ -233,6 +238,7 @@ sub save_dunning { $form->{keep_tmpfile} = 1; } + delete($form->{tmpfile}); $form->parse_template($myconfig, $userspath); $dbh->commit; @@ -258,9 +264,13 @@ sub get_invoices { WHERE dunning_level > (SELECT CASE - WHEN a.dunning_id IS NULL + WHEN a.dunning_config_id IS NULL THEN 0 - ELSE (SELECT dunning_level FROM dunning_config WHERE id = a.dunning_id ORDER BY dunning_level LIMIT 1) + ELSE (SELECT dunning_level + FROM dunning_config + WHERE id = a.dunning_config_id + ORDER BY dunning_level + LIMIT 1) END FROM dunning_config LIMIT 1) LIMIT 1)) |; @@ -289,7 +299,7 @@ sub get_invoices { } if ($form->{dunning_level}) { - $where .= qq| AND a.dunning_id = ?|; + $where .= qq| AND a.dunning_config_id = ?|; push(@values, conv_i($form->{dunning_level})); } @@ -310,11 +320,11 @@ sub get_invoices { a.duedate + dnn.terms - current_date AS nextlevel, $paymentdate - a.duedate AS pastdue, dn.dunning_level, current_date + dnn.payment_terms AS next_duedate, - dnn.dunning_description AS next_dunning_description, dnn.id AS next_dunning_id, + dnn.dunning_description AS next_dunning_description, dnn.id AS next_dunning_config_id, dnn.interest AS interest_rate, dnn.terms FROM dunning_config dnn, ar a JOIN customer ct ON (a.customer_id = ct.id) - LEFT JOIN dunning_config dn ON (dn.id = a.dunning_id) + LEFT JOIN dunning_config dn ON (dn.id = a.dunning_config_id) LEFT JOIN dunning da ON ((da.trans_id = a.id) AND (dn.dunning_level = da.dunning_level)) $where ORDER BY a.id, transdate, duedate, name|; @@ -378,7 +388,7 @@ sub get_dunning { } if ($form->{dunning_level}) { - $where .= qq| AND a.dunning_id = ?|; + $where .= qq| AND a.dunning_config_id = ?|; push(@values, conv_i($form->{dunning_level})); } @@ -389,7 +399,7 @@ sub get_dunning { } if (!$form->{showold}) { - $where .= qq| AND (a.amount > a.paid) AND (da.dunning_id = a.dunning_id) |; + $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |; } if ($form->{transdatefrom}) { @@ -411,13 +421,14 @@ sub get_dunning { $query = qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, - ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee, - da.interest, dn.dunning_description, da.transdate AS dunning_date, da.duedate AS dunning_duedate, da.dunning_id - FROM ar a - JOIN customer ct ON (a.customer_id = ct.id), dunning da - LEFT JOIN dunning_config dn ON (da.dunning_id = dn.id) - $where - ORDER BY name, a.id|; + ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee, + da.interest, dn.dunning_description, da.transdate AS dunning_date, + da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id + FROM ar a + JOIN customer ct ON (a.customer_id = ct.id), dunning da + LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id) + $where + ORDER BY name, a.id|; $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values); @@ -485,8 +496,6 @@ sub melt_pdfs { my ($self, $myconfig, $form, $userspath) = @_; - map({ $_ =~ s|.*/||g; } @{ $form->{DUNNING_PDFS} }); - foreach my $file (@{ $form->{DUNNING_PDFS} }) { $inputfiles .= " $userspath/$file "; } @@ -543,7 +552,7 @@ Content-Length: $numbytes sub print_dunning { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form, $dunning_id, $customer_id, $userspath,$spool, $sendmail) = @_; + my ($self, $myconfig, $form, $dunning_id, $userspath,$spool, $sendmail) = @_; # connect to database my $dbh = $form->dbconnect_noauto($myconfig); @@ -552,12 +561,12 @@ sub print_dunning { ar.transdate, ar.duedate, paid, amount - paid AS open_amount, template AS formname, email_subject, email_body, email_attachment, da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate - FROM ar - LEFT JOIN dunning_config ON (dunning_config.id = ar.dunning_id) - LEFT JOIN dunning da ON ((ar.id = da.trans_id) AND (dunning_config.dunning_level = da.dunning_level)) - WHERE (ar.dunning_id = ?) AND (customer_id = ?)|; + FROM dunning da + LEFT JOIN dunning_config ON (dunning_config.id = da.dunning_config_id) + LEFT JOIN ar ON (ar.id = da.trans_id) + WHERE (da.dunning_id = ?)|; - my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id, $customer_id); + my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id); my $first = 1; while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { if ($first) { diff --git a/SL/IS.pm b/SL/IS.pm index 6971f9f48..310dcbb59 100644 --- a/SL/IS.pm +++ b/SL/IS.pm @@ -1700,12 +1700,12 @@ sub get_customer { map { $form->{$_} = $ref->{$_} } keys %$ref; my $query = qq|SELECT sum(a.amount - a.paid) AS dunning_amount FROM ar a - WHERE a.paid < a.amount AND a.customer_id = ? AND a.dunning_id IS NOT NULL|; + WHERE a.paid < a.amount AND a.customer_id = ? AND a.dunning_config_id IS NOT NULL|; $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id}); map { $form->{$_} = $ref->{$_} } keys %$ref; my $query = qq|SELECT dnn.dunning_description AS max_dunning_level FROM dunning_config dnn - WHERE id in (SELECT dunning_id from ar WHERE paid < amount AND customer_id = ? AND dunning_id IS NOT NULL) + WHERE id in (SELECT dunning_config_id from ar WHERE paid < amount AND customer_id = ? AND dunning_id IS NOT NULL) ORDER BY dunning_level DESC LIMIT 1|; $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id}); map { $form->{$_} = $ref->{$_} } keys %$ref; diff --git a/bin/mozilla/dn.pl b/bin/mozilla/dn.pl index bd96d3edc..1992ce810 100644 --- a/bin/mozilla/dn.pl +++ b/bin/mozilla/dn.pl @@ -163,9 +163,8 @@ sub edit_config { $column_data{dunning_level} = qq|$i|; $column_data{dunning_description} = qq||; - my $active = ""; $column_data{active} = - qq||; + qq||; my $email = ""; $column_data{email} = qq||; @@ -465,12 +464,12 @@ sub show_invoices { |; $form->{selectdunning} =~ s/ selected//g; - if ($ref->{next_dunning_id} ne "") { - $form->{selectdunning} =~ s/value=$ref->{next_dunning_id}/value=$ref->{next_dunning_id} selected/; + if ($ref->{next_dunning_config_id} ne "") { + $form->{selectdunning} =~ s/value=$ref->{next_dunning_config_id}/value=$ref->{next_dunning_config_id} selected/; } - $dunning = qq||; + $dunning = qq||; $column_data{dunning_description} = qq|$ref->{dunning_level}: $dunning|; @@ -589,7 +588,7 @@ sub save_dunning { } } if (scalar(@{ $form->{inv_ids} }) != 0) { - DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath,$spool, $sendmail); + DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail); } } } else { @@ -598,7 +597,7 @@ sub save_dunning { @rows = (); $form->{inv_ids} = [ $form->{"inv_id_$i"} ]; push(@rows, $i); - DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath,$spool, $sendmail); + DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail); } } } @@ -887,14 +886,28 @@ sub show_dunning { print qq| |; + + my %columns = ( + "dunning_duedate" => "next_duedate", + "duedate" => "inv_duedate", + "transdate" => "invdate", + "amount" => "invamount", + ); + my $i = 0; my $j = 0; - my $previous_customer_id; + my ($previous_dunning_id, $first_row_for_dunning); foreach $ref (@{ $form->{DUNNINGS} }) { $i++; - $j++ if ($previous_customer_id != $ref->{customer_id}); - $j = $j % 2; - $previous_customer_id = $ref->{customer_id}; + + if ($previous_dunning_id != $ref->{dunning_id}) { + $j++; + $j = $j % 2; + $first_row_for_dunning = 1; + } else { + $first_row_for_dunning = 0; + } + $previous_dunning_id = $ref->{dunning_id}; print qq| @@ -902,27 +915,28 @@ sub show_dunning { - $dunning = qq||; - my $script = ""; - if ($ref->{invoice}) { - $script = "is.pl"; - } else { - $script = "ar.pl"; + foreach (qw(dunning_date dunning_duedate duedate transdate customername + amount fee interest)) { + my $col = $columns{$_} ? $columns{$_} : $_; + $column_data{$col} = "" . H($ref->{$_}) . ""; } - $column_data{dunning_description} = qq|{dunning_id}&customer_id=$ref->{customer_id}&format=pdf&media=screen&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$form->{callback}>$ref->{dunning_description}|; - my $active = "checked"; - $column_data{dunning_date} = qq|$ref->{dunning_date}|; - $column_data{next_duedate} = qq|$ref->{dunning_duedate}|; - - $column_data{inv_duedate} = qq|$ref->{duedate}|; - $column_data{invdate} = qq|$ref->{transdate}|; - $column_data{invnumber} = qq|{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$form->{callback}>$ref->{invnumber}|; - $column_data{customername} = qq|$ref->{customername}|; - $column_data{invamount} = qq|$ref->{amount}|; - $column_data{fee} = qq|$ref->{fee}|; - $column_data{interest} = qq|$ref->{interest}|; + if ($first_row_for_dunning) { + $column_data{dunning_description} = + qq|| . H($ref->{dunning_description}) . qq||; + } else { + $column_data{dunning_description} = qq| |; + $column_data{customername} = qq| |; + } + $column_data{invnumber} = + qq|{id}) . + join(map({ "&${_}=" . E($form->{$_}) } qw(login path password callback))) . + qq|">| . H($ref->{invnumber}) . qq||; map { print "$column_data{$_}\n" } @column_index; @@ -968,7 +982,7 @@ sub show_dunning { sub print_dunning { $lxdebug->enter_sub(); - DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $form->{customer_id}, $userspath, $spool, $sendmail); + DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail); if($form->{DUNNING_PDFS}) { DN->melt_pdfs(\%myconfig, \%$form,$spool); diff --git a/lx-erp.conf b/lx-erp.conf index 6818572e1..85291943a 100644 --- a/lx-erp.conf +++ b/lx-erp.conf @@ -81,7 +81,7 @@ $dbcharset = "ISO-8859-15"; # # Beipiel: # $LXDebug::global_level = LXDebug::TRACE | LXDebug::QUERY; -$LXDebug::global_level = LXDebug::NONE; +$LXDebug::global_level = LXDebug::ALL; 1; diff --git a/sql/Pg-upgrade2/dunning_dunning_id.sql b/sql/Pg-upgrade2/dunning_dunning_id.sql new file mode 100644 index 000000000..5bf464243 --- /dev/null +++ b/sql/Pg-upgrade2/dunning_dunning_id.sql @@ -0,0 +1,21 @@ +-- @tag: dunning_dunning_id +-- @description: In der Tabelle dunning ist dunning_id falsch benannt und es fehlt eine Spalte, die mehrere Einträge zusammenfasst. +-- @depends: release_2_4_2 +ALTER TABLE dunning ADD COLUMN dunning_config_id integer; +UPDATE dunning SET dunning_config_id = dunning_id; +ALTER TABLE dunning ADD FOREIGN KEY (dunning_config_id) REFERENCES dunning_config (id); + +ALTER TABLE dunning ADD COLUMN itime timestamp; +ALTER TABLE dunning ALTER COLUMN itime SET DEFAULT now(); +UPDATE dunning SET itime = now(); + +ALTER TABLE dunning ADD COLUMN mtime timestamp; +CREATE TRIGGER mtime_dunning + BEFORE UPDATE ON dunning + FOR EACH ROW + EXECUTE PROCEDURE set_mtime(); + +UPDATE dunning SET dunning_id = nextval('id'); + +ALTER TABLE ar RENAME COLUMN dunning_id TO dunning_config_id; +ALTER TABLE ar ADD FOREIGN KEY (dunning_config_id) REFERENCES dunning_config (id);