def add(request): if request.method == 'POST': # set current user as employer implicitly form = EmployeeForm(request.POST) form_valid = form.is_valid() if form_valid: employee = form.save(commit=False) employee.employer = request.user.service_provider employee.save() h = EmployeeWorkingHours() h.employee = employee spwh = WorkingHours.objects.filter(service_provider=request.user.service_provider.id)[0] if spwh: h.time_from = spwh.time_from h.time_to = spwh.time_to h.week_days = [1, 2, 3, 4, 5] else: h.time_from = datetime.time(9, 0) h.time_to = datetime.time(21, 0) h.week_days = [1, 2, 3, 4, 5] h.save() return HttpResponseRedirect(reverse(myemployees)) else: form = EmployeeForm() # render form - new (get request) or invalid with error messages (post request) return render_to_response('employees/add.html', locals(), context_instance=RequestContext(request))
def getReservations(service, provider, start, end): events = [] working_employees = [] if service.employees.all(): for emp in service.employees.all(): if EmployeeWorkingHours.get_for_day(emp, start.date().weekday()) is not None: working_employees.append(emp) today_res = Reservation.objects.filter(date__gte=start, date__lt=end) if list(service.employees.all()).__len__() > 1: today_res = today_res.filter(employee__in=working_employees) active_during_termin = dict() for r in today_res: start = datetime.datetime.combine(start.date(), r.time) end = start + datetime.timedelta(minutes=r.service_duration) while start < end: if r.active_during(start): if not start in active_during_termin: active_during_termin[start] = 1 else: active_during_termin[start] += 1 start += datetime.timedelta(minutes=15) overlaps = [] currently_working = working_employees for term in active_during_termin.keys(): cur_emp = list(working_employees).__len__() for emp in currently_working: cwh = EmployeeWorkingHours.get_for_day(emp, start.weekday()) if term + datetime.timedelta(minutes=service.duration) > datetime.datetime.combine(start.date(), cwh.time_to): cur_emp -= 1 if active_during_termin[term] >= cur_emp: overlaps.append(term) if overlaps: events.extend(group_events(overlaps)) else: events.extend(get_all_reservations(service, provider, start, end)) return events
def getEmployeeWorkingHours(provider, service_id, employee_id, date): workinghrs = EmployeeWorkingHours.get_for_day(service_id, employee_id, date.weekday()) events = [] # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [{ 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR }] # Start events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'color': EVENT_PAUSE_COLOR }) # End events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) return events
def getEmployeeWorkingHours(provider, service_id, employee_id, date): workinghrs = EmployeeWorkingHours.get_for_day(service_id, employee_id, date.weekday()) events = [] # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [ { "title": ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), "start": encodeDatetime(date), "end": encodeDatetime(date + datetime.timedelta(days=1)), "color": EVENT_CLOSED_COLOR, } ] # Start events.append( { "title": ugettext(EVENT_TITLE_CLOSED), "start": encodeDatetime(date), "end": encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), "color": EVENT_PAUSE_COLOR, } ) # End events.append( { "title": ugettext(EVENT_TITLE_CLOSED), "start": encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), "end": encodeDatetime(date + datetime.timedelta(days=1)), "color": EVENT_PAUSE_COLOR, } ) return events
def clean_time(self): employee_id = 0 try: employee_id = self.data['service_provider_employee'] except: print "bubu" now = datetime.datetime.now() data = self.cleaned_data.get('time') if not data or not self.cleaned_data.get('date'): return data start = datetime.datetime.combine(self.cleaned_data['date'], data) end = start + datetime.timedelta(minutes=self.service.duration) if start.date() != end.date(): raise ValidationError(_('Sorry, reservation can\'t span over multiple days.')) # Check current time with date if start < now: raise ValidationError(_('Sorry, you can\'t make a reservation in the past.')) service_provider = self.service.service_provider # Check working hours if employee_id != u'': wrk = EmployeeWorkingHours.get_for_day(self.service.id,employee_id, start.weekday()) if wrk: if start.time() < wrk.time_from: messages.warning(self.request,_('Sorry, the service isn\'t available at this time')) raise ValidationError("") elif end.time() > wrk.time_to: messages.warning(self.request,_('Sorry, the service isn\'t available at this time')) raise ValidationError("") # Check pauses wrk = WorkingHours.get_for_day(self.service.service_provider, start.weekday()) for wrkBr in wrk.breaks.all(): if is_overlapping(start.time(), end.time(), wrkBr.time_from, wrkBr.time_to): raise ValidationError(_('Sorry, the service isn\'t available at specified time.')) # Check reservations service = self.service if employee_id ==u'': reservations = Reservation.objects.filter(service=service, service_provider=service_provider, date=self.cleaned_data.get('date')) else: reservations = Reservation.objects.filter(service=service, service_provider=service_provider,service_provider_employee_id=employee_id, date=self.cleaned_data.get('date')) for res in reservations: resDt = datetime.datetime.combine(res.date, res.time) if is_overlapping(start, end, resDt, resDt + datetime.timedelta(minutes=res.service_duration)): free_employees = check_if_there_is_free_employee(service, start, end, resDt) str='' for e in free_employees: str +=e.first_name+' '+e.last_name+', ' if free_employees: if free_employees.__len__()==1: messages.info(self.request, str +'is free at this time') raise ValidationError(_('Sorry, your reservation is overlapping with another reservation.')) else: messages.info(self.request, str +'are free at this time') raise ValidationError(_('Sorry, your reservation is overlapping with another reservation./n')) else: raise ValidationError(_('Sorry, your reservation is overlapping with another reservation.')) return data
def fillDatabase(): # Users # Password is defined as: admin password = '******' name = 'admin' obj = User(id=666, password=password, is_superuser=1, username=name, first_name='Admin', email=name + '@gmail.com', is_staff=1, is_active=1, phone='435345', language='en') obj.save() password = '******' name = 'user' for i in range(1, 11): obj = User(id=i, password=password, is_superuser=0, username=name + str(i), first_name='John', last_name='Doe ' + str(i), email=name + str(i) + '@gmail.com', is_staff=0, is_active=1, phone='435345', language='en', notification_type=1) obj.save() # Provider categories cat = ["Nega telesa", "Frizerski salon", "Kozmetični salon"] generic = ["massage_salon", "hairdresser_salon", "cosmetic_salon"] for i in range(1, len(cat) + 1): obj = ProvCat(id=i, name=cat[i - 1], generic_gallery=generic[i - 1]) obj.save() # Service categories cat = [ "Negovalni salon", "Masažni salon", "Manikura, nega rok", "Pedikura, nega nog", "Depilacija", "Solarij", "Ličenje", "Masaža", "Frizerske storitve", "Fotografske storitve" ] for i in range(1, len(cat) + 1): obj = Category(id=i, name=cat[i - 1], show_in_gallery=False) if obj.name == "Fotografske storitve": obj.show_in_gallery = True obj.save() # Service Providers int_list = "1,2,3,4,5" for i in range(2, 10): obj = ServiceProvider(id=i, name="Provider" + str(i - 1), street="Strasse" + str(i - 1), zipcode='100' + str(i - 1), city="City" + str(i - 1), country="Country" + str(i - 1), category_id=random.randint( 1, len(ProvCat.objects.all())), subscription_mail_sent=0, reservation_confirmation_needed=0, display_generic_gallery=True, userpage_link="Provider" + str(i - 1)) obj.save() user = User.objects.get(id=i) user.service_provider_id = i user.save() #Add working hours on weekdays (9h-21h) h = WorkingHours() h.service_provider = obj h.time_from = datetime.time(9) h.time_to = datetime.time(21) h.week_days = int_list h.save() # Services # 10 services for each provider durations = [15, 30, 45, 60, 75, 90, 105, 120] discounts = [10, 15, 30, 50, 70, 90, 100] gender = ['m', 'f'] id = 1 for p in ServiceProvider.objects.all(): for s in range(1, 11): obj = Service( id=id, service_provider_id=p.id, name="Service" + str(s), duration=durations[random.randint(0, len(durations) - 1)], price=round(random.uniform(10, 250), 2), sex=gender[random.randint(0, len(gender) - 1)], category_id=random.randint(1, len(Category.objects.all()))) obj.description = str(Category.objects.get(id=obj.category_id)) if random.uniform(1, 100): disc = Discount( discount=discounts[random.randint(0, len(discounts) - 1)], service=obj, valid_from=datetime.date.today(), valid_to=datetime.date.today() + relativedelta(months=1)) disc.save() obj.save() id += 1 #some employees for p in ServiceProvider.objects.all(): for s in range(1, 11): e = Employee(name="Name" + str(s), surname="Surname" + str(s), phone=random.randint(100000, 999999), employer=p) e.save() h = EmployeeWorkingHours() h.employee = e h.time_from = datetime.time(9) h.time_to = datetime.time(21) h.week_days = "1,2,3,4,5" h.save() name = "Customer_" + str(s) c = Customer(name=name, service=p, phone=random.randint(100000, 999999), email=name + '@gmail.com', last_reservation=datetime.datetime.now()) c.save()
def clean_time(self): employee_id = 0 try: employee_id = self.data['service_provider_employee'] except: print "bubu" now = datetime.datetime.now() data = self.cleaned_data.get('time') if not data or not self.cleaned_data.get('date'): return data start = datetime.datetime.combine(self.cleaned_data['date'], data) end = start + datetime.timedelta(minutes=self.service.duration) if start.date() != end.date(): raise ValidationError( _('Sorry, reservation can\'t span over multiple days.')) # Check current time with date if start < now: raise ValidationError( _('Sorry, you can\'t make a reservation in the past.')) service_provider = self.service.service_provider # Check working hours if employee_id != u'': wrk = EmployeeWorkingHours.get_for_day(self.service.id, employee_id, start.weekday()) if wrk: if start.time() < wrk.time_from: messages.warning( self.request, _('Sorry, the service isn\'t available at this time')) raise ValidationError("") elif end.time() > wrk.time_to: messages.warning( self.request, _('Sorry, the service isn\'t available at this time')) raise ValidationError("") # Check pauses wrk = WorkingHours.get_for_day(self.service.service_provider, start.weekday()) for wrkBr in wrk.breaks.all(): if is_overlapping(start.time(), end.time(), wrkBr.time_from, wrkBr.time_to): raise ValidationError( _('Sorry, the service isn\'t available at specified time.') ) # Check reservations service = self.service if employee_id == u'': reservations = Reservation.objects.filter( service=service, service_provider=service_provider, date=self.cleaned_data.get('date')) else: reservations = Reservation.objects.filter( service=service, service_provider=service_provider, service_provider_employee_id=employee_id, date=self.cleaned_data.get('date')) for res in reservations: resDt = datetime.datetime.combine(res.date, res.time) if is_overlapping( start, end, resDt, resDt + datetime.timedelta(minutes=res.service_duration)): free_employees = check_if_there_is_free_employee( service, start, end, resDt) str = '' for e in free_employees: str += e.first_name + ' ' + e.last_name + ', ' if free_employees: if free_employees.__len__() == 1: messages.info(self.request, str + 'is free at this time') raise ValidationError( _('Sorry, your reservation is overlapping with another reservation.' )) else: messages.info(self.request, str + 'are free at this time') raise ValidationError( _('Sorry, your reservation is overlapping with another reservation./n' )) else: raise ValidationError( _('Sorry, your reservation is overlapping with another reservation.' )) return data
def add(request): if request.method == 'POST': # set current user as employer implicitly form = EmployeeForm(request.POST) form_valid = form.is_valid() if form_valid: employee = form.save(commit=False) employee.employer = request.user.service_provider employee.save() h = EmployeeWorkingHours() h.employee = employee spwh = WorkingHours.objects.filter( service_provider=request.user.service_provider.id)[0] if spwh: h.time_from = spwh.time_from h.time_to = spwh.time_to h.week_days = [1, 2, 3, 4, 5] else: h.time_from = datetime.time(9, 0) h.time_to = datetime.time(21, 0) h.week_days = [1, 2, 3, 4, 5] h.save() return HttpResponseRedirect(reverse(myemployees)) else: form = EmployeeForm() # render form - new (get request) or invalid with error messages (post request) return render_to_response('employees/add.html', locals(), context_instance=RequestContext(request))
def getEmployeeWorkingHours(provider, employee, date, past): sp_workinghrs = WorkingHours.get_for_day(provider, date.weekday()) workinghrs = EmployeeWorkingHours.get_for_day(employee, date.weekday()) events = [] if past: now = datetime.datetime.now() if date < now.date(): return [ { 'title': ugettext('In the past'), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, datetime.time(23, 59))), 'color': '#444444' } ] elif date == now.date(): events.append( { 'title': ugettext('In the past'), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, now.time())), 'color': '#444444' } ) # TODO add employee absence support if sp_workinghrs is None or Absence.is_absent_on(provider, date): return [ { 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR } ] if workinghrs is None: return [ { 'title': ugettext(EVENT_TITLE_NOT_WORKING_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR } ] events.append({ 'title': ugettext(EVENT_TITLE_NOT_WORKING), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'color': EVENT_PAUSE_COLOR }) events.append({ 'title': ugettext(EVENT_TITLE_NOT_WORKING), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) for wrkbrk in sp_workinghrs.breaks.all(): events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), 'color': EVENT_PAUSE_COLOR }) return events