Esempio n. 1
0
class Boiler(models.Model):
    number = models.CharField(max_length=15)
    description = models.CharField(max_length=255)
    fuels = models.ManyToManyField(base.Fuel)
    flow = models.ForeignKey(BoilerFlow, blank=True, null=True)
    mbh = uscs.MBHField()
    cost = fields.CostField()

    class Meta:
        ordering = 'mbh',

    @property
    def cost_per_mbh(self):
        return self.cost / self.mbh

    def matching(self, objects, fueled=False):
        if not fueled: return objects
        objects = objects.annotate(num=Count('fuels'))
        objects = objects.filter(num=self.fuels.count())
        hasfuel = lambda set, fuel: set.filter(fuels=fuel)
        return reduce(hasfuel, self.fuels.all(), objects)

    def bestfit(self, objects, fueled=False):
        for item in self.matching(objects, fueled):
            if item.contains(self.mbh):
                return item

    def maintenance_cost_for(self, building, mbh, tons):
        return self.maintenance_cost

    def repair_cost_for(self, building, mbh, tons):
        return self.repair_cost * mbh

    def replacement_cost_for(self, building, mbh, tons):
        return self.replacement_cost * mbh

    @cached_property
    def repair_cost(self):
        return self.bestfit(BoilerRepair.objects, True).cost

    @cached_property
    def maintenance_cost(self):
        return self.bestfit(
            BoilerMaintenance.objects.filter(flow=self.flow)).cost

    @cached_property
    def replacement_cost(self):
        try:
            cost = self.replacements.all()[0].cost
        except IndexError:
            cost = self.cost
        return cost / self.mbh

    def rates(self, zone):
        return self.matching(BoilerRates.objects, True).get(zone=zone)

    def __unicode__(self):
        fuels = '/'.join(map(str, self.fuels.all()))
        return '%.2f MBH, %s %s boiler' % (self.mbh, price(self.cost), fuels)
Esempio n. 2
0
class Upkeep(models.Model):
    min = uscs.MBHField()
    max = uscs.MBHField(blank=True,
                        null=True,
                        help_text='Leave blank for none.')

    class Meta:
        abstract = True
        ordering = 'cost',

    def contains(self, mbh):
        return self.min <= mbh <= (self.max or mbh)

    def __unicode__(self):
        if self.max:
            return '%s to %s: %s' % (self.min, self.max, self.cost)
        return 'More than %s: %s' % (self.min, self.cost)
Esempio n. 3
0
class FurnaceUpkeep(heating.Upkeep):
    fuel = models.ForeignKey(base.Fuel)
    cost = fields.CostField()
    mbh = uscs.MBHField()

    class Meta:
        abstract = True

    @cached_property
    def cost_per_mbh(self):
        return self.cost / self.mbh

    def adjusted_cost(self, mbh):
        return self.cost_per_mbh * mbh
Esempio n. 4
0
class BoilerRepair(Upkeep):
    mbh = uscs.MBHField()
    fuels = models.ManyToManyField(base.Fuel)
    cost = uscs.CostPerMBHField()

    @cached_property
    def cost_per_mbh(self):
        return self.cost / self.mbh

    def adjusted_cost(self, mbh):
        return self.cost_per_mbh * mbh

    def __unicode__(self):
        fuels = '/'.join(map(str, self.fuels.all()))
        return super(BoilerRepair, self).__unicode__() + ' (%s)' % fuels
Esempio n. 5
0
class EnergySupply(models.Model):
    number = models.CharField(max_length=15)
    description = models.CharField(max_length=255)
    mbh = uscs.MBHField()
    cost = fields.CostField(help_text='Boiler cost not included')
    square_feet = uscs.SquareFootField()
    boilers = models.ManyToManyField(Boiler)

    class Meta:
        ordering = 'mbh',
        verbose_name_plural = 'energy supplies'

    def __iter__(self):
        return iter(self.boilers.all())

    @property
    def cost_per_mbh(self):
        return self.cost / self.mbh

    @cached_property
    def fixed_cost_per_square_foot(self):
        return self.cost / self.square_feet

    def fixed_cost(self, building):
        return self.fixed_cost_per_square_foot * building.square_feet

    def cost_for(self, building, mbh, tons):
        mbh = mbh.into(MBH)
        cost_per_mbh = sum(boiler.cost_per_mbh
                           for boiler in self.boilers.all())
        return (cost_per_mbh * mbh) + self.fixed_cost(building)

    def original_cost(self, building):
        return self.cost_for(building, self.mbh, None)

    def __unicode__(self):
        return u'%s ft\u00B2, %.2f MBH, %s unit' % (intcomma(
            self.square_feet.just('ft**2')), self.mbh, price(self.cost))
Esempio n. 6
0
class PackagedUnit(models.Model):
    number = models.CharField(max_length=15)
    description = models.CharField(max_length=255)
    type = models.ForeignKey(SystemType)
    flow = models.ForeignKey(base.Flow, blank=True, null=True)
    vav = models.BooleanField()
    dx = models.BooleanField()
    multizone = models.BooleanField()
    tons = uscs.TonField()
    mbh = uscs.MBHField(blank=True,
                        null=True,
                        help_text='Leave blank if there is no heating system.')
    cost = fields.CostField()

    class Meta:
        ordering = 'tons',

    @property
    def cost_per_mbh(self):
        return self.cost / self.mbh

    @property
    def cost_per_ton(self):
        return self.cost / self.tons

    def cost_for(self, building, mbh, tons):
        ccost = self.cost_per_ton * tons
        if mbh is None or self.mbh is None:
            return ccost
        hcost = self.cost_per_mbh * mbh
        return max(hcost, ccost)

    def original_cost(self, building):
        return self.cost_for(building, self.mbh, self.tons)

    ###########
    # Helpers #
    ###########

    def bestfit(self, objects):
        for item in objects.filter(flow=self.flow, type=self.type):
            if item.contains(self.tons):
                return item

    def typefit(self, objects):
        if self.vav:
            return objects.filter(vav=True, multizone=self.multizone)
        elif self.dx:
            return objects.filter(vav=False, dx=True)
        return objects.filter(vav=False, dx=False, multizone=self.multizone)

    @cached_property
    def furnace_component(self):
        return FurnaceComponent(self, base.Fuel.objects.get(name='Gas'))

    ##############################
    # Maintenance/Repair/Replace #
    ##############################

    def __iter__(self):
        return iter([self, self.furnace_component])

    def maintenance_cost_for(self, building, mbh, tons):
        return self.maintenance_cost

    def repair_cost_for(self, building, mbh, tons):
        return self.repair_cost * tons

    def replacement_cost_for(self, building, mbh, tons):
        return self.replacement_cost * tons

    ########################
    # Main Costs and Rates #
    ########################

    @cached_property
    def maintenance_cost(self):
        return self.bestfit(Maintenance.objects).cost

    @cached_property
    def repair_cost(self):
        for repair in self.typefit(Repair.objects):
            if repair.contains(self.tons):
                return repair.cost_per_ton

    @cached_property
    def replacement_cost(self):
        try:
            cost = self.replacements.all()[0].cost
        except self.replacements.model.DoesNotExist:
            cost = self.cost
        return cost / self.tons

    def rates(self, zone):
        return self.typefit(Rates.objects).get(zone=zone)

    ###########################

    def __unicode__(self):
        power = '{0} ton'.format(self.tons.item())
        if self.mbh: power += ', {0}'.format(self.mbh)
        type = '{1} {0}'.format(self.flow,
                                self.type) if self.flow else unicode(self.type)
        return '%s, %s %s unit' % (power, price(self.cost), type)