update excel warehouse
This commit is contained in:
@@ -34,22 +34,27 @@ class QuotaDistributionExcelViewSet(viewsets.ModelViewSet):
|
||||
product = self.queryset
|
||||
query = request.query_params
|
||||
organization = get_organization_by_user(request.user)
|
||||
description_name = ''
|
||||
|
||||
if query.get('param') == 'assigned':
|
||||
product = product.filter(
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
description_name='سهمیه'
|
||||
|
||||
elif query.get('param') == 'assigner':
|
||||
product = product.filter(
|
||||
Q(assigner_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
description_name = 'توزیع'
|
||||
|
||||
elif query.get('param') == 'all':
|
||||
product = product.filter(
|
||||
Q(assigner_organization=organization) |
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
description_name = ''
|
||||
|
||||
ser_data = distribution_serializers.QuotaDistributionSerializer(product, many=True).data
|
||||
|
||||
excel_options = [
|
||||
@@ -80,7 +85,7 @@ class QuotaDistributionExcelViewSet(viewsets.ModelViewSet):
|
||||
|
||||
create_header(worksheet, header_list, 5, 2, height=20, border_style='thin')
|
||||
|
||||
excel_description(worksheet, 'B1', f'سهمیه', row2='C3')
|
||||
excel_description(worksheet, 'B1', f'{description_name}', row2='C3')
|
||||
|
||||
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
||||
|
||||
@@ -152,7 +157,7 @@ class QuotaDistributionExcelViewSet(viewsets.ModelViewSet):
|
||||
response = HttpResponse(
|
||||
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
response[
|
||||
'Content-Disposition'] = f'attachment; filename="اختلاف کشتار.xlsx"'.encode(
|
||||
'Content-Disposition'] = f'attachment; filename="سهمیه.xlsx"'.encode(
|
||||
'utf-8')
|
||||
response.write(output.getvalue())
|
||||
return response
|
||||
|
||||
@@ -1,29 +1,11 @@
|
||||
from io import BytesIO
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from openpyxl import Workbook
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from common.helpers import get_organization_by_user
|
||||
from apps.product import models as product_models
|
||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
||||
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
|
||||
convert_str_to_date
|
||||
from io import BytesIO
|
||||
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from openpyxl import Workbook
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from openpyxl.styles import Font
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from apps.product import models as product_models
|
||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
||||
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
|
||||
@@ -31,236 +13,105 @@ from common.helper_excel import create_header, excel_description, create_header_
|
||||
from common.helpers import get_organization_by_user
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
@permission_classes([IsAuthenticated])
|
||||
@csrf_exempt
|
||||
def warehouse_excel(request):
|
||||
sheet_names = [
|
||||
'سهمیه',
|
||||
'ورودی به انبار',
|
||||
class InventoryEntryExcelViewSet(viewsets.ModelViewSet):
|
||||
queryset = warehouse_models.InventoryEntry.objects.all()
|
||||
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
||||
|
||||
]
|
||||
output = BytesIO()
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
workbook.remove(worksheet)
|
||||
for name in sheet_names:
|
||||
sheet_name = name
|
||||
worksheet = workbook.create_sheet(sheet_name)
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='warehouse_excel',
|
||||
url_name='warehouse_excel',
|
||||
name='warehouse_excel'
|
||||
)
|
||||
def warehouse_excel(self, request):
|
||||
output = BytesIO()
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
worksheet.sheet_view.rightToLeft = True
|
||||
worksheet.insert_rows(1)
|
||||
if sheet_name == 'سهمیه':
|
||||
product = product_models.QuotaDistribution.objects.all()
|
||||
query = request.query_params
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
if query.get('param') == 'assigned':
|
||||
product = product.filter(
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
entries = self.queryset.filter(organization=get_organization_by_user(request.user))
|
||||
ser_data = self.serializer_class(entries, many=True).data
|
||||
|
||||
elif query.get('param') == 'assigner':
|
||||
product = product.filter(
|
||||
Q(assigner_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
excel_options = [
|
||||
"ردیف",
|
||||
"تاریخ ورود به انبار",
|
||||
"شماره سهمیه",
|
||||
"وزن",
|
||||
"بارنامه",
|
||||
"محل دریافت",
|
||||
"سند",
|
||||
"توضیحات",
|
||||
]
|
||||
|
||||
elif query.get('param') == 'all':
|
||||
product = product.filter(
|
||||
Q(assigner_organization=organization) |
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
ser_data = distribution_serializers.QuotaDistributionSerializer(product, many=True).data
|
||||
header_list = [
|
||||
"وزن",
|
||||
|
||||
excel_options = [
|
||||
"ردیف",
|
||||
"شناسه توزیع",
|
||||
"شناسه سهمیه",
|
||||
"تاریخ ثبت",
|
||||
"توزیع کننده",
|
||||
"دریافت کننده",
|
||||
"وزن",
|
||||
"وزن توزیع شده",
|
||||
"وزن باقیمانده",
|
||||
"وزن فروش رفته",
|
||||
"مانده انبار",
|
||||
"ورودی به انبار",
|
||||
"توضیحات",
|
||||
]
|
||||
]
|
||||
|
||||
header_list = [
|
||||
"وزن",
|
||||
"وزن توزیع شده",
|
||||
"وزن باقیمانده",
|
||||
"وزن فروش رفته",
|
||||
"مانده انبار",
|
||||
"ورودی به انبار",
|
||||
create_header(worksheet, header_list, 5, 2, height=20, border_style='thin')
|
||||
excel_description(worksheet, 'B1', f'ورودی به انبار', row2='C3')
|
||||
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
||||
|
||||
]
|
||||
l = 6
|
||||
m = 1
|
||||
if ser_data:
|
||||
for data in ser_data:
|
||||
document = data.get('document')
|
||||
if document:
|
||||
if str(document).startswith(('http://', 'https://')):
|
||||
document_value = f'=HYPERLINK("{document}", "دانلود")'
|
||||
else:
|
||||
full_path = f"https://yourdomain.com/{document}"
|
||||
document_value = f'=HYPERLINK("{full_path}", "دانلود")'
|
||||
|
||||
create_header(worksheet, header_list, 5, 2, height=20, border_style='thin')
|
||||
else:
|
||||
document_value = 'ندارد'
|
||||
list1 = [
|
||||
m,
|
||||
str(shamsi_date(convert_str_to_date(data['create_date']), in_value=True)) if data.get(
|
||||
'create_date') else '',
|
||||
str(data[
|
||||
'distribution'].get('distribution')) or '-',
|
||||
data.get('weight') or 0,
|
||||
data.get('lading_number') or '-',
|
||||
data.get('delivery_address') or '-',
|
||||
document_value,
|
||||
data.get('notes') or '',
|
||||
]
|
||||
create_value(worksheet, list1, l + 1, 1, m=m)
|
||||
if document:
|
||||
worksheet.cell(row=l + 1, column=7).font = Font(color="0563C1", underline='single', bold=True)
|
||||
m += 1
|
||||
l += 1
|
||||
|
||||
excel_description(worksheet, 'B1', f'سهمیه', row2='C3')
|
||||
weight = sum((data['weight'] or 0) for data in ser_data)
|
||||
|
||||
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
||||
value_list = [
|
||||
weight
|
||||
]
|
||||
|
||||
l = 6
|
||||
m = 1
|
||||
if ser_data:
|
||||
for data in ser_data:
|
||||
list1 = [
|
||||
m,
|
||||
data['distribution_id'] or '',
|
||||
str(data['quota']['quota_id']) if data.get('quota') and data['quota'].get('quota_id') else '',
|
||||
str(shamsi_date(convert_str_to_date(data['create_date']), in_value=True)) if data.get(
|
||||
'create_date') else '',
|
||||
data[
|
||||
'assigner_organization'].get('organization') or '-',
|
||||
data[
|
||||
'assigned_organization'].get('organization') or '-',
|
||||
data.get('weight') or 0,
|
||||
data.get('distributed') or 0,
|
||||
data.get('remaining_weight') or 0,
|
||||
data.get('been_sold') or 0,
|
||||
data.get('warehouse_balance') or 0,
|
||||
data.get('warehouse_entry') or 0,
|
||||
data.get('description') or '-'
|
||||
]
|
||||
create_value(worksheet, list1, l + 1, 1, m=m)
|
||||
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||
|
||||
m += 1
|
||||
l += 1
|
||||
list2 = [
|
||||
'مجموع==>',
|
||||
'',
|
||||
'',
|
||||
weight,
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
''
|
||||
]
|
||||
create_value(worksheet, list2, l + 3, 1, color='gray')
|
||||
workbook.save(output)
|
||||
output.seek(0)
|
||||
|
||||
weight = sum((data['weight'] or 0) for data in ser_data)
|
||||
distributed = sum((data['distributed'] or 0) for data in ser_data)
|
||||
remaining_weight = sum((data['remaining_weight'] or 0) for data in ser_data)
|
||||
been_sold = sum((data['been_sold'] or 0) for data in ser_data)
|
||||
warehouse_balance = sum((data['warehouse_balance'] or 0) for data in ser_data)
|
||||
warehouse_entry = sum((data['warehouse_entry'] or 0) for data in ser_data)
|
||||
|
||||
value_list = [
|
||||
weight,
|
||||
distributed,
|
||||
remaining_weight,
|
||||
been_sold,
|
||||
warehouse_balance,
|
||||
warehouse_entry,
|
||||
]
|
||||
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||
|
||||
list2 = [
|
||||
'مجموع==>',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
weight,
|
||||
distributed,
|
||||
remaining_weight,
|
||||
been_sold,
|
||||
warehouse_balance,
|
||||
warehouse_entry,
|
||||
'',
|
||||
|
||||
]
|
||||
create_value(worksheet, list2, l + 3, 1, color='gray')
|
||||
else:
|
||||
entries = warehouse_models.InventoryEntry.objects.filter(
|
||||
organization=get_organization_by_user(request.user))
|
||||
ser_data = warehouse_serializers.InventoryEntrySerializer(entries, many=True).data
|
||||
|
||||
excel_options = [
|
||||
"ردیف",
|
||||
"تاریخ ورود به انبار",
|
||||
"شماره سهمیه",
|
||||
"وزن",
|
||||
"بارنامه",
|
||||
"محل دریافت",
|
||||
"سند",
|
||||
"سند",
|
||||
"توضیحات",
|
||||
]
|
||||
|
||||
header_list = [
|
||||
"وزن",
|
||||
|
||||
|
||||
]
|
||||
|
||||
create_header(worksheet, header_list, 5, 2, height=20, border_style='thin')
|
||||
|
||||
excel_description(worksheet, 'B1', f'ورودی به انبار', row2='C3')
|
||||
|
||||
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
||||
|
||||
l = 6
|
||||
m = 1
|
||||
if ser_data:
|
||||
for data in ser_data:
|
||||
list1 = [
|
||||
m,
|
||||
data['distribution_id'] or '',
|
||||
str(data['quota']['quota_id']) if data.get('quota') and data['quota'].get('quota_id') else '',
|
||||
str(shamsi_date(convert_str_to_date(data['create_date']), in_value=True)) if data.get(
|
||||
'create_date') else '',
|
||||
data[
|
||||
'assigner_organization'].get('organization') or '-',
|
||||
data[
|
||||
'assigned_organization'].get('organization') or '-',
|
||||
data.get('weight') or 0,
|
||||
data.get('distributed') or 0,
|
||||
data.get('remaining_weight') or 0,
|
||||
data.get('been_sold') or 0,
|
||||
data.get('warehouse_balance') or 0,
|
||||
data.get('warehouse_entry') or 0,
|
||||
data.get('description') or '-'
|
||||
]
|
||||
create_value(worksheet, list1, l + 1, 1, m=m)
|
||||
|
||||
m += 1
|
||||
l += 1
|
||||
|
||||
weight = sum((data['weight'] or 0) for data in ser_data)
|
||||
distributed = sum((data['distributed'] or 0) for data in ser_data)
|
||||
remaining_weight = sum((data['remaining_weight'] or 0) for data in ser_data)
|
||||
been_sold = sum((data['been_sold'] or 0) for data in ser_data)
|
||||
warehouse_balance = sum((data['warehouse_balance'] or 0) for data in ser_data)
|
||||
warehouse_entry = sum((data['warehouse_entry'] or 0) for data in ser_data)
|
||||
|
||||
value_list = [
|
||||
weight,
|
||||
distributed,
|
||||
remaining_weight,
|
||||
been_sold,
|
||||
warehouse_balance,
|
||||
warehouse_entry,
|
||||
]
|
||||
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||
|
||||
list2 = [
|
||||
'مجموع==>',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
weight,
|
||||
distributed,
|
||||
remaining_weight,
|
||||
been_sold,
|
||||
warehouse_balance,
|
||||
warehouse_entry,
|
||||
'',
|
||||
|
||||
]
|
||||
create_value(worksheet, list2, l + 3, 1, color='gray')
|
||||
workbook.save(output)
|
||||
output.seek(0)
|
||||
|
||||
response = HttpResponse(
|
||||
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
response[
|
||||
'Content-Disposition'] = f'attachment; filename="اختلاف کشتار.xlsx"'.encode(
|
||||
'utf-8')
|
||||
response.write(output.getvalue())
|
||||
return response
|
||||
response = HttpResponse(
|
||||
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
response[
|
||||
'Content-Disposition'] = f'attachment; filename="ورودی به انبار.xlsx"'.encode(
|
||||
'utf-8')
|
||||
response.write(output.getvalue())
|
||||
return response
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from apps.warehouse.services.excel import excel_processing as excel
|
||||
from apps.warehouse.services.excel.excel_processing import warehouse_excel
|
||||
from apps.warehouse.services.excel.excel_processing import InventoryEntryExcelViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'', InventoryEntryExcelViewSet, basename='inventory_entry_excel')
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
path('warehouse_excel/', warehouse_excel),
|
||||
# path('warehouse_excel/', warehouse_excel),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user