def saveAttendance(request):    
    alpha = request.user.username.split('m')
    alpha = alpha[1]
    cMid = Mid.objects.get(alpha=alpha)
    cCompany = cMid.company
    cPlatoon = cMid.platoon
    
    #List of current mid's billets
    lBillets = Billet.objects.filter(mid=cMid)
    
    flagPLTS = False
    for p in lBillets :
        if p.billet == "PLTS" and p.current :
            flagPLTS = True

    if not flagPLTS :
        return HttpResponseRedirect('/')
    #End of second check
    
    lMids = Mid.objects.filter(company = cCompany).filter(platoon = cPlatoon)
    
    #Safety feature, makes sure we POST data to this view
    if request.method != "POST" :
        return HttpResponseRedirect('/')
    
    cEvent = Event.objects.get(id = request.POST['event'])
    
    for p in lMids :
        cAttendance = Attendance(mid = p,
                                 event = cEvent,
                                 status = request.POST[p.alpha+'A']                                
                                 )
        cAttendance.save()
        
    if cPlatoon == "1":
        cEvent.platoonOneSubmitted = True
    elif cPlatoon == "2":
        cEvent.platoonTwoSubmitted = True
    elif cPlatoon == "3":
        cEvent.platoonThreeSubmitted = True
    elif cPlatoon == "4":
        cEvent.platoonFourSubmitted = True
    
    if cEvent.platoonOneSubmitted and cEvent.platoonTwoSubmitted and cEvent.platoonThreeSubmitted and cEvent.platoonFourSubmitted :
        cEvent.companyComplete = True
    
    cEvent.save()

    return HttpResponseRedirect(reverse('accountability:enterAttendance'))
def makeDay(request):
    alpha = request.user.username.split('m')
    alpha = alpha[1]
    cMid = Mid.objects.get(alpha=alpha)
    cCompany = cMid.company  
    
    cEvent = Event(dateTime = datetime.combine(date.today(), time(07, 00, 00)),
                   type = "MMF",
                   location = "Company Area",
                   platoonOneSubmitted = False,
                   platoonTwoSubmitted = False,
                   platoonThreeSubmitted = False,
                   platoonFourSubmitted = False,
                   companyComplete = False,
                   company = cCompany
                   )
    cEvent.save()
    
    cEvent = Event(dateTime = datetime.combine(date.today(), time(12, 05, 00)),
                   type = "NMF",
                   location = "Tecumseh Court",
                   platoonOneSubmitted = False,
                   platoonTwoSubmitted = False,
                   platoonThreeSubmitted = False,
                   platoonFourSubmitted = False,
                   companyComplete = False,
                   company = cCompany
                   )
    cEvent.save()
    
    cEvent = Event(dateTime = datetime.combine(date.today(), time(18, 30, 00)),
                   type = "EMF",
                   location = "Company Area",
                   platoonOneSubmitted = False,
                   platoonTwoSubmitted = False,
                   platoonThreeSubmitted = False,
                   platoonFourSubmitted = False,
                   companyComplete = False,
                   company = cCompany
                   )
    cEvent.save()
    
    cEvent = Event(dateTime = datetime.combine(date.today(), time(23, 55, 00)),
                   type = "TAP",
                   location = "Company Area",
                   platoonOneSubmitted = True,
                   platoonTwoSubmitted = True,
                   platoonThreeSubmitted = True,
                   platoonFourSubmitted = True,
                   companyComplete = False,
                   company = cCompany
                   )
    cEvent.save()
    
    cReport = Zero8(offgoingCDO = cMid,
                    oncomingCDO = cMid,
                    reportDate = date.today(),
                    forceProtectionCondition = "A",
                    workOrderActive = 0,
                    workOrderClosed = 0,
                    workOrderOverdue = 0,
                    company = Unit.objects.get(company = cMid.company)
                    )
    cReport.save()
    
    lMids = Mid.objects.filter(company = cCompany).order_by('alpha')
    
    for p in lMids :
        cAttendance = Attendance(mid = p,
                                 event = cEvent,
                                 status = "U"                                
                                 )
        cAttendance.save()
    
    cDate = date.today()
    cNextWeekendBeg = cDate + timedelta(days=(5 - cDate.isoweekday()));
    cNextWeekendBegAlt = cDate + timedelta(days=(6 - cDate.isoweekday()));
    
    lWeekends = []
    
    if date.today().isoweekday() == 5 :
        lWeekends = Weekend.objects.filter(startDate = date.today()).filter(mid__company = cCompany).filter(status='A').order_by('-mid')
 
    if date.today().isoweekday() == 6 :
        lWeekends1 = Weekend.objects.filter(startDate = cNextWeekendBeg).filter(mid__company = cCompany).filter(status='A').order_by('-mid')
        lWeekends2 = Weekend.objects.filter(startDate = cNextWeekendBegAlt).filter(mid__company = cCompany).filter(status='A').order_by('-mid')
        lWeekends = lWeekends1 | lWeekends2

    for p in lWeekends :
        tMid = p.mid
        tAttendance = Attendance.objects.get(mid = tMid, event = cEvent)
        tAttendance.status = "W"
        tAttendance.comment = p.location + " (" + p.contactNumber + ")"
        tAttendance.save()
    
    return HttpResponseRedirect(reverse('switchboard'))