bug 2503: moving offline circulation db generator to misc/cronjobs directory
[koha.git] / misc / cronjobs / create_koc_db.pl
1 #!/usr/bin/perl -w
2
3 ## FIXME: if there is an existing borrowers.db file it needs to be deleted before this script is ran.
4
5 use DBI;
6 use C4::Context;
7
8 use strict;
9
10 ## Create DB Connections
11 my $dbh_mysql = C4::Context->dbh;
12 my $dbh_sqlite = DBI->connect("dbi:SQLite:dbname=borrowers.db","","");
13
14 ## Create sqlite borrowers table to mirror the koha borrowers table structure
15 my $sth_mysql = $dbh_mysql->prepare("DESCRIBE borrowers");
16 $sth_mysql->execute();
17
18 my $sqlite_create_sql = "CREATE TABLE borrowers ( \n";
19
20 my $result = $sth_mysql->fetchrow_hashref();
21 my $field = $result->{'Field'};
22 my $type = $result->{'Type'};
23 $sqlite_create_sql .= " $field $type ";
24
25 while ( my $result = $sth_mysql->fetchrow_hashref() ) {
26   $field = $result->{'Field'};
27   $type = $result->{'Type'};
28   $sqlite_create_sql .= " , \n $field $type ";
29 }
30 $sth_mysql->finish();
31
32 $sqlite_create_sql .= " , \n total_fines decimal(28,6) "; ## Extra field to store the total fines for a borrower in.
33 $sqlite_create_sql .= " ) ";
34
35 my $sth_sqlite = $dbh_sqlite->prepare( $sqlite_create_sql );
36 $sth_sqlite->execute();
37 $sth_sqlite->finish();
38
39 ## Import the data from the koha.borrowers table into our sqlite table
40 $sth_mysql = $dbh_mysql->prepare("SELECT * FROM borrowers ORDER BY borrowernumber DESC");
41 my $fields_count = $sth_mysql->execute();
42
43 print "Number of Borrowers: $fields_count\n";
44
45 while ( my $borrower = $sth_mysql->fetchrow_hashref ) {
46   my @keys;
47   my @values;
48
49   print "Working on Borrower # $borrower->{'borrowernumber'} \n";
50
51   my $sql = "INSERT INTO borrowers ( ";
52   
53   my $firstLine = 1;
54   foreach my $key (keys %$borrower) {
55     if ( $firstLine ) {
56       $sql .= '?';
57       $firstLine = 0;
58     } else {
59       $sql .= ', ?';
60     }
61     push( @keys, $key );
62   }
63   
64   $sql .= " ) VALUES ( ";
65   
66   $firstLine = 1;
67   foreach my $key (keys %$borrower) {
68     my $data = $borrower->{$key};
69     
70     if ( $firstLine ) {
71       $sql .= '?';
72       $firstLine = 0;
73     } else {
74       $sql .= ', ?';
75     }
76     push( @values, $data );
77   }
78
79   $sql .= " ) ";
80
81 print "\n$sql\n";
82
83   $sth_sqlite = $dbh_sqlite->prepare( $sql );
84   $sth_sqlite->execute( @keys, @values );
85   $sth_sqlite->finish();
86 }
87
88 ## Import the fines from koha.accountlines into the sqlite db
89 $sth_mysql = $dbh_mysql->prepare( "SELECT DISTINCT borrowernumber, SUM( amountoutstanding ) AS total_fines
90                                     FROM accountlines
91                                     GROUP BY borrowernumber" );
92 while ( my $result = $sth_mysql->fetchrow_hashref() ) {
93   my $borrowernumber = $result->{'borrowernumber'};
94   my $total_fines = $result->{'total_fines'};
95   
96   print "Fines for Borrower # $borrowernumber are \$ $total_fines \n";
97   
98   my $sql = "UPDATE borrowers SET total_fines = ? WHERE borrowernumber = ?";
99   
100   $sth_sqlite = $dbh_sqlite->prepare( $sql );
101   $sth_sqlite->execute( $total_fines, $borrowernumber );
102   $sth_sqlite->finish();
103 }
104
105 ## Create sqlite issues table with minimal information needed from koha tables issues, items, biblio, biblioitems
106 my $fields = GetIssuesFields();
107
108 $sqlite_create_sql = "CREATE TABLE issues ( \n";
109
110 my $firstField = 1;
111 foreach my $key (keys %$fields) {
112   my $field = $key;
113   my $type = $fields->{$key};
114
115   if ( $firstField ) {
116     $sqlite_create_sql .= " $field, $type ";
117     $firstField = 0;
118   } else {
119     $sqlite_create_sql .= ", \n $field, $type ";
120   }
121 }
122 $sqlite_create_sql .= " ) ";
123
124 $sth_sqlite = $dbh_sqlite->preapre( $sqlite_create_sql );
125 $sth_sqlite->execute();
126 $sth_sqlite->finish();
127
128 ## Import open issues from the koha database
129 $sth_mysql = $dbh_mysql->prepare( "SELECT * FROM issues, items, biblioitems, biblio
130                                     WHERE issues.itemnumber = items.itemnumber
131                                     AND items.biblionumber = biblioitems.biblionumber
132                                     AND items.biblionumber = biblio.biblionumber
133                                     AND returndate IS NULL
134                                     ORDER By borrowernumber DESC" );
135
136 while ( my $result = $sth_mysql->fetchrow_hashref ) {
137   my @keys;
138   my @values;
139
140   print "Adding issue for Borrower # $result->{'borrowernumber'} \n";
141
142   my $sql = "INSERT INTO issues ( ";
143   
144   my $firstLine = 1;
145   foreach my $key (keys %$result) {
146     if ( $firstLine ) {
147       $sql .= '?';
148       $firstLine = 0;
149     } else {
150       $sql .= ', ?';
151     }
152     push( @keys, $key );
153   }
154   
155   $sql .= " ) VALUES ( ";
156   
157   $firstLine = 1;
158   foreach my $key (keys %$result) {
159     my $data = $result->{$key};
160     
161     if ( $firstLine ) {
162       $sql .= '?';
163       $firstLine = 0;
164     } else {
165       $sql .= ', ?';
166     }
167     push( @values, $data );
168   }
169
170   $sql .= " ) ";
171
172 print "\n$sql\n";
173
174   $sth_sqlite = $dbh_sqlite->prepare( $sql );
175   $sth_sqlite->execute( @keys, @values );
176   $sth_sqlite->finish();
177 }
178
179
180 ## This sub returns a hashref where the keys are all the fields in the given tables, and the data is the field's type
181 sub GetIssuesFields {
182   my $dbh = C4::Context->dbh;
183   
184   my @tables = ( 'issues', 'items', 'biblioitems', 'biblio' );
185   my $fields;
186   
187   foreach my $table ( @tables ) {
188     my $sth = $dbh->prepare( 'DESCRIBE ?' );
189     $sth->execute( $table );
190     
191     while ( my $result = $sth->fetchrow_hashref ) {
192       my $field = $result->{'Field'};
193       my $type = $result->{'Type'};
194       
195       $fields->{$field} = $type;
196     }
197     
198     $sth->finish()
199   }
200   
201   return $fields;
202 }