def percentSummary(start_date, end_date): total_EggAmount = Egg.getAmount(start_date, end_date) # 중량 processProduct_amount = ProductEgg.objects.values('type').filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .filter(type='할란').annotate(tankAmount=Sum('rawTank_amount') + Sum('pastTank_amount')) openEggUse_amount = ProductEgg.objects.values('type').filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .filter(type='할란사용').annotate(tankAmount=Sum('rawTank_amount') + Sum('pastTank_amount')) product_amount = Product.objects.values('type').filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .filter(type='제품생산').filter(purchaseYmd=None).annotate(tankAmount=Sum('amount')) processProductCreate_amount = ProductEgg.objects.values('type').filter(ymd__gte=start_date).filter( ymd__lte=end_date) \ .filter(type='공정품발생').annotate(tankAmount=Sum('rawTank_amount') + Sum('pastTank_amount')) processProductInsert_amount = ProductEgg.objects.values('type').filter(ymd__gte=start_date).filter( ymd__lte=end_date) \ .filter(type='공정품투입').annotate(tankAmount=Sum('rawTank_amount') + Sum('pastTank_amount')) recallProductInsert_amount = ProductEgg.objects.values('type').filter(ymd__gte=start_date).filter( ymd__lte=end_date) \ .filter(type='미출고품투입').annotate(tankAmount=Sum('rawTank_amount') + Sum('pastTank_amount')) if not total_EggAmount: total_EggAmount = 0 processProduct_amount = processProduct_amount[0]['tankAmount'] if processProduct_amount else 0 openEggUse_amount = openEggUse_amount[0]['tankAmount'] if openEggUse_amount else 0 product_amount = product_amount[0]['tankAmount'] if product_amount else 0 processProductCreate_amount = processProductCreate_amount[0]['tankAmount'] if processProductCreate_amount else 0 processProductInsert_amount = processProductInsert_amount[0]['tankAmount'] if processProductInsert_amount else 0 recallProductInsert_amount = recallProductInsert_amount[0]['tankAmount'] if recallProductInsert_amount else 0 loss_clean_amount = Product.objects.filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .filter(purchaseYmd=None).aggregate(loss_clean=Sum('loss_clean')) loss_fill_amount = Product.objects.filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .filter(purchaseYmd=None).aggregate(loss_fill=Sum('loss_fill')) loss_insert_amount = ProductEgg.objects.filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .aggregate(loss_insert=Sum('loss_insert')) loss_openEgg_amount = ProductEgg.objects.filter(ymd__gte=start_date).filter(ymd__lte=end_date) \ .aggregate(loss_openEgg=Sum('loss_openEgg')) loss_clean_amount = loss_clean_amount['loss_clean'] if loss_clean_amount['loss_clean'] else 0 loss_fill_amount = loss_fill_amount['loss_fill'] if loss_fill_amount['loss_fill'] else 0 loss_insert_amount = loss_insert_amount['loss_insert'] if loss_insert_amount['loss_insert'] else 0 loss_openEgg_amount = loss_openEgg_amount['loss_openEgg'] if loss_openEgg_amount['loss_openEgg'] else 0 openEggPercent = round((processProduct_amount / total_EggAmount * 100), 2) if total_EggAmount > 0 else 0 productPercent = round( ((product_amount + processProduct_amount + openEggUse_amount + processProductInsert_amount + processProductCreate_amount + recallProductInsert_amount) / total_EggAmount * 100), 2) \ if total_EggAmount > 0 else 0 lossTotal = loss_clean_amount + loss_fill_amount + loss_insert_amount + loss_openEgg_amount insertLoss = round((loss_insert_amount / total_EggAmount * 100), 2) if total_EggAmount > 0 else 0 openEggLoss = round((loss_openEgg_amount / processProduct_amount * 100), 2) if processProduct_amount > 0 else 0 result = {'openEggPercent': openEggPercent, 'productPercent': productPercent, 'lossTotal': lossTotal, 'insertLoss': insertLoss, 'openEggLoss': openEggLoss} return result
def get(self, request, format=None): try: result = dict() Eggs = Egg.eggReportQuery(**request.query_params) result['data'] = Eggs['items'] result['draw'] = Eggs['draw'] result['recordsTotal'] = Eggs['total'] result['recordsFiltered'] = Eggs['count'] return Response(result, status=status.HTTP_200_OK, template_name=None, content_type=None) except Exception as e: return Response(e, status=status.HTTP_404_NOT_FOUND, template_name=None, content_type=None)