# main code for r in Referral.objects.filter(covenant__isnull=False, covenant__charge=covenant_charge, referraldischarge__isnull=True): ''' filter for all referall that don't have discharge ''' # create a receive for each covenant of referral for c in r.covenant.filter(charge=covenant_charge): # new receive = Receive() receive.created = datetime receive.name = c.name receive.price = c.price receive.off = 0 receive.total = c.price receive.covenant_charge = c.charge receive.covenant_id = c.id # clear all receive.covenant_payment_way_options = '' for pw in c.payment_way.all(): x = "(%s,'%s')," % ( pw.id , pw.name ) # need be a dict receive.covenant_payment_way_options += x # add referral receive.referral = r # save receive.save()
def add_event( request, template='schedule/schedule_form.html', event_form_class=ReferralForm, recurrence_form_class=ScheduleOccurrenceForm, redirect_to = None ): # have to contains dtstart variable in URL. URL from schedule have to contains date and time data. if not 'dtstart' in request.GET: return http.HttpResponseRedirect('/schedule/') if request.POST: # instance form recurrence_form = recurrence_form_class(request, request.POST) # no errors found, form is valid. if recurrence_form.is_valid(): if not request.POST.get('group'): # booking single client referral = get_object_or_404(Referral, pk=request.POST.get('referral'), service__organization=request.user.get_profile().org_active) event = recurrence_form.save(referral) else: # booking a group group = get_object_or_404(ServiceGroup, pk=request.POST.get('group'), service__organization=request.user.get_profile().org_active, active=True) if group.charged_members(): # this check is already done in template. just to prevent empty groups first = True for group_member in group.charged_members(): if first: event = recurrence_form.save(group_member.referral) first = False else: if not event.errors: event = recurrence_form.save(group_member.referral, True) # ignore busy check if not request.POST.get('group'): # booking single client ''' Create a payment for each upcoming event when event by pack or occurrence Event per period will be created by script run by crontab everyday ''' # check if occurrences have one payment by pack or event opened for o in referral.upcoming_nopayment_occurrences_(): # exist a payment for event? if Receive.objects.filter(occurrence=o).count() == 0 : # Filter payment by pack or occurrence for x in referral.covenant.filter(Q(charge=1) | Q(charge=2) ).distinct(): receive = Receive() # new # by pack if x.charge == 2: # check not terminated pack of same referral for p in Receive.objects.filter(occurrence__event=event, covenant_charge=2): if not p.terminated_(): # not terminated pack receive = p # by occurrence # new if not receive.id: receive.name = x.name receive.price = x.price receive.off = 0 receive.total = x.price receive.covenant_charge = x.charge receive.covenant_id = x.id receive.save() # by pack receive.covenant_pack_size = x.event_time if x.charge == 2 else 0 # clear all receive.covenant_payment_way_options = '' for pw in x.payment_way.all(): x = "(%s,'%s')," % ( pw.id , pw.name ) # need be a dict receive.covenant_payment_way_options += x # add occurrence receive.occurrence.add(o) # update m2m receive.save() if not event.errors: messages.success(request, _('Schedule saved successfully')) return http.HttpResponseRedirect(redirect_to or '/schedule/') else: return render_to_response( 'schedule/event_detail.html', dict(event=event), context_instance=RequestContext(request) ) # mount form or return form errors # get from url dtstart = parser.parse( request.GET['dtstart'] ) room = get_object_or_None(Room, pk=request.GET.get('room'), place__organization=request.user.get_profile().org_active) client = get_object_or_None(Client, pk=request.GET.get('client'), person__organization=request.user.get_profile().org_active) referral = get_object_or_None(Referral, pk=request.GET.get('referral'), service__organization=request.user.get_profile().org_active) event_form = event_form_class recurrence_form = recurrence_form_class( request, initial = dict( dtstart=dtstart, day=datetime.strptime(dtstart.strftime("%Y-%m-%d"), "%Y-%m-%d"), until=datetime.strptime(dtstart.strftime("%Y-%m-%d"), "%Y-%m-%d"), room=room.id, ) ) recurrence_form.fields['device'].widget.choices = [(i.id, i) for i in DeviceDetails.objects.active(request.user.get_profile().org_active).filter(Q(room=room) | Q(mobility="2", lendable=True) | Q(place=room.place, mobility="2", lendable=False))] return render_to_response( template, dict( dtstart = dtstart, event_form = event_form, recurrence_form = recurrence_form, group = ServiceGroup.objects.filter(service__organization = request.user.get_profile().org_active, active=True), room = room, object = client, referral = referral, room_id = room.id, ), context_instance=RequestContext(request) )
covenant__charge=covenant_charge, referraldischarge__isnull=True): ''' filter for all referall that don't have discharge ''' # create a receive for each covenant of referral for c in r.covenant.filter(charge=covenant_charge): # new receive = Receive() receive.created = datetime.today() receive.launch_date = datetime.today() receive.name = c.name receive.price = c.price receive.off = 0 receive.total = c.price receive.covenant_charge = c.charge receive.covenant_id = c.id # clear all receive.covenant_payment_way_options = '' for pw in c.payment_way.all(): x = "(%s,'%s')," % (pw.id, pw.name) # need be a dict receive.covenant_payment_way_options += x # add referral receive.referral = r # save receive.save()
def add_event( request, template='schedule/schedule_form.html', event_form_class=ReferralForm, recurrence_form_class=ScheduleOccurrenceForm, redirect_to = None ): disable_check_busy = False to_confirm_conflict = False # dont show checkbox to ignore conflict # have to contains dtstart variable in URL. URL from schedule have to contains date and time data. if not 'dtstart' in request.GET: return http.HttpResponseRedirect('/schedule/') # get from url dtstart = parser.parse( request.GET['dtstart'] ) room = get_object_or_None(Room, pk=request.GET.get('room'), place__organization=request.user.get_profile().org_active) client = get_object_or_None(Client, pk=request.GET.get('client'), person__organization=request.user.get_profile().org_active) referral = get_object_or_None(Referral, pk=request.GET.get('referral'), service__organization=request.user.get_profile().org_active) event_form = event_form_class if request.POST: if request.POST.get('ignore_conflict') == 'on': disable_check_busy = True # instance form recurrence_form = recurrence_form_class(request, room.place, request.POST) # no errors found, form is valid. if recurrence_form.is_valid(): if not request.POST.get('group'): # booking single client referral = get_object_or_404(Referral, pk=request.POST.get('referral'), service__organization=request.user.get_profile().org_active) event_form = recurrence_form.save(referral, disable_check_busy=disable_check_busy) else: # booking a group group = get_object_or_404(ServiceGroup, pk=request.POST.get('group'), service__organization=request.user.get_profile().org_active, active=True) if group.charged_members(): # this check is already done in template. just to prevent empty groups first = True for group_member in group.charged_members(): if first: event_form = recurrence_form.save(group_member.referral) first = False else: if not event_form.errors: event_form = recurrence_form.save(group_member.referral, True) # ignore busy check if not request.POST.get('group'): # booking single client ''' Create a payment for each upcoming event when event by pack or occurrence Event per period will be created by script run by crontab everyday ''' # check if occurrences have one payment by pack or event opened for o in referral.upcoming_nopayment_occurrences_(): # exist a payment for event? if Receive.objects.filter(occurrence=o).count() == 0 : # Filter payment by pack or occurrence for x in referral.covenant.filter(Q(charge=1) | Q(charge=2) ).distinct(): receive = Receive() # new """ by pack, by event. charge2 overwrite charge1 """ # by pack if x.charge == 2: # check not terminated pack of same referral for p in Receive.objects.filter(occurrence__event=event_form, covenant_charge=2): if not p.terminated_()[0]: # not terminated pack receive = p # by occurrence # new if not receive.id: receive.name = x.name receive.price = x.price receive.off = 0 receive.total = x.price receive.covenant_charge = x.charge receive.covenant_id = x.id receive.save() # by pack receive.covenant_pack_size = x.event_time if x.charge == 2 else 0 # clear all receive.covenant_payment_way_options = '' for pw in x.payment_way.all(): x = "(%s,'%s')," % ( pw.id , pw.name ) # need be a dict receive.covenant_payment_way_options += x # add occurrence receive.occurrence.add(o) # update m2m receive.save() if not event_form.errors: messages.success(request, _('Schedule saved successfully')) return http.HttpResponseRedirect(redirect_to or '/schedule/') else: messages.info(request, _(u'Conflito no agendamento.')) to_confirm_conflict = True # show checkbox else: # mount form or return form errors # convert hour:minutes to second to set initial select interval_sec = time_delta_total_seconds( timedelta(minutes=int(request.user.get_profile().org_active.time_slot_schedule)) ) start_sec = time_delta_total_seconds( timedelta(hours=dtstart.hour, minutes=dtstart.minute) ) end_sec = start_sec + interval_sec recurrence_form = recurrence_form_class( request, room.place, # start, end hour of place, render select in this range. initial = dict( dtstart=dtstart, day=datetime.strptime(dtstart.strftime("%Y-%m-%d"), "%Y-%m-%d"), until=datetime.strptime(dtstart.strftime("%Y-%m-%d"), "%Y-%m-%d"), room=room.id, start_time_delta=start_sec, end_time_delta=end_sec, ) ) recurrence_form.fields['device'].widget.choices = [(i.id, i) for i in DeviceDetails.objects.active(request.user.get_profile().org_active).filter(Q(room=room) | Q(mobility="2", lendable=True) | Q(place=room.place, mobility="2", lendable=False))] return render_to_response(template, dict( slot_time = request.user.get_profile().org_active.time_slot_schedule, dtstart = dtstart, event_form = event_form, recurrence_form = recurrence_form, group = ServiceGroup.objects.filter(service__organization = request.user.get_profile().org_active, active=True), room = room, object = client, referral = referral, room_id = room.id, disable_check_busy = disable_check_busy, to_confirm_conflict = to_confirm_conflict, ), context_instance=RequestContext(request) )