1 <& elements/search.html,
2 'title' => emt('Customer Timespan Report'),
3 'name' => emt('customers'),
5 select => join(', ', @select),
7 addl_from => $addl_from,
8 extra_sql => $extra_sql,
10 'count_query' => $count_query,
19 unless $FS::CurrentUser::CurrentUser->access_right('Advanced customer search');
21 my $table = 'cust_main';
22 my $customer_link = [ "${p}view/cust_main.cgi?", 'custnum' ];
26 if (length($cgi->param('agentnum'))) {
27 $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
28 $agent_sql = ' and cust_main.agentnum = ' . $1;
32 ## get selected requested customers
33 my $cust_status = $cgi->param('cust_status');
36 'cancelled' => 'cancel_sql',
37 'suspended' => 'susp_sql',
40 my $type_sql = $type_sql_map{$cust_status};
41 $type_sql = 'cancel_sql' unless $type_sql;
45 extra_sql => ' where ' . FS::cust_main->$type_sql,
47 my @customers = ('0');
48 foreach my $cust (@custs) { push @customers, $cust->custnum; }
51 my $location_sub = sub {
53 my @cust_location = qsearch({
54 table => 'cust_location',
55 select => 'cust_location.*',
56 addl_from => ' LEFT JOIN cust_main ON (cust_location.locationnum = cust_main.bill_locationnum) ',
57 extra_sql => ' WHERE cust_main.custnum = ' . $customer->custnum ,
61 foreach my $loc (@cust_location) {
62 $location .= $loc->address1 unless !$loc->address1;
63 $location .= "<br>" . $loc->address2 unless !$loc->address2;
64 $location .= "<br>" . $loc->city . ", " . $loc->state . ' ' . $loc->zip unless !$loc->city;
69 ## get contact emails for customer
72 #can't because contactnum is in the wrong field #my @contact_email = $contact->contact_email;
73 my @contact_email = qsearch({
74 table => 'contact_email',
75 addl_from => ' LEFT JOIN cust_contact ON (contact_email.contactnum = cust_contact.contactnum) LEFT JOIN cust_main ON (cust_contact.custnum = cust_main.custnum) ',
76 extra_sql => ' WHERE cust_main.custnum = ' . $customer->custnum ,
78 join('<br>', map $_->emailaddress, @contact_email);
81 ## sql to get only canceled customers
82 my @status = ('active', 'on hold', 'suspended', 'not yet billed', 'one-time charge');
83 my $active_pkg_sql = 'select pkgnum from cust_pkg where cust_pkg.custnum = cust_main.custnum and ' . FS::cust_pkg->status_sql . " in ('".join( "', '", @status )."') limit 1";
85 ## sql to get the first active date, last cancel date, and last reason.
86 my $active_date = 'select min(setup) from cust_pkg left join part_pkg using (pkgpart) where cust_pkg.custnum = cust_main.custnum and part_pkg.freq > \'0\'';
87 my $cancel_date = 'select max(cancel) from cust_pkg where cust_pkg.custnum = cust_main.custnum';
88 my $cancel_reason = 'select reason.reason from cust_pkg
89 left join cust_pkg_reason on (cust_pkg.pkgnum = cust_pkg_reason.pkgnum)
90 left join reason on (cust_pkg_reason.reasonnum = reason.reasonnum)
91 where cust_pkg.custnum = cust_main.custnum and cust_pkg_reason.date = ('.$cancel_date.') limit 1
94 my @header = ( '#', 'Name', 'Address', 'Phone', 'Email', 'Active Date', 'Cancelled Date', 'Reason', 'Active Days' );
95 my @fields = ( 'custnum', 'custname', $location_sub, 'daytime', $email_sub, 'active_date', 'cancel_date', 'cancel_reason', 'active_days' );
96 my @links = ( $customer_link, $customer_link, '', '', '', '', '', '', '' );
101 "(select to_char((select to_timestamp((".$active_date."))), 'Mon DD YYYY')) AS active_date",
102 "(select to_char((select to_timestamp((".$cancel_date."))), 'Mon DD YYYY')) AS cancel_date",
103 "($cancel_reason) AS cancel_reason",
104 "(select date_part('day', (select to_timestamp((".$cancel_date."))) - (select to_timestamp((".$active_date."))) )) AS active_days",
105 "CONCAT_WS(', ', last, first) AS custname",
108 LEFT JOIN cust_location ON (cust_main.bill_locationnum = cust_location.locationnum)
109 LEFT JOIN cust_pkg ON (cust_main.custnum = cust_pkg.custnum)
110 LEFT JOIN part_pkg ON (cust_pkg.pkgpart = part_pkg.pkgpart)
112 my $extra_sql = " WHERE (".$active_date.") IS NOT NULL AND (".$cancel_date.") IS NOT NULL AND cust_main.custnum IN ('" . join( "', '", @customers ). "') $agent_sql ";
114 ## sql to get record count
115 my $count_query = 'select COUNT(*) from ' . $table . ' ' . $extra_sql;