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

(-)a/C4/Bookseller.pm (-17 / +25 lines)
Lines 64-70 sub GetBookSeller { Link Here
64
    my $searchstring = shift;
64
    my $searchstring = shift;
65
    $searchstring = q{%} . $searchstring . q{%};
65
    $searchstring = q{%} . $searchstring . q{%};
66
    my $query = "
66
    my $query = "
67
        SELECT aqbooksellers.*, count(*) AS basketcount
67
        SELECT aqbooksellers.*, count(aqbasket.basketname) AS basketcount
68
        FROM aqbooksellers
68
        FROM aqbooksellers
69
        LEFT JOIN aqbasket ON aqbasket.booksellerid = aqbooksellers.id
69
        LEFT JOIN aqbasket ON aqbasket.booksellerid = aqbooksellers.id
70
        WHERE name LIKE ? GROUP BY aqbooksellers.id ORDER BY name
70
        WHERE name LIKE ? GROUP BY aqbooksellers.id ORDER BY name
Lines 77-82 sub GetBookSeller { Link Here
77
    return @{$resultset_ref};
77
    return @{$resultset_ref};
78
}
78
}
79
79
80
81
80
sub GetBookSellerFromId {
82
sub GetBookSellerFromId {
81
    my $id = shift or return;
83
    my $id = shift or return;
82
    my $dbh = C4::Context->dbh;
84
    my $dbh = C4::Context->dbh;
Lines 110-120 sub GetBooksellersWithLateOrders { Link Here
110
112
111
    # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
113
    # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
112
    # should be tested with other DBMs
114
    # should be tested with other DBMs
113
115
    my $query;
114
    my $strsth;
115
    my @query_params = ();
116
    my @query_params = ();
116
    my $dbdriver = C4::Context->config("db_scheme") || "mysql";
117
    my $dbdriver = C4::Context->config("db_scheme") || "mysql";
117
    $strsth = "
118
    $query = "
118
        SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
119
        SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
119
        FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
120
        FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
120
        LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
121
        LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
Lines 128-151 sub GetBooksellersWithLateOrders { Link Here
128
            AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
129
            AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
129
            AND aqbasket.closedate IS NOT NULL
130
            AND aqbasket.closedate IS NOT NULL
130
    ";
131
    ";
131
    if ( defined $delay ) {
132
    if ( defined $delay && $delay >= 0 ) {
132
        $strsth .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) ";
133
        $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime, 0) DAY)) ";
133
        push @query_params, $delay;
134
        push @query_params, $delay;
134
    }
135
    }
136
    elsif ( defined $delay && $delay < 0 ){
137
        warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
138
        return;
139
    }
135
    if ( defined $estimateddeliverydatefrom ) {
140
    if ( defined $estimateddeliverydatefrom ) {
136
        $strsth .= '
141
        $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) >= ?';
137
            AND aqbooksellers.deliverytime IS NOT NULL
138
            AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
139
        push @query_params, $estimateddeliverydatefrom;
142
        push @query_params, $estimateddeliverydatefrom;
143
        if ( defined $estimateddeliverydateto ) {
144
            $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
145
            push @query_params, $estimateddeliverydateto;
146
        } elsif ( !defined $estimateddeliverydateto ) {
147
            $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
148
        }
140
    }
149
    }
141
    if ( defined $estimateddeliverydatefrom and defined $estimateddeliverydateto ) {
150
    if ( defined $estimateddeliverydateto ) {
142
        $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
151
        $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
143
        push @query_params, $estimateddeliverydateto;
152
        push @query_params, $estimateddeliverydateto;
144
    } elsif ( defined $estimateddeliverydatefrom ) {
145
        $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
146
    }
153
    }
147
154
148
    my $sth = $dbh->prepare($strsth);
155
    my $sth = $dbh->prepare($query);
149
    $sth->execute( @query_params );
156
    $sth->execute( @query_params );
150
    my %supplierlist;
157
    my %supplierlist;
