Diff of /koha-simulate/tags/0.1.1/Simulate.pm [000000] .. [r88] Maximize Restore

  Switch to side-by-side view

--- a
+++ b/koha-simulate/tags/0.1.1/Simulate.pm
@@ -0,0 +1,416 @@
+## Copyright 2006 Kyle Hall
+
+## Copyright 2006 Kyle Hall
+
+## This file is part of koha-tools.
+
+## koha-tools 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-tools 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-tools; if not, write to the Free Software
+## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+## The latest version of the file can always be found at
+## http://koha-tools.sourceforge.net
+
+package C4::Simulate;
+
+use strict;
+
+require Exporter;
+
+use DBI;
+use C4::Context;
+use C4::Circulation::Circ2;
+
+## Input: Number of patrons to check out items to, max number of items per patron
+## Output: None
+## Action: Randomly checks out a number of books to a random borrower
+sub perform_random_issues {
+  my ($number_of_patrons, $max_items_per_patron) = @_;
+
+  my @borrower_numbers = get_random_borrower_numbers($number_of_patrons);
+  
+  foreach my $borrower_number (@borrower_numbers) {
+    my $number_of_items = rand($max_items_per_patron) + 1;
+    my @barcodes = get_random_unissued_item_barcodes($number_of_items);
+    
+    foreach my $barcode (@barcodes) {
+      issue_item($barcode, $borrower_number);
+    }
+  }
+}
+
+## Input: Number of checkins to perform
+## Output: None
+## Action: Checks in a given number of randomly selected checked out books
+sub perform_random_returns {
+  my $number_of_returns = $_[0];
+  
+  my @barcodes = get_random_issued_item_barcodes($number_of_returns);
+  
+  foreach my $barcode (@barcodes) {
+    return_item($barcode);
+  }
+}
+
+## Input: Number of borrowers to get
+## Output: array of borrowernumbers
+sub get_random_borrower_numbers {
+  my ($number_of_borrowers_to_get) = @_;
+
+  my @borrowers;
+  
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT borrowernumber FROM borrowers
+                           ORDER BY RAND() LIMIT $number_of_borrowers_to_get")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute()
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  # Read the matching records and print them out          
+  while (my @data = $sth->fetchrow_array()) {
+    push(@borrowers, $data[0]);
+  }
+                                                        
+  return @borrowers;                                                        
+  
+}
+
+## Input: Number of items to get
+## Output: array of barcodes
+sub get_random_unissued_item_barcodes {
+  my ($number_of_items_to_get) = @_;
+
+  my @unissued_items;
+  
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT items.barcode
+                           FROM items
+                           LEFT JOIN issues ON items.itemnumber = issues.itemnumber
+                           WHERE (
+                             issues.returndate IS NULL
+                             AND issues.borrowernumber IS NULL
+                           )
+                           ORDER BY RAND()
+                           LIMIT $number_of_items_to_get")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute()
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  ## Push the barcodes onto the unissued_items array
+  while (my @data = $sth->fetchrow_array()) {
+    push(@unissued_items, $data[0]);
+  }
+                                                        
+  return @unissued_items;                                                        
+}
+
+## Input: Number of items to get
+## Output: array of barcodes
+sub get_random_issued_item_barcodes {
+  my ($number_of_items_to_get) = @_;
+  
+  my @issued_items;
+  
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT items.barcode
+                           FROM items, issues
+                           WHERE (
+                             issues.returndate IS NULL
+                             AND items.itemnumber = issues.itemnumber
+                           )
+                           ORDER BY RAND()
+                           LIMIT $number_of_items_to_get")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute()
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  # Read the matching records and print them out          
+  while (my @data = $sth->fetchrow_array()) {
+    push(@issued_items, $data[0]);
+  }
+                                                        
+  return @issued_items;                                                        
+
+}
+
+## Input: Item Barcode, Borrower Number
+## Output: None
+## Effects: Checks out given item to given borrower
+sub issue_item {
+  my ($item_barcode, $borrower_number) = @_;
+
+
+## I'd like to get this to work with C4::Circulation::Circ2::issuebook,
+## But for some reason it just doesn't do anything when I call it.
+
+#  my ($card_number, $date, $cancelreserve) = '';
+  
+#  my %env;
+#  $env{'branchcode'} = 'MPL';
+#  $env{'printer'}='devlp0';
+#  $env{'queue'}='devlp0';
+  
+#  my ($borrower, $flags) = &getpatroninformation(%env, $borrower_number, $card_number);
+
+#  &issuebook(\%env,$borrower,$item_barcode,$date,$cancelreserve);
+
+  use Date::Manip;
+  my $date_due;
+  $date_due = DateCalc("today","+ 14 days");
+  $date_due = UnixDate($date_due,"%Y-%m-%d");
+
+  my %item = get_item($item_barcode);
+  
+  write_issue_to_database($borrower_number,
+                          $item{'itemnumber'},
+                          $date_due,
+                          $item{'homebranch'},
+                          '',
+                          '',
+                          '',
+                          '');
+  
+  my $itemtype = get_itemtype($item{'biblioitemnumber'});
+  
+  write_statistic_to_database(
+                        $item{'holdingbranch'},
+                        my $proccode = '',
+                        my $value = 0,
+                        my $type = 'issue',
+                        my $other = '',
+                        my $usercode = '',
+                        $item{'itemnumber'},
+                        $itemtype,
+                        $borrower_number);
+}
+
+## Input: Item barcode
+## Output: None
+## Effects: Returns the item to its home branch
+sub return_item {
+  my ($item_barcode) = @_;
+
+  ## Get the home branch for this item
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT items.holdingbranch
+                           FROM items
+                           WHERE items.barcode = $item_barcode
+                           ")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute()
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  my @data = $sth->fetchrow_array();
+  my $item_branchcode = $data[0];
+
+  ## Return the item to its home branch
+  my ($doreturn, $messages, $iteminformation, $borrower) =
+      &returnbook($item_barcode, $item_branchcode);
+  
+  if ($doreturn) {
+    return 1;
+  } else {
+    die("Item Return Failed (barcode: $item_barcode, branchcode: $item_branchcode ): $messages");
+  }
+}
+
+## Input: Item barcode
+## Output: Hash of item's information
+sub get_item {
+  my $item_barcode = $_[0];  
+
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT *
+                           FROM items
+                           WHERE items.barcode = $item_barcode
+                           ")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute()
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  my @data = $sth->fetchrow_array();
+
+  my %item;  
+  
+  $item{'itemnumber'} = $data[0];
+  $item{'biblionumber'} = $data[1];
+  $item{'multivolumepart'} = $data[2];
+  $item{'biblioitemnumber'} = $data[3];
+  $item{'barcode'} = $data[4];
+  $item{'dateaccessioned'} = $data[5];
+  $item{'booksellerid'} = $data[6];
+  $item{'homebranch'} = $data[7];
+  $item{'price'} = $data[8];
+  $item{'replacementprice'} = $data[9];
+  $item{'replacementpricedate'} = $data[10];
+  $item{'datelastborrowed'} = $data[11];
+  $item{'datelastseen'} = $data[12];
+  $item{'multivolume'} = $data[13];
+  $item{'stack'} = $data[14];
+  $item{'notforloan'} = $data[15];
+  $item{'itemlost'} = $data[16];
+  $item{'wthdrawn'} = $data[17];
+  $item{'itemcallnumber'} = $data[18];
+  $item{'issues'} = $data[19];
+  $item{'renewals'} = $data[20];
+  $item{'reserves'} = $data[21];
+  $item{'restricted'} = $data[22];
+  $item{'binding'} = $data[23];
+  $item{'itemnotes'} = $data[24];
+  $item{'holdingbranch'} = $data[25];
+  $item{'paidfor'} = $data[26];
+  $item{'timestamp'} = $data[27];
+  $item{'location '} = $data[28];
+  
+  return %item;
+}
+
+## Input: borrowernumber, itemnumber, date_due, branchcode
+## Output: None
+## Action: Checks out issue to borrower from the books home branch
+sub write_issue_to_database {
+  my ($borrowernumber, 
+      $itemnumber, 
+      $date_due, 
+      $branchcode, 
+      $issueingbranch, 
+      $returndate, 
+      $lastrenewdate, 
+      $return, 
+      $renewals) = @_;
+
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("INSERT INTO issues (
+                           borrowernumber,
+                           itemnumber,
+                           date_due,
+                           branchcode,
+                           issuingbranch,
+                           returndate,
+                           lastreneweddate,
+                           return,
+                           renewals,
+                           timestamp )
+                           VALUES (
+                           ?, ?, ?, ?, NULL, NULL, NULL, NULL, NULL, NOW()
+                           );
+                           ")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute($borrowernumber, 
+                $itemnumber, 
+                $date_due, 
+                $branchcode, 
+                )
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";  
+  
+}
+
+## Input: datetime, branch, proccode, value, type, other, usercode, itemnumber, itemtype, borrowernumber
+## Output: None
+## Action: Creates a line in the statistics table
+sub write_statistic_to_database {
+  my ($branch,
+      $proccode,
+      $value,
+      $type,
+      $other,
+      $usercode,
+      $itemnumber,
+      $itemtype,
+      $borrowernumber) = @_;
+      
+  my $datetime = get_last_issued_timestamp();
+
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("INSERT INTO statistics (
+                           datetime,
+                           branch,
+                           proccode,
+                           value,
+                           type,
+                           other,
+                           usercode,
+                           itemnumber,
+                           itemtype,
+                           borrowernumber)
+                           VALUES (
+                           ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
+                           ")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute($datetime, 
+                $branch, 
+                $proccode, 
+                $value,
+                $type,
+                $other,
+                $usercode,
+                $itemnumber,
+                $itemtype,
+                $borrowernumber, 
+                )
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";  
+  
+}
+
+## Input: None
+## Output: The timestamp of the last issued item
+sub get_last_issued_timestamp {
+
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT MAX( TIMESTAMP ) AS newest_timestamp
+  FROM issues")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute()
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  my @data = $sth->fetchrow_array();
+
+  my $datetime  = $data[0];
+  
+  return $datetime;
+}
+
+sub get_itemtype {
+  my $biblioitemnumber = @_;
+  
+  my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
+
+  my $sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ?")
+    or die "Couldn't prepare statement: " . $dbh->errstr . "\n";                        
+
+  $sth->execute($biblioitemnumber)
+    or die "Couldn't execute statement: " . $sth->errstr . "\n";
+  
+  my @data = $sth->fetchrow_array();
+
+  my $itemtype  = $data[0];
+  
+  return $itemtype;
+  
+}