143 lines
5.3 KiB
Python
143 lines
5.3 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
from __future__ import unicode_literals
|
||
|
from django.db import models, connection
|
||
|
from django.utils import timezone
|
||
|
from datetime import datetime, date, timedelta
|
||
|
from django.core.exceptions import ValidationError
|
||
|
from decimal import Decimal
|
||
|
|
||
|
class Category(models.Model):
|
||
|
cat_name = models.CharField("Category", max_length=50)
|
||
|
cat_type = models.CharField("Type", max_length=1, choices=(('I', 'Income'), ('E', 'Expence')))
|
||
|
user = models.ForeignKey('auth.user', null=True, blank=True)
|
||
|
budget_type = models.CharField("Type", max_length=1, choices = (('W', 'Weekly'), ('M', 'Monthly'), ('Y', 'Yearly')), default='M')
|
||
|
budget_amount = models.DecimalField("Amount", max_digits=10, decimal_places=2, default=0)
|
||
|
|
||
|
def natural_key(self):
|
||
|
return self.cat_name
|
||
|
|
||
|
def __unicode__(self):
|
||
|
return self.cat_name
|
||
|
|
||
|
def cat_sum(self):
|
||
|
cur = connection.cursor()
|
||
|
month = date.today().month
|
||
|
year = date.today().year
|
||
|
cur.execute("""SELECT SUM(tr_amount) from pfm_transaction as t JOIN pfm_category as c
|
||
|
ON t.tr_cat_id = c.id WHERE c.id=%s AND tr_type='E' AND date_part('month', t.tr_date)=%s AND date_part('year', t.tr_date)=%s""", (self.id, month, year ))
|
||
|
try:
|
||
|
return float(cur.fetchone()[0])
|
||
|
except:
|
||
|
return 0
|
||
|
cur.close()
|
||
|
|
||
|
def minus_sum(self):
|
||
|
return (self.cat_sum()*(-1))
|
||
|
|
||
|
'''def budget(self):
|
||
|
cur = connection.cursor()
|
||
|
cur.execute("""SELECT amount FROM pfm_budget WHERE cat_id=:ID """, {"ID": self.id})
|
||
|
try: return float(cur.fetchone()[0])
|
||
|
except: return 0
|
||
|
cur.close()'''
|
||
|
|
||
|
def balance(self):
|
||
|
return -self.budget()
|
||
|
|
||
|
def ratio(self):
|
||
|
try: return int(Decimal(self.cat_sum())/self.budget_amount*100)
|
||
|
except: return 100
|
||
|
if ratio(self)>100:
|
||
|
return 100
|
||
|
|
||
|
class Meta:
|
||
|
verbose_name_plural = "Categories"
|
||
|
|
||
|
class Account(models.Model):
|
||
|
acc_name = models.CharField("Account", max_length=50)
|
||
|
acc_balance = models.DecimalField("Balance", max_digits=10, decimal_places=2)
|
||
|
acc_currency = models.CharField("Currency", max_length=3)
|
||
|
user = models.ForeignKey('auth.user', null=True, blank=True)
|
||
|
|
||
|
def natural_key(self):
|
||
|
return self.acc_name
|
||
|
|
||
|
def __unicode__(self):
|
||
|
return self.acc_name
|
||
|
|
||
|
SQL_exp = """SELECT SUM(tr_amount) from pfm_transaction as t JOIN pfm_account as a
|
||
|
ON t.tr_acc_id = a.id WHERE tr_type='E' AND tr_acc_id=%s"""
|
||
|
|
||
|
SQL_inc = """SELECT SUM(tr_amount) from pfm_transaction as t JOIN pfm_account as a
|
||
|
ON t.tr_acc_id = a.id WHERE tr_type='I' AND tr_acc_id=%s"""
|
||
|
|
||
|
def count(self, SQL):
|
||
|
cur = connection.cursor()
|
||
|
cur.execute(SQL, (self.id,))
|
||
|
try: return float(cur.fetchone()[0])
|
||
|
except: return 0
|
||
|
cur.close()
|
||
|
|
||
|
def exp(self): return self.count(Account.SQL_exp)
|
||
|
|
||
|
def inc(self): return self.count(Account.SQL_inc)
|
||
|
|
||
|
def tr_in(self):
|
||
|
cur = connection.cursor()
|
||
|
cur.execute("""SELECT SUM(tr_amount) from pfm_transfer as t JOIN pfm_account as a
|
||
|
ON t.to_acc_id = a.id WHERE to_acc_id=%s""", (self.id,))
|
||
|
try:
|
||
|
return float(cur.fetchone()[0])
|
||
|
except:
|
||
|
return 0
|
||
|
cur.close()
|
||
|
|
||
|
def tr_out(self):
|
||
|
cur = connection.cursor()
|
||
|
cur.execute("""SELECT SUM(tr_amount) from pfm_transfer as t JOIN pfm_account as a
|
||
|
ON t.from_acc_id = a.id WHERE from_acc_id=%s""", (self.id,))
|
||
|
try:
|
||
|
return float(cur.fetchone()[0])
|
||
|
except:
|
||
|
return 0
|
||
|
cur.close()
|
||
|
|
||
|
def rest(self):
|
||
|
return (float(self.acc_balance) - self.count(Account.SQL_exp) -
|
||
|
self.tr_out() + self.count(Account.SQL_inc) + self.tr_in())
|
||
|
|
||
|
|
||
|
class Transaction(models.Model):
|
||
|
tr_acc = models.ForeignKey(Account)
|
||
|
tr_cat = models.ForeignKey(Category)
|
||
|
tr_type = models.CharField("Type", max_length=1, choices=(('I', 'Income'), ('E', 'Expence')), default='E')
|
||
|
tr_amount = models.DecimalField("Amount", max_digits=10, decimal_places=2, default='0.00')
|
||
|
tr_note = models.CharField(max_length=100, null=True, blank=True)
|
||
|
tr_date = models.DateField(default=timezone.now)
|
||
|
pub_date = models.DateTimeField(blank=True, null=True)
|
||
|
user = models.ForeignKey('auth.user', blank=True, null=True)
|
||
|
|
||
|
def publish(self):
|
||
|
self.pub_date = timezone.now()
|
||
|
self.save()
|
||
|
|
||
|
def __unicode__(self):
|
||
|
return '%s,%s,%s,%s' % (self.tr_cat, self.tr_acc, self.tr_amount, self.tr_date.strftime("%d.%m.%Y"))
|
||
|
|
||
|
def sum_tr():
|
||
|
cur = connection.cursor()
|
||
|
cur.execute("SELECT SUM(tr_amount) from pfm_transaction WHERE tr_date > :D", {"D": date.today()-timedelta(days=30)})
|
||
|
return cur.fetchone()
|
||
|
cur.close()
|
||
|
|
||
|
class Transfer(models.Model):
|
||
|
from_acc = models.ForeignKey(Account, related_name="to_acc")
|
||
|
to_acc = models.ForeignKey(Account, related_name="from_acc")
|
||
|
tr_amount = models.DecimalField("Amount", max_digits=10, decimal_places=2)
|
||
|
tr_date = models.DateTimeField(default=timezone.now)
|
||
|
user = models.ForeignKey('auth.user', blank=True, null=True)
|
||
|
|
||
|
def publish(self): self.save()
|
||
|
def __unicode__(self):
|
||
|
return '%s >>> %s, %s' % (self.from_acc, self.to_acc, self.tr_amount)
|