Example #1
0
 def get_reference_price(self, customer_price=True):
     if (self.order_average_weight > DECIMAL_ZERO
             and self.order_average_weight != DECIMAL_ONE):
         if self.order_unit in [
                 PRODUCT_ORDER_UNIT_PC_PRICE_KG,
                 PRODUCT_ORDER_UNIT_PC_PRICE_LT,
                 PRODUCT_ORDER_UNIT_PC_PRICE_PC,
         ]:
             if customer_price:
                 reference_price = (self.customer_unit_price.amount /
                                    self.order_average_weight)
             else:
                 reference_price = (self.producer_unit_price.amount /
                                    self.order_average_weight)
             reference_price = RepanierMoney(
                 reference_price.quantize(TWO_DECIMALS), 2)
             if self.order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_KG:
                 reference_unit = _("/ kg")
             elif self.order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_LT:
                 reference_unit = _("/ l")
             else:
                 reference_unit = _("/ pc")
             return "{} {}".format(reference_price, reference_unit)
         else:
             return EMPTY_STRING
     else:
         return EMPTY_STRING
Example #2
0
 def get_producer_row_price_invoiced(self):
     if self.manage_replenishment:
         if self.producer_unit_price.amount > self.customer_unit_price.amount:
             return RepanierMoney(
                 (self.customer_unit_price.amount + self.unit_deposit.amount) * self.get_producer_qty_invoiced(), 2)
         else:
             return RepanierMoney(
                 (self.producer_unit_price.amount + self.unit_deposit.amount) * self.get_producer_qty_invoiced(), 2)
     else:
         if self.producer_unit_price.amount > self.customer_unit_price.amount:
             return self.total_selling_with_tax
         else:
             return self.total_purchase_with_tax
Example #3
0
    def get_bank_not_invoiced(self):
        if settings.REPANIER_SETTINGS_MANAGE_ACCOUNTING:
            result_set = (BankAccount.objects.filter(
                producer_id=self.id,
                producer_invoice__isnull=True).order_by("?").aggregate(
                    bank_amount_in=Sum(
                        "bank_amount_in",
                        output_field=DecimalField(max_digits=8,
                                                  decimal_places=2,
                                                  default=DECIMAL_ZERO),
                    ),
                    bank_amount_out=Sum(
                        "bank_amount_out",
                        output_field=DecimalField(max_digits=8,
                                                  decimal_places=2,
                                                  default=DECIMAL_ZERO),
                    ),
                ))

            total_bank_amount_in = (result_set["bank_amount_in"]
                                    if result_set["bank_amount_in"] is not None
                                    else DECIMAL_ZERO)
            total_bank_amount_out = (result_set["bank_amount_out"]
                                     if result_set["bank_amount_out"]
                                     is not None else DECIMAL_ZERO)
            bank_not_invoiced = RepanierMoney(total_bank_amount_out -
                                              total_bank_amount_in)
        else:
            bank_not_invoiced = REPANIER_MONEY_ZERO

        return bank_not_invoiced
Example #4
0
 def __init__(self, *args, **kwargs):
     getcontext().rounding = ROUND_HALF_UP
     super(OfferItemSendDataForm, self).__init__(*args, **kwargs)
     offer_item = self.instance
     self.fields["previous_producer_unit_price"].initial = offer_item.producer_unit_price
     self.fields["previous_customer_unit_price"].initial = offer_item.customer_unit_price
     self.fields["previous_unit_deposit"].initial = offer_item.unit_deposit
     if offer_item.manage_replenishment:
         invoiced_qty, taken_from_stock, customer_qty = offer_item.get_producer_qty_stock_invoiced()
         self.fields["offer_purchase_price"].initial = RepanierMoney(
             offer_item.total_purchase_with_tax.amount - (
                 (offer_item.producer_unit_price.amount +
                  offer_item.unit_deposit.amount) * taken_from_stock
             ).quantize(TWO_DECIMALS), 2)
         self.fields["qty_delivered"].initial = invoiced_qty.quantize(FOUR_DECIMALS)
         self.fields["qty_prepared"].initial = customer_qty.quantize(
             FOUR_DECIMALS)
         self.fields["qty_prepared"].widget.attrs['readonly'] = True
         self.fields["qty_prepared"].disabled = True
     else:
         self.fields["offer_purchase_price"].initial = offer_item.total_purchase_with_tax
     if offer_item.wrapped or offer_item.order_unit not in [PRODUCT_ORDER_UNIT_KG,
                                                            PRODUCT_ORDER_UNIT_PC_KG] or offer_item.manage_replenishment:
         self.fields["offer_purchase_price"].widget.attrs['readonly'] = True
         self.fields["offer_purchase_price"].disabled = True
     if offer_item.producer_price_are_wo_vat:
         self.fields["offer_purchase_price"].label = _("producer amount invoiced wo tax")
Example #5
0
 def get_bank_not_invoiced(self):
     if settings.REPANIER_SETTINGS_MANAGE_ACCOUNTING:
         result_set = BankAccount.objects.filter(
             customer_id=self.id,
             customer_invoice__isnull=True).order_by('?').aggregate(
                 Sum('bank_amount_in'), Sum('bank_amount_out'))
         if result_set["bank_amount_in__sum"] is not None:
             bank_in = RepanierMoney(result_set["bank_amount_in__sum"])
         else:
             bank_in = REPANIER_MONEY_ZERO
         if result_set["bank_amount_out__sum"] is not None:
             bank_out = RepanierMoney(result_set["bank_amount_out__sum"])
         else:
             bank_out = REPANIER_MONEY_ZERO
         bank_not_invoiced = bank_in - bank_out
     else:
         bank_not_invoiced = REPANIER_MONEY_ZERO
     return bank_not_invoiced
Example #6
0
 def get_bank_not_invoiced(self):
     from repanier.apps import REPANIER_SETTINGS_INVOICE
     if REPANIER_SETTINGS_INVOICE:
         result_set = BankAccount.objects.filter(
             customer_id=self.id, customer_invoice__isnull=True
         ).order_by('?').aggregate(Sum('bank_amount_in'), Sum('bank_amount_out'))
         if result_set["bank_amount_in__sum"] is not None:
             bank_in = RepanierMoney(result_set["bank_amount_in__sum"])
         else:
             bank_in = REPANIER_MONEY_ZERO
         if result_set["bank_amount_out__sum"] is not None:
             bank_out = RepanierMoney(result_set["bank_amount_out__sum"])
         else:
             bank_out = REPANIER_MONEY_ZERO
         bank_not_invoiced = bank_in - bank_out
     else:
         bank_not_invoiced = REPANIER_MONEY_ZERO
     return bank_not_invoiced
Example #7
0
 def get_html_producer_price_purchased(self):
     if self.manage_replenishment:
         invoiced_qty, taken_from_stock, customer_qty = self.get_producer_qty_stock_invoiced()
         price = RepanierMoney(
             ((self.producer_unit_price.amount + self.unit_deposit.amount) * invoiced_qty).quantize(TWO_DECIMALS))
     else:
         price = self.total_purchase_with_tax
     if price != DECIMAL_ZERO:
         return mark_safe(_("<b>%(price)s</b>") % {'price': price})
     return EMPTY_STRING
Example #8
0
 def get_reference_price(self, customer_price=True):
     if self.order_average_weight > DECIMAL_ZERO and self.order_average_weight != DECIMAL_ONE:
         if self.order_unit in [PRODUCT_ORDER_UNIT_PC_PRICE_KG, PRODUCT_ORDER_UNIT_PC_PRICE_LT,
                                PRODUCT_ORDER_UNIT_PC_PRICE_PC]:
             if customer_price:
                 reference_price = self.customer_unit_price.amount / self.order_average_weight
             else:
                 reference_price = self.producer_unit_price.amount / self.order_average_weight
             reference_price = RepanierMoney(reference_price.quantize(TWO_DECIMALS), 2)
             if self.order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_KG:
                 reference_unit = _("/ kg")
             elif self.order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_LT:
                 reference_unit = _("/ l")
             else:
                 reference_unit = _("/ pc")
             return "%s %s" % (reference_price, reference_unit)
         else:
             return EMPTY_STRING
     else:
         return EMPTY_STRING
