installers, RT#16584
authorIvan Kohler <ivan@freeside.biz>
Sat, 5 Apr 2014 23:46:29 +0000 (16:46 -0700)
committerIvan Kohler <ivan@freeside.biz>
Sat, 5 Apr 2014 23:46:29 +0000 (16:46 -0700)
FS/FS/sched_avail.pm
httemplate/browse/sched_item.html
httemplate/edit/process/sched_item.html [new file with mode: 0644]
httemplate/edit/sched_item.html [new file with mode: 0644]
httemplate/elements/select-time-from_to.html [new file with mode: 0644]
httemplate/elements/select-time.html [new file with mode: 0644]
httemplate/elements/tr-select-avail-time-from_to.html [new file with mode: 0644]

index 62b1a5d..245c69f 100644 (file)
@@ -111,6 +111,40 @@ sub check {
   $self->SUPER::check;
 }
 
+=item stime_pretty
+
+=item etime_pretty
+
+=cut
+
+sub stime_pretty { shift->_time_pretty('stime', @_); }
+sub etime_pretty { shift->_time_pretty('etime', @_); }
+
+sub _time_pretty {
+  my( $self, $field ) = @_;
+
+  pretty_time( $self->$field() );
+}
+
+#helper sub
+sub pretty_time {
+  my $t = shift;
+
+  return 'Midnight' if $t == 0 || $t == 1440;
+  return 'Noon'     if $t == 720;
+
+  my $h = int( $t / 60 );
+  my $m = $t % 60;
+
+  my $ap = 'AM';
+  if    ( $h == 0 || $h == 24 ) { $h = 12; }
+  elsif ( $h == 12 )           { $ap = 'PM'; }
+  elsif ( $h > 12 )            { $ap = 'PM'; $h -= 12; }
+
+  sprintf('%02d:%02d'." $ap", $h, $m);
+
+}
+
 =back
 
 =head1 BUGS
index 0943580..372d4fd 100644 (file)
@@ -1,11 +1,17 @@
 <& elements/browse.html,
-     'title'         => 'Installers', #PL($sched_item_class->classname),
+     'title'         => 'Installer schedules', #$sched_item_class->classname. ' schedules',
      'name_singular' => 'installer', #$sched_item_class->classname
      'query'         => { 'table' => 'sched_item' },
      'count_query'   => 'SELECT COUNT(*) FROM sched_item',
-     'header'        => [ 'Installer', ], #$sched_item_class->classname
-     'fields'        => [ 'name' ],
-     #'links'         => [ $link, ],
+     'header'        => [ 'Installer', #$sched_item_class->classname
+                          @dow,
+                          'Overrides',
+                        ],
+     'fields'        => [ 'name',
+                          ( map dow_factory($_), (0..6) ),
+                          $override_sub,
+                        ],
+     'links'         => [ $link, ],
      'disableable'        => 1,
      'disabled_statuspos' => 1,
 &>
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
+my $link = [ $p.'edit/sched_item.html?', 'itemnum' ];
+
+my @dow = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
+
+sub dow_factory {
+  my $wday = shift;
+
+  return sub {
+     my $sched_item = shift;
+
+     my @sched_avail = qsearch({
+                         'table'   => 'sched_avail',
+                         'hashref' => {
+                           'itemnum'       => $sched_item->itemnum,
+                           'wday'          => $wday, #thanks, closure
+                           'override_date' => '',
+                         },
+                         'order_by' => 'ORDER BY stime',
+                       });
+
+    return 'Not available' unless @sched_avail;
+
+    #data
+    # align bgcolor colspan rowspan style valign width
+    # link onclick size data_style   
+
+    [
+      map {
+            [ { data=> $_->stime_pretty },
+              { data=> '-', },
+              { data=> $_->etime_pretty },
+            ];
+          }
+        @sched_avail
+    ];
+
+  };
+
+}
+
+my $override_sub = sub {
+  my $sched_item = shift;
+
+   my @sched_avail = qsearch({
+                       'table'   => 'sched_avail',
+                       'hashref' => {
+                         'itemnum'       => $sched_item->itemnum,
+                         'override_date' => { op=>'!=', value=>'' },
+                       },
+                       'order_by' => 'ORDER BY override_date, stime',
+                     });
+
+  return 'None' unless @sched_avail;
+
+  #XXX print each override date (and day of week)
+
+};
+
 </%init>
