4.x style
[freeside.git] / httemplate / edit / cust_pkg.cgi
index ea92ac0..4e4f4d2 100755 (executable)
-<%
-#<!-- $Id: cust_pkg.cgi,v 1.3 2001-10-26 10:24:56 ivan Exp $ -->
-
-use strict;
-use vars qw( $cgi %pkg %comment $custnum $p1 @cust_pkg 
-             $cust_main $agent $type_pkgs $count %remove_pkg $pkgparts );
-use CGI;
-use CGI::Carp qw(fatalsToBrowser);
-use FS::UID qw(cgisuidsetup);
-use FS::Record qw(qsearch qsearchs);
-use FS::CGI qw(header popurl);
-use FS::part_pkg;
-use FS::type_pkgs;
-
-$cgi = new CGI;
-&cgisuidsetup($cgi);
-
-%pkg = ();
-%comment = ();
-foreach (qsearch('part_pkg', {})) {
-  $pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg');
-  $comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment');
-}
+<%doc>
+
+    Bulk package Edit Page
+
+</%doc>
+<& /elements/header-cust_main.html,
+    view              => 'packages',
+    cust_main         => $cust_main,
+    include_selectize => 1,
+&>
+<% include('/elements/error.html') %>
+
+<script>
+
+  let locationnum = "<% $cust_main->ship_locationnum %>";
+
+  function location_changed(e) {
+    locationnum = $(e).val();
+    $('tr[data-locationnum]').find('input').prop('checked', false);
+    $('tr[data-locationnum]').each( function() {
+      let tr_el = $(this);
+      tr_el.css(
+        'display',
+        locationnum == tr_el.data('locationnum') ? 'table-row' : 'none'
+      );
+    });
+  }
 
