Created
March 15, 2016 19:15
-
-
Save Verurteilt/ae9cda3f6ceca7a77baa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import base64 | |
import six | |
from decimal import Decimal | |
from django.conf import settings | |
from django.contrib.auth.models import User | |
from django.db import models | |
from Simoba.bancos.models import Moneda | |
from SistemaV2.institucion.models import Campus | |
#========================================= MANAGERS | |
class CuentaPaypalManager(models.Manager): | |
def obtener_cuenta_paypal(self, **kwargs): | |
return self.model.objects.select_related('cat_tipoentorno', 'cat_apipaypal').get( | |
cat_tipoentorno=settings.ENTORNO_PAYPAL_ID, | |
habilitado=True, | |
**kwargs | |
) | |
class CuentaPayPalHabilitadosManager(models.Manager): # habilitados | |
def get_query_set(self): | |
return super(CuentaPayPalHabilitadosManager, self).get_query_set().filter(habilitado=True) | |
class CuentaPayPalEntornoManager(CuentaPayPalHabilitadosManager): # de_entorno | |
def get_query_set(self): | |
return super(CuentaPayPalEntornoManager, self).get_query_set().filter(cat_tipoentorno=settings.ENTORNO_PAYPAL_ID) | |
class CatTipoSuscripcionPayPalHabilitadosManager(models.Manager): # habilitados | |
def get_query_set(self): | |
return super(CatTipoSuscripcionPayPalHabilitadosManager, self).get_query_set().filter(habilitado=True) | |
class SubscriptionPayPalHabilitadosManager(models.Manager): # habilitados | |
def get_query_set(self): | |
return super(SubscriptionPayPalHabilitadosManager, self).get_query_set().filter(habilitado=True) | |
class CatTipoEntorno(models.Model): | |
nombre = models.CharField(max_length=100) | |
class CatOperacionesAPIPaypal(models.Model): | |
metodo = models.CharField(max_length=80, unique=True) | |
class CatAPIPaypal(models.Model): | |
url = models.URLField() | |
url_paypal = models.URLField() | |
#url_redirect = models.URLField() | |
version = models.FloatField() | |
cat_tipoentorno = models.ForeignKey(CatTipoEntorno) | |
operaciones = models.ManyToManyField(CatOperacionesAPIPaypal, through='OpcionesAPIPaypalOperaciones') | |
class OpcionesAPIPaypalOperaciones(models.Model): | |
catapipaypal = models.ForeignKey(CatAPIPaypal) | |
catoperacionesapipaypal = models.ForeignKey(CatOperacionesAPIPaypal) | |
request_field = models.CharField(max_length=80) | |
value = models.CharField(max_length=2048) | |
habilitado = models.BooleanField(default=False) | |
class Meta: | |
unique_together = ('catapipaypal', 'catoperacionesapipaypal', 'request_field') | |
class CuentaPaypal(models.Model): | |
nombre = models.CharField(max_length=100) | |
apiusername = models.EmailField() | |
_apipassword = models.TextField(db_column='apipassword') | |
_apisignature = models.TextField(db_column='apisignature') | |
cat_apipaypal = models.ForeignKey(CatAPIPaypal) | |
habilitado = models.BooleanField(default=False) | |
cat_tipoentorno = models.ForeignKey(CatTipoEntorno) | |
receiver_email = models.EmailField(unique=True) | |
merchant_id = models.CharField(max_length=18, unique=True) | |
objects = CuentaPaypalManager() | |
habilitados = CuentaPayPalHabilitadosManager() | |
de_entorno = CuentaPayPalEntornoManager() | |
cuenta_banco = models.ForeignKey('bancos.CuentaBanco') | |
# class Meta: | |
# unique_together = ('cat_apipaypal', 'cuenta_banco') | |
def get_apipassword(self): | |
return base64.decodestring(self._apipassword) | |
def set_apipassword(self, apipassword): | |
self._apipassword = base64.encodestring(apipassword) | |
apipassword = property(get_apipassword, set_apipassword) | |
def get_apisignature(self): | |
return base64.decodestring(self._apisignature) | |
def set_apisignature(self, apisignature): | |
self._apisignature = base64.encodestring(apisignature) | |
apisignature = property(get_apisignature, set_apisignature) | |
class EstadosPayPal(models.Model): | |
estado = models.CharField(max_length=40) | |
class TransaccionPayPal(models.Model): | |
TRANS_PENDIENTE = 1 | |
TRANS_PROCESADA = 2 | |
TRANS_FALLIDA = 6 | |
# PayPal IPN | |
TRANS_IPN_RECIBIDO = 3 | |
TRANS_IPN_VERIFICADA = 4 | |
TRANS_IPN_INVALIDO = 5 | |
# Tipos de transaccion PayPal | |
TRANS_SUBSCR_PAYMENT = 'subscr_payment' | |
TRANS_SUBSCR_PAYMENT2 = 'subscrpayment' | |
TRANS_REFERENCE_TRANSACTION = 'merchtpmt' | |
TRANS_REFERENCE_TRANSACTION2 = 'merch_pmt' | |
TRANS_SUBSCR_SIGNUP = 'subscr_signup' | |
TRANS_NO_EXPRESS_CHECKOUT = ('send_money', TRANS_SUBSCR_PAYMENT, TRANS_SUBSCR_PAYMENT2, TRANS_REFERENCE_TRANSACTION) | |
transactionid = models.CharField(max_length=19, primary_key=True) | |
matricula = models.CharField(max_length=50) | |
amt = models.DecimalField(max_digits=14, decimal_places=2) | |
fee = models.DecimalField(max_digits=14, decimal_places=2) | |
tax = models.DecimalField(max_digits=14, decimal_places=2) | |
amount_received_nett = models.DecimalField(max_digits=14, decimal_places=2) | |
cat_estado = models.ForeignKey(EstadosPayPal) | |
cuentapaypal = models.ForeignKey(CuentaPaypal) | |
fecha = models.DateTimeField(auto_now_add=True) | |
producto = models.CharField(max_length=100, null=True) | |
campus = models.CharField(max_length=100, null=True) | |
nivel = models.CharField(max_length=100, null=True) | |
licenciatura = models.CharField(max_length=100, null=True) | |
matricula = models.CharField(max_length=100, null=True) | |
referencia_pago = models.CharField(max_length=100, null=True) | |
producto_servicio = models.CharField(max_length=100, null=True) | |
fee = models.DecimalField(max_digits=10, decimal_places=2) | |
procesado = models.BooleanField(default=False) | |
extra_info = models.TextField(null=True) | |
periodo = models.CharField(max_length=100, null=True) | |
def es_procesada(self): | |
return self.cat_estado_id == self.TRANS_PROCESADA | |
def save(self, *args, **kwargs): | |
from decimal import Decimal | |
self.amount_received_nett = Decimal(self.amt) - Decimal(self.fee) - Decimal(self.tax) | |
super(TransaccionPayPal, self).save(*args, **kwargs) | |
def __str__(self): | |
return self.transactionid | |
class CatTipoSuscripcionPayPal(models.Model): | |
nombre = models.CharField(max_length=80) | |
clave = models.CharField(max_length=30, unique=True) | |
habilitado = models.BooleanField(default=True) | |
objects = models.Manager() | |
habilitados = CatTipoSuscripcionPayPalHabilitadosManager() | |
class SubscriptionPayPal(models.Model): | |
subscriptionid = models.CharField(max_length=19, primary_key=True) | |
matricula = models.CharField(max_length=125) | |
fecha_suscripcion = models.DateTimeField() | |
cuentapaypal = models.ForeignKey(CuentaPaypal) | |
campus = models.CharField(max_length=100, null=True) | |
nivel = models.CharField(max_length=100, null=True) | |
licenciatura = models.CharField(max_length=100, null=True) | |
tipo_suscripcion = models.ForeignKey(CatTipoSuscripcionPayPal) | |
num_pagos = models.IntegerField(default=0) | |
fecha_pp = models.DateField(null=True) # fecha primer pago | |
habilitado = models.BooleanField(default=True) | |
dia_cobro = models.IntegerField(null=True) | |
monto = models.DecimalField(max_digits=14, decimal_places=2, default=0) | |
moneda = models.ForeignKey(Moneda) | |
objects = models.Manager() | |
habilitados = SubscriptionPayPalHabilitadosManager() | |
referencia = models.CharField(max_length=100, null=True) | |
fecha_creacion = property(lambda self: self.fecha_suscripcion) | |
# class Meta: | |
# unique_together = ('subscriptionid', 'usuario', 'licenciatura') | |
class SubscriptionTransaccionPayPal(models.Model): | |
transactionid = models.ForeignKey(TransaccionPayPal) | |
subscriptionid = models.ForeignKey(SubscriptionPayPal) | |
creado = models.DateTimeField(auto_now_add=True) | |
class Meta: | |
unique_together = ('transactionid', 'subscriptionid') | |
# class ModalidadCuentaPaypal(models.Model): | |
# cuentapaypal = models.ForeignKey(CuentaPaypal) | |
# programa = models.CharField(max_length=100) | |
# moneda = models.ForeignKey('General.Moneda') | |
# campus = models.CharField(max_length=100) | |
# nivel = models.CharField(max_length=100) | |
# #test = models.BooleanField(default=True) | |
class DescuentosDomiciliacion(models.Model): | |
codigo_banner_descuento = models.CharField(max_length=5) | |
codigo_banner_aplicativo = models.CharField(max_length=5) | |
campus = models.ForeignKey(Campus) | |
habilitado = models.BooleanField(default=True) | |
clacon_banco = models.ForeignKey('bancos.ClaconBanco') | |
class DatosEnviadosPayPal(models.Model): | |
fecha_envio = models.DateTimeField(auto_now_add=True) | |
status = models.CharField(null=True, max_length=50) | |
ba = models.CharField(max_length=100) | |
error = models.TextField(null=True) | |
class Dom(object): | |
token = None | |
creado = None | |
habilitado = None | |
institucion = None | |
dia_pago = 0 | |
se_puede_eliminar = False | |
def obtener_domiciliaciones_alumno(usuario): | |
from Simoba.payu.models import TokenPayu | |
domiciliaciones =[] | |
dom_payu = TokenPayu.objects.filter(alumno=usuario) | |
dom_paypal = SubscriptionPayPal.objects.filter(matricula=usuario.username) | |
for d in dom_payu: | |
dom = Dom() | |
dom.token = d.token | |
dom.creado = d.created | |
dom.habilitado = d.habilitado | |
dom.institucion = "Payu" | |
dom.se_puede_eliminar = d.habilitado | |
dom.dia_pago = d.payment_day | |
domiciliaciones.append(dom) | |
for d in dom_paypal: | |
dom = Dom() | |
dom.token = d.pk | |
dom.creado = d.fecha_suscripcion | |
dom.habilitado = d.habilitado | |
dom.institucion = "PayPal" | |
dom.se_puede_eliminar = d.habilitado | |
dom.dia_pago = d.dia_cobro | |
domiciliaciones.append(dom) | |
return domiciliaciones | |
def alumno_es_domiciliado(usuario): | |
doms = usuario.obtener_domiciliaciones_alumno() | |
for d in doms: | |
if d.habilitado: | |
return True | |
return False | |
def obtener_rango_domiciliacion_alumno_dias(usuario): | |
from Simoba.bancos.models import ConfiguracionDomiciliacion | |
insc = usuario.datosusuario.inscripciondelestudiante_set.filter().order_by('periodo').last() | |
a_o_b_15_o_30 = insc.plan_descuentos.rate[-2] | |
conf = ConfiguracionDomiciliacion.objects.first() | |
dia = conf.corte_uno if a_o_b_15_o_30 == "A" else conf.corte_dos #Regla establecida por Omar Aguilar | |
return dia | |
def metodos_puede_domiciliar(usuario): | |
inscripcion_estudiante = usuario.obtener_inscripciones_alumno() | |
puede_pagar_en_linea = inscripcion_estudiante.puede_pagar_en_linea() | |
return puede_pagar_en_linea | |
def alumno_puede_domiciliar(usuario): | |
if usuario.alumno_es_domiciliado(): | |
return False | |
return not Decimal(usuario.datosusuario.get_overdue_balance()) | |
User.add_to_class('obtener_domiciliaciones_alumno', obtener_domiciliaciones_alumno) | |
User.add_to_class('alumno_es_domiciliado', alumno_es_domiciliado) | |
User.add_to_class('obtener_rango_domiciliacion_alumno_dias', obtener_rango_domiciliacion_alumno_dias) | |
User.add_to_class('metodos_puede_domiciliar', metodos_puede_domiciliar) | |
User.add_to_class('alumno_puede_domiciliar', alumno_puede_domiciliar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment