Ejemplo n.º 1
0
def list(request):
    req = request.REQUEST
    q = Expense.objects.filter()
    if param_exist("amountStart",req):
        q = q.filter(amount__gte=req['amountStart'])
    if param_exist("amountEnd",req):
        q = q.filter(amount__lte=req['amountEnd'])
    if param_exist("dateStart",req):
        q = q.filter(date__gte=DateService.invert(req['dateStart']))
    if param_exist("dateEnd",req):
        q = q.filter(date__lte=DateService.invert(req['dateEnd']))
    if param_exist("subC",req):
        q = q.filter(subCategory=req['subC'])
    elif param_exist("cat",req):
        c = SubCategory.objects.filter(category=req['cat'])
        q = q.filter(subCategory__in=c)
    if param_exist("payT",req):
        q = q.filter(paymentType=req['payT'])
    if param_exist("sort",req):
        q = q.order_by(sortMethod(req))
    if param_exist("limit",req):
        list = q[req['start']:req['start']+req['limit']]
    else:
        list = q
    
    res = []
    for exp in list:
        res.append({'id': exp.id, 'amount': exp.amount, 'date': exp.date,
                    'text': exp.text, 'paymentType_name': exp.paymentType.name,
                    'subCategory_name': exp.subCategory.name, 'paymentTypeId': exp.paymentType.id,
                    'subCategoryId': exp.subCategory.id})

    data = '{"total": %s, "rows": %s}' % (q.count(), JsonParser.parse(res))
    return data
Ejemplo n.º 2
0
def update(request):
    req = request.REQUEST
    c = Card(pk=req['card.id'])
    p = CardDates(pk=req['id'],closeDate=DateService.invert(req['closeDate']),
                    expireDate=DateService.invert(req['expireDate']),
                    card=c)
    
    data = '{"success":true}'    
    try:
        p.save()
    except _mysql_exceptions.Warning:
        pass
    except Exception, e1:
        data = '{"success":false, "msg": "%s"}' % (e1.args)
Ejemplo n.º 3
0
def req_to_obj(request):
    req = request.REQUEST
    if req.has_key("id"):
        obj = Debits.objects.get(pk=req["id"])
    else:
        obj = Debits()
    obj.day = req["day"]
    obj.subCategory = SubCategory.objects.get(pk=req["subCategory.id"])
    obj.paymentType = PaymentType.objects.get(pk=req["paymentType.id"])
    obj.text = req["text"]
    obj.amount = req["amount"]
    obj.since = DateService.invert(req["since"])
    return obj
Ejemplo n.º 4
0
 def handle(self, *args, **options):
     list = Debits.objects.filter(Q(since__lte=datetime.now()) & (Q(last__isnull=True) | Q(last__lt=DateService.firstDateOfMonth(DateService.today()))))
     
     for debit in list:
         Expense.objects.create(text=debit.text,
                                amount=debit.amount,
                                subCategory=debit.subCategory,
                                paymentType=debit.paymentType,
                                date=DateService.invert(DateService.parse("%d/%d/%d" % (debit.day,
                                                                     DateService.today().tm_mon,
                                                                     DateService.today().tm_year))))
         debit.last = datetime.now()
         debit.save()
     
     self.stdout.write("Processed %d\n" % list.count())
Ejemplo n.º 5
0
def fromParams(req):
    if param_exist("id",req):
        e = CardData(pk=req['id'])
    else:
        e = CardData()
    
    c = Card(pk=req['card.id'])
    
    e.date=DateService.invert(req['date'])
    e.shop=req['shop']
    e.instalments=req['instalments']
    e.total=req['total']
    e.card = c
    
    return e
Ejemplo n.º 6
0
def fromParams(req):
    s = SubCategory.objects.get(pk=req['subCategory.id'])
    p = PaymentType(pk=req['paymentType.id'])
    if param_exist("text",req):
        text = req['text']
    else:
        text = s.name 

    if param_exist("id",req):
        e = Expense.objects.get(pk=req['id'])
    else:
        e = Expense()
        
    e.text=text
    e.date=DateService.invert(req['date']) 
    e.amount=req['amount']
    e.subCategory=s
    e.paymentType=p
    return e
