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

(-)a/C4/Bookseller.pm (-116 lines)
Lines 1-116 Link Here
1
package C4::Bookseller;
2
3
# Copyright 2000-2002 Katipo Communications
4
# Copyright 2010 PTFS Europe
5
#
6
# This file is part of Koha.
7
#
8
# Koha is free software; you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# Koha is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
use strict;
22
use warnings;
23
24
use base qw( Exporter );
25
26
our @EXPORT_OK = qw(
27
  GetBooksellersWithLateOrders
28
);
29
30
=head1 NAME
31
32
C4::Bookseller - Koha functions for dealing with booksellers.
33
34
=head1 SYNOPSIS
35
36
use C4::Bookseller;
37
38
=head1 DESCRIPTION
39
40
The functions in this module deal with booksellers. They allow to
41
add a new bookseller, to modify it or to get some informations around
42
a bookseller.
43
44
=head1 FUNCTIONS
45
46
=head2 GetBooksellersWithLateOrders
47
48
%results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
49
50
Searches for suppliers with late orders.
51
52
=cut
53
54
sub GetBooksellersWithLateOrders {
55
    my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
56
    my $dbh = C4::Context->dbh;
57
58
    # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
59
    # should be tested with other DBMs
60
61
    my $query;
62
    my @query_params = ();
63
    my $dbdriver = C4::Context->config("db_scheme") || "mysql";
64
    $query = "
65
        SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
66
        FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
67
        LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
68
        WHERE
69
            ( datereceived IS NULL
70
            OR aqorders.quantityreceived < aqorders.quantity
71
            )
72
            AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
73
            AND aqbasket.closedate IS NOT NULL
74
    ";
75
    if ( defined $delay && $delay >= 0 ) {
76
        $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) ";
77
        push @query_params, $delay;
78
    } elsif ( $delay && $delay < 0 ){
79
        warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
80
        return;
81
    }
82
    if ( defined $estimateddeliverydatefrom ) {
83
        $query .= '
84
            AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?';
85
            push @query_params, $estimateddeliverydatefrom;
86
            if ( defined $estimateddeliverydateto ) {
87
                $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
88
                push @query_params, $estimateddeliverydateto;
89
            } else {
90
                    $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
91
            }
92
    }
93
    if ( defined $estimateddeliverydateto ) {
94
        $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
95
        push @query_params, $estimateddeliverydateto;
96
    }
97
98
    my $sth = $dbh->prepare($query);
99
    $sth->execute( @query_params );
100
    my %supplierlist;
101
    while ( my ( $id, $name ) = $sth->fetchrow ) {
102
        $supplierlist{$id} = $name;
103
    }
104
105
    return %supplierlist;
