summaryrefslogtreecommitdiff
path: root/httemplate/search/cust_timespan.html
blob: 9c9a8261c03cb5b004e71db78f2cd7bb8e386c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<& elements/search.html,
                  'title'       => emt('Customer Timespan Report'),
                  'name'        => emt('customers'),
                  'query'       => {
                     select    	=> join(', ', @select),
                     table     	=> $table,
                     addl_from 	=> $addl_from,
                     extra_sql 	=> $extra_sql,
                   },
                  'count_query' => $count_query,
                  'header'      => \@header,
                  'fields'      => \@fields,
                  'links'       => \@links,
                  'disable_maxselect' => '1',

&>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Advanced customer search');

my $table = 'cust_main';
my $customer_link = [ "${p}view/cust_main.cgi?", 'custnum' ];
my $agent_sql;

## get agent numbers
if (length($cgi->param('agentnum'))) {
  $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
  $agent_sql = ' and cust_main.agentnum = ' . $1;
}


## get selected requested customers
my $cust_status = $cgi->param('cust_status');

my %type_sql_map = (
	'cancelled' => 'cancel_sql',
	'suspended' => 'susp_sql',
);

my $type_sql = $type_sql_map{$cust_status};
$type_sql = 'cancel_sql' unless $type_sql;

my @custs = qsearch({
	table     => 'cust_main',
	extra_sql => ' where ' . FS::cust_main->$type_sql,
});
my @customers = ('0');
foreach my $cust (@custs) { push @customers, $cust->custnum; }

## get locations
my $location_sub = sub {
  my $customer = shift;
  my @cust_location = qsearch({
	table => 'cust_location',
	select => 'cust_location.*',
	addl_from => ' LEFT JOIN cust_main ON (cust_location.locationnum = cust_main.bill_locationnum) ',
	extra_sql => ' WHERE cust_main.custnum = ' . $customer->custnum ,
  } );

  my $location;
  foreach my $loc (@cust_location) {
	$location .= $loc->address1 unless !$loc->address1;
	$location .= "<br>" . $loc->address2 unless !$loc->address2;
	$location .= "<br>" . $loc->city . ", " . $loc->state . ' ' . $loc->zip unless !$loc->city;
  }
  $location;
};

## get contact emails for customer
my $email_sub = sub {
  my $customer = shift;
  #can't because contactnum is in the wrong field #my @contact_email = $contact->contact_email;
  my @contact_email = qsearch({
	table => 'contact_email',
	addl_from => ' LEFT JOIN cust_contact ON (contact_email.contactnum = cust_contact.contactnum) LEFT JOIN cust_main ON (cust_contact.custnum = cust_main.custnum) ',
	extra_sql => ' WHERE cust_main.custnum = ' . $customer->custnum ,
  } );
  join('<br>', map $_->emailaddress, @contact_email);
};

## sql to get only canceled customers
my @status = ('active', 'on hold', 'suspended', 'not yet billed', 'one-time charge');
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";

## sql to get the first active date, last cancel date, and last reason.
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\'';

## set cancel date range here
my($beginning_date, $ending_date) = FS::UI::Web::parse_beginning_ending($cgi, '');
my $max_cancel_sql = "select max(cancel) from cust_pkg left join part_pkg using (pkgpart) where cust_pkg.custnum = cust_main.custnum and part_pkg.freq > \'0\'";
my $cancel_date = $max_cancel_sql.' and (('.$max_cancel_sql.') >= '.$beginning_date.' and ('.$max_cancel_sql.') <= '.$ending_date.')';

my $cancel_reason = 'select reason.reason from cust_pkg
	left join cust_pkg_reason on (cust_pkg.pkgnum = cust_pkg_reason.pkgnum)
	left join reason on (cust_pkg_reason.reasonnum = reason.reasonnum)
	where cust_pkg.custnum = cust_main.custnum and cust_pkg_reason.date = ('.$cancel_date.') limit 1
';

my @header = ( '#', 'Name', 'Address', 'Phone', 'Email', 'Active Date', 'Cancelled Date', 'Reason', 'Active Days' );
my @fields = ( 'custnum', 'custname', $location_sub, 'daytime', $email_sub, 'active_date', 'cancel_date', 'cancel_reason', 'active_days' );
my @links = ( $customer_link, $customer_link, '', '', '', '', '', '', '' );
my @select = (
	'cust_main.*',
	"(select to_char((select to_timestamp((".$active_date."))), 'Mon DD YYYY')) AS active_date",
	"(select to_char((select to_timestamp((".$cancel_date."))), 'Mon DD YYYY')) AS cancel_date",
	"($cancel_reason) AS cancel_reason",
	"(select date_part('day', (select to_timestamp((".$cancel_date."))) - (select to_timestamp((".$active_date."))) )) AS active_days",
	"CONCAT_WS(', ', last, first) AS custname",
);
my $addl_from = '
  LEFT JOIN cust_location ON (cust_main.bill_locationnum = cust_location.locationnum)
  LEFT JOIN cust_pkg ON (cust_main.custnum = cust_pkg.custnum)
  LEFT JOIN part_pkg ON (cust_pkg.pkgpart = part_pkg.pkgpart)
';
my $extra_sql = " WHERE (".$active_date.") IS NOT NULL AND (".$cancel_date.") IS NOT NULL AND cust_main.custnum IN ('" . join( "', '", @customers ). "') $agent_sql ";

## sql to get record count
my $count_query = 'select COUNT(*) from ' . $table . ' ' . $extra_sql;

</%init>