Exemple #1
0
def record_form(request, hour):
    day = request.session['selected_day']
    user = get_current_user(request)

    if request.method == 'POST':
        form = RecordCreateForm(request.POST)

        if form.is_valid():
            cd = form.cleaned_data

            record = RPRecord()
            record.user = get_current_user(request)
            service = RPService.objects.get(id=int(cd['service']))

            record.service = service

            h,m = cd['start_time'].split(':')
            s_date = datetime(day.year, day.month, day.day, int(h), int(m))
            record.start_datetime = s_date

            h,m = cd['stop_time'].split(':')
            s_date = datetime(day.year, day.month, day.day, int(h), int(m))
            record.stop_datetime = s_date
            record.comment = cd['comment']

            #check duble-record
            finded = RPRecord.objects.filter(start_datetime__gt=datetime(day.year, day.month, day.day,0,0), stop_datetime__lt=datetime(day.year, day.month, day.day,23,59), user=record.user)
            if finded:
                st_dt = finded[0].start_datetime
                en_dt = finded[0].stop_datetime
                start_time = '%.02i:%.02i'%(st_dt.hour, st_dt.minute)
                stop_time = '%.02i:%.02i'%(en_dt.hour, en_dt.minute)
                return inform_message(_(u'Ви вже записані на цей день з %(start_time)s до %(stop_time)s. Якщо бажаєте змінити час запису, то відмініть ваш прийом, а потім запишіться на інший час') % {'start_time': unicode(start_time), 'stop_time' : unicode(stop_time)}, redirect_link='/my_records/')


            res = record.stop_datetime - record.start_datetime
            cost = ((res.seconds/60.0) / ATOM_TIME) * service.atom_money

            #check current user balance
            if user.balance < cost:
                return inform_message(_(u'У вас недостатньо коштів для запису. Поповніть, будь ласка, свій рахунок '), '/my_balance')

            record.user.balance -= cost

            try:
                record.user.save()
                record.save()
            except Exception, err:
                transaction.rollback()
                raise err
            else:
                transaction.commit()

            return inform_message(_(u'Ви успішно записались на прийом з %(start_time)s до %(stop_time)s. Вартість послуги - %(cost)i гривень') % {'start_time': unicode(cd['start_time']), 'stop_time': unicode(cd['stop_time']), 'cost': int(cost)}, redirect_link='/my_records/')

        raise Exception('Record form data is not valid')
Exemple #2
0
def _get_free_ranges(day):
    nextday = day + timedelta(1)

    work_ranges = RPWorkTime.objects.filter(start_worktime__gt=day, stop_worktime__lt=nextday).order_by('start_worktime')

    record_ranges = RPRecord.objects.filter(start_datetime__gt=day, stop_datetime__lt=nextday).order_by('start_datetime')

    if day == datetime.today().date():
        fake_rec = RPRecord()
        d = datetime.now()
        fake_rec.start_datetime = datetime(d.year, d.month, d.day, 0,0)
        fake_rec.stop_datetime = datetime.now() + timedelta(0, IDENT_RECORD_TIME*3600,0)
        if fake_rec.start_datetime.day != fake_rec.stop_datetime:
            fake_rec.stop_datetime =  datetime(d.year, d.month, d.day,23,59)

        record_ranges = [r for r in record_ranges]
        record_ranges.append(fake_rec)

    if not work_ranges:
        return {}

    ranges = {}
    start_hour = work_ranges[0].start_worktime.hour
    stop_hour = work_ranges[len(work_ranges)-1].stop_worktime.hour

    if stop_hour != 23:
        stop_hour += 1

    ret_hours = {}
    for hour in range(start_hour, stop_hour):
        h_range = [time(hour,0,0,0),time(hour+1,0,0,0)]
        ret_hours[hour] = []
        for wrange in work_ranges:
            rr = _get_res_range([wrange.start_worktime.time(), wrange.stop_worktime.time()], h_range)
            if rr:
                ret_hours[hour].append( (rr[0], rr[1]) )

    hours = {}
    for hour in ret_hours:
        h = Hour(hour)
        for rrange in record_ranges:
            for hrange in ret_hours[hour]:
                rr = _get_free_range(hrange, [rrange.start_datetime.time(), rrange.stop_datetime.time()])

                if rr is None:
                    h.ranges = None
                    break

                for r in rr:
                    h.append( r )

        if h.ranges is None:
            pass
        elif not h.ranges:
            for r in ret_hours[hour]:
                h.append(r)

        hours[hour] = h

    for h in hours:
        c_hour = hours[h]
        #if c_hour.ranges and len(c_hour.ranges) > 1:
        #    hour = hours[h+1]
        #    if hour.ranges and hour.ranges[0][0].minute < 15:
        #        pass
        #    else:
        #        c_hour.ranges = []
        c_hour.calculate_free()

    return hours