106
}
107
108
1;
109
110
__END__
111
112
=head1 AUTHOR
113
114
Koha Development Team <http://koha-community.org/>
115
116
=cut
(-)a/t/Bookseller.t (-14 lines)
Lines 1-14 Link Here
1
#!/usr/bin/perl
2
#
3
# This Koha test module is a stub!  
4
# Add more tests here!!!
5
6
use strict;
7
use warnings;
8
9
use Test::More tests => 1;
10
11
BEGIN {
12
        use_ok('C4::Bookseller');
13
}
14
(-)a/t/db_dependent/Bookseller.t (-787 lines)
Lines 1-786 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 86;
6
use Test::MockModule;
7
use Test::Warn;
8
9
use t::lib::TestBuilder;
10
11
use C4::Context;
12
use Koha::DateUtils;
13
use DateTime::Duration;
14
use t::lib::Mocks;
15
use C4::Acquisition;
16
use C4::Serials;
17
use C4::Budgets;
18
use C4::Biblio;
19
20
use Koha::Acquisition::Booksellers;
21
use Koha::Acquisition::Orders;
22
use Koha::Database;
23
24
BEGIN {
25
    use_ok('C4::Bookseller');
26
}
27
28
can_ok(
29
30
    'C4::Bookseller', qw(
31
      GetBooksellersWithLateOrders
32
    )
33
);
34
35
#Start transaction
36
my $dbh = C4::Context->dbh;
37
my $database = Koha::Database->new();
38
my $schema = $database->schema();
39
$schema->storage->txn_begin();
40
my $builder = t::lib::TestBuilder->new;
41
42
#Start tests
43
$dbh->do(q|DELETE FROM aqorders|);
44
$dbh->do(q|DELETE FROM aqbasket|);
45
$dbh->do(q|DELETE FROM aqbooksellers|);
46
$dbh->do(q|DELETE FROM subscription|);
47
48
my $patron = $builder->build_object({ class => 'Koha::Patrons' });
49
# Add currency
50
my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
51
52
#Test AddBookseller
53
my $count            = Koha::Acquisition::Booksellers->search()->count();
54
my $sample_supplier1 = {
55
    name          => 'Name1',
56
    address1      => 'address1_1',
57
    address2      => 'address1-2',
58
    address3      => 'address1_2',
59
    address4      => 'address1_2',
60
    postal        => 'postal1',
61
    phone         => 'phone1',
62
    accountnumber => 'accountnumber1',
63
    fax           => 'fax1',
64
    url           => 'url1',
65
    active        => 1,
66
    gstreg        => 1,
67
    listincgst    => 1,
68
    invoiceincgst => 1,
69
    tax_rate      => '1.0000',
70
    discount      => 1.0000,
71
    notes         => 'notes1',
72
    deliverytime  => undef
73
};
74
my $sample_supplier2 = {
75
    name          => 'Name2',
76
    address1      => 'address1_2',
77
    address2      => 'address2-2',
78
    address3      => 'address3_2',
79
    address4      => 'address4_2',
80
    postal        => 'postal2',
81
    phone         => 'phone2',
82
    accountnumber => 'accountnumber2',
83
    fax           => 'fax2',
84
    url           => 'url2',
85
    active        => 1,
86
    gstreg        => 1,
87
    listincgst    => 1,
88
    invoiceincgst => 1,
89
    tax_rate      => '2.0000',
90
    discount      => 2.0000,
91
    notes         => 'notes2',
92
    deliverytime  => 2
93
};
94
95
my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
96
my $id_supplier1 = $supplier1->id;
97
my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
98
my $id_supplier2 = $supplier2->id;
99
100
like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
101
like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
102
is( Koha::Acquisition::Booksellers->search()->count,
103
    $count + 2, "Supplier1 and Supplier2 have been added" );
