Пример #1
0
    def post(self, action=None):
        if not users.get_current_user():
            self.redirect('/')

        startparam = self.request.get('startdate').strip()
        endparam = self.request.get('enddate').strip()
        dateformat = '%m/%d/%Y'

        if startparam != '' or endparam != '':
            startdate = time.strptime(startparam, dateformat)
            enddate = time.strptime(endparam, dateformat)

        if not startparam or not endparam:
            startdate = time.gmtime(
            )  #time.strftime(dateformat, time.gmtime())
            enddate = time.gmtime()  #time.strftime(dateformat, time.gmtime())

        startdate = time.strftime('%Y-%m-%d %H:%M:%S', startdate)
        enddate = time.strftime('%Y-%m-%d %H:%M:%S', enddate)

        trips = []
        userdata = None
        #AND when >= :start AND when <= :end
        if users.get_current_user():
            user = users.get_current_user()
            trip_user = TripFilesUser.gql("WHERE who =:who", who=user)
            userdata = trip_user.get()
            #if admin user is getting report for another user
            if self.request.get('user_email'):
                user_email = self.request.get('user_email').strip()
                logging.debug('user_email=' + user_email)
                user = User(email=self.request.get('user_email').strip())
                trip_users = TripFilesUser.gql("WHERE who =:who", who=user)
                logging.debug("Count=" + str(trip_users.count()))
                trip_user = trip_users.get()
                logging.debug('Report user='******'/')

        viewtrips = []
        report_total_distance = 0.0
        report_total_other_pound = 0
        report_total_other_pence = 0
        report_total_passenger_mileage = 0

        tripcount = 0
        firstpage_trips = []
        middlepage_trips = []  #trips in each middlepage
        for trip in trips:
            viewlegs = []
            trip_total_distance = 0.0
            count = len(trip.waypoints)
            waypoints_description = ''
            for i in xrange(count - 1):
                distance = (trip.distances[i + 1]) * 0.000621371192
                distance = round(distance, 2)
                viewleg = TripLegView(trip.waypoints[i], trip.waypoints[i + 1],
                                      distance,
                                      trip.when.strftime('%a, %d %b %Y'),
                                      i + 1)
                viewlegs.append(viewleg)
                trip_total_distance = trip_total_distance + distance
                if i == 0:
                    waypoints_description = trip.waypoints[i].upper(
                    ) + ' - ' + trip.waypoints[i + 1].upper()
                else:
                    waypoints_description = waypoints_description.upper(
                    ) + ' - ' + trip.waypoints[i + 1].upper()

            #if we have total_distance saved, use it since the user may have updated the total manually
            if trip.total_distance and trip.total_distance > 0:
                trip_total_distance = trip.total_distance

            other_expense_label = trip.other_expenses_label
            try:
                other_charges_pound = int(trip.other_expenses_charge / 100)
            except:
                other_charges_pound = 0

            if trip.other_expenses_charge > 0:
                other_charges_pence = trip.other_expenses_charge - (
                    other_charges_pound * 100)
            else:
                other_charges_pence = 0

            report_total_other_pence = report_total_other_pence + other_charges_pence + (
                other_charges_pound * 100)

            other_expense_pound = other_charges_pound if other_charges_pound > 0 else 0
            other_expense_pence = other_charges_pence if other_charges_pence > 0 else 0

            viewtrip = TripView(trip.when.strftime('%a, %d %b %Y'), viewlegs,
                                round(trip_total_distance, 2), trip.trip_code,
                                trip.key(), waypoints_description,
                                other_expense_label, other_expense_pound,
                                other_expense_pence, trip.passenger_count,
                                trip.passenger_mileage)

            #add viewtrip to report pages
            if tripcount < FIRSTPAGE_TRIPS_COUNT:
                firstpage_trips.append(viewtrip)

            if tripcount >= FIRSTPAGE_TRIPS_COUNT:
                middlepage_trips.append(viewtrip)

            report_total_distance = report_total_distance + trip_total_distance

            report_total_passenger_mileage = report_total_passenger_mileage + (
                trip.passenger_mileage if trip.passenger_mileage else 0)

            tripcount = tripcount + 1

        #split middle pages for report paging
        nextpages = []
        apage = []
        logging.debug('#middlepage_trips ' + str(len(middlepage_trips)))
        for (counter, atrip) in enumerate(middlepage_trips):
            apage.append(atrip)
            if (((counter + 1) % TRIPS_PER_PAGE) == 0):  #counter!= 0 and
                logging.debug('nextpage counter' + str(counter))
                nextpages.append(apage)
                apage = []
        #append all remaining trips
        if apage:
            nextpages.append(apage)

        startdate = time.strptime(startdate, '%Y-%m-%d %H:%M:%S')
        enddate = time.strptime(enddate, '%Y-%m-%d %H:%M:%S')
        startparam = time.strftime('%a, %d %b %Y', startdate)
        endparam = time.strftime('%a, %d %b %Y', enddate)

        try:
            report_other_charges_pound = int(report_total_other_pence / 100)
        except:
            report_other_charges_pound = 0

        if report_total_other_pence > 0:
            report_other_charges_pence = report_total_other_pence - (
                report_other_charges_pound * 100)
        else:
            report_other_charges_pence = 0

        viewreport = ReportView(startparam, endparam,
                                [firstpage_trips, nextpages],
                                round(report_total_distance,
                                      2), report_other_charges_pound,
                                report_other_charges_pence,
                                report_total_passenger_mileage)

        #reusing date for display
        template_values = {
            'reportview': viewreport,
            'userdata': userdata,
        }

        if action == 'download':
            path = os.path.join(os.path.dirname(__file__), '../../templates/',
                                'printable-tripreport.html')
            if userdata.mileage_template == 'BUCKS NHS TRUST':
                path = os.path.join(os.path.dirname(__file__),
                                    '../../templates/reports/',
                                    'bucks_nhs_trust.html')
            elif userdata.mileage_template == 'HILLINGDON PRIMARY CARE TRUST':
                path = os.path.join(os.path.dirname(__file__),
                                    '../../templates/',
                                    'printable-tripreport.html')
            html = template.render(path, template_values)
            logging.debug('Generating pdf...')
            self.pdf(html)
        else:
            path = os.path.join(os.path.dirname(__file__), '../../templates',
                                'tripreport.html')
            html = template.render(path, template_values)
            self.response.out.write(template.render(path, template_values))