Example #9
0
 def get_order_not_invoiced(self):
     from repanier.apps import REPANIER_SETTINGS_INVOICE
     if REPANIER_SETTINGS_INVOICE:
         result_set = CustomerInvoice.objects.filter(
             customer_id=self.id,
             status__gte=PERMANENCE_OPENED,
             status__lte=PERMANENCE_SEND,
             customer_charged_id=self.id
         ).order_by('?').aggregate(Sum('total_price_with_tax'), Sum('delta_price_with_tax'), Sum('delta_transport'))
         if result_set["total_price_with_tax__sum"] is not None:
             order_not_invoiced = RepanierMoney(result_set["total_price_with_tax__sum"])
         else:
             order_not_invoiced = REPANIER_MONEY_ZERO
         if result_set["delta_price_with_tax__sum"] is not None:
             order_not_invoiced += RepanierMoney(result_set["delta_price_with_tax__sum"])
         if result_set["delta_transport__sum"] is not None:
             order_not_invoiced += RepanierMoney(result_set["delta_transport__sum"])
     else:
         order_not_invoiced = REPANIER_MONEY_ZERO
     return order_not_invoiced
Example #10
0
 def get_order_not_invoiced(self):
     if settings.REPANIER_SETTINGS_MANAGE_ACCOUNTING:
         result_set = (ProducerInvoice.objects.filter(
             producer_id=self.id,
             status__gte=PERMANENCE_OPENED,
             status__lte=PERMANENCE_SEND,
         ).order_by("?").aggregate(
             total_price_with_tax=Sum(
                 "total_price_with_tax",
                 output_field=DecimalField(max_digits=8,
                                           decimal_places=2,
                                           default=DECIMAL_ZERO),
             ),
             delta_price_with_tax=Sum(
                 "delta_price_with_tax",
                 output_field=DecimalField(max_digits=8,
                                           decimal_places=2,
                                           default=DECIMAL_ZERO),
             ),
             delta_transport=Sum(
                 "delta_transport",
                 output_field=DecimalField(max_digits=5,
                                           decimal_places=2,
                                           default=DECIMAL_ZERO),
             ),
         ))
         if result_set["total_price_with_tax"] is not None:
             order_not_invoiced = RepanierMoney(
                 result_set["total_price_with_tax"])
         else:
             order_not_invoiced = REPANIER_MONEY_ZERO
         if result_set["delta_price_with_tax"] is not None:
             order_not_invoiced += RepanierMoney(
                 result_set["delta_price_with_tax"])
         if result_set["delta_transport"] is not None:
             order_not_invoiced += RepanierMoney(
                 result_set["delta_transport"])
     else:
         order_not_invoiced = REPANIER_MONEY_ZERO
     return order_not_invoiced
Example #11
0
 def get_order_not_invoiced(self):
     if settings.REPANIER_SETTINGS_MANAGE_ACCOUNTING:
         result_set = ProducerInvoice.objects.filter(
             producer_id=self.id,
             status__gte=PERMANENCE_OPENED,
             status__lte=PERMANENCE_SEND).order_by('?').aggregate(
                 Sum('total_price_with_tax'), Sum('delta_price_with_tax'),
                 Sum('delta_transport'))
         if result_set["total_price_with_tax__sum"] is not None:
             order_not_invoiced = RepanierMoney(
                 result_set["total_price_with_tax__sum"])
         else:
             order_not_invoiced = REPANIER_MONEY_ZERO
         if result_set["delta_price_with_tax__sum"] is not None:
             order_not_invoiced += RepanierMoney(
                 result_set["delta_price_with_tax__sum"])
         if result_set["delta_transport__sum"] is not None:
             order_not_invoiced += RepanierMoney(
                 result_set["delta_transport__sum"])
     else:
         order_not_invoiced = REPANIER_MONEY_ZERO
     return order_not_invoiced
Example #12
0
    def send_invoices(self, request, permanence_qs):
        if 'cancel' in request.POST:
            user_message = _("Action canceled by the user.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return
        permanence = permanence_qs.first()
        if permanence is None or permanence.status != PERMANENCE_INVOICED:
            user_message = _("Action canceled by the system.")
            user_message_level = messages.ERROR
            self.message_user(request, user_message, user_message_level)
            return
        template = Template(
            repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_customer_mail)
        invoice_description = permanence.safe_translation_getter(
            'invoice_description', any_language=True, default=EMPTY_STRING)
        sender_email, sender_function, signature, cc_email_staff = get_signature(
            is_reply_to_invoice_email=True)
        # TODO : Align on tools.payment_message
        customer_order_amount = \
                _('The amount of your order is %(amount)s.') % {
                    'amount': RepanierMoney(123.45)
                }
        customer_last_balance = \
            _('The balance of your account as of %(date)s is %(balance)s.') % {
                'date'   : timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                'balance': RepanierMoney(123.45)
            }
        customer_payment_needed = "%s %s %s (%s) %s \"%s\"." % (
            _('Please pay'), RepanierMoney(123.45),
            _('to the bank account number'),
            repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT,
            _('with communication'), _('short_basket_name'))
        context = TemplateContext({
            'name':
            _('long_basket_name'),
            'long_basket_name':
            _('long_basket_name'),
            'basket_name':
            _('short_basket_name'),
            'short_basket_name':
            _('short_basket_name'),
            'permanence_link':
            mark_safe('<a href=#">%s</a>' % permanence),
            'last_balance_link':
            mark_safe('<a href="#">%s</a>' % customer_last_balance),
            'last_balance':
            customer_last_balance,
            'order_amount':
            mark_safe(customer_order_amount),
            'payment_needed':
            mark_safe(customer_payment_needed),
            'invoice_description':
            mark_safe(invoice_description),
            'signature':
            mark_safe('%s<br/>%s<br/>%s' %
                      (signature, sender_function,
                       repanier.apps.REPANIER_SETTINGS_GROUP_NAME)),
        })
        template_invoice_customer_mail = template.render(context)

        invoice_customer_email_will_be_sent, invoice_customer_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER)

        template = Template(
            repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_producer_mail)

        context = TemplateContext({
            'name':
            _('long_profile_name'),
            'long_profile_name':
            _('long_profile_name'),
            'permanence_link':
            mark_safe('<a href=#">%s</a>' % permanence),
            'signature':
            mark_safe('%s<br/>%s<br/>%s' %
                      (signature, sender_function,
                       repanier.apps.REPANIER_SETTINGS_GROUP_NAME)),
        })
        template_invoice_producer_mail = template.render(context)

        invoice_producer_email_will_be_sent, invoice_producer_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER)
        if 'apply' in request.POST:
            form = InvoiceOrderForm(request.POST)
            if form.is_valid():
                user_message, user_message_level = task_invoice.admin_send(
                    permanence)
                self.message_user(request, user_message, user_message_level)
            return HttpResponseRedirect(request.get_full_path())
        else:
            form = InvoiceOrderForm(
                initial={
                    'template_invoice_customer_mail':
                    mark_safe(template_invoice_customer_mail),
                    'template_invoice_producer_mail':
                    mark_safe(template_invoice_producer_mail),
                })
        return render(
            request, 'repanier/confirm_admin_send_invoice.html', {
                'sub_title':
                _("Please, confirm the action : send invoices"),
                'action_checkbox_name':
                admin.ACTION_CHECKBOX_NAME,
                'action':
                'send_invoices',
                'permanence':
                permanence,
                'form':
                form,
                'invoice_customer_email_will_be_sent':
                invoice_customer_email_will_be_sent,
                'invoice_customer_email_will_be_sent_to':
                invoice_customer_email_will_be_sent_to,
                'invoice_producer_email_will_be_sent':
                invoice_producer_email_will_be_sent,
                'invoice_producer_email_will_be_sent_to':
                invoice_producer_email_will_be_sent_to
            })
