3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 3;
22 use t::lib::TestBuilder;
24 use C4::Acquisition qw( NewBasket );
25 use C4::Biblio qw( AddBiblio );
26 use C4::Budgets qw( AddBudgetPeriod AddBudget );
27 use C4::Serials qw( NewSubscription SearchSubscriptions );
29 use Koha::Acquisition::Booksellers;
31 use Koha::DateUtils qw( dt_from_string output_pref );
33 my $schema = Koha::Database->schema();
34 my $builder = t::lib::TestBuilder->new;
36 subtest '->baskets() tests' => sub {
40 $schema->storage->txn_begin();
42 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
44 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
46 is( $vendor->baskets->count, 0, 'Vendor has no baskets' );
49 my $basket_1_id = C4::Acquisition::NewBasket( $vendor->id, $patron->borrowernumber, 'basketname1' );
50 my $basket_2_id = C4::Acquisition::NewBasket( $vendor->id, $patron->borrowernumber, 'basketname2' );
53 $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
54 is( $vendor->baskets->count, 2, 'Vendor has two baskets' );
56 $schema->storage->txn_rollback();
59 subtest '->subscriptions() tests' => sub {
63 $schema->storage->txn_begin();
65 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
66 is( $vendor->subscriptions->count, 0, 'Vendor has no subscriptions' );
68 my $dt_today = dt_from_string;
69 my $today = output_pref(
70 { dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
72 my $dt_today1 = dt_from_string;
73 my $dur5 = DateTime::Duration->new( days => -5 );
74 $dt_today1->add_duration($dur5);
75 my $daysago5 = output_pref(
76 { dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
78 my $budgetperiod = C4::Budgets::AddBudgetPeriod(
79 { budget_period_startdate => $daysago5,
80 budget_period_enddate => $today,
81 budget_period_description => "budget desc"
84 my $id_budget = AddBudget(
85 { budget_code => "CODE",
86 budget_amount => "123.132",
87 budget_name => "Budgetname",
88 budget_notes => "This is a note",
89 budget_period_id => $budgetperiod
92 my $bib = MARC::Record->new();
94 MARC::Field->new( '245', ' ', ' ', a => 'Journal of ethnology' ),
95 MARC::Field->new( '500', ' ', ' ', a => 'bib notes' ),
97 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $bib, '' );
99 # Add two subscriptions
100 my $subscription_1_id = NewSubscription(
101 undef, 'BRANCH2', $vendor->id, undef,
102 $id_budget, $biblionumber, '2013-01-01', undef,
103 undef, undef, undef, undef,
104 undef, undef, undef, undef,
105 undef, 1, "subscription notes", undef,
106 '2013-01-01', undef, undef, undef,
107 'CALL ABC', 0, "intnotes", 0,
108 undef, undef, 0, undef,
112 my @subscriptions = SearchSubscriptions( { biblionumber => $biblionumber } );
113 is( $subscriptions[0]->{publicnotes},
114 'subscription notes',
115 'subscription search results include public notes (bug 10689)'
118 my $id_subscription2 = NewSubscription(
119 undef, 'BRANCH2', $vendor->id, undef,
120 $id_budget, $biblionumber, '2013-01-01', undef,
121 undef, undef, undef, undef,
122 undef, undef, undef, undef,
123 undef, 1, "subscription notes", undef,
124 '2013-01-01', undef, undef, undef,
125 'CALL DEF', 0, "intnotes", 0,
126 undef, undef, 0, undef,
131 $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
132 is( $vendor->subscriptions->count, 2, 'Vendor has two subscriptions' );
133 foreach my $subscription ( $vendor->subscriptions ) {
134 is( ref($subscription), 'Koha::Subscription', 'Type is correct' );
137 $schema->storage->txn_rollback();
140 subtest '->contacts() tests' => sub {
144 $schema->storage->txn_begin();
146 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
148 is( $vendor->contacts->count, 0, 'Vendor has no contacts' );
151 my $contact_1 = $builder->build_object(
152 { class => 'Koha::Acquisition::Bookseller::Contacts',
153 value => { booksellerid => $vendor->id }
156 my $contact_2 = $builder->build_object(
157 { class => 'Koha::Acquisition::Bookseller::Contacts',
158 value => { booksellerid => $vendor->id }
163 $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
164 my $contacts = $vendor->contacts;
165 is( $contacts->count, 2, 'Vendor has two contacts' );
166 is( ref($contacts), 'Koha::Acquisition::Bookseller::Contacts', 'Type is correct' );
168 $schema->storage->txn_rollback();