Beispiel #1
0
    def get(self, request, *args, **kwargs):
        print(" APPLICATION FEE SUCCESS ")
        #        for ss in request.session.keys():
        #            print (ss)
        #            print (request.session[ss])

        proposal = None
        submitter = None
        invoice = None
        try:
            context = template_context(self.request)
            basket = None
            application_fee = get_session_application_invoice(request.session)
            proposal = application_fee.proposal

            try:
                recipient = proposal.applicant.email
                submitter = proposal.applicant
            except:
                recipient = proposal.submitter.email
                submitter = proposal.submitter

            if self.request.user.is_authenticated():
                basket = Basket.objects.filter(
                    status='Submitted', owner=request.user).order_by('-id')[:1]
            else:
                basket = Basket.objects.filter(
                    status='Submitted',
                    owner=booking.proposal.submitter).order_by('-id')[:1]

            order = Order.objects.get(basket=basket[0])
            invoice = Invoice.objects.get(order_number=order.number)
            invoice_ref = invoice.reference
            fee_inv, created = ApplicationFeeInvoice.objects.get_or_create(
                application_fee=application_fee, invoice_reference=invoice_ref)

            if application_fee.payment_type == ApplicationFee.PAYMENT_TYPE_TEMPORARY:
                try:
                    inv = Invoice.objects.get(reference=invoice_ref)
                    order = Order.objects.get(number=inv.order_number)
                    order.user = submitter
                    order.save()
                except Invoice.DoesNotExist:
                    logger.error(
                        '{} tried paying an application fee with an incorrect invoice'
                        .format('User {} with id {}'.
                                format(proposal.submitter.get_full_name(
                                ), proposal.submitter.id) if proposal.
                                submitter else 'An anonymous user'))
                    return redirect('external-proposal-detail',
                                    args=(proposal.id, ))
                if inv.system not in ['0557']:
                    logger.error(
                        '{} tried paying an application fee with an invoice from another system with reference number {}'
                        .format(
                            'User {} with id {}'.format(
                                proposal.submitter.get_full_name(),
                                proposal.submitter.id) if proposal.submitter
                            else 'An anonymous user', inv.reference))
                    return redirect('external-proposal-detail',
                                    args=(proposal.id, ))

                if fee_inv:
                    #application_fee.payment_type = 1  # internet booking
                    application_fee.payment_type = ApplicationFee.PAYMENT_TYPE_INTERNET
                    application_fee.expiry_time = None
                    update_payments(invoice_ref)

                    proposal = proposal_submit(proposal, request)
                    if proposal and (invoice.payment_status == 'paid'
                                     or invoice.payment_status == 'over_paid'):
                        proposal.fee_invoice_reference = invoice_ref
                        proposal.save()
                    else:
                        logger.error('Invoice payment status is {}'.format(
                            invoice.payment_status))
                        raise

                    application_fee.save()
                    request.session[
                        'cols_last_app_invoice'] = application_fee.id
                    delete_session_application_invoice(request.session)

                    send_application_fee_invoice_tclass_email_notification(
                        request, proposal, invoice, recipients=[recipient])
                    send_application_fee_confirmation_tclass_email_notification(
                        request,
                        application_fee,
                        invoice,
                        recipients=[recipient])

                    context = {
                        'proposal': proposal,
                        'submitter': submitter,
                        'fee_invoice': invoice
                    }
                    return render(request, self.template_name, context)

        except Exception as e:
            if ('cols_last_app_invoice'
                    in request.session) and ApplicationFee.objects.filter(
                        id=request.session['cols_last_app_invoice']).exists():
                application_fee = ApplicationFee.objects.get(
                    id=request.session['cols_last_app_invoice'])
                proposal = application_fee.proposal

                try:
                    recipient = proposal.applicant.email
                    submitter = proposal.applicant
                except:
                    recipient = proposal.submitter.email
                    submitter = proposal.submitter

                if ApplicationFeeInvoice.objects.filter(
                        application_fee=application_fee).count() > 0:
                    afi = ApplicationFeeInvoice.objects.filter(
                        application_fee=application_fee)
                    invoice = afi[0]
            else:
                return redirect('home')

        context = {
            'proposal': proposal,
            'submitter': submitter,
            'fee_invoice': invoice
        }
        return render(request, self.template_name, context)
