def buy_with_bank(): course_id = request.args.get('course') if course_id == None: return redirect(url_for('course.index')) bank_form = EnrollBankForm() cc_form = EnrollCreditCardForm() course_id = int(course_id) user = User(current_user) courses = [] courses.append(course_id) with Transaction().set_context(courses=courses): user = User(current_user) if user.active_membership: return redirect(url_for('course.index')) courses = Course.search([('id', '=', course_id)], limit=1) if len(courses) == 1: course, = courses if bank_form.bank_membership.data == 'MONTHLY': service_id = course.service.id else: service_id = course.yearly_service.id service = Service(service_id) if course: web_course = course ip_address = request.remote_addr if not ip_address or ip_address == '127.0.0.1': ip_address = '186.189.195.217' if ipaddress.ip_address(ip_address).is_private: ip_address = '186.189.195.217' access_token = 'cda5ddffc1e6b7' handler = ipinfo.getHandler(access_token) details = handler.getDetails(ip_address) country_code = details.country country_code = str(country_code) lcase_country_code = country_code.lower() currency_id = 16 for currency in data: if str(currency['code']) == country_code: currency_id = currency['id'] break currency = Currency(currency_id) if bank_form.validate_on_submit(): party = user.party today = date.today() subscriptions = Subscription.search([ ('party', '=', party.id), ('state', '=', 'running'), ('lines.service', '=', service_id), ]) if len(subscriptions) >= 1: print("NO ACTIVE SUBSCRIPTIONS") return redirect(url_for('course.payment')) else: enrolment, = Product.search([('is_enrolment', '=', True)], limit=1) payment_term, = PaymentTerm.search([()], limit=1) address = user.party.addresses[0] new_subscription = Subscription.create([{ 'party': user.party.id, 'start_date': today, 'invoice_start_date': today, 'invoice_recurrence': service.consumption_recurrence, 'enrolment': enrolment.id, 'unit_price_enrolment': 0, 'invoice_address': address, 'is_online': True, 'payment_term': payment_term.id, }]) new_subscription_line, = SubscriptionLine.create([{ 'subscription': new_subscription[0].id, 'service': service.id, 'quantity': 1, 'start_date': today, 'consumption_recurrence': service.consumption_recurrence, 'unit': 1, 'unit_price': service.product.list_price, }]) Subscription.quote(new_subscription) Subscription.run(new_subscription) SaleSubscriptionLine.generate_consumption(date=today, party=user.party) Subscription.generate_invoice(date=today, party=user.party) invoices = Invoice.search([('party', '=', user.party)], order=[('number', 'DESC')]) if len(invoices) >= 1: for invoice in invoices: if invoice.state == 'draft': Invoice.post([invoice]) current_line = None current_invoice = None for invoice in invoices: if invoice.state not in ['cancel', 'paid']: move = invoice.move for line in move.lines: if line.debit > 0: current_line = MoveLine(line.id) current_invoice = invoice break if not current_line or not current_invoice: return redirect(url_for('main.welcome')) def _group_payment_key(payment): return (('journal', payment.journal.id), ('kind', payment.kind)) def _new_group(values): return PaymentGroup(**values) invoice = current_invoice amount = current_invoice.total_amount ticket = bank_form.ticket.data journal, = Journal.search([('name', '=', 'DCOMPENSA')], limit=1) new_payment = Payment.create([{ 'journal': journal.id, 'kind': 'receivable', 'party': party.id, 'line': current_line.id, 'description': ticket, 'amount': current_line.debit, }]) Payment.approve(new_payment) payment, = new_payment groups = [] new_payment = sorted(new_payment, key=_group_payment_key) for key, grouped_payments in groupby(new_payment, key=_group_payment_key): def group(): group = _new_group(dict(key)) group.save() groups.append(group) return group Payment.process(list(grouped_payments), group) body = "Hemos recibido la siguiente informacion: " + \ "Nombre: "+ party.name + \ " \n Ticket: " + ticket + \ " \n Usuario: " + user.email msg = Message('Usuario Web API: '+ party.name, \ sender = '*****@*****.**', \ recipients = ['*****@*****.**']) msg.body = "Usuario Web API " + body mail.send(msg) return redirect(url_for('main.welcome')) return render_template('course/enroll.html', cc_form=cc_form, bank_form=bank_form, user=user, web_course=course, currency=currency)
def pay_with_bank(): user = User(current_user) party = user.party today = date.today() courses = [] subscriptions = Subscription.search([('is_online', '=', True), ('party', '=', party.id), ('state', '=', 'running')]) if len(subscriptions) < 1: return render_template( 'bienvenido.html', user=user, flash_message= "Something wrong. Sorry the inconvenient. We will call you later.") else: for subscription in subscriptions: for line in subscription.lines: courses.append(line.service.id) with Transaction().set_context(courses=courses): if user.active_membership: return redirect(url_for('course.index')) ip_address = request.remote_addr if not ip_address or ip_address == '127.0.0.1': ip_address = '186.189.195.217' if ipaddress.ip_address(ip_address).is_private: ip_address = '186.189.195.217' access_token = 'cda5ddffc1e6b7' handler = ipinfo.getHandler(access_token) details = handler.getDetails(ip_address) country_code = details.country country_code = str(country_code) lcase_country_code = country_code.lower() currency_id = 16 for currency in data: if str(currency['code']) == country_code: currency_id = currency['id'] break currency = Currency(currency_id) subscriptions = Subscription.search([('party', '=', party.id), ('is_online', '=', True), ('state', '=', 'running')]) invoices = Invoice.search([ ('party', '=', party.id), ('state', '=', 'posted'), ], limit=1) current_line = None for invoice in invoices: move = invoice.move lines = move.lines for line in lines: if line.debit > 0: current_line = MoveLine(line.id) break if current_line is not None: enroll_form = EnrollRegistrationForm(invoice=current_line.description, amount=current_line.debit) if len(subscriptions) >= 1 and len(invoices) >= 1: if enroll_form.validate_on_submit(): def _group_payment_key(payment): return (('journal', payment.journal.id), ('kind', payment.kind)) def _new_group(values): return PaymentGroup(**values) invoice = enroll_form.invoice.data amount = enroll_form.amount.data ticket = enroll_form.ticket.data if not current_line: return redirect(url_for('main.welcome')) journal, = Journal.search([('name', '=', 'DCOMPENSA')], limit=1) new_payment = Payment.create([{ 'journal': journal.id, 'kind': 'receivable', 'party': party.id, 'line': current_line.id, 'description': ticket, 'amount': current_line.debit, }]) Payment.approve(new_payment) payment, = new_payment groups = [] new_payment = sorted(new_payment, key=_group_payment_key) for key, grouped_payments in groupby(new_payment, key=_group_payment_key): def group(): group = _new_group(dict(key)) group.save() groups.append(group) return group Payment.process(list(grouped_payments), group) body = "Hemos recibido la siguiente informacion: " + \ "Nombre: "+ party.name + \ " \n Ticket: " + ticket + \ " \n Usuario: " + user.email msg = Message('Usuario Web API: '+ party.name, \ sender = '*****@*****.**', \ recipients = ['*****@*****.**']) msg.body = "Usuario Web API " + body mail.send(msg) return redirect(url_for('main.welcome')) return render_template('course/pay_enroll_with_bank.html', user=user, enroll_form=enroll_form, invoices=invoices, subscriptions=subscriptions, currency=currency) else: return redirect(url_for('course.index'))