# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
#======================================================================
#
# Dunning process module
#
#======================================================================
+use POSIX qw(strftime);
+
+use List::Util qw(notall);
+use List::MoreUtils qw(none);
+
use SL::IS;
-use SL::PE;
use SL::DN;
-use Data::Dumper;
-
+use SL::DB::Department;
+use SL::DB::Dunning;
+use SL::File;
+use SL::Helper::Flash qw(flash);
+use SL::Locale::String qw(t8);
+use SL::Presenter::EmailJournal;
+use SL::Presenter::FileObject;
+use SL::Presenter::WebdavObject;
+use SL::ReportGenerator;
+
+require "bin/mozilla/common.pl";
+require "bin/mozilla/reportgenerator.pl";
require "bin/mozilla/io.pl";
-require "bin/mozilla/arap.pl";
+
+use strict;
1;
sub edit_config {
- $lxdebug->enter_sub();
-
- # edit all dunning config data
-
- $form->header;
- DN->get_config(\%myconfig, \%$form);
- $form->{title} = $locale->text('Edit Dunning Process Config');
-
- $form->{callback} =
- "$form->{script}?action=edit_config&login=$form->{login}&password=$form->{password}"
- unless $form->{callback};
-
- @column_index = qw(dunning_level dunning_description active auto email payment_terms terms fee interest template);
-
- $column_header{dunning_level} =
- qq|<th class=listheading>|
- . $locale->text('Dunning Level')
- . qq|</th>|;
- $column_header{dunning_description} =
- qq|<th class=listheading>|
- . $locale->text('Dunning Description')
- . qq|</th>|;
- $column_header{active} =
- qq|<th class=listheading>|
- . $locale->text('Active?')
- . qq|</th>|;
- $column_header{auto} =
- qq|<th class=listheading>|
- . $locale->text('Auto Send?')
- . qq|</th>|;
- $column_header{email} =
- qq|<th class=listheading>|
- . $locale->text('eMail Send?')
- . qq|</th>|;
- $column_header{payment_terms} =
- qq|<th class=listheading>|
- . $locale->text('Fristsetzung')
- . qq|</th>|;
- $column_header{terms} =
- qq|<th class=listheading>|
- . $locale->text('Duedate +Days')
- . qq|</th>|;
- $column_header{fee} =
- qq|<th class=listheading>|
- . $locale->text('Fee')
- . qq|</th>|;
- $column_header{interest} =
- qq|<th class=listheading>|
- . $locale->text('Interest Rate')
- . qq|</th>|;
- $column_header{template} =
- qq|<th class=listheading>|
- . $locale->text('Template')
- . qq|</th>|;
- print qq|
-<body>
-<script type="text/javascript" src="js/common.js"></script>
-<script type="text/javascript" src="js/dunning.js"></script>
-<form method=post action=$form->{script}>
-
-
-<table width=100%>
- <tr>
- <th class=listtop colspan=10>$form->{title}</th>
- </tr>
- <tr height="5"></tr>
- <tr>|;
- map { print "$column_header{$_}\n" } @column_index;
-
- print qq|
- </tr>
-|;
- my $i = 0;
- foreach $ref (@{ $form->{DUNNING} }) {
-
- $i++;
- my $j = $i % 2;
-
- print qq|
- <tr valign=top class=listrow$j>
-|;
+ $main::lxdebug->enter_sub();
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
- $column_data{dunning_level} =
- qq|<td><input type=hidden name=dunning_level_$i size=2 value="$i"><input type=hidden name=id_$i value="$ref->{id}">$i</td>|;
- $column_data{dunning_description} = qq|<td><input name=dunning_description_$i value="$ref->{dunning_description}"></td>|;
- my $active = ($ref->{active}) ? "checked" : "";
- $column_data{active} =
- qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
- my $email = ($ref->{email}) ? "checked" : "";
- $column_data{email} =
- qq|<td><input type=checkbox name=email_$i value=1 $email><button type="button" onclick="set_email_window('email_subject_$i', 'email_body_$i', 'email_attachment_$i')">| . $locale->text('L') . qq|</button><input type=hidden name=email_body_$i value="$ref->{email_body}"><input type=hidden name=email_subject_$i value="$ref->{email_subject}"><input type=hidden name=email_attachment_$i value="$ref->{email_attachment}"></td>|;
+ $main::auth->assert('config');
- my $auto = ($ref->{auto}) ? "checked" : "";
- $column_data{auto} =
- qq|<td><input type=checkbox name=auto_$i value=1 $auto></td>|;
- $column_data{payment_terms} = qq|<td><input name=payment_terms_$i size=3 value="$ref->{payment_terms}"></td>|;
- $column_data{terms} = qq|<td><input name=terms_$i size=3 value="$ref->{terms}"></td>|;
- $column_data{fee} = qq|<td><input name=fee_$i size=5 value="$ref->{fee}"></td>|;
- $column_data{interest} = qq|<td><input name=interest_$i size=4 value="$ref->{interest}">%</td>|;
- $column_data{template} = qq|<td><input name=template_$i value="$ref->{template}"></td>|;
+ DN->get_config(\%myconfig, \%$form);
+ $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
+ 'transdate' => 'current_date' });
+ $form->{SELECT_AR_AMOUNT} = [];
+ $form->{SELECT_AR} = [];
+ foreach my $chart (@{ $form->{ALL_CHARTS} }) {
+ $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
- map { print "$column_data{$_}\n" } @column_index;
+ if ($chart->{LINKS}->{AR}) {
+ $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
+ push @{ $form->{SELECT_AR} }, $chart;
+ }
- print qq|
- </tr>
-|;
+ if ($chart->{LINKS}->{AR_amount}) {
+ $chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
+ $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
+ push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
+ }
}
- $i++;
- my $j = $i % 2;
-
- print qq|
- <tr valign=top class=listrow$j>
-|;
+ $form->{title} = $locale->text('Edit Dunning Process Config');
+ $form->{callback} ||= build_std_url("action=edit_config");
- $column_data{dunning_level} =
- qq|<td><input type=hidden size=2 name=dunning_level_$i value=$i>$i</td>|;
- $column_data{dunning_description} = qq|<td><input name=dunning_description_$i ></td>|;
- $column_data{active} =
- qq|<td><input type=checkbox name=active_$i value=1></td>|;
- my $email = "";
- $column_data{email} =
- qq|<td><input type=checkbox name=email_$i value=1 $email><button type="button" onclick="set_email_window('email_subject_$i', 'email_body_$i', 'email_attachment_$i')">| . $locale->text('L') . qq|</button><input type=hidden name=email_body_$i><input type=hidden name=email_subject_$i><input type=hidden name=email_attachment_$i></td>|;
- my $auto = "";
- $column_data{auto} =
- qq|<td><input type=checkbox name=auto_$i value=1 $auto></td>|;
- $column_data{payment_terms} = qq|<td><input size=3 name=payment_terms_$i></td>|;
- $column_data{terms} = qq|<td><input size=3 name=terms_$i></td>|;
- $column_data{fee} = qq|<td><input size=5 name=fee_$i></td>|;
- $column_data{interest} = qq|<td><input size=4 name=interest_$i>%</td>|;
- $column_data{template} = qq|<td><input name=template_$i></td>|;
+ setup_dn_edit_config_action_bar();
+ $form->header();
+ print $form->parse_html_template("dunning/edit_config");
- $form->{rowcount} = $i;
- map { print "$column_data{$_}\n" } @column_index;
+ $main::lxdebug->leave_sub();
+}
- print qq|
- </tr>
-|;
+sub add {
+ $main::lxdebug->enter_sub();
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
- print qq|
- </table>
- </td>
- </tr>
- <tr>
- <td><hr size=3 noshade></td>
- </tr>
-</table>
+ $main::auth->assert('dunning_edit');
-<br>
-<form method=post action=$form->{script}>
+ DN->get_config(\%myconfig, \%$form);
-<input name=callback type=hidden value="$form->{callback}">
-<input name=rowcount type=hidden value="$form->{rowcount}">
+ $form->get_lists("departments" => "ALL_DEPARTMENTS");
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
+ $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
+ $form->{SHOW_DEPARTMENT_SELECTION} = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
-<input class=submit type=submit name=action value="|
- . $locale->text('Save') . qq|">
+ $form->{title} = $locale->text('Start Dunning Process');
- </form>
+ setup_dn_add_action_bar();
+ $form->header();
- </body>
- </html>
-|;
+ print $form->parse_html_template("dunning/add");
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
-sub add {
- $lxdebug->enter_sub();
- # setup customer selection
- $form->all_vc(\%myconfig, "customer", "AR");
-
- DN->get_config(\%myconfig, \%$form);
-
- if (@{ $form->{all_customer} }) {
- map { $customer .= "<option>$_->{name}--$_->{id}\n" }
- @{ $form->{all_customer} };
- $customer = qq|<select name=customer><option>\n$customer</select>|;
- } else {
- $customer = qq|<input name=customer size=35>|;
- }
-
- # dunning levels
- if (@{ $form->{DUNNING} }) {
- $form->{selectdunning_level} = "<option></option\n";
- map {
- $form->{selectdunning_level} .=
- "<option value=$_->{id}>$_->{dunning_description}</option>\n"
- } (@{ $form->{DUNNING} });
- }
- $dunning_level = qq|
- <tr>
- <th align=right nowrap>| . $locale->text('Next Dunning Level') . qq|</th>
- <td colspan=3><select name=dunning_level>$form->{selectdunning_level}</select></td>
- </tr>
- | if $form->{selectdunning_level};
-
- # departments
- if (@{ $form->{all_departments} }) {
- $form->{selectdepartment} = "<option>\n";
- map {
- $form->{selectdepartment} .=
- "<option>$_->{description}--$_->{id}\n"
- } (@{ $form->{all_departments} });
- }
- $department = qq|
- <tr>
- <th align=right nowrap>| . $locale->text('Department') . qq|</th>
- <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
- </tr>
- | if $form->{selectdepartment};
- $form->{title} = $locale->text('Start Dunning Process');
- $form->{nextsub} = "show_invoices";
-
- # use JavaScript Calendar or not
- $form->{jsscript} = $jscalendar;
- $jsscript = "";
- if ($form->{jsscript}) {
-
- # with JavaScript Calendar
- $button1 = qq|
- <td><input name=paymentuntil id=paymentuntil size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
- <input type=button name=paymentuntil id="trigger1" value=|
- . $locale->text('button') . qq|></td>
- |;
-
- #write Trigger
- $jsscript =
- Form->write_trigger(\%myconfig, "1", "paymentuntil", "BR", "trigger1");
- } else {
-
- # without JavaScript Calendar
- $button1 =
- qq|<td><input name=paymentuntil id=paymentuntil size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
- }
- $form->{fokus} = "search.customer";
- $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
- $form->header;
- $onload = qq|focus()|;
- $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
- $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
- print qq|
-<body onLoad="$onload">
-
-<form method=post name="search" action=$form->{script}>
-
-<table width=100%>
- <tr><th class=listtop>$form->{title}</th></tr>
- <tr height="5"></tr>
- <tr>
- <td>
- <table>
- <tr>
- <th align=right>| . $locale->text('Customer') . qq|</th>
- <td colspan=3>$customer</td>
- </tr>
- $dunning_level
- $department
- <tr>
- <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
- <td colspan=3><input name=invnumber size=20></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
- <td colspan=3><input name=ordnumber size=20></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
- <td colspan=3><input name=notes size=40></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Payment until') . qq|</th>
- $button1
- </tr>
- </table>
- </td>
- </tr>
- <tr>
- <td><hr size=3 noshade></td>
- </tr>
- <tr>
- <td>
- <table>
- <tr>
- <th align=right nowrap>| . $locale->text('Minimum Amount') . qq|</th>
- <td><input name=minamount size=6></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Group Invoices') . qq|</th>
- <td><input type=checkbox value=1 name=groupinvoices checked></td>
- </tr>
- </table>
- </td>
- </tr>
-</table>
-
-<input type=hidden name=nextsub value=$form->{nextsub}>
-
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
-
-<br>
-<input class=submit type=submit name=action value="|
- . $locale->text('Continue') . qq|">
-
-</form>
-
-</body>
-
-$jsscript
-
-</html>
-|;
-
- $lxdebug->leave_sub();
+sub show_invoices {
+ $main::lxdebug->enter_sub();
-}
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
-sub show_invoices {
- $lxdebug->enter_sub();
+ $main::auth->assert('dunning_edit');
DN->get_invoices(\%myconfig, \%$form);
$form->{title} = $locale->text('Start Dunning Process');
- if (@{ $form->{DUNNING_CONFIG} }) {
- foreach $item (@{ $form->{DUNNING_CONFIG} }) {
- $form->{selectdunning} .=
- "<option value=$item->{id}>$item->{dunning_description}</option>";
- }
- }
-
-
- $form->{nextsub} = "save_dunning";
-
- $form->{callback} =
- "$form->{script}?action=show_invoices&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&paymentuntil=$form->{paymentuntil}&groupinvoices=$form->{groupinvoices}&minamount=$form->{minamount}&dunning_level=$form->{dunning_level}¬es=$form->{notes}"
- unless $form->{callback};
-
- @column_index = qw(dunning_description active email customername invnumber invdate inv_duedate invamount next_duedate fee interest );
-
- $column_header{dunning_description} =
- qq|<th class=listheading>|
- . $locale->text('Current / Next Level')
- . qq|</th>|;
- $column_header{active} =
- qq|<th class=listheading>|
- . $locale->text('Active?')
- . qq|</th>|;
- $column_header{email} =
- qq|<th class=listheading>|
- . $locale->text('eMail?')
- . qq|</th>|;
- $column_header{customername} =
- qq|<th class=listheading>|
- . $locale->text('Customername')
- . qq|</th>|;
- $column_header{invnumber} =
- qq|<th class=listheading>|
- . $locale->text('Invno.')
- . qq|</th>|;
- $column_header{inv_duedate} =
- qq|<th class=listheading>|
- . $locale->text('Inv. Duedate')
- . qq|</th>|;
- $column_header{next_duedate} =
- qq|<th class=listheading>|
- . $locale->text('Dunning Duedate')
- . qq|</th>|;
- $column_header{invdate} =
- qq|<th class=listheading>|
- . $locale->text('Invdate')
- . qq|</th>|;
- $column_header{invamount} =
- qq|<th class=listheading>|
- . $locale->text('Amount')
- . qq|</th>|;
- $column_header{fee} =
- qq|<th class=listheading>|
- . $locale->text('Total Fees')
- . qq|</th>|;
- $column_header{interest} =
- qq|<th class=listheading>|
- . $locale->text('Total Interest')
- . qq|</th>|;
-
- $form->header;
-
-
- print qq|
-<body>
-<script type="text/javascript" src="js/common.js"></script>
-<script type="text/javascript" src="js/dunning.js"></script>
-<form name=Form method=post action=$form->{script}>
-
-
-<table width=100%>
- <tr>
- <th class=listtop colspan=11>$form->{title}</th>
- </tr>
- <tr height="5"></tr>
- <tr>|;
- map { print "$column_header{$_}\n" } @column_index;
-
- print qq|
- </tr>
-|;
- my $i = 0;
- foreach $ref (@{ $form->{DUNNINGS} }) {
-
- $i++;
- my $j = $i % 2;
-
- print qq|
- <tr valign=top class=listrow$j>
-|;
-
- $form->{selectdunning} =~ s/ selected//g;
- 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|<select name=next_dunning_config_id_$i>$form->{selectdunning}</select>|;
+ foreach my $row (@{ $form->{DUNNINGS} }) {
+ $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
+ if ($row->{next_dunning_config_id}) {
+ map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
+ }
+ map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
- $column_data{dunning_description} = qq|<td><input type=hidden name=inv_id_$i size=2 value="$ref->{id}"><input type=hidden name=customer_id_$i size=2 value="$ref->{customer_id}">$ref->{dunning_level}: $dunning</td>|;
- my $active = ($ref->{active}) ? "checked" : "";
- $column_data{active} =
- qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
- my $email = ($ref->{email}) ? "checked" : "";
- $column_data{email} =
- qq|<td><input type=checkbox name=email_$i value=1 $email></td>|;
- $column_data{next_duedate} = qq|<td><input type=hidden name=next_duedate_$i size=6 value="$ref->{next_duedate}">$ref->{next_duedate}</td>|;
-
- $column_data{inv_duedate} = qq|<td><input type=hidden name=inv_duedate_$i size=6 value="$ref->{duedate}">$ref->{duedate}</td>|;
- $column_data{invdate} = qq|<td><input type=hidden name=invdate_$i size=6 value="$ref->{transdate}">$ref->{transdate}</td>|;
- $column_data{invnumber} = qq|<td><input type=hidden name=invnumber_$i size=6 value="$ref->{invnumber}">$ref->{invnumber}</td>|;
- $column_data{customername} = qq|<td><input type=hidden name=customername_$i size=6 value="$ref->{customername}">$ref->{customername}</td>|;
- $column_data{invamount} = qq|<td><input type=hidden name=invamount_$i size=6 value="$ref->{amount}">$ref->{amount}</td>|;
- $column_data{fee} = qq|<td><input type=hidden name=fee_$i size=5 value="$ref->{fee}">$ref->{fee}</td>|;
- $column_data{interest} = qq|<td><input type=hidden name=interest_$i size=4 value="$ref->{interest}">$ref->{interest}</td>|;
-
-
-
- map { print "$column_data{$_}\n" } @column_index;
-
- print qq|
- </tr>
-|;
+ if ($row->{'language_id'}) {
+ $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
+ }
}
- $form->{rowcount} = $i;
-
- print qq|
- </table>
- </td>
- </tr>
- <tr>
- <td><hr size=3 noshade></td>
- </tr>
-</table>|;
- print_options();
- print qq|
-<br>
-<form method=post action=$form->{script}>
-
-<input name=callback type=hidden value="$form->{callback}">
-<input name=rowcount type=hidden value="$form->{rowcount}">
-<input name=nextsub type=hidden value="$form->{nextsub}">
-<input name=groupinvoices type=hidden value="$form->{groupinvoices}">
-
-
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
-<input type="hidden" name="action">
-<input type="submit" name="dummy" value="|
- . $locale->text('Continue') . qq|" onclick="this.disabled=true; this.value='| . $locale->text("The dunning process started") . qq|'; document.Form.action.value='| . $locale->text('Continue') . qq|'; document.Form.submit()">
-
- </form>
+ $form->get_lists('printers' => 'printers',
+ 'languages' => 'languages');
- </body>
- </html>
-|;
+ $form->{type} = 'dunning';
+ $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
+ $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
+ $form->{PRINT_OPTIONS} = print_options('inline' => 1,
+ 'no_queue' => 1,
+ 'no_postscript' => 1,
+ 'no_html' => 1,
+ 'no_opendocument' => 1,);
- $lxdebug->leave_sub();
+ setup_dn_show_invoices_action_bar();
+ $form->header();
+ print $form->parse_html_template("dunning/show_invoices");
+ $main::lxdebug->leave_sub();
}
sub save {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ $main::auth->assert('config');
for my $i (1 .. $form->{rowcount}) {
if ($form->{"dunning_description_$i"} ne "") {
DN->save_config(\%myconfig, \%$form);
# saving the history
if(!exists $form->{addition} && $form->{id} ne "") {
- $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
+ $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
$form->{addition} = "SAVED FOR DUNNING";
- $form->save_history($form->dbconnect(\%myconfig));
+ $form->save_history;
}
- # /saving the history
+ # /saving the history
$form->redirect($locale->text('Dunning Process Config saved!'));
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub save_dunning {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ $main::auth->assert('dunning_edit');
my $active=1;
my @rows = ();
+ my @status;
undef($form->{DUNNING_PDFS});
- if ($form->{groupinvoices}) {
- while ($active) {
- $lastcustomer = 0;
- $form->{inv_ids} = [];
- $active = 0;
- @rows = ();
- for my $i (1 .. $form->{rowcount}) {
- $form->{"active_$i"} *= 1;
- $lastcustomer = $form->{"customer_id_$i"} unless ($lastcustomer);
- if ($form->{"active_$i"} && ($form->{"customer_id_$i"} == $lastcustomer)) {
- push(@{ $form->{inv_ids} }, $form->{"inv_id_$i"});
- $form->{"active_$i"} = 0;
- $form->{"customer_id_$i"} = 0;
- push(@rows, $i);
- } elsif ($form->{"active_$i"}) {
- $active = 1;
- } else {
- $form->{"customer_id_$i"} = 0;
+
+ my $saved_language_id = $form->{language_id};
+
+ if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
+ my %dunnings_for;
+
+ for my $i (1 .. $form->{rowcount}) {
+ next unless ($form->{"active_$i"});
+
+ $dunnings_for{$form->{"customer_id_$i"}} ||= {};
+ my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
+
+ $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
+ my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
+
+ push @{ $level }, { "row" => $i,
+ "invoice_id" => $form->{"inv_id_$i"},
+ "credit_note" => $form->{"credit_note_$i"},
+ "customer_id" => $form->{"customer_id_$i"},
+ "language_id" => $form->{"language_id_$i"},
+ "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+ "print_invoice" => $form->{"include_invoice_$i"},
+ "email" => $form->{"email_$i"}, };
+ }
+
+ foreach my $levels (values %dunnings_for) {
+ foreach my $level (values %{ $levels }) {
+ next unless scalar @{ $level };
+ if (!$form->{force_lang}) {
+ $form->{language_id} = @{$level}[0]->{language_id};
}
- }
- if (scalar(@{ $form->{inv_ids} }) != 0) {
- DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail);
+ my $rc = DN->save_dunning(\%myconfig, $form, $level);
+ $rc->{error} =~ s{\n}{<br />}g if $rc->{error};
+ push @status, { invnumbers => [map { $form->{'invnumber_' . $_->{row}} } @$level],
+ map { ( $_ => $rc->{$_} ) } qw(error dunning_id print_original_invoice send_email), };
}
}
+
} else {
for my $i (1 .. $form->{rowcount}) {
- if ($form->{"active_$i"}) {
- @rows = ();
- $form->{inv_ids} = [ $form->{"inv_id_$i"} ];
- push(@rows, $i);
- DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail);
+ next unless $form->{"active_$i"};
+
+ my $level = [ { "row" => $i,
+ "invoice_id" => $form->{"inv_id_$i"},
+ "customer_id" => $form->{"customer_id_$i"},
+ "language_id" => $form->{"language_id_$i"},
+ "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+ "print_invoice" => $form->{"include_invoice_$i"},
+ "email" => $form->{"email_$i"}, } ];
+ if (!$form->{force_lang}) {
+ $form->{language_id} = @{$level}[0]->{language_id};
}
+ my $rc = DN->save_dunning(\%myconfig, $form, $level);
+ $rc->{error} =~ s{\n}{<br />}g if $rc->{error};
+ push @status, { invnumbers => [map { $form->{'invnumber_' . $_->{row}} } @$level],
+ map { ( $_ => $rc->{$_} ) } qw(error dunning_id print_original_invoice send_email), };
}
}
- if($form->{DUNNING_PDFS}) {
- DN->melt_pdfs(\%myconfig, \%$form,$spool);
+
+ $form->{language_id} = $saved_language_id;
+
+ my $pdf_filename;
+ my $pdf_content;
+ if ($form->{DUNNING_PDFS} && scalar @{ $form->{DUNNING_PDFS} }) {
+ $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
+ ($pdf_filename, $pdf_content) = DN->melt_pdfs(\%myconfig, $form, $form->{copies}, return_content => $form->{media} ne 'printer');
+
+ flash('info', t8('Dunning Process started for selected invoices!'));
+ if ($form->{media} eq 'printer') {
+ flash('info', t8('The PDF has been printed'));
+ } else {
+ flash('info', t8('The PDF has been created'));
+ }
}
+
# saving the history
if(!exists $form->{addition} && $form->{id} ne "") {
- $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
+ $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
$form->{addition} = "DUNNING STARTED";
- $form->save_history($form->dbconnect(\%myconfig));
+ $form->save_history;
}
- # /saving the history
- $form->redirect($locale->text('Dunning Process started for selected invoices!'));
+ # /saving the history
+
+ setup_dn_status_action_bar();
+ $form->{"title"} = $locale->text("Dunning status");
+ $form->header();
+ print $form->parse_html_template('dunning/status', {
+ pdf_filename => $pdf_filename,
+ pdf_content => $pdf_content,
+ status => \@status, });
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
-
+
sub set_email {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+ my $form = $main::form;
+ my $locale = $main::locale;
- my $callback = "$form->{script}?action=set_email&";
- map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
- (qw(login password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
+ $main::auth->assert('dunning_edit');
- if ($form->{email_attachment}) {
- $form->{email_attachment} = "checked";
- }
$form->{"title"} = $locale->text("Set eMail text");
- $form->header();
+ $form->header(
+ no_layout => 1,
+ use_javascripts => [ qw(ckeditor/ckeditor ckeditor/adapters/jquery) ],
+ );
print($form->parse_html_template("dunning/set_email"));
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub search {
- $lxdebug->enter_sub();
- # setup customer selection
- $form->all_vc(\%myconfig, "customer", "AR");
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ $main::auth->assert('dunning_edit');
+
+ $form->get_lists("customers" => "ALL_CUSTOMERS",
+ "departments" => "ALL_DEPARTMENTS");
+ $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
DN->get_config(\%myconfig, \%$form);
- if (@{ $form->{all_customer} }) {
- map { $customer .= "<option>$_->{name}--$_->{id}\n" }
- @{ $form->{all_customer} };
- $customer = qq|<select name=customer><option>\n$customer</select>|;
- } else {
- $customer = qq|<input name=customer size=35>|;
- }
+ $form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
- # dunning levels
- if (@{ $form->{DUNNING} }) {
- $form->{selectdunning_level} = "<option></option\n";
- map {
- $form->{selectdunning_level} .=
- "<option value=$_->{id}>$_->{dunning_description}</option>\n"
- } (@{ $form->{DUNNING} });
- }
- $dunning_level = qq|
- <tr>
- <th align=right nowrap>| . $locale->text('Next Dunning Level') . qq|</th>
- <td colspan=3><select name=dunning_level>$form->{selectdunning_level}</select></td>
- </tr>
- | if $form->{selectdunning_level};
-
- # departments
- if (@{ $form->{all_departments} }) {
- $form->{selectdepartment} = "<option>\n";
- map {
- $form->{selectdepartment} .=
- "<option>$_->{description}--$_->{id}\n"
- } (@{ $form->{all_departments} });
- }
- $department = qq|
- <tr>
- <th align=right nowrap>| . $locale->text('Department') . qq|</th>
- <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
- </tr>
- | if $form->{selectdepartment};
- $form->{title} = $locale->text('Search Dunning');
- $form->{nextsub} = "show_dunning";
-
- # use JavaScript Calendar or not
- $form->{jsscript} = $jscalendar;
- $jsscript = "";
- if ($form->{jsscript}) {
-
- # with JavaScript Calendar
- $button1 = qq|
- <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
- <input type=button name=transdatefrom id="trigger1" value=|
- . $locale->text('button') . qq|></td>
- |;
- $button2 = qq|
- <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
- <input type=button name=transdateto id="trigger2" value=|
- . $locale->text('button') . qq|></td>
- |;
- $button3 = qq|
- <td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
- <input type=button name=dunningfrom id="trigger3" value=|
- . $locale->text('button') . qq|></td>
- |;
- $button4 = qq|
- <td><input name=dunningto id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
- <input type=button name=dunningto id="trigger4" value=|
- . $locale->text('button') . qq|></td>
- |;
-
- #write Trigger
- $jsscript =
- Form->write_trigger(\%myconfig, "4", "transdatefrom", "BR", "trigger1", "transdateto", "BR", "trigger2", "dunningfrom", "BR", "trigger3", "dunningto", "BR", "trigger4");
- } else {
+ $form->{title} = $locale->text('Dunnings');
- # without JavaScript Calendar
- $button1 =
- qq|<td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
- $button2 =
- qq|<td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
- $button3 =
- qq|<td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
- $button4 =
- qq|<td><input name=dunningfrom id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
- }
+ setup_dn_search_action_bar();
+ $form->header();
- $form->{fokus} = "search.customer";
- $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
- $form->header;
- $onload = qq|focus()|;
- $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
- $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
-
- print qq|
-<body onLoad="$onload">
-
-<form method=post name="search" action=$form->{script}>
-
-<table width=100%>
- <tr><th class=listtop>$form->{title}</th></tr>
- <tr height="5"></tr>
- <tr>
- <td>
- <table>
- <tr>
- <th align=right>| . $locale->text('Customer') . qq|</th>
- <td colspan=3>$customer</td>
- </tr>
- $dunning_level
- $department
- <tr>
- <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
- <td colspan=3><input name=invnumber size=20></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
- <td colspan=3><input name=ordnumber size=20></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
- <td colspan=3><input name=notes size=40></td>
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Invdate from') . qq|</th>
- $button1
- <th align=right nowrap>| . $locale->text('To') . qq|</th>
- $button2
- </tr>
- <tr>
- <th align=right nowrap>| . $locale->text('Dunning Date from') . qq|</th>
- $button3
- <th align=right nowrap>| . $locale->text('To') . qq|</th>
- $button4
- </tr>
-
- </table>
- </td>
- </tr>
- <tr>
- <td><hr size=3 noshade></td>
- </tr>
- <tr>
- <td>
- <table>
- <tr>
- <th align=right nowrap>| . $locale->text('Show old dunnings') . qq|</th>
- <td><input type=checkbox value=1 name=showold></td>
- </tr>
- </table>
- </td>
- </tr>
-</table>
-
-<input type=hidden name=nextsub value=$form->{nextsub}>
-
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
-
-<br>
-<input class=submit type=submit name=action value="|
- . $locale->text('Continue') . qq|">
-
-</form>
-
-</body>
-
-$jsscript
-
-</html>
-|;
-
- $lxdebug->leave_sub();
+ print $form->parse_html_template("dunning/search");
+
+ $main::lxdebug->leave_sub();
}
sub show_dunning {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+ my $cgi = $::request->{cgi};
+
+ $main::auth->assert('dunning_edit');
+
+ my @filter_field_list = qw(customer_id customer dunning_id dunning_level department_id invnumber ordnumber
+ transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id
+ l_mails l_webdav l_documents);
+
+ report_generator_set_default_sort('customername', 1);
DN->get_dunning(\%myconfig, \%$form);
- $form->{title} = $locale->text('Dunning overview');
-
-
-
-
-
- $form->{callback} =
- "$form->{script}?action=show_dunning&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&dunningfrom=$form->{dunningfrom}&dunningto=$form->{dunningto}¬es=$form->{notes}&showold=$form->{showold}&dunning_level=$form->{dunning_level}"
- unless $form->{callback};
-
- @column_index = qw(dunning_description customername invnumber invdate inv_duedate invamount dunning_date next_duedate fee interest );
-
- $column_header{dunning_description} =
- qq|<th class=listheading>|
- . $locale->text('Dunning Level')
- . qq|</th>|;
- $column_header{customername} =
- qq|<th class=listheading>|
- . $locale->text('Customername')
- . qq|</th>|;
- $column_header{invnumber} =
- qq|<th class=listheading>|
- . $locale->text('Invnumber')
- . qq|</th>|;
- $column_header{inv_duedate} =
- qq|<th class=listheading>|
- . $locale->text('Invoice Duedate')
- . qq|</th>|;
- $column_header{dunning_date} =
- qq|<th class=listheading>|
- . $locale->text('Dunning Date')
- . qq|</th>|;
- $column_header{next_duedate} =
- qq|<th class=listheading>|
- . $locale->text('Dunning Duedate')
- . qq|</th>|;
- $column_header{invdate} =
- qq|<th class=listheading>|
- . $locale->text('Invdate')
- . qq|</th>|;
- $column_header{invamount} =
- qq|<th class=listheading>|
- . $locale->text('Amount')
- . qq|</th>|;
- $column_header{fee} =
- qq|<th class=listheading>|
- . $locale->text('Total Fees')
- . qq|</th>|;
- $column_header{interest} =
- qq|<th class=listheading>|
- . $locale->text('Total Interest')
- . qq|</th>|;
-
- $form->header;
-
-
- print qq|
-<body>
-<script type="text/javascript" src="js/common.js"></script>
-<script type="text/javascript" src="js/dunning.js"></script>
-<form method=post action=$form->{script}>
-
-
-<table width=100%>
- <tr>
- <th class=listtop colspan=10>$form->{title}</th>
- </tr>
- <tr height="5"></tr>
- <tr>|;
- map { print "$column_header{$_}\n" } @column_index;
-
- print qq|
- </tr>
-|;
-
- my %columns = (
- "dunning_duedate" => "next_duedate",
- "duedate" => "inv_duedate",
- "transdate" => "invdate",
- "amount" => "invamount",
- );
+
+ if (!$form->{callback}) {
+ $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
+ }
+
+ $form->get_lists('printers' => 'printers',
+ 'languages' => 'languages');
+
+ $form->{type} = 'dunning';
+ $form->{PRINT_OPTIONS} = print_options('inline' => 1,
+ 'no_queue' => 1,
+ 'no_postscript' => 1,
+ 'no_html' => 1,
+ 'no_opendocument' => 1,);
+ $form->{title} = $locale->text('Dunning overview');
+
+ my $report = SL::ReportGenerator->new(\%myconfig, $form);
+
+ $report->set_options('std_column_visibility' => 1,
+ 'title' => $form->{title});
+ $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
+
+ my %column_defs = (
+ 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
+ 'dunning_description' => { 'text' => $locale->text('Dunning Level') },
+ 'customername' => { 'text' => $locale->text('Customername') },
+ 'departmentname' => { 'text' => $locale->text('Department') },
+ 'language' => { 'text' => $locale->text('Language') },
+ 'invnumber' => { 'text' => $locale->text('Invnumber') },
+ 'transdate' => { 'text' => $locale->text('Invdate') },
+ 'duedate' => { 'text' => $locale->text('Invoice Duedate') },
+ 'amount' => { 'text' => $locale->text('Amount') },
+ 'dunning_id' => { 'text' => $locale->text('Dunning number') },
+ 'dunning_date' => { 'text' => $locale->text('Dunning Date') },
+ 'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
+ 'fee' => { 'text' => $locale->text('Total Fees') },
+ 'interest' => { 'text' => $locale->text('Interest') },
+ 'salesman' => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
+ 'documents' => { 'text' => $locale->text('Documents'), 'visible' => $form->{l_documents}? 1 : 0 },
+ 'webdav' => { 'text' => $locale->text('WebDAV'), 'visible' => $form->{l_webdav} ? 1 : 0 },
+ 'mails' => { 'text' => $locale->text('Mails'), 'visible' => $form->{l_mails} ? 1 : 0 },
+ );
+
+ $report->set_columns(%column_defs);
+ $report->set_column_order(qw(checkbox dunning_description dunning_id customername language invnumber transdate
+ duedate amount dunning_date dunning_duedate fee interest salesman departmentname mails webdav documents));
+ $report->set_sort_indicator($form->{sort}, $form->{sortdir});
+
+ my $edit_url = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
+ my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
+ my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
+
+ foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman dunning_id)) {
+ my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
+ $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
+ }
+
+ my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman dunning_id);
+
+ my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
+
+ $current_dunning_rows = [];
+ $first_row_for_dunning = 1;
+ $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
my $i = 0;
- my $j = 0;
- my ($previous_dunning_id, $first_row_for_dunning);
- foreach $ref (@{ $form->{DUNNINGS} }) {
+
+ foreach my $ref (@{ $form->{DUNNINGS} }) {
$i++;
if ($previous_dunning_id != $ref->{dunning_id}) {
- $j++;
- $j = $j % 2;
+ $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
+ $current_dunning_rows = [];
$first_row_for_dunning = 1;
- } else {
- $first_row_for_dunning = 0;
}
- $previous_dunning_id = $ref->{dunning_id};
- print qq|
- <tr valign=top class=listrow$j>
-|;
+ if ($ref->{'language_id'}) {
+ $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
+ }
+
+ my $row = { };
+ foreach my $column (keys %{ $ref }) {
+ $row->{$column} = {
+ 'data' => $first_row_for_dunning || (none { $_ eq $column } qw(dunning_description customername dunning_id)) ? $ref->{$column} : '',
-
+ 'align' => $alignment{$column},
- foreach (qw(dunning_date dunning_duedate duedate transdate customername
- amount fee interest)) {
- my $col = $columns{$_} ? $columns{$_} : $_;
- $column_data{$col} = "<td>" . H($ref->{$_}) . "</td>";
+ 'link' => ( $column eq 'invnumber' ? $edit_url->($ref)
+ : $column eq 'dunning_description' ? $print_url->($ref)
+ : $column eq 'dunning_id' ? $print_url->($ref)
+ : ''),
+ };
}
+ $row->{checkbox} = !$first_row_for_dunning ? { } : {
+ 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
+ . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
+ 'valign' => 'center',
+ 'align' => 'center',
+ };
+
if ($first_row_for_dunning) {
- $column_data{dunning_description} =
- qq|<td><a href="dn.pl?action=print_dunning&format=pdf&media=screen&| .
- qq|dunning_id=| . E($ref->{dunning_id}) .
- join("", map({ "&${_}=" . E($form->{$_}) } qw(login password callback))) .
- qq|">| . H($ref->{dunning_description}) . qq|</a></td>|;
+ $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
+ . " $ref->{language}" };
} else {
- $column_data{dunning_description} = qq|<td> </td>|;
- $column_data{customername} = qq|<td> </td>|;
+ $row->{language} = { };
+ }
+
+ if ($form->{l_documents} && $first_row_for_dunning) {
+ my @files = SL::File->get_all_versions(object_id => $ref->{dunning_id},
+ object_type => 'dunning',
+ file_type => 'document',);
+ if (scalar @files) {
+ my $html = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
+ my $text = join "\n", map { $_->file_name } @files;
+ $row->{documents} = { 'raw_data' => $html, data => $text };
+ } else {
+ $row->{documents} = { };
+ }
+ }
+ if ($form->{l_webdav} && $first_row_for_dunning) {
+ my $webdav = SL::Webdav->new(
+ type => 'dunning',
+ number => $ref->{dunning_id},
+ );
+ my @all_objects = $webdav->get_all_objects;
+ if (scalar @all_objects) {
+ my $html = join '<br>', map { SL::Presenter::WebdavObject::webdav_object($_) } @all_objects;
+ my $text = join "\n", map { $_->filename } @all_objects;
+ $row->{webdav} = { 'raw_data' => $html, data => $text };
+ } else {
+ $row->{webdav} = { };
+ }
}
- $column_data{invnumber} =
- qq|<td><a href="| . ($ref->{invoice} ? "is.pl" : "ar.pl" ) .
- qq|?action=edit&id=| . H($ref->{id}) .
- join("", map({ "&${_}=" . E($form->{$_}) } qw(login password callback))) .
- qq|">| . H($ref->{invnumber}) . qq|</a></td>|;
+ if ($form->{l_mails}) {
+ my @mail_links = RecordLinks->get_links(from_table => 'dunning', to_table => 'email_journal', from_id => $ref->{dunning_table_id});
+ if (scalar @mail_links) {
+ my $email_journals = SL::DB::Manager::EmailJournal->get_all(where => [id => [ map { $_->{to_id} } @mail_links ]]);
+ my $html = join '<br>', map { SL::Presenter::EmailJournal::email_journal($_) } @$email_journals;
+ my $text = join "\n", map { $_->subject } @$email_journals;
+ $row->{mails} = { 'raw_data' => $html, data => $text };
+ } else {
+ $row->{mails} = { };
+ }
+ }
- map { print "$column_data{$_}\n" } @column_index;
+ push @{ $current_dunning_rows }, $row;
- print qq|
- </tr>
-|;
+ $previous_dunning_id = $ref->{dunning_id};
+ $first_row_for_dunning = 0;
}
- $form->{rowcount} = $i;
+ $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
+
+ $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
+ 'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
+ 'output_format' => 'HTML',
+ 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
+ );
- print qq|
- </table>
- </td>
- </tr>
- <tr>
- <td><hr size=3 noshade></td>
- </tr>
-</table>
+ $report->set_options_from_form();
-<br>
-<form method=post action=$form->{script}>
+ setup_dn_show_dunning_action_bar();
+ $report->generate_with_headers();
-<input name=callback type=hidden value="$form->{callback}">
-<input name=rowcount type=hidden value="$form->{rowcount}">
-<input name=nextsub type=hidden value="$form->{nextsub}">
+ $main::lxdebug->leave_sub();
+}
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
+sub print_dunning {
+ $main::lxdebug->enter_sub();
- </form>
+ my $form = $main::form;
- </body>
- </html>
-|;
+ $main::auth->assert('dunning_edit');
+ $form->{rowcount} = 1;
+ $form->{selected_1} = 1;
+ $form->{dunning_id_1} = $form->{dunning_id};
+ $form->{language_id_1} = $form->{language_id};
- $lxdebug->leave_sub();
+ print_multiple();
+ $main::lxdebug->leave_sub();
}
-sub print_dunning {
- $lxdebug->enter_sub();
+sub delete {
+ $main::auth->assert('dunning_edit');
+
+ my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
+
+ if (!scalar @dunning_ids) {
+ $::form->error($::locale->text('No dunnings have been selected for printing.'));
+ }
+
+ my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
+
+ SL::DB::Dunning->new->db->with_transaction(sub {
+ for my $dunning (@$dunnings) {
+ SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
+ $dunning->delete;
+ }
+ });
+
+ flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
+
+ search();
+}
+
+sub print_multiple {
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ $main::auth->assert('dunning_edit');
+
+ $form->{title} = $locale->text('Print dunnings');
+
+ my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
+ my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
+
+ if (!scalar @dunning_ids) {
+ $form->error($locale->text('No dunnings have been selected for printing.'));
+ }
- DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
+ $form->{DUNNING_PDFS} = [];
+
+ my $saved_language_id = $form->{language_id};
+ my $i = 0;
+ foreach my $dunning_id (@dunning_ids) {
+ if (!$form->{force_lang}) {
+ $form->{language_id} = $language_ids[$i];
+ }
+ $form->{dunning_id} = $dunning_id;
+ DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
+ DN->print_dunning(\%myconfig, $form, $dunning_id);
+
+ # print original dunned invoices, if they where printed on dunning run
+ my $dunnings = SL::DB::Manager::Dunning->get_all(where => [dunning_id => $dunning_id, original_invoice_printed => 1]);
+ DN->print_original_invoice(\%myconfig, $form, $dunning_id, $_->trans_id) for @$dunnings;
+
+ $i++;
+ }
+ $form->{language_id} = $saved_language_id;
+
+ if (scalar @{ $form->{DUNNING_PDFS} }) {
+ $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
+ DN->melt_pdfs(\%myconfig, $form, $form->{copies});
+
+ if ($form->{media} eq 'printer') {
+ $form->header();
+ $form->info($locale->text('The dunnings have been printed.'));
+ }
- if($form->{DUNNING_PDFS}) {
- DN->melt_pdfs(\%myconfig, \%$form,$spool);
} else {
- $form->redirect($locale->text('Could not create dunning copy!'));
+ $form->redirect($locale->text('Could not print dunning.'));
}
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
+}
+sub continue {
+ call_sub($main::form->{nextsub});
}
-# end of main
+sub dispatcher {
+ foreach my $action (qw(delete print_multiple)) {
+ if ($::form->{"action_${action}"}) {
+ call_sub($action);
+ return;
+ }
+ }
+
+ $::form->error($::locale->text('No action defined.'));
+}
+
+sub setup_dn_add_action_bar {
+ my %params = @_;
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ t8('Search'),
+ submit => [ '#form', { action => "show_invoices" } ],
+ accesskey => 'enter',
+ ],
+ );
+ }
+}
+
+sub setup_dn_show_invoices_action_bar {
+ my %params = @_;
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ t8('Create'),
+ submit => [ '#form', { action => "save_dunning" } ],
+ checks => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
+ accesskey => 'enter',
+ only_once => 1,
+ ],
+ );
+ }
+}
+
+sub setup_dn_search_action_bar {
+ my %params = @_;
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ t8('Search'),
+ submit => [ '#form', { action => "show_dunning" } ],
+ accesskey => 'enter',
+ ],
+ );
+ }
+}
+
+sub setup_dn_show_dunning_action_bar {
+ my %params = @_;
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ t8('Print'),
+ submit => [ '#form', { action => "print_multiple" } ],
+ checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
+ accesskey => 'enter',
+ ],
+
+ action => [
+ t8('Delete'),
+ submit => [ '#form', { action => "delete" } ],
+ checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
+ confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
+ ],
+ );
+ }
+}
+
+sub setup_dn_edit_config_action_bar {
+ my %params = @_;
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ t8('Save'),
+ submit => [ '#form', { action => "save" } ],
+ accesskey => 'enter',
+ ],
+ );
+ }
+}
+
+sub setup_dn_status_action_bar {
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ t8('Back'),
+ link => $::form->{callback},
+ accesskey => 'enter',
+ ],
+ );
+ }
+
+}
+
+# end of main