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

(-)a/C4/Members.pm (-93 lines)
Lines 78-84 BEGIN { Link Here
78
78
79
    #Insert data
79
    #Insert data
80
    push @EXPORT, qw(
80
    push @EXPORT, qw(
81
        &AddMember
82
    &AddMember_Auto
81
    &AddMember_Auto
83
        &AddMember_Opac
82
        &AddMember_Opac
84
    );
83
    );
Lines 375-472 sub ModMember { Link Here
375
    return $execute_success;
374
    return $execute_success;
376
}
375
}
377
376
378
=head2 AddMember
379
380
  $borrowernumber = &AddMember(%borrower);
381
382
insert new borrower into table
383
384
(%borrower keys are database columns. Database columns could be
385
different in different versions. Please look into database for correct
386
column names.)
387
388
Returns the borrowernumber upon success
389
390
Returns as undef upon any db error without further processing
391
392
=cut
393
394
#'
395
sub AddMember {
396
    my (%data) = @_;
397
    my $dbh = C4::Context->dbh;
398
    my $schema = Koha::Database->new()->schema;
399
400
    my $category = Koha::Patron::Categories->find( $data{categorycode} );
401
    unless ($category) {
402
        Koha::Exceptions::Object::FKConstraint->throw(
403
            broken_fk => 'categorycode',
404
            value     => $data{categorycode},
405
        );
406
    }
407
408
    my $p = Koha::Patron->new( { userid => $data{userid}, firstname => $data{firstname}, surname => $data{surname} } );
409
    # generate a proper login if none provided
410
    $data{'userid'} = $p->generate_userid
411
      if ( $data{'userid'} eq '' || ! $p->has_valid_userid );
412
413
    # add expiration date if it isn't already there
414
    $data{dateexpiry} ||= $category->get_expiry_date;
415
416
    # add enrollment date if it isn't already there
417
    unless ( $data{'dateenrolled'} ) {
418
        $data{'dateenrolled'} = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
419
    }
420
421
    $data{'privacy'} =
422
        $category->default_privacy() eq 'default' ? 1
423
      : $category->default_privacy() eq 'never'   ? 2
424
      : $category->default_privacy() eq 'forever' ? 0
425
      :                                             undef;
426
427
    $data{'privacy_guarantor_checkouts'} = 0 unless defined( $data{'privacy_guarantor_checkouts'} );
428
429
    # Make a copy of the plain text password for later use
430
    my $plain_text_password = $data{'password'};
431
432
    # create a disabled account if no password provided
433
    $data{'password'} = ($data{'password'})? hash_password($data{'password'}) : '!';
434
435
    # we don't want invalid dates in the db (mysql has a bad habit of inserting 0000-00-00
436
    $data{'dateofbirth'}     = undef if ( not $data{'dateofbirth'} );
437
    $data{'debarred'}        = undef if ( not $data{'debarred'} );
438
    $data{'sms_provider_id'} = undef if ( not $data{'sms_provider_id'} );
439
    $data{'guarantorid'}     = undef if ( not $data{'guarantorid'} );
440
441
    # get only the columns of Borrower
442
    # FIXME Do we really need this check?
443
    my @columns = $schema->source('Borrower')->columns;
444
    my $new_member = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $data{$_} )  : () } keys(%data) } ;
445
446
    delete $new_member->{borrowernumber};
447
448
    my $patron = Koha::Patron->new( $new_member )->store;
449
    $data{borrowernumber} = $patron->borrowernumber;
450
451
    # If NorwegianPatronDBEnable is enabled, we set syncstatus to something that a
452
    # cronjob will use for syncing with NL
453
    if ( exists $data{'borrowernumber'} && C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
454
        Koha::Database->new->schema->resultset('BorrowerSync')->create({
455
            'borrowernumber' => $data{'borrowernumber'},
456
            'synctype'       => 'norwegianpatrondb',
457
            'sync'           => 1,
458
            'syncstatus'     => 'new',
459
            'hashed_pin'     => Koha::NorwegianPatronDB::NLEncryptPIN( $plain_text_password ),
460
        });
461
    }
462
463
    logaction("MEMBERS", "CREATE", $data{'borrowernumber'}, "") if C4::Context->preference("BorrowersLog");
464
465
    $patron->add_enrolment_fee_if_needed;
466
467
    return $data{borrowernumber};
468
}
469
470
=head2 GetAllIssues
377
=head2 GetAllIssues
471
378
472
  $issues = &GetAllIssues($borrowernumber, $sortkey, $limit);
379
  $issues = &GetAllIssues($borrowernumber, $sortkey, $limit);
