Commit 3a123e59 authored by Patrick's avatar Patrick

Refactor export history of purchases, per permanence, per producer, per customer

parent 713d0fdf
......@@ -24,7 +24,7 @@ from import_export.widgets import CharWidget
from repanier.const import EMPTY_STRING, ORDER_GROUP, INVOICE_GROUP, \
COORDINATION_GROUP, DECIMAL_ONE, TWO_DECIMALS
from repanier.models import Customer, LUT_DeliveryPoint
from repanier.xlsx import xlsx_purchase
from repanier.xlsx import xlsx_invoice
from repanier.xlsx.widget import IdWidget, OneToOneWidget, \
DecimalBooleanWidget, ZeroDecimalsWidget, TwoMoneysWidget, TranslatedForeignKeyWidget, DateWidgetExcel
from repanier.xlsx.extended_formats import XLSX_OPENPYXL_1_8_6
......@@ -214,9 +214,9 @@ class CustomerResource(resources.ModelResource):
def create__customer_action(year):
def action(modeladmin, request, queryset):
queryset = queryset.order_by('?')
return xlsx_purchase.admin_export_year_by_customer(year, queryset)
def action(modeladmin, request, customer_qs):
# return xlsx_purchase.admin_export_year_by_customer(year, customer_qs)
return xlsx_invoice.admin_export_customer_invoices_report(request, customer_qs, year)
name = "export_producer_%d" % (year,)
return (name, (action, name, _("Export purchases of %s") % (year,)))
......
# -*- coding: utf-8
from __future__ import unicode_literals
import datetime
from django.conf import settings
from django.contrib import admin
from django.core.checks import messages
from django.db.models import Q, F, Sum
from django.db.models import Q, F
from django.shortcuts import render
from django.utils import timezone
from django.http import HttpResponseRedirect
......@@ -101,14 +99,18 @@ class PermanenceDoneAdmin(TranslatableAdmin):
import_xlsx.short_description = _("Import orders prepared from a XLSX file")
def preview_invoices(self, request, queryset):
permanence = queryset.order_by('?').first()
if permanence is None or permanence.status not in [PERMANENCE_DONE, PERMANENCE_ARCHIVED]:
user_message = _("Action canceled by the system.")
user_message_level = messages.ERROR
self.message_user(request, user_message, user_message_level)
return None
return xlsx_invoice.admin_export(request, queryset)
def preview_invoices(self, request, permanence_qs):
valid_permanence_qs = permanence_qs.filter(
status__in=[PERMANENCE_DONE, PERMANENCE_ARCHIVED]
)
if valid_permanence_qs.exists():
xlsx = xlsx_invoice.admin_export_permanences_invoices_report(request, valid_permanence_qs)
if xlsx is not None:
return xlsx
user_message = _("No invoice available for %(permanence)s.") % {'permanence': ', '.join("%s" % p for p in permanence_qs.all())}
user_message_level = messages.WARNING
self.message_user(request, user_message, user_message_level)
return None
preview_invoices.short_description = _("Preview invoices before sending them by email")
......
......@@ -21,7 +21,7 @@ from repanier.models import Permanence, Product, \
Producer
from repanier.tools import producer_web_services_activated, \
update_offer_item
from repanier.xlsx import xlsx_stock, xlsx_purchase, xlsx_product
from repanier.xlsx import xlsx_stock, xlsx_invoice, xlsx_product
from repanier.xlsx.widget import IdWidget, TwoDecimalsWidget, \
DecimalBooleanWidget, TwoMoneysWidget, DateWidgetExcel
from repanier.xlsx.extended_formats import XLSX_OPENPYXL_1_8_6
......@@ -88,9 +88,9 @@ class ProducerResource(resources.ModelResource):
def create__producer_action(year):
def action(modeladmin, request, queryset):
queryset = queryset.order_by('?')
return xlsx_purchase.admin_export_year_by_producer(year, queryset)
def action(modeladmin, request, producer_qs):
# return xlsx_purchase.admin_export_year_by_producer(year, queryset)
return xlsx_invoice.admin_export_producer_invoices_report(request, producer_qs, year)
name = "export_producer_%d" % (year,)
return (name, (action, name, _("Export purchases of %s") % (year,)))
......
This diff is collapsed.
This diff is collapsed.
......@@ -520,34 +520,6 @@ def admin_export_permanence_by_producer(request, permanence):
return None
def admin_export_year_by_producer(year, queryset):
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
slugify(_("Invoices")),
str(year)
)
wb = None
for producer in queryset.order_by('short_profile_name'):
wb = export_purchase(producer=producer, year=year, wb=wb)
if wb is not None:
wb.save(response)
return response
def admin_export_year_by_customer(year, queryset):
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = "attachment; filename={0}-{1}.xlsx".format(
slugify(_("Invoices")),
str(year)
)
wb = None
for customer in queryset:
wb = export_purchase(customer=customer, year=year, wb=wb)
if wb is not None:
wb.save(response)
return response
@transaction.atomic
def import_purchase_sheet(worksheet, permanence=None,
customer_2_id_dict=None,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment