RT# 73964 - Added biling event action to send an email to phone nunber, and updated...
[freeside.git] / httemplate / misc / did_order_confirmed.html
1 <% include("/elements/header$popup.html", $header ) %>
2 <% include('/elements/error.html') %>
3 % unless ( $cgi->param('error') ) {
4   <SCRIPT TYPE="text/javascript">
5     <% $js %>
6   </SCRIPT>
7 % }
8   </BODY>
9 </HTML>
10 <%init>
11 die "access denied"
12   unless $FS::CurrentUser::CurrentUser->access_right('Import');
13
14 my $action = $1;
15 my $header = '';
16 my $popup = '';
17 my $js = 'topreload();';
18
19 $cgi->param('ordernum') =~ /^(\d+)$/ or die 'illegal ordernum';
20 my $ordernum = $1;
21 my $did_order = qsearchs( {
22   'table'     => 'did_order',
23   'hashref'   => { 'ordernum' => $ordernum },
24 } );
25 die "No order $ordernum" unless $did_order;
26
27 sub didprov {
28     my($pkgnum,$svcpart,$phonenum) = (shift,shift,shift);
29     my $svc_phone = new FS::svc_phone({
30         pkgnum  => $pkgnum,
31         svcpart => $svcpart,
32         countrycode => 1,
33         phonenum    => $phonenum,
34     });
35     $cgi->param('error',$svc_phone->insert);
36 }
37
38 my $action = $cgi->param('action');
39 if ( $action eq 'confirm' ) {
40     my $confirmed = '';
41     $header = 'DID order confirmed';
42     $confirmed = parse_datetime($cgi->param('confirmed'))
43         if $cgi->param('confirmed') && $cgi->param('confirmed') !~ /^\d+$/;
44     $confirmed = $1
45         if $cgi->param('confirmed') && $cgi->param('confirmed') =~ /^(\d+)$/;
46
47     die "invalid confirmation date" unless $confirmed;
48
49     $did_order->confirmed($confirmed);
50     $did_order->vendor_order_id($cgi->param('vendor_order_id'));
51     $cgi->param('error',$did_order->replace);
52     print $cgi->redirect(popurl(1). "did_order_confirm.html?". $cgi->query_string )
53          if $cgi->param('error');
54     $popup = '-popup';
55 }
56 elsif ( $action eq 'cancel' ) {
57     $header = 'DID order cancelled';
58     $cgi->param('error',$did_order->delete);
59     $js = "window.location.href = '${p}browse/did_order.html'";
60 }
61 elsif ( $action eq 'provision' ) {
62     # XXX: most of this should move into did_order.pm and become transaction-enabled
63
64     $header = 'DID order provisioned';
65     my @dids = qsearch( 'phone_avail', { ordernum => $ordernum } );
66     die "no DIDs on order" unless scalar(@dids);
67     my @params = $cgi->param;
68     foreach my $param ( @params ) {
69         next unless $param =~ /^custnum(\d+)_pkgnum_svcpart$/;
70         my $custnum = $1;
71         my $value = $cgi->param($param);
72         next unless $value =~ /^(\d+)_(\d+)$/;
73         my $pkgnum = $1;
74         my $svcpart = $2;
75         if ( $did_order->custnum && $did_order->custnum == $custnum ) {
76             # per-order custnum case
77
78             foreach my $did ( @dids ) {
79                 didprov($pkgnum,$svcpart,$did->npa.$did->nxx.$did->station);
80                 last if $cgi->param('error');
81             }
82         }
83         elsif(!$did_order->custnum) {  # multi-customer/stock case
84
85             # get each order item for this customer for this order
86             my @did_order_item = qsearch('did_order_item', {
87                                                 'ordernum' => $ordernum,
88                                                 'custnum' => $custnum,
89                                         });
90             foreach my $did_order_item ( @did_order_item ) {
91                 # get imported unprovisioned DIDs for this order matching this order item
92                 my $didsearch = {   'ordernum'  => $ordernum,
93                                     'svcnum'    => '',
94                                 };
95                 $didsearch->{'npa'} = $did_order_item->npa 
96                                                     if $did_order_item->npa;
97                 $didsearch->{'state'} = $did_order_item->state
98                                                     if $did_order_item->state;
99                 $didsearch->{'name'} = $did_order_item->rate_center->description 
100                                             if $did_order_item->rate_center;
101                 $didsearch->{'msanum'} = $did_order_item->msanum
102                                                     if $did_order_item->msanum;
103                 $didsearch->{'latanum'} = $did_order_item->latanum
104                                                     if $did_order_item->latanum;
105                 @dids = qsearch('phone_avail', $didsearch);
106                 my $limit = $did_order_item->quantity;
107                 foreach my $did ( @dids ) {
108                     didprov($pkgnum,$svcpart,$did->npa.$did->nxx.$did->station);
109                     $limit--;
110                     last if $cgi->param('error') || $limit == 0;
111                 }
112                 last if $cgi->param('error');
113             }
114         }
115     }
116     $popup = '-popup';
117 }
118 elsif ( $action eq 'merge' ) {
119     $header = 'DID order merged';
120     $cgi->param('target_ordernum') =~ /^(\d+)$/ or die 'illegal target_ordernum';
121     my $target_ordernum = $1;
122     my $target_did_order = qsearchs( {
123       'table'     => 'did_order',
124       'hashref'   => { 'ordernum' => $target_ordernum },
125     } );
126     die "No order $target_ordernum" unless $target_did_order;
127     $cgi->param('error',$target_did_order->merge($did_order));
128     $js = "window.location.href = '${p}browse/did_order.html'";
129 }
130 else { 
131     die "illegal action";
132 }
133
134 $header = 'Error' if $cgi->param('error');
135
136 </%init>