Ejemplo n.º 1
0
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()

        min_value = configuration_get(name='MIN_PRICE_LYDIA').get_value()
        kwargs['min_value'] = decimal.Decimal(min_value)

        max_value = configuration_get(name='MAX_PRICE_LYDIA').get_value()
        if max_value == 0:
            kwargs['max_value'] = None
        else:
            kwargs['max_value'] = decimal.Decimal(max_value)

        return kwargs
Ejemplo n.º 2
0
    def form_query(self, query):
        if self.search:
            query = query.filter(
                Q(last_name__icontains=self.search)
                | Q(first_name__icontains=self.search)
                | Q(surname__icontains=self.search)
                | Q(username__icontains=self.search))

        if self.year and self.year != 'all':
            query = query.filter(year=self.year)

        if self.state and self.state != 'all':
            if self.state == 'internals':
                query = query.exclude(groups=get_members_group(
                    is_externals=True)).filter(is_active=True)
            if self.state == 'negative_balance':
                query = query.filter(balance__lt=0.0, is_active=True)
            elif self.state == 'threshold':
                threshold = configuration_get(
                    'BALANCE_THRESHOLD_PURCHASE').get_value()
                query = query.filter(balance__lt=threshold, is_active=True)
            elif self.state == 'unactive':
                query = query.filter(is_active=False)
        else:
            query = query.filter(is_active=True)

        return query
Ejemplo n.º 3
0
 def get_initial(self):
     initial = super().get_initial()
     initial['balance_threshold_purchase'] = configuration_get(
         'BALANCE_THRESHOLD_PURCHASE').get_value()
     #initial['balance_threshold_mail_alert'] = configuration_get('BALANCE_THRESHOLD_MAIL_ALERT').get_value()
     #initial['balance_frequency_mail_alert'] = configuration_get('BALANCE_FREQUENCY_MAIL_ALERT').get_value()
     return initial
Ejemplo n.º 4
0
 def form_valid(self, form):
     # Enable
     enable_self_lydia = configuration_get('ENABLE_SELF_LYDIA')
     enable_self_lydia.value = form.cleaned_data['enable_self_lydia']
     enable_self_lydia.save()
     # Lydia min price
     min_price_lydia = configuration_get('MIN_PRICE_LYDIA')
     min_price_lydia.value = form.cleaned_data['min_price_lydia']
     min_price_lydia.save()
     # Lydia max price
     max_price_lydia = configuration_get('MAX_PRICE_LYDIA')
     if not form.cleaned_data['max_price_lydia']:
         max_price_lydia.value = ''
     else:
         max_price_lydia.value = form.cleaned_data['max_price_lydia']
     max_price_lydia.save()
     # Enable fee
     enable_fee_lydia = configuration_get('ENABLE_FEE_LYDIA')
     enable_fee_lydia.value = form.cleaned_data['enable_fee_lydia']
     enable_fee_lydia.save()
     # Base fee
     base_fee_lydia = configuration_get('BASE_FEE_LYDIA')
     if not form.cleaned_data['base_fee_lydia']:
         base_fee_lydia.value = ''
     else:
         base_fee_lydia.value = form.cleaned_data['base_fee_lydia']
     base_fee_lydia.save()
     # Ratio fee
     ratio_fee_lydia = configuration_get('RATIO_FEE_LYDIA')
     if not form.cleaned_data['ratio_fee_lydia']:
         ratio_fee_lydia.value = ''
     else:
         ratio_fee_lydia.value = form.cleaned_data['ratio_fee_lydia']
     ratio_fee_lydia.save()
     # Tax fee
     tax_fee_lydia = configuration_get('TAX_FEE_LYDIA')
     if not form.cleaned_data['tax_fee_lydia']:
         tax_fee_lydia.value = ''
     else:
         tax_fee_lydia.value = form.cleaned_data['tax_fee_lydia']
     tax_fee_lydia.save()
     # Lydia api token
     api_token_lydia = configuration_get('API_TOKEN_LYDIA')
     api_token_lydia.value = form.cleaned_data['api_token_lydia']
     api_token_lydia.save()
     # Lydia vendor token
     vendor_token_lydia = configuration_get('VENDOR_TOKEN_LYDIA')
     vendor_token_lydia.value = form.cleaned_data['vendor_token_lydia']
     vendor_token_lydia.save()
     return super().form_valid(form)
