def add_member(request): view_all = Member.objects.all() subs_end_today_count = Member.objects.filter( registration_upto=datetime.datetime.now(), notification=1).count() search_form = SearchForm() success = 0 if request.method == 'POST': form = AddMemberForm(request.POST, request.FILES) if form.is_valid(): temp = form.save(commit=False) temp.registration_upto = parser.parse( request.POST.get('registration_date')) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) temp.save() success = 'Successfully Added Member' # Add payments if payment is 'paid' if temp.fee_status == 'paid': payments = Payments(user=temp, payment_date=temp.registration_date, payment_period=temp.subscription_period, payment_amount=temp.amount) payments.save() form = AddMemberForm() # get all members according to their batches evening = Member.objects.filter(batch='evening') morning = Member.objects.filter(batch='morning') context = { 'add_success': success, 'form': form, 'morning': morning, 'evening': evening, 'search_form': search_form, 'subs_end_today_count': subs_end_today_count, } return render(request, 'tab_base.html', context) else: # get all members according to their batches evening = Member.objects.filter(batch='evening') morning = Member.objects.filter(batch='morning') form = AddMemberForm() context = { 'form': form, 'morning': morning, 'evening': evening, 'search_form': search_form, 'subs_end_today_count': subs_end_today_count, } return render(request, 'tab_base.html', context)
def mark_approved(self, approved_by=None): self.Status = 'Approved' for tender in self.product_and_quantity.all(): supplier_product = SupplierProductInfo.objects.filter( product=tender.products)[0] payment = Payments(supplier=supplier_product.supplier, amount=tender.get_amount(), accepted_by=approved_by, done=True) payment.save() invoice = Invoice.objects.create(order=self, supplier_product=supplier_product, amount_due=tender.get_amount(), payment=payment) invoice.save()
def add_member(request): view_all = Member.objects.all() success = 0 member = None if request.method == 'POST': form = AddMemberForm(request.POST, request.FILES) if form.is_valid(): temp = form.save(commit=False) temp.first_name = request.POST.get('first_name').capitalize() temp.last_name = request.POST.get('last_name').capitalize() temp.registration_upto = parser.parse( request.POST.get('registration_date')) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) print('amount remaining', temp.remaining_amount) if request.POST.get('fee_status') == 'pending': temp.notification = 1 model_save(temp) success = 'Successfully Added Member' # Add payments if payment is 'paid' if temp.fee_status == 'paid': payments = Payments(user=temp, payment_date=temp.registration_date, payment_period=temp.subscription_period, payment_amount=temp.paid_amount) payments.save() form = AddMemberForm() member = Member.objects.last() context = { 'add_success': success, 'form': form, 'member': member, 'subs_end_today_count': get_notification_count(), } return render(request, 'add_member.html', context) else: form = AddMemberForm() context = { 'form': form, 'subs_end_today_count': get_notification_count(), } return render(request, 'add_member.html', context)
def update_member(request, id): if request.method == 'POST': object = Member.objects.get(pk=id) object.first_name = request.POST.get('first_name') object.last_name = request.POST.get('last_name') object.registration_date = request.POST.get('registration_date') object.registration_upto = parser.parse( request.POST.get('registration_date')) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) object.subscription_type = request.POST.get('subscription_type') object.fee_status = request.POST.get('fee_status') object.amount = request.POST.get('amount') object.notification = 2 # for updating photo if 'photo' in request.FILES: myfile = request.FILES['photo'] fs = FileSystemStorage(base_url="") photo = fs.save(myfile.name, myfile) object.photo = fs.url(photo) object.save() # Add payments if payment is 'paid' if object.fee_status == 'paid': payments = Payments(user=object, payment_date=object.registration_date, payment_period=object.subscription_period, payment_amount=object.amount) payments.save() user = Member.objects.get(pk=id) subs_end_today_count = Member.objects.filter( registration_upto=datetime.datetime.now(), notification=1).count() form = UpdateMemberForm( initial={ 'registration_date': user.registration_date, 'registration_upto': user.registration_upto, 'subscription_type': user.subscription_type, 'subscription_period': user.subscription_period, 'amount': user.amount, 'fee_status': user.fee_status, 'first_name': user.first_name, 'last_name': user.last_name, }) try: payments = Payments.objects.filter(user=user) except Payments.DoesNotExist: payments = 'No Records' return render( request, 'update.html', { 'payments': payments, 'form': form, 'user': user, 'updated': 'Record Updated Successfully', 'subs_end_today_count': subs_end_today_count, }) else: user = Member.objects.get(pk=id) subs_end_today_count = Member.objects.filter( registration_upto=datetime.datetime.now(), notification=1).count() if len(Payments.objects.filter(user=user)) > 0: payments = Payments.objects.filter(user=user) else: payments = 'No Records' form = UpdateMemberForm( initial={ 'registration_date': user.registration_date, 'registration_upto': user.registration_upto, 'subscription_type': user.subscription_type, 'subscription_period': user.subscription_period, 'amount': user.amount, 'fee_status': user.fee_status, 'first_name': user.first_name, 'last_name': user.last_name, }) return render( request, 'update.html', { 'payments': payments, 'form': form, 'user': user, 'subs_end_today_count': subs_end_today_count, })
def update_member(request, id): if request.method == 'POST' and request.POST.get('export'): return export_all(Member.objects.filter(pk=id)) if request.method == 'POST' and request.POST.get('no'): return redirect('/') if request.method == 'POST' and request.POST.get('gym_membership'): gym_form = UpdateMemberGymForm(request.POST) if gym_form.is_valid(): object = Member.objects.get(pk=id) amount = request.POST.get('amount') day = ( parser.parse(request.POST.get('registration_upto')) - delta.relativedelta( months=int(request.POST.get('subscription_period')))).day last_day = parser.parse(str(object.registration_upto)).day month = parser.parse(request.POST.get('registration_upto')).month last_month = parser.parse(str(object.registration_upto)).month # if status is stopped then do not update anything if object.stop == 1 and not request.POST.get( 'stop') == '0' and request.POST.get('gym_membership'): messages.error( request, 'Please start the status of user to update the record') return redirect('update_member', id=object.pk) # to change only the batch elif (object.batch != request.POST.get('batch')): object.batch = request.POST.get('batch') object = check_status(request, object) model_save(object) # check if user has modified only the date elif (datetime.datetime.strptime(str(object.registration_date), "%Y-%m-%d") != datetime.datetime.strptime( request.POST.get('registration_date'), "%Y-%m-%d")): object.registration_date = parser.parse( request.POST.get('registration_date')) object.registration_upto = parser.parse( request.POST.get('registration_date') ) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) object.fee_status = request.POST.get('fee_status') object = check_status(request, object) model_save(object) # if amount and period are changed elif (object.amount != amount) and (object.subscription_period != request.POST.get('subscription_period')): object.subscription_type = request.POST.get( 'subscription_type') object.subscription_period = request.POST.get( 'subscription_period') object.registration_date = parser.parse( request.POST.get('registration_upto')) object.registration_upto = parser.parse( request.POST.get('registration_upto') ) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) object.fee_status = request.POST.get('fee_status') object.amount = request.POST.get('amount') object = check_status(request, object) model_save(object) # if only subscription_period is Changed elif (object.subscription_period != request.POST.get('subscription_period')): object.subscription_period = request.POST.get( 'subscription_period') object = check_status(request, object) model_save(object) # if amount and type are changed elif (object.amount != amount) and (object.subscription_type != request.POST.get('subscription_type')): object.subscription_type = request.POST.get( 'subscription_type') object.subscription_period = request.POST.get( 'subscription_period') object.registration_date = parser.parse( request.POST.get('registration_upto')) object.registration_upto = parser.parse( request.POST.get('registration_upto') ) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) object.fee_status = request.POST.get('fee_status') object.amount = request.POST.get('amount') object = check_status(request, object) model_save(object) # if amount ad fee status are changed elif (object.amount != amount) and ( (request.POST.get('fee_status') == 'paid') or (request.POST.get('fee_status') == 'pending')): object.amount = amount object.fee_status = request.POST.get('fee_status') object = check_status(request, object) model_save(object) # if only amount is channged elif (object.amount != amount): object.registration_date = parser.parse( request.POST.get('registration_upto')) object.registration_upto = parser.parse( request.POST.get('registration_upto') ) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) object.fee_status = request.POST.get('fee_status') object.amount = request.POST.get('amount') if request.POST.get('fee_status') == 'pending': object.notification = 1 elif request.POST.get('fee_status') == 'paid': object.notification = 2 object = check_status(request, object) model_save(object) # nothing is changed else: if not request.POST.get('stop') == '1': object.registration_date = parser.parse( request.POST.get('registration_upto')) object.registration_upto = parser.parse( request.POST.get('registration_upto') ) + delta.relativedelta( months=int(request.POST.get('subscription_period'))) object.amount = request.POST.get('amount') if request.POST.get('fee_status') == 'pending': object.notification = 1 elif request.POST.get('fee_status') == 'paid': object.notification = 2 object.fee_status = request.POST.get('fee_status') object = check_status(request, object) model_save(object) # Add payments if payment is 'paid' if object.fee_status == 'paid': check = Payments.objects.filter( payment_date=object.registration_date, user__pk=object.pk).count() if check == 0: payments = Payments( user=object, payment_date=object.registration_date, payment_period=object.subscription_period, payment_amount=object.amount) payments.save() user = Member.objects.get(pk=id) gym_form = UpdateMemberGymForm( initial={ 'registration_date': user.registration_date, 'registration_upto': user.registration_upto, 'subscription_type': user.subscription_type, 'subscription_period': user.subscription_period, 'amount': user.amount, 'fee_status': user.fee_status, 'batch': user.batch, 'stop': user.stop, }) info_form = UpdateMemberInfoForm( initial={ 'first_name': user.first_name, 'last_name': user.last_name, 'dob': user.dob, }) try: payments = Payments.objects.filter(user=user) except Payments.DoesNotExist: payments = 'No Records' messages.success(request, 'Record updated successfully!') return redirect('update_member', id=user.pk) else: user = Member.objects.get(pk=id) info_form = UpdateMemberInfoForm( initial={ 'first_name': user.first_name, 'last_name': user.last_name, 'dob': user.dob, }) try: payments = Payments.objects.filter(user=user) except Payments.DoesNotExist: payments = 'No Records' return render( request, 'update.html', { 'payments': payments, 'gym_form': gym_form, 'info_form': info_form, 'user': user, 'subs_end_today_count': get_notification_count(), }) elif request.method == 'POST' and request.POST.get('info'): object = Member.objects.get(pk=id) object.first_name = request.POST.get('first_name') object.last_name = request.POST.get('last_name') object.dob = request.POST.get('dob') # for updating photo if 'photo' in request.FILES: myfile = request.FILES['photo'] fs = FileSystemStorage(base_url="") photo = fs.save(myfile.name, myfile) object.photo = fs.url(photo) model_save(object) user = Member.objects.get(pk=id) gym_form = UpdateMemberGymForm( initial={ 'registration_date': user.registration_date, 'registration_upto': user.registration_upto, 'subscription_type': user.subscription_type, 'subscription_period': user.subscription_period, 'amount': user.amount, 'fee_status': user.fee_status, 'batch': user.batch, 'stop': user.stop, }) info_form = UpdateMemberInfoForm( initial={ 'first_name': user.first_name, 'last_name': user.last_name, 'dob': user.dob, }) try: payments = Payments.objects.filter(user=user) except Payments.DoesNotExist: payments = 'No Records' return render( request, 'update.html', { 'payments': payments, 'gym_form': gym_form, 'info_form': info_form, 'user': user, 'updated': 'Record Updated Successfully', 'subs_end_today_count': get_notification_count(), }) else: user = Member.objects.get(pk=id) if len(Payments.objects.filter(user=user)) > 0: payments = Payments.objects.filter(user=user) else: payments = 'No Records' gym_form = UpdateMemberGymForm( initial={ 'registration_date': user.registration_date, 'registration_upto': user.registration_upto, 'subscription_type': user.subscription_type, 'subscription_period': user.subscription_period, 'amount': user.amount, 'fee_status': user.fee_status, 'batch': user.batch, 'stop': user.stop, }) info_form = UpdateMemberInfoForm( initial={ 'first_name': user.first_name, 'last_name': user.last_name, 'dob': user.dob, }) return render( request, 'update.html', { 'payments': payments, 'gym_form': gym_form, 'info_form': info_form, 'user': user, 'subs_end_today_count': get_notification_count(), })
def subscribe(request): #get all subscriptions from the DB subscriptions = Subscription.objects.all() if request.method == "POST": form = request.POST subscription = form['subscription'] phone_number = form['phone_number'] if len(phone_number) < 9: messages.error(request,'Invalid phone number') return redirect('subscriptions:subscribe') else: phone_number = '254' + phone_number[-9:] #logged in user user = request.user #start date is the current instance of time start_date = datetime.now() #set default amount amount = 0 #set end date to none before assignment end_date = None #grab current user subscription package from the DB user_subscription = Subscription.objects.get(pk=subscription) weekly_subscription = Subscription.objects.get(subscription_type="W") weekly_subscription_amount = weekly_subscription.amount monthly_subscription = Subscription.objects.get(subscription_type="M") monthly_subscription_amount = monthly_subscription.amount annual_subscription = Subscription.objects.get(subscription_type="A") annual_subscription_amount = annual_subscription.amount #if the choosen value is 1 from the form,set amount and global end date if subscription == "1": amount += weekly_subscription_amount end_date = start_date + relativedelta(weeks=1) print(end_date) elif subscription == "2": amount += monthly_subscription_amount end_date = start_date + relativedelta(months=1) elif subscription == "3": amount += annual_subscription_amount end_date = start_date + relativedelta(years=1) else: amount += 0 #grab payments table and update with new values payment_request_response = lipa_na_mpesa_online(request,phone_number,amount) if payment_request_response['error']: # Handles when there was an error from safcom while making request messages.error(request, payment_request_response['errorMessage']) # TODO don't return error msg from safcom. Make the error msg more user friendly return redirect('subscriptions:subscribe') merchant_request_id = payment_request_response['merchant_request_id'] #not sure how to validate if payment is made payments_data = Payments() payments_data.user = user payments_data.start_date = start_date payments_data.end_date = end_date payments_data.subscription = user_subscription payments_data.amount = amount payments_data.merchantId = merchant_request_id payments_data.phone_number = phone_number payments_data.validation = False payments_data.save() ctx = {'merchant_id': merchant_request_id} return render(request, "subscriptions/subscription_confirmation.html", ctx) context = {'subscriptions':subscriptions} return render(request,"subscriptions/subscribeform.html",context)