Ejemplo n.º 1
0
 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('')
Ejemplo n.º 2
0
    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)
        """