Пример #2
0
    def post(self):
        args = self.request.arguments()
        waypoints = []
        distances = []
        when = datetime.now()
        daytrip = DayTrip()

        if self.request.get('trip_key'):
            trip_key = self.request.get('trip_key')
            daytrip = db.get(db.Key(trip_key))
        else:
            if users.get_current_user():
                daytrip.who = users.get_current_user()

        daytrip.when = when

        total_distance = 0
        if self.request.get('total_miles'):
            try:
                total_distance = float(self.request.get('total_miles'))
            except:
                total_distance = 0

        daytrip.total_distance = total_distance

        cpm = 0
        if self.request.get('cost_per_mile_pound'):
            cost_per_mile_pound = int(self.request.get('cost_per_mile_pound'))
            cpm = cost_per_mile_pound * 100

        if self.request.get('cost_per_mile_pence'):
            cost_per_mile_pence = int(self.request.get('cost_per_mile_pence'))
            cpm = cpm + cost_per_mile_pence

        daytrip.pence_per_mile = cpm

        if self.request.get('label_parking_expenses'):
            daytrip.other_expenses_label = self.request.get(
                'label_parking_expenses')
        else:
            daytrip.other_expenses_label = ''

        other_expenses = 0
        if self.request.get('other_expenses_charges_pound'):
            other_expenses_charges_pound = int(
                self.request.get('other_expenses_charges_pound'))
            other_expenses = other_expenses_charges_pound * 100

        if self.request.get('other_expenses_charges_pence'):
            other_expenses_charges_pence = int(
                self.request.get('other_expenses_charges_pence'))
            other_expenses = other_expenses + other_expenses_charges_pence

        daytrip.other_expenses_charge = other_expenses

        if self.request.get('mode_of_travel'):
            daytrip.trip_code = self.request.get('mode_of_travel')
        else:
            daytrip.trip_code = ''

        if self.request.get('other_no_passengers'):
            try:
                daytrip.passenger_count = int(
                    self.request.get('other_no_passengers'))
            except:
                daytrip.passenger_count = 0
        else:
            daytrip.passenger_count = 0

        if self.request.get('other_passenger_miles'):
            try:
                daytrip.passenger_mileage = int(
                    self.request.get('other_passenger_miles'))
            except:
                daytrip.passenger_mileage = 0
        else:
            daytrip.passenger_mileage = 0

        count = 0
        for arg in args:
            argstr = 'input' + str(count)
            legstr = 'leg' + str(count)
            #google maps api only allows 10 addresses
            if count < 10:
                if self.request.get(argstr):
                    waypoints.append(self.request.get(argstr))
                    if self.request.get(legstr):
                        try:
                            distance = int(
                                float(self.request.get(legstr)) * 1609.344)
                        except:
                            distance = 0
                    else:
                        distance = 0
                    distances.append(distance)
            count = count + 1

        if self.request.get('tripdate'):
            tripdate = datetime.strptime(self.request.get('tripdate'),
                                         '%m/%d/%Y')
        else:
            tripdate = None

        if tripdate:
            daytrip.waypoints = waypoints
            daytrip.distances = distances
            daytrip.when = tripdate
            daytrip.put()
            notification = '{"app_notification_type": "app_tip", "app_notification": "Trip saved." }'
            logging.debug(notification)
        else:
            notification = '{"app_notification_type": "app_error", "app_notification": "Trip data incomplete. Please ensure a Trip date is set."}'

        self.response.out.write(notification)