-if ( $cgi->param('error') ) {
-  $custnum = $cgi->param('custnum');
-  %remove_pkg = map { $_ => 1 } $cgi->param('remove_pkg');
-} else {
-  my($query) = $cgi->keywords;
-  $query =~ /^(\d+)$/;
-  $custnum = $1;
-  undef %remove_pkg;
-}
+  function pkg_class_filter_onchange( selected ) {
+    if ( selected.length == 0 ) {
+      $('tr[data-classnum]').css('display', 'table-row');
+    } else {
+      $('tr[data-classnum]').each( function() {
+        let tr_el = $(this);
+        let classnum = tr_el.data('classnum');
+        let is_displayed = $.grep( selected, function( item ) {
+          return item == classnum;
+        });
+        let display = is_displayed.length ? 'table-row' : 'none';
+        tr_el.css( 'display', is_displayed.length ? 'table-row' : 'none' );
+      });
+    }
+  }
+
+  function confirm_form() {
+    let cust_pkg_removed = [];
+    let pkg_part_added   = [];
+
+    $('input[data-locationnum]:checked').each( function() {
+      let this_el = $(this);
+      cust_pkg_removed.push(
+        '#' + this_el.data('pkgnum') + ' ' + this_el.data('pkg')
+      );
+    });
+
+    $('input[data-pkgpart]').each( function() {
+      qty_el = $(this);
+      qty = qty_el.val();
 
-$p1 = popurl(1);
-print $cgi->header( @FS::CGI::header ), header("Add/Edit Packages", '');
-
-print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
-      "</FONT>"
-  if $cgi->param('error');
-
-print qq!<FORM ACTION="${p1}process/cust_pkg.cgi" METHOD=POST>!;
-
-print qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!;
-
-#current packages
-@cust_pkg = qsearch('cust_pkg',{ 'custnum' => $custnum, 'cancel' => '' } );
-
-if (@cust_pkg) {
-  print <<END;
-Current packages - select to remove (services are moved to a new package below)
-<BR><BR>
-END
-
-  my ($count) = 0 ;
-  print qq!<TABLE>! ;
-  foreach (@cust_pkg) {
-    print '<TR>' if $count == 0;
-    my($pkgnum,$pkgpart)=( $_->getfield('pkgnum'), $_->getfield('pkgpart') );
-    print qq!<TD><INPUT TYPE="checkbox" NAME="remove_pkg" VALUE="$pkgnum"!;
-    print " CHECKED" if $remove_pkg{$pkgnum};
-    print qq!>$pkgnum: $pkg{$pkgpart} - $comment{$pkgpart}</TD>\n!;
-    $count ++ ;
-    if ($count == 2)
-    {
-      $count = 0 ;
-      print qq!</TR>\n! ;
+      if ( qty < 1 ) { return; }
+
+      pkg_part_added.push( qty + ' x ' + qty_el.data('pkg') );
+    });
+
+    if ( cust_pkg_removed.length == 0 ) {
+      cust_pkg_removed.push('No Existing Packages Selected');
     }
+    if ( pkg_part_added.length == 0 ) {
+      pkg_part_added.push('No New Packages Selected');
+    }
+
+    console.log( cust_pkg_removed );
+    console.log( pkg_part_added );
+
+    confirm_html =
+      '<div style="margin: 1em;">'
+      + '<b><u>Removed Packages:</u></b><br>'
+      + cust_pkg_removed.join('<br>')
+      + '<br><br>'
+      + '<b><u>Replacement Packages:</u></b><br>'
+      + pkg_part_added.join('<br>')
+      + '<br><br>'
+      + '<input type="button" role="button" onclick="submit_form();" value="Confirm Order">'
+      + '</div>';
+
+      overlib(
+        confirm_html,
+        CAPTION, 'Confirm bulk change',
+        STICKY,
+        AUTOSTATUSCAP,
+        MIDX, 0,
+        MIDY, 0,
+        WIDTH, 300,
+        HEIGHT, 200,
+        TEXTSIZE, 3,
+        BGCOLOR, '#ff0000',
+        CGCOLOR, '#ff0000'
+      );
   }
-  print qq!</TABLE><BR><BR>!;
-}
 
-print <<END;
-Order new packages<BR><BR>
-END
-
-$cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
-$agent = qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
-
-$count = 0;
-$pkgparts = 0;
-print qq!<TABLE>!;
-foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
-  $pkgparts++;
-  my($pkgpart)=$type_pkgs->pkgpart;
-  print qq!<TR>! if ( $count == 0 );
-  my $value = $cgi->param("pkg$pkgpart") || 0;
-  print <<END;
-  <TD>
-  <INPUT TYPE="text" NAME="pkg$pkgpart" VALUE="$value" SIZE="2" MAXLENGTH="2">
-  $pkgpart: $pkg{$pkgpart} - $comment{$pkgpart}</TD>\n
-END
-  $count ++ ;
-  if ( $count == 2 ) {
-    print qq!</TR>\n! ;
-    $count = 0;
+  function submit_form() {
+    $('#formBulkEdit').submit();
   }
+</script>
+
+<form action="<% $fsurl %>edit/process/cust_pkg.cgi" method="POST" id="formBulkEdit">
+<input type="hidden" name="custnum" value="<% $custnum %>">
+<input type="hidden" name="action" value="bulk">
+
+<p style="margin-bottom: 2em;">
+  <label for="locationnum">Service Location</label>
+  <% include( '/elements/select-cust_location.html',
+      cust_main => $cust_main,
+      addnew    => 0,
+      onchange  => 'javascript:location_changed(this);',
+  ) %><br>
+  <span style="font-size: .8em; padding-left: 1em;">
+    Bulk-edit works with one customer location at a time
+  </span>
+</p>
+
+<table style="margin-bottom: 2em;">
+  <thead>
+    <tr style="background-color: #ccc;">
+      <th colspan="2" style="text-align: left;">
+        Pkg #
+      </th>
+      <th style="text-align: left;">
+        Current Packages<br>
+        <div style="font-size: .8em; padding-left: 1em; font-weight: normal;">
+          Selected packages are removed.<br>
+          Attached services are moved to the new package selected below
+        </span>
+      </th>
+    </tr>
+  </thead>
+  <tbody>
+%   for my $cust_pkg ( @cust_pkg ) {
+%     my $id = sprintf 'remove_cust_pkg[%s]', $cust_pkg->pkgnum;
+%     my $is_displayed = $cust_main->ship_locationnum == $cust_pkg->locationnum ? 1 : 0;
+      <tr data-locationnum="<% $cust_pkg->locationnum %>" data-pkg="<% $cust_pkg->pkg |h %>" style="display: <% $is_displayed ? 'table-row' : 'none' %>;">
+        <td>
+          <input type="checkbox"
+                 name="<% $id %>"
+                 id="<% $id %>"
+                 data-pkgnum="<% $cust_pkg->pkgnum %>"
+                 data-locationnum="<% $cust_pkg->locationnum %>"
+                 data-pkg="<% $part_pkg{ $cust_pkg->pkgpart }->pkg |h %>">
+        </td>
+        <td>#<% $cust_pkg->pkgnum %></td>
+        <td>
+          <label for="<% $id %>">
+            <% $part_pkg{ $cust_pkg->pkgpart }->pkg %><br>
+%           for my $cust_pkg_supp ( @{ $cust_pkg_supp_of{ $cust_pkg->pkgnum }} ) {
+              <span style="font-size: .8em; padding-left: 1em;">
+                <b>Supplementary:</b> <% $part_pkg{ $cust_pkg_supp->pkgpart }->pkg %>
+              </span>
+            </label>
+%         }
+        </td>
+      </tr>
+%   }
+  </tbody>
+</table>
+
+<table style="margin-bottom: 2em;">
+  <thead>
+    <tr style="background-color: #ccc;">
+      <th colspan="3">
+        <% include('/elements/selectize/select-multiple-pkg_class.html',
+            id       => 'filter_pkg_class',
+            onchange => 'pkg_class_filter_onchange',
+        ) %>
+      </th>
+    </tr>
+    <tr style="background-color: #ccc;">
+      <th>Qty</th>
+      <th>Class</th>
+      <th style="text-align: left;">Order New Packages</th>
+    </tr>
+  </thead>
+  <tbody>
+%   for my $part_pkg ( @part_pkg_enabled ) {
+%     my $id = sprintf 'qty_part_pkg[%s]', $part_pkg->pkgpart;
+      <tr data-classnum="<% $part_pkg->classnum %>">
+        <td>
+          <input type="text"
+                 name="<% $id %>"
+                 id="<% $id %>"
+                 value="0"
+                 size="2"
+                 data-pkgpart="<% $part_pkg->pkgpart %>"
+                 data-pkg="<% $part_pkg->pkg %>">
+          </td>
+        <td><% $part_pkg->classname || '(none)' %></td>
+        <td><% $part_pkg->pkg %></td>
+      </tr>
+%   }
+  </tbody>
+</table>
+
+<input type="button" role="button" value="Order" onclick="confirm_form();">
+
+</form>
+
+<% include('/elements/footer.html') %>
+
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Bulk change customer packages');
+
+my $custnum = $cgi->param('keywords') || $cgi->param('custnum');
+$custnum =~ /^\d+$/
+  or die "Invalid custnum($custnum)";
+
+my $cust_main = qsearchs( cust_main => { custnum => $custnum })
+  or die "Invalid custnum ($custnum)";
+
+my %part_pkg;
+my @part_pkg_enabled;
+
+for my $part_pkg ( qsearch( part_pkg => {} )) {
+  $part_pkg{ $part_pkg->pkgpart } = $part_pkg;
+  push @part_pkg_enabled, $part_pkg
+    unless $part_pkg->disabled;
 }
-print qq!</TABLE>!;
-
-unless ( $pkgparts ) {
-  my $p2 = popurl(2);
-  my $typenum = $agent->typenum;
-  my $agent_type = qsearchs( 'agent_type', { 'typenum' => $typenum } );
-  my $atype = $agent_type->atype;
-  print <<END;
-(No <a href="${p2}browse/part_pkg.cgi">package definitions</a>, or agent type
-<a href="${p2}edit/agent_type.cgi?$typenum">$atype</a> not allowed to purchase
-any packages.)
-END
+@part_pkg_enabled =
+  sort { $a->classname cmp $b->classname || $a->pkg cmp $b->pkg }
+  @part_pkg_enabled;
+
+my @cust_pkg;
+my %cust_pkg_supp_of;
+for my $cust_pkg (
+  qsearch(
+    cust_pkg => {
+      custnum  => $custnum,
+      cancel   => '',
+    }
+  )
+) {
+  if ( my $main_pkgnum = $cust_pkg->main_pkgnum ) {
+    $cust_pkg_supp_of{ $main_pkgnum } //= [];
+    push @{ $cust_pkg_supp_of{ $main_pkgnum } }, $cust_pkg;
+  } else {
+    $cust_pkg_supp_of{ $cust_pkg->pkgnum } //= [];
+    push @cust_pkg, $cust_pkg;
+  }
 }
-
-#submit
-print <<END;
-<P><INPUT TYPE="submit" VALUE="Order">
-    </FORM>
-  </BODY>
-</HTML>
-END
-%>
+</%init>