7 use Test::More tests => 4;
9 use C4::Acquisition qw( NewBasket GetBasket GetBasketAsCSV );
10 use C4::Biblio qw( AddBiblio );
12 use Koha::CsvProfiles;
13 use Koha::Acquisition::Orders;
18 my $schema = Koha::Database->new()->schema();
19 $schema->storage->txn_begin();
21 my $query = CGI->new();
23 my $vendor = Koha::Acquisition::Bookseller->new({
25 address1 => 'vendor address',
30 my $budget_id = C4::Budgets::AddBudget({
31 budget_code => 'my_budget_code',
32 budget_name => 'My budget name',
34 my $budget = C4::Budgets::GetBudget( $budget_id );
36 my $csv_profile = Koha::CsvProfile->new({
37 profile => 'my user profile',
38 type => 'export_basket',
40 content => 'autor=biblio.author|title=biblio.title|quantity=aqorders.quantity',
41 description => 'csv profile',
44 my $csv_profile2 = Koha::CsvProfile->new({
45 profile => 'my user profile',
46 type => 'export_basket',
48 content => 'biblio.author | title = biblio.title|quantity=aqorders.quantity',
49 description => 'csv profile 2',
53 $basketno = NewBasket($vendor->id, 1);
55 my $biblio = MARC::Record->new();
56 $biblio->append_fields(
57 MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
58 MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
60 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
62 my $order = Koha::Acquisition::Order->new({
63 basketno => $basketno,
65 biblionumber => $biblionumber,
66 budget_id => $budget_id,
67 entrydate => '2016-01-02',
70 # Use user CSV profile
71 my $basket_csv1 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile->export_format_id);
72 is($basket_csv1, 'autor,title,quantity
73 "King, Stephen","Test Record",3
74 ', 'CSV should be generated with user profile');
76 # Use default template
77 t::lib::Mocks::mock_preference('CSVDelimiter', ',');
78 my $basket_csv2 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
79 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
80 "",' . $order->ordernumber . ',2016-01-02,,"King, Stephen","Test Record",,"","","",3,,"",""
81 ', 'CSV should be generated with default template');
83 my $basket_csv3 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile2->export_format_id);
84 is($basket_csv3, 'biblio.author,title,quantity
85 "King, Stephen","Test Record",3
86 ', 'CSV should be generated with user profile which does not have all headers defined');
89 my $basket_csv4 = C4::Acquisition::GetBasketAsCSV($basketno, $query, 'non_existant_profile_id');
90 fail("It is not possible to export basket using non-existant profile");
92 ok($_->isa("Koha::Exceptions::ObjectNotFound"), "Using non-existant profile should throw ObjectNotFound exception");
95 $schema->storage->txn_rollback();