def editIncome(self): errorslist = self.validate() if errorslist: obj = { 'errors': errorslist } return json.dumps(obj) transId = request.POST['transId'] incomeType = request.POST['incomeType'] incomeFrom = request.POST['incomeFrom'] incomeAmount = request.POST['incomeAmount'] incomeDate = request.POST['incomeDate'] now = datetime.datetime.today() curHour = now.hour curMin = now.minute curSec = now.second date = incomeDate.split('/') date = datetime.datetime(int(date[2]),int(date[0]),int(date[1]),curHour,curMin,curSec) Transaction.save( id=transId, type=incomeType, name=incomeFrom, amount=incomeAmount, date=date ) return json.dumps('')
def markLate(self): unitId = request.POST['unitId'] latefee = request.POST['latefee'] forMonth = request.POST['forMonth'] forYear = request.POST['forYear'] date = request.POST['date'] latefeeNum = latefee.replace('$','') Transaction.record_latefee(unitId,latefeeNum,forMonth,forYear,date) remind = True if request.POST['remind'] == 'true' and float(latefeeNum) > 0.0 else False if remind: tenants = Tenant_lease.get_tenants_from_unit(unitId) emails = [tenant.email for tenant in tenants] subj = '{0} - late fee owed'.format(request.environ.get('COMPANY_NAME')) msg = """\ Dear Tenant, This is a notice from your landlord that your rent was turned in late. Amount owed: ${0} Please pay this amount promptly. Thanks, {1} """.format(latefee, request.environ.get('COMPANY_NAME')) mailman.send(emails, subj, msg) return json.dumps('')
def editExpense(self): errorslist = self.validate() if errorslist: obj = { 'errors': errorslist } return json.dumps(obj) transId = request.POST['transId'] expenseType = request.POST['expenseType'] expenseTo = request.POST['payTo'] expenseAmount = '-'+request.POST['expenseAmount'] expenseDate = request.POST['expenseDate'] now = datetime.datetime.today() curHour = now.hour curMin = now.minute curSec = now.second date = expenseDate.split('/') date = datetime.datetime(int(date[2]),int(date[0]),int(date[1]),curHour,curMin,curSec) Transaction.save( id=transId, type=expenseType, name=expenseTo, amount=expenseAmount, date=date ) return json.dumps('')
def markUnpaid(self): unitId = request.POST['unitId'] forMonth = request.POST['forMonth'] forYear = request.POST['forYear'] Transaction.undo_rent(unitId,forMonth,forYear) return json.dumps('')
def updateTransaction(self): unitId = request.POST['unitId'] forMonth = request.POST['forMonth'] forYear = request.POST['forYear'] amount = request.POST['amount'] Transaction.updateRent(amount,unitId,forMonth,forYear) return json.dumps('')
def get(self): unitId = request.POST['unitId'] type = request.POST['type'] transactions = Transaction.get_type(unitId, type) transactionInfo = [] for transaction in transactions: date = transaction.date if transaction.income: transAmount = '$' + str('%.2f' % transaction.amount) else: transAmount = '- $' + str('%.2f' % transaction.amount) transObj = { 'type': transaction.type, 'amount': transAmount, 'income': transaction.income, 'month': date.month, 'day': date.day, 'year': date.year } transactionInfo.append(transObj) return json.dumps(transactionInfo)
def markPaid(self): unitId = request.POST['unitId'] leaseId = request.POST['leaseId'] amount = request.POST['amount'] due = int(request.POST['due']) forMonth = int(request.POST['forMonth']) forYear = int(request.POST['forYear']) markLatePaid = request.POST['markLatePaid'] result = {} if markLatePaid == '1': Transaction.record_late_paid(unitId,forMonth,forYear) else: companyId = request.environ.get("COMPANY_ID") now = datetime.datetime.today() id = str(uuid.uuid1()) record = Unit.get_unit_info(unitId) curM = now.month curY = now.year if curM != forMonth or (curM == forMonth and curY != forYear): now = datetime.datetime.today() curHour = now.hour curMin = now.minute curSec = now.second date = datetime.datetime(forYear,forMonth,due,curHour,curMin,curSec) else: date = now Transaction.record_rent( id=str(uuid.uuid1()), companyid=companyId, propertyid=record[2], leaseid=leaseId, unitid=unitId, type='Rent', formonth=forMonth, foryear=forYear, date=date, income=1, name='Unit ' + record[0] + ', ' + record[1], amount=amount ) return json.dumps(result)
def addExpense(self): errorslist = self.validate() if errorslist: obj = { 'errors': errorslist } return json.dumps(obj) expenseType = request.POST['expenseType'] payTo = request.POST['payTo'] expenseAmount = '-'+request.POST['expenseAmount'] expenseDate = request.POST['expenseDate'] contactType = request.POST['contactType'] contactId = request.POST['contactId'] companyId = request.environ.get("COMPANY_ID") now = datetime.datetime.today() curHour = now.hour curMin = now.minute curSec = now.second date = expenseDate.split('/') date = datetime.datetime(int(date[2]),int(date[0]),int(date[1]),curHour,curMin,curSec) if contactType == 'unit': leaseId = Lease.get_current_of_unit(contactId) if not leaseId: leaseId = None unitInfo = Unit.get_unit_info(contactId) propertyId = unitInfo[2] payTo = payTo.split('#') prop = payTo[0] prop = prop.strip() unit = payTo[1] payTo = prop + ' #' + unit Transaction.create( id=str(uuid.uuid1()), leaseid=leaseId, unitid=contactId, companyid=companyId, propertyid=propertyId, type=expenseType, name=payTo, amount=expenseAmount, date=date ) elif contactType == 'property': Transaction.create( id=str(uuid.uuid1()), companyid=companyId, propertyid=contactId, type=expenseType, name=payTo, amount=expenseAmount, date=date ) elif contactType == 'tenant': leaseId = Tenant_lease.get_lease_of_tenant(contactId) if not leaseId: leaseId = None record = Tenant.get_tenantInfo(contactId) if record: unitId = record[0] propertyId = record[1] else: unitId = None propertyId = None Transaction.create( id=str(uuid.uuid1()), leaseid=leaseId, unitid=unitId, companyid=companyId, propertyid=propertyId, type=expenseType, name=payTo, amount=expenseAmount, date=date ) elif contactType == 'contact': Transaction.create( id=str(uuid.uuid1()), contactid=contactId, companyid=companyId, type=expenseType, name=payTo, amount=expenseAmount, date=date ) else: Transaction.create( id=str(uuid.uuid1()), companyid=companyId, type=expenseType, name=payTo, amount=expenseAmount, date=date ) return json.dumps('')
def json(self): month = request.POST['month'] year = request.POST['year'] type = request.POST['type'] search = request.POST['search'] filter = request.POST['filter'] datePeriod = request.POST['datePeriod'] page = request.POST['page'] initLoad = request.POST['initLoad'] periodClick = request.POST['periodClick'] updateBalance = request.POST['updateBalance'] property = request.POST['property'] companyId = request.environ.get("COMPANY_ID") if not valid.search(search): return json.dumps({'errors':[{'selector':'', "message":"Search only accepts letters and numbers. Please try again."}]}) startSlice = (int(page) - 1) * 20; endSlice = startSlice + 20; query = meta.Session.query(Transaction).filter(Transaction.companyid==companyId).filter(Transaction.deleted==None) if property != 'all': query = query.filter(Transaction.propertyid==property) incomeTotal = 0 expenseTotal = 0 if initLoad == '1' or updateBalance == '1': for record in query: if record.amount > 0: incomeTotal = incomeTotal + record.amount else: expenseTotal = expenseTotal + record.amount expenseTotal = expenseTotal * -1 if incomeTotal: incomeTotal = '%.2f' % incomeTotal if expenseTotal: expenseTotal = '%.2f' % expenseTotal if search != 'all': query = query.filter(Transaction.name.like('%'+search+'%')) if periodClick == '1' and datePeriod != 'all': datePeriodList = Transaction.get_date_period(datePeriod) startDate = datePeriodList[0] endDate = datePeriodList[1] startDate = datetime.datetime(startDate[0],startDate[1],startDate[2]) endDate = datetime.datetime(endDate[0],endDate[1],endDate[2]) query = query.filter(and_(Transaction.date>=startDate,Transaction.date<=endDate)) for record in query: if record.amount > 0: incomeTotal = incomeTotal + record.amount else: expenseTotal = expenseTotal + record.amount expenseTotal = expenseTotal * -1 if month != 'all': month = int(month) year = int(year) lastDay = calendar.monthrange(year,month) lastDay = lastDay[1] firstDate = datetime.datetime(year,month,1) lastDate = datetime.datetime(year,month,lastDay) query = query.filter(and_(Transaction.date>=firstDate,Transaction.date<=lastDate)) if type != 'all': if type == 'Rent': query = query.filter(Transaction.type=='Rent') elif type == 'Deposit': query = query.filter(Transaction.type=='Deposit') else: typeList = ['Rent','Deposit'] query = query.filter(~Transaction.type.in_(typeList)) if filter != 'all': if filter == 'income': query = query.filter(Transaction.amount>0) else: query = query.filter(Transaction.amount<0) all_records = query records = query.order_by(Transaction.date.desc()).all() allTransList = [] for record in records: contact = record.name transDate = record.date d = str(transDate.day) m = str(transDate.month) y = str(transDate.year) hr = str(transDate.hour) min = str(transDate.minute) sec = str(transDate.second) date = y+'-'+m+'-'+d+'-'+hr+'-'+min+'-'+sec obj = { 'transId': record.id, 'transAmount': record.amount, 'transDate': date, 'transType': record.type, 'transContact': contact, 'transPropertyId': record.propertyid } allTransList.append(obj) slicedRecords = [] counter = 0 has_prev_records = 0 for item in allTransList: if startSlice <= counter < endSlice: slicedRecords.append(item) if counter == endSlice: has_prev_records = 1 break counter = counter + 1 transList = slicedRecords backwards = lambda l: (backwards (l[1:]) + l[:1] if l else []) transList = backwards(transList) prevTotal = 0 if has_prev_records: firstDate = transList[0] firstDate = firstDate['transDate'].split('-') year = int(firstDate[0]) mo = int(firstDate[1]) day = int(firstDate[2]) hr = int(firstDate[3]) min = int(firstDate[4]) sec = int(firstDate[5]) firstDate = datetime.datetime(year,mo,day,hr,min,sec) prev_query = meta.Session.query(func.sum(Transaction.amount)).filter(and_(Transaction.companyid==companyId,\ Transaction.date<firstDate,\ Transaction.deleted==None)) if property != 'all': prev_query = prev_query.filter(Transaction.propertyid==property) if search != 'all': prev_query = prev_query.filter(Transaction.name.like('%'+search+'%')) if periodClick == '1' and datePeriod != 'all': datePeriodList = Transaction.get_date_period(datePeriod) startDate = datePeriodList[0] endDate = datePeriodList[1] startDate = datetime.datetime(startDate[0],startDate[1],startDate[2]) endDate = datetime.datetime(endDate[0],endDate[1],endDate[2]) prev_query = prev_query.filter(and_(Transaction.date>=startDate,Transaction.date<=endDate)) if month != 'all': month = int(month) year = int(year) lastDay = calendar.monthrange(year,month) lastDay = lastDay[1] firstDate = datetime.datetime(year,month,1) lastDate = datetime.datetime(year,month,lastDay) prev_query = prev_query.filter(and_(Transaction.date>=firstDate,Transaction.date<=lastDate)) if type != 'all': if type == 'Rent': prev_query = prev_query.filter(Transaction.type=='Rent') elif type == 'Deposit': prev_query = prev_query.filter(Transaction.type=='Deposit') else: typeList = ['Rent','Deposit'] prev_query = prev_query.filter(~Transaction.type.in_(typeList)) if filter != 'all': if filter == 'income': prev_query = prev_query.filter(Transaction.amount>0) else: prev_query = prev_query.filter(Transaction.amount<0) prev_query = prev_query.first() prevTotal = prev_query[0] if not prevTotal: prevTotal = 0 transRecords = { 'transactions': transList, 'totalRecords': len(records), 'incomeTotal': incomeTotal, 'expenseTotal': expenseTotal, 'initLoad': initLoad, 'prevTotal': prevTotal } return json.dumps(transRecords)
def delete(self): transId = request.POST['transId'] Transaction.delete(id=transId) return json.dumps('')
def json(self): companyid = request.environ.get("COMPANY_ID") propertyId = request.POST['propertyId'] unit = request.POST['unit'] sort = request.POST['sort'] sortOrder = request.POST['sortOrder'] status = request.POST['status'] curPage = request.POST['curPage'] if not valid.search(unit): return json.dumps({'errors':[{'selector':'', "message":"Unit filter only accepts letters and numbers. Please try again."}]}) if status == 'vacant': status = 'Vacant' elif status == 'underContract': status = 'Under Contract' elif status == 'movingIn': status = 'Moving In' elif status == 'movingOut': status = 'Moving Out' elif status == 'm2m': status = 'Month-to-month' now = datetime.date.today() startSlice = (int(curPage) - 1) * 20; endSlice = startSlice + 20; propIdList = [] if propertyId == 'all': user_id = Manager.get_id_of_username(request.environ.get("REMOTE_USER")) properties = Property.get_propertyList_of_userid(user_id) for item in properties: propIdList.append(item.id) else: propIdList.append(propertyId) if sort == 'rent': if sortOrder == 'asc': order = Lease.rent.asc() else: order = Lease.rent.desc() elif sort == 'due': if sortOrder == 'asc': order = Lease.due.asc() else: order = Lease.due.desc() elif sort == 'latePay': order = Property.name.asc() # ignore latePay for time being else: order = Unit.label.asc() records = meta.Session.query(Unit.label, Unit.id, Lease.rent, Lease.due,\ Lease.startdate, Lease.enddate, Lease.outdate, Lease.id,\ Property.id, Property.name).outerjoin(Lease,Property).\ filter(Unit.deleted==None).\ filter(Lease.deleted==None).\ filter(Unit.propertyid.in_(propIdList)) # records at this point includes multiple records of same unit with multiple lease records allRecords = [] for record in records: allRecords.append((record[1], record[4], record[6])) no_lease_ever = [] has_old_lease = [] cur_active_lease = [] future_lease = [] for item in allRecords: unitid = item[0] startdate = item[1] outdate = item[2] if not startdate: no_lease_ever.append(unitid) elif now >= startdate and (not outdate or now <= outdate): cur_active_lease.append(unitid) elif now > startdate and (now > outdate): has_old_lease.append(unitid) elif now < startdate: future_lease.append(unitid) records = records.filter(or_(Unit.id.in_(no_lease_ever),\ (and_(Unit.id.in_(cur_active_lease),\ now>=Lease.startdate,\ (or_(Lease.outdate==None,now<Lease.outdate)))),\ (and_(Unit.id.in_(has_old_lease),\ ~Unit.id.in_(cur_active_lease),\ ~Unit.id.in_(future_lease))),\ (and_(Unit.id.in_(future_lease),\ ~Unit.id.in_(cur_active_lease),\ (or_(Lease.outdate==None,\ now<Lease.outdate)))))) if unit != '0': labelLike = '%'+unit+'%' records = records.filter(Unit.label.like('%'+labelLike+'%')) records = records.order_by(order,Unit.label).all() unsortedRecords = [] duplicateUnit = [] for label, unitid, rent, due, start, end, out, leaseid, propid, propname in records: if unitid in duplicateUnit and unitid in has_old_lease and not unitid in cur_active_lease and not unitid in future_lease: continue obj = { 'label': label, 'unitid': unitid, 'rent': rent, 'due': due, 'start': start, 'end': end, 'out': out, 'leaseid': leaseid, 'propid': propid, 'propname': propname } unsortedRecords.append(obj) duplicateUnit.append(unitid) labelUnitIdList = [] recordsList = [] for record in unsortedRecords: label = record['label'] unitid = record['unitid'] rent = record['rent'] due = record['due'] start = record['start'] end = record['end'] out = record['out'] leaseid = record['leaseid'] propid = record['propid'] propname = record['propname'] labelId = label+'*'+unitid labelUnitIdList.append(labelId) obj = { 'label': label, 'unitid': unitid, 'rent': rent, 'due': due, 'start': start, 'end': end, 'out': out, 'leaseid': leaseid, 'propid': propid, 'propname': propname } recordsList.append(obj) sortedRecords = [] if sort == "0": labelUnitIdList = sorted(labelUnitIdList, key=self.keynat) for labelId in labelUnitIdList: labelId = labelId.split('*') label = labelId[0] id = labelId[1] sortedRecords.extend([d for d in recordsList if d['label'] == label and d['unitid'] == id]) recordsList = sortedRecords unitRecords = [] vacantList = [] vacant = 0 m2m = 0 underContract = 0 movingIn = 0 movingOut = 0 totalStatusRecords = 0 for record in recordsList: label = record['label'] unitid = record['unitid'] rent = record['rent'] due = record['due'] start = record['start'] end = record['end'] out = record['out'] leaseid = record['leaseid'] propid = record['propid'] propname = record['propname'] unit_status = Lease.unit_status(start, end, out) if unit_status == "Vacant": rent = '-' due = '-' vacant = vacant + 1 elif unit_status == 'Month-to-month': m2m = m2m + 1 elif unit_status == 'Under Contract': underContract = underContract + 1 elif unit_status == 'Moving In': movingIn = movingIn + 1 elif unit_status == 'Moving Out': movingOut = movingOut + 1 if status == "all" or unit_status == status: totalStatusRecords = totalStatusRecords + 1 obj = { 'propertyName': propname, 'propertyId': propid, 'leaseid': leaseid, 'unitId': unitid, 'label': label, 'rent': rent, 'due': due, 'latepay': '-', 'status': unit_status } if unit_status == 'Vacant': vacantList.append(obj) else: unitRecords.append(obj) unitRecords.extend(vacantList) slicedRecords = [] counter = 0 for record in unitRecords: if startSlice <= counter < endSlice: slicedRecords.append(record) counter = counter + 1 slicedRecords = Transaction.calLatefee(slicedRecords) unitRecords = slicedRecords propertyUnit = { 'unit': unitRecords, 'totalStatusRecords': totalStatusRecords, 'vacant': vacant, 'm2m': m2m, 'underContract': underContract, 'movingIn': movingIn, 'movingOut': movingOut } return json.dumps(propertyUnit) """