5 use FS::Record qw(qsearchs qsearch dbh);
7 use FS::cust_main_invoice;
11 getopts('c:'); # contact classname
13 $FS::UID::AutoCommit = 0;
19 my $class = qsearchs('contact_class', { classname => $opt{c} });
21 $class = FS::contact_class->new({ classname => $opt{c} });
22 $error = $class->insert;
25 $classnum = $class->classnum;
28 # Find all invoice destinations that are email addresses, except those where
29 # there is already a contact with that email address.
30 my @invoice_dests = qsearch({
31 select => 'cust_main_invoice.*',
32 table => 'cust_main_invoice',
33 hashref => { 'dest' => { op=>'!=', value=>'POST' } },
34 addl_from => ' LEFT JOIN contact_email ON
35 (cust_main_invoice.dest = contact_email.emailaddress)',
36 extra_sql => ' AND contact_email.contactnum IS NULL',
38 print "Found email destinations: ".scalar(@invoice_dests)."\n";
41 foreach my $invoice_dest (@invoice_dests) {
42 my $cust_main = $invoice_dest->cust_main
43 or next; #cust_main_invoice.custnum points to non-existant customer?
44 #will need to be fixed before v4 upgrade, but this can still run..
45 my $last = $cust_main->get('last');
46 my $first = $cust_main->get('first');
47 my $email = $invoice_dest->dest;
48 print "$first $last <$email>\n";
49 if (exists $email_used{$email}) {
50 print "-- in use by cust#$email_used{$email}\n";
53 $email_used{$email} = $cust_main->custnum;
55 my $contact = qsearchs('contact', {
56 'custnum' => $invoice_dest->custnum,
61 my $contact_email = FS::contact_email->new({
62 'contactnum' => $contact->contactnum,
63 'emailaddress' => $email
65 $error = $contact_email->insert;
66 die "inserting contact email: $error\n" if $error;
68 # use the 'emailaddress' param here so that send_reset_email will
70 $contact = FS::contact->new({
71 'custnum' => $invoice_dest->custnum,
72 'locationnum' => $cust_main->bill_locationnum,
75 'classnum' => $classnum,
76 'selfservice_access' => 'Y',
77 'emailaddress' => $email,
79 '_password_encoding' => '',
81 $error = $contact->insert;
82 die "inserting contact: $error\n" if $error;