def membership(request, username): user = get_object_or_404(User, username=username) start = today = timezone.localtime(timezone.now()).date() last_membership = user.profile.last_membership() if last_membership and last_membership.end_date and last_membership.end_date > today - timedelta(days=10): start = (last_membership.end_date + timedelta(days=1)) last = start + MonthDelta(1) - timedelta(days=1) if request.method == 'POST': membership_form = MembershipForm(request.POST, request.FILES) try: if membership_form.is_valid(): membership_form.created_by = request.user membership_form.save() return HttpResponseRedirect(reverse('staff_user_detail', kwargs={'username': username})) except Exception as e: messages.add_message(request, messages.ERROR, e) else: membership_form = MembershipForm(initial={'username': username, 'start_date': start}) # Send them to the update page if we don't have an end date if (last_membership and not last_membership.end_date): return HttpResponseRedirect(reverse('staff_membership', kwargs={'membership_id': last_membership.id})) plans = MembershipPlan.objects.filter(enabled=True).order_by('name') context = {'user':user, 'membership_plans': plans, 'membership_form': membership_form, 'today': today.isoformat(), 'last': last.isoformat()} return render(request, 'staff/membership.html', context)
def membership(request, membership_id): membership = get_object_or_404(Membership, pk=membership_id) if request.method == 'POST': membership_form = MembershipForm(request.POST, request.FILES) try: if membership_form.is_valid(): membership_form.save() return HttpResponseRedirect(reverse('staff_user_detail', kwargs={'username': membership.user.username})) except Exception as e: messages.add_message(request, messages.ERROR, e) else: membership_form = MembershipForm(initial={'membership_id': membership.id, 'username': membership.user.username, 'membership_plan': membership.membership_plan, 'start_date': membership.start_date, 'end_date': membership.end_date, 'monthly_rate': membership.monthly_rate, 'dropin_allowance': membership.dropin_allowance, 'daily_rate': membership.daily_rate, 'has_desk': membership.has_desk, 'has_key': membership.has_key, 'has_mail': membership.has_mail, 'paid_by': membership.paid_by}) today = timezone.localtime(timezone.now()).date() last = membership.next_billing_date() - timedelta(days=1) context = {'user': membership.user, 'membership': membership, 'membership_plans': MembershipPlan.objects.all(), 'membership_form': membership_form, 'today': today.isoformat(), 'last': last.isoformat()} return render(request, 'staff/membership.html', context)
def add_membership(request, username): user = get_object_or_404(User, username=username) start = today = timezone.localtime(timezone.now()).date() last_membership = user.profile.last_membership() if last_membership and last_membership.end_date and last_membership.end_date > today - timedelta( days=10): start = (last_membership.end_date + timedelta(days=1)) last = start + relativedelta(months=1) - timedelta(days=1) if request.method == 'POST': membership_form = MembershipForm(request.POST, request.FILES) try: if membership_form.is_valid(): membership_form.created_by = request.user membership_form.save() return HttpResponseRedirect( reverse('staff:user:detail', kwargs={'username': username})) except Exception as e: messages.add_message(request, messages.ERROR, e) else: membership_form = MembershipForm(initial={ 'username': username, 'start_date': start }) # Send them to the update page if we don't have an end date if (last_membership and not last_membership.end_date): return HttpResponseRedirect( reverse('staff:user:membership', kwargs={'membership_id': last_membership.id})) plans = MembershipPlan.objects.filter(enabled=True).order_by('name') context = { 'user': user, 'membership_plans': plans, 'membership_form': membership_form, 'today': today.isoformat(), 'last': last.isoformat() } return render(request, 'staff/user/membership.html', context)
def membership(request, membership_id): membership = get_object_or_404(Membership, pk=membership_id) if request.method == 'POST': membership_form = MembershipForm(request.POST, request.FILES) try: if membership_form.is_valid(): membership_form.save() return HttpResponseRedirect( reverse('staff:user:detail', kwargs={'username': membership.user.username})) except Exception as e: messages.add_message(request, messages.ERROR, e) else: membership_form = MembershipForm( initial={ 'membership_id': membership.id, 'username': membership.user.username, 'membership_plan': membership.membership_plan, 'start_date': membership.start_date, 'end_date': membership.end_date, 'monthly_rate': membership.monthly_rate, 'dropin_allowance': membership.dropin_allowance, 'daily_rate': membership.daily_rate, 'has_desk': membership.has_desk, 'has_key': membership.has_key, 'has_mail': membership.has_mail, 'paid_by': membership.paid_by }) today = timezone.localtime(timezone.now()).date() last = membership.next_billing_date() - timedelta(days=1) context = { 'user': membership.user, 'membership': membership, 'membership_plans': MembershipPlan.objects.all(), 'membership_form': membership_form, 'today': today.isoformat(), 'last': last.isoformat() } return render(request, 'staff/user/membership.html', context)
def membership(request, username): user = get_object_or_404(User, username=username) membership = Membership.objects.for_user(user) subscriptions = None sub_data = None start = None action = request.GET.get('action', None) active_members = User.helper.active_members() active_billing = User.objects.filter(profile__valid_billing=True).order_by('first_name') SubFormSet = formset_factory(SubscriptionForm) old_pkg = None bill_day = membership.bill_day if membership.package_name and membership.active_subscriptions(): old_pkg_name = membership.package_name() old_pkg = MembershipPackage.objects.get(name=old_pkg_name).id package = request.GET.get('package', old_pkg) today = localtime(now()).date() target_date = request.GET.get('target_date', None) if package != old_pkg: subscriptions = SubscriptionDefault.objects.filter(package=package) sub_data=[{'s_id': None, 'resource': s.resource, 'allowance':s.allowance, 'start_date': today, 'end_date': None, 'username': user.username, 'created_by': request.user, 'monthly_rate': s.monthly_rate, 'overage_rate': s.overage_rate, 'paid_by': None} for s in subscriptions] action = 'change' else: subscriptions = membership.active_subscriptions() sub_data=[{'s_id': s.id, 'package_name': s.package_name, 'resource': s.resource, 'allowance':s.allowance, 'start_date':s.start_date, 'end_date': s.end_date, 'username': user.username, 'created_by': s.created_by, 'monthly_rate': s.monthly_rate, 'overage_rate': s.overage_rate, 'paid_by': s.paid_by} for s in subscriptions] if request.method == 'POST': if 'ending' in request.POST: if request.POST['ending'] == 'today': end_date = today end_target = end_date.strftime('%Y-%m-%d') elif request.POST['ending'] == 'yesterday': end_date = today - timedelta(days=1) end_target = end_date.strftime('%Y-%m-%d') elif request.POST['ending'] == 'eop': ps, end_date = user.membership.get_period() end_target = end_date.strftime('%Y-%m-%d') else: end_target = request.POST['date-end'] return HttpResponseRedirect(reverse('staff:members:confirm', kwargs={'username': username, 'package': None, 'new_subs': None, 'end_target': end_target, 'start_target': None})) else: package_form = MembershipForm(request.POST) sub_formset = SubFormSet(request.POST) if sub_formset.is_valid() and package_form.is_valid(): try: with transaction.atomic(): new_subs = [] package = request.POST['package'] membership = {'package': package} for sub_form in sub_formset: paid_by = None package_name = MembershipPackage.objects.get(id=package).name s_id = sub_form.cleaned_data.get('s_id', None) resource = sub_form.cleaned_data.get('resource', None) allowance = sub_form.cleaned_data.get('allowance', None) start_date = sub_form.cleaned_data.get('start_date', None) if start_date: start = start_date start_date = start_date.strftime('%Y-%m-%d') end_date = sub_form.cleaned_data.get('end_date', None) if end_date: end_date = end_date.strftime('%Y-%m-%d') monthly_rate = sub_form.cleaned_data.get('monthly_rate', None) overage_rate = sub_form.cleaned_data.get('overage_rate', None) paid_by = sub_form.cleaned_data.get('paid_by', None) if resource and start_date: new_subs.append({'s_id': s_id, 'resource':resource.id, 'package_name': package_name, 'allowance':allowance, 'start_date':start_date, 'end_date':end_date, 'monthly_rate': monthly_rate, 'overage_rate':overage_rate, 'paid_by':paid_by, 'membership':None}) end_target = start - timedelta(days=1) return HttpResponseRedirect(reverse('staff:members:confirm', kwargs={'username': username, 'package': membership, 'end_target': end_target, 'start_target': start, 'new_subs': new_subs})) except IntegrityError: messages.error(request, 'There was an error updating the subscriptions') else: print((sub_formset.errors)) print((package_form.errors)) messages.error(request, 'There was an error updating the subscriptions') else: package_form = MembershipForm() sub_formset = SubFormSet(initial=sub_data) context = { 'entity': user, 'subscriptions':subscriptions, 'package_form': package_form, 'package': package, 'sub_formset': sub_formset, 'active_members': active_members, 'active_billing': active_billing, 'target_date': target_date, 'bill_day': bill_day, 'action': action, } return render(request, 'staff/members/membership.html', context)