Ejemplo n.º 5
0
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['module_class'] = self.module_class
        kwargs['module'] = self.module
        kwargs['balance_threshold_purchase'] = configuration_get(
            'BALANCE_THRESHOLD_PURCHASE')

        if self.module_class == "self_sales":
            kwargs['client'] = self.request.user
        elif self.module_class == "operator_sales":
            kwargs['client'] = None
        else:
            self.handle_unexpected_module_class()
        return kwargs
Ejemplo n.º 6
0
 def get_initial(self):
     initial = super().get_initial()
     initial['enable_self_lydia'] = configuration_get(
         'ENABLE_SELF_LYDIA').get_value()
     initial['min_price_lydia'] = configuration_get(
         'MIN_PRICE_LYDIA').get_value()
     initial['max_price_lydia'] = configuration_get(
         'MAX_PRICE_LYDIA').get_value()
     initial['api_token_lydia'] = configuration_get(
         'API_TOKEN_LYDIA').get_value()
     initial['vendor_token_lydia'] = configuration_get(
         'VENDOR_TOKEN_LYDIA').get_value()
     initial['enable_fee_lydia'] = configuration_get(
         'ENABLE_FEE_LYDIA').get_value()
     initial['base_fee_lydia'] = configuration_get(
         'BASE_FEE_LYDIA').get_value()
     initial['ratio_fee_lydia'] = configuration_get(
         'RATIO_FEE_LYDIA').get_value()
     initial['tax_fee_lydia'] = configuration_get(
         'TAX_FEE_LYDIA').get_value()
     return initial
Ejemplo n.º 7
0
    def get_automatic_price(self):
        """
        Return the price calculated over the last stockentry concerning the product.
        If there is no stock entry realisated, return 0.
        """
        try:
            margin_profit = configuration_get('MARGIN_PROFIT').get_value()

            last_stockentry = self.stockentryproduct_set.order_by(
                '-stockentry__datetime').first()
            if last_stockentry is not None:
                return round(decimal.Decimal(last_stockentry.unit_price() * self.correcting_factor * decimal.Decimal(1 + margin_profit / 100)), 4)
            else:
                return 0
        except IndexError:
            return decimal.Decimal(0)
Ejemplo n.º 8
0
    def form_valid(self, form):
        """
        Save the current phone number as default phone number for the user.
        Render the Lydia button.
        """
        user = self.request.user
        if user.phone is None:
            user.phone = form.cleaned_data['tel_number']
            user.save()

        context = self.get_context_data()
        context['vendor_token'] = configuration_get(
            "VENDOR_TOKEN_LYDIA").get_value()
        context['confirm_url'] = self.request.build_absolute_uri(
            reverse('url_self_lydia_confirm'))
        context['callback_url'] = self.request.build_absolute_uri(
            reverse('url_self_lydia_callback'))
        recharging_amount = form.cleaned_data['recharging_amount']

        if self.enable_fee_lydia:
            if self.tax_fee_lydia and self.tax_fee_lydia != 0:
                total_amount = calculate_total_amount_lydia(
                    recharging_amount, self.base_fee_lydia, self.ratio_fee_lydia, self.tax_fee_lydia)
            else:
                total_amount = calculate_total_amount_lydia(
                    recharging_amount, self.base_fee_lydia, self.ratio_fee_lydia)

            fee_amount = total_amount - recharging_amount
            context['fee_amount'] = fee_amount
        else:
            total_amount = recharging_amount

        context['total_amount'] = total_amount
        context['recharging_amount'] = recharging_amount
        context['tel_number'] = form.cleaned_data['tel_number']
        context['message'] = (
            "Borgia - AE ENSAM - Crédit de "
            + user.__str__())
        return render(self.request,
                      'finances/self_lydia_button.html',
                      context=context)