diff --git a/httemplate/edit/process/sched_item.html b/httemplate/edit/process/sched_item.html
new file mode 100644 (file)
index 0000000..7731a35
--- /dev/null
@@ -0,0 +1,32 @@
+%if ( $error ) {
+%  $cgi->param('error', $error );
+<% $cgi->redirect(popurl(2). "sched_item.html?". $cgi->query_string ) %>
+%} else {
+<% $cgi->redirect(popurl(3). "browse/sched_item.html") %>
+%}
+<%init>
+
+#XXX more specific AccessRight for scheduling
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my $itemnum = $cgi->param('itemnum');
+
+my $sched_item = qsearchs('sched_item', { 'itemnum' => $itemnum })
+  or die "unknown itemnum $itemnum";
+
+my @sched_avail = ();
+foreach my $wday (0..6) {
+  if ( $cgi->param("sched_avail$wday") eq 'Y' ) {
+    push @sched_avail, new FS::sched_avail {
+      'itemnum' => $itemnum,
+      'wday'    => $wday,
+      'stime'   => $cgi->param("sched_avail$wday".'_stime'),
+      'etime'   => $cgi->param("sched_avail$wday".'_etime'),
+    };
+  }
+}
+
+my $error = $sched_item->replace_sched_avail(@sched_avail);
+
+</%init>
diff --git a/httemplate/edit/sched_item.html b/httemplate/edit/sched_item.html
new file mode 100644 (file)
index 0000000..7dfe268
--- /dev/null
@@ -0,0 +1,49 @@
+<& elements/edit.html,
+     'name_singular'   => 'Installer schedule', #$sched_item_class->classname. ' schedule',
+     'table'           => 'sched_item',
+     'viewall_dir'     => 'browse',
+     'no_pkey_display' => 1,
+     'labels'          => { 'name' => 'Name',
+                            map { ( "sched_avail$_" => $dow[$_] ) } (0..6),
+                          },
+     'fields'          => [
+                            { 'field' => 'name',
+                              'type'  => 'fixed',
+                            },
+                            ( map dow_factory($_), (0..6) ),
+                          ],
+     'edit_callback'   => $edit_callback,
+&>
+<%init>
+
+#XXX more specific AccessRight for scheduling
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my @dow = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
+
+sub dow_factory {
+  my $wday  = shift;
+
+  { 'field'     => 'sched_avail'. $wday,
+    'type'      => 'select-avail-time-from_to',
+    'prefix'    => 'sched_avail'. $wday. '_',
+  };
+
+}
+
+my $edit_callback = sub {
+  my( $cgi, $sched_item, $fields_listref, $opt_hashref ) = @_;
+  foreach my $sched_avail ( $sched_item->sched_avail ) {
+    if ( $sched_avail->override_date ) {
+      #XXX handle
+    } else {
+      my $param = 'sched_avail'. $sched_avail->wday;
+      $cgi->param($param, 'Y');
+      $cgi->param($param.'_stime', $sched_avail->stime);
+      $cgi->param($param.'_etime', $sched_avail->etime);
+    }
+  }
+};
+
+</%init>
diff --git a/httemplate/elements/select-time-from_to.html b/httemplate/elements/select-time-from_to.html
new file mode 100644 (file)
index 0000000..9d49b7a
--- /dev/null
@@ -0,0 +1,17 @@
+<& select-time.html,
+     %opt,
+     'field'            => $opt{'prefix'}. 'stime',
+     'selected_default' => 480, #8am... configure?
+     'curr_value'       => $cgi->param($opt{'prefix'}.'stime'),
+&>
+ - 
+<& select-time.html,
+     %opt,
+     'field'            => $opt{'prefix'}. 'etime',
+     'selected_default' => 1080, #6pm.. configure?
+     'curr_value'       => $cgi->param($opt{'prefix'}.'etime'),
+&>
+<%init>
+  my %opt = @_;
+
+</%init>
diff --git a/httemplate/elements/select-time.html b/httemplate/elements/select-time.html
new file mode 100644 (file)
index 0000000..6e22e1e
--- /dev/null
@@ -0,0 +1,49 @@
+% unless ( $opt{'js_only'} ) {
+    <SELECT NAME="<% $opt{'field'} %>" SIZE="1" <%$disabled%> <%$onchange%> >
+%     for ( my $t = 0; $t <=1440; $t += $increment ) {
+        <OPTION VALUE="<% $t %>"
+                <%    ( $curr_value eq '' && $t == $selected_default )
+                   || $curr_value == $t
+                     ? 'SELECTED' : ''
+                %>
+        ><% pretty_time($t) %>
+%     }
+    </SELECT>
+% }
+<%init>
+
+my %opt = @_;
+my $increment = $opt{increment} || 30;
+my $selected_default = $opt{selected_default};
+my $disabled = $opt{'disabled'} || '';
+
+my $curr_value = $opt{curr_value};
+
+my $onchange = '';
+if ( $opt{'onchange'} ) {
+  $onchange = $opt{'onchange'};
+  $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
+  $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
+                                        #callbacks should act the same
+  $onchange = 'onChange="'. $onchange. '"' unless $onchange =~ /^onChange=/i;
+}
+
+sub pretty_time {
+  my $t = shift;
+
+  return 'Midnight' if $t == 0 || $t == 1440;
+  return 'Noon'     if $t == 720;
+
+  my $h = int( $t / 60 );
+  my $m = $t % 60;
+
+  my $ap = 'AM';
+  if    ( $h == 0 || $h == 24 ) { $h = 12; }
+  elsif ( $h == 12 )           { $ap = 'PM'; }
+  elsif ( $h > 12 )            { $ap = 'PM'; $h -= 12; }
+
+  sprintf('%02d:%02d'." $ap", $h, $m);
+
+}
+
+</%init>
diff --git a/httemplate/elements/tr-select-avail-time-from_to.html b/httemplate/elements/tr-select-avail-time-from_to.html
new file mode 100644 (file)
index 0000000..b867388
--- /dev/null
@@ -0,0 +1,58 @@
+% unless ( $opt{'js_only'} ) {
+
+    <& tr-td-label.html, %opt &>
+
+      <TD <% $colspan %> <% $style %>>
+% }
+    <& checkbox.html,
+         %opt,
+         prefix     => '',
+         value      => 'Y',
+         curr_value => $curr_value,
+         onchange   => $opt{field}. '_changed',
+    &>
+
+    <& select-time-from_to.html,
+         %opt,
+         disabled => ($curr_value eq 'Y' ? '' : 'DISABLED' )
+    &>
+
+% unless ( $opt{'js_only'} ) {
+      </TD>
+    </TR>
+% }
+% unless ( $opt{'html_only'} || $opt{'js_only'} ) {
+    <SCRIPT TYPE="text/javascript">
+% }
+% unless ( $opt{'html_only'} ) {
+      function <% $opt{field} %>_changed(what) {
+
+        <% $opt{'onchange'} %>
+
+        var stime_el = what.form.<% $opt{field} %>_stime;
+        var etime_el = what.form.<% $opt{field} %>_etime;
+
+        if ( what.checked ) {
+          stime_el.disabled = false;
+          etime_el.disabled = false;
+        } else {
+          stime_el.disabled = true;
+          etime_el.disabled = true;
+        }
+
+      }  
+% }
+% unless ( $opt{'html_only'} || $opt{'js_only'} ) {
+    </SCRIPT>
+% }
+<%init>
+
+my %opt = @_;
+
+my $style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
+
+my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
+
+my $curr_value = $cgi->param($opt{'field'}); #wtf?
+
+</%init>