Bug 30572: Adjust search_marc_to_field.sort in kohastructure
[koha.git] / installer / onboarding.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2017 Catalyst IT
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Context;
22 use C4::InstallAuth qw( checkauth get_template_and_user );
23 use CGI qw ( -utf8 );
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Members qw( checkcardnumber );
26 use Koha::Patrons;
27 use Koha::Libraries;
28 use Koha::Database;
29 use Koha::DateUtils qw( dt_from_string output_pref );
30 use Koha::Patrons;
31 use Koha::Patron::Categories;
32 use Koha::ItemTypes;
33 use Koha::CirculationRules;
34
35 #Setting variables
36 my $input = CGI->new;
37
38 unless ( C4::Context->preference('Version') ) {
39     print $input->redirect("/cgi-bin/koha/installer/install.pl");
40     exit;
41 }
42
43 my ( $user, $cookie, $sessionID, $flags ) =
44   C4::InstallAuth::checkauth( $input, 0, undef, 'intranet' );
45 die "Not logged in"
46   unless $user
47   ; # Should not happen, we should be redirect if the user is not logged in. But do not trust authentication...
48
49 my $step = $input->param('step') || 1;
50 my $op   = $input->param('op')   || '';
51
52 my $template_params = {};
53 $template_params->{op} = $op;
54
55 my $schema = Koha::Database->new()->schema();
56
57 my @messages;
58
59 if ( $step == 1 ) {
60
61     if ( $op eq 'add_validate_library' ) {
62
63         my $branchcode = $input->param('branchcode');
64         $branchcode = uc($branchcode);
65
66         $branchcode =~ s|\s||g
67           ; # Use a regular expression to check the value of the inputted branchcode
68
69         my $library = Koha::Library->new(
70             {
71                 branchcode => $branchcode,
72                 branchname => scalar $input->param('branchname'),
73             }
74         );
75
76         eval { $library->store; };
77         unless ($@) {
78             push @messages, { code => 'success_on_insert_library' };
79         }
80         else {
81             push @messages, { code => 'error_on_insert_library' };
82         }
83     }
84
85     $step++ if Koha::Libraries->count;
86 }
87 if ( $step == 2 ) {
88     if ( $op eq "add_validate_category" ) {
89
90         my $searchfield = $input->param('description') // q||;
91         my $categorycode = $input->param('categorycode');
92         my $category;
93         $template_params->{categorycode} = $categorycode;
94
95         $categorycode = $input->param('categorycode');
96         my $description           = $input->param('description');
97         my $overduenoticerequired = $input->param('overduenoticerequired');
98         my $category_type         = $input->param('category_type');
99         my $default_privacy       = $input->param('default_privacy');
100         my $enrolmentperiod       = $input->param('enrolmentperiod');
101         my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
102
103         #Converts the string into a date format
104         if ($enrolmentperioddate) {
105             $enrolmentperioddate = output_pref(
106                 {
107                     dt         => dt_from_string($enrolmentperioddate),
108                     dateformat => 'DateTime',
109                     dateonly   => 1,
110                 }
111             );
112         }
113
114         #Adds a new patron category to the database
115         $category = Koha::Patron::Category->new(
116             {
117                 categorycode          => $categorycode,
118                 description           => $description,
119                 overduenoticerequired => $overduenoticerequired,
120                 category_type         => $category_type,
121                 default_privacy       => $default_privacy,
122                 enrolmentperiod       => $enrolmentperiod,
123                 enrolmentperioddate   => $enrolmentperioddate
124             }
125         );
126
127         eval { $category->store; };
128
129         unless ($@) {
130             push @messages, { code => 'success_on_insert_category' };
131         }
132         else {
133             push @messages, { code => 'error_on_insert_category' };
134         }
135     }
136
137     $step++ if Koha::Patron::Categories->count;
138 }
139 if ( $step == 3 ) {
140     if ( $op eq 'add_validate_patron' ) {
141
142         #Create a patron
143         my $firstpassword  = $input->param('password')  || '';
144         my $secondpassword = $input->param('password2') || '';
145         my $cardnumber     = $input->param('cardnumber');
146         my $userid         = $input->param('userid');
147         my $categorycode = $input->param('categorycode_entry');
148         my $patron_category =
149           Koha::Patron::Categories->find( $categorycode );
150
151         my ( $is_valid, $passworderror ) =
152           Koha::AuthUtils::is_password_valid( $firstpassword,
153             $patron_category );
154
155
156         if ( my $error_code = checkcardnumber($cardnumber) ) {
157             if ( $error_code == 1 ) {
158                 push @messages, { code => 'ERROR_cardnumber_already_exists' };
159             }
160             elsif ( $error_code == 2 ) {
161                 push @messages, { code => 'ERROR_cardnumber_length' };
162             }
163         }
164         elsif ( $firstpassword ne $secondpassword ) {
165
166             push @messages, { code => 'ERROR_password_mismatch' };
167         }
168         elsif ( $passworderror) {
169                 push @messages, { code => 'ERROR_password_too_short'} if $passworderror eq 'too_short';
170                 push @messages, { code => 'ERROR_password_too_weak'} if $passworderror eq 'too_weak';
171                 push @messages, { code => 'ERROR_password_has_whitespaces'} if $passworderror eq 'has_whitespaces';
172
173         }
174         else {
175             my $patron_data = {
176                 surname      => scalar $input->param('surname'),
177                 firstname    => scalar $input->param('firstname'),
178                 cardnumber   => scalar $input->param('cardnumber'),
179                 branchcode   => scalar $input->param('libraries'),
180                 categorycode => $categorycode,
181                 userid       => scalar $input->param('userid'),
182                 privacy      => "default",
183                 address      => "",
184                 city         => "",
185                 flags        => 1,    # Will be superlibrarian
186             };
187
188             $patron_data->{dateexpiry} =
189               $patron_category->get_expiry_date( $patron_data->{dateenrolled} );
190
191             eval {
192                 my $patron = Koha::Patron->new($patron_data)->store;
193                 $patron->set_password({ password =>  $firstpassword });
194             };
195
196             #Error handling checking if the patron was created successfully
197             unless ($@) {
198                 push @messages, { code => 'success_on_insert_patron' };
199             }
200             else {
201                 warn $@;
202                 push @messages, { code => 'error_on_insert_patron' };
203             }
204         }
205     }
206
207     $step++ if Koha::Patrons->search( { flags => 1 } )->count;
208 }
209 if ( $step == 4 ) {
210     if ( $op eq 'add_validate_itemtype' ) {
211         my $description   = $input->param('description');
212         my $itemtype_code = $input->param('itemtype');
213         $itemtype_code = uc($itemtype_code);
214
215         my $itemtype = Koha::ItemType->new(
216             {
217                 itemtype    => $itemtype_code,
218                 description => $description,
219             }
220         );
221         eval { $itemtype->store; };
222
223         unless ($@) {
224             push @messages, { code => 'success_on_insert_itemtype' };
225         }
226         else {
227             push @messages, { code => 'error_on_insert_itemtype' };
228         }
229     }
230
231     $step++ if Koha::ItemTypes->count;
232 }
233 if ( $step == 5 ) {
234
235     if ( $op eq 'add_validate_circ_rule' ) {
236
237         #If no libraries exist then set the $branch value to *
238         my $branch = $input->param('branch') || '*';
239
240         my $type            = $input->param('type');
241         my $branchcode      = $input->param('branch');
242         my $categorycode    = $input->param('categorycode');
243         my $itemtype        = $input->param('itemtype');
244         my $maxissueqty     = $input->param('maxissueqty');
245         my $issuelength     = $input->param('issuelength');
246         my $lengthunit      = $input->param('lengthunit');
247         my $renewalsallowed = $input->param('renewalsallowed');
248         my $renewalperiod   = $input->param('renewalperiod');
249         my $reservesallowed = $input->param('reservesallowed');
250         my $holds_per_day   = $input->param('holds_per_day');
251         my $holds_per_record = $input->param('holds_per_record');
252         my $onshelfholds    = $input->param('onshelfholds') || 0;
253         $maxissueqty =~ s/\s//g;
254         $maxissueqty = undef if $maxissueqty !~ /^\d+/;
255         $issuelength = $issuelength eq q{} ? undef : $issuelength;
256
257         my $params = {
258             branchcode      => $branchcode,
259             categorycode    => $categorycode,
260             itemtype        => $itemtype,
261             rules => {
262                 renewalsallowed                  => $renewalsallowed,
263                 renewalperiod                    => $renewalperiod,
264                 issuelength                      => $issuelength,
265                 lengthunit                       => $lengthunit,
266                 onshelfholds                     => $onshelfholds,
267                 article_requests                 => "no",
268                 auto_renew                       => 0,
269                 cap_fine_to_replacement_price    => 0,
270                 chargeperiod                     => 0,
271                 chargeperiod_charge_at           => 0,
272                 fine                             => 0,
273                 finedays                         => 0,
274                 firstremind                      => 0,
275                 hardduedate                      => "",
276                 hardduedatecompare               => -1,
277                 holds_per_day                    => $holds_per_day,
278                 holds_per_record                 => $holds_per_record,
279                 maxissueqty                      => $maxissueqty,
280                 maxonsiteissueqty                => "",
281                 maxsuspensiondays                => "",
282                 no_auto_renewal_after            => "",
283                 no_auto_renewal_after_hard_limit => "",
284                 norenewalbefore                  => "",
285                 opacitemholds                    => "N",
286                 overduefinescap                  => "",
287                 rentaldiscount                   => 0,
288                 reservesallowed                  => $reservesallowed,
289                 suspension_chargeperiod          => undef,
290                 decreaseloanholds                => undef,
291                 recalls_allowed                  => undef,
292                 recalls_per_record               => undef,
293                 on_shelf_recalls                 => undef,
294                 recall_due_date_interval         => undef,
295                 recall_overdue_fine              => undef,
296                 recall_shelf_time                => undef,
297               }
298         };
299
300         eval {
301             Koha::CirculationRules->set_rules($params);
302         };
303
304         if ($@) {
305             warn $@;
306             push @messages, { code => 'error_on_insert_circ_rule' };
307         } else {
308             push @messages, { code => 'success_on_insert_circ_rule' };
309         }
310     }
311
312     $step++ if Koha::CirculationRules->count;
313 }
314
315 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
316 $template_params->{libraries}   = $libraries;
317
318 if ( $step > 5 ) {
319     $template_params->{all_done} = 1;    # If step 5 is complete, we are done!
320     $step = 5;
321 }
322
323 #Getting the appropriate template to display to the user
324 my ( $template, $loggedinuser );
325 ( $template, $loggedinuser, $cookie ) = C4::InstallAuth::get_template_and_user(
326     {
327         template_name   => "onboarding/onboardingstep${step}.tt",
328         query           => $input,
329         type            => "intranet",
330     }
331 );
332
333 $template_params->{messages} = \@messages;
334 my $categories = Koha::Patron::Categories->search();
335 $template_params->{categories} = $categories;
336
337 my $itemtypes = Koha::ItemTypes->search();
338 $template_params->{itemtypes} = $itemtypes;
339
340 $template->param(%$template_params);
341
342 output_html_with_http_headers $input, $cookie, $template->output;