Example #13
0
    def send_invoices(self, request, permanence_qs):
        if "cancel" in request.POST:
            user_message = _("Action canceled by the user.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return
        permanence = permanence_qs.first()
        if permanence.status != PERMANENCE_INVOICED:
            user_message = _(
                "The status of %(permanence)s prohibit you to perform this action."
            ) % {
                "permanence": permanence
            }
            user_message_level = messages.ERROR
            self.message_user(request, user_message, user_message_level)
            return
        template = Template(
            repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_customer_mail)
        invoice_description = permanence.safe_translation_getter(
            "invoice_description", any_language=True, default=EMPTY_STRING)
        staff = Staff.get_or_create_invoice_responsible()

        # TODO : Align on tools.payment_message
        customer_order_amount = _(
            "The amount of your order is %(amount)s.") % {
                "amount": RepanierMoney(123.45)
            }
        customer_last_balance = _(
            "The balance of your account as of %(date)s is %(balance)s.") % {
                "date": timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                "balance": RepanierMoney(123.45),
            }
        bank_account_number = repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT
        if bank_account_number is not None:
            group_name = settings.REPANIER_SETTINGS_GROUP_NAME
            if permanence.short_name:
                communication = "{} ({})".format(_("Short name"),
                                                 permanence.short_name)
            else:
                communication = _("Short name")
            customer_payment_needed = '<font color="#bd0926">{}</font>'.format(
                _("Please pay a provision of %(payment)s to the bank account %(name)s %(number)s with communication %(communication)s."
                  ) % {
                      "payment": RepanierMoney(123.45),
                      "name": group_name,
                      "number": bank_account_number,
                      "communication": communication,
                  })
        else:
            customer_payment_needed = EMPTY_STRING
        context = TemplateContext({
            "name":
            _("Long name"),
            "long_basket_name":
            _("Long name"),
            "basket_name":
            _("Short name"),
            "short_basket_name":
            _("Short name"),
            "permanence_link":
            mark_safe('<a href="#">{}</a>'.format(permanence)),
            "last_balance_link":
            mark_safe('<a href="#">{}</a>'.format(customer_last_balance)),
            "last_balance":
            customer_last_balance,
            "order_amount":
            mark_safe(customer_order_amount),
            "payment_needed":
            mark_safe(customer_payment_needed),
            "invoice_description":
            mark_safe(invoice_description),
            "signature":
            staff.get_html_signature,
        })
        template_invoice_customer_mail = template.render(context)

        invoice_customer_email_will_be_sent, invoice_customer_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=repanier.apps.
            REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER)

        template = Template(
            repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_producer_mail)

        context = TemplateContext({
            "name":
            _("Long name"),
            "long_profile_name":
            _("Long name"),
            "permanence_link":
            mark_safe('<a href="#">{}</a>'.format(permanence)),
            "signature":
            staff.get_html_signature,
        })
        template_invoice_producer_mail = template.render(context)

        invoice_producer_email_will_be_sent, invoice_producer_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=repanier.apps.
            REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER)
        if "apply" in request.POST:
            form = InvoiceOrderForm(request.POST)
            if form.is_valid():
                t = threading.Thread(target=email_invoice.send_invoice,
                                     args=(permanence.id, ))
                t.start()
                user_message = _(
                    "Emails containing the invoices will be send to the customers and the producers."
                )
                user_message_level = messages.INFO
                self.message_user(request, user_message, user_message_level)
            return HttpResponseRedirect(request.get_full_path())
        else:
            form = InvoiceOrderForm(
                initial={
                    "template_invoice_customer_mail":
                    mark_safe(template_invoice_customer_mail),
                    "template_invoice_producer_mail":
                    mark_safe(template_invoice_producer_mail),
                })
        template_name = get_repanier_template_name(
            "confirm_admin_send_invoice.html")
        return render(
            request,
            template_name,
            {
                "sub_title":
                _("Please, confirm the action : send invoices"),
                "action_checkbox_name":
                admin.ACTION_CHECKBOX_NAME,
                "action":
                "send_invoices",
                "permanence":
                permanence,
                "form":
                form,
                "invoice_customer_email_will_be_sent":
                invoice_customer_email_will_be_sent,
                "invoice_customer_email_will_be_sent_to":
                invoice_customer_email_will_be_sent_to,
                "invoice_producer_email_will_be_sent":
                invoice_producer_email_will_be_sent,
                "invoice_producer_email_will_be_sent_to":
                invoice_producer_email_will_be_sent_to,
            },
        )
Example #14
0
    def send_invoices(self, request, permanence_qs):
        if 'cancel' in request.POST:
            user_message = _("Action canceled by the user.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return
        permanence = permanence_qs.first()
        if permanence.status != PERMANENCE_INVOICED:
            user_message = _("The status of %(permanence)s prohibit you to perform this action.") % {
                'permanence': permanence}
            user_message_level = messages.ERROR
            self.message_user(request, user_message, user_message_level)
            return
        template = Template(repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_customer_mail)
        invoice_description = permanence.safe_translation_getter(
            'invoice_description', any_language=True, default=EMPTY_STRING
        )
        staff = Staff.get_or_create_invoice_responsible()

        # TODO : Align on tools.payment_message
        customer_order_amount = \
            _('The amount of your order is %(amount)s.') % {
                'amount': RepanierMoney(123.45)
            }
        customer_last_balance = \
            _('The balance of your account as of %(date)s is %(balance)s.') % {
                'date': timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                'balance': RepanierMoney(123.45)
            }
        customer_payment_needed = "{} {} {} ({}) {} \"{}\".".format(
            _('Please pay'),
            RepanierMoney(123.45),
            _('to the bank account number'),
            repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT,
            _('with communication'),
            _('Short name'))
        context = TemplateContext({
            'name': _('Long name'),
            'long_basket_name': _('Long name'),
            'basket_name': _('Short name'),
            'short_basket_name': _('Short name'),
            'permanence_link': mark_safe("<a href=\"#\">{}</a>".format(permanence)),
            'last_balance_link': mark_safe("<a href=\"#\">{}</a>".format(customer_last_balance)),
            'last_balance': customer_last_balance,
            'order_amount': mark_safe(customer_order_amount),
            'payment_needed': mark_safe(customer_payment_needed),
            'invoice_description': mark_safe(invoice_description),
            'signature': staff.get_html_signature,
        })
        template_invoice_customer_mail = template.render(context)

        invoice_customer_email_will_be_sent, invoice_customer_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER
        )

        template = Template(repanier.apps.REPANIER_SETTINGS_CONFIG.invoice_producer_mail)

        context = TemplateContext({
            'name': _('Long name'),
            'long_profile_name': _('Long name'),
            'permanence_link': mark_safe("<a href=\"#\">{}</a>".format(permanence)),
            'signature': staff.get_html_signature,
        })
        template_invoice_producer_mail = template.render(context)

        invoice_producer_email_will_be_sent, invoice_producer_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER
        )
        if 'apply' in request.POST:
            form = InvoiceOrderForm(request.POST)
            if form.is_valid():
                t = threading.Thread(target=email_invoice.send_invoice, args=(permanence.id,))
                t.start()
                user_message = _("Emails containing the invoices will be send to the customers and the producers.")
                user_message_level = messages.INFO
                self.message_user(request, user_message, user_message_level)
            return HttpResponseRedirect(request.get_full_path())
        else:
            form = InvoiceOrderForm(
                initial={
                    'template_invoice_customer_mail': mark_safe(template_invoice_customer_mail),
                    'template_invoice_producer_mail': mark_safe(template_invoice_producer_mail),
                }
            )
        return render(
            request,
            'repanier/confirm_admin_send_invoice.html', {
                'sub_title': _("Please, confirm the action : send invoices"),
                'action_checkbox_name': admin.ACTION_CHECKBOX_NAME,
                'action': 'send_invoices',
                'permanence': permanence,
                'form': form,
                'invoice_customer_email_will_be_sent': invoice_customer_email_will_be_sent,
                'invoice_customer_email_will_be_sent_to': invoice_customer_email_will_be_sent_to,
                'invoice_producer_email_will_be_sent': invoice_producer_email_will_be_sent,
                'invoice_producer_email_will_be_sent_to': invoice_producer_email_will_be_sent_to
            })
