Add inventory_sale_transaction_excel to excel warehouse
This commit is contained in:
2
.idea/Rasaddam_Backend.iml
generated
2
.idea/Rasaddam_Backend.iml
generated
@@ -14,7 +14,7 @@
|
||||
</component>
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.10 (env)" jdkType="Python SDK" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.9 (dam_env)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PyDocumentationSettings">
|
||||
|
||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -3,5 +3,5 @@
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.10 (env)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (env)" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (dam_env)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
@@ -21,6 +21,6 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='deviceassignment',
|
||||
constraint=models.UniqueConstraint(fields=('client', 'device'), name='unique_assign_client_device', violation_error_code=403, violation_error_message='این کلاینت با همین دستگاه قبلا تخصیص داده شده است'),
|
||||
constraint=models.UniqueConstraint(fields=('client', 'device'), name='unique_assign_client_device', violation_error_message='این کلاینت با همین دستگاه قبلا تخصیص داده شده است'),
|
||||
),
|
||||
]
|
||||
|
||||
@@ -13,6 +13,68 @@ from common.helper_excel import create_header, excel_description, create_header_
|
||||
convert_str_to_date
|
||||
from common.helpers import get_organization_by_user
|
||||
|
||||
TRANSACTION_STATUS_MAP = {
|
||||
'success': 'موفق',
|
||||
'waiting': 'در انتظار',
|
||||
'failed': 'ناموفق',
|
||||
}
|
||||
|
||||
|
||||
def process_shares(shares):
|
||||
"""
|
||||
پردازش سهمها: سهم "حساب اصلی" رو با سهمهایی که shaba یکسان دارن ادغام میکنه
|
||||
"""
|
||||
if not shares or len(shares) == 0:
|
||||
return []
|
||||
|
||||
# کپی از سهمها
|
||||
processed_shares = [dict(share) for share in shares]
|
||||
|
||||
# پیدا کردن سهمهای حساب اصلی
|
||||
main_account_shares = [
|
||||
share for share in processed_shares
|
||||
if share.get('name') == 'حساب اصلی'
|
||||
]
|
||||
|
||||
for main_share in main_account_shares:
|
||||
# پیدا کردن سهم متناظر با همون shaba ولی اسم متفاوت
|
||||
matching_share = next(
|
||||
(share for share in processed_shares
|
||||
if share.get('name') != 'حساب اصلی'
|
||||
and share.get('shaba') == main_share.get('shaba')
|
||||
and share.get('shaba')),
|
||||
None
|
||||
)
|
||||
|
||||
if matching_share:
|
||||
matching_share['price'] = (matching_share.get('price') or 0) + (main_share.get('price') or 0)
|
||||
|
||||
# برگردوندن سهمها بدون حساب اصلی
|
||||
return [share for share in processed_shares if share.get('name') != 'حساب اصلی']
|
||||
|
||||
|
||||
def calculate_share_totals(items):
|
||||
"""
|
||||
محاسبه جمع کل سهمها از تمام آیتمها
|
||||
"""
|
||||
share_totals = {}
|
||||
|
||||
for item in items:
|
||||
item_share = item.get('item_share', [])
|
||||
if item_share and len(item_share) > 0:
|
||||
processed_shares = process_shares(item_share)
|
||||
for share in processed_shares:
|
||||
key = share.get('shaba') or share.get('name') or 'unknown'
|
||||
if key not in share_totals:
|
||||
share_totals[key] = {
|
||||
'name': share.get('name') or '-',
|
||||
'shaba': share.get('shaba') or '-',
|
||||
'total': 0,
|
||||
}
|
||||
share_totals[key]['total'] += share.get('price') or 0
|
||||
|
||||
return list(share_totals.values())
|
||||
|
||||
|
||||
class WareHouseExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
|
||||
queryset = warehouse_models.InventoryEntry.objects.all()
|
||||
@@ -127,3 +189,184 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
|
||||
'utf-8')
|
||||
response.write(output.getvalue())
|
||||
return response
|
||||
|
||||
# noqa # اکسل تراکنشها
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='inventory_sale_transaction_excel',
|
||||
url_name='inventory_sale_transaction_excel',
|
||||
name='inventory_sale_transaction_excel'
|
||||
)
|
||||
def inventory_sale_transaction_excel(self, request):
|
||||
output = BytesIO()
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
worksheet.sheet_view.rightToLeft = True
|
||||
worksheet.insert_rows(1)
|
||||
|
||||
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all()
|
||||
|
||||
if 'status' in request.GET.keys():
|
||||
status_param = self.request.query_params.get('status') # noqa
|
||||
|
||||
if status_param == 'waiting':
|
||||
queryset = queryset.filter(transaction_status='waiting').order_by('-create_date')
|
||||
elif status_param == 'success':
|
||||
queryset = queryset.filter(transaction_status='success').order_by('-create_date')
|
||||
elif status_param == 'failed':
|
||||
queryset = queryset.filter(transaction_status='failed').order_by('-create_date')
|
||||
else:
|
||||
queryset = queryset.order_by('-create_date')
|
||||
else:
|
||||
queryset = queryset.order_by('-create_date')
|
||||
|
||||
queryset = self.filter_query(queryset)
|
||||
|
||||
ser_data = warehouse_serializers.InventoryQuotaSaleTransactionSerializer(queryset, many=True).data
|
||||
|
||||
# جمعآوری تمام آیتمها برای پیدا کردن سهمهای یونیک
|
||||
all_items = []
|
||||
for data in ser_data:
|
||||
all_items.extend(data.get('items', []))
|
||||
|
||||
# محاسبه سهمهای یونیک (برای ستونهای داینامیک)
|
||||
all_share_totals = calculate_share_totals(all_items)
|
||||
share_names = [share['name'] for share in all_share_totals]
|
||||
|
||||
excel_options = [
|
||||
"ردیف",
|
||||
"تعاونی دامدار",
|
||||
"کد ملی دامدار",
|
||||
"تاریخ",
|
||||
"محصولات",
|
||||
"شناسه تراکنش",
|
||||
"شماره کارت",
|
||||
"مبلغ",
|
||||
"وضعیت",
|
||||
]
|
||||
# اضافه کردن ستونهای داینامیک سهمها
|
||||
excel_options.extend(share_names)
|
||||
|
||||
header_list = [
|
||||
"مبلغ کل",
|
||||
"تعداد تراکنشها",
|
||||
]
|
||||
# اضافه کردن سهمها به هدر
|
||||
header_list.extend(share_names)
|
||||
|
||||
# محاسبه height داینامیک بر اساس تعداد آیتمها
|
||||
header_height = max(25, 15 + len(header_list) * 3)
|
||||
options_height = max(25, 15 + len(excel_options) * 2)
|
||||
|
||||
create_header(worksheet, header_list, 5, 2, height=header_height, border_style='thin')
|
||||
|
||||
# ساخت عنوان با بازه تاریخ
|
||||
start_date = request.query_params.get('start')
|
||||
end_date = request.query_params.get('end')
|
||||
|
||||
title = 'تراکنشها'
|
||||
if start_date and end_date:
|
||||
start_shamsi = shamsi_date(convert_str_to_date(start_date))
|
||||
end_shamsi = shamsi_date(convert_str_to_date(end_date))
|
||||
title = f'تراکنشها از {start_shamsi} تا {end_shamsi}'
|
||||
elif start_date:
|
||||
start_shamsi = shamsi_date(convert_str_to_date(start_date))
|
||||
title = f'تراکنشها از {start_shamsi}'
|
||||
elif end_date:
|
||||
end_shamsi = shamsi_date(convert_str_to_date(end_date))
|
||||
title = f'تراکنشها تا {end_shamsi}'
|
||||
|
||||
excel_description(worksheet, 'B1', title, row2='C3')
|
||||
create_header_freez(worksheet, excel_options, 1, 6, 7, height=options_height, width=20)
|
||||
|
||||
l = 6
|
||||
m = 1
|
||||
# دیکشنری برای نگهداری جمع سهمها
|
||||
share_column_totals = {name: 0 for name in share_names}
|
||||
|
||||
if ser_data:
|
||||
for data in ser_data:
|
||||
items = data.get('items', [])
|
||||
products_list = []
|
||||
for item in items:
|
||||
product_name = item.get('name', '')
|
||||
if product_name:
|
||||
products_list.append(product_name)
|
||||
products_str = '، '.join(products_list) if products_list else '-'
|
||||
|
||||
rancher_data = data.get('rancher')
|
||||
national_code = rancher_data.get('national_code', '-') if rancher_data else '-'
|
||||
|
||||
seller_org = data.get('seller_organization')
|
||||
org_name = seller_org.get('name', '-') if seller_org else '-'
|
||||
|
||||
status = TRANSACTION_STATUS_MAP.get(data.get('transaction_status'), '-')
|
||||
|
||||
# محاسبه سهمهای این تراکنش
|
||||
transaction_shares = calculate_share_totals(items)
|
||||
share_values = []
|
||||
for share_name in share_names:
|
||||
share_value = next(
|
||||
(s['total'] for s in transaction_shares if s['name'] == share_name),
|
||||
0
|
||||
)
|
||||
share_values.append(share_value)
|
||||
share_column_totals[share_name] += share_value
|
||||
|
||||
list1 = [
|
||||
m,
|
||||
org_name,
|
||||
national_code,
|
||||
str(shamsi_date(convert_str_to_date(data['transaction_date']), in_value=True)) if data.get(
|
||||
'transaction_date') else '',
|
||||
products_str,
|
||||
data.get('transaction_id') or '-',
|
||||
data.get('payer_cart') or '-',
|
||||
data.get('price_paid') or 0,
|
||||
status,
|
||||
]
|
||||
# اضافه کردن مقادیر سهمها
|
||||
list1.extend(share_values)
|
||||
|
||||
create_value(worksheet, list1, l + 1, 1, height=23, m=m)
|
||||
m += 1
|
||||
l += 1
|
||||
|
||||
total_price = sum((data['price_paid'] or 0) for data in ser_data)
|
||||
transaction_count = len(ser_data)
|
||||
|
||||
value_list = [
|
||||
total_price,
|
||||
transaction_count,
|
||||
]
|
||||
# اضافه کردن جمع سهمها به مقادیر هدر
|
||||
value_list.extend([share_column_totals[name] for name in share_names])
|
||||
|
||||
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||
|
||||
list2 = [
|
||||
'مجموع==>',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
total_price,
|
||||
'',
|
||||
]
|
||||
# اضافه کردن جمع سهمها به ردیف مجموع
|
||||
list2.extend([share_column_totals[name] for name in share_names])
|
||||
|
||||
create_value(worksheet, list2, l + 3, 1, color='gray', height=23)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user