예제 #1
0
 def export_xlsx_customer_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, PERMANENCE_SEND]:
         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 not permanence.with_delivery_point:
         # Perform the action directly. Do not ask to select any delivery point.
         response = None
         wb = generate_customer_xlsx(permanence)[0]
         if wb is not None:
             response = HttpResponse(
                 content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
             response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
                 _("Customers"),
                 permanence
             )
             wb.save(response)
         return response
     if 'apply' in request.POST:
         if admin.ACTION_CHECKBOX_NAME in request.POST:
             deliveries_to_be_exported = request.POST.getlist("deliveries", [])
             if len(deliveries_to_be_exported) == 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
                 # Also display order without delivery point -> The customer has not selected it yet
                 # deliveries_to_be_exported.append(None)
         else:
             deliveries_to_be_exported = ()
         response = None
         wb = generate_customer_xlsx(permanence, deliveries_id=deliveries_to_be_exported)[0]
         if wb is not None:
             response = HttpResponse(
                 content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
             response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
                 _("Customers"),
                 permanence
             )
             wb.save(response)
         return response
     return render(
         request,
         'repanier/confirm_admin_export_customer_order.html', {
             'sub_title': _("Please, confirm the action : Export customer orders"),
             'action_checkbox_name': admin.ACTION_CHECKBOX_NAME,
             'action': 'export_xlsx_customer_order',
             'permanence': permanence,
             'deliveries': DeliveryBoard.objects.filter(
                 permanence_id=permanence.id
             ),
         })
