refactor qrcode pour product & price
This commit is contained in:
parent
cd80c74f90
commit
1ddbec4b05
|
|
@ -120,7 +120,6 @@ class ConfigurationAdmin(SingletonModelAdmin):
|
||||||
('Adhésions', {
|
('Adhésions', {
|
||||||
'fields': (
|
'fields': (
|
||||||
'adhesion_obligatoire',
|
'adhesion_obligatoire',
|
||||||
'cadeau_adhesion',
|
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
('Paiements', {
|
('Paiements', {
|
||||||
|
|
|
||||||
|
|
@ -157,7 +157,6 @@ class Product(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name}"
|
return f"{self.name}"
|
||||||
|
|
||||||
'''
|
|
||||||
def get_id_product_stripe(self):
|
def get_id_product_stripe(self):
|
||||||
configuration = Configuration.get_solo()
|
configuration = Configuration.get_solo()
|
||||||
if configuration.stripe_api_key and not self.id_product_stripe:
|
if configuration.stripe_api_key and not self.id_product_stripe:
|
||||||
|
|
@ -180,6 +179,7 @@ class Product(models.Model):
|
||||||
)
|
)
|
||||||
self.id_product_stripe = product.id
|
self.id_product_stripe = product.id
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
return self.id_product_stripe
|
return self.id_product_stripe
|
||||||
|
|
||||||
elif self.id_product_stripe:
|
elif self.id_product_stripe:
|
||||||
|
|
@ -187,35 +187,9 @@ class Product(models.Model):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_id_price_stripe(self):
|
|
||||||
configuration = Configuration.get_solo()
|
|
||||||
if configuration.stripe_api_key and not self.id_price_stripe:
|
|
||||||
if configuration.stripe_mode_test:
|
|
||||||
stripe.api_key = configuration.stripe_test_api_key
|
|
||||||
else:
|
|
||||||
stripe.api_key = configuration.stripe_api_key
|
|
||||||
|
|
||||||
price = stripe.Price.create(
|
|
||||||
unit_amount=int("{0:.2f}".format(self.prix).replace('.', '')),
|
|
||||||
currency="eur",
|
|
||||||
product=self.get_id_product_stripe(),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.id_price_stripe = price.id
|
|
||||||
self.save()
|
|
||||||
return self.id_price_stripe
|
|
||||||
|
|
||||||
elif self.id_price_stripe:
|
|
||||||
return self.id_price_stripe
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def reset_id_stripe(self):
|
def reset_id_stripe(self):
|
||||||
self.id_price_stripe = None
|
|
||||||
self.id_product_stripe = None
|
self.id_product_stripe = None
|
||||||
self.save()
|
self.save()
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
class Price(models.Model):
|
class Price(models.Model):
|
||||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True, db_index=True)
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True, db_index=True)
|
||||||
|
|
@ -247,7 +221,35 @@ class Price(models.Model):
|
||||||
return range(self.max_per_user + 1)
|
return range(self.max_per_user + 1)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name}"
|
return f"{self.product.name} {self.name}"
|
||||||
|
|
||||||
|
|
||||||
|
def get_id_price_stripe(self):
|
||||||
|
configuration = Configuration.get_solo()
|
||||||
|
if configuration.stripe_api_key and not self.id_price_stripe:
|
||||||
|
if configuration.stripe_mode_test:
|
||||||
|
stripe.api_key = configuration.stripe_test_api_key
|
||||||
|
else:
|
||||||
|
stripe.api_key = configuration.stripe_api_key
|
||||||
|
|
||||||
|
price = stripe.Price.create(
|
||||||
|
unit_amount=int("{0:.2f}".format(self.prix).replace('.', '')),
|
||||||
|
currency="eur",
|
||||||
|
product=self.product.get_id_product_stripe(),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.id_price_stripe = price.id
|
||||||
|
self.save()
|
||||||
|
return self.id_price_stripe
|
||||||
|
|
||||||
|
elif self.id_price_stripe:
|
||||||
|
return self.id_price_stripe
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def reset_id_stripe(self):
|
||||||
|
self.id_price_stripe = None
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
class Event(models.Model):
|
class Event(models.Model):
|
||||||
|
|
@ -366,7 +368,8 @@ class LigneArticle(models.Model):
|
||||||
uuid = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4)
|
uuid = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4)
|
||||||
datetime = models.DateTimeField(auto_now=True)
|
datetime = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True)
|
price = models.ForeignKey(Price, on_delete=models.CASCADE)
|
||||||
|
|
||||||
qty = models.SmallIntegerField()
|
qty = models.SmallIntegerField()
|
||||||
|
|
||||||
reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE, blank=True, null=True)
|
reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE, blank=True, null=True)
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ class creation_paiement_stripe():
|
||||||
def _total(self):
|
def _total(self):
|
||||||
total = 0
|
total = 0
|
||||||
for ligne in self.liste_ligne_article:
|
for ligne in self.liste_ligne_article:
|
||||||
total += float(ligne.qty) * float(ligne.product.prix)
|
total += float(ligne.qty) * float(ligne.price.prix)
|
||||||
return total
|
return total
|
||||||
|
|
||||||
def _paiement_stripe_db(self):
|
def _paiement_stripe_db(self):
|
||||||
|
|
@ -96,7 +96,7 @@ class creation_paiement_stripe():
|
||||||
ligne: LigneArticle
|
ligne: LigneArticle
|
||||||
line_items.append(
|
line_items.append(
|
||||||
{
|
{
|
||||||
"price": f"{ligne.product.get_id_price_stripe()}",
|
"price": f"{ligne.price.get_id_price_stripe()}",
|
||||||
"quantity": int(ligne.qty),
|
"quantity": int(ligne.qty),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -264,7 +264,7 @@
|
||||||
{% for tarif in tarifs_adhesion %}
|
{% for tarif in tarifs_adhesion %}
|
||||||
<ul class="actions">
|
<ul class="actions">
|
||||||
<li><a id="adh_{{ tarif.name | slugify }}" href="#paiementadhesionenligne"
|
<li><a id="adh_{{ tarif.name | slugify }}" href="#paiementadhesionenligne"
|
||||||
class="button primary field">{{ tarif.name }} {{ tarif.prix | floatformat:2 | intcomma }}€</a>
|
class="button primary field">{{ tarif.product.name }} {{ tarif.name }} {{ tarif.prix | floatformat:2 | intcomma }}€</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ from rest_framework.generics import get_object_or_404
|
||||||
from django.views import View
|
from django.views import View
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
from BaseBillet.models import Configuration, Product, LigneArticle
|
from BaseBillet.models import Configuration, Product, LigneArticle, Price
|
||||||
from PaiementStripe.models import Paiement_stripe
|
from PaiementStripe.models import Paiement_stripe
|
||||||
from PaiementStripe.views import creation_paiement_stripe
|
from PaiementStripe.views import creation_paiement_stripe
|
||||||
from QrcodeCashless.models import CarteCashless
|
from QrcodeCashless.models import CarteCashless
|
||||||
|
|
@ -19,6 +19,7 @@ from django.db.models.signals import pre_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -114,7 +115,7 @@ class index_scan(View):
|
||||||
request,
|
request,
|
||||||
self.template_name,
|
self.template_name,
|
||||||
{
|
{
|
||||||
'tarifs_adhesion': Product.objects.filter(categorie_article=Product.ADHESION).order_by('-prix'),
|
'tarifs_adhesion': Price.objects.filter(product__categorie_article=Product.ADHESION),
|
||||||
'adhesion_obligatoire': configuration.adhesion_obligatoire,
|
'adhesion_obligatoire': configuration.adhesion_obligatoire,
|
||||||
'history': json_reponse.get('history'),
|
'history': json_reponse.get('history'),
|
||||||
'carte_resto': configuration.carte_restaurant,
|
'carte_resto': configuration.carte_restaurant,
|
||||||
|
|
@ -143,7 +144,7 @@ class index_scan(View):
|
||||||
return HttpResponse('Forbidden', status=status.HTTP_403_FORBIDDEN)
|
return HttpResponse('Forbidden', status=status.HTTP_403_FORBIDDEN)
|
||||||
else:
|
else:
|
||||||
return HttpResponse("Serveur non disponible. Merci de revenir ultérieurement.",
|
return HttpResponse("Serveur non disponible. Merci de revenir ultérieurement.",
|
||||||
status=status.HTTP_503_SERVICE_UNAVAILABLE)
|
status=status.HTTP_503_SERVICE_UNAVAILABLE)
|
||||||
|
|
||||||
def post(self, request, uuid):
|
def post(self, request, uuid):
|
||||||
carte = check_carte_local(uuid)
|
carte = check_carte_local(uuid)
|
||||||
|
|
@ -157,37 +158,43 @@ class index_scan(View):
|
||||||
montant_recharge = data.get('montant_recharge')
|
montant_recharge = data.get('montant_recharge')
|
||||||
|
|
||||||
# c'est un paiement
|
# c'est un paiement
|
||||||
if ( pk_adhesion or montant_recharge ) and data.get('email'):
|
if (pk_adhesion or montant_recharge) and data.get('email'):
|
||||||
# montant_recharge = data.get('montant_recharge')
|
# montant_recharge = data.get('montant_recharge')
|
||||||
ligne_articles = []
|
ligne_articles = []
|
||||||
metadata = {}
|
metadata = {}
|
||||||
metadata['recharge_carte_uuid'] = str(carte.uuid)
|
metadata['recharge_carte_uuid'] = str(carte.uuid)
|
||||||
|
|
||||||
if montant_recharge:
|
if montant_recharge:
|
||||||
art, created = Product.objects.get_or_create(
|
product, created = Product.objects.get_or_create(
|
||||||
name="Recharge Carte",
|
name=f"Recharge Carte {carte.detail.origine.name} v{carte.detail.generation}",
|
||||||
prix=1,
|
|
||||||
categorie_article=Product.RECHARGE_CASHLESS,
|
categorie_article=Product.RECHARGE_CASHLESS,
|
||||||
|
img=carte.detail.img,
|
||||||
|
)
|
||||||
|
|
||||||
|
price, created = Price.objects.get_or_create(
|
||||||
|
product=product,
|
||||||
|
name=f"{montant_recharge}€",
|
||||||
|
prix=int(montant_recharge),
|
||||||
)
|
)
|
||||||
|
|
||||||
ligne_article_recharge = LigneArticle.objects.create(
|
ligne_article_recharge = LigneArticle.objects.create(
|
||||||
article=art,
|
price=price,
|
||||||
qty=montant_recharge,
|
|
||||||
carte=carte,
|
|
||||||
)
|
|
||||||
ligne_articles.append(ligne_article_recharge)
|
|
||||||
|
|
||||||
|
|
||||||
metadata['recharge_carte_montant'] = str(montant_recharge)
|
|
||||||
|
|
||||||
if pk_adhesion:
|
|
||||||
art_adhesion = Product.objects.get(pk=data.get('pk_adhesion'))
|
|
||||||
ligne_article_recharge = LigneArticle.objects.create(
|
|
||||||
article=art_adhesion,
|
|
||||||
qty=1,
|
qty=1,
|
||||||
carte=carte,
|
carte=carte,
|
||||||
)
|
)
|
||||||
ligne_articles.append(ligne_article_recharge)
|
ligne_articles.append(ligne_article_recharge)
|
||||||
metadata['pk_adhesion'] = str(art_adhesion.pk)
|
|
||||||
|
metadata['recharge_carte_montant'] = str(montant_recharge)
|
||||||
|
|
||||||
|
if pk_adhesion:
|
||||||
|
price_adhesion = Price.objects.get(pk=data.get('pk_adhesion'))
|
||||||
|
ligne_article_recharge = LigneArticle.objects.create(
|
||||||
|
price=price_adhesion,
|
||||||
|
qty=1,
|
||||||
|
carte=carte,
|
||||||
|
)
|
||||||
|
ligne_articles.append(ligne_article_recharge)
|
||||||
|
metadata['pk_adhesion'] = str(price_adhesion.pk)
|
||||||
|
|
||||||
if len(ligne_articles) > 0:
|
if len(ligne_articles) > 0:
|
||||||
new_paiement_stripe = creation_paiement_stripe(
|
new_paiement_stripe = creation_paiement_stripe(
|
||||||
|
|
@ -262,17 +269,17 @@ def changement_paid_to_valid(sender, instance: Paiement_stripe, update_fields=No
|
||||||
data_pour_serveur_cashless = {'uuid_commande': paiementStripe.uuid}
|
data_pour_serveur_cashless = {'uuid_commande': paiementStripe.uuid}
|
||||||
|
|
||||||
for ligne_article in paiementStripe.lignearticle_set.all():
|
for ligne_article in paiementStripe.lignearticle_set.all():
|
||||||
if ligne_article.carte :
|
if ligne_article.carte:
|
||||||
data_pour_serveur_cashless['uuid'] = ligne_article.carte.uuid
|
data_pour_serveur_cashless['uuid'] = ligne_article.carte.uuid
|
||||||
|
|
||||||
if ligne_article.product.categorie_article == Product.RECHARGE_CASHLESS :
|
if ligne_article.price.product.categorie_article == Product.RECHARGE_CASHLESS:
|
||||||
data_pour_serveur_cashless['recharge_qty'] = float(ligne_article.qty)
|
data_pour_serveur_cashless['recharge_qty'] = ligne_article.price.prix
|
||||||
|
|
||||||
if ligne_article.product.categorie_article == Product.ADHESION :
|
if ligne_article.price.product.categorie_article == Product.ADHESION:
|
||||||
data_pour_serveur_cashless['tarif_adhesion'] = ligne_article.product.prix
|
data_pour_serveur_cashless['tarif_adhesion'] = ligne_article.price.prix
|
||||||
|
|
||||||
# si il y a autre chose que uuid_commande :
|
# si il y a autre chose que uuid_commande :
|
||||||
if len(data_pour_serveur_cashless) > 1 :
|
if len(data_pour_serveur_cashless) > 1:
|
||||||
sess = requests.Session()
|
sess = requests.Session()
|
||||||
configuration = Configuration.get_solo()
|
configuration = Configuration.get_solo()
|
||||||
r = sess.post(
|
r = sess.post(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue