Esempio n. 1
0
def create_stats(fromDate, toDate):
    logger.debug("Process %s - %s" % (fromDate, toDate))
    month = fromDate.strftime('%Y%m')
    
    incomeSum = 0
    for income in Income.objects.filter(period__gte=fromDate, period__lte=toDate):
        incomeSum += income.amount
    
    try:
        data = StatsData.objects.get(pk=month)
    except StatsData.DoesNotExist:
        data = StatsData(month=month)
        
    data.incomes = incomeSum
    
    expenseSum = 0
    
    q = Expense.objects.filter(date__gte=fromDate, date__lte=toDate)

    defaultpt = getattr(settings,'STATS_PAYMENT_TYPE', None)
    if defaultpt:
        q = q.filter(paymentType__id=defaultpt)

    categoryExclude = getattr(settings, 'MONTH_STATS_CATEGORY_EXCLUDE', [])
    subCategoryExclude = getattr(settings, 'MONTH_STATS_SUBCATEGORY_EXCLUDE', [])
                    
    for expense in q:
        if not (expense.subCategory.pk in subCategoryExclude or expense.subCategory.category.pk in categoryExclude):
            expenseSum += expense.amount
    
    data.expenses = expenseSum
    
    data.save()
    
    create_category_stats(fromDate, toDate)
Esempio n. 2
0
def create_chart():
    import pygal
    #chart = pygal.Line()
    chart = pygal.Bar(disable_xml_declaration=True)
    chart.title = 'Gastos'
    
    q = StatsData.objects.all().order_by('-month')[:6]
    data = sorted(q, key=lambda d: d.month)
    
    labels = []
    expenses = []
    incomes = []
    for d in data:
        labels.append(d.display_month)
        expenses.append(float(d.expenses))
        incomes.append(float(d.incomes))
    
    chart.x_labels = labels   
    logger.debug(labels) 
    chart.add('Gastos', expenses)
    chart.add('Ingresos', incomes)
    
    return chart.render()
Esempio n. 3
0
def create_category_stats(fromDate, toDate):
    logger.debug("Process Categories %s - %s" % (fromDate, toDate))
    month = fromDate.strftime('%Y%m')
    
    categoryCache = {}
    
    categoryExclude = getattr(settings, 'STATS_CATEGORY_EXCLUDE', [])
    subCategoryExclude = getattr(settings, 'STATS_SUBCATEGORY_EXCLUDE', [])
    
    q = Expense.objects.filter(date__gte=fromDate, date__lte=toDate)
    for expense in q:
        if not (expense.subCategory.pk in subCategoryExclude or expense.subCategory.category.pk in categoryExclude):
            category = categoryCache.get(expense.subCategory.category.pk)
            if not category:
                category = {'category': expense.subCategory.category, 'amount': 0}
            category['amount'] += expense.amount
            categoryCache[expense.subCategory.category.pk] = category

    CategoryStatsData.objects.filter(month=month).delete()
    
    for c in categoryCache.values():
        if c['amount'] > 0:
            CategoryStatsData.objects.create(month=month, category=c['category'], amount=c['amount'])