add -a option for agentnum
[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 $refnum = 1;
14
15 my @pkgs = ( 2, 3, 4 );
16 #my @pkgs = ( 4, 5, 6 );
17 my $svcpart = 2;
18
19 use vars qw( $opt_p $opt_a );
20 getopts('p:a:');
21
22 my $agentnum = $opt_a || 1;
23
24 my $user = shift or die &usage;
25 my $num = shift or die &usage;
26 adminsuidsetup($user);
27
28 my $onum = $num;
29 my $start = time;
30
31 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 );
32 #FM MH
33
34 until ( $num-- <= 0 ) {
35
36   my $faker = new Data::Faker;
37
38   my $cust_main = new FS::cust_main {
39     'agentnum' => $agentnum,
40     'refnum'   => $refnum,
41     'first'    => $faker->first_name,
42     'last'     => $faker->last_name,
43     'company'  => ( $num % 2 ? $faker->company. ', '. $faker->company_suffix : '' ), #half with companies..
44     'address1' => $faker->street_address,
45     'city'     => 'Tofutown', #missing, so everyone is from tofutown# $faker->city,
46     #'state'    => $faker->us_state_abbr,
47     'state'    => $states[ int(rand($#states)) ],
48     'zip'      => $faker->us_zip_code,
49     'country'  => 'US',
50     'daytime'  => $faker->phone_number,
51     'night'    => $faker->phone_number,
52     #forget it, these can have extensions# 'fax'      => ( $num % 2 ? $faker->phone_number : '' ), #ditto
53     #bah, forget shipping addresses
54     'payby'    => 'BILL',
55     'payip'    => $faker->ip_address,
56   };
57
58   if ( $opt_p eq 'CARD' || ( !$opt_p && rand() > .33 ) ) {
59     $cust_main->payby('CARD');
60     my $cardnum = '4123'. sprintf('%011u', int(rand(100000000000)) );
61     $cust_main->payinfo( $cardnum. generate_last_digit($cardnum) );
62     $cust_main->paydate( '2009-05-01' );
63   } elsif ( $opt_p eq 'CHEK' || ( !$opt_p && rand() > .66 ) ) {
64     $cust_main->payby('CHEK');
65     my $payinfo = sprintf('%7u@%09u', int(rand(10000000)), int(rand(1000000000)) ); 
66     $cust_main->payinfo($payinfo);
67     $cust_main->payname( 'First International Bank of Testing' );
68   }
69
70   # could insert invoicing_list and other stuff too..  hell, could insert
71   # packages, services, more
72   # but i just wanted 10k customers to test the pager and this was good enough
73   # not anymore, here's some services and packages
74   
75   my $now = time;
76   my $year = 31556736; #60*60*24*365.24
77   my $setup = $now - int(rand($year));
78
79   my $cust_pkg = new FS::cust_pkg {
80     'pkgpart' => $pkgs[ int(rand(scalar(@pkgs))) ],
81
82     #some dates in here would be nice
83     'setup'      => $setup,
84     #'last_bill'
85     #'bill'
86     #'susp'
87     #'expire'
88     #'cancel'
89   };
90
91   my $svc_acct = new FS::svc_acct {
92     'svcpart'  => $svcpart,
93     'username' => $faker->username,
94   };
95
96   while ( qsearch( 'svc_acct', { 'username' => $svc_acct->username } ) ) {
97     my $username = $svc_acct->username;
98     $username++;
99     $svc_acct->username($username);
100   }
101
102   use Tie::RefHash;
103   tie my %hash, 'Tie::RefHash',
104     $cust_pkg => [ $svc_acct ],
105   ;
106
107   my $error = $cust_main->insert( \%hash );
108   die $error if $error;
109
110 }
111
112 my $end = time;
113
114 my $sec = $end-$start;
115 $sec=1 if $sec==0;
116 my $persec = $onum / $sec;
117 print "$onum customers inserted in $sec seconds ($persec customers/sec)\n";
118
119 #---
120
121 sub usage {
122   die "Usage:\n\n  customer-faker [ -p payby ] [ -a agentnum ] user num_fakes\n";
123 }