Example #15
0
 def get_display(self,
                 qty=0,
                 order_unit=PRODUCT_ORDER_UNIT_PC,
                 unit_price_amount=None,
                 for_customer=True,
                 for_order_select=False,
                 without_price_display=False):
     magnitude = None
     display_qty = True
     if order_unit == PRODUCT_ORDER_UNIT_KG:
         if qty == DECIMAL_ZERO:
             unit = EMPTY_STRING
         elif for_customer and qty < 1:
             unit = "{}".format(_('gr'))
             magnitude = 1000
         else:
             unit = "{}".format(_('kg'))
     elif order_unit == PRODUCT_ORDER_UNIT_LT:
         if qty == DECIMAL_ZERO:
             unit = EMPTY_STRING
         elif for_customer and qty < 1:
             unit = "{}".format(_('cl'))
             magnitude = 100
         else:
             unit = "{}".format(_('l'))
     elif order_unit in [
             PRODUCT_ORDER_UNIT_PC_KG, PRODUCT_ORDER_UNIT_PC_PRICE_KG
     ]:
         # display_qty = not (order_average_weight == 1 and order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_KG)
         average_weight = self.order_average_weight
         if for_customer:
             average_weight *= qty
         if order_unit == PRODUCT_ORDER_UNIT_PC_KG and unit_price_amount is not None:
             unit_price_amount *= self.order_average_weight
         if average_weight < 1:
             average_weight_unit = _('gr')
             average_weight *= 1000
         else:
             average_weight_unit = _('kg')
         decimal = 3
         if average_weight == int(average_weight):
             decimal = 0
         elif average_weight * 10 == int(average_weight * 10):
             decimal = 1
         elif average_weight * 100 == int(average_weight * 100):
             decimal = 2
         tilde = EMPTY_STRING
         if order_unit == PRODUCT_ORDER_UNIT_PC_KG:
             tilde = '~'
         if for_customer:
             if qty == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             else:
                 if self.order_average_weight == 1 and order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_KG:
                     unit = "{}{} {}".format(
                         tilde, number_format(average_weight, decimal),
                         average_weight_unit)
                 else:
                     unit = "{}{}{}".format(
                         tilde, number_format(average_weight, decimal),
                         average_weight_unit)
         else:
             if qty == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             else:
                 unit = "{}{}{}".format(
                     tilde, number_format(average_weight, decimal),
                     average_weight_unit)
     elif order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_LT:
         display_qty = self.order_average_weight != 1
         average_weight = self.order_average_weight
         if for_customer:
             average_weight *= qty
         if average_weight < 1:
             average_weight_unit = _('cl')
             average_weight *= 100
         else:
             average_weight_unit = _('l')
         decimal = 3
         if average_weight == int(average_weight):
             decimal = 0
         elif average_weight * 10 == int(average_weight * 10):
             decimal = 1
         elif average_weight * 100 == int(average_weight * 100):
             decimal = 2
         if for_customer:
             if qty == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             else:
                 if display_qty:
                     unit = "{}{}".format(
                         number_format(average_weight, decimal),
                         average_weight_unit)
                 else:
                     unit = "{} {}".format(
                         number_format(average_weight, decimal),
                         average_weight_unit)
         else:
             if qty == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             else:
                 unit = "{}{}".format(
                     number_format(average_weight, decimal),
                     average_weight_unit)
     elif order_unit == PRODUCT_ORDER_UNIT_PC_PRICE_PC:
         display_qty = self.order_average_weight != 1
         average_weight = self.order_average_weight
         if for_customer:
             average_weight *= qty
             if qty == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             else:
                 if average_weight < 2:
                     pc_pcs = _('pc')
                 else:
                     pc_pcs = _('pcs')
                 if display_qty:
                     unit = "{}{}".format(number_format(average_weight, 0),
                                          pc_pcs)
                 else:
                     unit = "{} {}".format(number_format(average_weight, 0),
                                           pc_pcs)
         else:
             if average_weight == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             elif average_weight < 2:
                 unit = "{} {}".format(number_format(average_weight, 0),
                                       _('pc'))
             else:
                 unit = "{} {}".format(number_format(average_weight, 0),
                                       _('pcs'))
     else:
         if for_order_select:
             if qty == DECIMAL_ZERO:
                 unit = EMPTY_STRING
             elif qty < 2:
                 unit = "{}".format(_('unit'))
             else:
                 unit = "{}".format(_('units'))
         else:
             unit = EMPTY_STRING
     if unit_price_amount is not None:
         price_display = " = {}".format(
             RepanierMoney(unit_price_amount * qty))
     else:
         price_display = EMPTY_STRING
     if magnitude is not None:
         qty *= magnitude
     decimal = 3
     if qty == int(qty):
         decimal = 0
     elif qty * 10 == int(qty * 10):
         decimal = 1
     elif qty * 100 == int(qty * 100):
         decimal = 2
     if for_customer or for_order_select:
         if unit:
             if display_qty:
                 qty_display = "{} ({})".format(number_format(qty, decimal),
                                                unit)
             else:
                 qty_display = "{}".format(unit)
         else:
             qty_display = "{}".format(number_format(qty, decimal))
     else:
         if unit:
             qty_display = "({})".format(unit)
         else:
             qty_display = EMPTY_STRING
     if without_price_display:
         return qty_display
     else:
         display = "{}{}".format(qty_display, price_display)
         return display
    def send_order(self, request, queryset):
        if 'cancel' in request.POST:
            user_message = _("Action canceled by the user.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return
        permanence = queryset.first()
        if permanence is None or permanence.status not in [
                PERMANENCE_OPENED, PERMANENCE_CLOSED
        ]:
            user_message = _("Action canceled by the system.")
            user_message_level = messages.ERROR
            self.message_user(request, user_message, user_message_level)
            return
        if 'apply' in request.POST:
            deliveries_to_be_send = []
            producers_to_be_send = []
            producer_qs = Producer.objects.filter(permanence=permanence.id)
            if "deliveries" in request.POST:
                deliveries_to_be_send = request.POST.getlist("deliveries")
                if len(deliveries_to_be_send) == 0:
                    user_message = _(
                        "You must select at least one delivery point.")
                    user_message_level = messages.WARNING
                    self.message_user(request, user_message,
                                      user_message_level)
                    return
            all_producers = "all-producer-invoices" in request.POST
            producers_invoices_to_be_send = []
            if "producer-invoices" in request.POST:
                producers_invoices_to_be_send = request.POST.getlist(
                    "producer-invoices")
            if not all_producers and len(producers_invoices_to_be_send) == 0:
                user_message = _("You must select at least one producer.")
                user_message_level = messages.WARNING
                self.message_user(request, user_message, user_message_level)
                return
            producer_qs = producer_qs.filter(
                producerinvoice__in=producers_invoices_to_be_send)
            for producer in producer_qs.only("id").order_by('?'):
                producers_to_be_send.append(producer.id)

            user_message, user_message_level = task_order.admin_send(
                permanence_id=permanence.id,
                all_producers=all_producers,
                deliveries_id=deliveries_to_be_send,
                producers_id=producers_to_be_send)
            self.message_user(request, user_message, user_message_level)
            return

        template_order_customer_mail = []
        template_order_producer_mail = []
        template_order_staff_mail = []
        cur_language = translation.get_language()
        for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
            language_code = language["code"]
            translation.activate(language_code)

            template = Template(
                repanier.apps.REPANIER_SETTINGS_CONFIG.order_customer_mail)
            sender_email, sender_function, signature, cc_email_staff = get_signature(
                is_reply_to_order_email=True)
            customer_last_balance = \
                _('The balance of your account as of %(date)s is %(balance)s.') % {
                    'date'   : timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                    'balance': RepanierMoney(123.45)
                }
            customer_on_hold_movement = \
                _(
                    'This balance does not take account of any unrecognized payments %(bank)s and any unbilled order %(other_order)s.') \
                % {
                    'bank'       : RepanierMoney(123.45),
                    'other_order': RepanierMoney(123.45)
                }

            bank_account_number = repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT
            if bank_account_number is not None:
                group_name = repanier.apps.REPANIER_SETTINGS_GROUP_NAME
                if permanence.short_name:
                    communication = "%s (%s)" % (_('Short name'),
                                                 permanence.short_name)
                else:
                    communication = _('Short name')
                customer_payment_needed = '<font color="#bd0926">%s</font>' % (
                    _('Please pay %(payment)s to the bank account %(name)s %(number)s with communication %(communication)s.'
                      ) % {
                          'payment': RepanierMoney(123.45),
                          'name': group_name,
                          'number': bank_account_number,
                          'communication': communication
                      })
            else:
                customer_payment_needed = EMPTY_STRING
            context = TemplateContext({
                'name':
                _('Long name'),
                'long_basket_name':
                _('Long name'),
                'basket_name':
                _('Short name'),
                'short_basket_name':
                _('Short name'),
                'permanence_link':
                mark_safe('<a href=#">%s</a>' % permanence),
                'last_balance':
                mark_safe('<a href="#">%s</a>' % customer_last_balance),
                'order_amount':
                RepanierMoney(123.45),
                'on_hold_movement':
                mark_safe(customer_on_hold_movement),
                'payment_needed':
                mark_safe(customer_payment_needed),
                'delivery_point':
                _('delivery point').upper(),
                'signature':
                mark_safe('%s<br/>%s<br/>%s' %
                          (signature, sender_function,
                           repanier.apps.REPANIER_SETTINGS_GROUP_NAME)),
            })

            template_order_customer_mail.append(language_code)
            template_order_customer_mail.append(template.render(context))

            template = Template(
                repanier.apps.REPANIER_SETTINGS_CONFIG.order_producer_mail)
            context = TemplateContext({
                'name':
                _('Long name'),
                'long_profile_name':
                _('Long name'),
                'order_empty':
                False,
                'duplicate':
                True,
                'permanence_link':
                mark_safe('<a href=#">%s</a>' % permanence),
                'signature':
                mark_safe('%s<br/>%s<br/>%s' %
                          (signature, sender_function,
                           repanier.apps.REPANIER_SETTINGS_GROUP_NAME)),
            })

            template_order_producer_mail.append(language_code)
            template_order_producer_mail.append(template.render(context))

            board_composition, board_composition_and_description = get_board_composition(
                permanence.id)
            template = Template(
                repanier.apps.REPANIER_SETTINGS_CONFIG.order_staff_mail)
            context = TemplateContext({
                'permanence_link':
                mark_safe('<a href=#">%s</a>' % permanence),
                'board_composition':
                mark_safe(board_composition),
                'board_composition_and_description':
                mark_safe(board_composition_and_description),
                'signature':
                mark_safe('%s<br/>%s<br/>%s' %
                          (signature, sender_function,
                           repanier.apps.REPANIER_SETTINGS_GROUP_NAME)),
            })

            template_order_staff_mail.append(language_code)
            template_order_staff_mail.append(template.render(context))

        translation.activate(cur_language)

        order_customer_email_will_be_sent, order_customer_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_CUSTOMER)
        order_producer_email_will_be_sent, order_producer_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER)
        order_board_email_will_be_sent, order_board_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD,
            board=True)

        form = CloseAndSendOrderForm(
            initial={
                'template_order_customer_mail':
                mark_safe("<br/>==============<br/>".join(
                    template_order_customer_mail)),
                'template_order_producer_mail':
                mark_safe("<br/>==============<br/>".join(
                    template_order_producer_mail)),
                'template_order_staff_mail':
                mark_safe("<br/>==============<br/>".join(
                    template_order_staff_mail)),
            })
        if repanier.apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS or DeliveryBoard.objects.filter(
                permanence_id=permanence.id,
                status__gt=PERMANENCE_OPENED).order_by('?').exists():
            # /!\ If one delivery point has been closed, I may not close anymore by producer:
            producer_invoices = ProducerInvoice.objects.none()
        else:
            producer_invoices = ProducerInvoice.objects.filter(
                Q(permanence_id=permanence.id,
                  status=PERMANENCE_OPENED,
                  producer__represent_this_buyinggroup=False)
                | Q(permanence_id=permanence.id,
                    status=PERMANENCE_CLOSED,
                    producer__represent_this_buyinggroup=False)).order_by(
                        "producer")
        return render(
            request, 'repanier/confirm_admin_send_order.html', {
                'sub_title':
                _("Please, confirm the action : send orders"),
                'action_checkbox_name':
                admin.ACTION_CHECKBOX_NAME,
                'action':
                'send_order',
                'permanence':
                permanence,
                'deliveries':
                DeliveryBoard.objects.filter(
                    permanence_id=permanence.id,
                    status__in=[PERMANENCE_OPENED, PERMANENCE_CLOSED
                                ]).order_by("id"),
                'producer_invoices':
                producer_invoices,
                'form':
                form,
                'order_customer_email_will_be_sent':
                order_customer_email_will_be_sent,
                'order_customer_email_will_be_sent_to':
                order_customer_email_will_be_sent_to,
                'order_producer_email_will_be_sent':
                order_producer_email_will_be_sent,
                'order_producer_email_will_be_sent_to':
                order_producer_email_will_be_sent_to,
                'order_board_email_will_be_sent':
                order_board_email_will_be_sent,
                'order_board_email_will_be_sent_to':
                order_board_email_will_be_sent_to
            })
