bulk DID orders/inventory enhancements, RT11291
authorlevinse <levinse>
Sun, 17 Apr 2011 03:27:42 +0000 (03:27 +0000)
committerlevinse <levinse>
Sun, 17 Apr 2011 03:27:42 +0000 (03:27 +0000)
FS/FS/AccessRight.pm
FS/FS/did_order.pm
httemplate/browse/did_order.html
httemplate/misc/did_order_confirmed.html
httemplate/misc/did_order_provision.html
httemplate/search/phone_avail.html
httemplate/view/cust_main/packages/services.html

index 09ec948..aa59e7d 100644 (file)
@@ -152,6 +152,7 @@ tie my %rights, 'Tie::IxHash',
   'Customer service rights' => [
     'View customer services', #NEW
     'Provision customer service',
+    'Bulk provision customer service',
     'Recharge customer service', #NEW
     'Unprovision customer service',
     'Change customer service', #NEWNEW
index b138192..c1b34c3 100644 (file)
@@ -186,6 +186,21 @@ sub cust_main {
   qsearchs('cust_main', { 'custnum' => $self->custnum } );
 }
 
+=item provisioned
+
+Returns the provisioned DIDs, if any, as phone_avail (see L<FS::phone_avail>) objects.
+
+=cut
+
+sub provisioned {
+  my $self = shift;
+  qsearch({ table   => 'phone_avail',
+              hashref => { 'ordernum' => $self->ordernum, },
+              select  => 'phone_avail.*',
+              extra_sql => ' and svcnum is not null ',
+         });
+}
+
 =back
 
 =head1 SEE ALSO
index 363a201..3da8cb1 100644 (file)
@@ -1,14 +1,10 @@
 <% include( 'elements/browse.html',
                  'title'       => 'Bulk DID Orders',
-                 'html_init'   => qq!<A HREF="${p}edit/did_order.html"><I>Add a bulk DID order</I></A><BR><BR>!,
+                 'html_init'   =>  $html_init,
                  'name'        => 'bulk DID orders',
                  'disableable' => 0,
-                 'query'       => { 'table'     => 'did_order',
-                                                   'addl_from' => 'left join did_vendor using (vendornum) ',
-                                    'hashref'   => {},
-                                    'order_by' => 'ORDER BY ordernum',
-                                  },
-                 'count_query' => 'SELECT COUNT(*) FROM did_order',
+                 'query'       => $query,
+                 'count_query' => 'SELECT COUNT(*) FROM did_order', # XXX: this is wrong!?
                  'header'      => [ '#', 'Vendor',' Vendor Order #', 
                                     'Submitted', 'Confirmed', 'Customer',
                                     'Received', 'Provision', 'Cancel',
@@ -20,7 +16,7 @@
                                     'vendorname', 
                                     'vendor_order_id', 
                                     sub { &$display_date(shift->submitted); },
-                                    sub {
+                                    sub { # Confirmed
                                         my $did_order = shift;
                                         my $ordernum = $did_order->ordernum;
                                         return &$display_date($did_order->confirmed) 
                                           }
                                         )
                                     }, 
-                                    sub {
+                                    sub { # Customer
                                         my $did_order = shift;
                                         my $cust_main = $did_order->cust_main;
                                         return "Stock" unless $cust_main;
                                         "<A HREF='${p}view/cust_main.cgi?".$cust_main->custnum."'>".$cust_main->name."</A>";
                                     },
-                                    sub { 
+                                    sub { # Received
                                         my $did_order = shift;
                                         my $ordernum = $did_order->ordernum;
-                                        return &$display_date($did_order->received) 
-                                            if $did_order->received;
-                                        "<A HREF='${p}misc/phone_avail-import.html?ordernum=$ordernum'>Upload Received</A>";
+                                        return "<A HREF='${p}misc/phone_avail-import.html?ordernum=$ordernum'>Upload Received</A>"
+                                            unless $did_order->received;
+                                        "<A HREF='${p}search/phone_avail.html?ordernum=$ordernum'>"
+                                                . &$display_date($did_order->received) . "</A>";
                                     },
-                                    sub {
+                                    sub { # Provision
                                         my $did_order = shift;
                                         my $ordernum = $did_order->ordernum;
-                                        my @provisioned = 
-                                            qsearch({ table   => 'phone_avail',
-                                                      hashref => { 'ordernum' => $ordernum, },
-                                                      select  => 'phone_avail.*',
-                                                      extra_sql => ' and svcnum is not null ',
-                                                    });
+                                        my @provisioned = $did_order->provisioned;
                                         return '' 
                                             unless $did_order->received 
                                                         && $did_order->custnum
@@ -69,7 +61,7 @@
                                           }
                                         )
                                     },
-                                    sub {
+                                    sub { # Cancel
                                         my $did_order = shift;
                                         return '' unless !$did_order->received;
                                         qq!<A HREF="javascript:areyousure('${p}misc/did_order_confirmed.html?action=cancel;ordernum=!
@@ -104,4 +96,29 @@ my $display_date = sub {
     time2str($date_format, $date);
 };
 
+my $html_init = qq!<A HREF="${p}edit/did_order.html">Add a bulk DID order</A>
+                    <BR><BR>!;
+
+my $query = { 
+    'table'      => 'did_order',
+    'hashref'    => {},
+    'addl_from' => 'left join did_vendor using (vendornum) ',
+    'order_by' => 'ORDER BY ordernum',
+            };
+$query->{'hashref'}->{'custnum'} = $1 if $cgi->param('custnum') =~ /^(\d+)$/;
+if ( $cgi->param('custrcvdunprov') ) {
+    $query->{'hashref'}->{'received'} = { 'op' => '>', 'value' => '0', };
+    $query->{'hashref'}->{'custnum'} = { 'op' => '>', 'value' => '0', };
+    $query->{'addl_from'} .= ' left join phone_avail using (ordernum) ';
+    $query->{'extra_sql'} .= ' and svcnum is null ';
+    $html_init .= qq!<A HREF="${p}browse/did_order.html">Browse all DID orders</A>!;
+}
+else {
+    $html_init .= qq!<A HREF="${p}browse/did_order.html?custrcvdunprov=1">
+                    Browse all non-stock orders with received unprovisioned DIDs
+                    </A>!;
+}
+
+$html_init .= "<BR><BR>";
+
 </%init>
index aaded9f..c0c4795 100644 (file)
@@ -8,7 +8,7 @@
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Import');
 
-$cgi->param('action') =~ /^(confirm|cancel)$/ or die 'illegal action';
+$cgi->param('action') =~ /^(confirm|cancel|provision)$/ or die 'illegal action';
 my $action = $1;
 my $success_msg = '';
 my $error = '';
@@ -45,4 +45,23 @@ elsif ( $action eq 'cancel' ) {
     $error = $did_order->delete;
     $js = "window.location.href = '${p}browse/did_order.html'";
 }
+elsif ( $action eq 'provision' ) {
+    my $sucess_msg = 'DID order provisioned';
+    $cgi->param('pkgnum_svcpart') =~ /^(\d+)_(\d+)$/ or die 'illegal pkgnum_svcpart';
+    my $pkgnum = $1;
+    my $svcpart = $2;
+    my @dids = qsearch( 'phone_avail', { ordernum => $ordernum } );
+    die "no DIDs on order" unless scalar(@dids);
+    foreach my $did ( @dids ) {
+        my $svc_phone = new FS::svc_phone({
+            pkgnum  => $pkgnum,
+            svcpart => $svcpart,
+            countrycode => 1,
+            phonenum    => $did->npa.$did->nxx.$did->station,
+        });
+        $error = $svc_phone->insert;
+        last if $error;
+    }
+}
+
 </%init>
index b0c7210..8241121 100644 (file)
         <TD>Order #</TD>
         <TD><% $ordernum %></TD>
     </TR>
+    <TR>    
+        <TD>Customer</TD>
+        <TD><% $cust_main->name %></TD>
+    <TR>
+        <TD>Package/Service</TD>
+        <TD>
+%           if ( !$avail ) {
+                No packages exist for this customer having at least <% scalar(@dids) %>
+                    unprovisioned DIDs, as required for this order.
+%           } else {
+           <SELECT NAME="pkgnum_svcpart">
+%           foreach my $pkgnum ( keys %cust_pkg_phone ) {
+%               my @svcpart = @{$cust_pkg_phone{$pkgnum}};
+%               foreach my $svcpart ( @svcpart ) {
+                    <OPTION value="<%"${pkgnum}_$svcpart"%>">
+                        <% $cust_pkg_label{$pkgnum} %> / <% $svc_label{$svcpart} %>
+                    </OPTION>
+%               }
+%           }
+           </SELECT>
+%           }
+       </TD>
+    </TR>
+%   if ( $avail ) {
     <TR>
         <TD COLSPAN="2"><INPUT TYPE="SUBMIT" value="Provision"></TD>
     </TR>
+%  }
 </TABLE>
 
 <%init>
@@ -31,5 +56,31 @@ die "No order $ordernum" unless $did_order;
 die "Order is not in received status and/or DIDs not assigned to a customer"
     unless $did_order->received && $did_order->custnum; 
 
+my $cust_main = qsearchs('cust_main', { custnum => $did_order->custnum } );
+die "invalid customer" unless $cust_main;
+
+my @pkgs = $cust_main->ncancelled_pkgs;
+die "no packages" unless scalar(@pkgs);
+
+my @dids = qsearch( 'phone_avail', { ordernum => $ordernum } );
+die "no DIDs on order" unless scalar(@dids);
+
+my (%cust_pkg_phone, %cust_pkg_label, %svc_label );
+
+foreach my $pkg ( @pkgs ) {
+    my @avail_part_svc = $pkg->available_part_svc;
+    my @svcpart;
+    foreach my $avail_part_svc ( @avail_part_svc ) {
+        if ($avail_part_svc->svcdb eq 'svc_phone'
+                  && $avail_part_svc->num_avail >= scalar(@dids)) {
+            push @svcpart, $avail_part_svc->svcpart;
+            $svc_label{$avail_part_svc->svcpart} = $avail_part_svc->svc;
+        }
+    }
+    $cust_pkg_phone{$pkg->pkgnum} = \@svcpart if scalar(@svcpart);
+    $cust_pkg_label{$pkg->pkgnum} = $pkg->part_pkg->pkg;
+}
+
+my $avail = keys(%cust_pkg_phone);
 
 </%init>
index 8dec7b2..1335379 100644 (file)
@@ -3,7 +3,7 @@
               'name_singular' => 'phone number',
               'query'         => {
                                    'table'   => 'phone_avail',
-                                   'hashref' => {},
+                                   'hashref' => $hashref,
                                    'select'  => join(', ',
                                        'phone_avail.*',
                                        'cust_main.custnum',
@@ -129,6 +129,9 @@ my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
 
 my $count_query = "SELECT COUNT(*) FROM phone_avail $search"; #$addl_from?
 
+my $hashref = {};
+$hashref->{'ordernum'} = $1 if $cgi->param('ordernum') =~ /^(\d+)$/;
+
 my $link_cust = sub {
   my $phone_avail = shift;
   if ( $phone_avail->svcnum ) {
index 1e636ad..a5a5552 100644 (file)
@@ -63,6 +63,10 @@ function clearhint_search_cust_svc(obj, str) {
       <TR>
         <TD COLSPAN=3 ALIGN="center" STYLE="padding-bottom:4px;padding-top:0px">
           <B><% svc_provision_link($cust_pkg, $part_svc, \%opt, $curuser) %></B>
+%        if ( $curuser->access_right('Bulk provision customer service') 
+%               && $part_svc->svcdb eq 'svc_phone' ) {
+            <BR><A HREF="<%$p%>browse/did_order.html?custnum=<%$cust_pkg->custnum%>">Browse Received DID Inventory</A> 
+%       }
         </TD>
       </TR>