def expensesList(request): req = request.POST if RequestUtils.param_exist("toDate", req): toDate = DateService.parseDate(req['toDate']) else: toDate = datetime.datetime.now() if RequestUtils.param_exist("fromDate", req): fromDate = DateService.parseDate(req['fromDate']) else: fromDate = toDate - datetime.timedelta(days=getattr(settings,'EXPENSES_DEFAULT_DAYS_LIST', 5)) q = Expense.objects.filter(date__gte = DateService.midNight(fromDate), date__lte = DateService.midNight(toDate, True)) currentCategory = 0 if RequestUtils.param_exist("category", req) and req['category']!='0': c = SubCategory.objects.filter(category=req['category']).values_list('pk', flat=True) q = q.filter(subCategory__in=list(c)) currentCategory = req['category'] q = q.order_by("-date") total = 0 for e in q: total += e.amount cList = Category.objects.all().order_by("name") return {"settings": settings, "list": q, "today": datetime.date.today(), "filterStart": fromDate, "filterEnd": toDate, "total": total, "categoryList": cList, "currentCategory": int(currentCategory)}
def get_loan_to_save(data, amount = None, lid = None): if not lid: l = Loan.objects.get(pk=data['loan.id']) else: l = Loan.objects.get(pk=lid) if not amount: amount = round(l.amount / l.instalments,2) if amount > l.remain: amount = l.remain validate_amount(amount) if param_exist("id",data): p = Payment.objects.get(pk=data['id']) prevAmount = p.amount else: p = Payment(loan=l) prevAmount = None if checkPayment(l,amount,prevAmount): if prevAmount: diff = float(prevAmount) - float(amount) l.remain = unicode(float(l.remain) + diff) else: l.remain = unicode(float(l.remain) - float(amount)) p.amount=amount p.date=DateService.parseDate(data['date']) else: raise ValidationError(_('The entered amount is greater than the amount owed.')) return (l,p)
def incomeList(request): req = request.POST if RequestUtils.param_exist("fromDate", req): fromDate = DateService.parseDate(req['fromDate']) else: dt = datetime.date.today() - datetime.timedelta(days=getattr(settings,'INCOME_DEFAULT_DAYS_AHEAD', 90)) fromDate = datetime.date(dt.year, dt.month, 1) q = Income.objects.filter(period__gte = DateService.midNight(fromDate)) q = q.order_by("-period") return {"list": q, "filterStart": fromDate}
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)
p.save() amount += float(p.amount) except _mysql_exceptions.Warning: pass except Exception, e1: failed.append(l.reason) if param_exist('subCategory.id', req) and not req['subCategory.id'] == '0': try: subCategory = SubCategory.objects.get(pk=req['subCategory.id']) pt = getattr(settings,'LOAN_PAYMENT_TYPE', None) if pt: paymentType = PaymentType.objects.get(pk=pt) Expense.objects.create(amount=-1*amount, text=subCategory.name, date=DateService.parseDate(req['date']), subCategory=subCategory, paymentType=paymentType) except Exception, e2: failed.append(e2) if len(failed) > 0: return '{"success":false, "msg": "Failed to save %s"}' % ",".join(failed) return data #deprecated @json_response def save_or_update_old(request): data = '{"success":true}' req = request.REQUEST amount=req['amount']