Пример #3
0
    def post(self):
        args = self.request.arguments()
        waypoints = []
        distances = []
        when = datetime.now()
        daytrip = DayTrip()

        if self.request.get('trip_key'):
            trip_key = self.request.get('trip_key')
            daytrip = db.get(db.Key(trip_key))
        else:
            if users.get_current_user():
                daytrip.who = users.get_current_user()

        daytrip.when = when

        total_distance = 0
        if self.request.get('total_miles'):
            try:
                total_distance = float(self.request.get('total_miles'))
            except:
                total_distance = 0
                
        daytrip.total_distance = total_distance
        
        cpm = 0	
        if self.request.get('cost_per_mile_pound'):
            cost_per_mile_pound = int(self.request.get('cost_per_mile_pound'))
            cpm = cost_per_mile_pound * 100

        if self.request.get('cost_per_mile_pence'):
            cost_per_mile_pence = int(self.request.get('cost_per_mile_pence'))
            cpm = cpm + cost_per_mile_pence

        daytrip.pence_per_mile = cpm

        if self.request.get('label_parking_expenses'):
            daytrip.other_expenses_label = self.request.get('label_parking_expenses')
        else:
            daytrip.other_expenses_label = ''

        other_expenses = 0	
        if self.request.get('other_expenses_charges_pound'):
            other_expenses_charges_pound = int(self.request.get('other_expenses_charges_pound'))
            other_expenses = other_expenses_charges_pound * 100

        if self.request.get('other_expenses_charges_pence'):
            other_expenses_charges_pence = int(self.request.get('other_expenses_charges_pence'))
            other_expenses = other_expenses + other_expenses_charges_pence

        daytrip.other_expenses_charge = other_expenses

        if self.request.get('mode_of_travel'):
            daytrip.trip_code = self.request.get('mode_of_travel')
        else:
            daytrip.trip_code = ''


        if self.request.get('other_no_passengers'):
            try:
                daytrip.passenger_count = int(self.request.get('other_no_passengers'))
            except:
                daytrip.passenger_count = 0
        else:
            daytrip.passenger_count = 0	 

        if self.request.get('other_passenger_miles'):
            try:
                daytrip.passenger_mileage = int(self.request.get('other_passenger_miles'))
            except:
                daytrip.passenger_mileage = 0
        else:
            daytrip.passenger_mileage = 0	    

        count = 0        
        for arg in args:
            argstr = 'input'+str(count)
            legstr = 'leg'+str(count)
            #google maps api only allows 10 addresses
            if count < 10:
                if self.request.get(argstr):
                    waypoints.append(self.request.get(argstr))
                    if self.request.get(legstr):
                        try:
                            distance = int(float(self.request.get(legstr)) * 1609.344)
                        except:
                            distance = 0
                    else:
                        distance = 0
                    distances.append(distance)
            count = count+1

        if self.request.get('tripdate'):
            tripdate = datetime.strptime(self.request.get('tripdate'), '%m/%d/%Y')
        else:
            tripdate = None


        if tripdate:
            daytrip.waypoints = waypoints
            daytrip.distances = distances
            daytrip.when = tripdate
            daytrip.put()
            notification = '{"app_notification_type": "app_tip", "app_notification": "Trip saved." }'
            logging.debug(notification)
        else:
            notification = '{"app_notification_type": "app_error", "app_notification": "Trip data incomplete. Please ensure a Trip date is set."}'

        self.response.out.write(notification)
