This commit was manufactured by cvs2svn to create tag 'freeside_1_4_2'.
[freeside.git] / FS / bin / freeside-setup
index 213dcb9..65e67b5 100755 (executable)
@@ -6,6 +6,8 @@ BEGIN { $FS::Record::setup_hack = 1; }
 use strict;
 use vars qw($opt_s);
 use Getopt::Std;
+use Locale::Country;
+use Locale::SubCountry;
 use DBI;
 use DBIx::DBSchema 0.20;
 use DBIx::DBSchema::Table;
@@ -240,60 +242,40 @@ foreach my $statement ( $dbdef->sql($dbh) ) {
     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
 }
 
-#not really sample data (and shouldn't default to US)
-
 #cust_main_county
+foreach my $country ( sort map uc($_), all_country_codes ) {
 
-#USPS state codes
-foreach ( qw(
-AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
-ME MH MD MA MI MN MS MO MT NC ND NE NH NJ NM NV NY MP OH OK OR PA PW PR RI 
-SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
-) ) {
-  my($cust_main_county)=new FS::cust_main_county({
-    'state' => $_,
-    'tax'   => 0,
-    'country' => 'US',
-  });  
-  my($error);
-  $error=$cust_main_county->insert;
-  die $error if $error;
-}
+  my $subcountry = eval { new Locale::SubCountry($country) };
+  my @states = $subcountry ? $subcountry->all_codes : undef;
 
-#AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
-foreach ( qw(
-VIC NSW NT QLD TAS ACT WA SA
-) ) {
-  my($cust_main_county)=new FS::cust_main_county({
-    'state' => $_,
-    'tax'   => 0,
-    'country' => 'AU',
-  });  
-  my($error);
-  $error=$cust_main_county->insert;
-  die $error if $error;
-}
+  if ( !scalar(@states) || ( scalar(@states) == 1 && !defined($states[0]) ) ) {
 
-#ISO 2-letter country codes (same as country TLDs) except US and AU
-foreach ( qw(
-AF AL DZ AS AD AO AI AQ AG AR AM AW AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
-BA BW BV BR IO BN BG BF BI KH CM CA CV KY CF TD CL CN CX CC CO KM CG CK CR CI
-HR CU CY CZ DK DJ DM DO TP EC EG SV GQ ER EE ET FK FO FJ FI FR FX GF PF TF GA
-GM GE DE GH GI GR GL GD GP GU GT GN GW GY HT HM HN HK HU IS IN ID IR IQ IE IL
-IT JM JP JO KZ KE KI KP KR KW KG LA LV LB LS LR LY LI LT LU MO MK MG MW MY MV
-ML MT MH MQ MR MU YT MX FM MD MC MN MS MA MZ MM NA NR NP NL AN NC NZ NI NE NG
-NU NF MP NO OM PK PW PA PG PY PE PH PN PL PT PR QA RE RO RU RW KN LC VC WS SM
-ST SA SN SC SL SG SK SI SB SO ZA GS ES LK SH PM SD SR SJ SZ SE CH SY TW TJ TZ
-TH TG TK TO TT TN TR TM TC TV UG UA AE GB UM UY UZ VU VA VE VN VG VI WF EH
-YE YU ZR ZM ZW
-) ) {
-  my($cust_main_county)=new FS::cust_main_county({
-    'tax'   => 0,
-    'country' => $_,
-  });  
-  my($error);
-  $error=$cust_main_county->insert;
-  die $error if $error;
+    my $cust_main_county = new FS::cust_main_county({
+      'tax'   => 0,
+      'country' => $country,
+    });  
+    my $error = $cust_main_county->insert;
+    die $error if $error;
+
+  } else {
+
+    if ( $states[0] =~ /^(\d+|\w)$/ ) {
+      @states = map $subcountry->full_name($_), @states
+    }
+
+    foreach my $state ( @states ) {
+
+      my $cust_main_county = new FS::cust_main_county({
+        'state' => $state,
+        'tax'   => 0,
+        'country' => $country,
+      });  
+      my $error = $cust_main_county->insert;
+      die $error if $error;
+
+    }
+  
+  }
 }
 
 #billing events
@@ -436,7 +418,7 @@ sub tables_hash_hack {
         'custnum',  'int', '', '',
         '_date',    @date_type,
         'amount',   @money_type,
-        'otaker',   'varchar', '', 8,
+        'otaker',   'varchar', '', 32,
         'reason',   'text', 'NULL', '',
         'closed',    'char', 'NULL', 1,
       ],
@@ -466,7 +448,7 @@ sub tables_hash_hack {
         'last',     'varchar', '',     $char_d,
 #        'middle',   'varchar', 'NULL', $char_d,
         'first',    'varchar', '',     $char_d,
-        'ss',       'char', 'NULL', 11,
+        'ss',       'varchar', 'NULL', 11,
         'company',  'varchar', 'NULL', $char_d,
         'address1', 'varchar', '',     $char_d,
         'address2', 'varchar', 'NULL', $char_d,
@@ -498,7 +480,7 @@ sub tables_hash_hack {
         'paydate',  'varchar', 'NULL', 10,
         'payname',  'varchar', 'NULL', $char_d,
         'tax',      'char', 'NULL', 1,
-        'otaker',   'varchar', '',     8,
+        'otaker',   'varchar', '',     32,
         'refnum',   'int',  '',     '',
         'referral_custnum', 'int',  'NULL', '',
         'comments', 'text', 'NULL', '',
@@ -600,7 +582,7 @@ sub tables_hash_hack {
         'pkgnum',    'int',    '',   '',
         'custnum',   'int',    '',   '',
         'pkgpart',   'int',    '',   '',
-        'otaker',    'varchar', '', 8,
+        'otaker',    'varchar', '', 32,
         'setup',     @date_type,
         'bill',      @date_type,
         'susp',      @date_type,
@@ -620,7 +602,7 @@ sub tables_hash_hack {
         'custnum',  'int',    '',   '',
         '_date',        @date_type,
         'refund',       @money_type,
-        'otaker',       'varchar',   '',   8,
+        'otaker',       'varchar',   '',   32,
         'reason',       'varchar',   '',   $char_d,
         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
                                              # into payment type table.
@@ -823,10 +805,10 @@ sub tables_hash_hack {
       'columns' => [
         'recnum',    'int',     '',  '',
         'svcnum',    'int',     '',  '',
-        'reczone',   'varchar', '',  $char_d,
+        'reczone',   'varchar', '',  255,
         'recaf',     'char',    '',  2,
-        'rectype',   'char',    '',  5,
-        'recdata',   'varchar', '',  $char_d,
+        'rectype',   'varchar', '',  5,
+        'recdata',   'varchar', '',  255,
       ],
       'primary_key' => 'recnum',
       'unique'      => [],