예제 #2
0
def export_order_2_1_group(config, delivery_id, filename, permanence, order_responsible):
    delivery_board = DeliveryBoard.objects.filter(
        id=delivery_id
    ).exclude(
        delivery_point__customer_responsible=None
    ).order_by('?').first()
    if delivery_board is None:
        return

    delivery_point = delivery_board.delivery_point
    customer_responsible = delivery_point.customer_responsible

    wb = generate_customer_xlsx(permanence=permanence, deliveries_id=[delivery_id], group=True)[0]
    if wb is not None:

        order_customer_mail = config.safe_translation_getter(
            'order_customer_mail', any_language=True, default=EMPTY_STRING
        )
        order_customer_mail_subject = "{} - {}".format(settings.REPANIER_SETTINGS_GROUP_NAME, permanence)

        long_basket_name = customer_responsible.long_basket_name or str(customer_responsible)

        template = Template(order_customer_mail)
        context = TemplateContext({
            'name': long_basket_name,
            'long_basket_name': long_basket_name,  # deprecated
            'basket_name': str(customer_responsible),
            'short_basket_name': str(customer_responsible),  # deprecated
            'permanence_link': mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                settings.ALLOWED_HOSTS[0], reverse('order_view', args=(permanence.id,)), permanence)),
            'last_balance_link': mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                settings.ALLOWED_HOSTS[0], reverse('customer_invoice_view', args=(0,)), _("Group invoices"))),
            'last_balance': EMPTY_STRING,
            'order_amount': EMPTY_STRING,
            'on_hold_movement': EMPTY_STRING,
            'payment_needed': EMPTY_STRING,
            'delivery_point': delivery_point,
            'signature': order_responsible.get_html_signature
        })
        html_body = template.render(context)

        to_email = [customer_responsible.user.email]
        if customer_responsible.email2:
            to_email.append(customer_responsible.email2)
        to_email = list(set(to_email + order_responsible.get_to_email))

        email = RepanierEmail(
            subject=order_customer_mail_subject,
            html_body=html_body,
            to=to_email,
        )
        email.attach(filename,
                     save_virtual_workbook(wb),
                     'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

        email.send_email()
예제 #3
0
def export_order_2_1_customer(customer, filename, permanence, order_responsible=None,
                              abstract_ws=None, cancel_order=False):
    from repanier.apps import \
        REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER, \
        REPANIER_SETTINGS_CONFIG

    config = REPANIER_SETTINGS_CONFIG
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence.id,
        customer_id=customer.id
    ).order_by('?').first()
    if customer_invoice is not None:
        if order_responsible is None:
            order_responsible = Staff.get_or_create_order_responsible()
        wb = generate_customer_xlsx(permanence=permanence, customer_invoice=customer_invoice)[0]
        if wb is not None:
            to_email = [customer.user.email]
            if customer.email2:
                to_email.append(customer.email2)
            if customer_invoice.delivery is not None:
                delivery_point = customer_invoice.delivery
                if delivery_point.delivery_point.inform_customer_responsible and delivery_point.delivery_point.customer_responsible is not None:
                    customer_responsible = delivery_point.delivery_point.customer_responsible
                    if customer_responsible.id != customer.id:
                        to_email.append(customer_responsible.user.email)
                        if customer_responsible.email2:
                            to_email.append(customer_responsible.email2)
            else:
                delivery_point = EMPTY_STRING
            customer_last_balance, customer_on_hold_movement, customer_payment_needed, customer_order_amount = payment_message(
                customer, permanence)
            long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name
            if cancel_order:
                order_customer_mail = config.safe_translation_getter(
                    'cancel_order_customer_mail', any_language=True, default=EMPTY_STRING
                )
                order_customer_mail_subject = "{} - {} - {}".format(
                    _('⚠ Order cancelled'), settings.REPANIER_SETTINGS_GROUP_NAME, permanence)
            else:
                order_customer_mail = config.safe_translation_getter(
                    'order_customer_mail', any_language=True, default=EMPTY_STRING
                )
                order_customer_mail_subject = "{} - {}".format(settings.REPANIER_SETTINGS_GROUP_NAME, permanence)

            template = Template(order_customer_mail)
            context = TemplateContext({
                'name': long_basket_name,
                'long_basket_name': long_basket_name,
                'basket_name': customer.short_basket_name,
                'short_basket_name': customer.short_basket_name,
                'permanence_link': mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                    settings.ALLOWED_HOSTS[0], reverse('order_view', args=(permanence.id,)), permanence)),
                'last_balance_link': mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                    settings.ALLOWED_HOSTS[0], reverse('customer_invoice_view', args=(0,)), customer_last_balance)),
                'last_balance': mark_safe(customer_last_balance),
                'order_amount': mark_safe(customer_order_amount),
                'on_hold_movement': customer_on_hold_movement,
                'payment_needed': mark_safe(customer_payment_needed),
                'delivery_point': delivery_point,
                'signature': order_responsible.get_html_signature
            })
            html_body = template.render(context)

            email = RepanierEmail(
                subject=order_customer_mail_subject,
                html_body=html_body,
                to=to_email,
                show_customer_may_unsubscribe=False,
                send_even_if_unsubscribed=True
            )
            if not cancel_order and REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER:
                if abstract_ws is not None:
                    wb.add_sheet(abstract_ws, index=0)
            email.attach(filename,
                         save_virtual_workbook(wb),
                         'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

            email.send_email()
예제 #4
0
def email_order(permanence_id, everything=True, deliveries_id=()):
    from repanier.apps import REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD, REPANIER_SETTINGS_CONFIG
    cur_language = translation.get_language()
    for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
        language_code = language["code"]
        translation.activate(language_code)
        permanence = Permanence.objects.get(id=permanence_id)
        config = REPANIER_SETTINGS_CONFIG
        filename = "{}-{}.xlsx".format(
            _("Order"),
            permanence
        )
        order_responsible = Staff.get_or_create_order_responsible()

        if len(deliveries_id) > 0:
            for delivery_id in deliveries_id:
                # Send a recap of the orders to the responsible
                export_order_2_1_group(
                    config, delivery_id, filename,
                    permanence,
                    order_responsible
                )

        if not everything:
            abstract_ws = None
        else:
            # Orders send to the preparation team, to the order_responsible
            wb, abstract_ws = generate_customer_xlsx(permanence=permanence, deliveries_id=deliveries_id)
            if wb is not None:
                # At least one order
                order_staff_mail = config.safe_translation_getter(
                    'order_staff_mail', any_language=True, default=EMPTY_STRING
                )
                order_staff_mail_subject = "{} - {}".format(settings.REPANIER_SETTINGS_GROUP_NAME, permanence)

                board_composition, board_composition_and_description = get_board_composition(permanence.id)

                template = Template(order_staff_mail)
                context = TemplateContext({
                    'permanence_link': mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                        settings.ALLOWED_HOSTS[0], reverse('order_view', args=(permanence.id,)), permanence)),
                    'board_composition': mark_safe(board_composition),
                    'board_composition_and_description': mark_safe(board_composition_and_description),
                    'signature': order_responsible.get_html_signature
                })
                html_body = template.render(context)

                if REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD:
                    to_email = []
                    for permanence_board in PermanenceBoard.objects.filter(
                            permanence_id=permanence.id).order_by('?'):
                        if permanence_board.customer:
                            to_email.append(permanence_board.customer.user.email)
                    to_email = list(set(to_email + order_responsible.get_to_email))
                else:
                    to_email = list(set(order_responsible.get_to_email))

                email = RepanierEmail(
                    subject=order_staff_mail_subject,
                    html_body=html_body,
                    to=to_email,
                )
                email.attach(filename,
                             save_virtual_workbook(wb),
                             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                email.send_email()

        # Orders send to our producers
        # producer_set = Producer.objects.filter(
        #     permanence=permanence,
        #     language=language_code,
        # ).order_by('?')
        # all_producers = permanence.contract.producers.all() if permanence.contract else permanence.producers.all()
        producer_set = Producer.objects.filter(
            permanence=permanence,
            language=language_code,
        ).order_by('?')
        for producer in producer_set:
            long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name
            wb = generate_producer_xlsx(permanence=permanence, producer=producer, wb=None)

            order_producer_mail = config.safe_translation_getter(
                'order_producer_mail', any_language=True, default=EMPTY_STRING
            )
            order_producer_mail_subject = "{} - {}".format(settings.REPANIER_SETTINGS_GROUP_NAME, permanence)

            template = Template(order_producer_mail)
            context = TemplateContext({
                'name': long_profile_name,
                'long_profile_name': long_profile_name,
                'order_empty': wb is None,
                'duplicate': not (wb is None or producer.manage_replenishment),
                'permanence_link': mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                    settings.ALLOWED_HOSTS[0], reverse('order_view', args=(permanence.id,)), permanence)),
                'signature': order_responsible.get_html_signature
            })
            html_body = template.render(context)

            producer_invoice = ProducerInvoice.objects.filter(
                producer_id=producer.id, permanence_id=permanence.id
            ).order_by('?').first()

            to_email = []
            if producer_invoice is not None \
                    and producer_invoice.get_total_price_with_tax() < producer.minimum_order_value:
                html_body = "{}<br><br>{}".format(
                    order_producer_mail_subject, html_body
                )
                order_producer_mail_subject = _(
                    '⚠ Mail not send to our producer {} because the minimum order value has not been reached.').format(
                    long_profile_name)
            else:
                if producer.email:
                    to_email.append(producer.email)
                if producer.email2:
                    to_email.append(producer.email2)
                if producer.email3:
                    to_email.append(producer.email3)
            to_email = list(set(to_email + order_responsible.get_to_email))
            email = RepanierEmail(
                subject=order_producer_mail_subject,
                html_body=html_body,
                to=to_email,
            )
            if wb is not None:
                if producer.represent_this_buyinggroup:
                    if abstract_ws is not None:
                        wb.add_sheet(abstract_ws, index=0)
                email.attach(
                    filename,
                    save_virtual_workbook(wb),
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                )

            email.send_email()

        if everything:
            # Orders send to our customers only if they don't have already received it
            customer_set = Customer.objects.filter(
                represent_this_buyinggroup=False,
                customerinvoice__is_order_confirm_send=False,
                customerinvoice__permanence_id=permanence.id,
                language=language_code
            ).order_by('?')
            if len(deliveries_id) > 0:
                customer_set = customer_set.filter(
                    customerinvoice__delivery_id__in=deliveries_id,
                )
            for customer in customer_set:
                export_order_2_1_customer(
                    customer, filename, permanence,
                    order_responsible,
                    abstract_ws
                )
                customer_invoice = CustomerInvoice.objects.filter(
                    customer_id=customer.id,
                    permanence_id=permanence_id
                ).order_by('?').first()
                customer_invoice.confirm_order()
                customer_invoice.save()
    translation.activate(cur_language)
