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

(-)a/t/db_dependent/Koha/MarcOrder.t (-1 / +701 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha
4
#
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.
9
#
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.
14
#
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>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 6;
21
22
use Koha::MarcOrder;
23
use Koha::Acquisition::Baskets;
24
use Koha::Acquisition::Bookseller;
25
use MARC::Record;
26
use Koha::Import::Records;
27
use Koha::Database;
28
29
use t::lib::Mocks;
30
use t::lib::TestBuilder;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
36
subtest '_get_MarcItemFieldsToOrder_syspref_data()' => sub {
37
    plan tests => 14;
38
39
    $schema->storage->txn_begin;
40
41
    t::lib::Mocks::mock_preference( 'MarcItemFieldsToOrder', 'homebranch: 975$a
42
holdingbranch: 975$b
43
itype: 975$y
44
nonpublic_note: 975$x
45
public_note: 975$z
46
loc: 975$c
47
ccode: 975$8
48
notforloan: 975$7
49
uri: 975$u
50
copyno: 975$n
51
quantity: 975$q
52
budget_code: 975$h
53
price: 975$p
54
replacementprice: 975$v' );
55
56
    my $record = MARC::Record->new();
57
58
    $record->add_fields(
59
        [ '001', '1234' ],
60
        [
61
            '975', ' ', ' ', p => 10, q => 1, h => 1, a => 'CPL', b => 'CPL', y => 'Book',
62
            x => 'Private note', z => 'Public note', c => 'Shelf', 8 => 'ccode', 7 => 0,
63
            u => '12345abcde',   n => '12345',       v => 10
64
        ],
65
    );
66
67
    my $marc_item_fields_to_order = Koha::MarcOrder::_get_MarcItemFieldsToOrder_syspref_data(
68
        $record,
69
    );
70
71
72
    is(
73
        $marc_item_fields_to_order->{price}, 10,
74
        "price has been read correctly"
75
    );
76
    is(
77
        $marc_item_fields_to_order->{quantity}, 1,
78
        "quantity has been read correctly"
79
    );
80
    is(
81
        $marc_item_fields_to_order->{budget_code}, 1,
82
        "budget_code has been read correctly"
83
    );
84
    is(
85
        $marc_item_fields_to_order->{homebranch}, 'CPL',
86
        "homebranch has been read correctly"
87
    );
88
    is(
89
        $marc_item_fields_to_order->{holdingbranch}, 'CPL',
90
        "holdingbranch has been read correctly"
91
    );
92
    is(
93
        $marc_item_fields_to_order->{itype}, 'Book',
94
        "itype has been read correctly"
95
    );
96
    is(
97
        $marc_item_fields_to_order->{nonpublic_note}, 'Private note',
98
        "nonpublic_note has been read correctly"
99
    );
100
    is(
101
        $marc_item_fields_to_order->{public_note}, 'Public note',
102
        "public_note has been read correctly"
103
    );
104
    is(
105
        $marc_item_fields_to_order->{loc}, 'Shelf',
106
        "loc has been read correctly"
107
    );
108
    is(
109
        $marc_item_fields_to_order->{ccode}, 'ccode',
110
        "ccode has been read correctly"
111
    );
112
    is(
113
        $marc_item_fields_to_order->{notforloan}, 0,
114
        "notforloan has been read correctly"
115
    );
116
    is(
117
        $marc_item_fields_to_order->{uri}, '12345abcde',
118
        "uri has been read correctly"
119
    );
120
    is(
121
        $marc_item_fields_to_order->{copyno}, '12345',
122
        "copyno has been read correctly"
123
    );
124
    is(
125
        $marc_item_fields_to_order->{replacementprice}, 10,
126
        "replacementprice has been read correctly"
127
    );
128
129
    $schema->storage->txn_rollback;
130
};
131
132
subtest '_get_MarcFieldsToOrder_syspref_data()' => sub {
133
    plan tests => 3;
134
135
    $schema->storage->txn_begin;
136
137
    t::lib::Mocks::mock_preference( 'MarcFieldsToOrder', 'price: 975$p
138
quantity: 975$q
139
budget_code: 975$h' );
140
141
    my $record = MARC::Record->new();
142
143
    $record->add_fields(
144
        [ '001', '1234' ],
145
        [ '975', ' ', ' ', p => 10, q => 1, h => 1 ],
146
    );
147
148
    my $marc_fields_to_order = Koha::MarcOrder::_get_MarcFieldsToOrder_syspref_data(
149
        'MarcFieldsToOrder', $record,
150
        [ 'price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2' ]
151
    );
152
153
    is(
154
        $marc_fields_to_order->{price}, 10,
155
        "Price has been read correctly"
156
    );
157
    is(
158
        $marc_fields_to_order->{quantity}, 1,
159
        "Quantity has been read correctly"
160
    );
161
    is(
162
        $marc_fields_to_order->{budget_code}, 1,
163
        "Budget code has been read correctly"
164
    );
165
166
    $schema->storage->txn_rollback;
167
};
168
169
subtest 'add_biblio_from_import_record()' => sub {
170
    plan tests => 4;
171
172
    $schema->storage->txn_begin;
173
174
    my $sample_import_batch = {
175
        matcher_id     => 1,
176
        template_id    => 1,
177
        branchcode     => 'CPL',
178
        overlay_action => 'create_new',
179
        nomatch_action => 'create_new',
180
        item_action    => 'always_add',
181
        import_status  => 'staged',
182
        batch_type     => 'z3950',
183
        file_name      => 'test.mrc',
184
        comments       => 'test',
185
        record_type    => 'auth',
186
    };
187
188
    my $import_batch_id = C4::ImportBatch::AddImportBatch($sample_import_batch);
189
190
    my $record = MARC::Record->new();
191
192
    $record->add_fields(
193
        [ '001', '1234' ],
194
        [ '020', ' ', ' ', a => '9780596004931'],
195
        [ '975', ' ', ' ', p => 10, q => 1, h => 1 ],
196
    );
197
198
    my $import_record_id = C4::ImportBatch::AddBiblioToBatch( $import_batch_id, 0, $record, 'utf8', 0 );
199
    my $import_record = Koha::Import::Records->find($import_record_id);
200
201
    my $result = Koha::MarcOrder::add_biblio_from_import_record(
202
        {
203
            import_record   => $import_record,
204
            matcher_id      => $sample_import_batch->{matcher_id},
205
            overlay_action  => $sample_import_batch->{overlay_action},
206
            agent           => 'cron',
207
            import_batch_id => $import_batch_id
208
        }
209
    );
210
211
    isnt(
212
        $result->{record_result}->{biblionumber}, undef,
213
        'A new biblionumber is added or an existing biblionumber is returned'
214
    );
215
216
    # Check that records are skipped if not selected when called from addorderiso2709.pl
217
    # Pass in an empty array and the record should be skipped
218
    my @import_record_id_selected = ();
219
    my $result2 = Koha::MarcOrder::add_biblio_from_import_record(
220
        {
221
            import_record             => $import_record,
222
            matcher_id                => $sample_import_batch->{matcher_id},
223
            overlay_action            => $sample_import_batch->{overlay_action},
224
            import_record_id_selected => \@import_record_id_selected,
225
            agent                     => 'client',
226
            import_batch_id           => $import_batch_id
227
        }
228
    );
229
230
    is( $result2->{skip}, 1, 'Record was skipped');
231
    is( $result2->{duplicates_in_batch}, 0, 'Record was skipped - no duplicate checking needed' );
232
    is( $result2->{record_result}, 0, 'Record was skipped');
233
234
    $schema->storage->txn_rollback;
235
};
236
237
subtest 'add_items_from_import_record() - cronjob' => sub {
238
    plan tests => 9;
239
240
    $schema->storage->txn_begin;
241
242
    my $bpid = C4::Budgets::AddBudgetPeriod(
243
        {
244
            budget_period_startdate => '2008-01-01', budget_period_enddate => '2008-12-31'
245
            , budget_period_active      => 1
246
            , budget_period_description => "MAPERI"
247
        }
248
    );
249
    my $budgetid = C4::Budgets::AddBudget(
250
        {
251
            budget_code      => "BC_1",
252
            budget_name      => "budget_name_test_1",
253
            budget_period_id => $bpid,
254
        }
255
    );
256
    my $budgetid2 = C4::Budgets::AddBudget(
257
        {
258
            budget_code      => "BC_2",
259
            budget_name      => "budget_name_test_2",
260
            budget_period_id => $bpid,
261
        }
262
    );
263
    my $fund1 = C4::Budgets::GetBudget($budgetid);
264
    my $fund2 = C4::Budgets::GetBudget($budgetid2);
265
    my $budget_code_1 = $fund1->{budget_code};
266
    my $budget_code_2 = $fund2->{budget_code};
267
268
    my $sample_import_batch = {
269
        matcher_id     => 1,
270
        template_id    => 1,
271
        branchcode     => 'CPL',
272
        overlay_action => 'create_new',
273
        nomatch_action => 'create_new',
274
        item_action    => 'always_add',
275
        import_status  => 'staged',
276
        batch_type     => 'z3950',
277
        file_name      => 'test.mrc',
278
        comments       => 'test',
279
        record_type    => 'auth',
280
    };
281
282
    my $import_batch_id = C4::ImportBatch::AddImportBatch($sample_import_batch);
283
284
    my $record = MARC::Record->new();
285
286
    $record->add_fields(
287
        [ '001', '1234' ],
288
        [ '020', ' ', ' ', a => '9780596004931' ],
289
        [ '975', ' ', ' ', p => 10, q => 1, h => $budget_code_2 ],
290
    );
291
292
    my $import_record_id = C4::ImportBatch::AddBiblioToBatch( $import_batch_id, 0, $record, 'utf8', 0 );
293
    my $import_record    = Koha::Import::Records->find($import_record_id);
294
295
    my $result = Koha::MarcOrder::add_biblio_from_import_record(
296
        {
297
            import_record   => $import_record,
298
            matcher_id      => $sample_import_batch->{matcher_id},
299
            overlay_action  => $sample_import_batch->{overlay_action},
300
            agent           => 'cron',
301
            import_batch_id => $import_batch_id
302
        }
303
    );
304
305
    my $bookseller = Koha::Acquisition::Bookseller->new(
306
        {
307
            name         => "my vendor",
308
            address1     => "bookseller's address",
309
            phone        => "0123456",
310
            active       => 1,
311
            deliverytime => 5,
312
        }
313
    )->store;
314
315
    t::lib::Mocks::mock_preference('MarcItemFieldsToOrder', '');
316
    t::lib::Mocks::mock_preference('MarcFieldsToOrder', '');
317
318
    my $order_line_details = Koha::MarcOrder::add_items_from_import_record(
319
        {
320
            record_result => $result->{record_result},
321
            basket_id     => 1,
322
            vendor        => $bookseller,
323
            budget_id     => $budgetid,
324
            agent         => 'cron',
325
        }
326
    );
327
328
    is(
329
        @{$order_line_details}[0]->{rrp}, undef,
330
        "Price has been read correctly"
331
    );
332
    is(
333
        @{$order_line_details}[0]->{listprice}, 0,
334
        "Listprice has defaulted to 0"
335
    );
336
    is(
337
        @{$order_line_details}[0]->{quantity}, 1,
338
        "Quantity has been defaulted to 1 correctly"
339
    );
340
    is(
341
        @{$order_line_details}[0]->{budget_id}, $budgetid,
342
        "Budget code has been read correctly"
343
    );
344
345
    #test that when a budget is mapped it is correctly used in the order line
346
    t::lib::Mocks::mock_preference(
347
        'MarcItemFieldsToOrder', 'homebranch: 975$a
348
holdingbranch: 975$b
349
itype: 975$y
350
nonpublic_note: 975$x
351
public_note: 975$z
352
loc: 975$c
353
ccode: 975$8
354
notforloan: 975$7
355
uri: 975$u
356
copyno: 975$n
357
quantity: 975$q
358
budget_code: 975$h
359
price: 975$p
360
replacementprice: 975$v'
361
    );
362
363
    my $order_line_details2 = Koha::MarcOrder::add_items_from_import_record(
364
        {
365
            record_result => $result->{record_result},
366
            basket_id     => 1,
367
            vendor        => $bookseller,
368
            budget_id     => $budgetid,
369
            agent         => 'cron',
370
        }
371
    );
372
373
    is(
374
        @{$order_line_details2}[0]->{budget_id}, $budgetid2,
375
        "Budget code has been read correctly from the syspref mapping"
376
    );
377
    is(
378
        @{$order_line_details2}[0]->{rrp}, 10,
379
        "Price has been read correctly"
380
    );
381
    is(
382
        @{$order_line_details2}[0]->{quantity}, 1,
383
        "Quantity has been read correctly"
384
    );
385
386
    my @created_items = @{$order_line_details2}[0]->{itemnumbers};
387
    my $new_item = Koha::Items->find($created_items[0]);
388
389
    isnt(
390
        $new_item, undef,
391
        'Item was successfully created'
392
    );
393
    is(
394
        $new_item->price, '10.00',
395
        'Item values mapped correctly'
396
    );
397
398
    $schema->storage->txn_rollback;
399
};
400
401
subtest 'add_items_from_import_record() - addorderiso2709.pl' => sub {
402
    plan tests => 6;
403
404
    $schema->storage->txn_begin;
405
406
    my $bpid = C4::Budgets::AddBudgetPeriod(
407
        {
408
            budget_period_startdate => '2008-01-01', budget_period_enddate => '2008-12-31'
409
            , budget_period_active      => 1
410
            , budget_period_description => "MAPERI"
411
        }
412
    );
413
    my $budgetid = C4::Budgets::AddBudget(
414
        {
415
            budget_code      => "BC_1",
416
            budget_name      => "budget_name_test_1",
417
            budget_period_id => $bpid,
418
        }
419
    );
420
    my $budgetid2 = C4::Budgets::AddBudget(
421
        {
422
            budget_code      => "BC_2",
423
            budget_name      => "budget_name_test_2",
424
            budget_period_id => $bpid,
425
        }
426
    );
427
    my $fund1         = C4::Budgets::GetBudget($budgetid);
428
    my $fund2         = C4::Budgets::GetBudget($budgetid2);
429
    my $budget_code_1 = $fund1->{budget_code};
430
    my $budget_code_2 = $fund2->{budget_code};
431
432
    my $sample_import_batch = {
433
        matcher_id     => 1,
434
        template_id    => 1,
435
        branchcode     => 'CPL',
436
        overlay_action => 'create_new',
437
        nomatch_action => 'create_new',
438
        item_action    => 'always_add',
439
        import_status  => 'staged',
440
        batch_type     => 'z3950',
441
        file_name      => 'test.mrc',
442
        comments       => 'test',
443
        record_type    => 'auth',
444
    };
445
446
    my $import_batch_id = C4::ImportBatch::AddImportBatch($sample_import_batch);
447
448
    my $record = MARC::Record->new();
449
450
    $record->add_fields(
451
        [ '001', '1234' ],
452
        [ '020', ' ', ' ', a => '9780596004931' ],
453
        [ '975', ' ', ' ', p => 10, q => 1, h => $budget_code_2 ],
454
    );
455
456
    my $import_record_id = C4::ImportBatch::AddBiblioToBatch( $import_batch_id, 0, $record, 'utf8', 0 );
457
    my $import_record    = Koha::Import::Records->find($import_record_id);
458
    my @import_record_id_selected = ( $import_record_id );
459
460
    my $result = Koha::MarcOrder::add_biblio_from_import_record(
461
        {
462
            import_record             => $import_record,
463
            matcher_id                => $sample_import_batch->{matcher_id},
464
            overlay_action            => $sample_import_batch->{overlay_action},
465
            agent                     => 'client',
466
            import_batch_id           => $import_batch_id,
467
            import_record_id_selected => \@import_record_id_selected
468
        }
469
    );
470
471
    my $bookseller = Koha::Acquisition::Bookseller->new(
472
        {
473
            name         => "my vendor",
474
            address1     => "bookseller's address",
475
            phone        => "0123456",
476
            active       => 1,
477
            deliverytime => 5,
478
        }
479
    )->store;
480
481
    my $client_item_fields = {
482
        'notforloans' => [
483
            '',
484
            ''
485
        ],
486
        'c_budget_id'       => 2,
487
        'replacementprices' => [
488
            '0.00',
489
            '0.00'
490
        ],
491
        'uris' => [
492
            '',
493
            ''
494
        ],
495
        'c_replacement_price' => '0.00',
496
        'public_notes'        => [
497
            '',
498
            ''
499
        ],
500
        'itemcallnumbers' => [
501
            '',
502
            ''
503
        ],
504
        'budget_codes' => [
505
            '',
506
            ''
507
        ],
508
        'nonpublic_notes' => [
509
            '',
510
            ''
511
        ],
512
        'homebranches' => [
513
            'CPL',
514
            'CPL'
515
        ],
516
        'copynos' => [
517
            '',
518
            ''
519
        ],
520
        'holdingbranches' => [
521
            'CPL',
522
            'CPL'
523
        ],
524
        'ccodes' => [
525
            '',
526
            ''
527
        ],
528
        'locs' => [
529
            '',
530
            ''
531
        ],
532
        'itemprices' => [
533
            '10.00',
534
            '10.00'
535
        ],
536
        'c_discount' => '',
537
        'c_price'    => '0.00',
538
        'c_sort2'    => '',
539
        'c_sort1'    => '',
540
        'c_quantity' => '1',
541
        'itypes'     => [
542
            'BK',
543
            'BK'
544
        ]
545
    };
546
547
    my $order_line_details = Koha::MarcOrder::add_items_from_import_record(
548
        {
549
            record_result      => $result->{record_result},
550
            basket_id          => 1,
551
            vendor             => $bookseller,
552
            budget_id          => $budgetid,
553
            agent              => 'client',
554
            client_item_fields => $client_item_fields,
555
        }
556
    );
557
558
    is(
559
        @{$order_line_details}[0]->{rrp}, '10.00',
560
        "Price has been read correctly"
561
    );
562
    is(
563
        @{$order_line_details}[0]->{listprice}, '10',
564
        "Listprice has been created successfully"
565
    );
566
    is(
567
        @{$order_line_details}[0]->{quantity}, 1,
568
        "Quantity has been read correctly"
569
    );
570
    is(
571
        @{$order_line_details}[0]->{budget_id}, $budgetid,
572
        "Budget code has been read correctly"
573
    );
574
575
    my @created_items = @{$order_line_details}[0]->{itemnumbers};
576
    my $new_item      = Koha::Items->find( $created_items[0] );
577
578
    isnt(
579
        $new_item, undef,
580
        'Item was successfully created'
581
    );
582
    is(
583
        $new_item->price, '10.00',
584
        'Item values mapped correctly'
585
    );
586
587
    $schema->storage->txn_rollback;
588
};
589
590
591
subtest 'create_order_lines()' => sub {
592
    plan tests => 1;
593
594
    $schema->storage->txn_begin;
595
596
    my $quantity = 3;
597
598
    my $bpid = C4::Budgets::AddBudgetPeriod(
599
        {
600
            budget_period_startdate => '2008-01-01', budget_period_enddate => '2008-12-31'
601
            , budget_period_active      => 1
602
            , budget_period_description => "MAPERI"
603
        }
604
    );
605
    my $budgetid = C4::Budgets::AddBudget(
606
        {
607
            budget_code      => "budget_code_test_1",
608
            budget_name      => "budget_name_test_1",
609
            budget_period_id => $bpid,
610
        }
611
    );
612
613
    my $sample_import_batch = {
614
        matcher_id     => 1,
615
        template_id    => 1,
616
        branchcode     => 'CPL',
617
        overlay_action => 'create_new',
618
        nomatch_action => 'create_new',
619
        item_action    => 'always_add',
620
        import_status  => 'staged',
621
        batch_type     => 'z3950',
622
        file_name      => 'test.mrc',
623
        comments       => 'test',
624
        record_type    => 'auth',
625
    };
626
627
    my $import_batch_id = C4::ImportBatch::AddImportBatch($sample_import_batch);
628
629
    my $record = MARC::Record->new();
630
631
    $record->add_fields(
632
        [ '001', '1234' ],
633
        [ '020', ' ', ' ', a => '9780596004931' ],
634
        [
635
            '975', ' ', ' ', p => 10, q => $quantity, h => $budgetid, a => 'CPL', b => 'CPL', y => 'Book',
636
            x => 'Private note', z => 'Public note', c => 'Shelf', 8 => 'ccode', 7 => 0,
637
            u => '12345abcde',   n => '12345',       v => 10
638
        ]
639
    );
640
641
    my $import_record_id = C4::ImportBatch::AddBiblioToBatch( $import_batch_id, 0, $record, 'utf8', 0 );
642
    my $import_record    = Koha::Import::Records->find($import_record_id);
643
644
    my $result = Koha::MarcOrder::add_biblio_from_import_record(
645
        {
646
            import_record   => $import_record,
647
            matcher_id      => $sample_import_batch->{matcher_id},
648
            overlay_action  => $sample_import_batch->{overlay_action},
649
            agent           => 'cron',
650
            import_batch_id => $import_batch_id
651
        }
652
    );
653
654
    my $bookseller = Koha::Acquisition::Bookseller->new(
655
        {
656
            name         => "my vendor",
657
            address1     => "bookseller's address",
658
            phone        => "0123456",
659
            active       => 1,
660
            deliverytime => 5,
661
        }
662
    )->store;
663
664
665
        t::lib::Mocks::mock_preference(
666
        'MarcItemFieldsToOrder', 'homebranch: 975$a
667
holdingbranch: 975$b
668
itype: 975$y
669
nonpublic_note: 975$x
670
public_note: 975$z
671
loc: 975$c
672
ccode: 975$8
673
notforloan: 975$7
674
uri: 975$u
675
copyno: 975$n
676
quantity: 975$q
677
budget_code: 975$h
678
price: 975$p
679
replacementprice: 975$v'
680
    );
681
682
    my $order_line_details = Koha::MarcOrder::add_items_from_import_record(
683
        {
684
            record_result => $result->{record_result},
685
            basket_id     => 1,
686
            vendor        => $bookseller,
687
            budget_id     => $budgetid,
688
            agent         => 'cron',
689
        }
690
    );
691
692
    my $nb_of_orders = Koha::Acquisition::Orders->count;
693
694
    my $order_lines = Koha::MarcOrder::create_order_lines( { order_line_details => $order_line_details } );
695
696
    my $new_nb_of_orders = Koha::Acquisition::Orders->count;
697
698
    is($new_nb_of_orders, ( $nb_of_orders + $quantity ), 'Orders created successfully');
699
700
    $schema->storage->txn_rollback;
701
};

Return to bug 35026