def update(pricing_id, **mod_data): pricing = pricing_store.get(pricing_id) new_starts = commonlib.helpers.iso2date(mod_data['starts']) if 'starts' in mod_data else pricing.starts #Checking time interval for which pricing will change if pricing.starts == new_starts: changed_intervals_starts = pricing.starts changed_intervals_ends = pricing.ends elif pricing.starts < new_starts: changed_intervals_starts = pricing.starts changed_intervals_ends = new_starts elif pricing.starts > new_starts: changed_intervals_starts = new_starts changed_intervals_ends = pricing.starts #Checking usages for pricing changed time interval if dbaccess.find_usage(start=changed_intervals_starts, end=changed_intervals_ends, resource_ids=[pricing.resource]): msg = "Usages are associated with this pricing, you can't delete pricing." raise Exception(msg)#be.errors.ErrorWithHint(msg) if new_starts != pricing.starts: mod_data['starts'] = new_starts crit = dict(plan=pricing.plan, resource=pricing.resource, ends=pricing.starts-datetime.timedelta(1)) prev_pricing = pricing_store.get_by(crit) if prev_pricing: set(prev_pricing[0].id, 'ends', pricing.ends) old_pricing = dbaccess.get_resource_pricing(pricing.plan, pricing.resource, new_starts, [pricing_id]) if old_pricing:#old_pricing contains pricing at new_starts if old_pricing.starts and old_pricing.starts >= new_starts: msg = "Pricing start date should be greater than %s" % old_pricing.starts raise Exception(msg)#be.errors.ErrorWithHint(msg) mod_data['ends'] = old_pricing.ends set(old_pricing.id, 'ends', mod_data['starts']-datetime.timedelta(1)) pricing_store.update(pricing_id, **mod_data)
def get(member_id, resource_id, usage_time=None): """ returns rate """ # TODO: if resource owner is not bizplace then? usage_time = commonlib.helpers.iso2date(usage_time) or datetime.date.today() bizplace_id = resource_store.get(resource_id, fields=['owner'], hashrows=False) plan_id = dbaccess.get_member_plan_id(member_id, bizplace_id, usage_time) pricing = dbaccess.get_resource_pricing(plan_id, resource_id, usage_time) if pricing: return pricing.amount return dbaccess.get_default_pricing(resource_id, usage_time).amount
def new(resource_id, tariff_id, starts, amount, ends=None): if starts and ends: assert(commonlib.helpers.iso2date(ends) > commonlib.helpers.iso2date(starts)), \ "Pricing end date (%s) should be greater than start date (%s)" % (ends, starts) old_pricing = dbaccess.get_resource_pricing(tariff_id, resource_id, starts) if starts and old_pricing: starts = commonlib.helpers.iso2date(starts) if old_pricing.starts and old_pricing.starts>= starts: msg = "Pricing start date should be greater than %s" % old_pricing.starts raise Exception(msg)#be.errors.ErrorWithHint(msg) old_pricing_ends = starts - datetime.timedelta(1) set(old_pricing.id, 'ends', old_pricing_ends) return pricing_store.add(plan=tariff_id, resource=resource_id, starts=starts, ends=ends, amount=amount)