예제 #5
0
def export_order_2_1_customer(customer,
                              filename,
                              permanence,
                              order_responsible=None,
                              abstract_ws=None,
                              cancel_order=False):
    from repanier.apps import \
        REPANIER_SETTINGS_GROUP_NAME, \
        REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER, \
        REPANIER_SETTINGS_CONFIG

    config = REPANIER_SETTINGS_CONFIG
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence.id,
        customer_id=customer.id).order_by('?').first()
    if customer_invoice is not None:
        if order_responsible is None:
            order_responsible = Staff.get_or_create_order_responsible()
        wb = generate_customer_xlsx(permanence=permanence,
                                    customer_invoice=customer_invoice)[0]
        if wb is not None:
            to_email = [customer.user.email]
            if customer.email2:
                to_email.append(customer.email2)
            if customer_invoice.delivery is not None:
                delivery_point = customer_invoice.delivery
                if delivery_point.delivery_point.inform_customer_responsible and delivery_point.delivery_point.customer_responsible is not None:
                    customer_responsible = delivery_point.delivery_point.customer_responsible
                    if customer_responsible.id != customer.id:
                        to_email.append(customer_responsible.user.email)
                        if customer_responsible.email2:
                            to_email.append(customer_responsible.email2)
            else:
                delivery_point = EMPTY_STRING
            customer_last_balance, customer_on_hold_movement, customer_payment_needed, customer_order_amount = payment_message(
                customer, permanence)
            long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name
            if cancel_order:
                order_customer_mail = config.safe_translation_getter(
                    'cancel_order_customer_mail',
                    any_language=True,
                    default=EMPTY_STRING)
                order_customer_mail_subject = "{} - {} - {}".format(
                    _('⚠ Order cancelled'), REPANIER_SETTINGS_GROUP_NAME,
                    permanence)
            else:
                order_customer_mail = config.safe_translation_getter(
                    'order_customer_mail',
                    any_language=True,
                    default=EMPTY_STRING)
                order_customer_mail_subject = "{} - {}".format(
                    REPANIER_SETTINGS_GROUP_NAME, permanence)

            template = Template(order_customer_mail)
            context = TemplateContext({
                'name':
                long_basket_name,
                'long_basket_name':
                long_basket_name,
                'basket_name':
                customer.short_basket_name,
                'short_basket_name':
                customer.short_basket_name,
                'permanence_link':
                mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                    settings.ALLOWED_HOSTS[0],
                    reverse('order_view', args=(permanence.id, )),
                    permanence)),
                'last_balance_link':
                mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                    settings.ALLOWED_HOSTS[0],
                    reverse('customer_invoice_view', args=(0, )),
                    customer_last_balance)),
                'last_balance':
                mark_safe(customer_last_balance),
                'order_amount':
                mark_safe(customer_order_amount),
                'on_hold_movement':
                customer_on_hold_movement,
                'payment_needed':
                mark_safe(customer_payment_needed),
                'delivery_point':
                delivery_point,
                'signature':
                order_responsible.get_html_signature
            })
            html_body = template.render(context)

            if settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER:
                to_email = list(set(to_email + order_responsible.get_to_email))

            email = RepanierEmail(
                subject=order_customer_mail_subject,
                html_body=html_body,
                from_email=order_responsible.get_from_email,
                to=to_email,
                reply_to=order_responsible.get_reply_to_email,
                show_customer_may_unsubscribe=False,
                send_even_if_unsubscribed=True)
            if not cancel_order and REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER:
                if abstract_ws is not None:
                    wb.add_sheet(abstract_ws, index=0)
            email.attach(
                filename, save_virtual_workbook(wb),
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            )

            email.send_email()
