backdate credits, RT#32320
[freeside.git] / httemplate / browse / cgp_rule.html
1 <% include('elements/browse.html',
2              'title'         => "Rules for $svc_label: $svc_value",
3              'name_singular' => 'rule',
4              'html_init'     => $html_init,
5              'query'         => { 'table'   => 'cgp_rule',
6                                   'hashref' => { 'svcnum' => $svcnum },
7                                   'order_by' => 'ORDER BY priority DESC',
8                                 },
9              'count_query'   => $count_query,
10              'header'        => [ 'Priority', 'Name', 'Conditions', 'Actions', '' ],
11              'fields'        => [ sub { shift->priority || 'Inactive'; },
12                                   'name',
13                                   $condition_sub,
14                                   $action_sub,
15                                   sub { 'Delete'; },
16                                 ],
17              #'align'
18              'links'         => [ $edit_sub, $edit_sub, '', '', $del_sub ],
19           )
20 %>
21 <%init>
22
23 $cgi->param('svcnum') =~ /^(\d+)$/ or die 'no svcnum';
24 my $svcnum = $1;
25
26 #agent virt so you can't do cross-agent communigate rules
27 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum })
28   or die 'unknown svcnum';
29 my $part_svc = $cust_svc->part_svc;
30
31 my $count_query = "SELECT COUNT(*) FROM cgp_rule WHERE svcnum = $svcnum";
32
33 my($svc_label, $svc_value, $svcdb) = $cust_svc->label;
34
35 my $view = FS::UI::Web::svc_url( 'm'        => $m,
36                                  'action'   => 'view',
37                                  'part_svc' => $part_svc, 
38                                  'svc'      => $cust_svc,
39                                );
40
41 my $html_init =
42   qq(<A HREF="$view">View this $svc_label</A><BR><BR>).
43   qq!<A HREF="${p}edit/cgp_rule.html?svcnum=$svcnum">Add new rule</A><BR>!;
44
45 if ( $part_svc->svcdb eq 'svc_domain' ) {
46
47   #XXX add areyousure javscript confirmation for adding these
48
49   foreach my $line ( FS::Conf->new->config('cgp_rule-domain_templates') ) {
50     $line =~ /^\s*(\d+)\s+(.+)\s*$/ or next;
51     my($t_svcnum, $t_name) = ( $1, $2 );
52     next if $t_svcnum == $svcnum;
53     $html_init .=
54       qq!<A HREF="${p}misc/clone-cgp_rule.html?clone=$t_svcnum;svcnum=$svcnum">!
55        ."Add $t_name rule</A><BR>";
56   }
57
58 }
59
60 $html_init .=
61   '<BR>'.
62   qq!
63   <SCRIPT>
64     function areyousure_delete(href) {
65      areyousure(href,"Are you sure you want to delete this rule?");
66     }
67     function areyousure(href,message) {
68      if (confirm(message) == true)
69        window.location.href = href;
70     }
71   </SCRIPT>
72 !;
73
74 my $condition_sub = sub {
75   my $cgp_rule = shift;
76
77   [ map {
78           [
79             { data  => $_->conditionname,
80               #align =>
81             },
82             { data  => $_->op,
83               align => 'center',
84             },
85             { data  => $_->params,
86               #align =>
87             },
88           ];
89         }
90         $cgp_rule->cgp_rule_condition
91   ];
92 };
93
94 my $action_sub = sub {
95   my $cgp_rule = shift;
96
97   [ map {
98           [
99             { data => $_->action },
100             #{ data => '<pre>'.$_->params.'</pre>' }, #gets very big.. limit to
101             { data => $_->params },                   # some actions?
102           ];
103         }
104         $cgp_rule->cgp_rule_action
105   ];
106 };
107
108 my $edit_sub = [ $p.'edit/cgp_rule.html?', 'rulenum' ];
109 my $del_sub = sub {
110   my $rulenum = shift->rulenum;
111   [ "javascript:areyousure_delete('${p}misc/delete-cgp_rule.html?$rulenum')", '' ];
112 };
113
114 </%init>