Beispiel #2
0
    def get(self, request, *args, **kwargs):
        print (" APPLICATION FEE SUCCESS ")
#        for ss in request.session.keys():
#            print (ss)
#            print (request.session[ss])

        proposal = None
        submitter = None
        invoice = None
        try:
            print '0a Session: {}'.format(request.session['cols_app_invoice'] if 'cols_app_invoice' in request.session else '')
            print '0b Last Session: {}'.format(request.session['cols_last_app_invoice'] if 'cols_last_app_invoice' in request.session else '')
            #import ipdb; ipdb.set_trace()
            context = template_context(self.request)
            basket = None
            application_fee = get_session_application_invoice(request.session)
            print (" Session (App Fee) {}".format(application_fee))
            print (" 1 ")
            proposal = application_fee.proposal
            print (" 2 ")
            #proposal = get_session_application_invoice(request.session)

            try:
                recipient = proposal.applicant.email
                submitter = proposal.applicant
            except:
                recipient = proposal.submitter.email
                submitter = proposal.submitter

            print (" 3 ")
            if self.request.user.is_authenticated():
                basket = Basket.objects.filter(status='Submitted', owner=request.user).order_by('-id')[:1]
                #basket_open = Basket.objects.filter(status='Open', owner=request.user).order_by('-id')[:1]
                #print '3a - Basket ID: {}, Status: {}'.format(basket_open[0].id, basket_open[0].status)
                print (" 3a ")
            else:
                basket = Basket.objects.filter(status='Submitted', owner=booking.proposal.submitter).order_by('-id')[:1]
                print (" 3b ")

            print (" 4 ")
            order = Order.objects.get(basket=basket[0])
            print (" 5 ")
            invoice = Invoice.objects.get(order_number=order.number)
            print (" 6 ")
            invoice_ref = invoice.reference
            print (" 7 ")
            #book_inv, created = BookingInvoice.objects.get_or_create(booking=booking, invoice_reference=invoice_ref)
            fee_inv, created = ApplicationFeeInvoice.objects.get_or_create(application_fee=application_fee, invoice_reference=invoice_ref)

            #import ipdb; ipdb.set_trace()
            print 'Basket ID: {}, Status: {}, Order: {}, Invoice: {}'.format(basket[0].id, basket[0].status, order, invoice_ref)
            if application_fee.payment_type == 3:
                try:
                    inv = Invoice.objects.get(reference=invoice_ref)
                    order = Order.objects.get(number=inv.order_number)
                    order.user = submitter
                    order.save()
                    print (" 8 ")
                except Invoice.DoesNotExist:
                    print ("INVOICE ERROR")
                    logger.error('{} tried paying an application fee with an incorrect invoice'.format('User {} with id {}'.format(proposal.submitter.get_full_name(), proposal.submitter.id) if proposal.submitter else 'An anonymous user'))
                    return redirect('external-proposal-detail', args=(proposal.id,))
                #if inv.system not in ['S557']:
                print ("INVOICE SYSTEM {}, settings.PS_PAYMENT_SYSTEM_ID {}".format(inv.system, settings.PS_PAYMENT_SYSTEM_ID))
                if inv.system not in ['0557']:
                    print ("SYSTEM ERROR")
                    logger.error('{} tried paying an application fee with an invoice from another system with reference number {}'.format('User {} with id {}'.format(proposal.submitter.get_full_name(), proposal.submitter.id) if proposal.submitter else 'An anonymous user',inv.reference))
                    return redirect('external-proposal-detail', args=(proposal.id,))

                if fee_inv:
                    application_fee.payment_type = 1  # internet booking
                    application_fee.expiry_time = None
                    update_payments(invoice_ref)

                    print (" 9 ")
                    proposal = proposal_submit(proposal, request)
                    if proposal and (invoice.payment_status == 'paid' or invoice.payment_status == 'over_paid'):
                        proposal.fee_invoice_reference = invoice_ref
                        proposal.save()
                        print (" 10 ")
                    else:
                        logger.error('Invoice payment status is {}'.format(invoice.payment_status))
                        raise

                    application_fee.save()
                    print (" 11 ")
                    request.session['cols_last_app_invoice'] = application_fee.id
                    delete_session_application_invoice(request.session)
                    print '11a Session: {}'.format(request.session['cols_app_invoice'] if 'cols_app_invoice' in request.session else '')
                    print '11b Last Session: {}'.format(request.session['cols_last_app_invoice'] if 'cols_last_app_invoice' in request.session else '')

                    send_application_fee_invoice_tclass_email_notification(request, proposal, invoice, recipients=[recipient])
                    send_application_fee_confirmation_tclass_email_notification(request, proposal, invoice, recipients=[recipient])
                    print (" 12 ")

                    context = {
                        'proposal': proposal,
                        'submitter': submitter,
                        'fee_invoice': invoice
                    }
                    print (" 13 ")
                    return render(request, self.template_name, context)

        except Exception as e:
            print 'My Exception: {}'.format(e)
            if ('cols_last_app_invoice' in request.session) and ApplicationFee.objects.filter(id=request.session['cols_last_app_invoice']).exists():
                application_fee = ApplicationFee.objects.get(id=request.session['cols_last_app_invoice'])
                proposal = application_fee.proposal

                try:
                    recipient = proposal.applicant.email
                    submitter = proposal.applicant
                except:
                    recipient = proposal.submitter.email
                    submitter = proposal.submitter

                if ApplicationFeeInvoice.objects.filter(application_fee=application_fee).count() > 0:
                    afi = ApplicationFeeInvoice.objects.filter(application_fee=application_fee)
                    #invoice = afi[0].invoice_reference
                    invoice = afi[0]
                    print (" 13a: {} ".format(invoice))
