New tool to import borrowers
authorChris Cormack <crc@liblime.com>
Mon, 29 Oct 2007 02:50:58 +0000 (21:50 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Mon, 29 Oct 2007 03:30:59 +0000 (22:30 -0500)
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tmpl [new file with mode: 0644]
koha-tmpl/intranet-tmpl/prog/en/modules/tools/tools-home.tmpl
tools/import_borrowers.pl [new file with mode: 0755]

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tmpl
new file mode 100644 (file)
index 0000000..4d601ba
--- /dev/null
@@ -0,0 +1,61 @@
+<!-- TMPL_INCLUDE NAME="doc-head-open.inc" -->
+<title>Koha &rsaquo; Cataloging &rsaquo; MARC Import</title>
+<!-- TMPL_INCLUDE NAME="doc-head-close.inc" -->
+</head>
+<body>
+<!-- TMPL_INCLUDE NAME="header.inc" -->
+<!-- TMPL_INCLUDE NAME="cat-search.inc" -->
+
+<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/reports/tools-home.pl">Tools</a> &rsaquo; Borrower Import</div>
+
+<div id="doc3" class="yui-t2">
+   
+   <div id="bd">
+       <div id="yui-main">
+       <div class="yui-b">
+
+<h1>Borrower Import</h1>
+
+<!-- TMPL_IF name="uploadborrowers" -->
+<p>Import results :</p>
+<ul>
+       <li><!-- TMPL_VAR name="imported" --> imported records</li>
+       <li><!-- TMPL_VAR name="overwritten" --> overwritten</li>
+       <li><!-- TMPL_VAR name="alreadyindb" --> not imported because already in borrowers table and overwrite disabled</li>
+       <li><!-- TMPL_VAR name="invalid" --> not imported because they are not in the expected format !</li>
+       <li><!-- TMPL_VAR name="total" -->  records parsed</li>
+       <li><a href="/cgi-bin/koha/admin-home.pl">Back</a></li>
+</ul>
+<!-- TMPL_ELSE -->
+<ul>
+       <li>Select a file to import into the borrowers table</li>
+       <li>If a cardnumber exists in the table, you can choose whether to ignore the new one or overwrite the old one.</li>
+</ul>
+<form method="post" action="<!-- TMPL_VAR name="SCRIPT_NAME" -->" enctype="multipart/form-data">
+<fieldset class="rows">
+<legend>Import into the borrowers table</legend><ol>
+       <li>
+               <label for="uploadborrowers">Select the file to import: </label>
+               
+                       <input type="file" id="uploadborrowers" name="uploadborrowers" /><br />
+               
+       </li>
+
+</ol></fieldset>
+       <fieldset class="rows">
+       <legend>If cardnumber is already in the borrowers table:</legend><ol><li class="radio">
+                
+               <input type="radio" id="overwrite_cardnumberno" name="overwrite_cardnumber" value="0" checked="checked" /><label for="overwrite_cardnumberno">Ignore this one, keep the existing one</label></li>
+<li class="radio">
+                       <input type="radio" id="overwrite_cardnumberyes" name="overwrite_cardnumber" value="1" /><label for="overwrite_cardnumberyes">Overwrite the existing one with this</label>
+       </li></ol></fieldset><input type="submit" value="Import" />
+</form>
+<!-- /TMPL_IF -->
+
+</div>
+</div>
+<div class="yui-b">
+<!-- TMPL_INCLUDE NAME="tools-menu.inc" -->
+</div>
+</div>
+<!-- TMPL_INCLUDE NAME="intranet-bottom.inc" -->
index cd07499..98046f7 100644 (file)
 
     <dt><a href="/cgi-bin/koha/tools/import.pl">Reservoir Import</a></dt>
     <dd>Import data into the reservoir.</dd>
+       
+       <dt><a href="/cgi-bin/koha/tools/import_borrowers.pl">Import Borrowers</a></dt>
+    <dd>Import borrowers data.</dd>
+       
        </dl>
 </div>
 <div class="yui-u">
diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl
new file mode 100755 (executable)
index 0000000..54ba751
--- /dev/null
@@ -0,0 +1,122 @@
+#!/usr/bin/perl
+
+# Copyright 2007 Liblime Ltd
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
+# Script to take some borrowers data in a known format and load it into Koha
+#
+# File format
+#
+# cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,
+# address line , address line 2, city, zipcode, email, phone, mobile, fax, work email, work phone,
+# alternate streetnumber, alternate streettype, alternate address line 1, alternate city,
+# alternate zipcode, alternate email, alternate phone, date of birth, branchcode,
+# categorycode, enrollment date, expiry date, noaddress, lost, debarred, contact surname,
+# contact firstname, contact title, borrower notes, contact relationship, ethnicity, ethnicity notes
+# gender, username, opac note, contact note, password, sort one, sort two
+#
+# any fields except cardnumber can be blank but the number of fields must match
+# dates should be in the format you have set up Koha to expect
+
+use strict;
+use C4::Auth;
+use C4::Output;
+use C4::Date;
+use C4::Context;
+
+use Text::CSV;
+use CGI;
+
+my @columnkeys = (
+    'cardnumber',   'surname',          'firstname',    'title',
+    'othernames',   'initials',         'streetnumber', 'streettype',
+    'address',      'address2',         'city',         'email',
+    'phone',        'mobile',           'fax',          'emailpro',
+    'phonepro',     'B_streetnumber',   'B_streettype', 'B_address',
+    'B_city',       'B_zipcode',        'B_email',      'B_phone',
+    'dateofbirth',  'branchcode',       'categorycode', 'dateenrolled',
+    'dateexpiry',   'gonenoaddress',    'lost',         'debarred',
+    'contactname',  'contactfirstname', 'contacttitle', 'borrowernotes',
+    'relationship', 'ethnicity',        'ethnotes',     'sex',
+    'userid',       'opacnote',         'contactnote',  'password',
+    'sort1',        'sort2'
+);
+
+my $input = new CGI;
+
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+    {
+        template_name   => "tools/import_borrowers.tmpl",
+        query           => $input,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { tools => 1 },
+        debug           => 1,
+    }
+);
+
+my $uploadborrowers      = $input->param('uploadborrowers');
+my $overwrite_cardnumber = $input->param('overwrite_cardnumber');
+
+$template->param( SCRIPT_NAME => $ENV{'SCRIPT_NAME'} );
+
+if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
+    my $csv         = Text::CSV->new();
+    my $imported    = 0;
+    my $alreadyindb = 0;
+    my $overwritten = 0;
+    my $invalid     = 0;
+    while ( my $borrowerline = <$uploadborrowers> ) {
+
+        my $status  = $csv->parse($borrowerline);
+        my @columns = $csv->fields();
+        my %borrower;
+        if ( @columns == @columnkeys ) {
+            @borrower{@columnkeys} = @columns;
+            if ( GetMember( $borrower{'cardnumber'}, 'cardnumber' ) ) {
+
+                # borrower exists
+                if ($overwrite_cardnumber) {
+                    ModMember(%borrower);
+                    $overwritten++;
+                }
+                else {
+                    $alreadyindb++;
+                }
+            }
+            else {
+                AddMember(%borrower);
+                $imported++;
+            }
+        }
+        else {
+            $invalid++;
+        }
+    }
+    $template->param( 'uploadborrowers' => 1 );
+    $template->param(
+        'uploadborrowers' => 1,
+        'imported'        => $imported,
+        'overwritten'     => $overwritten,
+        'alreadyindb'     => $alreadyindb,
+        'invalid'         => $invalid,
+        'total'           => $imported + $alreadyindb + $invalid + $overwritten,
+    );
+
+}
+output_html_with_http_headers $input, $cookie, $template->output;
+