View | Details | Raw Unified | Return to bug 12772
Collapse All | Expand All

(-)a/C4/Overdues.pm (-7 / +7 lines)
Lines 766-774 returns a list of branch codes for branches with overdue rules defined. Link Here
766
sub GetBranchcodesWithOverdueRules {
766
sub GetBranchcodesWithOverdueRules {
767
    my $dbh               = C4::Context->dbh;
767
    my $dbh               = C4::Context->dbh;
768
    my $branchcodes = $dbh->selectcol_arrayref(q|
768
    my $branchcodes = $dbh->selectcol_arrayref(q|
769
        SELECT DISTINCT(branchcode)
769
        SELECT DISTINCT b.branchcode
770
        FROM overduerules
770
        FROM branches b, overduerules r
771
        WHERE delay1 IS NOT NULL
771
        WHERE b.branchcode = r.branchcode or r.branchcode = ''
772
        ORDER BY branchcode
772
        ORDER BY branchcode
773
    |);
773
    |);
774
    if ( $branchcodes->[0] eq '' ) {
774
    if ( $branchcodes->[0] eq '' ) {
Lines 935-948 sub RemoveNotifyLine { Link Here
935
=cut
935
=cut
936
936
937
sub GetOverdueMessageTransportTypes {
937
sub GetOverdueMessageTransportTypes {
938
    my ( $branchcode, $categorycode, $letternumber ) = @_;
938
    my ( $overduerules_id ) = @_;
939
    return unless $categorycode and $letternumber;
939
    return unless $overduerules_id;
940
    my $dbh = C4::Context->dbh;
940
    my $dbh = C4::Context->dbh;
941
    my $sth = $dbh->prepare("
941
    my $sth = $dbh->prepare("
942
        SELECT message_transport_type FROM overduerules_transport_types
942
        SELECT message_transport_type FROM overduerules_transport_types
943
        WHERE branchcode = ? AND categorycode = ? AND letternumber = ?
943
        WHERE overduerules_id = ?
944
    ");
944
    ");
945
    $sth->execute( $branchcode, $categorycode, $letternumber );
945
    $sth->execute( $overduerules_id );
946
    my @mtts;
946
    my @mtts;
947
    while ( my $mtt = $sth->fetchrow ) {
947
    while ( my $mtt = $sth->fetchrow ) {
948
        push @mtts, $mtt;
948
        push @mtts, $mtt;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/overduerules.tt (-153 / +226 lines)
Lines 2-178 Link Here
2
[% INCLUDE 'doc-head-open.inc' %]
2
[% INCLUDE 'doc-head-open.inc' %]
3
<title>Koha &rsaquo; Tools &rsaquo; Notice triggers</title>
3
<title>Koha &rsaquo; Tools &rsaquo; Notice triggers</title>
4
[% INCLUDE 'doc-head-close.inc' %]
4
[% INCLUDE 'doc-head-close.inc' %]
5
5
<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
6
[% INCLUDE 'datatables.inc' %]
6
<script type="text/javascript">
7
<script type="text/javascript">
7
//<![CDATA[
8
//<![CDATA[
8
var tab_map = { "1" : _("First"), "2" : _("Second"), "3" : _("Third")};
9
function clear_edit(){
9
$(document).ready(function() {
10
//    var cancel = confirm(_("Are you sure you want to cancel your changes?"));
10
    $('#selectlibrary').find("input:submit").hide();
11
//    if ( !cancel ) return;
11
    $('#branch').change(function() {
12
    edit_value_add(0); // reset add value
12
            $('#selectlibrary').submit();
13
    $('#table_overduerules td').removeClass('highlighted-row');
14
    var edit_row = $("#edit_row");
15
    $(edit_row).find("input").each(function(){
16
        var type = $(this).attr("type");
17
        if (type != "button" && type != "submit" ) {
18
            $(this).val("");
19
            $(this).removeAttr("disabled");
20
        }
21
        if (type == "checkbox"){
22
            row = document.getElementById('edit_row');
23
            if ($(this).prop('outerHTML').contains('checked="checked"')) {
24
                var newOuterHTML = $(this).prop('outerHTML').replace(/checked="checked"/, '');
25
                $(this).prop('outerHTML', newOuterHTML);
26
                $(this).checked = true;
27
            }
28
        }
13
    });
29
    });
14
    $("li>a.tab").each( function(){
30
    $(edit_row).find("select").removeAttr("disabled");
15
        var id = $(this).attr("data-number");
31
    $(edit_row).find("select option:first").attr("selected", "selected");
16
        $(this).html(tab_map[id]);
32
    $(edit_row).find("td:last input[name='clear']").remove();
17
    } );
33
}
18
    $('#rulestabs').tabs();
34
19
});
35
function edit_value_add(id_overduerules) {
36
    var input = document.getElementById('edit_id');
37
    if (id_overduerules != 0) {
38
        input.value = "add&overduerules_id=" + id_overduerules;
39
    } else {
40
        input.value = "add";
41
    }
42
}
43
44
     $(document).ready(function() {
45
        $('#icons').tabs();
46
        $("#table_overduerules").dataTable($.extend(true, {}, dataTablesDefaults, {
47
            "aoColumnDefs": [
48
                { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false },
49
            ],
50
            "aaSorting": [[ 2, "asc" ]],
51
            "iDisplayLength": 100
52
        }));
53
        $( "#itemtypeentry" ).validate({
54
            rules: {
55
                itemtype: { required: true },
56
                description: { required: true },
57
                rentalcharge: { number: true }
58
            }
59
        });
60
        $(".editrule").click(function(){
61
            if ( $(edit_row).find("input[type='text'][value!='']").length > 0 ) {
62
                var edit = confirm(_("Are you sure you want to edit another rule?"));
63
                if (!edit) return false;
64
                else clear_edit(); // reset header
65
            }
66
            $('#table_overduerules td').removeClass('highlighted-row');
67
            $(this).parent().parent().find("td").each(function (i) {
68
                $(this).addClass('highlighted-row');
69
                itm = $(this).text();
70
                itm = itm.replace(/^\s*|\s*$/g,'');
71
                var current_column = $("#edit_row td:eq("+i+")");
72
                $(current_column).find("input[type='text']").val(itm);
73
                // select the corresponding option
74
                $(current_column).find("select option").each(function(){
75
                    opt = $(this).text().toLowerCase();
76
                    opt = opt.replace(/^\s*|\s*$/g,'');
77
                    if ( opt == itm.toLowerCase() ) {
78
                        $(this).attr('selected', 'selected');
79
                    }
80
                });
81
                if(($(this)[0].innerHTML).contains('checked')) {
82
                    row = document.getElementById('edit_row');
83
                    row.cells[i].innerHTML = row.cells[i].innerHTML.replace(/>/, 'checked="checked">');
84
                }
85
            });
86
            $("#table_overduerules tr:last td:eq(0) select").attr('disabled', 'disabled');
87
            $("#table_overduerules tr:last td:eq(1) select").attr('disabled', 'disabled');
88
            $("#table_overduerules tr:last td:eq(2) select").attr('disabled', 'disabled');
89
            $("#table_overduerules tr:last td:eq(3) select").attr('disabled', 'disabled');
90
            return false;
91
        });
92
     });
20
//]]>
93
//]]>
21
</script>
94
</script>
22
95
<style type="text/css">
96
fieldset.rows div.toptabs li { clear:none;margin-right:.5em;padding-bottom:0;width:auto; }
97
fieldset.rows div.toptabs .ui-tabs-nav li.ui-tabs-active {background-color : #F4F8F9; }
98
fieldset.rows .ui-tabs-panel { margin-right : 10px; margin-left : 10px;margin-bottom:10px;}
99
fieldset.rows .ui-tabs-nav { margin-left : 10px; }
100
</style>
23
</head>
101
</head>
24
<body id="tools_overduerules" class="tools">
102
<body id="tools_overduerules" class="admin">
25
[% INCLUDE 'header.inc' %]
103
[% INCLUDE 'header.inc' %]
26
[% INCLUDE 'cat-search.inc' %]
104
[% INCLUDE 'cat-search.inc' %]
27
105
28
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a> &rsaquo; Notice triggers</div>
106
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a> &rsaquo;
107
</div>
29
108
30
<div id="doc3" class="yui-t2">
109
<div id="doc3" class="yui-t2">
31
   
110
   
32
   <div id="bd">
111
   <div id="bd">
33
    <div id="yui-main">
112
     <div id="yui-main">
34
    <div class="yui-b">
113
       <div class="yui-b">
35
114
36
        <h1>Defining [% IF ( branch ) %]overdue actions for [% branch %][% ELSE %]default overdue actions[% END %]</h1>
115
<form method="post" action="/cgi-bin/koha/tools/overduerules.pl">
37
        <div class="help">
116
<input  id="edit_id" type="hidden" name="op" value="add" />
38
            <p>Delay is the number of days after an checkout is due before an action is triggered. </p>
117
<input type="hidden" name="edit_overduerules_id" value="0" />
39
            <p>If you want Koha to trigger an action (send a letter or restrict a member), a delay value is required.</p>
118
<table id="table_overduerules">
40
            <p>Columns must be filled left to right: if the first column is blank, other columns will be ignored. </p>
119
  <thead>
41
        </div>
120
    <tr>
42
            <form method="post" action="/cgi-bin/koha/tools/overduerules.pl" id="selectlibrary">
121
        <th>Branch</th>
43
            <label for="branch">Select a library:</label>
122
        <th>Category</th>
44
                <select id="branch" name="branch">
123
        <th>Item type</th>
45
                    <option value="">Default</option>
124
        <th>On hold</th>
46
                    [% FOREACH branchloo IN branchloop %]
125
        <th>Delay</th>
47
                        [% IF ( branchloo.selected ) %]
126
        <th>Letter</th>
48
                            <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
127
        <th>Restrict</th>
49
                        [% ELSE %]
128
        [% FOREACH mtt IN message_transport_types %]
50
                            <option value="[% branchloo.value %]">[% branchloo.branchname %]</option>
129
        <th scpre="col">
51
                        [% END %]
130
            [% SWITCH mtt %]
52
                    [% END %]
131
            [% CASE 'email' %]Email
53
                </select>
132
            [% CASE 'print' %]Print
54
                <input type="submit" value="Select" />
133
            [% CASE 'sms' %]SMS
55
            </form>
134
            [% CASE 'feed' %]Feed
56
            [% IF ( ERROR ) %]
135
            [% CASE 'phone' %]Phone
57
            <div class="dialog alert">
136
            [% CASE %]% mtt %]
58
                <h3>Data error</h3>
59
                <p>The following errors were found. Please correct them and submit again:</p>
60
                <ul>
61
                [% IF ( ERRORDELAY ) %]
62
                    <li>Delay [% ERRORDELAY %] for [% BORERR %] patron category has some unexpected characters. There should be only numerical characters. </li>
63
                [% END %]
64
                [% IF ( ERRORUSELESSDELAY ) %]
65
                    <li>No letter or restriction action specified for delay [% ERRORUSELESSDELAY %] for [% BORERR %] patron category.  If a delay is supplied, either a letter, restrict action, or both should be specified.</li>
66
                [% END %]
67
                [% IF ( ERRORORDER ) %]
68
                    <li>The first notice's delay should be less than the second, which should be less than the third for the <strong>[% BORERR %]</strong> patron category </li>
69
                [% END %]
70
                </ul>
71
            </div>
72
            [% END %]
137
            [% END %]
73
            [% IF ( table ) %]
138
        </th>
74
            <form method="post" action="/cgi-bin/koha/tools/overduerules.pl">
75
                <input type="hidden" name="op" value="save" />
76
                <input type="hidden" name="branch" value="[% branch %]" />
77
				<h3>Rules for overdue actions: [% IF ( branch ) %][% branch %][% ELSE %] default library [% END %]</h3>
78
                    [% IF ( datasaved ) %]<div class="dialog message">INPUT SAVED</div> [% END %]
79
80
  <div id="rulestabs" class="toptabs">
81
    <ul class="ui-tabs-nav">
82
      [% FOR tab IN tabs %]
83
          <li><a href="#[% tab.id %]" class="tab [% tab.id %]" data-number="[% tab.number %]"></a></li>
84
      [% END %]
85
    </ul>
86
    [% FOR tab IN tabs %]
87
      <div id="[% tab.id %]">
88
        <table>
89
          <thead>
90
            <tr>
91
              <th>&nbsp;</th>
92
              <th scope="col">Delay</th>
93
              <th scope="col">Letter</th>
94
              <th scope="col">Restrict</th>
95
              [% FOREACH mtt IN message_transport_types %]
96
                <th scpre="col">
97
                  [% SWITCH mtt %]
98
                  [% CASE 'email' %]
99
                    Email
100
                  [% CASE 'print' %]
101
                    Print
102
                  [% CASE 'sms' %]
103
                    SMS
104
                  [% CASE 'feed' %]
105
                    Feed
106
                  [% CASE 'phone' %]
107
                    Phone
108
                  [% CASE %]
109
                    [% mtt %]
110
                  [% END %]
111
                </th>
112
              [% END %]
113
            </tr>
114
          </thead>
115
          <tbody>
116
          [% FOREACH value IN tab.values %]
117
            <tr>
118
              <th scope="row">[% value.line %]</th>
119
              <td>
120
              <input type="text" name="delay[% tab.number %]-[% value.overduename %]" size="5" value="[% value.delay %]" />
121
              </td>
122
              <td>
123
                <select name="letter[% tab.number %]-[% value.overduename %]">
124
                  <option value="">No notice</option>
125
                  [% FOREACH letter IN letters %]
126
                    [% IF letter.code == value.selected_lettercode %]
127
                      <option value="[% letter.code %]" selected="selected">[% letter.name %]</option>
128
                    [% ELSE %]
129
                      <option value="[% letter.code %]">[% letter.name %]</option>
130
                    [% END %]
131
                  [% END %]
132
                </select>
133
              </td>
134
              <td>
135
              [% IF ( value.debarred ) %]
136
                <input type="checkbox" name="debarred[% tab.number %]-[% value.overduename %]" checked="checked" value="1" />
137
              [% ELSE %]
138
                <input type="checkbox" name="debarred[% tab.number %]-[% value.overduename %]" value="1" />
139
              [% END %]
140
              </td>
141
              [% FOREACH mtt IN value.message_transport_types %]
142
                <td>
143
                  [% IF mtt.selected %]
144
                    <input type="checkbox" name="mtt[% tab.number %]-[% value.overduename %]" value="[% mtt.value %]" checked="checked" />
145
                  [% ELSE %]
146
                    [% IF mtt.value == "sms" and not Koha.Preference("SMSSendDriver") %]
147
                      <input type="checkbox" name="mtt[% tab.number %]-[% value.overduename %]" value="[% mtt.value %]" disabled="disabled" />
148
                    [% ELSIF mtt.value == "phone" and not Koha.Preference("TalkingTechItivaPhoneNotification") %]
149
                      <input type="checkbox" name="mtt[% tab.number %]-[% value.overduename %]" value="[% mtt.value %]" disabled="disabled" />
150
                    [% ELSE %]
151
                      <input type="checkbox" name="mtt[% tab.number %]-[% value.overduename %]" value="[% mtt.value %]" />
152
                    [% END %]
153
                  [% END %]
154
                </td>
155
              [% END %]
156
            </tr>
157
          [% END %]
158
          </tbody>
159
        </table>
160
      </div>
161
    [% END %]
139
    [% END %]
140
        <th>Action</th>
141
    </tr>
142
  </thead>
143
  <thead>
144
    <tr id="edit_row">
145
        <td>
146
            <select name="branchcode" id="branchcode">
147
                <option value="">All</option>
148
            [% FOREACH b IN branches %]
149
                <option value="[% b.value.branchcode %]">[% b.value.branchname %]</option>
150
            [% END %]
151
            </select>
152
        </td>
153
        <td>
154
            <select name="categorycode" id="categorycode">
155
                <option value="">All</option>
156
            [% FOREACH c IN categories %]
157
                <option value="[% c.value.categorycode %]">[% c.value.description %]</option>
158
            [% END %]
159
            </select>
160
        </td>
161
        <td>
162
            <select name="itemtype" id="itemtype" style="width:13em;">
163
                <option value="">All</option>
164
            [% FOREACH it IN itemtypes %]
165
                <option value="[% it.value.itemtype %]">[% it.value.description %]</option>
166
            [% END %]
167
            </select>
168
        </td>
169
        <td><input type="checkbox" name="onhold" value="1" /></td>
170
        <td><input type="text" name="delay" id="delay" size="3" /></td>
171
        <td>
172
            <select name="letter" id="letter" style="width:13em;">
173
            <option value="">No notice</option>
174
            [% FOREACH letter IN letters %]
175
                <option value="[% letter.value.code %]">[% letter.value.name %]</option>
176
            [% END %]
177
            </select>
178
        </td>
179
        <td>
180
            <input type="checkbox" name="debarred" value="1" />
181
        </td>
182
        [% FOREACH mtt IN message_transport_types %]
183
        <td>
184
            <input type="checkbox" name="mtt-[% mtt %]" value="1" />
185
        </td>
186
        [% END %]
187
        <td colspan="2">
188
            <input type="hidden" name="branch" value="[% current_branch %]"/>
189
            <input type="submit" value="Save" class="submit" />
190
            <input type="button" name="cancel" value="Clear" onclick="clear_edit();return false;" />
191
        </td>
192
    </tr>
193
  </thead>
194
  <tbody>
195
  [% FOREACH loo IN loop %]
196
    [% UNLESS ( loop.odd ) %]
197
    <tr class="highlight">[% ELSE %]<tr>[% END %]
198
    <td>[% IF loo.branchname == '' %]All[% ELSE %][% loo.branchname %][% END %]</td>
199
    <td>[% IF loo.categorydescription == '' %]All[% ELSE %][% loo.categorydescription %][% END %]</td>
200
    <td>[% IF loo.itemtypedescription == '' %]All[% ELSE %][% loo.itemtypedescription %][% END %]</td>
201
    <td>[% IF loo.onhold %]
202
        <input type="checkbox" name="onhold[% loo.overduerules_id %]" checked="checked" value="1" disabled="disabled"/>
203
        [% ELSE %]
204
        <input type="checkbox" name="onhold[% loo.overduerules_id %]" value="1" disabled="disabled"/>
205
        [% END %]
206
    </td>
207
    <td>[% loo.delay %]</td>
208
    <td>[% IF loo.lettername %][% loo.lettername %][% ELSE %]No notice[% END %]</td>
209
    <td>[% IF loo.debarred %]
210
        <input type="checkbox" name="debarred[% loo.overduerules_id %]" checked="checked" value="1" disabled="disabled"/>
211
        [% ELSE %]
212
        <input type="checkbox" name="debarred[% loo.overduerules_id %]" value="1" disabled="disabled"/>
213
        [% END %]
214
    </td>
215
    [% FOREACH mtt IN loo.message_transport_types %]
216
    <td>
217
        [% IF mtt.selected %]
218
        <input type="checkbox" name="mtt[% loo.overduerules_id %]-[% mtt.value %]" value="[% mtt.value %]" checked="checked" disabled="disabled"/>
219
        [% ELSE %]
220
            [% IF mtt.value == "sms" and not Koha.Preference("SMSSendDriver") %]
221
            <input type="checkbox" name="mtt[% loo.overduerules_id %]-[% mtt.value %]" value="[% mtt.value %]" disabled="disabled" />
222
            [% ELSIF mtt.value == "phone" and not Koha.Preference("TalkingTechItivaPhoneNotification") %]
223
            <input type="checkbox" name="mtt[% loo.overduerules_id %]-[% mtt.value %]" value="[% mtt.value %]" disabled="disabled" />
224
            [% ELSE %]
225
            <input type="checkbox" name="mtt[% loo.overduerules_id %]-[% mtt.value %]" value="[% mtt.value %]" disabled="disabled"/>
226
            [% END %]
227
        [% END %]
228
    </td>
229
    [% END %]
230
    <td>
231
      <a href="#" class="editrule" onclick="edit_value_add([% loo.overduerules_id %])">Edit</a>
232
      <a href="[% loo.script_name %]?op=delete&amp;overduerules_id=[% loo.overduerules_id %]">Delete</a>
233
    </td>
234
    </tr>
235
  [% END %]
236
  </tbody>
237
</table>
238
</form>
162
239
240
<div class="pages">[% pagination_bar %]</div>
163
241
164
</div>
165
242
166
                <fieldset class="action"><input type="submit" value="Save changes" /></fieldset>
243
167
            </form>
244
168
            [% ELSE %]
245
</div>
169
                <div class="warning">You don't have patron categories defined, or the patron categories are not set to receive overdue notices.</div>
246
</div>
170
                <div class="warning">If this is not what you were expecting, go to <a href="../admin/categorie.pl">patron categories</p></div>
247
<div class="yui-b">
171
            [% END %]
248
[% INCLUDE 'tools-menu.inc' %]
172
    </div>
249
</div>
173
    </div>
174
    <div class="yui-b noprint">
175
        [% INCLUDE 'tools-menu.inc' %]
176
    </div>
177
</div>
250
</div>
178
[% INCLUDE 'intranet-bottom.inc' %]
251
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/tools/overduerules.pl (-266 / +128 lines)
Lines 1-6 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Copyright 2000-2002 Katipo Communications
3
# Copyright 2014 Solutions inLibro
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 20-308 Link Here
20
use strict;
20
use strict;
21
use warnings;
21
use warnings;
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use List::Util qw/min/;
24
use File::Spec;
25
use C4::Koha;
23
use C4::Context;
26
use C4::Context;
24
use C4::Output;
25
use C4::Auth;
27
use C4::Auth;
26
use C4::Koha;
28
use C4::Output;
27
use C4::Branch;
28
use C4::Letters;
29
use C4::Letters;
29
use C4::Members;
30
use C4::Branch;
30
use C4::Overdues;
31
use C4::Overdues;
32
use Data::Dumper;
33
34
sub StringSearch {
35
    my ( $searchstring, $type ) = @_;
36
    my $dbh = C4::Context->dbh;
37
    $searchstring =~ s/\'/\\\'/g;
38
    my @data = split( ' ', $searchstring );
39
    my $sth = $dbh->prepare(
40
        "SELECT * FROM overduerules WHERE (branchcode LIKE ?) or (categorycode LIKE ?) or (letter LIKE ?) or (itemtype LIKE ?) ORDER BY branchcode, delay"
41
    );
42
    $sth->execute("$data[0]%","$data[0]%","$data[0]%","$data[0]%");
43
    return $sth->fetchall_arrayref({}); # return ref-to-array of ref-to-hashes
44
                                        # like [ fetchrow_hashref(), fetchrow_hashref() ... ]
45
}
31
46
32
our $input = new CGI;
47
my $input       = new CGI;
33
my $dbh = C4::Context->dbh;
48
my $searchfield = $input->param('description');
34
49
my $script_name = "/cgi-bin/koha/tools/overduerules.pl";
35
my @categories = @{$dbh->selectall_arrayref(
50
my $op          = $input->param('op');
36
    'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
51
my $overduerules_id = $input->param('overduerules_id');
37
    { Slice => {} }
52
my $branch      = $input->param('branchcode');
38
)};
53
my $cat         = $input->param('categorycode');
39
my @category_codes  = map { $_->{categorycode} } @categories;
54
my $itemtype    = $input->param('itemtype');
40
our @rule_params     = qw(delay letter debarred);
55
my $onhold      = $input->param('onhold');
41
56
my $delay       = $input->param('delay') || 1;
42
# blank_row($category_code) - return true if the entire row is blank.
57
my @mttparams;
43
sub blank_row {
58
foreach ($input->param){
44
    my ($category_code) = @_;
59
    push (@mttparams, $1) if ($_ =~ /^mtt-(.*)/);
45
    for my $rp (@rule_params) {
46
        for my $n (1 .. 3) {
47
            my $key   = "${rp}${n}-$category_code";
48
49
            if (utf8::is_utf8($key)) {
50
              utf8::encode($key);
51
            }
52
53
            my $value = $input->param($key);
54
            if ($value) {
55
                return 0;
56
            }
57
        }
58
    }
59
    return 1;
60
}
60
}
61
61
62
my $type=$input->param('type');
62
$searchfield =~ s/\,//g;
63
my $branch = $input->param('branch');
63
my ($template, $borrowernumber, $cookie ) = get_template_and_user(
64
$branch ||= q{};
64
    {
65
my $op = $input->param('op');
65
        template_name   => "tools/overduerules.tt",
66
$op ||= q{};
66
        query           => $input,
67
        type            => "intranet",
68
        authnotrequired => 0,
69
        flagsrequired   => { parameters => 'edit_notice_status_triggers' },
70
        debug           => 1,
71
    }
72
);
67
73
68
my ($template, $loggedinuser, $cookie)
74
my $dbh = C4::Context->dbh;
69
    = get_template_and_user({template_name => "tools/overduerules.tt",
70
                            query => $input,
71
                            type => "intranet",
72
                            authnotrequired => 0,
73
                            flagsrequired => { tools => 'edit_notice_status_triggers'},
74
                            debug => 1,
75
                            });
76
my $err=0;
77
75
78
# save the values entered into tables
76
# collect the parameters as hashrefs
79
my %temphash;
77
my $hrBranches = GetBranches();
80
my $input_saved = 0;
78
my $hrUsercats = $dbh->selectall_hashref("SELECT description,categorycode FROM categories WHERE overduenoticerequired = 1", "categorycode");
81
if ($op eq 'save') {
79
my $hrItemtypes = $dbh->selectall_hashref("SELECT description,itemtype FROM itemtypes ", "itemtype");
82
    my @names=$input->param();
80
my $hrLetters = $dbh->selectall_hashref("SELECT name,code FROM letter WHERE module = 'circulation' order by name", "code");
83
    my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
81
my $arMessage_transport_types = C4::Letters::GetMessageTransportTypes();
84
82
85
    my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
83
# get everything on the page
86
    my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
84
$template->param(
87
    my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
85
    script_name => $script_name,
88
    my $sth_insert_mtt = $dbh->prepare("
86
    branches => $hrBranches,
89
        INSERT INTO overduerules_transport_types(
87
    categories => $hrUsercats,
90
            branchcode, categorycode, letternumber, message_transport_type
88
    itemtypes => $hrItemtypes,
91
        ) VALUES (
89
    letters => $hrLetters,
92
            ?, ?, ?, ?
90
    message_transport_types => $arMessage_transport_types,
93
        )
91
);
94
    ");
95
    my $sth_delete_mtt = $dbh->prepare("
96
        DELETE FROM overduerules_transport_types
97
        WHERE branchcode = ? AND categorycode = ?
98
    ");
99
92
100
    foreach my $key (@names){
93
# operations
101
            # ISSUES
94
if ( $op =~ m/add/ ) {
102
            if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
95
    $op =~ s/\D//g;
103
                    my $type = $1; # data type
96
    $branch = (!defined $branch ) ? '' : $branch;
104
                    my $num = $2; # From 1 to 3
97
    $op = ($op eq '') ? 0 : $op;
105
                    my $bor = $3; # borrower category
98
    $cat = (!defined $cat ) ? 0 : $cat;
106
                    my $value = $input->param($key);
99
    $itemtype = (!defined $itemtype ) ? '' :$itemtype;
107
                    if ($type eq 'delay') {
100
    $onhold = (!defined $onhold ) ? 0 : $onhold;
108
                        $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : '';
101
    my $debarred = (!defined $input->param('debarred')) ? 0 : $input->param('debarred');
109
                    } else {
102
    my $ruleId = $dbh->selectrow_array("SELECT overduerules_id from overduerules where branchcode = '$branch' and categorycode = '$cat' and itemtype = '$itemtype' and onhold = '$onhold' and delay = '$delay' ");
110
                        # type is letter
103
    if ( $ruleId ) {# it's a modification.  We update the overrules table and delete corresponding entries in overduerules_transport_types
111
                        $temphash{$bor}->{"$type$num"} = $value if $value ne '';
104
        my $query2 = "
112
                    }
105
            UPDATE overduerules
113
            }
106
            SET    letter = ?, debarred = ?
107
            WHERE  branchcode = ? and categorycode = ? and itemtype = ? and onhold = ? and delay = ? ";
108
        my $sth = $dbh->prepare($query2);
109
        $sth->execute($input->param('letter') || '', $debarred , $branch, $cat, $itemtype, $onhold, $delay);
110
        $dbh->do("DELETE FROM overduerules_transport_types where overduerules_id = $ruleId");
114
    }
111
    }
112
    elsif ($op != 0) { # case edit pkeys
113
        my $query2 = "
114
            UPDATE overduerules
115
            SET    letter = ?, debarred = ?, branchcode = ?, categorycode = ?, itemtype = ?, onhold = ?, delay = ?
116
            WHERE   overduerules_id = ? ";
117
        my $sth = $dbh->prepare($query2);
118
        $sth->execute($input->param('letter') || '', $debarred , $branch, $cat, $itemtype, $onhold, $delay, $op);
119
        $dbh->do("DELETE FROM overduerules_transport_types where overduerules_id = $op");
120
    } else { # add a new entry in overrules
121
        my $query = "
122
            INSERT INTO overduerules (branchcode, categorycode, itemtype, onhold, delay, letter, debarred)
123
            VALUES (?,?,?,?,?,?,?);";
124
        my $sth = $dbh->prepare($query);
125
        $sth->execute($branch, $cat, $itemtype, $onhold, $delay, $input->param('letter') || '', $debarred);
126
        $ruleId = $sth->{mysql_insertid};
115
127
116
    # figure out which rows need to be deleted
117
    my @rows_to_delete = grep { blank_row($_) } @category_codes;
118
119
    foreach my $bor (keys %temphash){
120
        # get category name if we need it for an error message
121
        my $bor_category = GetBorrowercategory($bor);
122
        my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
123
124
        # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
125
        # Raise error if not true
126
        if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
127
            $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
128
            $err=1;
129
        } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
130
            $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
131
            $err=1;
132
        } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
133
            $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
134
            $err=1;
135
        } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
136
            $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
137
            $err=1;
138
        } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
139
            $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
140
            $err=1;
141
        } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
142
            $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
143
            $err=1;
144
        }elsif ($temphash{$bor}->{delay3} and
145
                ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
146
                or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
147
                    $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
148
                        $err=1;
149
        }
150
        unless ($err){
151
            if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
152
                or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
153
                or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
154
                    $sth_search->execute($branch,$bor);
155
                    my $res = $sth_search->fetchrow_hashref();
156
                    if ($res->{'total'}>0) {
157
                        $sth_update->execute(
158
                            ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef),
159
                            ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
160
                            ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
161
                            ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef),
162
                            ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
163
                            ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
164
                            ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef),
165
                            ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
166
                            ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
167
                            $branch ,$bor
168
                            );
169
                    } else {
170
                        $sth_insert->execute($branch,$bor,
171
                            ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
172
                            ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
173
                            ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
174
                            ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
175
                            ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
176
                            ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
177
                            ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
178
                            ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
179
                            ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
180
                            );
181
                    }
182
183
                    $sth_delete_mtt->execute( $branch, $bor );
184
                    for my $letternumber ( 1..3 ) {
185
                        my @mtt = $input->param( "mtt${letternumber}-$bor" );
186
                        next unless @mtt;
187
                        for my $mtt ( @mtt ) {
188
                            $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
189
                        }
190
                    }
191
                }
192
        }
193
    }
128
    }
194
    unless ($err) {
129
    # following an update or an insert, we have to insert into overduerules_transport_types
195
        for my $category_code (@rows_to_delete) {
130
    my $sth = $dbh->prepare("INSERT INTO overduerules_transport_types (message_transport_type, overduerules_id) values (?,?)");
196
            $sth_delete->execute($branch, $category_code);
131
    foreach my $newMtt (@mttparams){
197
        }
132
        $sth->execute($newMtt, $ruleId);
198
        $template->param(datasaved => 1);
199
        $input_saved = 1;
200
    }
133
    }
134
    print $input->redirect('overduerules.pl');
135
    exit;
201
}
136
}
202
my $branchloop = GetBranchesLoop($branch);
137
elsif ( $op eq 'delete' ) {
203
138
    my $sth1 = $dbh->prepare("delete from overduerules_transport_types where overduerules_id = ?");
204
my $letters = C4::Letters::GetLettersAvailableForALibrary(
139
    my $sth2 = $dbh->prepare("delete from overduerules where overduerules_id = ?");
205
    {
140
    $sth1->execute($overduerules_id);
206
        branchcode => $branch,
141
    $sth2->execute($overduerules_id);
207
        module => "circulation",
142
    print $input->redirect('overduerules.pl');
208
    }
143
    exit;
209
);
144
}
210
145
else { # default
211
my @line_loop;
146
    my ($results) = StringSearch( $searchfield );
212
147
    my @loop;
213
my $message_transport_types = C4::Letters::GetMessageTransportTypes();
148
    foreach my $rule ( @{$results} ) {
214
my ( @first, @second, @third );
149
        $rule->{branchname} = $hrBranches->{$rule->{branchcode}}->{branchname};
215
for my $data (@categories) {
150
        $rule->{lettername} = $hrLetters->{$rule->{letter}}->{name};
216
    if (%temphash and not $input_saved){
151
        $rule->{categorydescription} = $hrUsercats->{$rule->{categorycode}}->{description};
217
        # if we managed to save the form submission, don't
152
        $rule->{itemtypedescription} = $hrItemtypes->{$rule->{itemtype}}->{description};
218
        # reuse %temphash, but take the values from the
153
219
        # database - this makes it easier to identify
154
        my @selected_mtts = @{ GetOverdueMessageTransportTypes( $rule->{overduerules_id} ) };
220
        # bugs where the form submission was not correctly saved
155
        my @mtts;
221
        for my $i ( 1..3 ){
156
        for my $mtt ( @$arMessage_transport_types ) {
222
            my %row = (
157
            push @mtts, {
223
                overduename => $data->{'categorycode'},
158
                value => $mtt,
224
                line        => $data->{'description'}
159
                selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
225
            );
226
            $row{delay}=$temphash{$data->{'categorycode'}}->{"delay$i"};
227
            $row{debarred}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
228
            $row{selected_lettercode} = $temphash{ $data->{categorycode} }->{"letter$i"};
229
            my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
230
            my @mtts;
231
            for my $mtt ( @$message_transport_types ) {
232
                push @mtts, {
233
                    value => $mtt,
234
                    selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
235
                }
236
            }
237
            $row{message_transport_types} = \@mtts;
238
            if ( $i == 1 ) {
239
                push @first, \%row;
240
            } elsif ( $i == 2 ) {
241
                push @second, \%row;
242
            } else {
243
                push @third, \%row;
244
            }
245
        }
246
    } else {
247
    #getting values from table
248
        my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
249
        $sth2->execute($branch,$data->{'categorycode'});
250
        my $dat=$sth2->fetchrow_hashref;
251
        for my $i ( 1..3 ){
252
            my %row = (
253
                overduename => $data->{'categorycode'},
254
                line        => $data->{'description'}
255
            );
256
257
            $row{selected_lettercode} = $dat->{"letter$i"};
258
259
            if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
260
            if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
261
            my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
262
            my @mtts;
263
            for my $mtt ( @$message_transport_types ) {
264
                push @mtts, {
265
                    value => $mtt,
266
                    selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
267
                }
268
            }
269
            $row{message_transport_types} = \@mtts;
270
            if ( $i == 1 ) {
271
                push @first, \%row;
272
            } elsif ( $i == 2 ) {
273
                push @second, \%row;
274
            } else {
275
                push @third, \%row;
276
            }
160
            }
277
278
        }
161
        }
162
        $rule->{message_transport_types} = \@mtts;
163
        push( @loop, $rule );
279
    }
164
    }
280
}
281
165
282
my @tabs = (
166
    $template->param(
283
    {
167
        loop           => \@loop,
284
        id => 'first',
168
    );
285
        number => 1,
169
}
286
        values => \@first,
287
    },
288
    {
289
        id => 'second',
290
        number => 2,
291
        values => \@second,
292
    },
293
    {
294
        id => 'third',
295
        number => 3,
296
        values => \@third,
297
    },
298
);
299
170
300
$template->param(
301
    table => ( @first or @second or @third ? 1 : 0 ),
302
    branchloop => $branchloop,
303
    branch => $branch,
304
    tabs => \@tabs,
305
    message_transport_types => $message_transport_types,
306
    letters => $letters,
307
);
308
output_html_with_http_headers $input, $cookie, $template->output;
171
output_html_with_http_headers $input, $cookie, $template->output;
309
- 

Return to bug 12772