예제 #6
0
def export_order_2_1_group(config, delivery_id, filename, permanence,
                           order_responsible):
    delivery_board = DeliveryBoard.objects.filter(id=delivery_id).exclude(
        delivery_point__customer_responsible=None).order_by('?').first()
    if delivery_board is None:
        return

    from repanier.apps import REPANIER_SETTINGS_GROUP_NAME
    delivery_point = delivery_board.delivery_point
    customer_responsible = delivery_point.customer_responsible

    wb = generate_customer_xlsx(permanence=permanence,
                                deliveries_id=[delivery_id],
                                group=True)[0]
    if wb is not None:

        order_customer_mail = config.safe_translation_getter(
            'order_customer_mail', any_language=True, default=EMPTY_STRING)
        order_customer_mail_subject = "{} - {}".format(
            REPANIER_SETTINGS_GROUP_NAME, permanence)

        long_basket_name = customer_responsible.long_basket_name or str(
            customer_responsible)

        template = Template(order_customer_mail)
        context = TemplateContext({
            'name':
            long_basket_name,
            'long_basket_name':
            long_basket_name,  # deprecated
            'basket_name':
            str(customer_responsible),
            'short_basket_name':
            str(customer_responsible),  # deprecated
            'permanence_link':
            mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                settings.ALLOWED_HOSTS[0],
                reverse('order_view', args=(permanence.id, )), permanence)),
            'last_balance_link':
            mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                settings.ALLOWED_HOSTS[0],
                reverse('customer_invoice_view', args=(0, )),
                _("Group invoices"))),
            'last_balance':
            EMPTY_STRING,
            'order_amount':
            EMPTY_STRING,
            'on_hold_movement':
            EMPTY_STRING,
            'payment_needed':
            EMPTY_STRING,
            'delivery_point':
            delivery_point,
            'signature':
            order_responsible.get_html_signature
        })
        html_body = template.render(context)

        to_email = [customer_responsible.user.email]
        if customer_responsible.email2:
            to_email.append(customer_responsible.email2)
        to_email = list(set(to_email + order_responsible.get_to_email))

        email = RepanierEmail(subject=order_customer_mail_subject,
                              html_body=html_body,
                              from_email=order_responsible.get_from_email,
                              to=to_email,
                              reply_to=order_responsible.get_reply_to_email)
        email.attach(
            filename, save_virtual_workbook(wb),
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        )

        email.send_email()
