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

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

Return to bug 12772