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
def generate(): date = DateService.addMonth(DateService.todayDate(),-1) fromDate = DateService.addMonth(DateService.firstDateOfMonth(date),-6) toDate = DateService.lastDateOfMonth(date) values = DateService.getMonthRange(fromDate, toDate) for value in values: create_stats(*value)
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)
def generate_current(): date = DateService.todayDate() fromDate = DateService.firstDateOfMonth(date) toDate = DateService.lastDateOfMonth(date) create_stats(fromDate, toDate)
def current_date(request): from pycash.services import DateService return {'today': DateService.todayDate()}
def taxHome(request): # some stuff are resolved using python code instead of django queryset because limitations of appengine limit = (datetime.datetime.now() + datetime.timedelta(days=getattr(settings,'TAX_DEFAULT_DAYS_ADVANCE', 5))) upcoming = Tax.objects.filter(expire__lte=limit).order_by('expire') upcoming = [tax for tax in upcoming if tax.amount > 0] current = Tax.objects.filter(expire__range=(DateService.firstDateOfMonth(datetime.date.today()), DateService.lastDateOfMonth(datetime.date.today()))) nextList = Tax.objects.filter(expire__range=DateService.getMonthDateRange(DateService.addMonth(DateService.todayDate(), 1))) return {"list": upcoming, "listmonth": sorted(current, key=lambda service: tax.service), "listnextmonth": sorted(nextList, key=lambda service: tax.service)}