Пример #4
0
    def post(self, action=None):
        if not users.get_current_user():
            self.redirect("/")

        startparam = self.request.get("startdate").strip()
        endparam = self.request.get("enddate").strip()
        dateformat = "%m/%d/%Y"

        if startparam != "" or endparam != "":
            startdate = time.strptime(startparam, dateformat)
            enddate = time.strptime(endparam, dateformat)

        if not startparam or not endparam:
            startdate = time.gmtime()  # time.strftime(dateformat, time.gmtime())
            enddate = time.gmtime()  # time.strftime(dateformat, time.gmtime())

        startdate = time.strftime("%Y-%m-%d %H:%M:%S", startdate)
        enddate = time.strftime("%Y-%m-%d %H:%M:%S", enddate)

        trips = []
        userdata = None
        # AND when >= :start AND when <= :end
        if users.get_current_user():
            user = users.get_current_user()
            trip_user = TripFilesUser.gql("WHERE who =:who", who=user)
            userdata = trip_user.get()
            # if admin user is getting report for another user
            if self.request.get("user_email"):
                user_email = self.request.get("user_email").strip()
                logging.debug("user_email=" + user_email)
                user = User(email=self.request.get("user_email").strip())
                trip_users = TripFilesUser.gql("WHERE who =:who", who=user)
                logging.debug("Count=" + str(trip_users.count()))
                trip_user = trip_users.get()
                logging.debug("Report user="******"WHERE who = :who AND when >= DATETIME(:start) AND when <= DATETIME(:end) ORDER BY when ASC",
                who=user,
                start=startdate,
                end=enddate,
            )

        else:
            self.redirect("/")

        viewtrips = []
        report_total_distance = 0.0
        report_total_other_pound = 0
        report_total_other_pence = 0
        report_total_passenger_mileage = 0

        tripcount = 0
        firstpage_trips = []
        middlepage_trips = []  # trips in each middlepage
        for trip in trips:
            viewlegs = []
            trip_total_distance = 0.0
            count = len(trip.waypoints)
            waypoints_description = ""
            for i in xrange(count - 1):
                distance = (trip.distances[i + 1]) * 0.000621371192
                distance = round(distance, 2)
                viewleg = TripLegView(
                    trip.waypoints[i], trip.waypoints[i + 1], distance, trip.when.strftime("%a, %d %b %Y"), i + 1
                )
                viewlegs.append(viewleg)
                trip_total_distance = trip_total_distance + distance
                if i == 0:
                    waypoints_description = trip.waypoints[i].upper() + " - " + trip.waypoints[i + 1].upper()
                else:
                    waypoints_description = waypoints_description.upper() + " - " + trip.waypoints[i + 1].upper()

            # if we have total_distance saved, use it since the user may have updated the total manually
            if trip.total_distance and trip.total_distance > 0:
                trip_total_distance = trip.total_distance

            other_expense_label = trip.other_expenses_label
            try:
                other_charges_pound = int(trip.other_expenses_charge / 100)
            except:
                other_charges_pound = 0

            if trip.other_expenses_charge > 0:
                other_charges_pence = trip.other_expenses_charge - (other_charges_pound * 100)
            else:
                other_charges_pence = 0

            report_total_other_pence = report_total_other_pence + other_charges_pence + (other_charges_pound * 100)

            other_expense_pound = other_charges_pound if other_charges_pound > 0 else 0
            other_expense_pence = other_charges_pence if other_charges_pence > 0 else 0

            viewtrip = TripView(
                trip.when.strftime("%a, %d %b %Y"),
                viewlegs,
                round(trip_total_distance, 2),
                trip.trip_code,
                trip.key(),
                waypoints_description,
                other_expense_label,
                other_expense_pound,
                other_expense_pence,
                trip.passenger_count,
                trip.passenger_mileage,
            )

            # add viewtrip to report pages
            if tripcount < FIRSTPAGE_TRIPS_COUNT:
                firstpage_trips.append(viewtrip)

            if tripcount >= FIRSTPAGE_TRIPS_COUNT:
                middlepage_trips.append(viewtrip)

            report_total_distance = report_total_distance + trip_total_distance

            report_total_passenger_mileage = report_total_passenger_mileage + (
                trip.passenger_mileage if trip.passenger_mileage else 0
            )

            tripcount = tripcount + 1

        # split middle pages for report paging
        nextpages = []
        apage = []
        logging.debug("#middlepage_trips " + str(len(middlepage_trips)))
        for (counter, atrip) in enumerate(middlepage_trips):
            apage.append(atrip)
            if ((counter + 1) % TRIPS_PER_PAGE) == 0:  # counter!= 0 and
                logging.debug("nextpage counter" + str(counter))
                nextpages.append(apage)
                apage = []
        # append all remaining trips
        if apage:
            nextpages.append(apage)

        startdate = time.strptime(startdate, "%Y-%m-%d %H:%M:%S")
        enddate = time.strptime(enddate, "%Y-%m-%d %H:%M:%S")
        startparam = time.strftime("%a, %d %b %Y", startdate)
        endparam = time.strftime("%a, %d %b %Y", enddate)

        try:
            report_other_charges_pound = int(report_total_other_pence / 100)
        except:
            report_other_charges_pound = 0

        if report_total_other_pence > 0:
            report_other_charges_pence = report_total_other_pence - (report_other_charges_pound * 100)
        else:
            report_other_charges_pence = 0

        viewreport = ReportView(
            startparam,
            endparam,
            [firstpage_trips, nextpages],
            round(report_total_distance, 2),
            report_other_charges_pound,
            report_other_charges_pence,
            report_total_passenger_mileage,
        )

        # reusing date for display
        template_values = {"reportview": viewreport, "userdata": userdata}

        if action == "download":
            path = os.path.join(os.path.dirname(__file__), "../../templates/", "printable-tripreport.html")
            if userdata.mileage_template == "BUCKS NHS TRUST":
                path = os.path.join(os.path.dirname(__file__), "../../templates/reports/", "bucks_nhs_trust.html")
            elif userdata.mileage_template == "HILLINGDON PRIMARY CARE TRUST":
                path = os.path.join(os.path.dirname(__file__), "../../templates/", "printable-tripreport.html")
            html = template.render(path, template_values)
            logging.debug("Generating pdf...")
            self.pdf(html)
        else:
            path = os.path.join(os.path.dirname(__file__), "../../templates", "tripreport.html")
            html = template.render(path, template_values)
            self.response.out.write(template.render(path, template_values))