예제 #7
0
def email_order(permanence_id,
                everything=True,
                producers_id=(),
                deliveries_id=()):
    from repanier.apps import REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD, \
        REPANIER_SETTINGS_GROUP_NAME, \
        REPANIER_SETTINGS_CONFIG
    cur_language = translation.get_language()
    for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
        language_code = language["code"]
        translation.activate(language_code)
        permanence = Permanence.objects.get(id=permanence_id)
        config = REPANIER_SETTINGS_CONFIG
        filename = "{}-{}.xlsx".format(_("Order"), permanence)
        order_responsible = Staff.get_or_create_order_responsible()

        if len(deliveries_id) > 0:
            # if closed deliveries_id is not empty list and not "None" then all_producers should be True
            everything = True
            for delivery_id in deliveries_id:
                # Send a recap of the orders to the responsible
                export_order_2_1_group(config, delivery_id, filename,
                                       permanence, order_responsible)

        if not everything:
            abstract_ws = None
        else:
            # Orders send to the preparation team, to the order_responsible
            wb, abstract_ws = generate_customer_xlsx(
                permanence=permanence, deliveries_id=deliveries_id)
            if wb is not None:
                # At least one order
                order_staff_mail = config.safe_translation_getter(
                    'order_staff_mail',
                    any_language=True,
                    default=EMPTY_STRING)
                order_staff_mail_subject = "{} - {}".format(
                    REPANIER_SETTINGS_GROUP_NAME, permanence)

                board_composition, board_composition_and_description = get_board_composition(
                    permanence.id)

                template = Template(order_staff_mail)
                context = TemplateContext({
                    'permanence_link':
                    mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                        settings.ALLOWED_HOSTS[0],
                        reverse('order_view', args=(permanence.id, )),
                        permanence)),
                    'board_composition':
                    mark_safe(board_composition),
                    'board_composition_and_description':
                    mark_safe(board_composition_and_description),
                    'signature':
                    order_responsible.get_html_signature
                })
                html_body = template.render(context)

                if REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD:
                    to_email = []
                    for permanence_board in PermanenceBoard.objects.filter(
                            permanence_id=permanence.id).order_by('?'):
                        if permanence_board.customer:
                            to_email.append(
                                permanence_board.customer.user.email)
                    to_email = list(
                        set(to_email + order_responsible.get_to_email))
                else:
                    to_email = list(set(order_responsible.get_to_email))

                email = RepanierEmail(
                    subject=order_staff_mail_subject,
                    html_body=html_body,
                    from_email=order_responsible.get_from_email,
                    to=to_email,
                    reply_to=order_responsible.get_reply_to_email)
                email.attach(
                    filename, save_virtual_workbook(wb),
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                )
                email.send_email()

        # Orders send to our producers
        producer_set = Producer.objects.filter(
            permanence=permanence,
            language=language_code,
        ).order_by('?')
        if len(producers_id) > 0:
            producer_set = producer_set.filter(id__in=producers_id)
        for producer in producer_set:
            long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name
            wb = generate_producer_xlsx(permanence=permanence,
                                        producer=producer,
                                        wb=None)

            order_producer_mail = config.safe_translation_getter(
                'order_producer_mail', any_language=True, default=EMPTY_STRING)
            order_producer_mail_subject = "{} - {}".format(
                REPANIER_SETTINGS_GROUP_NAME, permanence)

            template = Template(order_producer_mail)
            context = TemplateContext({
                'name':
                long_profile_name,
                'long_profile_name':
                long_profile_name,
                'order_empty':
                wb is None,
                'duplicate':
                not (wb is None or producer.manage_replenishment),
                'permanence_link':
                mark_safe("<a href=\"https://{}{}\">{}</a>".format(
                    settings.ALLOWED_HOSTS[0],
                    reverse('order_view', args=(permanence.id, )),
                    permanence)),
                'signature':
                order_responsible.get_html_signature
            })
            html_body = template.render(context)

            producer_invoice = ProducerInvoice.objects.filter(
                producer_id=producer.id,
                permanence_id=permanence.id).order_by('?').first()

            to_email = []
            if producer_invoice is not None \
                    and producer_invoice.get_total_price_with_tax() < producer.minimum_order_value:
                html_body = "{}<br><br>{}".format(order_producer_mail_subject,
                                                  html_body)
                order_producer_mail_subject = _(
                    '⚠ Mail not send to our producer {} because the minimum order value has not been reached.'
                ).format(long_profile_name)
            else:
                if producer.email:
                    to_email.append(producer.email)
                if producer.email2:
                    to_email.append(producer.email2)
                if producer.email3:
                    to_email.append(producer.email3)
            to_email = list(set(to_email + order_responsible.get_to_email))
            email = RepanierEmail(
                subject=order_producer_mail_subject,
                html_body=html_body,
                from_email=order_responsible.get_from_email,
                to=to_email,
                reply_to=order_responsible.get_reply_to_email)
            if wb is not None:
                if producer.represent_this_buyinggroup:
                    if abstract_ws is not None:
                        wb.add_sheet(abstract_ws, index=0)
                email.attach(
                    filename, save_virtual_workbook(wb),
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                )

            email.send_email()

        if everything:
            # Orders send to our customers only if they don't have already received it
            # ==> customerinvoice__is_order_confirm_send=False
            #     customerinvoice__permanence_id=permanence.id
            if not settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER:
                # -> Do not send cancelled orders
                customer_set = Customer.objects.filter(
                    represent_this_buyinggroup=False,
                    customerinvoice__is_order_confirm_send=False,
                    customerinvoice__permanence_id=permanence.id,
                    language=language_code).order_by('?')
                if len(deliveries_id) > 0:
                    customer_set = customer_set.filter(
                        customerinvoice__delivery_id__in=deliveries_id, )
                for customer in customer_set:
                    export_order_2_1_customer(customer, filename, permanence,
                                              order_responsible, abstract_ws)
                    # confirm_customer_invoice(permanence_id, customer.id)
                    customer_invoice = CustomerInvoice.objects.filter(
                        customer_id=customer.id,
                        permanence_id=permanence_id).order_by('?').first()
                    customer_invoice.confirm_order()
                    customer_invoice.save()
    translation.activate(cur_language)