151
    while ( my ( $id, $name ) = $sth->fetchrow ) {
158
    while ( my ( $id, $name ) = $sth->fetchrow ) {
Lines 155-160 sub GetBooksellersWithLateOrders { Link Here
155
    return %supplierlist;
162
    return %supplierlist;
156
}
163
}
157
164
165
158
#--------------------------------------------------------------------#
166
#--------------------------------------------------------------------#
159
167
160
=head2 AddBookseller
168
=head2 AddBookseller
Lines 226-231 C<&ModBookseller> with the result. Link Here
226
sub ModBookseller {
234
sub ModBookseller {
227
    my ($data) = @_;
235
    my ($data) = @_;
228
    my $dbh    = C4::Context->dbh;
236
    my $dbh    = C4::Context->dbh;
237
    return unless $data->{'id'};
229
    my $query  = 'UPDATE aqbooksellers
238
    my $query  = 'UPDATE aqbooksellers
230
        SET name=?,address1=?,address2=?,address3=?,address4=?,
239
        SET name=?,address1=?,address2=?,address3=?,address4=?,
231
            postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?,
240
            postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?,
Lines 253-259 sub ModBookseller { Link Here
253
        $data->{deliverytime},
262
        $data->{deliverytime},
254
        $data->{'id'}
263
        $data->{'id'}
255
    );
264
    );
256
    return;
257
}
265
}
258
266
259
=head2 DelBookseller
267
=head2 DelBookseller
Lines 269-276 sub DelBookseller { Link Here
269
    my $id  = shift;
277
    my $id  = shift;
270
    my $dbh = C4::Context->dbh;
278
    my $dbh = C4::Context->dbh;
271
    my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
279
    my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
272
    $sth->execute($id);
280
    return $sth->execute($id);
273
    return;
281
274
}
282
}
275
283
276
1;
284
1;
(-)a/t/db_dependent/Bookseller.t (-1 / +662 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 67;
6
use C4::Context;
7
use Koha::DateUtils;
8
use DateTime::Duration;
9
10
BEGIN {
11
    use_ok('C4::Bookseller');
12
    use_ok('C4::Acquisition');
13
    use_ok('C4::Serials');
14
}
15
16
can_ok(
17
18
    'C4::Bookseller', qw(
19
      AddBookseller
20
      DelBookseller
21
      GetBookSeller
22
      GetBookSellerFromId
23
      GetBooksellersWithLateOrders
24
      ModBookseller )
25
);
26
27
#Start transaction
28
my $dbh = C4::Context->dbh;
29
$dbh->{RaiseError} = 1;
30
$dbh->{AutoCommit} = 0;
31
32
#Start tests
33
34
#Test AddBookseller
35
my $count            = scalar( C4::Bookseller::GetBookSeller('') );
36
my $sample_supplier1 = {
37
    name          => 'Name1',
38
    address1      => 'address1_1',
39
    address2      => 'address1-2',
40
    address3      => 'address1_2',
41
    address4      => 'address1_2',
42
    postal        => 'postal1',
43
    phone         => 'phone1',
44
    accountnumber => 'accountnumber1',
45
    fax           => 'fax1',
46
    url           => 'url1',
47
    contact       => 'contact1',
48
    contpos       => 'contpos1',
49
    contphone     => 'contphone1',
50
    contfax       => 'contefax1',
51
    contaltphone  => 'contaltphone1',
52
    contemail     => 'contemail1',
53
    contnotes     => 'contnotes1',
54
    active        => 1,
55
    gstreg        => 1,
56
    listincgst    => 1,
57
    invoiceincgst => 1,
58
    gstrate       => '1.0000',
59
    discount      => '1.0000',
60
    notes         => 'notes1',
61
    deliverytime  => undef
62
};
63
my $sample_supplier2 = {
64
    name          => 'Name2',
65
    address1      => 'address1_2',
66
    address2      => 'address2-2',
67
    address3      => 'address3_2',
68
    address4      => 'address4_2',
69
    postal        => 'postal2',
70
    phone         => 'phone2',
71
    accountnumber => 'accountnumber2',
72
    fax           => 'fax2',
73
    url           => 'url2',
74
    contact       => 'contact2',
75
    contpos       => 'contpos2',
76
    contphone     => 'contphone2',
77
    contfax       => 'contefax2',
78
    contaltphone  => 'contaltphone2',
79
    contemail     => 'contemail2',
80
    contnotes     => 'contnotes2',
81
    active        => 1,
82
    gstreg        => 1,
83
    listincgst    => 1,
84
    invoiceincgst => 1,
85
    gstrate       => '2.0000',
86
    discount      => '2.0000',
87
    notes         => 'notes2',
88
    deliverytime  => 2,
89
};
90
91
my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
92
my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
93
94
#my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
95
96
like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
97
like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
98
is( scalar( C4::Bookseller::GetBookSeller('') ),
99
    $count + 2, "Supplier1 and Supplier2 have been added" );
100
101
#Test DelBookseller
102
my $del = C4::Bookseller::DelBookseller($id_supplier1);
103
is( $del, 1, "DelBookseller returns 1" );
104
is( C4::Bookseller::GetBookSellerFromId($id_supplier1),
105
    undef, "Supplier1  has been deleted - id_supplier1 doesnt exist anymore" );
106
107
#Test GetBookSeller
108
my @bookseller2 = C4::Bookseller::GetBookSeller( $sample_supplier2->{name} );
109
is( scalar(@bookseller2), 1, "Get only  Supplier2" );
110
$bookseller2[0] = field_filter( $bookseller2[0] );
111
delete $bookseller2[0]->{basketcount};
112
113
$sample_supplier2->{id} = $id_supplier2;
114
is_deeply( $bookseller2[0], $sample_supplier2,
115
    "GetBookSeller returns the right informations about $sample_supplier2" );
116
117
$id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
118
my @booksellers = C4::Bookseller::GetBookSeller('')
119
  ;    #NOTE :without params, it returns all the booksellers
120
for my $i ( 0 .. scalar(@booksellers) - 1 ) {
121
    $booksellers[$i] = field_filter( $booksellers[$i] );
122
    delete $booksellers[$i]->{basketcount};
123
}
124
125
$sample_supplier1->{id} = $id_supplier1;
126
is( scalar(@booksellers), $count + 2, "Get  Supplier1 and Supplier2" );
127
my @tab = ( $sample_supplier1, $sample_supplier2 );
128
is_deeply( \@booksellers, \@tab,
129
    "Returns right fields of Supplier1 and Supllier2" );
130
131
#Test basketcount
132
my @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
133
is( $bookseller1[0]->{basketcount}, 0, 'Supplier1 has 0 basket' );
134
my $sample_basket1 =
135
  C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
136
my $sample_basket2 =
137
  C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
138
@bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
139
is( $bookseller1[0]->{basketcount}, 2, 'Supplier1 has 2 baskets' );
140
141
#Test GetBookSellerFromId
142
my $bookseller1fromid = C4::Bookseller::GetBookSellerFromId();
143
is( $bookseller1fromid, undef,
144
    "GetBookSellerFromId returns undef if no id given" );
145
$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
146
$bookseller1fromid = field_filter($bookseller1fromid);
147
delete $bookseller1fromid->{basketcount};
148
delete $bookseller1fromid->{subscriptioncount};
149
is_deeply( $bookseller1fromid, $sample_supplier1,
150
    "Get Supplier1 (GetBookSellerFromId)" );
151
152
#Test basketcount
153
$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
154
is( $bookseller1fromid->{basketcount}, 2, 'Supplier1 has 2 baskets' );
155
156
#Test subscriptioncount
157
my $id_budget    = 1;
158
my $biblionumber = 1;
159
$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
160
is( $bookseller1fromid->{subscriptioncount},
161
    0, 'Supplier1 has 0 subscription' );
162
my $id_subscription1 = C4::Serials::NewSubscription(
163
    undef,      "",            $id_supplier1, undef,
164
    $id_budget, $biblionumber, '01-01-2013',  undef,
165
    undef,      undef,         undef,         undef,
166
    undef,      undef,         undef,         undef,
167
    undef,      undef,         undef,         undef,
168
    undef,      undef,         undef,         undef,
169
    undef,      undef,         undef,         undef,
170
    undef,      undef,         undef,         1,
171
    "notes",    undef,         undef,         undef,
172
    undef,      undef,         undef,         0,
173
    "intnotes", 0,             undef,         undef,
174
    0,          undef,         '31-12-2013',
175
);
176
my $id_subscription2 = C4::Serials::NewSubscription(
177
    undef,      "",            $id_supplier1, undef,
178
    $id_budget, $biblionumber, '01-01-2013',  undef,
179
    undef,      undef,         undef,         undef,
180
    undef,      undef,         undef,         undef,
181
    undef,      undef,         undef,         undef,
182
    undef,      undef,         undef,         undef,
183
    undef,      undef,         undef,         undef,
184
    undef,      undef,         undef,         1,
185
    "notes",    undef,         undef,         undef,
186
    undef,      undef,         undef,         0,
187
    "intnotes", 0,             undef,         undef,
188
    0,          undef,         '31-12-2013',
189
);
190
$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
191
is( $bookseller1fromid->{subscriptioncount},
192
    2, 'Supplier1 has 2 subscriptions' );
193
194
#Test ModBookseller
195
$sample_supplier2 = {
196
    id            => $id_supplier2,
197
    name          => 'Name2 modified',
198
    address1      => 'address1_2 modified',
199
    address2      => 'address2-2 modified',
200
    address3      => 'address3_2 modified',
201
    address4      => 'address4_2 modified',
202
    postal        => 'postal2 modified',
203
    phone         => 'phone2 modified',
204
    accountnumber => 'accountnumber2 modified',
205
    fax           => 'fax2 modified',
206
    url           => 'url2 modified',
207
    contact       => 'contact2 modified',
208
    contpos       => 'contpos2 modified',
209
    contphone     => 'contphone2 modified',
210
    contfax       => 'contefax2 modified',
211
    contaltphone  => 'contaltphone2 modified',
212
    contemail     => 'contemail2 modified',
213
    contnotes     => 'contnotes2 modified',
214
    active        => 1,
215
    gstreg        => 1,
216
    listincgst    => 1,
217
    invoiceincgst => 1,
218
    gstrate       => '2.0000 ',
219
    discount      => '2.0000',
220
    notes         => 'notes2 modified',
221
    deliverytime  => 2,
222
};
223
224
my $modif1 = C4::Bookseller::ModBookseller();
225
is( $modif1, undef,
226
    "ModBookseller returns undef if no params given - Nothing happened" );
227
$modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
228
is( $modif1, 1, "ModBookseller modifies only the supplier2" );
229
is( scalar( C4::Bookseller::GetBookSeller('') ),
230
    $count + 2, "Supplier2 has been modified - Nothing added" );
231
232
$modif1 = C4::Bookseller::ModBookseller(
233
    {
234
        id   => -1,
235
        name => 'name3'
236
    }
237
);
238
is( $modif1, '0E0',
239
    "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
240
241
#Test GetBooksellersWithLateOrders
242
#Add 2 suppliers
243
my $sample_supplier3 = {
244
    name          => 'Name3',
245
    address1      => 'address1_3',
246
    address2      => 'address1-3',
247
    address3      => 'address1_3',
248
    address4      => 'address1_3',
249
    postal        => 'postal3',
250
    phone         => 'phone3',
251
    accountnumber => 'accountnumber3',
252
    fax           => 'fax3',
253
    url           => 'url3',
254
    contact       => 'contact3',
255
    contpos       => 'contpos3',
256
    contphone     => 'contphone3',
257
    contfax       => 'contefax3',
258
    contaltphone  => 'contaltphone3',
259
    contemail     => 'contemail3',
260
    contnotes     => 'contnotes3',
261
    active        => 1,
262
    gstreg        => 1,
263
    listincgst    => 1,
264
    invoiceincgst => 1,
265
    gstrate       => '3.0000',
266
    discount      => '3.0000',
267
    notes         => 'notes3',
268
    deliverytime  => 3
269
};
270
my $sample_supplier4 = {
271
    name          => 'Name4',
272
    address1      => 'address1_4',
273
    address2      => 'address1-4',
274
    address3      => 'address1_4',
275
    address4      => 'address1_4',
276
    postal        => 'postal4',
277
    phone         => 'phone4',
278
    accountnumber => 'accountnumber4',
279
    fax           => 'fax4',
280
    url           => 'url4',
281
    contact       => 'contact4',
282
    contpos       => 'contpos4',
283
    contphone     => 'contphone4',
284
    contfax       => 'contefax4',
285
    contaltphone  => 'contaltphone4',
286
    contemail     => 'contemail4',
287
    contnotes     => 'contnotes4',
288
    active        => 1,
289
    gstreg        => 1,
290
    listincgst    => 1,
291
    invoiceincgst => 1,
292
    gstrate       => '3.0000',
293
    discount      => '3.0000',
294
    notes         => 'notes3',
295
};
296
my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
297
my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
298
299
#Add 2 baskets
300
my $sample_basket3 =
301
  C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
302
    'basketnote3' );
303
my $sample_basket4 =
304
  C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
305
    'basketnote4' );