#                    book_inv = BookingInvoice.objects.get(booking=booking).invoice_reference
            else:
                #import ipdb; ipdb.set_trace()
                print (" 14 ")
                return redirect('home')

        context = {
            #'booking': booking,
            #'book_inv': [app_inv]
            'proposal': proposal,
            'submitter': submitter,
            'fee_invoice': invoice
        }
        print (" 15 ")
        return render(request, self.template_name, context)
    def post(self, request, *args, **kwargs):
        try:
            context_processor = template_context(request)
            application_fee = ApplicationFee.objects.get(pk=request.session['cols_app_invoice']) if 'cols_app_invoice' in request.session else None
            proposal = application_fee.proposal

            try:
                recipient = proposal.applicant.email
                submitter = proposal.applicant
            except:
                recipient = proposal.submitter.email
                submitter = proposal.submitter

            if request.user.is_staff or request.user.is_superuser or ApplicationFee.objects.filter(pk=application_fee.id).count() == 1:
                invoice = None
                #basket = get_basket(request)
                basket = request.basket

                # here we are manually creating an order and invoice from the basket - by-passing credit card payment screen.
                ## commenting below lines and using CreateInvoiceBasket because basket created in previous view
                #order_response = place_order_submission(request)
                #order = Order.objects.get(basket=basket, user=submitter)

                order = CreateInvoiceBasket(payment_method='other', system=settings.PAYMENT_SYSTEM_PREFIX).create_invoice_and_order(basket, 0, None, None, user=request.user, invoice_text='Application Fee')
                invoice = Invoice.objects.get(order_number=order.number)
                fee_inv, created = ApplicationFeeInvoice.objects.get_or_create(application_fee=application_fee, invoice_reference=invoice.reference)

                if fee_inv:
                    application_fee.payment_type = ApplicationFee.PAYMENT_TYPE_ZERO
                    application_fee.expiry_time = None

                    proposal = proposal_submit(proposal, request)
                    if proposal and (invoice.payment_status == 'paid' or invoice.payment_status == 'over_paid'):
                        proposal.fee_invoice_reference = invoice.reference
                        proposal.save()
                        proposal.reset_application_discount(request.user)
                    else:
                        logger.error('Invoice payment status is {}'.format(invoice.payment_status))
                        raise

                    application_fee.save()
                    request.session['cols_last_app_invoice'] = application_fee.id
                    delete_session_application_invoice(request.session)

                    send_application_fee_invoice_tclass_email_notification(request, proposal, invoice, recipients=[recipient])

                context = {
                    'proposal': proposal,
                    'submitter': submitter,
                    'fee_invoice': fee_inv,

                    'basket': basket,
                    'lines': request.basket.lines.all(),
                    'line_details': 'N/A', #request.POST['payment'],
                    'proposal_id': proposal.id,
                    'payment_method': 'N/A',
                }

                return render(request, self.template_name, context)
            else:
                return HttpResponseRedirect(reverse('home'))

        except Exception as e:
            return redirect('home')