예제 #8
0
def export_order_2_1_customer(customer,
                              filename,
                              permanence,
                              sender_email,
                              sender_function,
                              signature,
                              abstract_ws=None,
                              cancel_order=False):
    from repanier.apps import REPANIER_SETTINGS_SEND_CANCEL_ORDER_MAIL_TO_CUSTOMER, \
        REPANIER_SETTINGS_GROUP_NAME, REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS, \
        REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER, \
        REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_CUSTOMER
    config = Configuration.objects.get(id=DECIMAL_ONE)
    if (cancel_order and REPANIER_SETTINGS_SEND_CANCEL_ORDER_MAIL_TO_CUSTOMER) \
            or REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_CUSTOMER:
        customer_invoice = CustomerInvoice.objects.filter(
            permanence_id=permanence.id,
            customer_id=customer.id).order_by('?').first()
        if customer_invoice is not None:
            wb = generate_customer_xlsx(permanence=permanence,
                                        customer=customer)[0]
            if wb is not None:

                to_email_customer = [customer.user.email]
                if cancel_order or REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
                    to_email_customer.append(sender_email)
                if customer.email2:
                    to_email_customer.append(customer.email2)
                if customer_invoice.delivery is not None:
                    delivery_point = customer_invoice.delivery
                    if delivery_point.delivery_point.inform_customer_responsible and delivery_point.delivery_point.customer_responsible is not None:
                        customer_responsible = delivery_point.delivery_point.customer_responsible
                        if customer_responsible.id != customer.id:
                            to_email_customer.append(
                                customer_responsible.user.email)
                            if customer_responsible.email2:
                                to_email_customer.append(
                                    customer_responsible.email2)
                else:
                    delivery_point = EMPTY_STRING
                customer_last_balance, customer_on_hold_movement, customer_payment_needed, customer_order_amount = payment_message(
                    customer, permanence)
                long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name
                if cancel_order:
                    order_customer_mail = config.safe_translation_getter(
                        'cancel_order_customer_mail',
                        any_language=True,
                        default=EMPTY_STRING)
                    order_customer_mail_subject = "%s - %s - %s" % (
                        _('/!\ Order cancelled'), REPANIER_SETTINGS_GROUP_NAME,
                        permanence)
                else:
                    order_customer_mail = config.safe_translation_getter(
                        'order_customer_mail',
                        any_language=True,
                        default=EMPTY_STRING)
                    order_customer_mail_subject = "%s - %s" % (
                        REPANIER_SETTINGS_GROUP_NAME, permanence)

                template = Template(order_customer_mail)
                context = TemplateContext({
                    'name':
                    long_basket_name,
                    'long_basket_name':
                    long_basket_name,
                    'basket_name':
                    customer.short_basket_name,
                    'short_basket_name':
                    customer.short_basket_name,
                    'permanence_link':
                    mark_safe('<a href="https://%s%s">%s</a>' %
                              (settings.ALLOWED_HOSTS[0],
                               reverse('order_view',
                                       args=(permanence.id, )), permanence)),
                    'last_balance_link':
                    mark_safe('<a href="https://%s%s">%s</a>' %
                              (settings.ALLOWED_HOSTS[0],
                               reverse('customer_invoice_view',
                                       args=(0, )), customer_last_balance)),
                    'last_balance':
                    mark_safe(customer_last_balance),
                    'order_amount':
                    mark_safe(customer_order_amount),
                    'on_hold_movement':
                    customer_on_hold_movement,
                    'payment_needed':
                    mark_safe(customer_payment_needed),
                    'delivery_point':
                    delivery_point,
                    'signature':
                    mark_safe('%s<br/>%s<br/>%s' %
                              (signature, sender_function,
                               REPANIER_SETTINGS_GROUP_NAME))
                })
                html_content = template.render(context)
                email = RepanierEmail(subject=order_customer_mail_subject,
                                      html_content=html_content,
                                      from_email=sender_email,
                                      to=to_email_customer)
                if not cancel_order and REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER:
                    if abstract_ws is not None:
                        wb.add_sheet(abstract_ws, index=0)
                email.attach(
                    filename, save_virtual_workbook(wb),
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                )

                email.send_email()
예제 #9
0
def export_order_2_1_group(config, customer, delivery_point,
                           closed_delivery_id, filename, permanence,
                           sender_email, sender_function, signature):
    from repanier.apps import REPANIER_SETTINGS_GROUP_NAME

    wb = generate_customer_xlsx(permanence=permanence,
                                deliveries_id=closed_delivery_id,
                                group=True)[0]
    if wb is not None:

        order_customer_mail = config.safe_translation_getter(
            'order_customer_mail', any_language=True, default=EMPTY_STRING)
        order_customer_mail_subject = "%s - %s" % (
            REPANIER_SETTINGS_GROUP_NAME, permanence)

        to_email_customer = [customer.user.email]
        if customer.email2:
            to_email_customer.append(customer.email2)

        long_basket_name = customer.long_basket_name if customer.long_basket_name is not None else customer.short_basket_name

        template = Template(order_customer_mail)
        context = TemplateContext({
            'name':
            long_basket_name,
            'long_basket_name':
            long_basket_name,
            'basket_name':
            customer.short_basket_name,
            'short_basket_name':
            customer.short_basket_name,
            'permanence_link':
            mark_safe(
                '<a href="https://%s%s">%s</a>' %
                (settings.ALLOWED_HOSTS[0],
                 reverse('order_view', args=(permanence.id, )), permanence)),
            'last_balance_link':
            mark_safe('<a href="https://%s%s">%s</a>' %
                      (settings.ALLOWED_HOSTS[0],
                       reverse('customer_invoice_view',
                               args=(0, )), _("Group invoices"))),
            'last_balance':
            EMPTY_STRING,
            'order_amount':
            EMPTY_STRING,
            'on_hold_movement':
            EMPTY_STRING,
            'payment_needed':
            EMPTY_STRING,
            'delivery_point':
            delivery_point,
            'signature':
            mark_safe(
                '%s<br/>%s<br/>%s' %
                (signature, sender_function, REPANIER_SETTINGS_GROUP_NAME))
        })
        html_content = template.render(context)
        email = RepanierEmail(subject=order_customer_mail_subject,
                              html_content=html_content,
                              from_email=sender_email,
                              to=to_email_customer)
        email.attach(
            filename, save_virtual_workbook(wb),
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        )

        email.send_email()