306
307
#Modify the basket to add a close date
308
my $dt_today    = dt_from_string;
309
my $today       = output_pref( $dt_today, 'iso', '24hr', 1 );
310
my $basket1info = {
311
    basketno     => $sample_basket1,
312
    closedate    => $today,
313
    booksellerid => $id_supplier1
314
};
315
316
my $dt_today1 = dt_from_string;
317
my $dur5 = DateTime::Duration->new( days => -5 );
318
$dt_today1->add_duration($dur5);
319
my $daysago5 = output_pref( $dt_today1, 'iso', '24hr', 1 );
320
my $basket2info = {
321
    basketno     => $sample_basket2,
322
    closedate    => $daysago5,
323
    booksellerid => $id_supplier2
324
};
325
326
my $dt_today2 = dt_from_string;
327
my $dur10 = DateTime::Duration->new( days => -10 );
328
$dt_today2->add_duration($dur10);
329
my $daysago10 = output_pref( $dt_today2, 'iso', '24hr', 1 );
330
my $basket3info = {
331
    basketno  => $sample_basket3,
332
    closedate => $daysago10,
333
};
334
335
my $basket4info = {
336
    basketno  => $sample_basket4,
337
    closedate => $today,
338
};
339
ModBasket($basket1info);
340
ModBasket($basket2info);
341
ModBasket($basket3info);
342
ModBasket($basket4info);
343
344
#Add 1 subscription
345
my $id_subscription3 = C4::Serials::NewSubscription(
346
    undef,      "",            $id_supplier3, undef,
347
    $id_budget, $biblionumber, '01-01-2013',  undef,
348
    undef,      undef,         undef,         undef,
349
    undef,      undef,         undef,         undef,
350
    undef,      undef,         undef,         undef,
351
    undef,      undef,         undef,         undef,
352
    undef,      undef,         undef,         undef,
353
    undef,      undef,         undef,         1,
354
    "notes",    undef,         undef,         undef,
355
    undef,      undef,         undef,         0,
356
    "intnotes", 0,             undef,         undef,
357
    0,          undef,         '31-12-2013',
358
);
359
360
#Add 4 orders
361
my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
362
my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
363
( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
364
    {
365
        basketno         => $sample_basket1,
366
        quantity         => 24,
367
        biblionumber     => $biblionumber,
368
        budget_id        => $id_budget,
369
        entrydate        => '01-01-2013',
370
        currency         => 'EUR',
371
        notes            => "This is a note1",
372
        gstrate          => 0.0500,
373
        orderstatus      => 1,
374
        subscriptionid   => $id_subscription1,
375
        quantityreceived => 2,
376
        rrp              => 10,
377
        ecost            => 10,
378
        datereceived     => '01-06-2013'
379
    }
380
);
381
( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
382
    {
383
        basketno       => $sample_basket2,
384
        quantity       => 20,
385
        biblionumber   => $biblionumber,
386
        budget_id      => $id_budget,
387
        entrydate      => '01-01-2013',
388
        currency       => 'EUR',
389
        notes          => "This is a note2",
390
        gstrate        => 0.0500,
391
        orderstatus    => 1,
392
        subscriptionid => $id_subscription2,
393
        rrp            => 10,
394
        ecost          => 10,
395
    }
396
);
397
( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
398
    {
399
        basketno       => $sample_basket3,
400
        quantity       => 20,
401
        biblionumber   => $biblionumber,
402
        budget_id      => $id_budget,
403
        entrydate      => '02-02-2013',
404
        currency       => 'EUR',
405
        notes          => "This is a note3",
406
        gstrate        => 0.0500,
407
        orderstatus    => 2,
408
        subscriptionid => $id_subscription3,
409
        rrp            => 11,
410
        ecost          => 11,
411
    }
412
);
413
( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
414
    {
415
        basketno         => $sample_basket4,
416
        quantity         => 20,
417
        biblionumber     => $biblionumber,
418
        budget_id        => $id_budget,
419
        entrydate        => '02-02-2013',
420
        currency         => 'EUR',
421
        notes            => "This is a note3",
422
        gstrate          => 0.0500,
423
        orderstatus      => 2,
424
        subscriptionid   => $id_subscription3,
425
        rrp              => 11,
426
        ecost            => 11,
427
        quantityreceived => 20
428
    }
429
);
430
431
#Test cases:
432
# Sample datas :
433
#   Supplier1: delivery -> undef Basket1 : closedate -> today
434
#   Supplier2: delivery -> 2     Basket2 : closedate -> $daysago5
435
#   Supplier3: delivery -> 3     Basket3 : closedate -> $daysago10
436
#Case 1 : Without parameters:
437
#   quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
438
#   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
439
#   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
440
#   quantityreceived = quantity -NOT LATE-
441
my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
442
ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
443
ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
444
ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
445
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
446
  ;    # Quantity = quantityreceived
