add conditions for customer cancelled packages, RT#42043
[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 $label = '';
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 %   my $maxstarts = 0;
46 %   foreach my $id ( keys %{ $schedule{'scheduled'} } ) {
47 %
48 %     my( $starts, $due, $col, $t ) = @{ $schedule{'scheduled'}->{$id} };
49 %
50 %     # misleading loop--at most one id should pass this test
51 %     next if $starts >= ($tod_row+$timestep) || $due <= $tod_row;
52 %
53 %     # but, if for any reason a scheduling conflict occurs, 
54 %     # use the later starting one to minimize UI conflicts--
55 %     #   not to imply that this scenario has been tested or should ever happen!!!
56 %     next if $starts < $maxstarts;
57 %     $maxstarts = $starts;
58 %
59 %     $ticketid = $id;
60 %     $bgcolor = '#'.$col;
61 %     $border = '1px solid #D7D7D7';
62 %     # can't schedule a new appointment
63 %     $selectable = 0;
64 %     # but can reschedule a ticket overlapping its old slot (filtered by can_drop)
65 %     $droppable = 1 unless $scheduling;
66 %     $draggable_length = $due - $starts;
67 %     $cells = int( ($due-$starts) / $timestep );
68 %     $cells++ if ($due-$starts) % $timestep;
69 %
70 %     #false laziness w/misc/xmlhttp-ticket-update.html & CalendarDaySchedule
71 %     my %hash = $m->comp('/Ticket/Elements/Customers', Ticket => $t);
72 %     my @cust_main = values( %{$hash{cust_main}} );
73 %
74 %     #false laziness w/xmlhttp-ticket-update.html
75 %     $label .= FS::sched_avail::pretty_time($starts). '-'.
76 %               FS::sched_avail::pretty_time($due).
77 %               ': '. $cust_main[0]->_FreesideURILabel;
78 %               #'install for custname XX miles away'; #XXX placeholder/more
79 %
80 %     $offset = int( ($tod_row - $starts) / $timestep );
81 %     $offset++ if ($tod_row - $starts) % $timestep;
82 %   }
83 %
84 %   my $td_id = 'td_'. $Date->epoch. '_'. $tod_row. '_'. $username;
85
86     <td style = "background-color: <% $bgcolor %>; border: <% $border %>"
87           ID="<% $td_id %>"
88         class = "<% ($selectable && $scheduling) ? 'weeklyselectable' : 'weekly' %>"
89 %#                 <%   $is_today     ? 'today'
90 %#                    : $is_yesterday ? 'yesterday'
91 %#                    : $is_aweekago  ? 'aweekago'
92 %#                    : ''
93 %#                 %>"
94 %     if ( $selectable ) {
95 %
96 %       # Scheduling a new appointment
97 %       if ( $scheduling ) {
98 %
99 %         #XXX for now, construct a ticket creation URL
100 %         # eventually, do much the same, but say "appointment made", show time
101 %         # and date, have # options to do things with it? etc.
102 %         # then redir back to customer/appointment view i guess
103 %
104 %         #abstraction is leaking like a sieve... linking back to freeside cust
105 %         # (XXX and eventually, package)
106 %         my $cust_main = qsearchs('cust_main', { custnum=>$custnum } )
107 %           or die "unknown custnum $custnum";
108 %
109 %         my $conf = new FS::Conf;
110 %         my $Queue = $conf->config('ticket_system-appointment-queueid')
111 %           or die "ticket_system-appointment-queueid configuration not set";
112 %
113 %         my $member = "freeside://freeside/cust_main/$custnum";
114 %
115 %         my $Starts = int($tod_row/60). ':'. sprintf('%02d',$tod_row%60). ':00';
116 %         my $Due    = int(($tod_row+$LengthMin)/60). ':'.
117 %                      sprintf('%02d',($tod_row+$LengthMin)%60). ':00';
118 %
119 %         my $url = $RT::WebPath. '/Ticket/Display.html?id=new'.
120 %                   "&Queue=$Queue".
121 %                   "&Owner=$username".
122 %                   '&Starts='. $Date->strftime('%F').'%20'. $Starts.
123 %                   '&Due='.    $Date->strftime('%F').'%20'. $Due.
124 %                   '&new-MemberOf='. $member. #XXX uri_escape?
125 %                   '&Status=new'.
126 %                   '&RedirectToBasics='.$RedirectToBasics;
127 %                   #'&Requestors='. #XXX Freeside customer requestor(s) (package?
128
129           onmouseover = "boxon(this);"
130           onmouseout  = "boxoff(this);"
131           title       = "<% 'Make appointment for '.
132                               FS::sched_avail::pretty_time($tod_row). '-'.
133                               FS::sched_avail::pretty_time($tod_row+$LengthMin)
134                         %>"
135           onclick     = "window.location.href = '<% $url %>'"
136 %
137 %       # If not scheduling, allow drag-and-drop rescheduling
138 %       } else {
139 %         $droppable = 1;
140 %       }
141 %
142 %     }
143     ></td>
144     <SCRIPT TYPE="text/javascript">
145
146       var $cell_<% $td_id %> = $('#<% $td_id %>');
147       $cell_<% $td_id %>.data('username', "<% $username %>");
148       $cell_<% $td_id %>.data('starts',   <% $Date->epoch + $tod_row*60 %>);
149       $cell_<% $td_id %>.data('epoch',    <% $Date->epoch %>);
150       $cell_<% $td_id %>.data('tod_row',  <% $tod_row %>);
151
152 %     if ($selectable) {
153       set_schedulable_cell($cell_<% $td_id %>);
154 %     }
155
156 %     if ($ticketid) {
157       set_appointment_cell(
158         $cell_<% $td_id %>,
159         <% $ticketid |js_string %>,
160         <% $bgcolor |n,js_string %>,
161         <% $label |n,js_string %>,
162         <% $draggable_length * 60 %>,
163         <% $cells %>,
164         <% $offset %>
165       );
166 %     }
167 %     if ( $droppable ) {
168 %       if ( $draggable_length ) {
169       set_draggable_cell($cell_<% $td_id %>);
170 %       }
171       set_droppable_cell($cell_<% $td_id %>);
172 %     }
173
174     </SCRIPT>
175 % }
176 <%ONCE>
177 my $default_slots = RT->Config->Get('CalendarWeeklySlots') || 5;
178 my $default_timestep = RT->Config->Get('CalendarWeeklySizeMin') || 30; #1/2h
179 </%ONCE>
180 <%INIT>
181
182 use RTx::Schedule qw( UserDaySchedule );
183 use FS::sched_avail; #just for pretty_time
184
185 #what happened?  alas.  for constructing the ticket creation links
186 use FS::Record qw( qsearchs );
187 use FS::cust_main;
188
189 </%INIT>