Bug 20942: Split debit and credit lines
[koha.git] / t / db_dependent / Serials.t
index e09a076..6e65ee3 100755 (executable)
@@ -11,11 +11,13 @@ use C4::Serials;
 use C4::Serials::Frequency;
 use C4::Serials::Numberpattern;
 use C4::Debug;
-use C4::Bookseller;
 use C4::Biblio;
 use C4::Budgets;
+use C4::Items;
 use Koha::DateUtils;
+use Koha::Acquisition::Booksellers;
 use t::lib::Mocks;
+use t::lib::TestBuilder;
 use Test::More tests => 48;
 
 BEGIN {
@@ -28,11 +30,15 @@ my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
+my $builder = t::lib::TestBuilder->new();
+
 # This could/should be used for all untested methods
 my @methods = ('updateClaim');
 can_ok('C4::Serials', @methods);
 
-my $booksellerid = C4::Bookseller::AddBookseller(
+$dbh->do(q|UPDATE marc_subfield_structure SET value_builder="callnumber.pl" where kohafield="items.itemcallnumber" and frameworkcode=''|);
+
+my $bookseller = Koha::Acquisition::Bookseller->new(
     {
         name => "my vendor",
         address1 => "bookseller's address",
@@ -45,8 +51,8 @@ my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
 
 my $budgetid;
 my $bpid = AddBudgetPeriod({
-    budget_period_startdate   => '01-01-2015',
-    budget_period_enddate     => '31-12-2015',
+    budget_period_startdate   => '2015-01-01',
+    budget_period_enddate     => '2015-12-31',
     budget_period_description => "budget desc"
 });
 
@@ -61,6 +67,7 @@ my $budget_id = AddBudget({
 my $frequency_id = AddSubscriptionFrequency({ description => "Test frequency 1" });
 my $pattern_id = AddSubscriptionNumberpattern({
     label => 'Test numberpattern 1',
+    description => 'Description for numberpattern 1',
     numberingmethod => '{X}',
     label1 => q{},
     add1 => 1,
@@ -111,6 +118,7 @@ if (not $frequency->{unit}) {
     $frequency->{issuesperunit} = 1;
     $frequency->{description} = "Frequency created by t/db_dependant/Serials.t";
     $subscriptioninformation->{periodicity} = AddSubscriptionFrequency($frequency);
+    $subscriptioninformation->{serialsadditems} = 1;
 
     ModSubscription( @$subscriptioninformation{qw(
         librarian branchcode aqbooksellerid cost aqbudgetid startdate
@@ -135,6 +143,37 @@ ok(C4::Serials::GetSerialStatusFromSerialId($serial->{serialid}), 'test getting
 
 isa_ok(C4::Serials::GetSerialInformation($serial->{serialid}), 'HASH', 'test getting Serial Information');
 
+subtest 'Values should not be erased on editing' => sub {
+
+    plan tests => 1;
+
+    ( $biblionumber, $biblioitemnumber ) = get_biblio();
+    my ( $icn_tag, $icn_sf ) = GetMarcFromKohaField( 'items.itemcallnumber', '' );
+    my ( $it_tag, $it_sf )   = GetMarcFromKohaField( 'items.itype', '' );
+
+    my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
+    my $itemcallnumber = 'XXXmy itemcallnumberXXX';
+
+    my $item_record    = new MARC::Record;
+
+    $item_record->append_fields(
+        MARC::Field->new( '080', '', '', "a" => "default" ),
+        MARC::Field->new(
+            $icn_tag, '', '',
+            $icn_sf => $itemcallnumber,
+            $it_sf  => $itemtype
+        )
+    );
+    my ( undef, undef, $itemnumber ) = C4::Items::AddItemFromMarc( $item_record, $biblionumber );
+    my $serialid = C4::Serials::NewIssue( "serialseq", $subscriptionid, $biblionumber,
+                                          1, undef, undef, "publisheddatetext", "notes" );
+    C4::Serials::AddItem2Serial( $serialid, $itemnumber );
+    my $serial_info = C4::Serials::GetSerialInformation($serialid);
+    my ($itemcallnumber_info) = grep { $_->{kohafield} eq 'items.itemcallnumber' }
+                                     @{ $serial_info->{items}[0]->{iteminformation} };
+    like( $itemcallnumber_info->{marc_value}, qr|value="$itemcallnumber"| );
+};
+
 # Delete created frequency
 if ($old_frequency) {
     my $freq_to_delete = $subscriptioninformation->{periodicity};
@@ -153,7 +192,6 @@ if ($old_frequency) {
     DelSubscriptionFrequency($freq_to_delete);
 }
 
-
 # Test calling subs without parameters
 is(C4::Serials::AddItem2Serial(), undef, 'test adding item to serial');
 is(C4::Serials::GetFullSubscription(), undef, 'test getting full subscription');
@@ -228,8 +266,6 @@ subtest 'test_updateClaim' => sub {
     is($late_or_missing_issues_1_2[0]->{status}, 3, 'Got the expected unchanged claim status from update claim');
 };
 
-is(C4::Serials::getsupplierbyserialid(),undef, 'test getting supplier idea');
-
 is(C4::Serials::check_routing(), undef, 'test checking route');
 
 is(C4::Serials::addroutingmember(),undef, 'test adding route member');
@@ -317,3 +353,13 @@ subtest "Do not generate an expected if one already exists" => sub {
 };
 
 $dbh->rollback;
+
+sub get_biblio {
+    my $bib = MARC::Record->new();
+    $bib->append_fields(
+        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
+        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
+    );
+    my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
+    return ($bibnum, $bibitemnum);
+}