Ejemplo n.º 9
0
    def form_valid(self, form):
        # balance_threshold_purchase
        balance_threshold_purchase = configuration_get(
            'BALANCE_THRESHOLD_PURCHASE')
        balance_threshold_purchase.value = form.cleaned_data[
            'balance_threshold_purchase']
        balance_threshold_purchase.save()

        # # balance_threshold_mail_alert
        # balance_threshold_mail_alert = configuration_get('BALANCE_THRESHOLD_MAIL_ALERT')
        # if not form.cleaned_data['balance_threshold_mail_alert']:
        #     balance_threshold_mail_alert.value = ''
        # else:
        #     balance_threshold_mail_alert.value = form.cleaned_data['balance_threshold_mail_alert']
        # balance_threshold_mail_alert.save()
        # # balance_frequency_mail_alert
        # balance_frequency_mail_alert = configuration_get('BALANCE_FREQUENCY_MAIL_ALERT')
        # if not form.cleaned_data['balance_frequency_mail_alert']:
        #     balance_frequency_mail_alert.value = ''
        # else:
        #     balance_frequency_mail_alert.value = form.cleaned_data['balance_frequency_mail_alert']
        # balance_frequency_mail_alert.save()

        return super().form_valid(form)
Ejemplo n.º 10
0
    def add_lydia_context(self):
        if not configuration_get("ENABLE_SELF_LYDIA").get_value():
            self.state = "disabled"
        elif configuration_get("API_TOKEN_LYDIA").get_value() in ['', 'Undefined']:
            self.state = "undefined"
        else:
            self.state = "enabled"

            self.enable_fee_lydia = configuration_get(
                name='ENABLE_FEE_LYDIA').get_value()

            if self.enable_fee_lydia:
                base_fee_lydia = configuration_get(
                    name='BASE_FEE_LYDIA').get_value()
                self.base_fee_lydia = decimal.Decimal(
                    base_fee_lydia).quantize(decimal.Decimal('.01'))
                ratio_fee_lydia = configuration_get(
                    name='RATIO_FEE_LYDIA').get_value()
                self.ratio_fee_lydia = decimal.Decimal(
                    ratio_fee_lydia).quantize(decimal.Decimal('.01'))
                tax_fee_lydia = configuration_get(
                    name='TAX_FEE_LYDIA').get_value()
                self.tax_fee_lydia = decimal.Decimal(
                    tax_fee_lydia).quantize(decimal.Decimal('.01'))
Ejemplo n.º 11
0
 def get_initial(self):
     initial = super().get_initial()
     initial['margin_profit'] = configuration_get(
         'MARGIN_PROFIT').get_value()
     return initial
Ejemplo n.º 12
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['margin_profit'] = configuration_get(
         'MARGIN_PROFIT').get_value()
     return context
Ejemplo n.º 13
0
def get_center_name():
    return configuration_get('CENTER_NAME').get_value()
