show credit balance on invoices, #11564
[freeside.git] / rt / sbin / factory
index 882e4a8..ebe93b6 100644 (file)
@@ -1,39 +1,41 @@
-#!/usr/bin/perl
-# {{{ BEGIN BPS TAGGED BLOCK
-# 
+#!/usr/bin/perl -w
+# BEGIN BPS TAGGED BLOCK {{{
+#
 # COPYRIGHT:
 # COPYRIGHT:
-#  
-# This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
-#                                          <jesse@bestpractical.com>
-# 
+#
+# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
+#                                          <sales@bestpractical.com>
+#
 # (Except where explicitly superseded by other copyright notices)
 # (Except where explicitly superseded by other copyright notices)
-# 
-# 
+#
+#
 # LICENSE:
 # LICENSE:
-# 
+#
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # been provided with this software, but in any event can be snarfed
 # from www.gnu.org.
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # been provided with this software, but in any event can be snarfed
 # from www.gnu.org.
-# 
+#
 # This work 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.
 # This work 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 this program; if not, write to the Free Software
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-# 
-# 
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
 # CONTRIBUTION SUBMISSION POLICY:
 # CONTRIBUTION SUBMISSION POLICY:
-# 
+#
 # (The following paragraph is not intended to limit the rights granted
 # to you to modify and distribute this software under the terms of
 # the GNU General Public License and is only of importance to you if
 # you choose to contribute your changes and enhancements to the
 # community by submitting them to Best Practical Solutions, LLC.)
 # (The following paragraph is not intended to limit the rights granted
 # to you to modify and distribute this software under the terms of
 # the GNU General Public License and is only of importance to you if
 # you choose to contribute your changes and enhancements to the
 # community by submitting them to Best Practical Solutions, LLC.)
-# 
+#
 # By intentionally submitting any modifications, corrections or
 # derivatives to this work, or any other work intended for use with
 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
 # By intentionally submitting any modifications, corrections or
 # derivatives to this work, or any other work intended for use with
 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
 # royalty-free, perpetual, license to use, copy, create derivative
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
 # royalty-free, perpetual, license to use, copy, create derivative
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
-# 
-# }}} END BPS TAGGED BLOCK
+#
+# END BPS TAGGED BLOCK }}}
+use strict;
 use DBI;
 
 use DBI;
 
+die "Usage: $0 database namespace" if @ARGV != 2;
+
 my $database  = shift;
 my $namespace = shift;
 
 my $database  = shift;
 my $namespace = shift;
 
@@ -59,11 +64,17 @@ my $password = '';
 
 
 my $LicenseBlock = << '.';
 
 
 my $LicenseBlock = << '.';
-# BEGIN BPS TAGGED BLOCK
+# BEGIN BPS TAGGED BLOCK {{{
+# 
+# COPYRIGHT:
+# 
+# This software is Copyright (c) 1996-2008 Best Practical Solutions, LLC
+#                                          <jesse@bestpractical.com>
+# 
+# (Except where explicitly superseded by other copyright notices)
 # 
 # 
-# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
 # 
 # 
-# (Except where explictly superceded by other copyright notices)
+# LICENSE:
 # 
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # 
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
@@ -75,17 +86,35 @@ my $LicenseBlock = << '.';
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
 # 
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
 # 
-# Unless otherwise specified, all modifications, corrections or
-# extensions to this work which alter its source code become the
-# property of Best Practical Solutions, LLC when submitted for
-# inclusion in the work.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+# 
 # 
 # 
+# CONTRIBUTION SUBMISSION POLICY:
 # 
 # 
-# END BPS TAGGED BLOCK
-
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+# 
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+# 
+# END BPS TAGGED BLOCK }}}
 .
 
 my $Attribution = << '.';
 .
 
 my $Attribution = << '.';
+
 # Autogenerated by DBIx::SearchBuilder factory (by <jesse@bestpractical.com>)
 # WARNING: THIS FILE IS AUTOGENERATED. ALL CHANGES TO THIS FILE WILL BE LOST.  
 # 
 # Autogenerated by DBIx::SearchBuilder factory (by <jesse@bestpractical.com>)
 # WARNING: THIS FILE IS AUTOGENERATED. ALL CHANGES TO THIS FILE WILL BE LOST.  
 # 
@@ -100,7 +129,7 @@ my $dsn = "DBI:$driver:database=$database;host=$hostname";
 my $dbh = DBI->connect( $dsn, $user, $password );
 
 #get all tables out of database
 my $dbh = DBI->connect( $dsn, $user, $password );
 
 #get all tables out of database
-my @tables = $dbh->tables();
+my @tables = map { s/^\`\Q$database\E\`\.//; $_ } $dbh->tables();
 
 my ( %tablemap, $typemap, %modulemap );
 
 
 my ( %tablemap, $typemap, %modulemap );
 
@@ -126,6 +155,7 @@ foreach my $table (@tables) {
 $tablemap{'CreatedBy'} = 'User';
 $tablemap{'UpdatedBy'} = 'User';
 
 $tablemap{'CreatedBy'} = 'User';
 $tablemap{'UpdatedBy'} = 'User';
 
+my %typemap;
 $typemap{'id'}            = 'ro';
 $typemap{'Creator'}       = 'auto';
 $typemap{'Created'}       = 'auto';
 $typemap{'id'}            = 'ro';
 $typemap{'Creator'}       = 'auto';
 $typemap{'Created'}       = 'auto';
@@ -162,7 +192,27 @@ foreach my $table (@tables) {
     my $ClassAccessible = "";
     my $FieldsPod       = "";
     my $CreatePod       = "";
     my $ClassAccessible = "";
     my $FieldsPod       = "";
     my $CreatePod       = "";
+    my $RecordInit      = "";
     my %fields;
     my %fields;
+
+
+    my $introspection = $dbh->prepare("SELECT * from $table where id is null");
+    $introspection->execute();
+    my @names =@{ $introspection->{'NAME'}};
+    my @types = @{$introspection->{'TYPE'}};
+    my @is_blob = @{$introspection->{'mysql_is_blob'}};
+    my @is_num = @{$introspection->{'mysql_is_num'}};
+
+    my %blobness = ();
+    my %sqltypes = ();
+    my %numeric = ();
+    foreach my $name (@names) {
+        $sqltypes{$name} = shift @types;
+        $blobness{$name} = (shift @is_blob || "0");
+        $numeric{$name} = (shift @is_num || "0");
+    }
+
+
     my $sth = $dbh->prepare("DESCRIBE $table");
     $sth->execute;
 
     my $sth = $dbh->prepare("DESCRIBE $table");
     $sth->execute;
 
@@ -170,10 +220,16 @@ foreach my $table (@tables) {
         my $field   = $row->{'Field'};
         my $type    = $row->{'Type'};
         my $default = $row->{'Default'};
         my $field   = $row->{'Field'};
         my $type    = $row->{'Type'};
         my $default = $row->{'Default'};
+        my $length = 0;
+        if ($type =~ /^(?:.*?)\((\d+)\)$/) {
+                $length = $1; 
+        }
         $fields{$field} = 1;
 
         #generate the 'accessible' datastructure
 
         $fields{$field} = 1;
 
         #generate the 'accessible' datastructure
 
+        no warnings 'uninitialized';
+
         if ( $typemap{$field} eq 'auto' ) {
             $ClassAccessible .= "        $field => 
                {read => 1, auto => 1,";
         if ( $typemap{$field} eq 'auto' ) {
             $ClassAccessible .= "        $field => 
                {read => 1, auto => 1,";
@@ -187,7 +243,7 @@ foreach my $table (@tables) {
                {read => 1, write => 1,";
 
         }
                {read => 1, write => 1,";
 
         }
-
+        $ClassAccessible .= " sql_type => $sqltypes{$field}, length => $length,  is_blob => $blobness{$field},  is_numeric => $numeric{$field}, ";
         $ClassAccessible .= " type => '$type', default => '$default'},\n";
 
         #generate pod for the accessible fields
         $ClassAccessible .= " type => '$type', default => '$default'},\n";
 
         #generate pod for the accessible fields
@@ -199,7 +255,7 @@ Returns the current value of $field.
 
 ";
 
 
 ";
 
-        unless ( $typemap{$field} eq 'auto' || $typemap{$field} eq 'ro' ) {
+        unless ( exists $typemap{$field} && ( $typemap{$field} eq 'auto' || $typemap{$field} eq 'ro' )) {
             $FieldsPod .= "
 
 =head2 Set$field VALUE
             $FieldsPod .= "
 
 =head2 Set$field VALUE
@@ -254,7 +310,7 @@ sub ${field}Obj {
 
     }
 
 
     }
 
-    $Create = "
+    my $Create = "
 sub Create {
     my \$self = shift;
     my \%args = ( 
 sub Create {
     my \$self = shift;
     my \%args = ( 
@@ -306,10 +362,15 @@ sub _Init {
 
         $CollectionClass .= "
 
 
         $CollectionClass .= "
 
-  # By default, order by name
-  \$self->OrderBy( ALIAS => 'main',
-                  FIELD => 'SortOrder',
-                  ORDER => 'ASC');
+  # By default, order by SortOrder
+  \$self->OrderByCols(
+        { ALIAS => 'main',
+          FIELD => 'SortOrder',
+          ORDER => 'ASC' },
+        { ALIAS => 'main',
+          FIELD => 'id',
+          ORDER => 'ASC' },
+     );
 ";
     }
     $CollectionClass .= "
 ";
     }
     $CollectionClass .= "
@@ -397,7 +458,7 @@ $ClassAccessible
 
     open( COL, ">$CollectionClassPath" );
     print COL $CollectionClass;
 
     open( COL, ">$CollectionClassPath" );
     print COL $CollectionClass;
-    close($COL);
+    close(COL);
 
 }
 
 
 }
 
@@ -438,7 +499,7 @@ _Vendor is for 3rd-party vendor add-ons, while _Local is for site-local customiz
 
 These overlay files can contain new subs or subs to replace existing subs in this module.
 
 
 These overlay files can contain new subs or subs to replace existing subs in this module.
 
-If you'll be working with perl 5.6.0 or greater, each of these files should begin with the line 
+Each of these files should begin with the line 
 
    no warnings qw(redefine);
 
 
    no warnings qw(redefine);