summaryrefslogtreecommitdiff
path: root/httemplate/search/cust_pkg_discount.html
blob: ab6ad2bd07eb9e1062472250ed3af567db7c56cb (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<& elements/search.html,
                  'title'       => 'Package discounts', 
                  'name'        => 'discounts',
                  'query'       => $query,
                  'count_query' => $count_query,
                  #'redirect'    => $link,
                  'header'      => [ 'Status',
                                     'Discount',
                                     'Class',
                                     'Months used',
                                     'Employee',
                                     'Package',
                                     FS::UI::Web::cust_header(
                                     #  $cgi->param('cust_fields')
                                     ),
                                   ],
                  'fields'      => [
                                     sub { ucfirst( shift->status ) },
                                     sub { shift->discount->description },
                                     sub { shift->discount->classname },
                                     $months_used_sub,
                                     'otaker',
                                     'pkg',
                                     \&FS::UI::Web::cust_fields,
                                   ],
                  'links'       => [
                                     '',
                                     '',
                                     '',
                                     '',
                                     '',
                                     '',
                                     ( map { $_ ne 'Cust. Status' ? $clink : ''}
                                           FS::UI::Web::cust_header()
                                     ),
                                   ],
                  'align'       => 'clcrll'. FS::UI::Web::cust_aligns(),
                  'color'       => [ 
                                     '',
                                     '',
                                     '',
                                     '',
                                     '',
                                     '',
                                     FS::UI::Web::cust_colors(),
                                   ],
                 'style'        => [ 
                                     '',
                                     '',
                                     '',
                                     '',
                                     '',
                                     '',
                                     FS::UI::Web::cust_styles(),
                                   ],
           
&>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');

#my $conf = new FS::Conf;

#here is the agent virtualization
my $agentnums_sql =
  $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );

my @where = ( $agentnums_sql );

#status
if ( $cgi->param('status') eq 'active' ) {
  push @where, "     (    cust_pkg_discount.disabled IS NULL
                       OR cust_pkg_discount.disabled != 'Y'  )
                 AND ( months IS NULL OR months_used < months ) ";
                       #XXX also end date
} elsif ( $cgi->param('status') eq 'expired' ) {
  push @where, "    (      cust_pkg_discount.disabled IS NOT NULL
                      AND  cust_pkg_discount.disabled = 'Y'       )
                 OR ( months IS NOT NULL AND months_used >= months )
               ";     #XXX also end date
}

#classnum
#false laziness w/cust_pkg.pm::search
if ( grep { $_ eq 'classnum' } $cgi->param ) {

#  my @classnum = ();
#  if ( ref($params->{'classnum'}) ) {
#
#    if ( ref($params->{'classnum'}) eq 'HASH' ) {
#      @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
#    } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
#      @classnum = @{ $params->{'classnum'} };
#    } else {
#      die 'unhandled classnum ref '. $params->{'classnum'};
#    }
#
#
#  } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
#    @classnum = ( $1 );
#  }
#
#  if ( @classnum ) {

   if ( $cgi->param('classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
    my @classnum = ( $1 );

    my @c_where = ();
    my @nums = grep $_, @classnum;
    push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
    my $null = scalar( grep { $_ eq '' } @classnum );
    push @c_where, 'discount.classnum IS NULL' if $null;

    if ( scalar(@c_where) == 1 ) {
      push @where, @c_where;
    } elsif ( @c_where ) {
      push @where, ' ( '. join(' OR ', @c_where). ' ) ';
    }

  }

}

#usernum
if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
  push @where, "cust_pkg_discount.usernum = $1";
}

#agent
if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
  push @where, "cust_main.agentnum = $1";
}

my $count_query = "SELECT COUNT(*), SUM(amount)";

my $join = ' LEFT JOIN discount  USING ( discountnum )
             LEFT JOIN cust_pkg  USING ( pkgnum )
             LEFT JOIN part_pkg  USING ( pkgpart ) '.
             FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');

my $where = ' WHERE '. join(' AND ', @where);

$count_query .= " FROM cust_pkg_discount $join $where";

my @select = (
               'cust_pkg_discount.*',
               'part_pkg.pkg',
             );
push @select, 'cust_main.custnum',
              FS::UI::Web::cust_sql_fields();

my $query = {
  'table'     => 'cust_pkg_discount',
  'addl_from' => $join,
  'hashref'   => {},
  'select'    => join(', ', @select ),
  'extra_sql' => $where,
  'order_by'  => 'ORDER BY pkgdiscountnum',
};

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

my $conf = new FS::Conf;

my $months_used_sub = sub {
  my $cust_pkg_discount = shift;
  return 'Setup only' if $cust_pkg_discount->setuprecur eq 'setup';
  return sprintf('%.2f', $cust_pkg_discount->months_used);
};
</%init>