summaryrefslogtreecommitdiff
path: root/httemplate/search/cust_bill_void.html
blob: d99f759bd494c9f37c480666da6b2ab8c7f69f24 (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
<& elements/search.html,
                 'title'       => emt('Voided Invoice Search Results'),
                 'name'        => 'voided invoices',
                 'query'       => $sql_query,
                 'count_query' => $count_query,
                 'count_addl'  => $count_addl,
                 'redirect'    => $link,
                 'header'      => [ emt('Invoice #'),
                                    emt('Amount'),
                                    emt('Date'),
                                    FS::UI::Web::cust_header(),
                                    emt('Voided by'),
                                    emt('Date'),
                                    emt('Reason'),
                                  ],
                 'fields'      => [
                   'display_invnum',
                   sub { sprintf($money_char.'%.2f', shift->charged     ) },
                   sub { time2str('%b %d %Y', shift->_date ) },
                   \&FS::UI::Web::cust_fields,
                   'username',
                   sub { time2str('%b %d %Y', shift->void_date) },
                   'reason',
                 ],
                 'sort_fields' => [
                   'COALESCE( agent_invid, invnum )',
                   'charged',
                   '_date',
                   FS::UI::Web::cust_sort_fields(),
                   'username',
                   'void_date',
                   'reason',
                 ],
                 'align' => 'rrl'.FS::UI::Web::cust_aligns().'lll',
                 'links' => [
                   $link,
                   $link,
                   $link,
                   ( map { $_ ne 'Cust. Status' ? $clink : '' }
                         FS::UI::Web::cust_header()
                   ),
                   $link,
                   $link,
                 ],
                 'color' => [ 
                              '',
                              '',
                              '',
                              FS::UI::Web::cust_colors(),
                            ],
                 'style' => [ 
                              '',
                              '',
                              '',
                              FS::UI::Web::cust_styles(),
                            ],
&>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
  # or something else?

my $conf = new FS::Conf;
my $money_char = $conf->config('money_char') || '$';

my $addl_from =
  ' LEFT JOIN access_user ON (cust_bill_void.void_usernum = access_user.usernum) '.
   FS::UI::Web::join_cust_main('cust_bill_void');
#here is the agent virtualization
my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;

my( $count_query, $sql_query );
my $count_addl = '';
my %search;

if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {

  my $invnum_or_invid = "( invnum = $2 OR agent_invid = $2 )";
  my $where = "WHERE $invnum_or_invid AND $agentnums_sql";
  
  $count_query = "SELECT COUNT(*) FROM cust_bill_void $addl_from $where";

  $sql_query = {
    'table'     => 'cust_bill_void',
    'addl_from' => $addl_from,
    'hashref'   => {},
    'extra_sql' => $where,
  };

} else {

  #some false laziness w/cust_bill::re_X
  my $orderby = 'ORDER BY cust_bill_void._date';

  if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
    $search{'agentnum'} = $1;
  }

  if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
    $search{'refnum'} = $1;
  }

if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
    $search{'cust_classnum'} = [ $cgi->param('cust_classnum') ];
  }

  if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
    $search{'custnum'} = $1;
  }

  # begin/end/beginning/ending
  my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
  $search{'_date'} = [ $beginning, $ending ]
    unless $beginning == 0 && $ending == 4294967295;

  ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, 'void_');
  $search{'void_date'} = [ $beginning, $ending ]
    unless $beginning == 0 && $ending == 4294967295;

  if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
    $search{'invnum_min'} = $1;
  }
  if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
    $search{'invnum_max'} = $1;
  }
  #payby
  if ( $cgi->param('payby') ) {
    $search{'payby'} = [ $cgi->param('payby') ];
  }

  #amounts
  $search{'charged'} = [ FS::UI::Web::parse_lt_gt($cgi, 'charged') ];

  my $extra_sql = FS::cust_bill_void->search_sql_where( \%search );
  $extra_sql = "WHERE $extra_sql" if $extra_sql;

  unless ( $count_query ) {
    $count_query = 'SELECT COUNT(*), SUM(charged)';
    $count_addl = [ $money_char . '%.2f voided' ];
  }
  $count_query .=  " FROM cust_bill_void $addl_from $extra_sql";

  $sql_query = {
    'table'     => 'cust_bill_void',
    'addl_from' => $addl_from,
    'hashref'   => {},
    'select'    => join(', ',
                     'cust_bill_void.*',
                     #( map "cust_main.$_", qw(custnum last first company) ),
                     'cust_main.custnum as cust_main_custnum',
                     FS::UI::Web::cust_sql_fields(),
                     'access_user.username',
                   ),
    'extra_sql' => $extra_sql,
    'order_by'  => $orderby,
  };

}
my $link  = [ "${p}view/cust_bill_void.html?", 'invnum', ];
my $clink = sub {
  my $cust_bill = shift;
  $cust_bill->cust_main_custnum
    ? [ "${p}view/cust_main.cgi?", 'custnum' ]
    : '';
};

# really don't want the ability to print/spool/email voided invoices
</%init>