MakeBooking Method

     RMakeBooking ARGS;CODE;COURSE;SLOT;TEETIME
                        ;COURSECODES;COURSES;INDEX
                        ;COURSEI;IDN;DATES;COMPS;IDATE
                        ;TEETIMES;GOLFERS;OLD;COMP;HOURS
                        ;MINUTES;NEAREST;TIME;NAMES;FREE
                        ;FREETIMES;I;J;DIFF
      :Access Public
      ⍝ If GimmeNearest is 0, tries for specified time          ⍝ If GimmeNearest is 1, gets nearest time     
      CODE TEETIME NEAREST3ARGS
      COURSECODES COURSES INDEX⎕FREAD GOLFID 1
      COURSEICOURSECODESCODE
      COURSE⎕NEW GolfCourse(CODE(COURSEICOURSES,⊂''))
      SLOT⎕NEW Slot TEETIME
      R⎕NEW Booking(0 COURSE SLOT'')
      :If COURSEI>⍴COURSECODES
          R.Message'Invalid course code'
          :Return
      :EndIf
      :If TEETIME.Now>TEETIME
          R.Message'Requested tee-time is in the past'
          :Return
      :EndIf
      :If TEETIME>TEETIME.Now.AddDays 30
          R.Message'Requested tee-time is more than 30
                                          days from now'
          :Return
      :EndIf
      IDN2 ⎕NQ'.' 'DateToIDN',TEETIME.(Year Month Day)
      DATES COMPS⎕FREAD GOLFID,COURSEIINDEX
      IDATEDATESIDN
      :If IDATE>⍴DATES
          TEETIMES(24 607 0)+10ׯ1+⍳1+8×6
          GOLFERS((TEETIMES),4)⍴⊂''llowed per tee time
          :If 0=OLD(DATES<2 ⎕NQ'.' 'DateToIDN',3⎕TS)/
                                                 ⍳⍴DATES
              COMP(TEETIMES GOLFERS)⎕FAPPEND GOLFID
              DATES,IDN
              COMPS,COMP
              (DATES COMPS)⎕FREPLACE GOLFID,COURSEIINDEX
          :Else
              DATES[OLD]IDN
              (TEETIMES GOLFERS)⎕FREPLACE GOLFID,
                                          COMPOLDCOMPS
              DATES COMPS ⎕FREPLACE GOLFID,COURSEIINDEX
          :EndIf
          :Else
          COMPIDATECOMPS
          TEETIMES GOLFERS⎕FREAD GOLFID COMP
      :EndIf
      HOURS MINUTESTEETIME.(Hour Minute)
      NAMES(3ARGS)~''
      TIME24 60HOURS MINUTES
      TIME10×⌊0.5+TIME÷10
      :If ~NEAREST
          ITEETIMESTIME
          :If I>⍴TEETIMES
          :OrIf (NAMES)>⊃,/+/0=⍴¨GOLFERS[I;]
              R.Message'Not available'
              :Return
          :EndIf
      :Else
          :If ~∨/FREE(NAMES)≤⊃,/+/0=⍴¨GOLFERS
              R.Message'Not available'
              :Return
          :EndIf
          FREETIMES(FREE×TEETIMES)+32767×~FREE
          DIFF|FREETIMES-TIME
          IDIFF⍳⌊/DIFF
      :EndIf
      J(⊃,/0=⍴¨GOLFERS[I;])/4
      GOLFERS[I;(NAMES)J]NAMES
      (TEETIMES GOLFERS)⎕FREPLACE GOLFID COMP
      TEETIMEDateTime.New TEETIME.(Year Month Day),
                                  324 60ITEETIMES
      SLOT.TimeTEETIME
      SLOT.Players(⊃,/0<⍴¨GOLFERS[I;])/GOLFERS[I;]
      R.(OK TeeTime)1 SLOT
    
    
:EndClass