Example #17
0
    def send_invoices(self, request, permanence_id, permanence=None):
        if "apply" in request.POST:
            t = threading.Thread(target=email_invoice.send_invoice,
                                 args=(permanence_id, ))
            t.start()
            user_message = _("The invoices are being send.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return HttpResponseRedirect(self.get_redirect_to_change_list_url())

        template_invoice_customer_mail = []
        template_invoice_producer_mail = []
        invoice_customer_email_will_be_sent, invoice_customer_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=repanier.apps.
            REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER)
        invoice_producer_email_will_be_sent, invoice_producer_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=repanier.apps.
            REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER)

        if invoice_customer_email_will_be_sent or invoice_producer_email_will_be_sent:
            cur_language = translation.get_language()
            for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
                language_code = language["code"]
                translation.activate(language_code)
                invoice_responsible = Staff.get_or_create_invoice_responsible()

                if invoice_customer_email_will_be_sent:
                    with switch_language(
                            repanier.apps.REPANIER_SETTINGS_CONFIG,
                            language_code):
                        template = Template(
                            repanier.apps.REPANIER_SETTINGS_CONFIG.
                            invoice_customer_mail)
                    with switch_language(permanence, language_code):
                        invoice_description = permanence.safe_translation_getter(
                            "invoice_description",
                            any_language=True,
                            default=EMPTY_STRING,
                        )
                    # TODO : Align on tools.payment_message
                    customer_order_amount = _(
                        "The amount of your order is %(amount)s.") % {
                            "amount": RepanierMoney(123.45)
                        }
                    customer_last_balance = _(
                        "The balance of your account as of %(date)s is %(balance)s."
                    ) % {
                        "date":
                        timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                        "balance":
                        RepanierMoney(123.45),
                    }
                    bank_account_number = repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT
                    if bank_account_number is not None:
                        group_name = settings.REPANIER_SETTINGS_GROUP_NAME
                        if permanence.short_name:
                            communication = "{} ({})".format(
                                _("Short name"), permanence.short_name)
                        else:
                            communication = _("Short name")
                        customer_payment_needed = '<font color="#bd0926">{}</font>'.format(
                            _("Please pay a provision of %(payment)s to the bank account %(name)s %(number)s with communication %(communication)s."
                              ) % {
                                  "payment": RepanierMoney(123.45),
                                  "name": group_name,
                                  "number": bank_account_number,
                                  "communication": communication,
                              })
                    else:
                        customer_payment_needed = EMPTY_STRING
                    context = TemplateContext({
                        "name":
                        _("Long name"),
                        "long_basket_name":
                        _("Long name"),
                        "basket_name":
                        _("Short name"),
                        "short_basket_name":
                        _("Short name"),
                        "permanence_link":
                        mark_safe('<a href="#">{}</a>'.format(permanence)),
                        "last_balance_link":
                        mark_safe('<a href="#">{}</a>'.format(
                            customer_last_balance)),
                        "last_balance":
                        customer_last_balance,
                        "order_amount":
                        mark_safe(customer_order_amount),
                        "payment_needed":
                        mark_safe(customer_payment_needed),
                        "invoice_description":
                        mark_safe(invoice_description),
                        "signature":
                        invoice_responsible["html_signature"],
                    })
                    template_invoice_customer_mail.append(language_code)
                    template_invoice_customer_mail.append(
                        template.render(context))

                if invoice_producer_email_will_be_sent:
                    with switch_language(
                            repanier.apps.REPANIER_SETTINGS_CONFIG,
                            language_code):
                        template = Template(
                            repanier.apps.REPANIER_SETTINGS_CONFIG.
                            invoice_producer_mail)
                    context = TemplateContext({
                        "name":
                        _("Long name"),
                        "long_profile_name":
                        _("Long name"),
                        "permanence_link":
                        mark_safe('<a href="#">{}</a>'.format(permanence)),
                        "signature":
                        invoice_responsible["html_signature"],
                    })
                    template_invoice_producer_mail.append(language_code)
                    template_invoice_producer_mail.append(
                        template.render(context))

            translation.activate(cur_language)
        form = InvoiceOrderForm(
            initial={
                "template_invoice_customer_mail":
                mark_safe("<br>==============<br>".join(
                    template_invoice_customer_mail)),
                "template_invoice_producer_mail":
                mark_safe("<br>==============<br>".join(
                    template_invoice_producer_mail)),
            })
        template_name = get_repanier_template_name(
            "admin/confirm_send_invoice.html")
        return render(
            request,
            template_name,
            {
                **self.admin_site.each_context(request),
                "action_checkbox_name":
                admin.ACTION_CHECKBOX_NAME,
                "action":
                "send_invoices",
                "permanence":
                permanence,
                "form":
                form,
                "invoice_customer_email_will_be_sent_to":
                invoice_customer_email_will_be_sent_to,
                "invoice_producer_email_will_be_sent_to":
                invoice_producer_email_will_be_sent_to,
            },
        )
