コード例 #1
0
 def export_xlsx_producer_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, PERMANENCE_SEND
     ]:
         user_message = _("Action canceled by the system.")
         user_message_level = messages.ERROR
         self.message_user(request, user_message, user_message_level)
         return
     # The export producer order use the offer item qty ordered
     # So that, this export is for all deliveries points
     # Perform the action directly. Do not ask to select any delivery point.
     wb = None
     producer_set = Producer.objects.filter(
         permanence=permanence).order_by("short_profile_name")
     for producer in producer_set:
         wb = generate_producer_xlsx(permanence, producer=producer, wb=wb)
     if wb is not None:
         response = HttpResponse(
             content_type=
             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
         )
         response[
             'Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
                 slugify(_("Producers")), slugify(permanence))
         wb.save(response)
         return response
     else:
         return
コード例 #2
0
 def export_xlsx_producer_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
     # The export producer order use the offer item qty ordered
     # So that, this export is for all deliveries points
     # Perform the action directly. Do not ask to select any delivery point.
     wb = None
     producer_set = Producer.objects.filter(permanence=permanence).order_by("short_profile_name")
     for producer in producer_set:
         wb = generate_producer_xlsx(permanence, producer=producer, wb=wb)
     if wb is not None:
         response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
         response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
             _("Producers"),
             permanence
         )
         wb.save(response)
         return response
     else:
         return
コード例 #3
0
 def export_producer_order(self, request, permanence):
     # The export producer order use the offer item qty ordered
     # So that, this export is for all deliveries points
     # Perform the action directly. Do not ask to select any delivery point.
     response = HttpResponse(
         content_type=
         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
     )
     response[
         "Content-Disposition"] = "attachment; filename={0}-{1}.xlsx".format(
             _("Producers"), permanence)
     wb = None
     producer_set = Producer.objects.filter(
         permanence=permanence).order_by("short_profile_name")
     for producer in producer_set:
         wb = generate_producer_xlsx(permanence, producer=producer, wb=wb)
     if wb is not None:
         wb.save(response)
     return response
コード例 #4
0
ファイル: email_order.py プロジェクト: pcolmant/repanier
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
ファイル: email_order.py プロジェクト: chiora93/repanier
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)
コード例 #6
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)