keep our own list of states; remove some states that Data::Faker and Locale::SubCount...
[freeside.git] / bin / customer-faker
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Data::Faker;
6 use Business::CreditCard;
7 use FS::UID qw(adminsuidsetup);
8 use FS::Record qw(qsearch);
9 use FS::cust_main;
10 use FS::cust_pkg;
11 use FS::svc_acct;
12
13 my $agentnum = 1;
14 my $refnum = 1;
15
16 my @pkgs = ( 2, 3, 4 );
17 #my @pkgs = ( 4, 5, 6 );
18 my $svcpart = 2;
19
20 use vars qw( $opt_p );
21 getopts('p:');
22
23 my $user = shift or die &usage;
24 my $num = shift or die &usage;
25 adminsuidsetup($user);
26
27 my $onum = $num;
28 my $start = time;
29
30 my @states = qw( AL AK AS AZ AR CA CO CT DE DC FL GA GU HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND MP OH OK OR PA PR RI SC SD TN TX UT VT VI VA WA WV WI WY );
31 #FM MH
32
33 until ( $num-- <= 0 ) {
34
35   my $faker = new Data::Faker;
36
37   my $cust_main = new FS::cust_main {
38     'agentnum' => $agentnum,
39     'refnum'   => $refnum,
40     'first'    => $faker->first_name,
41     'last'     => $faker->last_name,
42     'company'  => ( $num % 2 ? $faker->company. ', '. $faker->company_suffix : '' ), #half with companies..
43     'address1' => $faker->street_address,
44     'city'     => 'Tofutown', #missing, so everyone is from tofutown# $faker->city,
45     #'state'    => $faker->us_state_abbr,
46     'state'    => $states[ int(rand($#states)) ],
47     'zip'      => $faker->us_zip_code,
48     'country'  => 'US',
49     'daytime'  => $faker->phone_number,
50     'night'    => $faker->phone_number,
51     #forget it, these can have extensions# 'fax'      => ( $num % 2 ? $faker->phone_number : '' ), #ditto
52     #bah, forget shipping addresses
53     'payby'    => 'BILL',
54     'payip'    => $faker->ip_address,
55   };
56
57   if ( $opt_p eq 'CARD' || ( !$opt_p && rand() > .33 ) ) {
58     $cust_main->payby('CARD');
59     my $cardnum = '4123'. sprintf('%011u', int(rand(100000000000)) );
60     $cust_main->payinfo( $cardnum. generate_last_digit($cardnum) );
61     $cust_main->paydate( '2009-05-01' );
62   } elsif ( $opt_p eq 'CHEK' || ( !$opt_p && rand() > .66 ) ) {
63     $cust_main->payby('CHEK');
64     my $payinfo = sprintf('%7u@%09u', int(rand(10000000)), int(rand(1000000000)) ); 
65     $cust_main->payinfo($payinfo);
66     $cust_main->payname( 'First International Bank of Testing' );
67   }
68
69   # could insert invoicing_list and other stuff too..  hell, could insert
70   # packages, services, more
71   # but i just wanted 10k customers to test the pager and this was good enough
72   # not anymore, here's some services and packages
73   
74   my $now = time;
75   my $year = 31556736; #60*60*24*365.24
76   my $setup = $now - int(rand($year));
77
78   my $cust_pkg = new FS::cust_pkg {
79     'pkgpart' => $pkgs[ int(rand(scalar(@pkgs))) ],
80
81     #some dates in here would be nice
82     'setup'      => $setup,
83     #'last_bill'
84     #'bill'
85     #'susp'
86     #'expire'
87     #'cancel'
88   };
89
90   my $svc_acct = new FS::svc_acct {
91     'svcpart'  => $svcpart,
92     'username' => $faker->username,
93   };
94
95   while ( qsearch( 'svc_acct', { 'username' => $svc_acct->username } ) ) {
96     my $username = $svc_acct->username;
97     $username++;
98     $svc_acct->username($username);
99   }
100
101   use Tie::RefHash;
102   tie my %hash, 'Tie::RefHash',
103     $cust_pkg => [ $svc_acct ],
104   ;
105
106   my $error = $cust_main->insert( \%hash );
107   die $error if $error;
108
109 }
110
111 my $end = time;
112
113 my $sec = $end-$start;
114 $sec=1 if $sec==0;
115 my $persec = $onum / $sec;
116 print "$onum customers inserted in $sec seconds ($persec customers/sec)\n";
117
118 #---
119
120 sub usage {
121   die "Usage:\n\n  customer-faker [ -p payby ] user num_fakes\n";
122 }