Example #18
0
    def invoice(self, request, permanence_id, permanence=None):
        max_payment_date = timezone.now().date()
        bank_account = (BankAccount.objects.filter(
            operation_status=BANK_LATEST_TOTAL).only(
                "operation_date").order_by("-id").first())
        if bank_account is not None:
            if bank_account.operation_date > max_payment_date:
                max_payment_date = bank_account.operation_date
            min_payment_date = bank_account.operation_date
        else:
            # This cas should never occur because of the first bank account record created at startup if none exists
            # via config.save() in apps.
            min_payment_date = timezone.now().date()

        if max_payment_date < min_payment_date:
            max_payment_date = min_payment_date
        if "apply" in request.POST and admin.ACTION_CHECKBOX_NAME in request.POST:
            permanence_form = PermanenceInvoicedForm(request.POST)
            producer_invoiced_formset = ProducerInvoicedFormSet(request.POST)
            if permanence_form.is_valid(
            ) and producer_invoiced_formset.is_valid():
                payment_date = permanence_form.cleaned_data.get("payment_date")
                if payment_date < min_payment_date or payment_date > max_payment_date:
                    permanence_form.add_error(
                        "payment_date",
                        _("The payment date must be between %(min_payment_date)s and %(max_payment_date)s."
                          ) % {
                              "min_payment_date":
                              min_payment_date.strftime(
                                  settings.DJANGO_SETTINGS_DATE),
                              "max_payment_date":
                              max_payment_date.strftime(
                                  settings.DJANGO_SETTINGS_DATE),
                          },
                    )
                else:
                    at_least_one_selected = False
                    for producer_invoiced_form in producer_invoiced_formset:
                        if producer_invoiced_form.is_valid():
                            producer_id = producer_invoiced_form.cleaned_data.get(
                                "id")
                            selected = producer_invoiced_form.cleaned_data.get(
                                "selected")
                            short_profile_name = producer_invoiced_form.cleaned_data.get(
                                "short_profile_name")
                            producer_invoice = (ProducerInvoice.objects.filter(
                                permanence_id=permanence_id,
                                invoice_sort_order__isnull=True,
                                producer_id=producer_id,
                            ).order_by("?").first())
                            if selected:
                                at_least_one_selected = True
                                producer_invoice.to_be_invoiced_balance = producer_invoiced_form.cleaned_data.get(
                                    "to_be_invoiced_balance")
                                producer_invoice.invoice_reference = producer_invoiced_form.cleaned_data.get(
                                    "invoice_reference", EMPTY_STRING)
                                producer_invoice.to_be_paid = True
                            else:
                                producer_invoice.to_be_invoiced_balance = DECIMAL_ZERO
                                producer_invoice.invoice_reference = EMPTY_STRING
                                producer_invoice.to_be_paid = False
                            producer_invoice.delta_vat = DECIMAL_ZERO
                            producer_invoice.delta_deposit = DECIMAL_ZERO
                            producer_invoice.delta_price_with_tax = DECIMAL_ZERO
                            producer_invoice.save(update_fields=[
                                "to_be_invoiced_balance",
                                "invoice_reference",
                                "delta_vat",
                                "delta_deposit",
                                "delta_price_with_tax",
                                "to_be_paid",
                            ])
                    if at_least_one_selected:
                        permanence.invoice(payment_date=payment_date)
                        previous_latest_total = (BankAccount.objects.filter(
                            operation_status=BANK_NOT_LATEST_TOTAL,
                            producer__isnull=True,
                            customer__isnull=True,
                        ).order_by("-id").first())
                        previous_latest_total_id = (previous_latest_total.id
                                                    if previous_latest_total
                                                    is not None else 0)
                        template_name = get_repanier_template_name(
                            "admin/confirm_bank_movement.html")
                        return render(
                            request,
                            template_name,
                            {
                                **self.admin_site.each_context(request),
                                "action":
                                "invoice",
                                "permanence":
                                permanence,
                                "bankaccounts":
                                BankAccount.objects.filter(
                                    id__gt=previous_latest_total_id,
                                    producer__isnull=False,
                                    producer__represent_this_buyinggroup=False,
                                    customer__isnull=True,
                                    operation_status=BANK_CALCULATED_INVOICE,
                                ).order_by("producer", "-operation_date",
                                           "-id"),
                                "action_checkbox_name":
                                admin.ACTION_CHECKBOX_NAME,
                            },
                        )
                    else:
                        user_message = _(
                            "You must select at least one producer.")
                        user_message_level = messages.WARNING
                        self.message_user(request, user_message,
                                          user_message_level)
                        return HttpResponseRedirect(
                            self.get_redirect_to_change_list_url())
        else:
            producers_invoiced = []
            for producer_invoice in (ProducerInvoice.objects.filter(
                    permanence_id=permanence_id,
                    invoice_sort_order__isnull=True).order_by(
                        "producer").select_related("producer")):
                producer = producer_invoice.producer
                if not producer.represent_this_buyinggroup:
                    # We have already pay to much (look at the bank movements).
                    # So we do not need to pay anything
                    producer_invoice.calculated_invoiced_balance.amount = producer.get_calculated_invoiced_balance(
                        permanence_id)
                else:
                    producer_invoice.calculated_invoiced_balance.amount = RepanierMoney(
                        producer_invoice.get_total_price_with_tax().amount)
                # First time invoiced ? Yes : propose the calculated invoiced balance as to be invoiced balance
                producer_invoice.to_be_invoiced_balance = (
                    producer_invoice.calculated_invoiced_balance)
                producer_invoice.save(update_fields=[
                    "calculated_invoiced_balance",
                    "to_be_invoiced_balance",
                ])
                producers_invoiced.append({
                    "id":
                    producer_invoice.producer_id,
                    "selected":
                    True,
                    "short_profile_name":
                    producer_invoice.producer.short_profile_name,
                    "calculated_invoiced_balance":
                    producer_invoice.calculated_invoiced_balance,
                    "to_be_invoiced_balance":
                    producer_invoice.to_be_invoiced_balance,
                    "invoice_reference":
                    producer_invoice.invoice_reference,
                    "producer_price_are_wo_vat":
                    producer_invoice.producer.producer_price_are_wo_vat,
                })
            if permanence.payment_date is not None:
                # In this case we the permanence has already been invoiced in the past
                # and the invoice has been cancelled
                payment_date = permanence.payment_date
            else:
                payment_date = max_payment_date
            permanence_form = PermanenceInvoicedForm(payment_date=payment_date)

            producer_invoiced_formset = ProducerInvoicedFormSet(
                initial=producers_invoiced)

        template_name = get_repanier_template_name(
            "admin/confirm_invoice.html")
        return render(
            request,
            template_name,
            {
                **self.admin_site.each_context(request),
                "action": "invoice",
                "permanence": permanence,
                "permanence_form": permanence_form,
                "producer_invoiced_formset": producer_invoiced_formset,
                "action_checkbox_name": admin.ACTION_CHECKBOX_NAME,
            },
        )
