7 use Test::More tests => 5;
9 use C4::Acquisition qw( NewBasket GetBasket GetBasketAsCSV );
10 use C4::Biblio qw( AddBiblio );
12 use Koha::CsvProfiles;
13 use Koha::Acquisition::Orders;
19 my $schema = Koha::Database->new()->schema();
20 $schema->storage->txn_begin();
22 my $query = CGI->new();
24 my $vendor = Koha::Acquisition::Bookseller->new({
26 address1 => 'vendor address',
31 my $budget_id = C4::Budgets::AddBudget({
32 budget_code => 'my_budget_code',
33 budget_name => 'My budget name',
35 my $budget = C4::Budgets::GetBudget( $budget_id );
37 my $csv_profile = Koha::CsvProfile->new({
38 profile => 'my user profile',
39 type => 'export_basket',
41 content => 'autor=biblio.author|title=biblio.title|quantity=aqorders.quantity',
42 description => 'csv profile',
45 my $csv_profile2 = Koha::CsvProfile->new({
46 profile => 'my user profile',
47 type => 'export_basket',
49 content => 'biblio.author | title = biblio.title|quantity=aqorders.quantity',
50 description => 'csv profile 2',
54 $basketno = NewBasket($vendor->id, 1);
56 my $biblio = MARC::Record->new();
57 $biblio->append_fields(
58 MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
59 MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
61 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
63 my $order = Koha::Acquisition::Order->new({
64 basketno => $basketno,
66 biblionumber => $biblionumber,
67 budget_id => $budget_id,
68 entrydate => '2016-01-02',
71 # Use user CSV profile
72 my $basket_csv1 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile->export_format_id);
73 is($basket_csv1, 'autor,title,quantity
74 "King, Stephen","Test Record",3
75 ', 'CSV should be generated with user profile');
77 # Use default template
78 t::lib::Mocks::mock_preference('CSVDelimiter', ',');
79 my $basket_csv2 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
80 is($basket_csv2, 'Contract name,Order number,Entry date,ISBN,Author,Title,Publication year,Publisher,Collection title,Note for vendor,Quantity,RRP,Delivery place,Billing place
81 "",' . $order->ordernumber . ',2016-01-02,,"King, Stephen","Test Record",,"","","",3,,"",""
82 ', 'CSV should be generated with default template');
84 my $basket_csv3 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile2->export_format_id);
85 is($basket_csv3, 'biblio.author,title,quantity
86 "King, Stephen","Test Record",3
87 ', 'CSV should be generated with user profile which does not have all headers defined');
90 my $basket_csv4 = C4::Acquisition::GetBasketAsCSV($basketno, $query, 'non_existant_profile_id');
91 fail("It is not possible to export basket using non-existant profile");
93 ok($_->isa("Koha::Exceptions::ObjectNotFound"), "Using non-existant profile should throw ObjectNotFound exception");
96 Koha::Biblios->find($biblionumber)->delete;
97 my $basket_csv4 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
98 is($basket_csv4, 'Contract name,Order number,Entry date,ISBN,Author,Title,Publication year,Publisher,Collection title,Note for vendor,Quantity,RRP,Delivery place,Billing place
99 "",' . $order->ordernumber . ',2016-01-02,,"","",,"","","",3,,"",""
100 ', 'CSV should not fail if biblio does not exist');
103 $schema->storage->txn_rollback();