invoice_sections_with_taxes per-agent, RT#79636
[freeside.git] / httemplate / search / cust_timespan.html
1 <& elements/search.html,
2                   'title'       => emt('Customer Timespan Report'),
3                   'name'        => emt('customers'),
4                   'query'       => {
5                      select     => join(', ', @select),
6                      table      => $table,
7                      addl_from  => $addl_from,
8                      extra_sql  => $extra_sql,
9                    },
10                   'count_query' => $count_query,
11                   'header'      => \@header,
12                   'fields'      => \@fields,
13                   'links'       => \@links,
14
15 &>
16 <%init>
17
18 die "access denied"
19   unless $FS::CurrentUser::CurrentUser->access_right('Advanced customer search');
20
21 my $table = 'cust_main';
22 my $customer_link = [ "${p}view/cust_main.cgi?", 'custnum' ];
23 my $agent_sql;
24
25 ## get agent numbers
26 if (length($cgi->param('agentnum'))) {
27   $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
28   $agent_sql = ' and cust_main.agentnum = ' . $1;
29 }
30
31
32 ## get selected requested customers
33 my $cust_status = $cgi->param('cust_status');
34
35 my %type_sql_map = (
36         'cancelled' => 'cancel_sql',
37         'suspended' => 'susp_sql',
38 );
39
40 my $type_sql = $type_sql_map{$cust_status};
41 $type_sql = 'cancel_sql' unless $type_sql;
42
43 my @custs = qsearch({
44         table     => 'cust_main',
45         extra_sql => ' where ' . FS::cust_main->$type_sql,
46 });
47 my @customers = ('0');
48 foreach my $cust (@custs) { push @customers, $cust->custnum; }
49
50 ## get locations
51 my $location_sub = sub {
52   my $customer = shift;
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 ,
58   } );
59
60   my $location;
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;
65   }
66   $location;
67 };
68
69 ## get contact emails for customer
70 my $email_sub = sub {
71   my $customer = shift;
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 ,
77   } );
78   join('<br>', map $_->emailaddress, @contact_email);
79 };
80
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";
84
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.')
92 ';
93
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, '', '', '', '', '', '', '' );
97 my @select = (
98         'cust_main.*',
99         'cust_location.*',
100         'part_pkg.*',
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",
106 );
107 my $addl_from = '
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)
111 ';
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 ";
113
114 ## sql to get record count
115 my $count_query = 'select COUNT(*) from ' . $table . ' ' . $extra_sql;
116
117 </%init>