def get_location_from_base_url(): try: return Location.select(Location.q.url==cherrypy.request.base)[0] except IndexError: try: # try w/o www. Ref: website #89 url_wo_www = cherrypy.request.base.replace("://www.", "") return Location.select(Location.q.url==url_wo_www)[0] except IndexError: pass return Location.get(1)
def get_location_from_base_url(): try: req_url = cherrypy.request.base.lower() if "https:" in req_url: # website#252 req_url = req_url.replace("https:", "http:") return Location.select(Location.q.url == req_url)[0] except IndexError: try: # try w/o www. Ref: website #89 url_wo_www = cherrypy.request.base.replace("://www.", "") return Location.select(Location.q.url == url_wo_www)[0] except IndexError: pass return Location.get(1)
def now(location=1): if not isinstance(location, Location): try: location = Location.get(location) except: return datetime.now() zone_name = location.timezone if not zone_name: zone_name = 'UTC' time_zone = timezone(zone_name) local_now = datetime.now(tz=time_zone) #convert back to a naive datetime object so that its comparable with those stored on rusages etc #ideally everything would be dealing with localised datetime objects but this would take significant work on the existing data and scripts. naive_now = datetime(local_now.year, local_now.month, local_now.day, local_now.hour, local_now.minute, local_now.second) return naive_now
def get_tariff(loc, userid, usage_start_time, default=True): result = Resource.select(AND(RUsage.q.resourceID == Resource.q.id, Resource.q.type=='tariff', RUsage.q.cancelled==0, RUsage.q.userID==userid, Resource.q.placeID==loc, RUsage.q.start <= usage_start_time, RUsage.q.end_time >= usage_start_time)) try: return result[0] except: if default: return Location.get(loc).defaulttariff return None
def vat_switch(): """add total taxes, resource_tax_dict, vat_included """ from hubspace.model import Invoice, Location from hubspace.invoice import calculate_tax_and_amount #recalculate all the old invoice costs and amounts, as used to happen everytime we did looked at the invoice! london = Location.get(1) bristol = Location.get(2) kx = Location.get(11) switch_time = datetime(2008, 12, 1, 0, 0) special = Invoice.select(AND(IN(Invoice.q.locationID, [1, 2, 11]), Invoice.q.created < switch_time)) not_special = Invoice.select(OR(NOT(IN(Invoice.q.locationID, [1, 2, 11])), Invoice.q.created >= switch_time)) for inv in not_special: tmp = inv.sent inv.sent = None calculate_tax_and_amount(inv) inv.sent = tmp london.vat_default = 17.5 bristol.vat_default = 17.5 kx.vat_default = 17.5 #might need to re-patch this bit later for bristol as I think they sent out some invoices at 17.5% after the 1st December for inv in special: tmp = inv.sent inv.sent = None calculate_tax_and_amount(inv) inv.sent = tmp london.vat_default = 15 bristol.vat_default = 15 kx.vat_default = 15
def __init__(self, loc_id, period=None, start=None, end=None): self.location = Location.get(loc_id) if start and end: self.start, self.end = start, end elif period: if period == 'thismonth': self.start, self.end = get_this_months_limits() elif period == 'thisandlastmonths': self.start, self.end = get_this_and_last_months_limits() elif period == 'lastmonth': self.start, self.end = get_last_months_limits() elif period == 'last12months': self.start, self.end = get_last_12months_limits() else: self.start, self.end = get_this_months_limits() self.results = dict()
def bristol_vat_switch(): """get the bristol invoices that were sent at 15% before mid day on the 2nd of december and set them to 17.5% """ from hubspace.model import Invoice, Location from hubspace.invoice import calculate_tax_and_amount #recalculate all the old invoice costs and amounts, as used to happen everytime we did looked at the invoice! bristol = Location.get(2) start_switch_time = datetime(2008, 12, 1, 0, 0) end_switch_time = datetime(2008, 12, 2, 12, 0) special = Invoice.select(AND(Invoice.q.locationID == 2, Invoice.q.created < end_switch_time, Invoice.q.created > start_switch_time)) bristol.vat_default = 17.5 for inv in special: tmp = inv.sent inv.sent = None calculate_tax_and_amount(inv) inv.sent = tmp bristol.vat_default = 15.0