Sample views

mail@pastecode.io avatar
unknown
python
2 years ago
1.7 kB
14
Indexable
Never
def get_expense_new(request, year, *args, **kwargs):

    expense_breakdown = {}

    for category in ExpenseCategory.objects.all():
        obj_data = ExpenseVoucherLineItem.objects.filter(voucher__transaction_date__year=year).filter(expense__category=category).annotate(date=TruncMonth('voucher__transaction_date')).values('date').annotate(amt=Sum('amount'))

        label = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
        figures = [0 for x in range(0, len(label))] # Prepopulate Figures


        for data in obj_data:
            idx = (data['date'].month-1)
            figures[idx] = float(data['amt'])

        data = {
            'label': label, 
            'figures': figures,
  
        }

        expense_breakdown.update(
            {category.name.replace(" ",""): data }
        )

    # MERGE SOME DATA

    merge = ['food', 'salaryregular', 'salaryadvance', 'salaryloan', 'contributionPHICphilhealth', 'contributionPAGIBIG', 'contributionSSS', 'allowance']
    stock_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    label = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']

    new_dict = {}

    for mat_name, mat_values in expense_breakdown.items():

        if mat_name in merge:
            for idx, value in enumerate(mat_values['figures']):
                stock_list[idx] = stock_list[idx] + value


        new_dict.update(
            {mat_name: mat_values
            }
        ) 

    new_dict.update(
        {'personnel': {'label': label, 'figures': stock_list}
        }
    )  

    print ('get_expense_new', (type(new_dict)))

    return JsonResponse(new_dict)