예제 #10
0
def email_order(permanence_id,
                all_producers=True,
                producers_id=None,
                closed_deliveries_id=None):
    from repanier.apps import REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD, \
        REPANIER_SETTINGS_GROUP_NAME, REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER, \
        REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_PRODUCER, \
        REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_CUSTOMER, \
        REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS
    cur_language = translation.get_language()
    for language in settings.PARLER_LANGUAGES[settings.SITE_ID]:
        language_code = language["code"]
        translation.activate(language_code)
        permanence = Permanence.objects.get(id=permanence_id)
        config = Configuration.objects.get(id=DECIMAL_ONE)
        filename = "{0}-{1}.xlsx".format(slugify(_("Order")),
                                         slugify(permanence))
        group_filename = "{0}-{1}.xlsx".format(
            slugify(REPANIER_SETTINGS_GROUP_NAME), slugify(filename))
        sender_email, sender_function, signature, cc_email_staff = get_signature(
            is_reply_to_order_email=True)

        if closed_deliveries_id:
            # closed_deliveries_id is not empty list and not "None"
            # all_producers is True
            all_producers = True
            for delivery_id in closed_deliveries_id:
                delivery_board = DeliveryBoard.objects.filter(
                    id=delivery_id).exclude(
                        delivery_point__customer_responsible=None).order_by(
                            '?').first()
                if delivery_board is not None:
                    # Send a recap of the orders to the responsible
                    delivery_point = delivery_board.delivery_point
                    customer = delivery_point.customer_responsible
                    # Orders send to the preparation group
                    export_order_2_1_group(config, customer, delivery_point,
                                           [delivery_id], filename, permanence,
                                           sender_email, sender_function,
                                           signature)

        if not all_producers:
            abstract_ws = None
        else:
            # Orders send to the preparation team
            wb, abstract_ws = generate_customer_xlsx(
                permanence, deliveries_id=closed_deliveries_id)
            if wb is not None:
                # At least one order
                to_email_board = []
                for permanence_board in PermanenceBoard.objects.filter(
                        permanence_id=permanence.id).order_by('?'):
                    if permanence_board.customer:
                        to_email_board.append(
                            permanence_board.customer.user.email)

                order_staff_mail = config.safe_translation_getter(
                    'order_staff_mail',
                    any_language=True,
                    default=EMPTY_STRING)
                order_staff_mail_subject = "%s - %s" % (
                    REPANIER_SETTINGS_GROUP_NAME, permanence)

                board_composition, board_composition_and_description = get_board_composition(
                    permanence.id)

                template = Template(order_staff_mail)
                context = TemplateContext({
                    'permanence_link':
                    mark_safe('<a href="https://%s%s">%s</a>' %
                              (settings.ALLOWED_HOSTS[0],
                               reverse('order_view',
                                       args=(permanence.id, )), 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_SETTINGS_GROUP_NAME))
                })
                html_content = template.render(context)
                email = RepanierEmail(subject=order_staff_mail_subject,
                                      html_content=html_content,
                                      from_email=sender_email,
                                      to=to_email_board,
                                      cc=cc_email_staff)
                email.attach(
                    group_filename, save_virtual_workbook(wb),
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                )

                if not REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD:
                    email.to = cc_email_staff
                    email.cc = []
                    email.bcc = []
                email.send_email()

        # Orders send to our producers
        if REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER:
            producer_set = Producer.objects.filter(
                permanence=permanence,
                language=language_code,
            ).order_by('?')
            if producers_id:
                producer_set = producer_set.filter(id__in=producers_id)
            for producer in producer_set:
                long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name
                wb = generate_producer_xlsx(permanence=permanence,
                                            producer=producer,
                                            wb=None)

                order_producer_mail = config.safe_translation_getter(
                    'order_producer_mail',
                    any_language=True,
                    default=EMPTY_STRING)
                order_producer_mail_subject = "%s - %s" % (
                    REPANIER_SETTINGS_GROUP_NAME, permanence)

                template = Template(order_producer_mail)
                context = TemplateContext({
                    'name':
                    long_profile_name,
                    'long_profile_name':
                    long_profile_name,
                    'order_empty':
                    wb is None,
                    'duplicate':
                    not (wb is None or producer.manage_replenishment),
                    'permanence_link':
                    mark_safe('<a href="https://%s%s">%s</a>' %
                              (settings.ALLOWED_HOSTS[0],
                               reverse('order_view',
                                       args=(permanence.id, )), permanence)),
                    'signature':
                    mark_safe('%s<br/>%s<br/>%s' %
                              (signature, sender_function,
                               REPANIER_SETTINGS_GROUP_NAME))
                })
                html_content = template.render(context)

                producer_invoice = ProducerInvoice.objects.filter(
                    producer_id=producer.id, permanence_id=permanence.id).only(
                        "total_price_with_tax").order_by('?').first()
                if producer_invoice is not None \
                        and producer_invoice.total_price_with_tax < producer.minimum_order_value:
                    to_email_producer = cc_email_staff
                    html_content = \
                        order_producer_mail_subject + '<br/><br/>' + html_content
                    cc_email_staff = []
                    order_producer_mail_subject = _(
                        '/!\ Mail not send to our producer %s because the minimum order value has not been reached.'
                    ) % long_profile_name
                else:
                    to_email_producer = []
                    if producer.email:
                        to_email_producer.append(producer.email)
                    if producer.email2:
                        to_email_producer.append(producer.email2)
                    if producer.email3:
                        to_email_producer.append(producer.email3)
                email = RepanierEmail(subject=order_producer_mail_subject,
                                      html_content=html_content,
                                      from_email=sender_email,
                                      to=to_email_producer,
                                      cc=cc_email_staff)
                if REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER and wb is not None:
                    if REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_PRODUCER:
                        if abstract_ws is not None:
                            wb.add_sheet(abstract_ws, index=0)
                    email.attach(
                        filename, save_virtual_workbook(wb),
                        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                    )

                email.send_email()

        if all_producers:
            # Orders send to our customers only if they don't have already received it
            # ==> customerinvoice__is_order_confirm_send=False
            #     customerinvoice__permanence_id=permanence.id
            if REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_CUSTOMER:
                all_producers_closed = not (ProducerInvoice.objects.filter(
                    permanence_id=permanence_id,
                    status=PERMANENCE_OPENED).order_by('?').exists())
                if all_producers_closed and not REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
                    # REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS -> Do not send cancelled orders
                    customer_set = Customer.objects.filter(
                        represent_this_buyinggroup=False,
                        customerinvoice__is_order_confirm_send=False,
                        customerinvoice__permanence_id=permanence.id,
                        language=language_code).order_by('?')
                    if closed_deliveries_id is not None:
                        customer_set = customer_set.filter(
                            customerinvoice__delivery_id__in=
                            closed_deliveries_id, )
                    for customer in customer_set:
                        export_order_2_1_customer(customer, filename,
                                                  permanence, sender_email,
                                                  sender_function, signature,
                                                  abstract_ws)
                        # confirm_customer_invoice(permanence_id, customer.id)
                        customer_invoice = CustomerInvoice.objects.filter(
                            customer_id=customer.id,
                            permanence_id=permanence_id).order_by('?').first()
                        customer_invoice.confirm_order()
                        customer_invoice.save()
    translation.activate(cur_language)