447
448
#Case 2: With $delay = 4
449
#    today + 0 > now-$delay -NOT LATE-
450
#    (today-5) + 2   <= now() - $delay -NOT LATE-
451
#    (today-10) + 3  <= now() - $delay -LATE-
452
#    quantityreceived = quantity -NOT LATE-
453
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
454
isnt( exists( $suppliers{$id_supplier1} ),
455
    1, "Supplier1 has late orders but  today  > now() - 4 days" );
456
isnt( exists( $suppliers{$id_supplier2} ),
457
    1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
458
ok( exists( $suppliers{$id_supplier3} ),
459
    "Supplier3 has late orders and $daysago10  <= now() - (4 days+3)" );
460
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
461
462
#Case 3: With $delay = -1
463
is( C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ),
464
    undef, "-1 is a wrong value for a delay" );
465
466
#Case 4: With $delay = 0
467
#    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
468
#    today-5   <= now() - $delay+2 -LATE-
469
#    today-10  <= now() - $delay+3 -LATE-
470
#    quantityreceived = quantity -NOT LATE-
471
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
472
473
ok( exists( $suppliers{$id_supplier1} ),
474
    "Supplier1 has late orders but $today == now() - 0 days" )
475
  ;
476
ok( exists( $suppliers{$id_supplier2} ),
477
    "Supplier2 has late orders and $daysago5 <= now() - 2" );
