views.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # coding=utf-8
  2. import traceback
  3. import json
  4. import tablib
  5. from _mysql_exceptions import IntegrityError
  6. from django.db.models import ProtectedError, Count, Sum, Q
  7. from django.utils import timezone
  8. from django.views.decorators.csrf import csrf_exempt
  9. from django.db import transaction, IntegrityError
  10. from apps.account.decorators import token_required, permission_required
  11. from apps.account.models import Department, User
  12. from apps.base import Formater
  13. from apps.foundation.models import BizLog, Option
  14. from libs.http import JSONResponse, JSONError, DataGridJSONResponse
  15. from libs import utils
  16. from apps.exceptions import CustomError, ExportChange
  17. from django.conf import settings
  18. from apps.goods.models import GoodsGodownEntry, GoodsGodownEntryDetail
  19. from apps.material.models import DeliverDetail, Deliver
  20. from apps.product.models import ProductBase
  21. @csrf_exempt
  22. @permission_required('product.view_department_ledger')
  23. def department_ledger_list(request):
  24. product_name = request.GET.get('product_name')
  25. product_model = request.GET.get('product_model')
  26. product_type = request.GET.get('product_type')
  27. date = request.GET.get('date')
  28. exclude_zero = request.GET.get('exclude_zero')
  29. rows = ProductBase.objects.filter()
  30. dep_rows = Department.objects.filter()
  31. if product_name:
  32. rows = rows.filter(name__icontains=product_name)
  33. if product_model:
  34. rows = rows.filter(model__icontains=product_model)
  35. if product_type:
  36. rows = rows.filter(type=int(product_type))
  37. date_begin = ''
  38. date_end = ''
  39. if date:
  40. date = date.split(' - ')
  41. date_begin = date[0]
  42. date_end = date[1]
  43. deliver_rows = DeliverDetail.objects.filter(main__status=settings.PASS)
  44. entry_rows = GoodsGodownEntryDetail.objects.filter(main__status=settings.PASS)
  45. if date_begin:
  46. deliver_rows = deliver_rows.filter(main__create_time__gte=date_begin)
  47. entry_rows = entry_rows.filter(main__create_time__gte=date_begin)
  48. if date_end:
  49. deliver_rows = deliver_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  50. entry_rows = entry_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  51. if exclude_zero:
  52. d_ids = deliver_rows.values_list('product_base_id')
  53. e_ids = entry_rows.values_list('goods__product_base_id')
  54. rows = rows.filter(Q(id__in=d_ids) | Q(id__in=e_ids))
  55. #计算合计
  56. more = {}
  57. for dep_row in dep_rows:
  58. d_sum = deliver_rows.filter(main__receiver_department_id=dep_row.id).aggregate(sum_count=Sum('count'), sum_amount=Sum('total_cost'))
  59. e_sum = entry_rows.filter(main__department_id=dep_row.id).aggregate(sum_count=Sum('count'), sum_amount=Sum('amount'))
  60. sum_count = (d_sum['sum_count'] or 0) + (e_sum['sum_count'] or 0)
  61. sum_amount = (d_sum['sum_amount'] or 0) + (e_sum['sum_amount'] or 0)
  62. more[str(dep_row.id) + '_count'] = Formater.formatCountShow(sum_count)
  63. more[str(dep_row.id) + '_amount'] = Formater.formatAmountShow(sum_amount)
  64. rows, total = utils.get_page_data(request, rows)
  65. data = []
  66. for row in rows:
  67. item = {
  68. 'product_name': row.name,
  69. 'product_model': row.model,
  70. 'type_text': row.get_type_display()
  71. }
  72. for dep_row in dep_rows:
  73. if row.type == ProductBase.MATERIAL or row.type == ProductBase.CONSUMABLE:
  74. d_rows = DeliverDetail.objects.filter(main__status=settings.PASS, product_base_id=row.id,
  75. main__receiver_department_id=dep_row.id)
  76. if date_begin:
  77. d_rows = d_rows.filter(main__create_time__gte=date_begin)
  78. if date_end:
  79. d_rows = d_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  80. sum_rows = d_rows.aggregate(sum_count=Sum('count'), sum_amount=Sum('total_cost'))
  81. else:
  82. g_rows = GoodsGodownEntryDetail.objects.filter(main__status=settings.PASS,main__department_id=dep_row.id,
  83. goods__product_base_id=row.id)
  84. if date_begin:
  85. g_rows = g_rows.filter(main__create_time__gte=date_begin)
  86. if date_end:
  87. g_rows = g_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  88. sum_rows = g_rows.aggregate(sum_count=Sum('count'), sum_amount=Sum('amount'))
  89. item[str(dep_row.id)+ '_count'] = Formater.formatCountShow(sum_rows['sum_count'] or 0)
  90. item[str(dep_row.id)+ '_amount'] = Formater.formatAmountShow(sum_rows['sum_amount'] or 0)
  91. data.append(item)
  92. return DataGridJSONResponse(data, total, more)
  93. @csrf_exempt
  94. @permission_required('product.export_department_ledger')
  95. def department_ledger_export(request):
  96. product_name = request.GET.get('product_name')
  97. product_model = request.GET.get('product_model')
  98. product_type = request.GET.get('product_type')
  99. date = request.GET.get('date')
  100. exclude_zero = request.GET.get('exclude_zero')
  101. rows = ProductBase.objects.filter()
  102. dep_rows = Department.objects.filter()
  103. if product_name:
  104. rows = rows.filter(name__icontains=product_name)
  105. if product_model:
  106. rows = rows.filter(model__icontains=product_model)
  107. if product_type:
  108. rows = rows.filter(type=int(product_type))
  109. date_begin = ''
  110. date_end = ''
  111. if date:
  112. date = date.split(' - ')
  113. date_begin = date[0]
  114. date_end = date[1]
  115. if exclude_zero:
  116. deliver_rows = DeliverDetail.objects.filter(main__status=settings.PASS)
  117. entry_rows = GoodsGodownEntryDetail.objects.filter(main__status=settings.PASS)
  118. if date_begin:
  119. deliver_rows = deliver_rows.filter(main__create_time__gte=date_begin)
  120. entry_rows = entry_rows.filter(main__create_time__gte=date_begin)
  121. if date_end:
  122. deliver_rows = deliver_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  123. entry_rows = entry_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  124. d_ids = deliver_rows.values_list('product_base_id')
  125. e_ids = entry_rows.values_list('goods__product_base_id')
  126. rows = rows.filter(Q(id__in=d_ids) | Q(id__in=e_ids))
  127. headers = [u'产品名称',u'产品代码',u'类别']
  128. for dep_row in dep_rows:
  129. headers.append(dep_row.name+u'数量')
  130. headers.append(dep_row.name+u'金额')
  131. data = []
  132. for row in rows:
  133. item = [row.name, row.model, row.get_type_display()]
  134. for dep_row in dep_rows:
  135. if row.type == ProductBase.MATERIAL or row.type == ProductBase.CONSUMABLE:
  136. d_rows = DeliverDetail.objects.filter(main__status=settings.PASS, product_base_id=row.id,
  137. main__receiver_department_id=dep_row.id)
  138. if date_begin:
  139. d_rows = d_rows.filter(main__create_time__gte=date_begin)
  140. if date_end:
  141. d_rows = d_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  142. sum_rows = d_rows.aggregate(sum_count=Sum('count'), sum_amount=Sum('total_cost'))
  143. else:
  144. g_rows = GoodsGodownEntryDetail.objects.filter(main__status=settings.PASS,main__department_id=dep_row.id,
  145. goods__product_base_id=row.id)
  146. if date_begin:
  147. g_rows = g_rows.filter(main__create_time__gte=date_begin)
  148. if date_end:
  149. g_rows = g_rows.filter(main__create_time__lte=date_end + ' 23:59:59')
  150. sum_rows = g_rows.aggregate(sum_count=Sum('count'), sum_amount=Sum('amount'))
  151. item.append(Formater.formatCountShow(sum_rows['sum_count'] or 0))
  152. item.append(Formater.formatAmountShow(sum_rows['sum_amount'] or 0))
  153. data.append(item)
  154. data = tablib.Dataset(*data, headers=headers, title=u"数据")
  155. filename = utils.attachment_save(data)
  156. return JSONResponse({'filename': filename})