Ejemplo n.º 7
0
def report(request):
    if RequestUtils.param_exist("date", request.REQUEST):
        date = DateService.parseDate(request.REQUEST['date'])
    else:
        date = DateService.addMonth(DateService.todayDate(),-1)
    fromDate = DateService.midNight(DateService.firstDateOfMonth(date))
    toDate = DateService.midNight(DateService.lastDateOfMonth(date),True)
    try:
        q = Expense.objects.filter(date__gte=fromDate, date__lte=toDate)
        filedata = StringIO.StringIO()
        filedata.write('"pk","date","text","amount","paymentTypePk","paymentTypeName","categoryPk","categoryName","subCategoryPk","subCategoryName"\n')
        for expense in q:
            d = {'pk': expense.pk,
                'date': DateService.invert(expense.date),
                'text': expense.text,
                'amount': expense.amount,
                'paymentTypePk': expense.paymentType.pk,
                'paymentTypeName': expense.paymentType.name,
                'categoryPk': expense.subCategory.category.pk,
                'categoryName': expense.subCategory.category.name,
                'subCategoryPk': expense.subCategory.pk,
                'subCategoryName': expense.subCategory.name}
            filedata.write(smart_str('%(pk)s,%(date)s,"%(text)s",%(amount)s,%(paymentTypePk)s,"%(paymentTypeName)s",%(categoryPk)s,"%(categoryName)s",%(subCategoryPk)s,"%(subCategoryName)s"\n' % d, 'latin1'))
        
        filename = "expensereport_%s.csv" % fromDate.strftime("%Y%m")
        
        if RequestUtils.param_exist("gz", request.REQUEST):
            filezip = StringIO.StringIO()
            zipped = gzip.GzipFile(filename, 'wb', fileobj=filezip)
            zipped.write(filedata)
            zipped.close()
            st = StorageService()
            st.file_put(filezip.getvalue(), filename + ".gz")
        else:
            st = StorageService()
            st.file_put(filedata.getvalue(), filename)
        error = ''
    except Exception, e:
        logger.error(str(e))
        send_mail("EXPORT ERROR", 'Processing %s.\n\nError: %s' % (fromDate.strftime("%Y-%m"), str(e)))
        error=str(e)
Ejemplo n.º 8
0
def list(request):
    req = request.REQUEST
    
    # find close date
    
    dateQuery = CardDates.objects.filter(closeDate__gt=DateService.todayDate())
    dateQuery = dateQuery.order_by('closeDate')
    if dateQuery.count()>0:
        dt = dateQuery[:1][0]
    else:
        dt = CardDates(closeDate=DateService.todayDate(), expireDate=DateService.todayDate())
    
    q = CardData.objects.extra(where=["ADDDATE(date, INTERVAL ((instalments+1) * 30) DAY) > '%s'" % DateService.invert(dt.closeDate)])
    if param_exist("sort",req):
        q = q.order_by(sortMethod(req))
    if param_exist("limit",req):
        start = req['start']
        limit = req['limit']
        list = q[start:start+limit]
    else:
        list = q

    res = []
    for exp in list:
        rem = FinancialService.remainInstalments(exp.date, dt.closeDate, exp.instalments)
        if rem >= 0:
            res.append({'id': exp.id, 'date': exp.date,
                            'shop': exp.shop, 'instalments': exp.instalments,
                            'card_id': exp.card.id, 'card_name': exp.card.name,
                            'total': exp.total, 'own': exp.own,
                            'partial': exp.total / exp.instalments,
                            'remain': rem })
    
    data = '{"total": %s,"close": "%s", "expire": "%s", "rows": %s}' % (CardData.objects.count(), DateService.format(dt.closeDate),DateService.format(dt.expireDate), JsonParser.parse(res))
    return data