Example #19
0
    def close_and_send_order(self, request, queryset):
        if "cancel" in request.POST:
            user_message = _("Action canceled by the user.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return
        permanence = queryset.first()
        if permanence.status not in [PERMANENCE_OPENED, PERMANENCE_CLOSED]:
            user_message = _(
                "The status of %(permanence)s prohibit you to perform this action."
            ) % {
                "permanence": permanence
            }
            user_message_level = messages.ERROR
            self.message_user(request, user_message, user_message_level)
            return
        if "apply" in request.POST:
            all_deliveries = True if request.POST.get("all-deliveries",
                                                      True) else False
            deliveries_to_be_send = request.POST.getlist("deliveries", [])
            logger.debug("all_deliveries : {}".format(
                request.POST.get("all-deliveries")))
            logger.debug("all_deliveries : {}".format(all_deliveries))
            logger.debug("deliveries_to_be_send : {}".format(
                request.POST.getlist("deliveries", [])))
            if (permanence.with_delivery_point and not all_deliveries
                    and len(deliveries_to_be_send) == 0):
                user_message = _(
                    "You must select at least one delivery point.")
                user_message_level = messages.WARNING
                self.message_user(request, user_message, user_message_level)
                return
            # close_and_send_order(permanence.id, all_deliveries, deliveries_to_be_send)
            t = threading.Thread(
                target=close_and_send_order,
                args=(permanence.id, all_deliveries, deliveries_to_be_send),
            )
            t.start()
            user_message = _("The orders are being send.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return

        template_order_customer_mail = []
        template_order_producer_mail = []
        template_order_staff_mail = []
        cur_language = translation.get_language()
        for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
            language_code = language["code"]
            translation.activate(language_code)

            template = Template(
                repanier.apps.REPANIER_SETTINGS_CONFIG.order_customer_mail)

            staff = Staff.get_or_create_order_responsible()

            customer_last_balance = _(
                "The balance of your account as of %(date)s is %(balance)s."
            ) % {
                "date": timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                "balance": RepanierMoney(123.45),
            }
            customer_on_hold_movement = _(
                "This balance does not take account of any unrecognized payments %(bank)s and any unbilled order %(other_order)s."
            ) % {
                "bank": RepanierMoney(123.45),
                "other_order": RepanierMoney(123.45)
            }

            bank_account_number = repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT
            if bank_account_number is not None:
                group_name = settings.REPANIER_SETTINGS_GROUP_NAME

                if permanence.short_name:
                    communication = "{} ({})".format(_("Short name"),
                                                     permanence.short_name)
                else:
                    communication = _("Short name")
                customer_payment_needed = '<font color="#bd0926">{}</font>'.format(
                    _("Please pay a provision of %(payment)s to the bank account %(name)s %(number)s with communication %(communication)s."
                      ) % {
                          "payment": RepanierMoney(123.45),
                          "name": group_name,
                          "number": bank_account_number,
                          "communication": communication,
                      })
            else:
                customer_payment_needed = EMPTY_STRING
            context = TemplateContext({
                "name":
                _("Long name"),
                "long_basket_name":
                _("Long name"),
                "basket_name":
                _("Short name"),
                "short_basket_name":
                _("Short name"),
                "permanence_link":
                mark_safe('<a href="#">{}</a>'.format(permanence)),
                "last_balance":
                mark_safe('<a href="#">{}</a>'.format(customer_last_balance)),
                "order_amount":
                RepanierMoney(123.45),
                "on_hold_movement":
                mark_safe(customer_on_hold_movement),
                "payment_needed":
                mark_safe(customer_payment_needed),
                "delivery_point":
                _("Delivery point").upper(),
                "signature":
                staff.get_html_signature,
            })

            template_order_customer_mail.append(language_code)
            template_order_customer_mail.append(template.render(context))

            template = Template(
                repanier.apps.REPANIER_SETTINGS_CONFIG.order_producer_mail)
            context = TemplateContext({
                "name":
                _("Long name"),
                "long_profile_name":
                _("Long name"),
                "order_empty":
                False,
                "duplicate":
                True,
                "permanence_link":
                format_html('<a href="#">{}</a>', permanence),
                "signature":
                staff.get_html_signature,
            })

            template_order_producer_mail.append(language_code)
            template_order_producer_mail.append(template.render(context))

            board_composition = permanence.get_html_board_composition()
            template = Template(
                repanier.apps.REPANIER_SETTINGS_CONFIG.order_staff_mail)
            context = TemplateContext({
                "permanence_link":
                format_html('<a href="#">{}</a>', permanence),
                "board_composition":
                board_composition,
                "board_composition_and_description":
                board_composition,
                "signature":
                staff.get_html_signature,
            })

            template_order_staff_mail.append(language_code)
            template_order_staff_mail.append(template.render(context))

        translation.activate(cur_language)

        order_customer_email_will_be_sent, order_customer_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=True)
        order_producer_email_will_be_sent, order_producer_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=True)
        order_board_email_will_be_sent, order_board_email_will_be_sent_to = RepanierEmail.send_email_to_who(
            is_email_send=repanier.apps.
            REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD,
            board=True,
        )

        form = CloseAndSendOrderForm(
            initial={
                "template_order_customer_mail":
                mark_safe("<br>==============<br>".join(
                    template_order_customer_mail)),
                "template_order_producer_mail":
                mark_safe("<br>==============<br>".join(
                    template_order_producer_mail)),
                "template_order_staff_mail":
                mark_safe("<br>==============<br>".join(
                    template_order_staff_mail)),
            })
        if permanence.with_delivery_point:
            deliveries = DeliveryBoard.objects.filter(
                permanence_id=permanence.id,
                status__in=[PERMANENCE_OPENED, PERMANENCE_CLOSED],
            )
        else:
            deliveries = DeliveryBoard.objects.none()
        template_name = get_repanier_template_name(
            "confirm_admin_send_order.html")
        return render(
            request,
            template_name,
            {
                "sub_title":
                _("Please, confirm the action : send orders"),
                "action_checkbox_name":
                admin.ACTION_CHECKBOX_NAME,
                "action":
                "close_and_send_order",
                "permanence":
                permanence,
                "with_delivery_point":
                permanence.with_delivery_point,
                "deliveries":
                deliveries,
                "form":
                form,
                "order_customer_email_will_be_sent":
                order_customer_email_will_be_sent,
                "order_customer_email_will_be_sent_to":
                order_customer_email_will_be_sent_to,
                "order_producer_email_will_be_sent":
                order_producer_email_will_be_sent,
                "order_producer_email_will_be_sent_to":
                order_producer_email_will_be_sent_to,
                "order_board_email_will_be_sent":
                order_board_email_will_be_sent,
                "order_board_email_will_be_sent_to":
                order_board_email_will_be_sent_to,
            },
        )