104
105
#Test DelBookseller
106
my $del = $supplier1->delete;
107
is( ref($del), 'Koha::Acquisition::Bookseller', "DelBookseller returns the supplier object - supplier has been deleted " );
108
my $b = Koha::Acquisition::Booksellers->find( $id_supplier1 );
109
is( $b,
110
    undef, "Supplier1  has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
111
112
#Test get bookseller
113
my @bookseller2 = Koha::Acquisition::Booksellers->search({name => $sample_supplier2->{name} });
114
is( scalar(@bookseller2), 1, "Get only  Supplier2" );
115
for my $bookseller ( @bookseller2 ) {
116
    $bookseller = field_filter( $bookseller->unblessed );
117
}
118
119
$sample_supplier2->{id} = $id_supplier2;
120
is_deeply( cast_precision($bookseller2[0]), $sample_supplier2,
121
    "Koha::Acquisition::Booksellers->search returns the right informations about supplier $sample_supplier2->{name}" );
122
123
$supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
124
$id_supplier1 = $supplier1->id;
125
my @booksellers = Koha::Acquisition::Booksellers->search();
126
for my $bookseller ( @booksellers ) {
127
    $bookseller = field_filter( $bookseller->unblessed );
128
    $bookseller = cast_precision($bookseller);
129
}
130
131
$sample_supplier1->{id} = $id_supplier1;
132
is( scalar(@booksellers), $count + 2, "Get  Supplier1 and Supplier2" );
133
my @tab = ( $sample_supplier2, $sample_supplier1 );
134
is_deeply( \@booksellers, \@tab,
135
    "Returns right fields of Supplier1 and Supplier2" );
136
137
#Test baskets
138
my @bookseller1 = Koha::Acquisition::Booksellers->search({name => $sample_supplier1->{name} });
139
is( $bookseller1[0]->baskets->count, 0, 'Supplier1 has 0 basket' );
140
my $basketno1 =
141
  C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname1' );
142
my $basketno2 =
143
  C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname2' );
144
@bookseller1 = Koha::Acquisition::Booksellers->search({ name => $sample_supplier1->{name} });
145
is( $bookseller1[0]->baskets->count, 2, 'Supplier1 has 2 baskets' );
146
147
#Test Koha::Acquisition::Bookseller->new using id
148
my $bookseller1fromid = Koha::Acquisition::Booksellers->find;
149
is( $bookseller1fromid, undef,
150
    "find returns undef if no id given" );
151
$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
152
$bookseller1fromid = field_filter($bookseller1fromid->unblessed);
153
is_deeply( cast_precision($bookseller1fromid), $sample_supplier1,
154
    "Get Supplier1 (find a bookseller by id)" );
155
156
$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
157
is( $bookseller1fromid->baskets->count, 2, 'Supplier1 has 2 baskets' );
158
159
#Test subscriptions
160
my $dt_today    = dt_from_string;
161
my $today       = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
162
163
my $dt_today1 = dt_from_string;
164
my $dur5 = DateTime::Duration->new( days => -5 );
165
$dt_today1->add_duration($dur5);
166
my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
167
168
my $budgetperiod = C4::Budgets::AddBudgetPeriod({
169
    budget_period_startdate   => $daysago5,
170
    budget_period_enddate     => $today,
171
    budget_period_description => "budget desc"
172
});
173
my $id_budget = AddBudget({
174
    budget_code        => "CODE",
175
    budget_amount      => "123.132",
176
    budget_name        => "Budgetname",
177
    budget_notes       => "This is a note",
178
    budget_period_id   => $budgetperiod
179
});
180
my $bib = MARC::Record->new();
181
$bib->append_fields(
182
    MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
183
    MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
184
);
185
my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
186
$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
187
is( $bookseller1fromid->subscriptions->count,
188
    0, 'Supplier1 has 0 subscription' );
189
190
my $id_subscription1 = NewSubscription(
191
    undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
192
    '2013-01-01',undef, undef, undef,  undef,
193
    undef,      undef,  undef, undef, undef, undef,
194
    1,          "subscription notes",undef, '2013-01-01', undef, undef,
195
    undef, 'CALL ABC',  0,    "intnotes",  0,
196
    undef, undef, 0,          undef,         '2013-11-30', 0
197
);
198
199
my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
200
is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
201
202
my $id_subscription2 = NewSubscription(
203
    undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
204
    '2013-01-01',undef, undef, undef,  undef,
205
    undef,      undef,  undef, undef, undef, undef,
206
    1,          "subscription notes",undef, '2013-01-01', undef, undef,
207
    undef, 'CALL DEF',  0,    "intnotes",  0,
208
    undef, undef, 0,          undef,         '2013-07-31', 0
209
);
210
211
$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
212
is( $bookseller1fromid->subscriptions->count,
213
    2, 'Supplier1 has 2 subscriptions' );
214
215
#Test ModBookseller
216
$sample_supplier2 = {
217
    id            => $id_supplier2,
218
    name          => 'Name2 modified',
219
    address1      => 'address1_2 modified',
220
    address2      => 'address2-2 modified',
221
    address3      => 'address3_2 modified',
222
    address4      => 'address4_2 modified',
223
    postal        => 'postal2 modified',
224
    phone         => 'phone2 modified',
225
    accountnumber => 'accountnumber2 modified',
226
    fax           => 'fax2 modified',
227
    url           => 'url2 modified',
228
    active        => 1,
229
    gstreg        => 1,
230
    listincgst    => 1,
231
    invoiceincgst => 1,
232
    tax_rate      => '2.0000',
233
    discount      => 2.0000,
234
    notes         => 'notes2 modified',
235
    deliverytime  => 2,
236
};
237
238
my $modif1 = Koha::Acquisition::Booksellers->find($id_supplier2)->set($sample_supplier2)->store;
239
is( ref $modif1, 'Koha::Acquisition::Bookseller', "ModBookseller has updated the bookseller" );
240
is( Koha::Acquisition::Booksellers->search->count,
241
    $count + 2, "Supplier2 has been modified - Nothing added" );
242
$supplier2 = Koha::Acquisition::Booksellers->find($id_supplier2);
243
is( $supplier2->name, 'Name2 modified', "supplier's name should have been modified" );
244
245
#Test GetBooksellersWithLateOrders
246
#Add 2 suppliers
247
my $sample_supplier3 = {
248
    name          => 'Name3',
249
    address1      => 'address1_3',
250
    address2      => 'address1-3',
251
    address3      => 'address1_3',
252
    address4      => 'address1_3',
253
    postal        => 'postal3',
254
    phone         => 'phone3',
255
    accountnumber => 'accountnumber3',
256
    fax           => 'fax3',
257
    url           => 'url3',
258
    active        => 1,
259
    gstreg        => 1,
260
    listincgst    => 1,
261
    invoiceincgst => 1,
262
    tax_rate      => '3.0000',
263
    discount      => 3.0000,
264
    notes         => 'notes3',
265
    deliverytime  => 3
266
};
267
my $sample_supplier4 = {
268
    name          => 'Name4',
269
    address1      => 'address1_4',
270
    address2      => 'address1-4',
271
    address3      => 'address1_4',
272
    address4      => 'address1_4',
273
    postal        => 'postal4',
274
    phone         => 'phone4',
275
    accountnumber => 'accountnumber4',
276
    fax           => 'fax4',
277
    url           => 'url4',
278
    active        => 1,
279
    gstreg        => 1,
280
    listincgst    => 1,
281
    invoiceincgst => 1,
282
    tax_rate      => '3.0000',
283
    discount      => 3.0000,
284
    notes         => 'notes3',
285
};
286
my $supplier3 = Koha::Acquisition::Bookseller->new($sample_supplier3)->store;
287
my $id_supplier3 = $supplier3->id;
288
my $supplier4 = Koha::Acquisition::Bookseller->new($sample_supplier4)->store;
289
my $id_supplier4 = $supplier4->id;
290
291
#Add 2 baskets
292
my $basketno3 =
293
  C4::Acquisition::NewBasket( $id_supplier3, $patron->borrowernumber, 'basketname3',
294
    'basketnote3' );
295
my $basketno4 =
296
  C4::Acquisition::NewBasket( $id_supplier4, $patron->borrowernumber, 'basketname4',
297
    'basketnote4' );
298
299
#Modify the basket to add a close date
300
my $basket1info = {
301
    basketno     => $basketno1,
302
    closedate    => $today,
303
    booksellerid => $id_supplier1
304
};
305
306
my $basket2info = {
307
    basketno     => $basketno2,
308
    closedate    => $daysago5,
309
    booksellerid => $id_supplier2
310
};
311
312
my $dt_today2 = dt_from_string;
313
my $dur10 = DateTime::Duration->new( days => -10 );
314
$dt_today2->add_duration($dur10);
315
my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
316
my $basket3info = {
317
    basketno  => $basketno3,
318
    closedate => $daysago10,
319
};
320
321
my $basket4info = {
322
    basketno  => $basketno4,
323
    closedate => $today,
324
};
325
ModBasket($basket1info);
326
ModBasket($basket2info);
327
ModBasket($basket3info);
328
ModBasket($basket4info);
329
330
#Add 1 subscription
331
my $id_subscription3 = NewSubscription(
332
    undef,      "BRANCH1",     $id_supplier1, undef, $id_budget, $biblionumber,
333
    '2013-01-01',undef, undef, undef,  undef,
334
    undef,      undef,  undef, undef, undef, undef,
335
    1,          "subscription notes",undef, '2013-01-01', undef, undef,
336
    undef,       undef,  0,    "intnotes",  0,
337
    undef, undef, 0,          'LOCA',         '2013-12-31', 0
338
);
339
340
@subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
341
is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
342
@subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
343
is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
344
@subscriptions = SearchSubscriptions({callnumber => 'CALL'});
345
is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
346
@subscriptions = SearchSubscriptions({callnumber => 'DEF'});
347
is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
348
@subscriptions = SearchSubscriptions({location => 'LOCA'});
349
is(scalar(@subscriptions), 1, 'search for subscriptions by location');
350
351
#Add 4 orders
352
my $order1 = Koha::Acquisition::Order->new(
353
    {
354
        basketno         => $basketno1,
355
        quantity         => 24,
356
        biblionumber     => $biblionumber,
357
        budget_id        => $id_budget,
358
        entrydate        => '2013-01-01',
359
        currency         => $curcode,
360
        notes            => "This is a note1",
361
        tax_rate         => 0.0500,
362
        orderstatus      => 1,
363
        subscriptionid   => $id_subscription1,
364
        quantityreceived => 2,
365
        rrp              => 10,
366
        ecost            => 10,
367
        datereceived     => '2013-06-01'
368
    }
369
)->store;
370
my $ordernumber1 = $order1->ordernumber;
371
372
my $order2 = Koha::Acquisition::Order->new(
373
    {
374
        basketno       => $basketno2,
375
        quantity       => 20,
376
        biblionumber   => $biblionumber,
377
        budget_id      => $id_budget,
378
        entrydate      => '2013-01-01',
379
        currency       => $curcode,
380
        notes          => "This is a note2",
381
        tax_rate       => 0.0500,
382
        orderstatus    => 1,
383
        subscriptionid => $id_subscription2,
384
        rrp            => 10,
385
        ecost          => 10,
386
    }
387
)->store;
388
my $ordernumber2 = $order2->ordernumber;
389
390
my $order3 = Koha::Acquisition::Order->new(
391
    {
392
        basketno       => $basketno3,
393
        quantity       => 20,
394
        biblionumber   => $biblionumber,
395
        budget_id      => $id_budget,
396
        entrydate      => '2013-02-02',
397
        currency       => $curcode,
398
        notes          => "This is a note3",
399
        tax_rate       => 0.0500,
400
        orderstatus    => 2,
401
        subscriptionid => $id_subscription3,
402
        rrp            => 11,
403
        ecost          => 11,
404
    }
405
)->store;
406
my $ordernumber3 = $order3->ordernumber;
407
408
my $order4 = Koha::Acquisition::Order->new(
409
    {
410
        basketno         => $basketno4,
411
        quantity         => 20,
412
        biblionumber     => $biblionumber,
413
        budget_id        => $id_budget,
414
        entrydate        => '2013-02-02',
415
        currency         => $curcode,
416
        notes            => "This is a note3",
417
        tax_rate         => 0.0500,
418
        orderstatus      => 2,
419
        subscriptionid   => $id_subscription3,
420
        rrp              => 11,
421
        ecost            => 11,
422
        quantityreceived => 20
423
    }
424
)->store;
425
my $ordernumber4 = $order4->ordernumber;
426
427
#Test cases:
428
# Sample datas :
429
#   Supplier1: delivery -> undef Basket1 : closedate -> today
430
#   Supplier2: delivery -> 2     Basket2 : closedate -> $daysago5
431
#   Supplier3: delivery -> 3     Basket3 : closedate -> $daysago10
432
#Case 1 : Without parameters:
433
#   quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
434
#   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
435
#   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
436
#   quantityreceived = quantity -NOT LATE-
437
my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
438
ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
439
ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
440
ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
441
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
442
  ;    # Quantity = quantityreceived
443
444
#Case 2: With $delay = 4
445
#    today + 0 > now-$delay -NOT LATE-
446
#    (today-5) + 2   <= now() - $delay -NOT LATE-
447
#    (today-10) + 3  <= now() - $delay -LATE-
448
#    quantityreceived = quantity -NOT LATE-
449
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
450
isnt( exists( $suppliers{$id_supplier1} ),
451
    1, "Supplier1 has late orders but  today  > now() - 4 days" );
452
isnt( exists( $suppliers{$id_supplier2} ),
453
    1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
454
ok( exists( $suppliers{$id_supplier3} ),
455
    "Supplier3 has late orders and $daysago10  <= now() - (4 days+3)" );
456
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
457
458
#Case 3: With $delay = -1
459
my $bslo;
460
warning_like
461
  { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
462
    qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
463
    "GetBooksellerWithLateOrders prints a warning on negative values";
464
465
is( $bslo, undef, "-1 is a wrong value for a delay" );
466
467
#Case 4: With $delay = 0
468
#    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
469
#    today-5   <= now() - $delay+2 -LATE-
470
#    today-10  <= now() - $delay+3 -LATE-
471
#    quantityreceived = quantity -NOT LATE-
472
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
473
474
ok( exists( $suppliers{$id_supplier1} ),
475
    "Supplier1 has late orders but $today == now() - 0 days" )
476
  ;
477
ok( exists( $suppliers{$id_supplier2} ),
478
    "Supplier2 has late orders and $daysago5 <= now() - 2" );
479
ok( exists( $suppliers{$id_supplier3} ),
480
    "Supplier3 has late orders and $daysago10 <= now() - 3" );
481
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
482
483
#Case 5 : With $estimateddeliverydatefrom = today-4
484
#    today >= today-4 -NOT LATE-
485
#    (today-5)+ 2 days >= today-4  -LATE-
486
#    (today-10) + 3 days < today-4   -NOT LATE-
487
#    quantityreceived = quantity -NOT LATE-
488
my $dt_today3 = dt_from_string;
489
my $dur4 = DateTime::Duration->new( days => -4 );
490
$dt_today3->add_duration($dur4);
491
my $daysago4 =  output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
492
%suppliers =
493
  C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
494
495
ok( exists( $suppliers{$id_supplier1} ),
496
    "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
497
ok( exists( $suppliers{$id_supplier2} ),
498
    "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
499
isnt( exists( $suppliers{$id_supplier3} ),
500
    1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
501
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
502
503
#Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
504
#    $daysago10<$daysago5<today -NOT LATE-
505
#    $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
506
#    $daysago10<$daysago10 +3 <$daysago5 -LATE-
507
#    quantityreceived = quantity -NOT LATE-
508
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
509
    $daysago5 );
510
isnt( exists( $suppliers{$id_supplier1} ),
511
    1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
512
isnt(
513
    exists( $suppliers{$id_supplier2} ),
514
    1,
515
    "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
516
);
517
ok(
518
    exists( $suppliers{$id_supplier3} ),
519
"Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
520
);
521
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
522
523
#Case 7: With $estimateddeliverydateto = today-5
524
#    $today >= $daysago5  -NOT LATE-
525
#    $daysago5 + 2 days  > $daysago5 -NOT LATE-
526
#    $daysago10 + 3  <+ $daysago5  -LATE-
527
#    quantityreceived = quantity -NOT LATE-
528
%suppliers =
529
  C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
530
isnt( exists( $suppliers{$id_supplier1} ),
531
    1,
532
    "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
533
isnt( exists( $suppliers{$id_supplier2} ),
534
    1, "Supplier2 has late orders but  $daysago5 + 2 days  > $daysago5 " );
535
ok( exists( $suppliers{$id_supplier3} ),
536
    "Supplier3 has late orders and $daysago10 + 3  <= $daysago5" );
537
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
538
539
#Test with $estimateddeliverydatefrom and  $estimateddeliverydateto and $delay
540
#Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and  $estimateddeliverydateto = 2013-07-08 and $delay =5
541
#    $daysago4<today<=$today and $today<now()-3  -NOT LATE-
542
#    $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
543
#    $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
544
#    quantityreceived = quantity -NOT LATE-
545
%suppliers =
546
  C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
547
isnt(
548
    exists( $suppliers{$id_supplier1} ),
549
    1,
550
    "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
551
);
552
ok(
553
    exists( $suppliers{$id_supplier2} ),
554
"Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
555
);
556
isnt(
557
    exists( $suppliers{$id_supplier3} ),
558
"Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
559
);
560
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
561
562
#Case 9 :With $estimateddeliverydatefrom = $daysago5  and $delay = 3
563
#   $today < $daysago5 and $today > $today-5 -NOT LATE-
564
#   $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2 -LATE-
565
#   $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
566
#   quantityreceived = quantity -NOT LATE-
567
%suppliers =
568
  C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
569
isnt( exists( $suppliers{$id_supplier1} ),
570
    1, "$today < $daysago10 and $today > $today-3" );
571
ok(
572
    exists( $suppliers{$id_supplier2} ),
573
"Supplier2 has late orders and $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2"
574
);
575
isnt(
576
    exists( $suppliers{$id_supplier3} ),
577
    1,
578
"Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
579
);
580
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
581
582
#Test with $estimateddeliverydateto  and $delay
583
#Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
584
#    today > $daysago5 today > now() -5 -NOT LATE-
585
#    $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days -NOT LATE-
586
#    $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
587
#    quantityreceived = quantity -NOT LATE-
588
%suppliers =
589
  C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
590
isnt( exists( $suppliers{$id_supplier1} ),
591
    1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
592
isnt(
593
    exists( $suppliers{$id_supplier2} ),
594
    1,
595
"Supplier2 has late orders but $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days"
596
);
597
ok(
598
    exists( $suppliers{$id_supplier3} ),
599
"Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
600
);
601
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
602
603
#Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
604
#    $daysago10==$daysago10==$daysago10 -NOT LATE-
605
#    $daysago10==$daysago10<$daysago5+2-NOT lATE-
606
#    $daysago10==$daysago10 <$daysago10+3-LATE-
607
#    quantityreceived = quantity -NOT LATE-
608
609
#Basket1 closedate -> $daysago10
610
$basket1info = {
611
    basketno  => $basketno1,
612
    closedate => $daysago10,
613
};
614
ModBasket($basket1info);
615
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
616
    $daysago10 );
617
ok( exists( $suppliers{$id_supplier1} ),
618
    "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
619
  ;
620
isnt( exists( $suppliers{$id_supplier2} ),
621
    1,
622
    "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
623
isnt( exists( $suppliers{$id_supplier3} ),
624
    1,
625
    "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
626
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
627
628
#Case 12: closedate == $estimateddeliverydatefrom =today-10
629
%suppliers =
630
  C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
631
ok( exists( $suppliers{$id_supplier1} ),
632
    "Supplier1 has late orders and $daysago10==$daysago10 " );
633
634
#Case 13: closedate == $estimateddeliverydateto =today-10
635
%suppliers =
636
  C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
637
ok( exists( $suppliers{$id_supplier1} ),
638
    "Supplier1 has late orders and $daysago10==$daysago10 " )
639
  ;
640
641
t::lib::Mocks::mock_userenv({ flags => 0, branchcode => 'BRANCH1' });
642
643
my $module = Test::MockModule->new('C4::Auth');
644
$module->mock(
645
    'haspermission',
646
    sub {
647
        # simulate user that has serials permissions but
648
        # NOT superserials
649
        my ($userid, $flagsrequired) = @_;
650
        return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
651
        return exists($flagsrequired->{serials});
652
    }
653
);
654
655
t::lib::Mocks::mock_preference('IndependentBranches', 0);
656
@subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
657
is(
658
    scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
659
    3,
660
    'ordinary user can see all subscriptions with IndependentBranches off'
661
);
662
663
t::lib::Mocks::mock_preference('IndependentBranches', 1);
664
@subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
665
is(
666
    scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
667
    1,
668
    'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
669
);
670
671
# don the cape and turn into Superlibrarian!
672
t::lib::Mocks::mock_userenv({ flags => 1, branchcode => 'BRANCH1' });
673
@subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
674
is(
675
    scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
676
    3,
677
    'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
678
);
679
680
#Test contact editing
681
my $sample_supplier = {
682
        name     => "my vendor",
683
        address1 => "bookseller's address",
684
        phone    => "0123456",
685
        active   => 1
686
    };
687
my $supplier = Koha::Acquisition::Bookseller->new($sample_supplier)->store;
688
my $booksellerid = $supplier->id;
689
my $contact1 = Koha::Acquisition::Bookseller::Contact->new({
690
        name => 'John Smith',
691
        phone => '0123456x1',
692
        booksellerid => $booksellerid,
693
})->store;
694
my $contact2 = Koha::Acquisition::Bookseller::Contact->new({
695
        name => 'Leo Tolstoy',
696
        phone => '0123456x2',
697
        booksellerid => $booksellerid,
698
})->store;
699
700
@booksellers = Koha::Acquisition::Booksellers->search({ name => 'my vendor' });
701
ok(
702
    ( grep { $_->id == $booksellerid } @booksellers ),
703
    'Koha::Acquisition::Booksellers->search returns correct record when passed a name'
704
);
705
706
my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
707
is( $bookseller->id, $booksellerid, 'Retrieved desired record' );
708
is( $bookseller->phone, '0123456', 'New bookseller has expected phone' );
709
my $contacts = $bookseller->contacts;
710
is( $contacts->count,
711
    2, 'bookseller should have 2 contacts' );
712
my $first_contact = $contacts->next;
713
is(
714
    ref $first_contact,
715
    'Koha::Acquisition::Bookseller::Contact',
716
    'First contact is a AqContact'
717
);
718
is( $first_contact->phone,
719
    '0123456x1', 'Contact has expected phone number' );
720
721
my $second_contact = $contacts->next;
722
$second_contact->delete;
723
$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
724
$bookseller->name('your vendor')->store;
725
$contacts = $bookseller->contacts;
726
$first_contact = $contacts->next;
727
$first_contact->phone('654321');
728
$first_contact->store;
729
730
$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
731
is( $bookseller->name, 'your vendor',
732
    'Successfully changed name of vendor' );
733
$contacts = $bookseller->contacts;
734
is( $contacts->count,
735
    1, 'Only one contact after modification' );
736
$first_contact = $contacts->next;
737
is( $first_contact->phone,
738
    '654321',
739
    'Successfully changed contact phone number by modifying bookseller hash' );
740
741
$first_contact->name( 'John Jacob Jingleheimer Schmidt' );
742
$first_contact->phone(undef);
743
$first_contact->store;
744
745
$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
746
$contacts = $bookseller->contacts;
747
$first_contact = $contacts->next;
748
is(
749
    $first_contact->name,
750
    'John Jacob Jingleheimer Schmidt',
751
    'Changed name of contact'
752
);
753
is( $first_contact->phone,
754
    undef, 'Removed phone number from contact' );
755
is( $contacts->count,
756
    1, 'Only one contact after modification' );
757
758
#End transaction
759
$schema->storage->txn_rollback();
760
761
#field_filter filters the useless fields or foreign keys
762
#NOTE: all the fields of aqbookseller arent considered
763
#returns a cleaned structure
764
sub field_filter {
765
    my ($struct) = @_;
766
767
    for my $field ( qw(invoiceprice listprice contacts) )
768
    {
769
770
        if ( grep { /^$field$/ } keys %$struct ) {
771
            delete $struct->{$field};
772
        }
773
    }
774
    return $struct;
775
}
776
777
# ensure numbers are actually tested as numbers to prevent
778
# precision changes causing test failures (D8->D9 Upgrades)
779
sub cast_precision {
780
    my ($struct) = @_;
781
    my @cast = ('discount');
782
    for my $cast (@cast) {
783
        $struct->{$cast} = $struct->{$cast}+0;
784
    }
785
    return $struct;
786
}
787
- 

Return to bug 25266