[freeside-commits] branch master updated. ac127e9cc71ed1cfebb159095dd59b3507e54e98

Ivan ivan at 420.am
Sat Apr 5 16:46:30 PDT 2014


The branch, master has been updated
       via  ac127e9cc71ed1cfebb159095dd59b3507e54e98 (commit)
       via  a57c537ff9ef2a57de3225fbd49772be4b81e74c (commit)
      from  7c0748c7e439cdbc14ced38c21e782f4669f9b15 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit ac127e9cc71ed1cfebb159095dd59b3507e54e98
Author: Ivan Kohler <ivan at freeside.biz>
Date:   Sat Apr 5 16:46:29 2014 -0700

    installers, RT#16584

diff --git a/FS/FS/sched_avail.pm b/FS/FS/sched_avail.pm
index 62b1a5d..245c69f 100644
--- a/FS/FS/sched_avail.pm
+++ b/FS/FS/sched_avail.pm
@@ -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
diff --git a/httemplate/browse/sched_item.html b/httemplate/browse/sched_item.html
index 0943580..372d4fd 100644
--- a/httemplate/browse/sched_item.html
+++ b/httemplate/browse/sched_item.html
@@ -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,
 &>
@@ -15,4 +21,62 @@
 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
index 0000000..7731a35
--- /dev/null
+++ b/httemplate/edit/process/sched_item.html
@@ -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
index 0000000..7dfe268
--- /dev/null
+++ b/httemplate/edit/sched_item.html
@@ -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
index 0000000..9d49b7a
--- /dev/null
+++ b/httemplate/elements/select-time-from_to.html
@@ -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
index 0000000..6e22e1e
--- /dev/null
+++ b/httemplate/elements/select-time.html
@@ -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
index 0000000..b867388
--- /dev/null
+++ b/httemplate/elements/tr-select-avail-time-from_to.html
@@ -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>

commit a57c537ff9ef2a57de3225fbd49772be4b81e74c
Author: Ivan Kohler <ivan at freeside.biz>
Date:   Sat Apr 5 16:46:14 2014 -0700

    installers, RT#16584

diff --git a/FS/FS/sched_item.pm b/FS/FS/sched_item.pm
index 5f55188..de792a3 100644
--- a/FS/FS/sched_item.pm
+++ b/FS/FS/sched_item.pm
@@ -2,7 +2,8 @@ package FS::sched_item;
 use base qw( FS::Record );
 
 use strict;
-#use FS::Record qw( qsearch qsearchs );
+use FS::Record qw( dbh ); # qsearch qsearchs );
+use FS::sched_avail;
 
 =head1 NAME
 
@@ -109,6 +110,43 @@ sub name {
   $access_user ? $access_user->name : $self->itemname;
 }
 
+=item replace_sched_avail SCHED_AVAIL, ...
+
+Replaces the existing availability schedule with the list of passed-in
+FS::sched_avail objects
+
+=cut
+
+sub replace_sched_avail {
+  my( $self, @new_sched_avail ) = @_;
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  foreach my $old_sched_avail ( $self->sched_avail ) {
+    my $error = $old_sched_avail->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  foreach my $new_sched_avail ( @new_sched_avail ) {
+    $new_sched_avail->itemnum( $self->itemnum );
+    my $error = $new_sched_avail->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  '';
+
+}
+
 =back
 
 =head1 BUGS

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/sched_avail.pm                               |   34 +++++++++
 FS/FS/sched_item.pm                                |   40 +++++++++++-
 httemplate/browse/sched_item.html                  |   72 ++++++++++++++++++-
 httemplate/edit/process/sched_item.html            |   32 +++++++++
 httemplate/edit/sched_item.html                    |   49 +++++++++++++
 httemplate/elements/select-time-from_to.html       |   17 +++++
 httemplate/elements/select-time.html               |   49 +++++++++++++
 .../elements/tr-select-avail-time-from_to.html     |   58 ++++++++++++++++
 8 files changed, 346 insertions(+), 5 deletions(-)
 create mode 100644 httemplate/edit/process/sched_item.html
 create mode 100644 httemplate/edit/sched_item.html
 create mode 100644 httemplate/elements/select-time-from_to.html
 create mode 100644 httemplate/elements/select-time.html
 create mode 100644 httemplate/elements/tr-select-avail-time-from_to.html




More information about the freeside-commits mailing list