Example #20
0
    def close_and_send_order(self, request, queryset):
        if 'cancel' in request.POST:
            user_message = _("Action canceled by the user.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return
        permanence = queryset.first()
        if permanence.status not in [PERMANENCE_OPENED, PERMANENCE_CLOSED]:
            user_message = _("The status of %(permanence)s prohibit you to perform this action.") % {
                'permanence': permanence}
            user_message_level = messages.ERROR
            self.message_user(request, user_message, user_message_level)
            return
        only_deliveries = permanence.with_delivery_point
        if 'apply' in request.POST:
            all_deliveries = True if request.POST.get("all-deliveries", False) else False
            deliveries_to_be_send = request.POST.getlist("deliveries", [])
            all_producers = True if request.POST.get("all-producers", False) else False
            producers_to_be_send = request.POST.getlist("producers", [])
            if only_deliveries:
                if not all_deliveries and len(deliveries_to_be_send) == 0:
                    user_message = _("You must select at least one delivery point.")
                    user_message_level = messages.WARNING
                    self.message_user(request, user_message, user_message_level)
                    return
            else:
                if not all_producers and len(producers_to_be_send) == 0:
                    user_message = _("You must select at least one producer.")
                    user_message_level = messages.WARNING
                    self.message_user(request, user_message, user_message_level)
                    return
                if not all_producers and settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER:
                    user_message = _("You must select all producers because the customers must confirm orders.")
                    user_message_level = messages.WARNING
                    self.message_user(request, user_message, user_message_level)
                    return
            everything = all_producers or all_deliveries
            # close_and_send_order(permanence.id, everything, producers_to_be_send, deliveries_to_be_send)
            t = threading.Thread(target=close_and_send_order,
                                 args=(permanence.id, everything, producers_to_be_send, deliveries_to_be_send))
            t.start()
            user_message = _("The orders are being send.")
            user_message_level = messages.INFO
            self.message_user(request, user_message, user_message_level)
            return

        template_order_customer_mail = []
        template_order_producer_mail = []
        template_order_staff_mail = []
        cur_language = translation.get_language()
        for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
            language_code = language["code"]
            translation.activate(language_code)

            template = Template(repanier.apps.REPANIER_SETTINGS_CONFIG.order_customer_mail)

            staff = Staff.get_or_create_order_responsible()

            customer_last_balance = \
                _('The balance of your account as of %(date)s is %(balance)s.') % {
                    'date': timezone.now().strftime(settings.DJANGO_SETTINGS_DATE),
                    'balance': RepanierMoney(123.45)
                }
            customer_on_hold_movement = \
                _(
                    'This balance does not take account of any unrecognized payments %(bank)s and any unbilled order %(other_order)s.') \
                % {
                    'bank': RepanierMoney(123.45),
                    'other_order': RepanierMoney(123.45)
                }

            bank_account_number = repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT
            if bank_account_number is not None:
                group_name = repanier.apps.REPANIER_SETTINGS_GROUP_NAME
                if permanence.short_name:
                    communication = "{} ({})".format(_('Short name'), permanence.short_name)
                else:
                    communication = _('Short name')
                customer_payment_needed = "<font color=\"#bd0926\">{}</font>".format(
                    _(
                        'Please pay %(payment)s to the bank account %(name)s %(number)s with communication %(communication)s.') % {
                        'payment': RepanierMoney(123.45),
                        'name': group_name,
                        'number': bank_account_number,
                        'communication': communication
                    }
                )
            else:
                customer_payment_needed = EMPTY_STRING
            context = TemplateContext({
                'name': _('Long name'),
                'long_basket_name': _('Long name'),
                'basket_name': _('Short name'),
                'short_basket_name': _('Short name'),
                'permanence_link': mark_safe("<a href=\"#\">{}</a>".format(permanence)),
                'last_balance': mark_safe("<a href=\"#\">{}</a>".format(customer_last_balance)),
                'order_amount': RepanierMoney(123.45),
                'on_hold_movement': mark_safe(customer_on_hold_movement),
                'payment_needed': mark_safe(customer_payment_needed),
                'delivery_point': _('Delivery point').upper(),
                'signature': staff.get_html_signature,
            })

            template_order_customer_mail.append(language_code)
            template_order_customer_mail.append(template.render(context))

            template = Template(repanier.apps.REPANIER_SETTINGS_CONFIG.order_producer_mail)
            context = TemplateContext({
                'name': _('Long name'),
                'long_profile_name': _('Long name'),
                'order_empty': False,
                'duplicate': True,
                'permanence_link': format_html("<a href=\"#\">{}</a>", permanence),
                'signature': staff.get_html_signature,
            })

            template_order_producer_mail.append(language_code)
            template_order_producer_mail.append(template.render(context))

            board_composition, board_composition_and_description = get_board_composition(permanence.id)
            template = Template(repanier.apps.REPANIER_SETTINGS_CONFIG.order_staff_mail)
            context = TemplateContext({
                'permanence_link': format_html("<a href=\"#\">{}</a>", permanence),
                'board_composition': mark_safe(board_composition),
                'board_composition_and_description': mark_safe(board_composition_and_description),
                'signature': staff.get_html_signature,
            })

            template_order_staff_mail.append(language_code)
            template_order_staff_mail.append(template.render(context))

        translation.activate(cur_language)

        order_customer_email_will_be_sent, order_customer_email_will_be_sent_to = send_email_to_who()
        order_producer_email_will_be_sent, order_producer_email_will_be_sent_to = send_email_to_who()
        order_board_email_will_be_sent, order_board_email_will_be_sent_to = send_email_to_who(
            repanier.apps.REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD, board=True
        )

        form = CloseAndSendOrderForm(
            initial={
                'template_order_customer_mail': mark_safe(
                    "<br>==============<br>".join(template_order_customer_mail)),
                'template_order_producer_mail': mark_safe(
                    "<br>==============<br>".join(template_order_producer_mail)),
                'template_order_staff_mail': mark_safe("<br>==============<br>".join(template_order_staff_mail)),
            }
        )
        if only_deliveries:
            # /!\ If one delivery point has been closed, I may not close anymore by producer
            producers = Producer.objects.none()
            deliveries = DeliveryBoard.objects.filter(
                permanence_id=permanence.id,
                status__in=[PERMANENCE_OPENED, PERMANENCE_CLOSED]
            )
        else:
            producers = Producer.objects.filter(
                producerinvoice__permanence_id=permanence.id,
                producerinvoice__status__in=[PERMANENCE_OPENED, PERMANENCE_CLOSED]
            )
            deliveries = DeliveryBoard.objects.none()
        return render(
            request,
            'repanier/confirm_admin_send_order.html', {
                'sub_title': _("Please, confirm the action : send orders"),
                'action_checkbox_name': admin.ACTION_CHECKBOX_NAME,
                'action': 'close_and_send_order',
                'permanence': permanence,
                'only_deliveries': only_deliveries,
                'deliveries': deliveries,
                'producers': producers,
                'form': form,
                'order_customer_email_will_be_sent': order_customer_email_will_be_sent,
                'order_customer_email_will_be_sent_to': order_customer_email_will_be_sent_to,
                'order_producer_email_will_be_sent': order_producer_email_will_be_sent,
                'order_producer_email_will_be_sent_to': order_producer_email_will_be_sent_to,
                'order_board_email_will_be_sent': order_board_email_will_be_sent,
                'order_board_email_will_be_sent_to': order_board_email_will_be_sent_to
            })
Example #21
0
DECIMAL_0_10 = Decimal("0.10")
DECIMAL_0_12 = Decimal("0.12")
DECIMAL_0_20 = Decimal("0.20")
DECIMAL_0_21 = Decimal("0.21")

DECIMAL_MAX_STOCK = Decimal("999999")
ZERO_DECIMAL = Decimal("0")
ONE_DECIMAL = Decimal("0.1")
TWO_DECIMALS = Decimal("0.01")
THREE_DECIMALS = Decimal("0.001")
FOUR_DECIMALS = Decimal("0.0001")

ONE_LEVEL_DEPTH = 0
TWO_LEVEL_DEPTH = 1

REPANIER_MONEY_ZERO = RepanierMoney()

PERMANENCE_PLANNED = "100"
PERMANENCE_WAIT_FOR_OPEN = "200"
PERMANENCE_OPENED = "300"
PERMANENCE_WAIT_FOR_CLOSED = "350"
PERMANENCE_CLOSED = "370"
PERMANENCE_WAIT_FOR_SEND = "400"
PERMANENCE_SEND = "500"
PERMANENCE_WAIT_FOR_INVOICED = "600"
PERMANENCE_WAIT_FOR_CANCEL_INVOICE = "700"
PERMANENCE_INVOICED = "800"
PERMANENCE_ARCHIVED = "900"
PERMANENCE_CANCELLED = "950"

LUT_PERMANENCE_STATUS = (
Example #22
0
def admin_generate_bank_account_movement(permanence, payment_date,
                                         customer_buyinggroup):

    for producer_invoice in ProducerInvoice.objects.filter(
            permanence_id=permanence.id,
            invoice_sort_order__isnull=True,
            to_be_paid=True).select_related("producer"):
        # We have to pay something
        producer = producer_invoice.producer
        result_set = BankAccount.objects.filter(
            producer_id=producer.id,
            producer_invoice__isnull=True).order_by('?').aggregate(
                Sum('bank_amount_in'), Sum('bank_amount_out'))
        if result_set["bank_amount_in__sum"] is not None:
            bank_in = RepanierMoney(result_set["bank_amount_in__sum"])
        else:
            bank_in = REPANIER_MONEY_ZERO
        if result_set["bank_amount_out__sum"] is not None:
            bank_out = RepanierMoney(result_set["bank_amount_out__sum"])
        else:
            bank_out = REPANIER_MONEY_ZERO
        bank_not_invoiced = bank_out - bank_in

        if producer.balance.amount != DECIMAL_ZERO or producer_invoice.to_be_invoiced_balance.amount != DECIMAL_ZERO or bank_not_invoiced != DECIMAL_ZERO:

            delta = (producer_invoice.to_be_invoiced_balance.amount -
                     bank_not_invoiced.amount).quantize(TWO_DECIMALS)
            if delta > DECIMAL_ZERO:

                if producer_invoice.invoice_reference:
                    operation_comment = producer_invoice.invoice_reference
                else:
                    if producer.represent_this_buyinggroup:
                        operation_comment = permanence.get_permanence_display()
                    else:
                        if producer_invoice is not None:
                            if producer_invoice.total_price_with_tax.amount == delta:
                                operation_comment = _("Delivery %(current_site)s - %(permanence)s. Thanks!") \
                                                    % {
                                                        'current_site': REPANIER_SETTINGS_GROUP_NAME,
                                                        'permanence'  : permanence.get_permanence_display()
                                                    }
                            else:
                                operation_comment = _(
                                    "Deliveries %(current_site)s - up to the %(permanence)s (included). Thanks!") \
                                                    % {
                                                        'current_site': REPANIER_SETTINGS_GROUP_NAME,
                                                        'permanence'  : permanence.get_permanence_display()
                                                    }
                        else:
                            operation_comment = _(
                                "Deliveries %(current_site)s - up to %(payment_date)s (included). Thanks!") \
                                                % {
                                                    'current_site': REPANIER_SETTINGS_GROUP_NAME,
                                                    'payment_date': payment_date.strftime(
                                                        settings.DJANGO_SETTINGS_DATE)
                                                }

                BankAccount.objects.create(
                    permanence_id=None,
                    producer_id=producer.id,
                    customer=None,
                    operation_date=payment_date,
                    operation_status=BANK_CALCULATED_INVOICE,
                    operation_comment=cap(operation_comment, 100),
                    bank_amount_out=delta,
                    customer_invoice=None,
                    producer_invoice=None)

        delta = (producer.balance.amount -
                 producer_invoice.to_be_invoiced_balance.amount
                 ).quantize(TWO_DECIMALS)
        if delta != DECIMAL_ZERO:
            # Profit or loss for the group
            operation_comment = _("Correction %(producer)s") \
                                % {
                                    'producer': producer.short_profile_name
                                }
            BankAccount.objects.create(
                permanence_id=permanence.id,
                producer=None,
                customer_id=customer_buyinggroup.id,
                operation_date=payment_date,
                operation_status=BANK_PROFIT,
                operation_comment=cap(operation_comment, 100),
                bank_amount_in=delta if delta > DECIMAL_ZERO else DECIMAL_ZERO,
                bank_amount_out=-delta
                if delta < DECIMAL_ZERO else DECIMAL_ZERO,
                customer_invoice_id=None,
                producer_invoice=None)
        producer_invoice.balance.amount -= delta
        producer_invoice.save(update_fields=['balance'])
        producer.balance.amount -= delta
        producer.save(update_fields=['balance'])

    return