Ejemplo n.º 14
0
def self_lydia_callback(request):
    """
    Function to catch the callback from Lydia after a payment.

    Create all objects needed to have a proper sale in the database, and credit
    the client.

    :param GET['user_pk']: pk of the client, mandatory.
    :param POST['currency']: icon of the currency, for instance EUR, mandatory.
    :param POST['request_id']: request id from lydia, mandatory.
    :param POST['amount']: amount of the transaction, in 'currency', mandatory.
    :param POST['signed']: internal to Lydia ?
    :param POST['transaction_identifier']: transaction id from Lydia,
    mandatory.
    :param POST['vendor_token']: public key of the association, mandatory.
    :param POST['sig']: signatory generated by Lydia to identify the
    transaction, mandator.
    :type GET['user_pk']: positiv integer
    :type POST['currency']: string
    :type POST['request_id: string
    :type POST['amount']: float (decimal)
    :type POST['signed']: boolean
    :type POST['transaction_identifier']: string
    :type POST['vendor_token']: string
    :type POST['sig']: string

    :note:: Even if some parameters tend to be useless (signed, request_id),
    they are mandatory because used to generated the signatory and verify the
    transaction.

    :raises: PermissionDenied if signatory generated is not sig.
    :returns: 300 if the user_pk doesn't match an user.
    :returns: 300 if a parameter is missing.
    :returns: 200 if all's good.
    :rtype: Http request
    """
    params_dict = {
        "currency": request.POST.get("currency"),
        "request_id": request.POST.get("request_id"),
        "amount": request.POST.get("amount"),
        "signed": request.POST.get("signed"),
        "transaction_identifier": request.POST.get("transaction_identifier"),
        "vendor_token": request.POST.get("vendor_token"),
        "sig": request.POST.get("sig")
    }
    lydia_token = configuration_get("API_TOKEN_LYDIA").get_value()

    if verify_token_lydia(params_dict, lydia_token) is False:
        raise PermissionDenied
    else:
        try:
            user_pk = request.GET.get('user_pk')
        except KeyError:
            return PermissionDenied

        try:
            user = User.objects.get(pk=user_pk)
        except ObjectDoesNotExist:
            raise Http404

        total_amount = decimal.Decimal(params_dict['amount'])
        if not configuration_get('ENABLE_FEE_LYDIA').get_value():
            fee = 0
            recharging_amount = total_amount
        else:
            base_fee = decimal.Decimal(configuration_get('BASE_FEE_LYDIA').get_value()).quantize(decimal.Decimal('.01'))
            ratio_fee = decimal.Decimal(configuration_get('RATIO_FEE_LYDIA').get_value()).quantize(decimal.Decimal('.01'))
            tax_fee = decimal.Decimal(configuration_get('TAX_FEE_LYDIA').get_value()).quantize(decimal.Decimal('.01'))

            fee = calculate_lydia_fee_from_total(
                total_amount, base_fee, ratio_fee, tax_fee)
            recharging_amount = total_amount - fee

        lydia = Lydia.objects.create(
            sender=user,
            amount=recharging_amount,
            id_from_lydia=params_dict['transaction_identifier'],
            fee=fee
        )
        recharging = Recharging.objects.create(
            sender=user,
            operator=user,
            content_solution=lydia
        )
        recharging.pay()

        return HttpResponse('200')
Ejemplo n.º 15
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['center_name'] = configuration_get('CENTER_NAME')
     context['margin_profit'] = configuration_get('MARGIN_PROFIT')
     context['enable_self_lydia'] = configuration_get('ENABLE_SELF_LYDIA')
     context['min_price_lydia'] = configuration_get('MIN_PRICE_LYDIA')
     context['max_price_lydia'] = configuration_get('MAX_PRICE_LYDIA')
     context['api_token_lydia'] = configuration_get('API_TOKEN_LYDIA')
     context['vendor_token_lydia'] = configuration_get('VENDOR_TOKEN_LYDIA')
     context['enable_fee_lydia'] = configuration_get('ENABLE_FEE_LYDIA')
     context['base_fee_lydia'] = configuration_get('BASE_FEE_LYDIA')
     context['ratio_fee_lydia'] = configuration_get('RATIO_FEE_LYDIA')
     context['tax_fee_lydia'] = configuration_get('TAX_FEE_LYDIA')
     context['balance_threshold_purchase'] = configuration_get(
         "BALANCE_THRESHOLD_PURCHASE")
     #context['balance_threshold_mail_alert'] = configuration_get("BALANCE_THRESHOLD_MAIL_ALERT")
     #context['balance_frequency_mail_alert'] = configuration_get("BALANCE_FREQUENCY_MAIL_ALERT")
     return context
Ejemplo n.º 16
0
 def form_valid(self, form):
     # Margin profit
     margin_profit = configuration_get('MARGIN_PROFIT')
     margin_profit.value = form.cleaned_data['margin_profit']
     margin_profit.save()
     return super().form_valid(form)
Ejemplo n.º 17
0
 def form_valid(self, form):
     # Margin profit
     center_name = configuration_get('CENTER_NAME')
     center_name.value = form.cleaned_data['center_name']
     center_name.save()
     return super().form_valid(form)
Ejemplo n.º 18
0
 def get_initial(self):
     initial = super().get_initial()
     initial['center_name'] = configuration_get('CENTER_NAME').get_value()
     return initial