Bug 6721 - improve searching in the acquisitions system
[koha.git] / t / db_dependent / lib / KohaTest / Acquisition / GetHistory.pm
1 package KohaTest::Acquisition::GetHistory;
2 use base qw( KohaTest::Acquisition );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Acquisition;
10 use C4::Context;
11 use C4::Members;
12 use C4::Biblio;
13 use C4::Bookseller;
14
15 =head3 no_history
16
17
18
19 =cut
20
21 sub no_history : Test( 4 ) {
22     my $self = shift;
23
24     # my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on )
25
26     my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory();
27     # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
28
29     is( scalar @$order_loop, 0, 'order_loop is empty' );
30     is( $total_qty,          0, 'total_qty' );
31     is( $total_price,        0, 'total_price' );
32     is( $total_qtyreceived,  0, 'total_qtyreceived' );
33
34     
35 }
36
37 =head3 one_order
38
39 =cut
40
41 my $INVOICE = "1234-56 AB";
42 sub one_order : Test( 55 ) {
43     my $self = shift;
44     
45     my ( $basketno, $ordernumber ) = $self->create_new_basket(invoice => $INVOICE);
46     ok( $basketno, "basketno is $basketno" );
47     ok( $ordernumber, "ordernumber is $ordernumber" );
48
49     # No arguments fetches no history.
50     {
51         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = eval { GetHistory() };
52         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
53         
54         is( $order_loop, undef, 'order_loop is empty' );
55     }
56
57     my $bibliodata = GetBiblioData( $self->{'biblios'}[0] );
58     ok( $bibliodata->{'title'}, 'the biblio has a title' )
59       or diag( Data::Dumper->Dump( [ $bibliodata ], [ 'bibliodata' ] ) );
60     
61     # searching by title should find it.
62     {
63         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( title => $bibliodata->{'title'} );
64         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
65     
66         is( scalar @$order_loop, 1, 'order_loop searched by title' );
67         is( $total_qty,          1, 'total_qty searched by title' );
68         is( $total_price,        1, 'total_price searched by title' );
69         is( $total_qtyreceived,  0, 'total_qtyreceived searched by title' );
70
71         # diag( Data::Dumper->Dump( [ $order_loop ], [ 'order_loop' ] ) );
72     }
73
74     # searching by basket number
75     {
76         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( basket => $basketno );
77         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
78     
79         is( scalar @$order_loop, 1, 'order_loop searched by basket no' );
80         is( $total_qty,          1, 'total_qty searched by basket no' );
81         is( $total_price,        1, 'total_price searched by basket no' );
82         is( $total_qtyreceived,  0, 'total_qtyreceived searched by basket no' );
83
84         # diag( Data::Dumper->Dump( [ $order_loop ], [ 'order_loop' ] ) );
85     }
86
87     # searching by invoice number
88     {
89         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( booksellerinvoicenumber  => $INVOICE );
90         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
91     
92         is( scalar @$order_loop, 1, 'order_loop searched by invoice no' );
93         is( $total_qty,          1, 'total_qty searched by invoice no' );
94         is( $total_price,        1, 'total_price searched by invoice no' );
95         is( $total_qtyreceived,  0, 'total_qtyreceived searched by invoice no' );
96
97         # diag( Data::Dumper->Dump( [ $order_loop ], [ 'order_loop' ] ) );
98     }
99
100     # searching by author
101     {
102         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( author => $bibliodata->{'author'} );
103         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
104     
105         is( scalar @$order_loop, 1, 'order_loop searched by author' );
106         is( $total_qty,          1, 'total_qty searched by author' );
107         is( $total_price,        1, 'total_price searched by author' );
108         is( $total_qtyreceived,  0, 'total_qtyreceived searched by author' );
109     }
110
111     # searching by name
112     {
113         # diag( Data::Dumper->Dump( [ $bibliodata ], [ 'bibliodata' ] ) );
114
115         my $bookseller = GetBookSellerFromId( $self->{'booksellerid'} );
116         ok( $bookseller->{'name'}, 'bookseller name' )
117           or diag( Data::Dumper->Dump( [ $bookseller ], [ 'bookseller' ] ) );
118         
119         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( name => $bookseller->{'name'} );
120         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
121     
122         is( scalar @$order_loop, 1, 'order_loop searched by name' );
123         is( $total_qty,          1, 'total_qty searched by name' );
124         is( $total_price,        1, 'total_price searched by name' );
125         is( $total_qtyreceived,  0, 'total_qtyreceived searched by name' );
126     }
127
128     # searching by from_date
129     {
130         my $tomorrow = $self->tomorrow();
131         # diag( "tomorrow is $tomorrow" );
132
133         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( to_placed_on =>  $tomorrow );
134         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
135     
136         is( scalar @$order_loop, 1, 'order_loop searched by to_date' );
137         is( $total_qty,          1, 'total_qty searched by to_date' );
138         is( $total_price,        1, 'total_price searched by to_date' );
139         is( $total_qtyreceived,  0, 'total_qtyreceived searched by to_date' );
140     }
141
142     # searching by from_date
143     {
144         my $yesterday = $self->yesterday();
145         # diag( "yesterday was $yesterday" );
146     
147         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( from_placed_on =>  $yesterday );
148         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
149     
150         is( scalar @$order_loop, 1, 'order_loop searched by from_date' );
151         is( $total_qty,          1, 'total_qty searched by from_date' );
152         is( $total_price,        1, 'total_price searched by from_date' );
153         is( $total_qtyreceived,  0, 'total_qtyreceived searched by from_date' );
154     }
155
156     # set up some things necessary to make GetHistory use the IndependantBranches
157     $self->enable_independant_branches();    
158
159     # just search by title here, we need to search by something.
160     {
161         my ( $order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( title => $bibliodata->{'title'} );
162         # diag( Data::Dumper->Dump( [ $order_loop, $total_qty, $total_price, $total_qtyreceived ], [ qw( order_loop total_qty total_price total_qtyreceived ) ] ) );
163     
164         is( scalar @$order_loop, 1, 'order_loop searched by title' );
165         is( $total_qty,          1, 'total_qty searched by title' );
166         is( $total_price,        1, 'total_price searched by title' );
167         is( $total_qtyreceived,  0, 'total_qtyreceived searched by title' );
168
169         # diag( Data::Dumper->Dump( [ $order_loop ], [ 'order_loop' ] ) );
170     }
171     
172     # reset that.
173     $self->disable_independant_branches();    
174
175     
176
177     
178 }
179
180
181 1;