(-)a/Koha/Patron.pm (-4 / +93 lines)
Lines 22-27 use Modern::Perl; Link Here
22
22
23
use Carp;
23
use Carp;
24
use List::MoreUtils qw( uniq );
24
use List::MoreUtils qw( uniq );
25
use Module::Load::Conditional qw( can_load );
25
use Text::Unaccent qw( unac_string );
26
use Text::Unaccent qw( unac_string );
26
27
27
use C4::Context;
28
use C4::Context;
Lines 41-46 use Koha::Club::Enrollments; Link Here
41
use Koha::Account;
42
use Koha::Account;
42
use Koha::Subscription::Routinglists;
43
use Koha::Subscription::Routinglists;
43
44
45
if ( ! can_load( modules => { 'Koha::NorwegianPatronDB' => undef } ) ) {
46
   warn "Unable to load Koha::NorwegianPatronDB";
47
}
48
44
use base qw(Koha::Object);
49
use base qw(Koha::Object);
45
50
46
our $RESULTSET_PATRON_ID_MAPPING = {
51
our $RESULTSET_PATRON_ID_MAPPING = {
Lines 110-116 sub trim_whitespaces { Link Here
110
    my( $self ) = @_;
115
    my( $self ) = @_;
111
116
112
    my $schema  = Koha::Database->new->schema;
117
    my $schema  = Koha::Database->new->schema;
113
    my @columns = $schema->source('Borrowers')->columns;
118
    my @columns = $schema->source($self->_type)->columns;
114
119
115
    for my $column( @columns ) {
120
    for my $column( @columns ) {
116
        my $value = $self->$column;
121
        my $value = $self->$column;
Lines 123-129 sub trim_whitespaces { Link Here
123
}
128
}
124
129
125
sub store {
130
sub store {
126
    my( $self ) = @_;
131
    my ($self) = @_;
127
132
128
    $self->_result->result_source->schema->txn_do(
133
    $self->_result->result_source->schema->txn_do(
129
        sub {
134
        sub {
Lines 138-147 sub store { Link Here
138
                # We are in a transaction but the table is not locked
143
                # We are in a transaction but the table is not locked
139
                $self->fixup_cardnumber;
144
                $self->fixup_cardnumber;
140
            }
145
            }
146
            unless ( $self->in_storage ) {    #AddMember
147
148
                unless( $self->category->in_storage ) {
149
                    Koha::Exceptions::Object::FKConstraint->throw(
150
                        broken_fk => 'categorycode',
151
                        value     => $self->categorycode,
152
                    );
153
                }
154
155
                $self->trim_whitespaces;
156
157
                # Generate a valid userid/login if needed
158
                $self->userid($self->generate_userid)
159
                  if not $self->userid or not $self->has_valid_userid;
160
161
                # Add expiration date if it isn't already there
162
                unless ( $self->dateexpiry ) {
163
                    $self->dateexpiry( $self->category->get_expiry_date );
164
                }
165
166
                # Add enrollment date if it isn't already there
167
                unless ( $self->dateenrolled ) {
168
                    $self->dateenrolled(dt_from_string);
169
                }
170
171
                # Set the privacy depending on the patron's category
172
                my $default_privacy = $self->category->default_privacy || q{};
173
                $default_privacy =
174
                    $default_privacy eq 'default' ? 1
175
                  : $default_privacy eq 'never'   ? 2
176
                  : $default_privacy eq 'forever' ? 0
177
                  :                                                   undef;
178
                $self->privacy($default_privacy);
179
180
                unless ( defined $self->privacy_guarantor_checkouts ) {
181
                    $self->privacy_guarantor_checkouts(0);
182
                }
183
184
                # Make a copy of the plain text password for later use
185
                my $plain_text_password = $self->password;
186
187
                # Create a disabled account if no password provided
188
                $self->password( $self->password
189
                    ? Koha::AuthUtils::hash_password( $self->password )
190
                    : '!' );
191
192
                # We don't want invalid dates in the db (mysql has a bad habit of inserting 0000-00-00)
193
                $self->dateofbirth(undef) unless $self->dateofbirth;
194
                $self->debarred(undef)    unless $self->debarred;
195
196
                # Set default values if not set
197
                $self->sms_provider_id(undef) unless $self->sms_provider_id;
198
                $self->guarantorid(undef)     unless $self->guarantorid;
199
200
                $self->borrowernumber(undef);
201
202
                $self = $self->SUPER::store;
203
204
                # If NorwegianPatronDBEnable is enabled, we set syncstatus to something that a
205
                # cronjob will use for syncing with NL
206
                if (   C4::Context->preference('NorwegianPatronDBEnable')
207
                    && C4::Context->preference('NorwegianPatronDBEnable') == 1 )
208
                {
209
                    Koha::Database->new->schema->resultset('BorrowerSync')
210
                      ->create(
211
                        {
212
                            'borrowernumber' => $self->borrowernumber,
213
                            'synctype'       => 'norwegianpatrondb',
214
                            'sync'           => 1,
215
                            'syncstatus'     => 'new',
216
                            'hashed_pin' =>
217
                              Koha::NorwegianPatronDB::NLEncryptPIN(
218
                                $plain_text_password),
219
                        }
220
                      );
221
                }
222
223
                $self->add_enrolment_fee_if_needed;
224
225
                logaction( "MEMBERS", "CREATE", $self->borrowernumber, "" )
226
                  if C4::Context->preference("BorrowersLog");
227
            }
228
            else {    #ModMember
229
                $self = $self->SUPER::store;
230
            }
141
231
142
            $self->SUPER::store;
143
        }
232
        }
144
    );
233
    );
234
    return $self;
145
}
235
}
146
236
147
=head3 delete
237
=head3 delete
148
- 

Return to bug 20287