Bug 5335 - More granular VAT
[koha.git] / t / db_dependent / lib / KohaTest / Acquisition / GetParcels.pm
1 package KohaTest::Acquisition::GetParcels;
2 use base qw( KohaTest::Acquisition );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Time::localtime;
9
10 use C4::Acquisition;
11
12 =head2 NOTE
13
14 Please do not confuse this with the test suite for C4::Acquisition::GetParcel.
15
16 =head3 no_parcels
17
18 at first, there should be no parcels for our bookseller.
19
20 =cut
21
22 sub no_parcels : Test( 1 ) {
23     my $self = shift;
24
25     my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
26                              # order
27                              # code ( aqorders.booksellerinvoicenumber )
28                              # datefrom
29                              # date to
30                         );
31                             
32     is( scalar @parcels, 0, 'our new bookseller has no parcels' )
33       or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
34 }
35
36 =head3 one_parcel
37
38 we create an order, mark it as received, and then see if we can find
39 it with GetParcels.
40
41 =cut
42
43 sub one_parcel : Test( 19 ) {
44     my $self = shift;
45
46     my $invoice = 123;    # XXX what should this be?
47     my $today = sprintf( '%04d-%02d-%02d',
48                          localtime->year() + 1900,
49                          localtime->mon() + 1,
50                          localtime->mday() );
51
52     $self->create_order( authorizedby => 1,   # XXX what should this be?
53                          invoice      => $invoice,
54                          date         => $today );
55     
56     my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
57                              # order
58                              # code ( aqorders.booksellerinvoicenumber )
59                              # datefrom
60                              # date to
61                         );
62     is( scalar @parcels, 1, 'we found one (1) parcel.' )
63       or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
64
65     my $thisparcel = shift( @parcels );
66     is( scalar ( keys( %$thisparcel ) ), 6, 'my parcel hashref has 6 keys' )
67       or diag( Data::Dumper->Dump( [ $thisparcel ], [ 'thisparcel' ] ) );
68       
69     is( $thisparcel->{'datereceived'},             $today,   'datereceived' );
70     is( $thisparcel->{'biblio'},                   1,        'biblio' );
71     is( $thisparcel->{'booksellerinvoicenumber'}, $invoice, 'booksellerinvoicenumber' );
72
73     # diag( Data::Dumper->Dump( [ $thisparcel ], [ 'thisparcel' ] ) );
74
75 }
76
77 =head3 two_parcels
78
79 we create another order, mark it as received, and then see if we can find
80 them all with GetParcels.
81
82 =cut
83
84 sub two_parcels : Test( 31 ) {
85     my $self = shift;
86
87     my $invoice = 1234;    # XXX what should this be?
88     my $today = sprintf( '%04d-%02d-%02d',
89                          localtime->year() + 1900,
90                          localtime->mon() + 1,
91                          localtime->mday() );
92     $self->create_order( authorizedby => 1,   # XXX what should this be?
93                          invoice      => $invoice,
94                          date         => $today );
95
96     {
97         # fetch them all and check that this one is last
98         my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
99                                   # order
100                                   # code ( aqorders.booksellerinvoicenumber )
101                                   # datefrom
102                                   # date to
103                              );
104         is( scalar @parcels, 2, 'we found two (2) parcels.' )
105           or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
106         
107         my $thisparcel = pop( @parcels );
108         is( scalar ( keys( %$thisparcel ) ), 6, 'my parcel hashref has 6 keys' )
109           or diag( Data::Dumper->Dump( [ $thisparcel ], [ 'thisparcel' ] ) );
110         
111         is( $thisparcel->{'datereceived'},             $today,   'datereceived' );
112         is( $thisparcel->{'biblio'},                   1,        'biblio' );
113         is( $thisparcel->{'booksellerinvoicenumber'}, $invoice, 'booksellerinvoicenumber' );
114     }
115
116     {
117         # fetch just one, by using the exact code
118         my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
119                                   undef,    # order
120                                   $invoice, # code ( aqorders.booksellerinvoicenumber )
121                                   undef,    # datefrom
122                                   undef,    # date to
123                              );
124         is( scalar @parcels, 1, 'we found one (1) parcels.' )
125           or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
126         
127         my $thisparcel = pop( @parcels );
128         is( scalar ( keys( %$thisparcel ) ), 6, 'my parcel hashref has 6 keys' )
129           or diag( Data::Dumper->Dump( [ $thisparcel ], [ 'thisparcel' ] ) );
130         
131         is( $thisparcel->{'datereceived'},             $today,   'datereceived' );
132         is( $thisparcel->{'biblio'},                   1,        'biblio' );
133         is( $thisparcel->{'booksellerinvoicenumber'}, $invoice, 'booksellerinvoicenumber' );
134     }
135     
136     {
137         # fetch them both by using code 123, which gets 123 and 1234
138         my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
139                                   undef,    # order
140                                   '123', # code ( aqorders.booksellerinvoicenumber )
141                                   undef,    # datefrom
142                                   undef,    # date to
143                              );
144         is( scalar @parcels, 2, 'we found 2 parcels.' )
145           or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
146         
147     }
148     
149     {
150         # fetch them both, and try to order them
151         my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
152                                   'aqorders.booksellerinvoicenumber',    # order
153                                   undef, # code ( aqorders.booksellerinvoicenumber )
154                                   undef,    # datefrom
155                                   undef,    # date to
156                              );
157         is( scalar @parcels, 2, 'we found 2 parcels.' )
158           or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
159         is( $parcels[0]->{'booksellerinvoicenumber'}, 123 );
160         is( $parcels[1]->{'booksellerinvoicenumber'}, 1234 );
161         
162     }
163     
164     {
165         # fetch them both, and try to order them, descending
166         my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
167                                   'aqorders.booksellerinvoicenumber desc',    # order
168                                   undef, # code ( aqorders.booksellerinvoicenumber )
169                                   undef,    # datefrom
170                                   undef,    # date to
171                              );
172         is( scalar @parcels, 2, 'we found 2 parcels.' )
173           or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
174         is( $parcels[0]->{'booksellerinvoicenumber'}, 1234 );
175         is( $parcels[1]->{'booksellerinvoicenumber'}, 123 );
176         
177     }
178     
179     
180     
181
182     # diag( Data::Dumper->Dump( [ $thisparcel ], [ 'thisparcel' ] ) );
183
184 }
185
186
187 =head3 z_several_parcels_with_different_dates
188
189 we create an order, mark it as received, and then see if we can find
190 it with GetParcels.
191
192 =cut
193
194 sub z_several_parcels_with_different_dates : Test( 44 ) {
195     my $self = shift;
196
197     my $authorizedby = 1; # XXX what should this be?
198
199     my @inputs = ( { invoice => 10,
200                       date     => sprintf( '%04d-%02d-%02d',
201                                            1950,
202                                            localtime->mon() + 1,
203                                            10 ), # I'm using the invoice number as the day.
204                  },
205                     { invoice => 15,
206                       date     => sprintf( '%04d-%02d-%02d',
207                                            1950,
208                                            localtime->mon() + 1,
209                                            15 ), # I'm using the invoice number as the day.
210                  },
211                     { invoice => 20,
212                       date     => sprintf( '%04d-%02d-%02d',
213                                            1950,
214                                            localtime->mon() + 1,
215                                            20 ), # I'm using the invoice number as the day.
216                  },
217                );
218
219     foreach my $input ( @inputs ) {
220         $self->create_order( authorizedby => $authorizedby,
221                              invoice      => $input->{'invoice'},
222                              date         => $input->{'date'},
223                         );
224     }
225                          
226     my @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
227                               undef, # order
228                               undef, # code ( aqorders.booksellerinvoicenumber )
229                               sprintf( '%04d-%02d-%02d',
230                                        1950,
231                                        localtime->mon() + 1,
232                                        10 ), # datefrom
233                               sprintf( '%04d-%02d-%02d',
234                                        1950,
235                                        localtime->mon() + 1,
236                                        20 ), # dateto
237                         );
238     is( scalar @parcels, scalar @inputs, 'we found all of the parcels.' )
239       or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
240
241     @parcels = GetParcels( $self->{'booksellerid'},  # bookseller
242                            undef, # order
243                            undef, # code ( aqorders.booksellerinvoicenumber )
244                            sprintf( '%04d-%02d-%02d',
245                                     1950,
246                                     localtime->mon() + 1,
247                                     10 ), # datefrom
248                            sprintf( '%04d-%02d-%02d',
249                                     1950,
250                                     localtime->mon() + 1,
251                                     16 ), # dateto
252                         );
253     is( scalar @parcels, scalar @inputs - 1, 'we found all of the parcels except one' )
254       or diag( Data::Dumper->Dump( [ \@parcels ], [ 'parcels' ] ) );
255
256
257
258     # diag( Data::Dumper->Dump( [ $thisparcel ], [ 'thisparcel' ] ) );
259
260 }
261
262 sub create_order {
263     my $self = shift;
264     my %param = @_;
265     $param{'authorizedby'} = 1 unless exists $param{'authorizedby'};
266     $param{'invoice'}      = 1 unless exists $param{'invoice'};
267     $param{'date'} = sprintf( '%04d-%02d-%02d',
268                               localtime->year() + 1900,
269                               localtime->mon() + 1,
270                               localtime->mday() ) unless exists $param{'date'};
271
272     my ( $basketno, $ordernumber ) = $self->create_new_basket( %param );
273
274     my $datereceived = ModReceiveOrder( $self->{'biblios'}[0],             # biblionumber
275                                         $ordernumber,       # $ordernumber,
276                                         undef,         # $quantrec,
277                                         undef,         # $user,
278                                         undef,         # $cost,
279                                         undef,         # $ecost,
280                                         $param{'invoice'},         # $invoiceno,
281                                         undef,         # $freight,
282                                         undef,         # $rrp,
283                                         $self->{'bookfundid'},         # $bookfund,
284                                         $param{'date'},         # $datereceived
285                                    );
286     is( $datereceived, $param{'date'}, "the parcel was received on $datereceived" );
287
288 }
289
290 1;