RT#34237: installer scheduling [major refactor, many bugs fixed]
[freeside.git] / rt / share / html / Elements / CalendarSlotSchedule
1 <%ARGS>
2   $Date      => undef
3   @Tickets   => ()
4   $slots     => $default_slots
5   $sday      => undef
6   $tod_row   => undef
7   $timestep  => $default_timestep
8   @username  => ()
9   $LengthMin => $default_timestep
10   $custnum   => undef
11   $pkgnum    => undef
12   $RedirectToBasics => 0
13 </%ARGS>
14 % my $scheduling = ($custnum && $LengthMin) ? 1 : 0;
15 % foreach my $username ( @username ) {
16 %
17 %   my %schedule = UserDaySchedule( username => $username,
18 %                                   'date'   => $Date->strftime('%F'),
19 %                                   Tickets  => \@Tickets,
20 %                                 );
21 %
22 %   my $bgcolor = '#666666';
23 %   my $border = '1px solid #555555';
24 %   my $content = '';
25 %   my $selectable = 0; # can we schedule a new appointment
26 %   my $ticketid = 0;
27 %   my $draggable_length = 0;
28 %   my $droppable = 0; # can we reschedule an appointment here
29 %   my $cells = 0; # total cell count for appointment
30 %   my $offset = 0; # position of cell in appointment
31 %
32 %   #white out available times
33 %   foreach my $avail ( @{ $schedule{'avail'} } ) {
34 %     my( $start, $end ) = @$avail;
35 %     next if $start >= ($tod_row+$timestep) || $end <= $tod_row;
36 %     $bgcolor = '#FFFFFF';
37 %     $border = '1px solid #D7D7D7';
38 %     $selectable = 1
39 %       if $LengthMin <= $end - $tod_row  #the slot is long enough
40 %       && ! grep { $_ > $tod_row && $LengthMin > $_ - $tod_row }
41 %              map $_->[0], values %{ $schedule{'scheduled'} };
42 %   }
43 %
44 %   #block out / show / color code existing appointments
45 %   foreach my $id ( keys %{ $schedule{'scheduled'} } ) {
46 %
47 %     my( $starts, $due, $col, $t ) = @{ $schedule{'scheduled'}->{$id} };
48 %
49 %     # misleading loop--at most one id will pass this test
50 %     next if $starts >= ($tod_row+$timestep) || $due <= $tod_row;
51 %
52 %     $ticketid = $id;
53 %     $bgcolor = '#'.$col;
54 %     $border = '1px solid #D7D7D7';
55 %     # can't schedule a new appointment
56 %     $selectable = 0;
57 %     # but can reschedule a ticket overlapping its old slot (filtered by can_drop)
58 %     $droppable = 1 unless $scheduling;
59 %     $draggable_length = $due - $starts;
60 %     $cells = int( ($due-$starts) / $timestep );
61 %     $cells++ if ($due-$starts) % $timestep;
62 %
63 %     if ( $starts >= $tod_row ) { #first row
64 %
65 %       #false laziness w/misc/xmlhttp-ticket-update.html & CalendarDaySchedule
66 %       my %hash = $m->comp('/Ticket/Elements/Customers', Ticket => $t);
67 %       my @cust_main = values( %{$hash{cust_main}} );
68 %
69 %       #false laziness w/xmlhttp-ticket-update.html
70 %       $content .= FS::sched_avail::pretty_time($starts). '-'.
71 %                   FS::sched_avail::pretty_time($due).
72 %                   ': '. $cust_main[0]->_FreesideURILabel;
73 %                   #'install for custname XX miles away'; #XXX placeholder/more
74 %
75 %     } else {
76 %       $offset = int( ($tod_row - $starts) / $timestep );
77 %       $offset++ if ($tod_row - $starts) % $timestep;
78 %     }
79 %   }
80 %
81 %   my $td_id = 'td_'. $Date->epoch. '_'. $tod_row. '_'. $username;
82
83     <td style = "background-color: <% $bgcolor %>; border: <% $border %>"
84           ID="<% $td_id %>"
85         class = "<% ($selectable && $scheduling) ? 'weeklyselectable' : 'weekly' %>"
86 %#                 <%   $is_today     ? 'today'
87 %#                    : $is_yesterday ? 'yesterday'
88 %#                    : $is_aweekago  ? 'aweekago'
89 %#                    : ''
90 %#                 %>"
91 %     if ( $selectable ) {
92 %
93 %       # Scheduling a new appointment
94 %       if ( $scheduling ) {
95 %
96 %         #XXX for now, construct a ticket creation URL
97 %         # eventually, do much the same, but say "appointment made", show time
98 %         # and date, have # options to do things with it? etc.
99 %         # then redir back to customer/appointment view i guess
100 %
101 %         #abstraction is leaking like a sieve... linking back to freeside cust
102 %         # (XXX and eventually, package)
103 %         my $cust_main = qsearchs('cust_main', { custnum=>$custnum } )
104 %           or die "unknown custnum $custnum";
105 %
106 %         my $conf = new FS::Conf;
107 %         my $Queue = $conf->config('ticket_system-appointment-queueid')
108 %           or die "ticket_system-appointment-queueid configuration not set";
109 %
110 %         my $member = "freeside://freeside/cust_main/$custnum";
111 %
112 %         my $Starts = int($tod_row/60). ':'. sprintf('%02d',$tod_row%60). ':00';
113 %         my $Due    = int(($tod_row+$LengthMin)/60). ':'.
114 %                      sprintf('%02d',($tod_row+$LengthMin)%60). ':00';
115 %
116 %         my $url = $RT::WebPath. '/Ticket/Display.html?id=new'.
117 %                   "&Queue=$Queue".
118 %                   "&Owner=$username".
119 %                   '&Starts='. $Date->strftime('%F').'%20'. $Starts.
120 %                   '&Due='.    $Date->strftime('%F').'%20'. $Due.
121 %                   '&new-MemberOf='. $member. #XXX uri_escape?
122 %                   '&Status=new'.
123 %                   '&RedirectToBasics='.$RedirectToBasics;
124 %                   #'&Requestors='. #XXX Freeside customer requestor(s) (package?
125
126           onmouseover = "boxon(this);"
127           onmouseout  = "boxoff(this);"
128           title       = "<% 'Make appointment for '.
129                               FS::sched_avail::pretty_time($tod_row). '-'.
130                               FS::sched_avail::pretty_time($tod_row+$LengthMin)
131                         %>"
132           onclick     = "window.location.href = '<% $url %>'"
133 %
134 %       # If not scheduling, allow drag-and-drop rescheduling
135 %       } else {
136 %         $droppable = 1;
137 %       }
138 %
139 %     }
140     ><% $content |h %></td>
141     <SCRIPT TYPE="text/javascript">
142
143       var $cell_<% $td_id %> = $('#<% $td_id %>');
144       $cell_<% $td_id %>.data('username', "<% $username %>");
145       $cell_<% $td_id %>.data('starts',   <% $Date->epoch + $tod_row*60 %>);
146       $cell_<% $td_id %>.data('epoch',    <% $Date->epoch %>);
147       $cell_<% $td_id %>.data('tod_row',  <% $tod_row %>);
148
149 %     if ($selectable) {
150       set_schedulable_cell($cell_<% $td_id %>);
151 %     }
152
153 %     if ($ticketid) {
154       set_appointment_cell(
155         $cell_<% $td_id %>,
156         <% $ticketid |js_string %>,
157         <% $bgcolor |n,js_string %>,
158         <% $content |n,js_string %>,
159         <% $draggable_length * 60 %>,
160         <% $cells %>,
161         <% $offset %>
162       );
163 %     }
164 %     if ( $droppable ) {
165 %       if ( $draggable_length ) {
166       set_draggable_cell($cell_<% $td_id %>);
167 %       }
168       set_droppable_cell($cell_<% $td_id %>);
169 %     }
170
171     </SCRIPT>
172 % }
173 <%ONCE>
174 my $default_slots = RT->Config->Get('CalendarWeeklySlots') || 5;
175 my $default_timestep = RT->Config->Get('CalendarWeeklySizeMin') || 30; #1/2h
176 </%ONCE>
177 <%INIT>
178
179 use RTx::Schedule qw( UserDaySchedule );
180 use FS::sched_avail; #just for pretty_time
181
182 #what happened?  alas.  for constructing the ticket creation links
183 use FS::Record qw( qsearchs );
184 use FS::cust_main;
185
186 </%INIT>