478
ok( exists( $suppliers{$id_supplier3} ),
479
    "Supplier3 has late orders and $daysago10 <= now() - 3" );
480
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
481
482
#Case 5 : With $estimateddeliverydatefrom = today-4 #NOTE if a supplier didnt fill in deliverytime is it late?
483
#    today >= today-4 -NOT LATE-
484
#    (today-5)+ 2 days >= today-4  -LATE-
485
#    (today-10) + 3 days < today-4   -NOT LATE-
486
#    quantityreceived = quantity -NOT LATE-
487
my $dt_today3 = dt_from_string;
488
my $dur4 = DateTime::Duration->new( days => -4 );
489
$dt_today3->add_duration($dur4);
490
my $daysago4 = output_pref( $dt_today3, 'iso', '24hr', 1 );
491
%suppliers =
492
  C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
493
ok( exists( $suppliers{$id_supplier1} ),
494
    "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
495
ok( exists( $suppliers{$id_supplier2} ),
496
    "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
497
isnt( exists( $suppliers{$id_supplier3} ),
498
    1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
499
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
500
501
#Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
502
#    $daysago10<$daysago5<today -NOT LATE-
503
#    $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
504
#    $daysago10<$daysago10 +3 <$daysago5 -LATE-
505
#    quantityreceived = quantity -NOT LATE-
506
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
507
    $daysago5 );
508
isnt( exists( $suppliers{$id_supplier1} ),
509
    1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
510
isnt(
511
    exists( $suppliers{$id_supplier2} ),
512
    1,
513
    "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
514
);
515
ok(
516
    exists( $suppliers{$id_supplier3} ),
517
"Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
518
);
519
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
520
521
#Case 7: With $estimateddeliverydateto = today-5
522
#    $today >= $daysago5  -NOT LATE-
523
#    $daysago5 + 2 days  > $daysago5 -NOT LATE-
524
#    $daysago10 + 3  <+ $daysago5  -LATE-
525
#    quantityreceived = quantity -NOT LATE-
526
%suppliers =
527
  C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
528
isnt( exists( $suppliers{$id_supplier1} ),
529
    1,
530
    "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
531
isnt( exists( $suppliers{$id_supplier2} ),
532
    1, "Supplier2 has late orders but  $daysago5 + 2 days  > $daysago5 " );
533
ok( exists( $suppliers{$id_supplier3} ),
534
    "Supplier3 has late orders and $daysago10 + 3  <= $daysago5" );
535
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
536
537
#Test with $estimateddeliverydatefrom and  $estimateddeliverydateto and $delay
538
#Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and  $estimateddeliverydateto = 2013-07-08 and $delay =5
539
#    $daysago4<today<=$today and $today<now()-3  -NOT LATE-
540
#    $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
541
#    $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
542
#    quantityreceived = quantity -NOT LATE-
543
%suppliers =
544
  C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
545
isnt(
546
    exists( $suppliers{$id_supplier1} ),
547
    1,
548
    "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
549
);
550
ok(
551
    exists( $suppliers{$id_supplier2} ),
552
"Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
553
);
554
isnt(
555
    exists( $suppliers{$id_supplier3} ),
556
"Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
557
);
558
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
559
560
#Case 9 :With $estimateddeliverydatefrom = $daysago5  and $delay = 3
561
#   $today < $daysago5 and $today > $today-5 -NOT LATE-
562
#   $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2 -LATE-
563
#   $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
564
#   quantityreceived = quantity -NOT LATE-
565
%suppliers =
566
  C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
567
isnt( exists( $suppliers{$id_supplier1} ),
568
    1, "$today < $daysago10 and $today > $today-3" );
569
ok(
570
    exists( $suppliers{$id_supplier2} ),
571
"Supplier2 has late orders and $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2"
572
);
573
isnt(
574
    exists( $suppliers{$id_supplier3} ),
575
    1,
576
"Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
577
);
578
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
579
580
#Test with $estimateddeliverydateto  and $delay
581
#Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
582
#    today > $daysago5 today > now() -5 -NOT LATE-
583
#    $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days -NOT LATE-
584
#    $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
585
#    quantityreceived = quantity -NOT LATE-
586
%suppliers =
587
  C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
588
isnt( exists( $suppliers{$id_supplier1} ),
589
    1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
590
isnt(
591
    exists( $suppliers{$id_supplier2} ),
592
    1,
593
"Supplier2 has late orders but $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days"
594
);
595
ok(
596
    exists( $suppliers{$id_supplier3} ),
597
"Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
598
);
599
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
600
601
#Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
602
#    $daysago10==$daysago10==$daysago10 -NOT LATE-
603
#    $daysago10==$daysago10<$daysago5+2-NOT lATE-
604
#    $daysago10==$daysago10 <$daysago10+3-LATE-
605
#    quantityreceived = quantity -NOT LATE-
606
607
#Basket1 closedate -> $daysago10
608
$basket1info = {
609
    basketno  => $sample_basket1,
610
    closedate => $daysago10,
611
};
612
ModBasket($basket1info);
613
%suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
614
    $daysago10 );
615
ok( exists( $suppliers{$id_supplier1} ),
616
    "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
617
  ;
618
isnt( exists( $suppliers{$id_supplier2} ),
619
    1,
620
    "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
621
isnt( exists( $suppliers{$id_supplier3} ),
622
    1,
623
    "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
624
isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
625
626
#Case 12: closedate == $estimateddeliverydatefrom =today-10
627
%suppliers =
628
  C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
629
ok( exists( $suppliers{$id_supplier1} ),
630
    "Supplier1 has late orders and $daysago10==$daysago10 " );
631
632
#Case 13: closedate == $estimateddeliverydateto =today-10
633
%suppliers =
634
  C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
635
ok( exists( $suppliers{$id_supplier1} ),
636
    "Supplier1 has late orders and $daysago10==$daysago10 " )
637
  ;
638
639
#End transaction
640
$dbh->rollback;
641
642
#field_filter filters the useless fields or foreign keys
643
#NOTE: all the fields of aqbookseller arent considered
644
#returns a cleaned structure
645
sub field_filter {
646
    my ($struct) = @_;
647
648
    for my $field (
649
        'bookselleremail', 'booksellerfax',
650
        'booksellerurl',   'othersupplier',
651
        'currency',        'invoiceprice',
652
        'listprice'
653
      )
654
    {
655
656
        if ( grep { /^$field$/ } keys %$struct ) {
657
            delete $struct->{$field};
658
        }
659
    }
660
    return $struct;
661
}
662

Return to bug 10528