fix FK upgrade for pkg_referral: remove records referencing non-existant customer...
[freeside.git] / httemplate / browse / reason.html
1 <% include( 'elements/browse.html',
2                  'title'       => ucfirst($classname) . ' Reasons',
3                  'menubar'     => [ ucfirst($classname).' Reason Types' =>
4                                       $p."browse/reason_type.html?class=$class"
5                                   ],
6                  'html_init'   => $html_init,
7                  'name'        => $classname . ' reasons',
8                  'disableable' => 1,
9                  'disabled_statuspos' => 3,
10                  'query'       => { 'table'     => 'reason',
11                                     'hashref'   => {},
12                                     'extra_sql' => $where_clause. 
13                                                    ' ORDER BY reason_type', 
14                                     'addl_from' => 'LEFT JOIN reason_type ON reason_type.typenum = reason.reason_type', 
15                                   },
16                  'count_query' => $count_query,
17                  'header'      => \@header,
18                  'fields'      => \@fields,
19                  'links'       => \@links,
20                  'align'       => $align,
21                  'html_form'   => qq!<FORM ACTION="${p}misc/reason_merge.html" METHOD="POST">!,
22                  'html_foot'   => $html_foot,
23              )
24 %>
25 <%init>
26
27 die "access denied"
28   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
29
30 $cgi->param('class') =~ /^(\w)$/ or die "illegal class";
31 my $class = $1;
32
33 my $classname = $FS::reason_type::class_name{$class};
34 my $classpurpose = $FS::reason_type::class_purpose{$class};
35
36 my $html_init = include('/elements/init_overlib.html').
37 ucfirst($classname).  " reasons $classpurpose.<BR><BR>".
38 qq!<A HREF="${p}edit/reason.html?class=$class">!.
39 "<I>Add a $classname reason</I></A><BR><BR>";
40
41 my $where_clause = " WHERE class='$class' ";
42
43 my $count_query = 'SELECT COUNT(*) FROM reason LEFT JOIN reason_type on ' .
44                 'reason_type.typenum = reason.reason_type ' . $where_clause;
45
46 my $link = [ $p."edit/reason.html?class=$class&reasonnum=", 'reasonnum' ];
47
48 my @header = (
49   '#',
50   ucfirst($classname) . ' reason type',
51   ucfirst($classname) . ' reason',
52 );
53
54 my @fields = (
55   'reasonnum',
56   sub { shift->reasontype->type },
57   'reason',
58 );
59
60 my @links = (
61   $link,
62   $link,
63   '',
64 );
65
66 my $align = 'rll';
67
68 if ( $class eq 'S' or $class eq 'C' ) {
69   push @header, 'Credit unused service';
70   push @fields,
71     sub {
72       my $reason = shift;
73       if ( $reason->unused_credit ) {
74         return '<SPAN STYLE="background-color:#00ff00">YES</SPAN>';
75       } else {
76         return '<SPAN STYLE="background-color:#ff0000">NO</SPAN>';
77       }
78     };
79   $align .= 'c';
80 }
81 if ( $class eq 'S' ) {
82   push @header, 'Suspension fee';
83   push @fields,
84     sub {
85       my $reason = shift;
86       my $feepart = $reason->feepart;
87       my ($href, $text, $detail);
88       if ( $feepart ) {
89         my $part_fee = FS::part_fee->by_key($feepart) or return '';
90         $text = $part_fee->itemdesc . ': ' . $part_fee->explanation;
91         $detail = $reason->fee_on_unsuspend ? 'unsuspension' : 'suspension';
92         if ( $reason->fee_hold ) {
93           $detail = "next bill after $detail";
94         }
95         $detail = "(on $detail)";
96         $href = $p."edit/part_fee.html?$feepart";
97       } else {
98         my $pkgpart = $reason->unsuspend_pkgpart;
99         my $part_pkg = FS::part_pkg->by_key($pkgpart) or return '';
100         $text = $part_pkg->pkg_comment;
101         $href = $p."edit/part_pkg.cgi?$pkgpart";
102         $detail = $reason->unsuspend_hold ?
103           '(on next bill after unsuspension)' : '(on unsuspension)';
104       }
105       return '' unless length($text);
106
107       $text = qq!<A HREF="$href">! . encode_entities($text) . "</A> ".
108               "<FONT SIZE=-1>$detail</FONT>";
109       $text .= '</FONT>';
110     }
111   ;
112   $align .= 'l';
113 }
114
115 # reason merge handling
116 push @header, '';
117 push @fields, sub {
118   my $reason = shift;
119   my $reasonnum = $reason->reasonnum;
120   qq!<INPUT TYPE="checkbox" NAME="reasonnum" VALUE="$reasonnum">!;
121 };
122 push @links, '';
123 $align .= 'l';
124 my $html_foot = include('/search/elements/checkbox-foot.html',
125   onclick  => include( '/elements/popup_link_onclick.html',
126                 js_action   => q!'! . "${p}misc/reason-merge.html?" . q!' + toCGIString()!,
127                 actionlabel => 'Merge reasons',
128               ),
129   label    => 'merge selected reasons',
130   minboxes => 2,
131 ) . '</FORM>';
132
133 </%init>