views.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # coding=utf-8
  2. from utils.custom_modelviewset import CustomModelViewSet
  3. import datetime
  4. from django.utils import timezone
  5. from rest_framework.decorators import action
  6. from utils.permission import IsAdministratorUser
  7. from apps.log.models import BizLog
  8. from django.db import transaction
  9. from utils.exceptions import CustomError
  10. from utils import response_ok, response_error
  11. from apps.tenant.serializers import Invoice,InvoiceSerializer,Pay
  12. from apps.tenant.filters import InvoiceFilter
  13. class InvoiceViewSet(CustomModelViewSet):
  14. permission_classes = [IsAdministratorUser, ]
  15. queryset = Invoice.objects.filter()
  16. serializer_class = InvoiceSerializer
  17. def filter_queryset(self, queryset):
  18. queryset = queryset.filter()
  19. f = InvoiceFilter(self.request.GET, queryset=queryset)
  20. return f.qs
  21. @action(methods=['post'], detail=True)
  22. def check(self, request, pk):
  23. # 审核
  24. status = request.POST.get('status')
  25. reject_reason = request.POST.get('reason')
  26. try:
  27. with transaction.atomic():
  28. instance = Invoice.objects.filter(id=pk).first()
  29. if not instance:
  30. raise CustomError('当前发票信息有误!')
  31. # 审核
  32. if instance.status in [Invoice.NOT_MAIL, Invoice.MAILED,]:
  33. raise CustomError('当前发票已审核!')
  34. instance.status = status
  35. instance.reject_reason = reject_reason
  36. instance.save()
  37. start_date = (datetime.datetime.now() - datetime.timedelta(days=90)).strftime('%Y-%m-%d')
  38. Pay.objects.filter(tenant=instance.tenant,invoice__isnull=True, create_time__gte=start_date,).update(invoice=instance)
  39. BizLog.objects.addnew(None, request.user, BizLog.INSERT,
  40. u'审核企业发票[%s]状态为[%s],id=%d' % (instance.invoice_name, status, instance.id))
  41. except CustomError as e:
  42. return response_error(e.get_error_msg())
  43. except Exception as e:
  44. return response_error(str(e))
  45. return response_ok('审核完成!')
  46. @action(methods=['post'], detail=True)
  47. def check(self, request, pk):
  48. # 审核
  49. status = request.POST.get('status')
  50. reject_reason = request.POST.get('reason')
  51. try:
  52. with transaction.atomic():
  53. instance = Invoice.objects.filter(id=pk).first()
  54. if not instance:
  55. raise CustomError('当前发票信息有误!')
  56. # 审核
  57. if instance.status in [Invoice.NOT_MAIL, Invoice.MAILED,]:
  58. raise CustomError('当前发票已审核!')
  59. instance.status = status
  60. instance.reject_reason = reject_reason
  61. instance.use_time = timezone.now()
  62. instance.save()
  63. Pay.objects.filter(tenant=instance.tenant,invoice__isnull=True).update(invoice=instance)
  64. BizLog.objects.addnew(None, request.user, BizLog.INSERT,
  65. u'审核企业发票[%s]状态为[%s],id=%d' % (instance.invoice_name, status, instance.id))
  66. except CustomError as e:
  67. return response_error(e.get_error_msg())
  68. except Exception as e:
  69. return response_error(str(e))
  70. return response_ok('审核完成!')
  71. @action(methods=['post'], detail=True)
  72. def deliver(self, request, pk):
  73. express_company = request.POST.get('express_company')
  74. express_number = request.POST.get('express_number')
  75. try:
  76. with transaction.atomic():
  77. instance = Invoice.objects.filter(id=pk).first()
  78. if not instance:
  79. raise CustomError('当前发票信息有误!')
  80. # 发快递
  81. if instance.status != Invoice.NOT_MAIL:
  82. raise CustomError('当前发票状态不对!')
  83. instance.status = Invoice.MAILED
  84. instance.express_company = express_company
  85. instance.express_number = express_number
  86. instance.use_time = timezone.now()
  87. instance.save()
  88. BizLog.objects.addnew(None, request.user, BizLog.INSERT,
  89. u'企业发票[%s]发货,id=%d' % (instance.invoice_name, instance.id))
  90. except CustomError as e:
  91. return response_error(e.get_error_msg())
  92. except Exception as e:
  93. return response_error(str(e))
  94. return response_ok('发货完成!')