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