} from '@angular/core';
import {Router} from '@angular/router';
import {Observable, Observer, of, empty} from 'rxjs';
-import {map} from 'rxjs/operators';
+import {map, tap, concatMap} from 'rxjs/operators';
import {Pager} from '@eg/share/util/pager';
import {IdlObject, IdlService} from '@eg/core/idl.service';
import {StaffCatalogService} from '../catalog.service';
orgClassCallback: (orgId: number) => string;
marked_orgs: number[] = [];
+ copyCounts: {[orgId: number]: {}} = {};
+
private _recId: number;
@Input() set recordId(id: number) {
this._recId = id;
setTreeCounts(node: HoldingsTreeNode) {
if (node.nodeType === 'org') {
- node.copyCount = 0;
- node.callNumCount = 0;
+ node.copyCount = this.copyCounts[node.target.id() + ''].copies;
+ node.callNumCount = this.copyCounts[node.target.id() + ''].call_numbers;
} else if (node.nodeType === 'callNum') {
node.copyCount = 0;
}
node.children.forEach(child => {
this.setTreeCounts(child);
if (node.nodeType === 'org') {
- node.copyCount += child.copyCount;
- if (child.nodeType === 'callNum') {
- node.callNumCount++;
- } else {
+ if (child.nodeType !== 'callNum') {
hasChildOrgWithData = child.callNumCount > 0;
hasChildOrgSansData = child.callNumCount === 0;
- node.callNumCount += child.callNumCount;
}
} else if (node.nodeType === 'callNum') {
node.copyCount = node.children.length;
// any that were deleted in an out-of-band update.
const volsFetched: number[] = [];
- this.pcrud.search('acn',
- { record: this.recordId,
- owning_lib: this.org.fullPath(this.contextOrg, true),
- deleted: 'f',
- label: {'!=' : '##URI##'}
- }, {
- flesh: 3,
- flesh_fields: {
- acp: ['status', 'location', 'circ_lib', 'parts', 'notes',
- 'tags', 'age_protect', 'copy_alerts', 'latest_inventory',
- 'total_circ_count', 'last_circ'],
- acn: ['prefix', 'suffix', 'copies'],
- acli: ['inventory_workstation']
- }
- },
- {authoritative: true}
+ return this.net.request(
+ 'open-ils.search',
+ 'open-ils.search.biblio.record.copy_counts.global.staff',
+ this.recordId
+ ).pipe(
+ tap(counts => this.copyCounts = counts),
+ concatMap(_ => {
+
+ return this.pcrud.search('acn',
+ { record: this.recordId,
+ owning_lib: this.org.fullPath(this.contextOrg, true),
+ deleted: 'f',
+ label: {'!=' : '##URI##'}
+ }, {
+ flesh: 3,
+ flesh_fields: {
+ acp: ['status', 'location', 'circ_lib', 'parts', 'notes',
+ 'tags', 'age_protect', 'copy_alerts', 'latest_inventory',
+ 'total_circ_count', 'last_circ'],
+ acn: ['prefix', 'suffix', 'copies'],
+ acli: ['inventory_workstation']
+ }
+ },
+ {authoritative: true}
+ );
+ })
).subscribe(
callNum => {
this.appendCallNum(callNum);
ok => this.flattenHoldingsTree(observer)
);
}
- );
+ );
});
}
};
}
+__PACKAGE__->register_method(
+ method => 'record_copy_counts_global',
+ api_name => 'open-ils.search.biblio.record.copy_counts.global.staff',
+ signature => {
+ desc => q/Returns a count of copies and call numbers for each org
+ unit, including items attached to each org unit plus
+ a sum of counts for all descendants./,
+ params => [
+ {desc => 'Record ID', type => 'number'}
+ ],
+ return => {
+ desc => 'Hash of org unit ID => {copy: $count, call_number: $id}'
+ }
+ }
+);
+
+sub record_copy_counts_global {
+ my ($self, $client, $rec_id) = @_;
+
+ my $copies = new_editor()->json_query({
+ select => {
+ acp => [{column => 'id', alias => 'copy_id'}, 'circ_lib'],
+ acn => [{column => 'id', alias => 'cn_id'}, 'owning_lib']
+ },
+ from => {acn => {acp => {type => 'left'}}},
+ where => {
+ '+acp' => {
+ '-or' => [
+ {deleted => 'f'},
+ {id => undef} # left join
+ ]
+ },
+ '+acn' => {deleted => 'f', record => $rec_id}
+ }
+ });
+
+ my $hash = {};
+ my %seen_cn;
+
+ for my $copy (@$copies) {
+ my $org = $copy->{circ_lib} || $copy->{owning_lib};
+ $hash->{$org} = {copies => 0, call_numbers => 0} unless $hash->{$org};
+ $hash->{$org}->{copies}++ if $copy->{circ_lib};
+
+ if (!$seen_cn{$copy->{cn_id}}) {
+ $seen_cn{$copy->{cn_id}} = 1;
+ $hash->{$org}->{call_numbers}++;
+ }
+ }
+
+ my $sum;
+ $sum = sub {
+ my $node = shift;
+ my $h = $hash->{$node->id} || {copies => 0, call_numbers => 0};
+ delete $h->{cn_id};
+
+ for my $child (@{$node->children}) {
+ my $vals = $sum->($child);
+ $h->{copies} += $vals->{copies};
+ $h->{call_numbers} += $vals->{call_numbers};
+ }
+
+ $hash->{$node->id} = $h;
+
+ return $h;
+ };
+
+ $sum->($U->get_org_tree);
+
+ return $hash;
+}
+
1;