예제 #11
0
 def export_customer_order(self, request, permanence, action):
     if not permanence.with_delivery_point:
         # Perform the action directly. Do not ask to select any delivery point.
         response = None
         wb = generate_customer_xlsx(permanence=permanence)[0]
         if wb is not None:
             response = HttpResponse(
                 content_type=
                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
             )
             response[
                 "Content-Disposition"] = "attachment; filename={0}-{1}.xlsx".format(
                     _("Customers"), permanence)
             wb.save(response)
         return response
     if "apply" in request.POST:
         if admin.ACTION_CHECKBOX_NAME in request.POST:
             deliveries_to_be_exported = request.POST.getlist(
                 "deliveries", [])
             if len(deliveries_to_be_exported) == 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 HttpResponseRedirect(
                     self.get_redirect_to_change_list_url())
                 # Also display order without delivery point -> The customer has not selected it yet
                 # deliveries_to_be_exported.append(None)
         else:
             deliveries_to_be_exported = ()
         response = HttpResponse(
             content_type=
             "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
         )
         response[
             "Content-Disposition"] = "attachment; filename={0}-{1}.xlsx".format(
                 _("Customers"), permanence)
         wb = generate_customer_xlsx(
             permanence=permanence,
             deliveries_id=deliveries_to_be_exported)[0]
         if wb is not None:
             wb.save(response)
         return response
     template_name = get_repanier_template_name(
         "admin/confirm_export_customer_order.html")
     return render(
         request,
         template_name,
         {
             **self.admin_site.each_context(request),
             "action_checkbox_name":
             admin.ACTION_CHECKBOX_NAME,
             "action":
             "export_customer_order",
             "permanence":
             permanence,
             "deliveries":
             DeliveryBoard.objects.filter(permanence_id=permanence.id),
         },
     )
예제 #12
0
 def export_xlsx_customer_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, PERMANENCE_SEND]:
         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 not permanence.with_delivery_point:
         # Perform the action directly. Do not ask to select any delivery point.
         response = None
         wb = generate_customer_xlsx(permanence=permanence)[0]
         if wb is not None:
             response = HttpResponse(
                 content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
             response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
                 _("Customers"),
                 permanence
             )
             wb.save(response)
         return response
     if 'apply' in request.POST:
         if admin.ACTION_CHECKBOX_NAME in request.POST:
             deliveries_to_be_exported = request.POST.getlist("deliveries", [])
             if len(deliveries_to_be_exported) == 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
                 # Also display order without delivery point -> The customer has not selected it yet
                 # deliveries_to_be_exported.append(None)
         else:
             deliveries_to_be_exported = ()
         response = None
         wb = generate_customer_xlsx(permanence=permanence, deliveries_id=deliveries_to_be_exported)[0]
         if wb is not None:
             response = HttpResponse(
                 content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
             response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
                 _("Customers"),
                 permanence
             )
             wb.save(response)
         return response
     template_name = get_repanier_template_name("confirm_admin_export_customer_order.html")
     return render(
         request,
         template_name, {
             'sub_title': _("Please, confirm the action : Export customer orders"),
             'action_checkbox_name': admin.ACTION_CHECKBOX_NAME,
             'action': 'export_xlsx_customer_order',
             'permanence': permanence,
             'deliveries': DeliveryBoard.objects.filter(
                 permanence_id=permanence.id
             ),
         })