From 73e5c89bebcb3fe72687a912b621f6d5c975ab21 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Wed, 20 Aug 2014 17:05:19 +0200
Subject: [PATCH] [SIGNED-OFF] Bug 12743: ACQ: default values for catalogue
 records

At the moment, it is possible to create records in acquisitions, but the
ACQ framework is only used for items created in this module.

This patch allows to defined default values in the ACQ framework for
records created on the acquisition module.

Test plan:
1/ Make sure you have the ACQ framework created (otherwise create it
from the default framework).
2/ Define a default value for a field (for instance 099$z=1).
3/ Go in the acquisition module and create a new order from a new
record.
4/ Fill mandatory information and save.
5/ Go on the detail page of this record and verify the default value
exist.

Signed-off-by: Gaetan Boisson <gaetan.boisson@biblibre.com>
---
 acqui/addorder.pl | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/acqui/addorder.pl b/acqui/addorder.pl
index e048d05..99cf762 100755
--- a/acqui/addorder.pl
+++ b/acqui/addorder.pl
@@ -129,6 +129,8 @@ use C4::Biblio;			# AddBiblio TransformKohaToMarc
 use C4::Budgets;
 use C4::Items;
 use C4::Output;
+use C4::Koha qw( subfield_is_koha_internal_p );
+use MARC::Field;
 
 ### "-------------------- addorder.pl ----------"
 
@@ -259,6 +261,25 @@ if ( $orderinfo->{quantity} ne '0' ) {
                 "aqorders.discount"           => $$orderinfo{discount} ? $$orderinfo{discount} : "",
             });
 
+
+        my $tagslib = C4::Biblio::GetMarcStructure( 1, 'ACQ' );
+        if ( $tagslib ) {
+            my ( $itemfield ) = C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
+            for my $tag ( sort keys %$tagslib ) {
+                next unless $tag;
+                next if $tag == $itemfield;
+                for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
+                    next if ( subfield_is_koha_internal_p($subfield) );
+                    my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
+                    if ( defined $defaultvalue and $defaultvalue ne '' ) {
+                        $record->insert_fields_ordered(
+                            MARC::Field->new( $tag, '', '', $subfield => $defaultvalue )
+                        );
+                    }
+                }
+            }
+        }
+
         # create the record in catalogue, with framework ''
         my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
         # change suggestion status if applicable
-- 
1.9.1