-=head2 %derived_columns
-
-This hash keeps track of item columns that
-are strictly derived from other columns in
-the item record and are not meant to be set
-independently.
-
-Each key in the hash should be the name of a
-column (as named by TransformMarcToKoha). Each
-value should be hashref whose keys are the
-columns on which the derived column depends. The
-hashref should also contain a 'BUILDER' key
-that is a reference to a sub that calculates
-the derived value.
-
-=cut
-
-my %derived_columns = (
- 'items.cn_sort' => {
- 'itemcallnumber' => 1,
- 'items.cn_source' => 1,
- 'BUILDER' => \&_calc_items_cn_sort,
- }
-);
-
-=head2 _set_derived_columns_for_add
-
- _set_derived_column_for_add($item);
-
-Given an item hash representing a new item to be added,
-calculate any derived columns. Currently the only
-such column is C<items.cn_sort>.
-
-=cut
-
-sub _set_derived_columns_for_add {
- my $item = shift;
-
- foreach my $column (keys %derived_columns) {
- my $builder = $derived_columns{$column}->{'BUILDER'};
- my $source_values = {};
- foreach my $source_column (keys %{ $derived_columns{$column} }) {
- next if $source_column eq 'BUILDER';
- $source_values->{$source_column} = $item->{$source_column};
- }
- $builder->($item, $source_values);
- }
-}
-
-=head2 _set_derived_columns_for_mod
-
- _set_derived_column_for_mod($item);
-
-Given an item hash representing a new item to be modified.
-calculate any derived columns. Currently the only
-such column is C<items.cn_sort>.
-
-This routine differs from C<_set_derived_columns_for_add>
-in that it needs to handle partial item records. In other
-words, the caller of C<ModItem> may have supplied only one
-or two columns to be changed, so this function needs to
-determine whether any of the columns to be changed affect
-any of the derived columns. Also, if a derived column
-depends on more than one column, but the caller is not
-changing all of then, this routine retrieves the unchanged
-values from the database in order to ensure a correct
-calculation.
-
-=cut
-
-sub _set_derived_columns_for_mod {
- my $item = shift;
-
- foreach my $column (keys %derived_columns) {
- my $builder = $derived_columns{$column}->{'BUILDER'};
- my $source_values = {};
- my %missing_sources = ();
- my $must_recalc = 0;
- foreach my $source_column (keys %{ $derived_columns{$column} }) {
- next if $source_column eq 'BUILDER';
- if (exists $item->{$source_column}) {
- $must_recalc = 1;
- $source_values->{$source_column} = $item->{$source_column};
- } else {
- $missing_sources{$source_column} = 1;
- }
- }
- if ($must_recalc) {
- foreach my $source_column (keys %missing_sources) {
- $source_values->{$source_column} = _get_single_item_column($source_column, $item->{'itemnumber'});
- }
- $builder->($item, $source_values);
- }
- }
-}
-
-=head2 _do_column_fixes_for_mod
-
- _do_column_fixes_for_mod($item);
-
-Given an item hashref containing one or more
-columns to modify, fix up certain values.
-Specifically, set to 0 any passed value
-of C<notforloan>, C<damaged>, C<itemlost>, or
-C<withdrawn> that is either undefined or
-contains the empty string.
-
-=cut
-
-sub _do_column_fixes_for_mod {
- my $item = shift;
-
- if (exists $item->{'notforloan'} and
- (not defined $item->{'notforloan'} or $item->{'notforloan'} eq '')) {
- $item->{'notforloan'} = 0;
- }
- if (exists $item->{'damaged'} and
- (not defined $item->{'damaged'} or $item->{'damaged'} eq '')) {
- $item->{'damaged'} = 0;
- }
- if (exists $item->{'itemlost'} and
- (not defined $item->{'itemlost'} or $item->{'itemlost'} eq '')) {
- $item->{'itemlost'} = 0;
- }
- if (exists $item->{'withdrawn'} and
- (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
- $item->{'withdrawn'} = 0;
- }
- if (exists $item->{location}
- and $item->{location} ne 'CART'
- and $item->{location} ne 'PROC'
- and not $item->{permanent_location}
- ) {
- $item->{'permanent_location'} = $item->{'location'};
- }
- if (exists $item->{'timestamp'}) {
- delete $item->{'timestamp'};
- }
-}
-
-=head2 _get_single_item_column
-
- _get_single_item_column($column, $itemnumber);
-
-Retrieves the value of a single column from an C<items>
-row specified by C<$itemnumber>.
-
-=cut
-
-sub _get_single_item_column {
- my $column = shift;
- my $itemnumber = shift;
-
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("SELECT $column FROM items WHERE itemnumber = ?");
- $sth->execute($itemnumber);
- my ($value) = $sth->fetchrow();
- return $value;
-}
-
-=head2 _calc_items_cn_sort
-
- _calc_items_cn_sort($item, $source_values);
-
-Helper routine to calculate C<items.cn_sort>.
-
-=cut
-
-sub _calc_items_cn_sort {
- my $item = shift;
- my $source_values = shift;
-
- $item->{'items.cn_sort'} = GetClassSort($source_values->{'items.cn_source'}, $source_values->{'itemcallnumber'}, "");
-}
-
-=head2 _set_defaults_for_add
-
- _set_defaults_for_add($item_hash);
-
-Given an item hash representing an item to be added, set
-correct default values for columns whose default value
-is not handled by the DBMS. This includes the following
-columns:
-
-=over 2
-
-=item *
-
-C<items.dateaccessioned>
-
-=item *
-
-C<items.notforloan>
-
-=item *
-
-C<items.damaged>
-
-=item *
-
-C<items.itemlost>
-
-=item *
-
-C<items.withdrawn>
-
-=back
-
-=cut
-
-sub _set_defaults_for_add {
- my $item = shift;
- $item->{dateaccessioned} ||= output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
- $item->{$_} ||= 0 for (qw( notforloan damaged itemlost withdrawn));
-}
-
-=head2 _koha_new_item
-
- my ($itemnumber,$error) = _koha_new_item( $item, $barcode );
-
-Perform the actual insert into the C<items> table.
-
-=cut
-
-sub _koha_new_item {
- my ( $item, $barcode ) = @_;
- my $dbh=C4::Context->dbh;
- my $error;
- $item->{permanent_location} //= $item->{location};
- _mod_item_dates( $item );
- my $query =
- "INSERT INTO items SET
- biblionumber = ?,
- biblioitemnumber = ?,
- barcode = ?,
- dateaccessioned = ?,
- booksellerid = ?,
- homebranch = ?,
- price = ?,
- replacementprice = ?,
- replacementpricedate = ?,
- datelastborrowed = ?,
- datelastseen = ?,
- stack = ?,
- notforloan = ?,
- damaged = ?,
- itemlost = ?,
- withdrawn = ?,
- itemcallnumber = ?,
- coded_location_qualifier = ?,
- restricted = ?,
- itemnotes = ?,
- itemnotes_nonpublic = ?,
- holdingbranch = ?,
- location = ?,
- permanent_location = ?,
- onloan = ?,
- issues = ?,
- renewals = ?,
- reserves = ?,
- cn_source = ?,
- cn_sort = ?,
- ccode = ?,
- itype = ?,
- materials = ?,
- uri = ?,
- enumchron = ?,
- more_subfields_xml = ?,
- copynumber = ?,
- stocknumber = ?,
- new_status = ?
- ";
- my $sth = $dbh->prepare($query);
- my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
- $sth->execute(
- $item->{'biblionumber'},
- $item->{'biblioitemnumber'},
- $barcode,
- $item->{'dateaccessioned'},
- $item->{'booksellerid'},
- $item->{'homebranch'},
- $item->{'price'},
- $item->{'replacementprice'},
- $item->{'replacementpricedate'} || $today,
- $item->{datelastborrowed},
- $item->{datelastseen} || $today,
- $item->{stack},
- $item->{'notforloan'},
- $item->{'damaged'},
- $item->{'itemlost'},
- $item->{'withdrawn'},
- $item->{'itemcallnumber'},
- $item->{'coded_location_qualifier'},
- $item->{'restricted'},
- $item->{'itemnotes'},
- $item->{'itemnotes_nonpublic'},
- $item->{'holdingbranch'},
- $item->{'location'},
- $item->{'permanent_location'},
- $item->{'onloan'},
- $item->{'issues'},
- $item->{'renewals'},
- $item->{'reserves'},
- $item->{'items.cn_source'},
- $item->{'items.cn_sort'},
- $item->{'ccode'},
- $item->{'itype'},
- $item->{'materials'},
- $item->{'uri'},
- $item->{'enumchron'},
- $item->{'more_subfields_xml'},
- $item->{'copynumber'},
- $item->{'stocknumber'},
- $item->{'new_status'},
- );
-
- my $itemnumber;
- if ( defined $sth->errstr ) {
- $error.="ERROR in _koha_new_item $query".$sth->errstr;
- }
- else {
- $itemnumber = $dbh->{'mysql_insertid'};
- }
-
- return ( $itemnumber, $error );
-}
-